Browse Source

Added base record data and raw/A

develop
Robin Thoni 6 years ago
parent
commit
4517d17c41

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

@@ -9,6 +9,7 @@ set(CMAKE_AUTOMOC ON)
9 9
 set(CMAKE_CXX_STANDARD 17)
10 10
 
11 11
 find_package(Qt5Core REQUIRED)
12
+find_package(Qt5Network REQUIRED)
12 13
 
13 14
 add_library(${PROJECT_NAME}
14 15
         src/QDnsHeader.cpp
@@ -22,10 +23,14 @@ add_library(${PROJECT_NAME}
22 23
         src/QDnsPacket.cpp
23 24
         includes/dns-base/QDnsPacket.h
24 25
         includes/dns-base/QDnsBase.h
25
-        src/RecordData/QDnsRDataDecoder.cpp
26
-        includes/dns-base/RecordData/QDnsRDataDecoder.h src/RecordData/QDnsRDataA.cpp includes/dns-base/RecordData/QDnsRDataA.h)
26
+        src/RecordData/QDnsRecordData.cpp
27
+        includes/dns-base/RecordData/QDnsRecordData.h
28
+        src/RecordData/QDnsRecordDataA.cpp
29
+        includes/dns-base/RecordData/QDnsRecordDataA.h
30
+        src/RecordData/QDnsRecordDataRaw.cpp
31
+        includes/dns-base/RecordData/QDnsRecordDataRaw.h)
27 32
 
28
-target_link_libraries(${PROJECT_NAME} Qt5::Core)
33
+target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Network)
29 34
 
30 35
 find_package(GTest)
31 36
 if (GTEST_FOUND)

+ 9
- 3
src/modules/dns-base/includes/dns-base/QDnsResourceRecord.h View File

@@ -8,6 +8,7 @@
8 8
 #include <qplatformdefs.h>
9 9
 #include <QByteArray>
10 10
 #include <QDataStream>
11
+#include "dns-base/RecordData/QDnsRecordData.h"
11 12
 
12 13
 namespace QDns
13 14
 {
@@ -22,6 +23,10 @@ namespace QDns
22 23
 
23 24
             QDnsResourceRecord(const QByteArray &packetData, QDataStream &dataStream);
24 25
 
26
+            QDnsResourceRecord(const QDnsResourceRecord& other);
27
+
28
+            QDnsResourceRecord & operator=(const QDnsResourceRecord& other);
29
+
25 30
             virtual ~QDnsResourceRecord() = default;
26 31
 
27 32
             QByteArray serialize() const;
@@ -46,11 +51,12 @@ namespace QDns
46 51
 
47 52
             void setTtl(quint32 ttl);
48 53
 
49
-            const QByteArray &getData() const;
54
+            QDnsRecordData* getData() const;
50 55
 
51
-            void setData(const QByteArray &data);
56
+            void setData(QDnsRecordData* data);
52 57
 
53 58
         private:
59
+            void copy(const QDnsResourceRecord& other);
54 60
             void parse(const QByteArray &packetData, QDataStream &dataStream);
55 61
 
56 62
             bool m_isValid;
@@ -58,7 +64,7 @@ namespace QDns
58 64
             quint16 m_type;
59 65
             quint16 m_class;
60 66
             quint32 m_ttl;
61
-            QByteArray m_data;
67
+            QScopedPointer<QDnsRecordData> m_data;
62 68
         };
63 69
     }
64 70
 }

+ 35
- 0
src/modules/dns-base/includes/dns-base/RecordData/QDnsRecordData.h View File

@@ -0,0 +1,35 @@
1
+//
2
+// Created by robin on 1/12/18.
3
+//
4
+
5
+#ifndef QDNSAGG_QDNSRECORDDATADECODER_H
6
+#define QDNSAGG_QDNSRECORDDATADECODER_H
7
+
8
+#include <QByteArray>
9
+
10
+namespace QDns
11
+{
12
+    namespace Base
13
+    {
14
+        class QDnsRecordData
15
+        {
16
+        public:
17
+            QDnsRecordData();
18
+
19
+            virtual ~QDnsRecordData() = default;
20
+
21
+            virtual QByteArray serialize() const = 0;
22
+
23
+            virtual void serialize(QDataStream &dataStream) const = 0;
24
+
25
+            bool isValid() const;
26
+
27
+            virtual QDnsRecordData* clone() const = 0;
28
+
29
+        protected:
30
+            bool m_isValid;
31
+        };
32
+    }
33
+}
34
+
35
+#endif //QDNSAGG_QDNSRECORDDATADECODER_H

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

@@ -0,0 +1,46 @@
1
+//
2
+// Created by robin on 1/12/18.
3
+//
4
+
5
+#ifndef QDNSAGG_QDNSRDATAA_H
6
+#define QDNSAGG_QDNSRDATAA_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 QDnsRecordDataA : public QDnsRecordData
20
+        {
21
+        public:
22
+            QDnsRecordDataA();
23
+
24
+            QDnsRecordDataA(const QByteArray &packetData, quint32 pos);
25
+
26
+            QDnsRecordDataA(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 QHostAddress &getIp() const;
35
+
36
+            void setIp(const QHostAddress &ip);
37
+
38
+        private:
39
+            void parse(const QByteArray &packetData, QDataStream &dataStream);
40
+
41
+            QHostAddress m_ip;
42
+        };
43
+    }
44
+}
45
+
46
+#endif //QDNSAGG_QDNSRDATAA_H

+ 41
- 0
src/modules/dns-base/includes/dns-base/RecordData/QDnsRecordDataRaw.h View File

@@ -0,0 +1,41 @@
1
+//
2
+// Created by robin on 1/14/18.
3
+//
4
+
5
+#ifndef QDNSAGG_QDNSRECORDDATARAW_H
6
+#define QDNSAGG_QDNSRECORDDATARAW_H
7
+
8
+#include "QDnsRecordData.h"
9
+
10
+namespace QDns
11
+{
12
+    namespace Base
13
+    {
14
+        class QDnsRecordDataRaw : public QDnsRecordData
15
+        {
16
+        public:
17
+            QDnsRecordDataRaw();
18
+
19
+            QDnsRecordDataRaw(const QByteArray &packetData, quint32 pos);
20
+
21
+            QDnsRecordDataRaw(const QByteArray &packetData, QDataStream &dataStream);
22
+
23
+            QByteArray serialize() const override;
24
+
25
+            void serialize(QDataStream &dataStream) const override;
26
+
27
+            virtual QDnsRecordData* clone() const override;
28
+
29
+            const QByteArray &getData() const;
30
+
31
+            void setData(const QByteArray &data);
32
+
33
+        private:
34
+            void parse(const QByteArray &packetData, QDataStream &dataStream);
35
+
36
+            QByteArray m_data;
37
+        };
38
+    }
39
+}
40
+
41
+#endif //QDNSAGG_QDNSRECORDDATARAW_H

+ 35
- 7
src/modules/dns-base/src/QDnsResourceRecord.cpp View File

@@ -2,6 +2,7 @@
2 2
 // Created by robin on 1/10/18.
3 3
 //
4 4
 
5
+#include "dns-base/RecordData/QDnsRecordDataRaw.h"
5 6
 #include "dns-base/QDnsLabel.h"
6 7
 #include "dns-base/QDnsResourceRecord.h"
7 8
 
@@ -11,6 +12,7 @@ namespace QDns
11 12
     {
12 13
         QDnsResourceRecord::QDnsResourceRecord()
13 14
                 : m_isValid(true)
15
+                , m_data(0)
14 16
         {
15 17
         }
16 18
 
@@ -28,6 +30,27 @@ namespace QDns
28 30
             parse(packetData, dataStream);
29 31
         }
30 32
 
33
+        QDnsResourceRecord::QDnsResourceRecord(const QDnsResourceRecord &other)
34
+        {
35
+            copy(other);
36
+        }
37
+
38
+        QDnsResourceRecord &QDnsResourceRecord::operator=(const QDnsResourceRecord& other)
39
+        {
40
+            copy(other);
41
+            return *this;
42
+        }
43
+
44
+        void QDnsResourceRecord::copy(const QDnsResourceRecord &other)
45
+        {
46
+            m_isValid = other.m_isValid;
47
+            m_name = other.m_name;
48
+            m_type = other.m_type;
49
+            m_class = other.m_class;
50
+            m_ttl = other.m_ttl;
51
+            m_data.reset(other.getData()->clone());
52
+        }
53
+
31 54
         void QDnsResourceRecord::parse(const QByteArray &packetData, QDataStream &dataStream)
32 55
         {
33 56
             m_name = QDnsLabel::parse(packetData, dataStream);
@@ -50,7 +73,9 @@ namespace QDns
50 73
                 {
51 74
                     char data[length];
52 75
                     dataStream.readRawData(data, length);
53
-                    m_data.append(data, length);
76
+                    QByteArray byteArray(data, length);
77
+                    QDataStream dataStream2(byteArray);
78
+                    m_data.reset(new QDnsRecordDataRaw(packetData, dataStream2));
54 79
                 }
55 80
             }
56 81
         }
@@ -70,8 +95,11 @@ namespace QDns
70 95
             dataStream << m_type;
71 96
             dataStream << m_class;
72 97
             dataStream << m_ttl;
73
-            dataStream << (quint16) m_data.length();
74
-            dataStream.writeRawData(m_data.constData(), m_data.length());
98
+            QByteArray byteArray;
99
+            QDataStream dataStream2(&byteArray, QIODevice::WriteOnly);
100
+            m_data->serialize(dataStream2);
101
+            dataStream << (quint16) byteArray.length();
102
+            dataStream.writeRawData(byteArray.constData(), byteArray.length());
75 103
         }
76 104
 
77 105
         bool QDnsResourceRecord::isValid() const
@@ -119,14 +147,14 @@ namespace QDns
119 147
             m_ttl = ttl;
120 148
         }
121 149
 
122
-        const QByteArray &QDnsResourceRecord::getData() const
150
+        QDnsRecordData* QDnsResourceRecord::getData() const
123 151
         {
124
-            return m_data;
152
+            return m_data.data();
125 153
         }
126 154
 
127
-        void QDnsResourceRecord::setData(const QByteArray &data)
155
+        void QDnsResourceRecord::setData(QDnsRecordData* data)
128 156
         {
129
-            m_data = data;
157
+            m_data.reset(data);
130 158
         }
131 159
     }
132 160
 }

+ 22
- 0
src/modules/dns-base/src/RecordData/QDnsRecordData.cpp View File

@@ -0,0 +1,22 @@
1
+//
2
+// Created by robin on 1/12/18.
3
+//
4
+
5
+#include "dns-base/RecordData/QDnsRecordData.h"
6
+
7
+
8
+namespace QDns
9
+{
10
+    namespace Base
11
+    {
12
+        QDnsRecordData::QDnsRecordData()
13
+            : m_isValid(true)
14
+        {
15
+        }
16
+
17
+        bool QDnsRecordData::isValid() const
18
+        {
19
+            return m_isValid;
20
+        }
21
+    }
22
+}

+ 75
- 0
src/modules/dns-base/src/RecordData/QDnsRecordDataA.cpp View File

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

+ 72
- 0
src/modules/dns-base/src/RecordData/QDnsRecordDataRaw.cpp View File

@@ -0,0 +1,72 @@
1
+//
2
+// Created by robin on 1/14/18.
3
+//
4
+
5
+#include <QIODevice>
6
+#include <QDataStream>
7
+#include "dns-base/RecordData/QDnsRecordDataRaw.h"
8
+
9
+namespace QDns
10
+{
11
+    namespace Base
12
+    {
13
+
14
+        QDnsRecordDataRaw::QDnsRecordDataRaw()
15
+            : QDnsRecordData()
16
+        {
17
+        }
18
+
19
+        QDnsRecordDataRaw::QDnsRecordDataRaw(const QByteArray &packetData, quint32 pos)
20
+        {
21
+            QDataStream dataStream(packetData);
22
+            dataStream.skipRawData(pos);
23
+            parse(packetData, dataStream);
24
+        }
25
+
26
+        QDnsRecordDataRaw::QDnsRecordDataRaw(const QByteArray &packetData, QDataStream &dataStream)
27
+        {
28
+            parse(packetData, dataStream);
29
+        }
30
+
31
+        QByteArray QDnsRecordDataRaw::serialize() const
32
+        {
33
+            QByteArray byteArray;
34
+            QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
35
+            serialize(dataStream);
36
+            return byteArray;
37
+        }
38
+
39
+        void QDnsRecordDataRaw::serialize(QDataStream &dataStream) const
40
+        {
41
+            dataStream.writeRawData(m_data.constData(), m_data.length());
42
+        }
43
+
44
+        QDnsRecordData *QDnsRecordDataRaw::clone() const
45
+        {
46
+            auto data = new QDnsRecordDataRaw();
47
+            data->m_isValid = m_isValid;
48
+            data->m_data = m_data;
49
+            return data;
50
+        }
51
+
52
+        void QDnsRecordDataRaw::parse(const QByteArray &packetData, QDataStream &dataStream)
53
+        {
54
+            while (!dataStream.atEnd())
55
+            {
56
+                char data[128];
57
+                auto read = dataStream.readRawData(data, sizeof(data));
58
+                m_data.append(data, read);
59
+            }
60
+        }
61
+
62
+        const QByteArray &QDnsRecordDataRaw::getData() const
63
+        {
64
+            return m_data;
65
+        }
66
+
67
+        void QDnsRecordDataRaw::setData(const QByteArray &data)
68
+        {
69
+            m_data = data;
70
+        }
71
+    }
72
+}

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

@@ -7,7 +7,8 @@ add_executable(${PROJECT_NAME}
7 7
         DnsLabel.cpp
8 8
         DnsQuery.cpp
9 9
         DnsResourceRecord.cpp
10
-        DnsPacket.cpp)
10
+        DnsPacket.cpp
11
+        RecordData/DnsRDataA.cpp)
11 12
 
12 13
 target_link_libraries(${PROJECT_NAME} gtest pthread qdnsagg-dns-base)
13 14
 

+ 6
- 2
src/modules/dns-base/tests/DnsResourceRecord.cpp View File

@@ -7,6 +7,7 @@
7 7
 #include <QString>
8 8
 
9 9
 #include "dns-base/QDnsResourceRecord.h"
10
+#include "dns-base/RecordData/QDnsRecordDataRaw.h"
10 11
 
11 12
 struct DnsResourceRecordParseTestParams
12 13
 {
@@ -38,7 +39,7 @@ TEST_P(DnsResourceRecordParseTest, parse)
38 39
          ASSERT_EQ(rr.getType(), params.type);
39 40
          ASSERT_EQ(rr.getClass(), params.clazz);
40 41
          ASSERT_EQ(rr.getTtl(), params.ttl);
41
-         ASSERT_EQ(rr.getData().toHex(), QString(params.hexData2).remove(" "));
42
+         ASSERT_EQ(rr.getData()->serialize().toHex(), QString(params.hexData2).remove(" "));
42 43
     }
43 44
 }
44 45
 
@@ -100,7 +101,10 @@ TEST_P(DnsResourceRecordSerializeTest, serialize)
100 101
     rr.setType(params.type);
101 102
     rr.setClass(params.clazz);
102 103
     rr.setTtl(params.ttl);
103
-    rr.setData(QByteArray::fromHex(params.hexData2));
104
+
105
+    auto data = new QDns::Base::QDnsRecordDataRaw();
106
+    data->setData(QByteArray::fromHex(params.hexData2));
107
+    rr.setData(data);
104 108
 
105 109
     ASSERT_EQ(rr.serialize().toHex(), QString(params.hexData).remove(" "));
106 110
 }

+ 46
- 0
src/modules/dns-base/tests/RecordData/DnsRDataA.cpp View File

@@ -0,0 +1,46 @@
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/QDnsRecordDataA.h>
10
+
11
+struct DnsRDataATestParams
12
+{
13
+    bool isValid;
14
+    const char* hexData;
15
+    quint32 pos;
16
+
17
+    quint32 ip;
18
+};
19
+
20
+class DnsRDataATest : public ::testing::TestWithParam<DnsRDataATestParams>
21
+{
22
+};
23
+
24
+TEST_P(DnsRDataATest, parseAndGetters)
25
+{
26
+    auto params = GetParam();
27
+    auto data = QByteArray::fromHex(params.hexData);
28
+    auto rdata = QDns::Base::QDnsRecordDataA(data, params.pos);
29
+    ASSERT_EQ(rdata.isValid(), params.isValid);
30
+    
31
+    if (rdata.isValid())
32
+    {
33
+        ASSERT_EQ(rdata.getIp().toIPv4Address(), params.ip);
34
+        
35
+        ASSERT_EQ(rdata.serialize().toHex(), QString(params.hexData).remove(" "));
36
+    }
37
+}
38
+
39
+INSTANTIATE_TEST_CASE_P(DnsRDataATestInst,
40
+                        DnsRDataATest,
41
+                        ::testing::Values(
42
+                                DnsRDataATestParams {false, "", 0},
43
+                                DnsRDataATestParams {false, "00", 0},
44
+                                DnsRDataATestParams {true, "acd913e3", 0, QHostAddress("172.217.19.227").toIPv4Address()},
45
+                                DnsRDataATestParams {true, "d83ad3a3", 0, QHostAddress("216.58.211.163").toIPv4Address()}
46
+                        ));

Loading…
Cancel
Save