Browse Source

Added MX record data

develop
Robin Thoni 6 years ago
parent
commit
38f1e441df

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

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

+ 53
- 0
src/modules/dns-base/includes/dns-base/RecordData/QDnsRecordDataMX.h View File

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

+ 92
- 0
src/modules/dns-base/src/RecordData/QDnsRecordDataMX.cpp View File

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

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

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

+ 47
- 0
src/modules/dns-base/tests/RecordData/DnsRDataMX.cpp View File

@@ -0,0 +1,47 @@
1
+//
2
+// Created by robin on 1/14/18.
3
+//
4
+
5
+#include <gtest/gtest.h>
6
+#include <QByteArray>
7
+#include <QString>
8
+#include <QtNetwork/QHostAddress>
9
+#include <dns-base/RecordData/QDnsRecordDataMX.h>
10
+
11
+struct DnsRDataMXTestParams
12
+{
13
+    bool isValid;
14
+    const char* hexData;
15
+    quint32 pos;
16
+
17
+    quint16 preference;
18
+    QString mx;
19
+};
20
+
21
+class DnsRDataMXTest : public ::testing::TestWithParam<DnsRDataMXTestParams>
22
+{
23
+};
24
+
25
+TEST_P(DnsRDataMXTest, parseAndGetters)
26
+{
27
+    auto params = GetParam();
28
+    auto data = QByteArray::fromHex(params.hexData);
29
+    auto rdata = QDns::Base::QDnsRecordDataMX(data, params.pos);
30
+    ASSERT_EQ(rdata.isValid(), params.isValid);
31
+    
32
+    if (rdata.isValid())
33
+    {
34
+        ASSERT_EQ(params.preference, rdata.getPreference());
35
+        ASSERT_EQ(params.mx, rdata.getMx());
36
+
37
+        ASSERT_EQ(rdata.serialize().toHex(), QString(params.hexData).remove(" "));
38
+    }
39
+}
40
+
41
+INSTANTIATE_TEST_CASE_P(DnsRDataMXTestInst,
42
+                        DnsRDataMXTest,
43
+                        ::testing::Values(
44
+                                DnsRDataMXTestParams {false, "", 0},
45
+                                DnsRDataMXTestParams {false, "00", 0},
46
+                                DnsRDataMXTestParams {true, "003204616c7434056173706d78016c06676f6f676c6503636f6d00", 0, 50, "alt4.aspmx.l.google.com"}
47
+                        ));

Loading…
Cancel
Save