ソースを参照

Added DnsQuery and associated tests; modified DnsLabel to use QDataStream

develop
Robin Thoni 6年前
コミット
3d48e3dcd6

+ 2
- 0
src/modules/dns/CMakeLists.txt ファイルの表示

@@ -8,6 +8,8 @@ add_library(qdnsagg-dns
8 8
         includes/dns/QDnsHeader.h
9 9
         src/QDnsLabel.cpp
10 10
         includes/dns/QDnsLabel.h
11
+        src/QDnsQuery.cpp
12
+        includes/dns/QDnsQuery.h
11 13
         )
12 14
 
13 15
 target_link_libraries(qdnsagg-dns Qt5::Core Qt5::Network)

+ 2
- 1
src/modules/dns/includes/dns/QDnsLabel.h ファイルの表示

@@ -15,7 +15,8 @@ namespace QDns
15 15
     public:
16 16
         QDnsLabel() = delete;
17 17
 
18
-        static QString parse(const QByteArray& data, quint32 pos, quint8 maxRecursion = 5);
18
+        static QString parse(const QByteArray& packetData, quint32 pos, quint8 maxRecursion = 5);
19
+        static QString parse(const QByteArray& packetData, QDataStream& dataStream, quint8 maxRecursion = 5);
19 20
     };
20 21
 
21 22
 }

+ 43
- 0
src/modules/dns/includes/dns/QDnsQuery.h ファイルの表示

@@ -0,0 +1,43 @@
1
+//
2
+// Created by robin on 1/8/18.
3
+//
4
+
5
+#ifndef QDNSAGG_QDNSQUERY_H
6
+#define QDNSAGG_QDNSQUERY_H
7
+
8
+#include <qplatformdefs.h>
9
+#include <QString>
10
+
11
+namespace QDns
12
+{
13
+
14
+    class QDnsQuery
15
+    {
16
+    public:
17
+        QDnsQuery();
18
+
19
+        static QDnsQuery parse(const QByteArray& packetData, quint32 pos);
20
+        QByteArray serialize(const QByteArray& data) const;
21
+
22
+        bool isValid() const;
23
+
24
+        const QString& getName() const;
25
+        void setName(const QString& name);
26
+
27
+        quint16 getType() const;
28
+        void setType(quint16 type);
29
+
30
+        quint16 getClass() const;
31
+        void setClass(quint16 aClass);
32
+
33
+    private:
34
+        bool m_isValid;
35
+
36
+        QString m_name;
37
+        quint16 m_type;
38
+        quint16 m_class;
39
+    };
40
+
41
+}
42
+
43
+#endif //QDNSAGG_QDNSQUERY_H

+ 11
- 4
src/modules/dns/src/QDnsLabel.cpp ファイルの表示

@@ -8,7 +8,14 @@
8 8
 namespace QDns
9 9
 {
10 10
 
11
-    QString QDnsLabel::parse(const QByteArray& data, quint32 pos, quint8 maxRecursion)
11
+    QString QDnsLabel::parse(const QByteArray& packetData, quint32 pos, quint8 maxRecursion)
12
+    {
13
+        QDataStream dataStream(packetData);
14
+        dataStream.skipRawData(pos);
15
+        return parse(packetData, dataStream, maxRecursion);
16
+    }
17
+
18
+    QString QDnsLabel::parse(const QByteArray& packetData, QDataStream& dataStream, quint8 maxRecursion)
12 19
     {
13 20
         if (maxRecursion == 0)
14 21
         {
@@ -16,8 +23,6 @@ namespace QDns
16 23
         }
17 24
 
18 25
         QString fullLabel = "";
19
-        QDataStream dataStream(data);
20
-        dataStream.skipRawData(pos);
21 26
 
22 27
         if (dataStream.device()->bytesAvailable() < 1)
23 28
         {
@@ -37,7 +42,9 @@ namespace QDns
37 42
                 quint8 labelSize2;
38 43
                 dataStream >> labelSize2;
39 44
                 quint16 pointer = ((labelSize << 8) | labelSize2) - (quint16) 49152;
40
-                auto labelEnd = parse(data, pointer, maxRecursion - (quint8) 1);
45
+                QDataStream dataStream2(packetData);
46
+                dataStream2.skipRawData(pointer);
47
+                auto labelEnd = parse(packetData, dataStream2, maxRecursion - (quint8) 1);
41 48
                 if (labelEnd.isNull())
42 49
                 {
43 50
                     return QString::null;

+ 88
- 0
src/modules/dns/src/QDnsQuery.cpp ファイルの表示

@@ -0,0 +1,88 @@
1
+//
2
+// Created by robin on 1/8/18.
3
+//
4
+
5
+#include <QDataStream>
6
+#include <includes/dns/QDnsLabel.h>
7
+#include "dns/QDnsQuery.h"
8
+
9
+namespace QDns
10
+{
11
+
12
+    QDnsQuery::QDnsQuery()
13
+        : m_isValid(true)
14
+        , m_type(0)
15
+        , m_class(0)
16
+    {
17
+    }
18
+
19
+    QDnsQuery QDnsQuery::parse(const QByteArray& packetData, quint32 pos)
20
+    {
21
+        QDnsQuery query;
22
+        QDataStream dataStream(packetData);
23
+        dataStream.skipRawData(pos);
24
+
25
+        if (dataStream.device()->bytesAvailable() < 5)
26
+        {
27
+            query.m_isValid = false;
28
+        }
29
+        else
30
+        {
31
+            query.m_name = QDnsLabel::parse(packetData, dataStream);
32
+            if (query.m_name.isNull() || dataStream.device()->bytesAvailable() < 4)
33
+            {
34
+                query.m_isValid = false;
35
+            }
36
+            else
37
+            {
38
+                dataStream >> query.m_type;
39
+                dataStream >> query.m_class;
40
+            }
41
+
42
+        }
43
+        return query;
44
+    }
45
+
46
+    QByteArray QDnsQuery::serialize(const QByteArray& data) const // TODO
47
+    {
48
+        QByteArray byteArray;
49
+        QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
50
+
51
+        return byteArray;
52
+    }
53
+
54
+    bool QDnsQuery::isValid() const
55
+    {
56
+        return m_isValid;
57
+    }
58
+
59
+    const QString& QDnsQuery::getName() const
60
+    {
61
+        return m_name;
62
+    }
63
+
64
+    void QDnsQuery::setName(const QString& name)
65
+    {
66
+        m_name = name;
67
+    }
68
+
69
+    quint16 QDnsQuery::getType() const
70
+    {
71
+        return m_type;
72
+    }
73
+
74
+    void QDnsQuery::setType(quint16 type)
75
+    {
76
+        m_type = type;
77
+    }
78
+
79
+    quint16 QDnsQuery::getClass() const
80
+    {
81
+        return m_class;
82
+    }
83
+
84
+    void QDnsQuery::setClass(quint16 aClass)
85
+    {
86
+        m_class = aClass;
87
+    }
88
+}

+ 1
- 1
tests/CMakeLists.txt ファイルの表示

@@ -9,7 +9,7 @@ add_executable(qdnsagg-tests
9 9
         main.cpp
10 10
         DnsHeader.cpp
11 11
         DnsLabel.cpp
12
-        )
12
+        DnsQuery.cpp)
13 13
 
14 14
 target_link_libraries(qdnsagg-tests gtest pthread qdnsagg-dns)
15 15
 add_test(qdnsagg-tests ${CMAKE_CURRENT_BINARY_DIR}/qdnsagg-tests)

+ 67
- 0
tests/DnsQuery.cpp ファイルの表示

@@ -0,0 +1,67 @@
1
+//
2
+// Created by robin on 1/9/18.
3
+//
4
+
5
+#include <gtest/gtest.h>
6
+#include <QByteArray>
7
+#include <QString>
8
+
9
+#include "dns/QDnsQuery.h"
10
+
11
+struct DnsQueryTestParams
12
+{
13
+    bool isValid;
14
+    const char* hexData;
15
+    quint32 pos;
16
+    const char* label;
17
+    quint16 type;
18
+    quint16 clazz;
19
+};
20
+
21
+class DnsQueryTest : public ::testing::TestWithParam<DnsQueryTestParams>
22
+{
23
+};
24
+
25
+TEST_P(DnsQueryTest, parse)
26
+{
27
+    auto params = GetParam();
28
+    auto data = QByteArray::fromHex(params.hexData);
29
+    auto query = QDns::QDnsQuery::parse(data, params.pos);
30
+
31
+    ASSERT_EQ(query.isValid(), params.isValid);
32
+
33
+    if (query.isValid())
34
+    {
35
+         ASSERT_STREQ(query.getName().toStdString().c_str(), params.label);
36
+         ASSERT_EQ(query.getType(), params.type);
37
+         ASSERT_EQ(query.getClass(), params.clazz);
38
+    }
39
+}
40
+
41
+INSTANTIATE_TEST_CASE_P(DnsQueryTestInst,
42
+                        DnsQueryTest,
43
+                        ::testing::Values(
44
+                                DnsQueryTestParams {false, "", 0},
45
+                                DnsQueryTestParams {false, "00", 0},
46
+                                DnsQueryTestParams {true,
47
+                                                    "0377777706676f6f676c650266720000010001",
48
+                                                    0,
49
+                                                    "www.google.fr",
50
+                                                    1,
51
+                                                    1
52
+                                },
53
+                                DnsQueryTestParams {true,
54
+                                                    "03777777067274686f6e6903636f6d0000030008",
55
+                                                    0,
56
+                                                    "www.rthoni.com",
57
+                                                    3,
58
+                                                    8
59
+                                },
60
+                                DnsQueryTestParams {true,
61
+                                                    "0003777777067274686f6e6903636f6d0000030008",
62
+                                                    1,
63
+                                                    "www.rthoni.com",
64
+                                                    3,
65
+                                                    8
66
+                                }
67
+                        ));

読み込み中…
キャンセル
保存