Browse Source

Added PTR record data

develop
Robin Thoni 6 years ago
parent
commit
1d492d6e70

+ 1
- 1
src/modules/dns-base/CMakeLists.txt View File

@@ -30,7 +30,7 @@ add_library(${PROJECT_NAME}
30 30
         src/RecordData/QDnsRecordDataRaw.cpp
31 31
         includes/dns-base/RecordData/QDnsRecordDataRaw.h
32 32
         src/RecordData/QDnsRecordDataAAAA.cpp
33
-        includes/dns-base/RecordData/QDnsRecordDataAAAA.h src/RecordData/QDnsRecordDataMX.cpp includes/dns-base/RecordData/QDnsRecordDataMX.h src/RecordData/QDnsRecordDataSOA.cpp includes/dns-base/RecordData/QDnsRecordDataSOA.h)
33
+        includes/dns-base/RecordData/QDnsRecordDataAAAA.h src/RecordData/QDnsRecordDataMX.cpp includes/dns-base/RecordData/QDnsRecordDataMX.h src/RecordData/QDnsRecordDataSOA.cpp includes/dns-base/RecordData/QDnsRecordDataSOA.h src/RecordData/QDnsRecordDataPTR.cpp includes/dns-base/RecordData/QDnsRecordDataPTR.h)
34 34
 
35 35
 target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Network)
36 36
 

+ 46
- 0
src/modules/dns-base/includes/dns-base/RecordData/QDnsRecordDataPTR.h View File

@@ -0,0 +1,46 @@
1
+//
2
+// Created by robin on 1/23/18.
3
+//
4
+
5
+#ifndef QDNSAGG_QDNSRECORDDATAPTR_H
6
+#define QDNSAGG_QDNSRECORDDATAPTR_H
7
+
8
+#include <qplatformdefs.h>
9
+#include <QByteArray>
10
+#include <QDataStream>
11
+#include <QHostAddress>
12
+
13
+#include "QDnsRecordData.h"
14
+
15
+namespace QDns
16
+{
17
+    namespace Base
18
+    {
19
+        class QDnsRecordDataPTR : public QDnsRecordData
20
+        {
21
+        public:
22
+            QDnsRecordDataPTR();
23
+
24
+            QDnsRecordDataPTR(const QByteArray &packetData, quint32 pos);
25
+
26
+            QDnsRecordDataPTR(const QByteArray &packetData, QDataStream &dataStream);
27
+
28
+            QByteArray serialize() const override;
29
+
30
+            void serialize(QDataStream &dataStream) const override;
31
+
32
+            virtual QDnsRecordData* clone() const override;
33
+
34
+            const QString &getHostname() const;
35
+
36
+            void setHostname(const QString &hostname);
37
+
38
+        private:
39
+            void parse(const QByteArray &packetData, QDataStream &dataStream);
40
+
41
+            QString m_hostname;
42
+        };
43
+    }
44
+}
45
+
46
+#endif //QDNSAGG_QDNSRECORDDATAPTR_H

+ 78
- 0
src/modules/dns-base/src/RecordData/QDnsRecordDataPTR.cpp View File

@@ -0,0 +1,78 @@
1
+//
2
+// Created by robin on 1/23/18.
3
+//
4
+
5
+#include "dns-base/QDnsLabel.h"
6
+#include "dns-base/RecordData/QDnsRecordDataPTR.h"
7
+
8
+namespace QDns
9
+{
10
+    namespace Base
11
+    {
12
+        QDnsRecordDataPTR::QDnsRecordDataPTR()
13
+                : QDnsRecordData()
14
+        {
15
+        }
16
+
17
+        QDnsRecordDataPTR::QDnsRecordDataPTR(const QByteArray &packetData, quint32 pos)
18
+                : QDnsRecordDataPTR()
19
+        {
20
+            QDataStream dataStream(packetData);
21
+            dataStream.skipRawData(pos);
22
+            parse(packetData, dataStream);
23
+        }
24
+
25
+        QDnsRecordDataPTR::QDnsRecordDataPTR(const QByteArray &packetData, QDataStream &dataStream)
26
+                : QDnsRecordDataPTR()
27
+        {
28
+            parse(packetData, dataStream);
29
+        }
30
+
31
+        void QDnsRecordDataPTR::parse(const QByteArray &packetData, QDataStream &dataStream)
32
+        {
33
+            if (dataStream.device()->bytesAvailable() < 1)
34
+            {
35
+                m_isValid = false;
36
+            }
37
+            else
38
+            {
39
+                m_hostname = QDnsLabel::parse(packetData, dataStream);
40
+                if (m_hostname.isNull())
41
+                {
42
+                    m_isValid = false;
43
+                }
44
+            }
45
+        }
46
+
47
+        QByteArray QDnsRecordDataPTR::serialize() const
48
+        {
49
+            QByteArray byteArray;
50
+            QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
51
+            serialize(dataStream);
52
+            return byteArray;
53
+        }
54
+
55
+        void QDnsRecordDataPTR::serialize(QDataStream &dataStream) const
56
+        {
57
+            QDnsLabel::serialize(m_hostname, dataStream);
58
+        }
59
+
60
+        QDnsRecordData *QDnsRecordDataPTR::clone() const
61
+        {
62
+            auto data = new QDnsRecordDataPTR();
63
+            data->m_isValid = m_isValid;
64
+            data->m_hostname = m_hostname;
65
+            return data;
66
+        }
67
+
68
+        const QString &QDnsRecordDataPTR::getHostname() const
69
+        {
70
+            return m_hostname;
71
+        }
72
+
73
+        void QDnsRecordDataPTR::setHostname(const QString &hostname)
74
+        {
75
+            m_hostname = hostname;
76
+        }
77
+    }
78
+}

+ 2
- 1
src/modules/dns-base/tests/CMakeLists.txt View File

@@ -11,7 +11,8 @@ add_executable(${PROJECT_NAME}
11 11
         RecordData/DnsRDataA.cpp
12 12
         RecordData/DnsRDataAAAA.cpp
13 13
         RecordData/DnsRDataMX.cpp
14
-        RecordData/DnsRDataSOA.cpp)
14
+        RecordData/DnsRDataSOA.cpp
15
+        RecordData/DnsRDataPTR.cpp)
15 16
 
16 17
 target_link_libraries(${PROJECT_NAME} gtest pthread qdnsagg-dns-base)
17 18
 

+ 45
- 0
src/modules/dns-base/tests/RecordData/DnsRDataPTR.cpp View File

@@ -0,0 +1,45 @@
1
+//
2
+// Created by robin on 1/14/18.
3
+//
4
+
5
+#include <gtest/gtest.h>
6
+#include <QByteArray>
7
+#include <QString>
8
+#include <QtNetwork/QHostAddress>
9
+#include <dns-base/RecordData/QDnsRecordDataPTR.h>
10
+
11
+struct DnsRDataPTRTestParams
12
+{
13
+    bool isValid;
14
+    const char* hexData;
15
+    quint32 pos;
16
+
17
+    QString hostname;
18
+};
19
+
20
+class DnsRDataPTRTest : public ::testing::TestWithParam<DnsRDataPTRTestParams>
21
+{
22
+};
23
+
24
+TEST_P(DnsRDataPTRTest, parseAndGetters)
25
+{
26
+    auto params = GetParam();
27
+    auto data = QByteArray::fromHex(params.hexData);
28
+    auto rdata = QDns::Base::QDnsRecordDataPTR(data, params.pos);
29
+    ASSERT_EQ(rdata.isValid(), params.isValid);
30
+
31
+    if (rdata.isValid())
32
+    {
33
+        ASSERT_EQ(params.hostname, rdata.getHostname());
34
+
35
+        ASSERT_EQ(rdata.serialize().toHex(), QString(params.hexData).remove(" "));
36
+    }
37
+}
38
+
39
+INSTANTIATE_TEST_CASE_P(DnsRDataPTRTestInst,
40
+                        DnsRDataPTRTest,
41
+                        ::testing::Values(
42
+                                DnsRDataPTRTestParams {false, "", 0},
43
+                                DnsRDataPTRTestParams {true, "00", 0, ""},
44
+                                DnsRDataPTRTestParams {true, "03777777067274686f6e6903636f6d00", 0, "www.rthoni.com"}
45
+                        ));

Loading…
Cancel
Save