Browse Source

Added Base namespace

develop
Robin Thoni 6 years ago
parent
commit
cece705be8

+ 73
- 51
src/modules/dns-base/includes/dns-base/QDnsHeader.h View File

@@ -11,79 +11,101 @@
11 11
 
12 12
 namespace QDns
13 13
 {
14
-
15
-    class QDnsHeader
14
+    namespace Base
16 15
     {
17
-    public:
18
-        QDnsHeader();
16
+        class QDnsHeader
17
+        {
18
+        public:
19
+            QDnsHeader();
20
+
21
+            static QDnsHeader parse(const QByteArray &data);
22
+
23
+            static QDnsHeader parse(QDataStream &dataStream);
24
+
25
+            QByteArray serialize() const;
26
+
27
+            void serialize(QDataStream &dataStream) const;
28
+
29
+            bool isValid() const;
30
+
31
+            quint16 getId() const;
32
+
33
+            void setId(quint16 id);
34
+
35
+
36
+            quint16 getFlags() const;
37
+
38
+            void setFlags(quint16 flags);
39
+
40
+            void setFlagBits(quint8 bit, quint16 value, quint8 bitCount);
41
+
42
+            quint16 getFlagBits(quint8 bit, quint8 bitCount) const;
43
+
44
+            static quint8 getFlagBitIndex(quint8 bit);
45
+
46
+            bool isQuery() const;
47
+
48
+            void setIsQuery(bool isQuery);
49
+
50
+            quint8 getOpcode() const;
51
+
52
+            void setOpcode(quint8 opcode);
53
+
54
+            bool isAuthoritative() const;
55
+
56
+            void setIsAuthoritative(bool isAuthoritative);
57
+
58
+            bool isTruncated() const;
19 59
 
20
-        static QDnsHeader parse(const QByteArray& data);
21
-        static QDnsHeader parse(QDataStream& dataStream);
22
-        QByteArray serialize() const;
23
-        void serialize(QDataStream& dataStream) const;
60
+            void setIsTruncated(bool isTruncated);
24 61
 
25
-        bool isValid() const;
62
+            bool isRecursionDesired() const;
26 63
 
27
-        quint16 getId() const;
28
-        void setId(quint16 id);
64
+            void setIsRecursionDesired(bool isRecursionDesired);
29 65
 
66
+            bool isRecursionAvailable() const;
30 67
 
31
-        quint16 getFlags() const;
32
-        void setFlags(quint16 flags);
33
-        void setFlagBits(quint8 bit, quint16 value, quint8 bitCount);
34
-        quint16 getFlagBits(quint8 bit, quint8 bitCount) const;
35
-        static quint8 getFlagBitIndex(quint8 bit);
68
+            void setIsRecursionAvailable(bool isRecursionAvailable);
36 69
 
37
-        bool isQuery() const;
38
-        void setIsQuery(bool isQuery);
70
+            bool isAnswerAuthenticated() const;
39 71
 
40
-        quint8 getOpcode() const;
41
-        void setOpcode(quint8 opcode);
72
+            void setIsAnswerAuthenticated(bool isAnswerAuthenticated);
42 73
 
43
-        bool isAuthoritative() const;
44
-        void setIsAuthoritative(bool isAuthoritative);
74
+            bool isNonAuthenticatedAcceptable() const;
45 75
 
46
-        bool isTruncated() const;
47
-        void setIsTruncated(bool isTruncated);
76
+            void setNonAuthenticatedAcceptable(bool isNonAuthenticatedAcceptable);
48 77
 
49
-        bool isRecursionDesired() const;
50
-        void setIsRecursionDesired(bool isRecursionDesired);
78
+            quint8 getReplyCode() const;
51 79
 
52
-        bool isRecursionAvailable() const;
53
-        void setIsRecursionAvailable(bool isRecursionAvailable);
80
+            void setReplyCode(quint8 replyCode);
54 81
 
55
-        bool isAnswerAuthenticated() const;
56
-        void setIsAnswerAuthenticated(bool isAnswerAuthenticated);
57 82
 
58
-        bool isNonAuthenticatedAcceptable() const;
59
-        void setNonAuthenticatedAcceptable(bool isNonAuthenticatedAcceptable);
83
+            quint16 getQueryCount() const;
60 84
 
61
-        quint8 getReplyCode() const;
62
-        void setReplyCode(quint8 replyCode);
85
+            void setQueryCount(quint16 questionCount);
63 86
 
87
+            quint16 getAnswerCount() const;
64 88
 
65
-        quint16 getQueryCount() const;
66
-        void setQueryCount(quint16 questionCount);
89
+            void setAnswerCount(quint16 answerCount);
67 90
 
68
-        quint16 getAnswerCount() const;
69
-        void setAnswerCount(quint16 answerCount);
91
+            quint16 getAuthorityCount() const;
70 92
 
71
-        quint16 getAuthorityCount() const;
72
-        void setAuthorityCount(quint16 authorityCount);
93
+            void setAuthorityCount(quint16 authorityCount);
73 94
 
74
-        quint16 getAdditionalCount() const;
75
-        void setAdditionalCount(quint16 additionalCount);
95
+            quint16 getAdditionalCount() const;
76 96
 
77
-    private:
78
-        bool m_isValid;
79
-        quint16 m_id;
80
-        quint16 m_flags;
81
-        quint16 m_queryCount;
82
-        quint16 m_answerCount;
83
-        quint16 m_authorityCount;
84
-        quint16 m_additionalCount;
85
-    };
97
+            void setAdditionalCount(quint16 additionalCount);
86 98
 
99
+        private:
100
+            bool m_isValid;
101
+            quint16 m_id;
102
+            quint16 m_flags;
103
+            quint16 m_queryCount;
104
+            quint16 m_answerCount;
105
+            quint16 m_authorityCount;
106
+            quint16 m_additionalCount;
107
+        };
108
+    }
87 109
 }
88 110
 
89 111
 #endif //QDNSAGG_QDNSHEADER_H

+ 12
- 9
src/modules/dns-base/includes/dns-base/QDnsLabel.h View File

@@ -10,19 +10,22 @@
10 10
 
11 11
 namespace QDns
12 12
 {
13
-
14
-    class QDnsLabel
13
+    namespace Base
15 14
     {
16
-    public:
17
-        QDnsLabel() = delete;
15
+        class QDnsLabel
16
+        {
17
+        public:
18
+            QDnsLabel() = delete;
19
+
20
+            static QString parse(const QByteArray &packetData, quint32 pos, quint8 maxRecursion = 5);
18 21
 
19
-        static QString parse(const QByteArray& packetData, quint32 pos, quint8 maxRecursion = 5);
20
-        static QString parse(const QByteArray& packetData, QDataStream& dataStream, quint8 maxRecursion = 5);
22
+            static QString parse(const QByteArray &packetData, QDataStream &dataStream, quint8 maxRecursion = 5);
21 23
 
22
-        static QByteArray serialize(const QString& label);
23
-        static void serialize(const QString& label, QDataStream& dataStream);
24
-    };
24
+            static QByteArray serialize(const QString &label);
25 25
 
26
+            static void serialize(const QString &label, QDataStream &dataStream);
27
+        };
28
+    }
26 29
 }
27 30
 
28 31
 #endif //QDNSAGG_QDNSLABEL_H

+ 31
- 27
src/modules/dns-base/includes/dns-base/QDnsPacket.h View File

@@ -12,50 +12,54 @@
12 12
 
13 13
 namespace QDns
14 14
 {
15
-
16
-    class QDnsPacket
15
+    namespace Base
17 16
     {
18
-    public:
19
-        QDnsPacket();
17
+        class QDnsPacket
18
+        {
19
+        public:
20
+            QDnsPacket();
21
+
22
+            static QDnsPacket parse(const QByteArray &data);
23
+
24
+            static QDnsPacket parse(const QByteArray &data, QDataStream &dataStream);
20 25
 
21
-        static QDnsPacket parse(const QByteArray& data);
22
-        static QDnsPacket parse(const QByteArray& data, QDataStream& dataStream);
23
-        QByteArray serialize() const;
24
-        void serialize(QDataStream& dataStream) const;
26
+            QByteArray serialize() const;
25 27
 
26
-        bool isValid() const;
28
+            void serialize(QDataStream &dataStream) const;
27 29
 
28
-        const QDnsHeader& getHeader() const;
30
+            bool isValid() const;
29 31
 
30
-        void setHeader(const QDnsHeader& header);
32
+            const QDnsHeader &getHeader() const;
31 33
 
32
-        const QList<QDnsQuery>& getQueries() const;
34
+            void setHeader(const QDnsHeader &header);
33 35
 
34
-        void setQueries(const QList<QDnsQuery>& queries);
36
+            const QList<QDnsQuery> &getQueries() const;
35 37
 
36
-        const QList<QDnsResourceRecord>& getAnswerRecords() const;
38
+            void setQueries(const QList<QDnsQuery> &queries);
37 39
 
38
-        void setAnswerRecords(const QList<QDnsResourceRecord>& answerRecords);
40
+            const QList<QDnsResourceRecord> &getAnswerRecords() const;
39 41
 
40
-        const QList<QDnsResourceRecord>& getAuthorityRecords() const;
42
+            void setAnswerRecords(const QList<QDnsResourceRecord> &answerRecords);
41 43
 
42
-        void setAuthorityRecords(const QList<QDnsResourceRecord>& authorityRecords);
44
+            const QList<QDnsResourceRecord> &getAuthorityRecords() const;
43 45
 
44
-        const QList<QDnsResourceRecord>& getAdditionalRecords() const;
46
+            void setAuthorityRecords(const QList<QDnsResourceRecord> &authorityRecords);
45 47
 
46
-        void setAdditionalRecords(const QList<QDnsResourceRecord>& additionalRecords);
48
+            const QList<QDnsResourceRecord> &getAdditionalRecords() const;
47 49
 
48
-    private:
49
-        bool m_isValid;
50
+            void setAdditionalRecords(const QList<QDnsResourceRecord> &additionalRecords);
50 51
 
51
-        QDnsHeader m_header;
52
+        private:
53
+            bool m_isValid;
52 54
 
53
-        QList<QDnsQuery> m_queries;
54
-        QList<QDnsResourceRecord> m_answerRecords;
55
-        QList<QDnsResourceRecord> m_authorityRecords;
56
-        QList<QDnsResourceRecord> m_additionalRecords;
57
-    };
55
+            QDnsHeader m_header;
58 56
 
57
+            QList<QDnsQuery> m_queries;
58
+            QList<QDnsResourceRecord> m_answerRecords;
59
+            QList<QDnsResourceRecord> m_authorityRecords;
60
+            QList<QDnsResourceRecord> m_additionalRecords;
61
+        };
62
+    }
59 63
 }
60 64
 
61 65
 #endif //QDNSAGG_QDNSPACKET_H

+ 28
- 21
src/modules/dns-base/includes/dns-base/QDnsQuery.h View File

@@ -10,36 +10,43 @@
10 10
 
11 11
 namespace QDns
12 12
 {
13
-
14
-    class QDnsQuery
13
+    namespace Base
15 14
     {
16
-    public:
17
-        QDnsQuery();
15
+        class QDnsQuery
16
+        {
17
+        public:
18
+            QDnsQuery();
19
+
20
+            static QDnsQuery parse(const QByteArray &packetData, quint32 pos);
21
+
22
+            static QDnsQuery parse(const QByteArray &packetData, QDataStream &dataStream);
23
+
24
+            QByteArray serialize() const;
25
+
26
+            void serialize(QDataStream &dataStream) const;
27
+
28
+            bool isValid() const;
18 29
 
19
-        static QDnsQuery parse(const QByteArray& packetData, quint32 pos);
20
-        static QDnsQuery parse(const QByteArray& packetData, QDataStream& dataStream);
21
-        QByteArray serialize() const;
22
-        void serialize(QDataStream& dataStream) const;
30
+            const QString &getName() const;
23 31
 
24
-        bool isValid() const;
32
+            void setName(const QString &name);
25 33
 
26
-        const QString& getName() const;
27
-        void setName(const QString& name);
34
+            quint16 getType() const;
28 35
 
29
-        quint16 getType() const;
30
-        void setType(quint16 type);
36
+            void setType(quint16 type);
31 37
 
32
-        quint16 getClass() const;
33
-        void setClass(quint16 aClass);
38
+            quint16 getClass() const;
34 39
 
35
-    private:
36
-        bool m_isValid;
40
+            void setClass(quint16 aClass);
37 41
 
38
-        QString m_name;
39
-        quint16 m_type;
40
-        quint16 m_class;
41
-    };
42
+        private:
43
+            bool m_isValid;
42 44
 
45
+            QString m_name;
46
+            quint16 m_type;
47
+            quint16 m_class;
48
+        };
49
+    }
43 50
 }
44 51
 
45 52
 #endif //QDNSAGG_QDNSQUERY_H

+ 31
- 27
src/modules/dns-base/includes/dns-base/QDnsResourceRecord.h View File

@@ -11,49 +11,53 @@
11 11
 
12 12
 namespace QDns
13 13
 {
14
-
15
-    class QDnsResourceRecord
14
+    namespace Base
16 15
     {
17
-    public:
18
-        QDnsResourceRecord();
16
+        class QDnsResourceRecord
17
+        {
18
+        public:
19
+            QDnsResourceRecord();
20
+
21
+            static QDnsResourceRecord parse(const QByteArray &packetData, quint32 pos);
22
+
23
+            static QDnsResourceRecord parse(const QByteArray &packetData, QDataStream &dataStream);
19 24
 
20
-        static QDnsResourceRecord parse(const QByteArray& packetData, quint32 pos);
21
-        static QDnsResourceRecord parse(const QByteArray& packetData, QDataStream& dataStream);
22
-        QByteArray serialize() const;
23
-        void serialize(QDataStream& dataStream) const;
25
+            QByteArray serialize() const;
24 26
 
25
-        bool isValid() const;
27
+            void serialize(QDataStream &dataStream) const;
26 28
 
27
-        const QString& getName() const;
29
+            bool isValid() const;
28 30
 
29
-        void setName(const QString& name);
31
+            const QString &getName() const;
30 32
 
31
-        quint16 getType() const;
33
+            void setName(const QString &name);
32 34
 
33
-        void setType(quint16 type);
35
+            quint16 getType() const;
34 36
 
35
-        quint16 getClass() const;
37
+            void setType(quint16 type);
36 38
 
37
-        void setClass(quint16 aClass);
39
+            quint16 getClass() const;
38 40
 
39
-        quint32 getTtl() const;
41
+            void setClass(quint16 aClass);
40 42
 
41
-        void setTtl(quint32 ttl);
43
+            quint32 getTtl() const;
42 44
 
43
-        const QByteArray& getData() const;
45
+            void setTtl(quint32 ttl);
44 46
 
45
-        void setData(const QByteArray& data);
47
+            const QByteArray &getData() const;
46 48
 
47
-    private:
48
-        bool m_isValid;
49
+            void setData(const QByteArray &data);
49 50
 
50
-        QString m_name;
51
-        quint16 m_type;
52
-        quint16 m_class;
53
-        quint32 m_ttl;
54
-        QByteArray m_data;
55
-    };
51
+        private:
52
+            bool m_isValid;
56 53
 
54
+            QString m_name;
55
+            quint16 m_type;
56
+            quint16 m_class;
57
+            quint32 m_ttl;
58
+            QByteArray m_data;
59
+        };
60
+    }
57 61
 }
58 62
 
59 63
 #endif //QDNSAGG_RESOURCERECORD_H

+ 196
- 195
src/modules/dns-base/src/QDnsHeader.cpp View File

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

+ 77
- 76
src/modules/dns-base/src/QDnsLabel.cpp View File

@@ -6,106 +6,107 @@
6 6
 
7 7
 namespace QDns
8 8
 {
9
-
10
-    QString QDnsLabel::parse(const QByteArray& packetData, quint32 pos, quint8 maxRecursion)
9
+    namespace Base
11 10
     {
12
-        QDataStream dataStream(packetData);
13
-        dataStream.skipRawData(pos);
14
-        return parse(packetData, dataStream, maxRecursion);
15
-    }
16
-
17
-    QString QDnsLabel::parse(const QByteArray& packetData, QDataStream& dataStream, quint8 maxRecursion)
18
-    {
19
-        if (maxRecursion == 0)
20
-        {
21
-            return QString::null;
22
-        }
23
-
24
-        QString fullLabel = "";
25
-
26
-        if (dataStream.device()->bytesAvailable() < 1)
11
+        QString QDnsLabel::parse(const QByteArray &packetData, quint32 pos, quint8 maxRecursion)
27 12
         {
28
-            return QString::null;
13
+            QDataStream dataStream(packetData);
14
+            dataStream.skipRawData(pos);
15
+            return parse(packetData, dataStream, maxRecursion);
29 16
         }
30 17
 
31
-        quint8 labelSize;
32
-        dataStream >> labelSize;
33
-        while (labelSize != 0)
18
+        QString QDnsLabel::parse(const QByteArray &packetData, QDataStream &dataStream, quint8 maxRecursion)
34 19
         {
35
-            if (labelSize >> 6 == 3)
20
+            if (maxRecursion == 0)
36 21
             {
37
-                if (dataStream.device()->bytesAvailable() < 1)
38
-                {
39
-                    return QString::null;
40
-                }
41
-                quint8 labelSize2;
42
-                dataStream >> labelSize2;
43
-                quint16 pointer = ((labelSize << 8) | labelSize2) - (quint16) 49152;
44
-                QDataStream dataStream2(packetData);
45
-                dataStream2.skipRawData(pointer);
46
-                auto labelEnd = parse(packetData, dataStream2, maxRecursion - (quint8) 1);
47
-                if (labelEnd.isNull())
48
-                {
49
-                    return QString::null;
50
-                }
51
-                if (!fullLabel.isEmpty())
52
-                {
53
-                    fullLabel.append(".");
54
-                }
55
-                fullLabel.append(labelEnd);
56
-                break;
22
+                return QString::null;
57 23
             }
58
-            else if (labelSize >> 6 == 0)
24
+
25
+            QString fullLabel = "";
26
+
27
+            if (dataStream.device()->bytesAvailable() < 1)
59 28
             {
29
+                return QString::null;
30
+            }
60 31
 
61
-                if (dataStream.device()->bytesAvailable() < labelSize + 1)
32
+            quint8 labelSize;
33
+            dataStream >> labelSize;
34
+            while (labelSize != 0)
35
+            {
36
+                if (labelSize >> 6 == 3)
62 37
                 {
63
-                    return QString::null;
38
+                    if (dataStream.device()->bytesAvailable() < 1)
39
+                    {
40
+                        return QString::null;
41
+                    }
42
+                    quint8 labelSize2;
43
+                    dataStream >> labelSize2;
44
+                    quint16 pointer = ((labelSize << 8) | labelSize2) - (quint16) 49152;
45
+                    QDataStream dataStream2(packetData);
46
+                    dataStream2.skipRawData(pointer);
47
+                    auto labelEnd = parse(packetData, dataStream2, maxRecursion - (quint8) 1);
48
+                    if (labelEnd.isNull())
49
+                    {
50
+                        return QString::null;
51
+                    }
52
+                    if (!fullLabel.isEmpty())
53
+                    {
54
+                        fullLabel.append(".");
55
+                    }
56
+                    fullLabel.append(labelEnd);
57
+                    break;
64 58
                 }
65
-
66
-                char label[labelSize];
67
-                int read = dataStream.readRawData(label, labelSize);
68
-                if (read != labelSize)
59
+                else if (labelSize >> 6 == 0)
69 60
                 {
70
-                    return QString::null;
61
+
62
+                    if (dataStream.device()->bytesAvailable() < labelSize + 1)
63
+                    {
64
+                        return QString::null;
65
+                    }
66
+
67
+                    char label[labelSize];
68
+                    int read = dataStream.readRawData(label, labelSize);
69
+                    if (read != labelSize)
70
+                    {
71
+                        return QString::null;
72
+                    }
73
+                    if (!fullLabel.isEmpty())
74
+                    {
75
+                        fullLabel.append(".");
76
+                    }
77
+                    fullLabel.append(QByteArray(label, labelSize));
78
+                    dataStream >> labelSize;
71 79
                 }
72
-                if (!fullLabel.isEmpty())
80
+                else
73 81
                 {
74
-                    fullLabel.append(".");
82
+                    return QString::null;
75 83
                 }
76
-                fullLabel.append(QByteArray(label, labelSize));
77
-                dataStream >> labelSize;
78
-            }
79
-            else
80
-            {
81
-                return QString::null;
82 84
             }
85
+            return fullLabel;
83 86
         }
84
-        return fullLabel;
85
-    }
86 87
 
87
-    QByteArray QDnsLabel::serialize(const QString& label)
88
-    {
89
-        QByteArray data;
90
-        QDataStream dataStream(&data, QIODevice::WriteOnly);
91
-        serialize(label, dataStream);
92
-        return data;
93
-    }
88
+        QByteArray QDnsLabel::serialize(const QString &label)
89
+        {
90
+            QByteArray data;
91
+            QDataStream dataStream(&data, QIODevice::WriteOnly);
92
+            serialize(label, dataStream);
93
+            return data;
94
+        }
94 95
 
95
-    void QDnsLabel::serialize(const QString& label, QDataStream& dataStream)
96
-    {
97
-        if (label.size() != 0)
96
+        void QDnsLabel::serialize(const QString &label, QDataStream &dataStream)
98 97
         {
99
-            for (const auto& part : label.split("."))
98
+            if (label.size() != 0)
100 99
             {
101
-                dataStream << (quint8) part.length();
102
-                for (const auto& c : part)
100
+                for (const auto &part : label.split("."))
103 101
                 {
104
-                    dataStream << (quint8) c.toLatin1();
102
+                    dataStream << (quint8) part.length();
103
+                    for (const auto &c : part)
104
+                    {
105
+                        dataStream << (quint8) c.toLatin1();
106
+                    }
105 107
                 }
106 108
             }
109
+            dataStream << (quint8) 0;
107 110
         }
108
-        dataStream << (quint8) 0;
109 111
     }
110
-
111 112
 }

+ 122
- 120
src/modules/dns-base/src/QDnsPacket.cpp View File

@@ -6,159 +6,161 @@
6 6
 
7 7
 namespace QDns
8 8
 {
9
-
10
-    QDnsPacket::QDnsPacket()
11
-        : m_isValid(true)
12
-    {
13
-    }
14
-
15
-    QDnsPacket QDnsPacket::parse(const QByteArray& data)
16
-    {
17
-        QDataStream dataStream(data);
18
-        return parse(data, dataStream);
19
-    }
20
-
21
-    QDnsPacket QDnsPacket::parse(const QByteArray& data, QDataStream& dataStream)
9
+    namespace Base
22 10
     {
23
-        QDnsPacket packet;
11
+        QDnsPacket::QDnsPacket()
12
+                : m_isValid(true)
13
+        {
14
+        }
24 15
 
25
-        packet.m_header = QDnsHeader::parse(dataStream);
26
-        if (!packet.m_header.isValid())
16
+        QDnsPacket QDnsPacket::parse(const QByteArray &data)
27 17
         {
28
-            packet.m_isValid = false;
18
+            QDataStream dataStream(data);
19
+            return parse(data, dataStream);
29 20
         }
30
-        else
21
+
22
+        QDnsPacket QDnsPacket::parse(const QByteArray &data, QDataStream &dataStream)
31 23
         {
32
-            for (quint16 i = 0; i < packet.m_header.getQueryCount() && packet.m_isValid; ++i)
24
+            QDnsPacket packet;
25
+
26
+            packet.m_header = QDnsHeader::parse(dataStream);
27
+            if (!packet.m_header.isValid())
33 28
             {
34
-                auto entry = QDnsQuery::parse(data, dataStream);
35
-                if (!entry.isValid())
36
-                {
37
-                    packet.m_isValid = false;
38
-                }
39
-                packet.m_queries.append(entry);
29
+                packet.m_isValid = false;
40 30
             }
41
-            for (quint16 i = 0; i < packet.m_header.getAnswerCount() && packet.m_isValid; ++i)
31
+            else
42 32
             {
43
-                auto entry = QDnsResourceRecord::parse(data, dataStream);
44
-                if (!entry.isValid())
33
+                for (quint16 i = 0; i < packet.m_header.getQueryCount() && packet.m_isValid; ++i)
45 34
                 {
46
-                    packet.m_isValid = false;
35
+                    auto entry = QDnsQuery::parse(data, dataStream);
36
+                    if (!entry.isValid())
37
+                    {
38
+                        packet.m_isValid = false;
39
+                    }
40
+                    packet.m_queries.append(entry);
47 41
                 }
48
-                packet.m_answerRecords.append(entry);
49
-            }
50
-            for (quint16 i = 0; i < packet.m_header.getAuthorityCount() && packet.m_isValid; ++i)
51
-            {
52
-                auto entry = QDnsResourceRecord::parse(data, dataStream);
53
-                if (!entry.isValid())
42
+                for (quint16 i = 0; i < packet.m_header.getAnswerCount() && packet.m_isValid; ++i)
54 43
                 {
55
-                    packet.m_isValid = false;
44
+                    auto entry = QDnsResourceRecord::parse(data, dataStream);
45
+                    if (!entry.isValid())
46
+                    {
47
+                        packet.m_isValid = false;
48
+                    }
49
+                    packet.m_answerRecords.append(entry);
56 50
                 }
57
-                packet.m_authorityRecords.append(entry);
58
-            }
59
-            for (quint16 i = 0; i < packet.m_header.getAdditionalCount() && packet.m_isValid; ++i)
60
-            {
61
-                auto entry = QDnsResourceRecord::parse(data, dataStream);
62
-                if (!entry.isValid())
51
+                for (quint16 i = 0; i < packet.m_header.getAuthorityCount() && packet.m_isValid; ++i)
63 52
                 {
64
-                    packet.m_isValid = false;
53
+                    auto entry = QDnsResourceRecord::parse(data, dataStream);
54
+                    if (!entry.isValid())
55
+                    {
56
+                        packet.m_isValid = false;
57
+                    }
58
+                    packet.m_authorityRecords.append(entry);
59
+                }
60
+                for (quint16 i = 0; i < packet.m_header.getAdditionalCount() && packet.m_isValid; ++i)
61
+                {
62
+                    auto entry = QDnsResourceRecord::parse(data, dataStream);
63
+                    if (!entry.isValid())
64
+                    {
65
+                        packet.m_isValid = false;
66
+                    }
67
+                    packet.m_additionalRecords.append(entry);
65 68
                 }
66
-                packet.m_additionalRecords.append(entry);
67 69
             }
68
-        }
69 70
 
70
-        return packet;
71
-    }
72
-
73
-    QByteArray QDnsPacket::serialize() const
74
-    {
75
-        QByteArray byteArray;
76
-        QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
77
-        serialize(dataStream);
78
-        return byteArray;
79
-    }
71
+            return packet;
72
+        }
80 73
 
81
-    void QDnsPacket::serialize(QDataStream& dataStream) const
82
-    {
83
-        m_header.serialize(dataStream);
84
-        for (const auto& entry : m_queries)
74
+        QByteArray QDnsPacket::serialize() const
85 75
         {
86
-            entry.serialize(dataStream);
76
+            QByteArray byteArray;
77
+            QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
78
+            serialize(dataStream);
79
+            return byteArray;
87 80
         }
88
-        for (const auto& entry : m_answerRecords)
81
+
82
+        void QDnsPacket::serialize(QDataStream &dataStream) const
89 83
         {
90
-            entry.serialize(dataStream);
84
+            m_header.serialize(dataStream);
85
+            for (const auto &entry : m_queries)
86
+            {
87
+                entry.serialize(dataStream);
88
+            }
89
+            for (const auto &entry : m_answerRecords)
90
+            {
91
+                entry.serialize(dataStream);
92
+            }
93
+            for (const auto &entry : m_authorityRecords)
94
+            {
95
+                entry.serialize(dataStream);
96
+            }
97
+            for (const auto &entry : m_additionalRecords)
98
+            {
99
+                entry.serialize(dataStream);
100
+            }
91 101
         }
92
-        for (const auto& entry : m_authorityRecords)
102
+
103
+        bool QDnsPacket::isValid() const
93 104
         {
94
-            entry.serialize(dataStream);
105
+            return m_isValid;
95 106
         }
96
-        for (const auto& entry : m_additionalRecords)
107
+
108
+        const QDnsHeader &QDnsPacket::getHeader() const
97 109
         {
98
-            entry.serialize(dataStream);
110
+            return m_header;
99 111
         }
100
-    }
101 112
 
102
-    bool QDnsPacket::isValid() const
103
-    {
104
-        return m_isValid;
105
-    }
106
-
107
-    const QDnsHeader& QDnsPacket::getHeader() const
108
-    {
109
-        return m_header;
110
-    }
111
-
112
-    void QDnsPacket::setHeader(const QDnsHeader& header)
113
-    {
114
-        m_header = header;
115
-        m_header.setQueryCount((quint16) m_queries.length());
116
-        m_header.setAnswerCount((quint16) m_answerRecords.length());
117
-        m_header.setAuthorityCount((quint16) m_authorityRecords.length());
118
-        m_header.setAdditionalCount((quint16) m_additionalRecords.length());
119
-    }
113
+        void QDnsPacket::setHeader(const QDnsHeader &header)
114
+        {
115
+            m_header = header;
116
+            m_header.setQueryCount((quint16) m_queries.length());
117
+            m_header.setAnswerCount((quint16) m_answerRecords.length());
118
+            m_header.setAuthorityCount((quint16) m_authorityRecords.length());
119
+            m_header.setAdditionalCount((quint16) m_additionalRecords.length());
120
+        }
120 121
 
121
-    const QList<QDnsQuery>& QDnsPacket::getQueries() const
122
-    {
123
-        return m_queries;
124
-    }
122
+        const QList<QDnsQuery> &QDnsPacket::getQueries() const
123
+        {
124
+            return m_queries;
125
+        }
125 126
 
126
-    void QDnsPacket::setQueries(const QList<QDnsQuery>& queries)
127
-    {
128
-        m_queries = queries;
129
-        m_header.setQueryCount((quint16) m_queries.length());
130
-    }
127
+        void QDnsPacket::setQueries(const QList<QDnsQuery> &queries)
128
+        {
129
+            m_queries = queries;
130
+            m_header.setQueryCount((quint16) m_queries.length());
131
+        }
131 132
 
132
-    const QList<QDnsResourceRecord>& QDnsPacket::getAnswerRecords() const
133
-    {
134
-        return m_answerRecords;
135
-    }
133
+        const QList<QDnsResourceRecord> &QDnsPacket::getAnswerRecords() const
134
+        {
135
+            return m_answerRecords;
136
+        }
136 137
 
137
-    void QDnsPacket::setAnswerRecords(const QList<QDnsResourceRecord>& answerRecords)
138
-    {
139
-        m_answerRecords = answerRecords;
140
-        m_header.setAnswerCount((quint16) m_answerRecords.length());
141
-    }
138
+        void QDnsPacket::setAnswerRecords(const QList<QDnsResourceRecord> &answerRecords)
139
+        {
140
+            m_answerRecords = answerRecords;
141
+            m_header.setAnswerCount((quint16) m_answerRecords.length());
142
+        }
142 143
 
143
-    const QList<QDnsResourceRecord>& QDnsPacket::getAuthorityRecords() const
144
-    {
145
-        return m_authorityRecords;
146
-    }
144
+        const QList<QDnsResourceRecord> &QDnsPacket::getAuthorityRecords() const
145
+        {
146
+            return m_authorityRecords;
147
+        }
147 148
 
148
-    void QDnsPacket::setAuthorityRecords(const QList<QDnsResourceRecord>& authorityRecords)
149
-    {
150
-        m_authorityRecords = authorityRecords;
151
-        m_header.setAuthorityCount((quint16) m_authorityRecords.length());
152
-    }
149
+        void QDnsPacket::setAuthorityRecords(const QList<QDnsResourceRecord> &authorityRecords)
150
+        {
151
+            m_authorityRecords = authorityRecords;
152
+            m_header.setAuthorityCount((quint16) m_authorityRecords.length());
153
+        }
153 154
 
154
-    const QList<QDnsResourceRecord>& QDnsPacket::getAdditionalRecords() const
155
-    {
156
-        return m_additionalRecords;
157
-    }
155
+        const QList<QDnsResourceRecord> &QDnsPacket::getAdditionalRecords() const
156
+        {
157
+            return m_additionalRecords;
158
+        }
158 159
 
159
-    void QDnsPacket::setAdditionalRecords(const QList<QDnsResourceRecord>& additionalRecords)
160
-    {
161
-        m_additionalRecords = additionalRecords;
162
-        m_header.setAdditionalCount((quint16) m_additionalRecords.length());
160
+        void QDnsPacket::setAdditionalRecords(const QList<QDnsResourceRecord> &additionalRecords)
161
+        {
162
+            m_additionalRecords = additionalRecords;
163
+            m_header.setAdditionalCount((quint16) m_additionalRecords.length());
164
+        }
163 165
     }
164 166
 }

+ 68
- 66
src/modules/dns-base/src/QDnsQuery.cpp View File

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

+ 92
- 90
src/modules/dns-base/src/QDnsResourceRecord.cpp View File

@@ -7,120 +7,122 @@
7 7
 
8 8
 namespace QDns
9 9
 {
10
-
11
-    QDnsResourceRecord::QDnsResourceRecord()
12
-        : m_isValid(true)
13
-    {
14
-    }
15
-
16
-    QDnsResourceRecord QDnsResourceRecord::parse(const QByteArray& packetData, quint32 pos)
10
+    namespace Base
17 11
     {
18
-        QDataStream dataStream(packetData);
19
-        dataStream.skipRawData(pos);
20
-        return parse(packetData, dataStream);
21
-    }
22
-
23
-    QDnsResourceRecord QDnsResourceRecord::parse(const QByteArray& packetData, QDataStream& dataStream)
24
-    {
25
-        QDnsResourceRecord rr;
12
+        QDnsResourceRecord::QDnsResourceRecord()
13
+                : m_isValid(true)
14
+        {
15
+        }
26 16
 
27
-        rr.m_name = QDnsLabel::parse(packetData, dataStream);
28
-        if (rr.m_name.isNull() || dataStream.device()->bytesAvailable() < 10)
17
+        QDnsResourceRecord QDnsResourceRecord::parse(const QByteArray &packetData, quint32 pos)
29 18
         {
30
-            rr.m_isValid = false;
19
+            QDataStream dataStream(packetData);
20
+            dataStream.skipRawData(pos);
21
+            return parse(packetData, dataStream);
31 22
         }
32
-        else
23
+
24
+        QDnsResourceRecord QDnsResourceRecord::parse(const QByteArray &packetData, QDataStream &dataStream)
33 25
         {
34
-            dataStream >> rr.m_type;
35
-            dataStream >> rr.m_class;
36
-            dataStream >> rr.m_ttl;
37
-            quint16 length;
38
-            dataStream >> length;
39
-            if (dataStream.device()->bytesAvailable() < length)
26
+            QDnsResourceRecord rr;
27
+
28
+            rr.m_name = QDnsLabel::parse(packetData, dataStream);
29
+            if (rr.m_name.isNull() || dataStream.device()->bytesAvailable() < 10)
40 30
             {
41 31
                 rr.m_isValid = false;
42 32
             }
43 33
             else
44 34
             {
45
-                char data[length];
46
-                dataStream.readRawData(data, length);
47
-                rr.m_data.append(data, length);
35
+                dataStream >> rr.m_type;
36
+                dataStream >> rr.m_class;
37
+                dataStream >> rr.m_ttl;
38
+                quint16 length;
39
+                dataStream >> length;
40
+                if (dataStream.device()->bytesAvailable() < length)
41
+                {
42
+                    rr.m_isValid = false;
43
+                }
44
+                else
45
+                {
46
+                    char data[length];
47
+                    dataStream.readRawData(data, length);
48
+                    rr.m_data.append(data, length);
49
+                }
48 50
             }
51
+            return rr;
49 52
         }
50
-        return rr;
51
-    }
52 53
 
53
-    QByteArray QDnsResourceRecord::serialize() const
54
-    {
55
-        QByteArray byteArray;
56
-        QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
57
-        serialize(dataStream);
58
-        return byteArray;
59
-    }
54
+        QByteArray QDnsResourceRecord::serialize() const
55
+        {
56
+            QByteArray byteArray;
57
+            QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
58
+            serialize(dataStream);
59
+            return byteArray;
60
+        }
60 61
 
61
-    void QDnsResourceRecord::serialize(QDataStream& dataStream) const
62
-    {
63
-        auto label = QDnsLabel::serialize(m_name);
64
-        dataStream.writeRawData(label.constData(), label.length());
65
-        dataStream << m_type;
66
-        dataStream << m_class;
67
-        dataStream << m_ttl;
68
-        dataStream << (quint16) m_data.length();
69
-        dataStream.writeRawData(m_data.constData(), m_data.length());
70
-    }
62
+        void QDnsResourceRecord::serialize(QDataStream &dataStream) const
63
+        {
64
+            auto label = QDnsLabel::serialize(m_name);
65
+            dataStream.writeRawData(label.constData(), label.length());
66
+            dataStream << m_type;
67
+            dataStream << m_class;
68
+            dataStream << m_ttl;
69
+            dataStream << (quint16) m_data.length();
70
+            dataStream.writeRawData(m_data.constData(), m_data.length());
71
+        }
71 72
 
72
-    bool QDnsResourceRecord::isValid() const
73
-    {
74
-        return m_isValid;
75
-    }
73
+        bool QDnsResourceRecord::isValid() const
74
+        {
75
+            return m_isValid;
76
+        }
76 77
 
77
-    const QString& QDnsResourceRecord::getName() const
78
-    {
79
-        return m_name;
80
-    }
78
+        const QString &QDnsResourceRecord::getName() const
79
+        {
80
+            return m_name;
81
+        }
81 82
 
82
-    void QDnsResourceRecord::setName(const QString& name)
83
-    {
84
-        m_name = name;
85
-    }
83
+        void QDnsResourceRecord::setName(const QString &name)
84
+        {
85
+            m_name = name;
86
+        }
86 87
 
87
-    quint16 QDnsResourceRecord::getType() const
88
-    {
89
-        return m_type;
90
-    }
88
+        quint16 QDnsResourceRecord::getType() const
89
+        {
90
+            return m_type;
91
+        }
91 92
 
92
-    void QDnsResourceRecord::setType(quint16 type)
93
-    {
94
-        m_type = type;
95
-    }
93
+        void QDnsResourceRecord::setType(quint16 type)
94
+        {
95
+            m_type = type;
96
+        }
96 97
 
97
-    quint16 QDnsResourceRecord::getClass() const
98
-    {
99
-        return m_class;
100
-    }
98
+        quint16 QDnsResourceRecord::getClass() const
99
+        {
100
+            return m_class;
101
+        }
101 102
 
102
-    void QDnsResourceRecord::setClass(quint16 aClass)
103
-    {
104
-        m_class = aClass;
105
-    }
103
+        void QDnsResourceRecord::setClass(quint16 aClass)
104
+        {
105
+            m_class = aClass;
106
+        }
106 107
 
107
-    quint32 QDnsResourceRecord::getTtl() const
108
-    {
109
-        return m_ttl;
110
-    }
108
+        quint32 QDnsResourceRecord::getTtl() const
109
+        {
110
+            return m_ttl;
111
+        }
111 112
 
112
-    void QDnsResourceRecord::setTtl(quint32 ttl)
113
-    {
114
-        m_ttl = ttl;
115
-    }
113
+        void QDnsResourceRecord::setTtl(quint32 ttl)
114
+        {
115
+            m_ttl = ttl;
116
+        }
116 117
 
117
-    const QByteArray& QDnsResourceRecord::getData() const
118
-    {
119
-        return m_data;
120
-    }
118
+        const QByteArray &QDnsResourceRecord::getData() const
119
+        {
120
+            return m_data;
121
+        }
121 122
 
122
-    void QDnsResourceRecord::setData(const QByteArray& data)
123
-    {
124
-        m_data = data;
123
+        void QDnsResourceRecord::setData(const QByteArray &data)
124
+        {
125
+            m_data = data;
126
+        }
125 127
     }
126 128
 }

+ 8
- 8
src/modules/dns-base/tests/DnsHeader.cpp View File

@@ -40,7 +40,7 @@ TEST_P(DnsHeaderTest, parseAndGetters)
40 40
 {
41 41
     auto params = GetParam();
42 42
     auto data = QByteArray::fromHex(params.hexData);
43
-    auto header = QDns::QDnsHeader::parse(data);
43
+    auto header = QDns::Base::QDnsHeader::parse(data);
44 44
     ASSERT_EQ(header.isValid(), params.isValid);
45 45
 
46 46
     if (header.isValid())
@@ -77,7 +77,7 @@ TEST_P(DnsHeaderTest, settersAndGetters1)
77 77
     }
78 78
 
79 79
     auto data = QByteArray::fromHex(params.hexData);
80
-    QDns::QDnsHeader header;
80
+    QDns::Base::QDnsHeader header;
81 81
 
82 82
     header.setId(params.id);
83 83
     header.setFlags(params.flags);
@@ -117,7 +117,7 @@ TEST_P(DnsHeaderTest, settersAndGetters2)
117 117
     }
118 118
 
119 119
     auto data = QByteArray::fromHex(params.hexData);
120
-    QDns::QDnsHeader header;
120
+    QDns::Base::QDnsHeader header;
121 121
 
122 122
     header.setId(params.id);
123 123
     header.setIsQuery(params.flagQuery);
@@ -247,7 +247,7 @@ INSTANTIATE_TEST_CASE_P(DnsHeaderTestInst,
247 247
 
248 248
 TEST(DnsHeaderTest, getFlagBits)
249 249
 {
250
-    QDns::QDnsHeader header;
250
+    QDns::Base::QDnsHeader header;
251 251
 
252 252
     header.setFlags(0x5555);
253 253
     ASSERT_EQ(header.getFlagBits(0, 1), 0);
@@ -256,8 +256,8 @@ TEST(DnsHeaderTest, getFlagBits)
256 256
 
257 257
 TEST(DnsHeaderTest, getFlagBitIndex)
258 258
 {
259
-    ASSERT_EQ(QDns::QDnsHeader::getFlagBitIndex(0), 15);
260
-    ASSERT_EQ(QDns::QDnsHeader::getFlagBitIndex(1), 14);
261
-    ASSERT_EQ(QDns::QDnsHeader::getFlagBitIndex(14), 1);
262
-    ASSERT_EQ(QDns::QDnsHeader::getFlagBitIndex(15), 0);
259
+    ASSERT_EQ(QDns::Base::QDnsHeader::getFlagBitIndex(0), 15);
260
+    ASSERT_EQ(QDns::Base::QDnsHeader::getFlagBitIndex(1), 14);
261
+    ASSERT_EQ(QDns::Base::QDnsHeader::getFlagBitIndex(14), 1);
262
+    ASSERT_EQ(QDns::Base::QDnsHeader::getFlagBitIndex(15), 0);
263 263
 }

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

@@ -24,7 +24,7 @@ TEST_P(DnsLabelParseTest, parse)
24 24
 {
25 25
     auto params = GetParam();
26 26
     auto data = QByteArray::fromHex(params.hexData);
27
-    auto label = QDns::QDnsLabel::parse(data, params.pos);
27
+    auto label = QDns::Base::QDnsLabel::parse(data, params.pos);
28 28
     if (params.isValid)
29 29
     {
30 30
         ASSERT_FALSE(label.isNull());
@@ -99,7 +99,7 @@ class DnsLabelSerializeTest : public ::testing::TestWithParam<DnsLabelSerializeT
99 99
 TEST_P(DnsLabelSerializeTest, parse)
100 100
 {
101 101
     auto params = GetParam();
102
-    auto label = QDns::QDnsLabel::serialize(params.label);
102
+    auto label = QDns::Base::QDnsLabel::serialize(params.label);
103 103
     auto data = QByteArray::fromHex(params.hexData);
104 104
     ASSERT_EQ(label, data);
105 105
 }

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

@@ -22,7 +22,7 @@ TEST_P(DnsPacketParseTest, parse)
22 22
 {
23 23
     auto params = GetParam();
24 24
     auto data = QByteArray::fromHex(params.hexData);
25
-    auto packet = QDns::QDnsPacket::parse(data);
25
+    auto packet = QDns::Base::QDnsPacket::parse(data);
26 26
 
27 27
     ASSERT_EQ(packet.isValid(), params.isValid);
28 28
 
@@ -60,7 +60,7 @@ TEST_P(DnsPacketSerializeTest, parse)
60 60
 {
61 61
     auto params = GetParam();
62 62
     auto data1 = QByteArray::fromHex(params.hexData1);
63
-    auto packet = QDns::QDnsPacket::parse(data1);
63
+    auto packet = QDns::Base::QDnsPacket::parse(data1);
64 64
 
65 65
     // TODO improve tests
66 66
     ASSERT_EQ(packet.serialize().toHex(), QString(params.hexData2).remove(" "));

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

@@ -26,7 +26,7 @@ TEST_P(DnsQueryParseTest, parse)
26 26
 {
27 27
     auto params = GetParam();
28 28
     auto data = QByteArray::fromHex(params.hexData);
29
-    auto query = QDns::QDnsQuery::parse(data, params.pos);
29
+    auto query = QDns::Base::QDnsQuery::parse(data, params.pos);
30 30
 
31 31
     ASSERT_EQ(query.isValid(), params.isValid);
32 32
 
@@ -84,7 +84,7 @@ class DnsQuerySerializeTest : public ::testing::TestWithParam<DnsQuerySerializeT
84 84
 TEST_P(DnsQuerySerializeTest, serialize)
85 85
 {
86 86
     auto params = GetParam();
87
-    QDns::QDnsQuery query;
87
+    QDns::Base::QDnsQuery query;
88 88
 
89 89
     query.setName(params.name);
90 90
     query.setType(params.type);

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

@@ -28,7 +28,7 @@ TEST_P(DnsResourceRecordParseTest, parse)
28 28
 {
29 29
     auto params = GetParam();
30 30
     auto data = QByteArray::fromHex(params.hexData);
31
-    auto rr = QDns::QDnsResourceRecord::parse(data, params.pos);
31
+    auto rr = QDns::Base::QDnsResourceRecord::parse(data, params.pos);
32 32
 
33 33
     ASSERT_EQ(rr.isValid(), params.isValid);
34 34
 
@@ -94,7 +94,7 @@ class DnsResourceRecordSerializeTest : public ::testing::TestWithParam<DnsResour
94 94
 TEST_P(DnsResourceRecordSerializeTest, serialize)
95 95
 {
96 96
     auto params = GetParam();
97
-    QDns::QDnsResourceRecord rr;
97
+    QDns::Base::QDnsResourceRecord rr;
98 98
 
99 99
     rr.setName(params.name);
100 100
     rr.setType(params.type);

Loading…
Cancel
Save