Browse Source

init

develop
Robin Thoni 6 years ago
commit
1199e4d07e

+ 3
- 0
.gitignore View File

@@ -0,0 +1,3 @@
1
+
2
+/cmake-build-*/
3
+.idea

+ 20
- 0
CMakeLists.txt View File

@@ -0,0 +1,20 @@
1
+cmake_minimum_required(VERSION 3.9)
2
+project(qdnsagg)
3
+
4
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
5
+
6
+set(CMAKE_AUTOMOC ON)
7
+
8
+set(CMAKE_CXX_STANDARD 17)
9
+
10
+find_package(Qt5Core REQUIRED)
11
+find_package(Qt5Network REQUIRED)
12
+set(PROJECT_LIBS Qt5::Core Qt5::Network)
13
+
14
+add_subdirectory(src)
15
+
16
+find_package(GTest)
17
+if (GTEST_FOUND)
18
+    enable_testing()
19
+    add_subdirectory(tests)
20
+endif (GTEST_FOUND)

+ 10
- 0
src/CMakeLists.txt View File

@@ -0,0 +1,10 @@
1
+cmake_minimum_required(VERSION 3.9)
2
+project(qdnsagg)
3
+
4
+add_executable(qdnsagg
5
+        main.cpp
6
+        )
7
+
8
+add_subdirectory(modules/dns)
9
+
10
+target_link_libraries(qdnsagg Qt5::Core qdnsagg-dns)

+ 9
- 0
src/main.cpp View File

@@ -0,0 +1,9 @@
1
+#include <iostream>
2
+#include <QCoreApplication>
3
+
4
+int main(int argc, char* argv[])
5
+{
6
+    QCoreApplication app(argc, argv);
7
+    std::cout << "Hello, World!" << std::endl;
8
+    return app.exec();
9
+}

+ 11
- 0
src/modules/dns/CMakeLists.txt View File

@@ -0,0 +1,11 @@
1
+cmake_minimum_required(VERSION 3.9)
2
+project(qdnsagg-dns)
3
+
4
+include_directories(includes)
5
+
6
+add_library(qdnsagg-dns
7
+        src/QDnsHeader.cpp
8
+        includes/dns/QDnsHeader.h
9
+        )
10
+
11
+target_link_libraries(qdnsagg-dns Qt5::Core Qt5::Network)

+ 85
- 0
src/modules/dns/includes/dns/QDnsHeader.h View File

@@ -0,0 +1,85 @@
1
+//
2
+// Created by robin on 1/7/18.
3
+//
4
+
5
+#ifndef QDNSAGG_QDNSHEADER_H
6
+#define QDNSAGG_QDNSHEADER_H
7
+
8
+#include <qplatformdefs.h>
9
+#include <QByteArray>
10
+
11
+namespace QDNS
12
+{
13
+
14
+    class QDnsHeader
15
+    {
16
+    public:
17
+        QDnsHeader();
18
+
19
+        static QDnsHeader parse(const QByteArray& data);
20
+
21
+        bool isValid() const;
22
+
23
+        quint16 getId() const;
24
+        void setId(quint16 id);
25
+
26
+
27
+        quint16 getFlags() const;
28
+        void setFlags(quint16 flags);
29
+        void setFlagBits(quint8 bit, quint16 value, quint8 bitCount);
30
+        quint16 getFlagBits(quint8 bit, quint8 bitCount) const;
31
+        static quint8 getFlagBitIndex(quint8 bit);
32
+
33
+        bool isQuery() const;
34
+        void setIsQuery(bool isQuery);
35
+
36
+        quint8 getOpcode() const;
37
+        void setOpcode(quint8 opcode);
38
+
39
+        bool isAuthoritative() const;
40
+        void setIsAuthoritative(bool isAuthoritative);
41
+
42
+        bool isTruncated() const;
43
+        void setIsTruncated(bool isTruncated);
44
+
45
+        bool isRecursionDesired() const;
46
+        void setIsRecursionDesired(bool isRecursionDesired);
47
+
48
+        bool isRecursionAvailable() const;
49
+        void setIsRecursionAvailable(bool isRecursionAvailable);
50
+
51
+        bool isAnswerAuthenticated() const;
52
+        void setIsAnswerAuthenticated(bool isAnswerAuthenticated);
53
+
54
+        bool isNonAuthenticatedAcceptable() const;
55
+        void setNonAuthenticatedAcceptable(bool isNonAuthenticatedAcceptable);
56
+
57
+        quint8 getReplyCode() const;
58
+        void setReplyCode(quint8 replyCode);
59
+
60
+
61
+        quint16 getQuestionCount() const;
62
+        void setQuestionCount(quint16 questionCount);
63
+
64
+        quint16 getAnswerCount() const;
65
+        void setAnswerCount(quint16 answerCount);
66
+
67
+        quint16 getAuthorityCount() const;
68
+        void setAuthorityCount(quint16 authorityCount);
69
+
70
+        quint16 getAdditionalCount() const;
71
+        void setAdditionalCount(quint16 additionalCount);
72
+
73
+    private:
74
+        bool m_isValid;
75
+        quint16 m_id;
76
+        quint16 m_flags;
77
+        quint16 m_questionCount;
78
+        quint16 m_answerCount;
79
+        quint16 m_authorityCount;
80
+        quint16 m_additionalCount;
81
+    };
82
+
83
+}
84
+
85
+#endif //QDNSAGG_QDNSHEADER_H

+ 233
- 0
src/modules/dns/src/QDnsHeader.cpp View File

@@ -0,0 +1,233 @@
1
+//
2
+// Created by robin on 1/7/18.
3
+//
4
+
5
+#include <QDataStream>
6
+#include "dns/QDnsHeader.h"
7
+
8
+namespace QDNS
9
+{
10
+
11
+    QDnsHeader::QDnsHeader()
12
+            : m_isValid(true)
13
+            , m_id(0)
14
+            , m_flags(0)
15
+            , m_questionCount(0)
16
+            , m_answerCount(0)
17
+            , m_authorityCount(0)
18
+            , m_additionalCount(0)
19
+    {
20
+
21
+    }
22
+
23
+    QDnsHeader QDnsHeader::parse(const QByteArray& data)
24
+    {
25
+        QDnsHeader header;
26
+
27
+        if (data.size() < 12)
28
+        {
29
+            header.m_isValid = false;
30
+        }
31
+        else
32
+        {
33
+            QDataStream ds(data);
34
+            ds >> header.m_id;
35
+            ds >> header.m_flags;
36
+            ds >> header.m_questionCount;
37
+            ds >> header.m_answerCount;
38
+            ds >> header.m_authorityCount;
39
+            ds >> header.m_additionalCount;
40
+        }
41
+
42
+        return header;
43
+    }
44
+
45
+    bool QDnsHeader::isValid() const
46
+    {
47
+        return m_isValid;
48
+    }
49
+
50
+    quint16 QDnsHeader::getId() const
51
+    {
52
+        return m_id;
53
+    }
54
+
55
+    void QDnsHeader::setId(quint16 id)
56
+    {
57
+        m_id = id;
58
+    }
59
+
60
+    quint16 QDnsHeader::getFlags() const
61
+    {
62
+        return m_flags;
63
+    }
64
+
65
+    void QDnsHeader::setFlags(quint16 flags)
66
+    {
67
+        m_flags = flags;
68
+    }
69
+
70
+    void QDnsHeader::setFlagBits(quint8 bit, quint16 value, quint8 bitCount)
71
+    {
72
+        quint8 baseBit = getFlagBitIndex(bit);
73
+        for (quint8 i = 0; i < bitCount; ++i) // TODO might be optimized
74
+        {
75
+            if ((value >> i) & 1)
76
+            {
77
+                m_flags |= 1UL << (baseBit + i);
78
+            }
79
+            else
80
+            {
81
+                m_flags &= ~(1UL << (baseBit + i));
82
+            }
83
+        }
84
+    }
85
+
86
+    quint16 QDnsHeader::getFlagBits(quint8 bit, quint8 bitCount) const
87
+    {
88
+        quint8 mask = 1;
89
+        for (quint8 i = 1; i < bitCount; ++i)
90
+        {
91
+            mask += 1 << i;
92
+        }
93
+        auto value = (quint16) ((m_flags >> getFlagBitIndex(bit)) & mask);
94
+        return value;
95
+    }
96
+
97
+    quint8 QDnsHeader::getFlagBitIndex(quint8 bit)
98
+    {
99
+        auto index = ((sizeof(m_flags) * 8) - bit - 1);
100
+        return index;
101
+    }
102
+
103
+    bool QDnsHeader::isQuery() const
104
+    {
105
+        return (bool) getFlagBits(0, 1);
106
+    }
107
+
108
+    void QDnsHeader::setIsQuery(bool isQuery)
109
+    {
110
+        setFlagBits(0, (quint8) isQuery, 1);
111
+    }
112
+
113
+    quint8 QDnsHeader::getOpcode() const
114
+    {
115
+        return (quint8) getFlagBits(4, 4);
116
+    }
117
+
118
+    void QDnsHeader::setOpcode(quint8 opcode)
119
+    {
120
+        setFlagBits(4, opcode, 4);
121
+    }
122
+
123
+    bool QDnsHeader::isAuthoritative() const
124
+    {
125
+        return (bool) getFlagBits(5, 1);
126
+    }
127
+
128
+    void QDnsHeader::setIsAuthoritative(bool isAuthoritative)
129
+    {
130
+        setFlagBits(5, (quint8) isAuthoritative, 1);
131
+    }
132
+
133
+    bool QDnsHeader::isTruncated() const
134
+    {
135
+        return (bool) getFlagBits(6, 1);
136
+    }
137
+
138
+    void QDnsHeader::setIsTruncated(bool isTruncated)
139
+    {
140
+        setFlagBits(6, (quint8) isTruncated, 1);
141
+    }
142
+
143
+    bool QDnsHeader::isRecursionDesired() const
144
+    {
145
+        return (bool) getFlagBits(7, 1);
146
+    }
147
+
148
+    void QDnsHeader::setIsRecursionDesired(bool isRecursionDesired)
149
+    {
150
+        setFlagBits(7, (quint8) isRecursionDesired, 1);
151
+    }
152
+
153
+    bool QDnsHeader::isRecursionAvailable() const
154
+    {
155
+        return (bool) getFlagBits(8, 1);
156
+    }
157
+
158
+    void QDnsHeader::setIsRecursionAvailable(bool isRecursionAvailable)
159
+    {
160
+        setFlagBits(8, (quint8) isRecursionAvailable, 1);
161
+    }
162
+
163
+    bool QDnsHeader::isAnswerAuthenticated() const
164
+    {
165
+        return (bool) getFlagBits(10, 1);
166
+    }
167
+
168
+    void QDnsHeader::setIsAnswerAuthenticated(bool isAnswerAuthenticated)
169
+    {
170
+        setFlagBits(10, (quint8) isAnswerAuthenticated, 1);
171
+    }
172
+
173
+    bool QDnsHeader::isNonAuthenticatedAcceptable() const
174
+    {
175
+        return (bool) getFlagBits(11, 1);
176
+    }
177
+
178
+    void QDnsHeader::setNonAuthenticatedAcceptable(bool isNonAuthenticatedAcceptable)
179
+    {
180
+        setFlagBits(11, (quint8) isNonAuthenticatedAcceptable, 1);
181
+    }
182
+
183
+    quint8 QDnsHeader::getReplyCode() const
184
+    {
185
+        return (quint8) getFlagBits(15, 4);
186
+    }
187
+
188
+    void QDnsHeader::setReplyCode(quint8 replyCode)
189
+    {
190
+        setFlagBits(15, (quint8) replyCode, 4);
191
+    }
192
+
193
+    quint16 QDnsHeader::getQuestionCount() const
194
+    {
195
+        return m_questionCount;
196
+    }
197
+
198
+    void QDnsHeader::setQuestionCount(quint16 questionCount)
199
+    {
200
+        m_questionCount = questionCount;
201
+    }
202
+
203
+    quint16 QDnsHeader::getAnswerCount() const
204
+    {
205
+        return m_answerCount;
206
+    }
207
+
208
+    void QDnsHeader::setAnswerCount(quint16 answerCount)
209
+    {
210
+        m_answerCount = answerCount;
211
+    }
212
+
213
+    quint16 QDnsHeader::getAuthorityCount() const
214
+    {
215
+        return m_authorityCount;
216
+    }
217
+
218
+    void QDnsHeader::setAuthorityCount(quint16 authorityCount)
219
+    {
220
+        m_authorityCount = authorityCount;
221
+    }
222
+
223
+    quint16 QDnsHeader::getAdditionalCount() const
224
+    {
225
+        return m_additionalCount;
226
+    }
227
+
228
+    void QDnsHeader::setAdditionalCount(quint16 additionalCount)
229
+    {
230
+        m_additionalCount = additionalCount;
231
+    }
232
+
233
+}

+ 13
- 0
tests/CMakeLists.txt View File

@@ -0,0 +1,13 @@
1
+cmake_minimum_required(VERSION 3.9)
2
+project(qdnsagg-tests)
3
+
4
+enable_testing()
5
+
6
+include_directories(../src/modules/dns/includes)
7
+
8
+add_executable(qdnsagg-tests
9
+        main.cpp
10
+        )
11
+
12
+target_link_libraries(qdnsagg-tests gtest pthread qdnsagg-dns)
13
+add_test(qdnsagg-tests ${CMAKE_CURRENT_BINARY_DIR}/qdnsagg-tests)

+ 246
- 0
tests/main.cpp View File

@@ -0,0 +1,246 @@
1
+#include <gtest/gtest.h>
2
+#include <QtCore/QByteArray>
3
+
4
+#include "dns/QDnsHeader.h"
5
+
6
+struct DNSHeaderTestParams
7
+{
8
+    bool isValid;
9
+    const char* hexData;
10
+    quint16 id;
11
+
12
+    quint16 flags;
13
+    bool flagQuery;
14
+    quint8 flagOpcode;
15
+    bool flagAuthoritative;
16
+    bool flagTruncated;
17
+    bool flagRecursionDesired;
18
+    bool flagRecursionAvailable;
19
+    bool flagReserved0;
20
+    bool flagAnswerAuthenticated;
21
+    bool flagNonAuthenticatedAcceptable;
22
+    quint8 flagReplyCode;
23
+
24
+    quint16 questionCount;
25
+    quint16 answerCount;
26
+    quint16 authorityCount;
27
+    quint16 additionalCount;
28
+};
29
+
30
+class DNSHeaderTest : public ::testing::TestWithParam<DNSHeaderTestParams>
31
+{
32
+};
33
+
34
+TEST_P(DNSHeaderTest, parseAndGetters)
35
+{
36
+    auto params = GetParam();
37
+    auto data = QByteArray::fromHex(params.hexData);
38
+    auto header = QDNS::QDnsHeader::parse(data);
39
+    ASSERT_EQ(header.isValid(), params.isValid);
40
+
41
+    if (header.isValid())
42
+    {
43
+        ASSERT_EQ(header.getId(), params.id);
44
+
45
+        ASSERT_EQ(header.getFlags(), params.flags);
46
+        ASSERT_EQ(header.isQuery(), params.flagQuery);
47
+        ASSERT_EQ(header.getOpcode(), params.flagOpcode);
48
+        ASSERT_EQ(header.isAuthoritative(), params.flagAuthoritative);
49
+        ASSERT_EQ(header.isTruncated(), params.flagTruncated);
50
+        ASSERT_EQ(header.isRecursionDesired(), params.flagRecursionDesired);
51
+        ASSERT_EQ(header.isRecursionAvailable(), params.flagRecursionAvailable);
52
+        ASSERT_EQ(header.isAnswerAuthenticated(), params.flagAnswerAuthenticated);
53
+        ASSERT_EQ(header.isNonAuthenticatedAcceptable(), params.flagNonAuthenticatedAcceptable);
54
+        ASSERT_EQ(header.getReplyCode(), params.flagReplyCode);
55
+
56
+        ASSERT_EQ(header.getQuestionCount(), params.questionCount);
57
+        ASSERT_EQ(header.getAnswerCount(), params.answerCount);
58
+        ASSERT_EQ(header.getAuthorityCount(), params.authorityCount);
59
+        ASSERT_EQ(header.getAdditionalCount(), params.additionalCount);
60
+    }
61
+}
62
+
63
+TEST_P(DNSHeaderTest, settersAndGetters1)
64
+{
65
+    auto params = GetParam();
66
+    auto data = QByteArray::fromHex(params.hexData);
67
+    QDNS::QDnsHeader header;
68
+
69
+    header.setId(params.id);
70
+    header.setFlags(params.flags);
71
+    header.setQuestionCount(params.questionCount);
72
+    header.setAnswerCount(params.answerCount);
73
+    header.setAuthorityCount(params.authorityCount);
74
+    header.setAdditionalCount(params.additionalCount);
75
+
76
+    ASSERT_EQ(header.getId(), params.id);
77
+
78
+    ASSERT_EQ(header.getFlags(), params.flags);
79
+    ASSERT_EQ(header.isQuery(), params.flagQuery);
80
+    ASSERT_EQ(header.getOpcode(), params.flagOpcode);
81
+    ASSERT_EQ(header.isAuthoritative(), params.flagAuthoritative);
82
+    ASSERT_EQ(header.isTruncated(), params.flagTruncated);
83
+    ASSERT_EQ(header.isRecursionDesired(), params.flagRecursionDesired);
84
+    ASSERT_EQ(header.isRecursionAvailable(), params.flagRecursionAvailable);
85
+    ASSERT_EQ(header.isAnswerAuthenticated(), params.flagAnswerAuthenticated);
86
+    ASSERT_EQ(header.isNonAuthenticatedAcceptable(), params.flagNonAuthenticatedAcceptable);
87
+    ASSERT_EQ(header.getReplyCode(), params.flagReplyCode);
88
+
89
+    ASSERT_EQ(header.getQuestionCount(), params.questionCount);
90
+    ASSERT_EQ(header.getAnswerCount(), params.answerCount);
91
+    ASSERT_EQ(header.getAuthorityCount(), params.authorityCount);
92
+    ASSERT_EQ(header.getAdditionalCount(), params.additionalCount);
93
+}
94
+
95
+TEST_P(DNSHeaderTest, settersAndGetters2)
96
+{
97
+    auto params = GetParam();
98
+    auto data = QByteArray::fromHex(params.hexData);
99
+    QDNS::QDnsHeader header;
100
+
101
+    header.setId(params.id);
102
+    header.setIsQuery(params.flagQuery);
103
+    header.setOpcode(params.flagOpcode);
104
+    header.setIsAuthoritative(params.flagAuthoritative);
105
+    header.setIsTruncated(params.flagTruncated);
106
+    header.setIsRecursionDesired(params.flagRecursionDesired);
107
+    header.setIsRecursionAvailable(params.flagRecursionAvailable);
108
+    header.setFlagBits(9, (quint16) params.flagReserved0, 1);
109
+    header.setIsAnswerAuthenticated(params.flagAnswerAuthenticated);
110
+    header.setNonAuthenticatedAcceptable(params.flagNonAuthenticatedAcceptable);
111
+    header.setReplyCode(params.flagReplyCode);
112
+    header.setQuestionCount(params.questionCount);
113
+    header.setAnswerCount(params.answerCount);
114
+    header.setAuthorityCount(params.authorityCount);
115
+    header.setAdditionalCount(params.additionalCount);
116
+
117
+    ASSERT_EQ(header.getId(), params.id);
118
+
119
+    ASSERT_EQ(header.getFlags(), params.flags);
120
+    ASSERT_EQ(header.isQuery(), params.flagQuery);
121
+    ASSERT_EQ(header.getOpcode(), params.flagOpcode);
122
+    ASSERT_EQ(header.isAuthoritative(), params.flagAuthoritative);
123
+    ASSERT_EQ(header.isTruncated(), params.flagTruncated);
124
+    ASSERT_EQ(header.isRecursionDesired(), params.flagRecursionDesired);
125
+    ASSERT_EQ(header.isRecursionAvailable(), params.flagRecursionAvailable);
126
+    ASSERT_EQ(header.isAnswerAuthenticated(), params.flagAnswerAuthenticated);
127
+    ASSERT_EQ(header.isNonAuthenticatedAcceptable(), params.flagNonAuthenticatedAcceptable);
128
+    ASSERT_EQ(header.getReplyCode(), params.flagReplyCode);
129
+
130
+    ASSERT_EQ(header.getQuestionCount(), params.questionCount);
131
+    ASSERT_EQ(header.getAnswerCount(), params.answerCount);
132
+    ASSERT_EQ(header.getAuthorityCount(), params.authorityCount);
133
+    ASSERT_EQ(header.getAdditionalCount(), params.additionalCount);
134
+}
135
+
136
+INSTANTIATE_TEST_CASE_P(DNSHeaderTestInst,
137
+                        DNSHeaderTest,
138
+                        ::testing::Values(
139
+                                DNSHeaderTestParams {false, ""},
140
+                                DNSHeaderTestParams {false, "00"},
141
+                                DNSHeaderTestParams {true,
142
+                                                     "3c dc 01 00 00 01 00 00  00 00 00 00",
143
+                                                     0x3cdc,
144
+
145
+                                                     0x0100,
146
+                                                     false,
147
+                                                     0,
148
+                                                     false,
149
+                                                     false,
150
+                                                     true,
151
+                                                     false,
152
+                                                     false,
153
+                                                     false,
154
+                                                     false,
155
+                                                     0,
156
+
157
+                                                     1,
158
+                                                     0,
159
+                                                     0,
160
+                                                     0
161
+                                },
162
+                                DNSHeaderTestParams {true,
163
+                                                     "3c dc 81 80 00 01 00 01  00 00 00 00",
164
+                                                     0x3cdc,
165
+                                                     0x8180,
166
+                                                     true,
167
+                                                     0,
168
+                                                     false,
169
+                                                     false,
170
+                                                     true,
171
+                                                     true,
172
+                                                     false,
173
+                                                     false,
174
+                                                     false,
175
+                                                     0,
176
+
177
+                                                     1,
178
+                                                     1,
179
+                                                     0,
180
+                                                     0
181
+                                },
182
+
183
+                                DNSHeaderTestParams {true,
184
+                                                     "3c dc aa aa aa aa 55 55  55 ff ff 55",
185
+                                                     0x3cdc,
186
+                                                     0xaaaa,
187
+                                                     true,
188
+                                                     5,
189
+                                                     false,
190
+                                                     true,
191
+                                                     false,
192
+                                                     true,
193
+                                                     false,
194
+                                                     true,
195
+                                                     false,
196
+                                                     10,
197
+
198
+                                                     43690,
199
+                                                     21845,
200
+                                                     22015,
201
+                                                     65365
202
+                                },
203
+                                DNSHeaderTestParams {true,
204
+                                                     "3c dc 55 55 55 ff aa aa  ff 55 55 55",
205
+                                                     0x3cdc,
206
+                                                     0x5555,
207
+                                                     false,
208
+                                                     10,
209
+                                                     true,
210
+                                                     false,
211
+                                                     true,
212
+                                                     false,
213
+                                                     true,
214
+                                                     false,
215
+                                                     true,
216
+                                                     5,
217
+
218
+                                                     22015,
219
+                                                     43690,
220
+                                                     65365,
221
+                                                     21845
222
+                                }
223
+                        ));
224
+
225
+TEST(DNSHeaderTest, getFlagBits)
226
+{
227
+    QDNS::QDnsHeader header;
228
+
229
+    header.setFlags(0x5555);
230
+    ASSERT_EQ(header.getFlagBits(0, 1), 0);
231
+    ASSERT_EQ(header.getFlagBits(3, 4), 5);
232
+}
233
+
234
+TEST(DNSHeaderTest, getFlagBitIndex)
235
+{
236
+    ASSERT_EQ(QDNS::QDnsHeader::getFlagBitIndex(0), 15);
237
+    ASSERT_EQ(QDNS::QDnsHeader::getFlagBitIndex(1), 14);
238
+    ASSERT_EQ(QDNS::QDnsHeader::getFlagBitIndex(14), 1);
239
+    ASSERT_EQ(QDNS::QDnsHeader::getFlagBitIndex(15), 0);
240
+}
241
+
242
+int main(int argc, char* argv[])
243
+{
244
+    ::testing::InitGoogleTest(&argc, argv);
245
+    return RUN_ALL_TESTS();
246
+}

Loading…
Cancel
Save