-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDnsProtokol.h
More file actions
127 lines (107 loc) · 4.54 KB
/
DnsProtokol.h
File metadata and controls
127 lines (107 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* Copyright (C) 2016-2020 Thomas Hauck - All Rights Reserved.
Distributed under MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
The author would be happy if changes and
improvements were reported back to him.
Author: Thomas Hauck
Email: Thomas@fam-hauck.de
*/
#pragma once
#include <string>
#include <vector>
using namespace std;
class DnsProtokol
{
typedef struct
{
unsigned short ID; // A 16 bit identifier assigned by the program that generates any kind of query
unsigned short QR : 1; // 0 = query, 1 = response
unsigned short Opcode : 4; // 0 = a standard query(QUERY), 1 = an inverse query(IQUERY), 2 = a server status request(STATUS), 3-15 = reserved for future use
unsigned short AA : 1; // Authoritative Answer
unsigned short TC : 1; // TrunCation
unsigned short RD : 1; // Recursion Desired
unsigned short RA : 1; // Recursion Available
unsigned short Z : 3; // Reserved for future use
unsigned short RCODE : 4; // Response code
unsigned short QDCOUNT; // number of entries in the question section
unsigned short ANCOUNT; // number of resource records in the answer section
unsigned short NSCOUNT; // number of name server resource records in the authority records section
unsigned short ARCOUNT; // number of resource records in the additional records section
}DNSHEADER;
typedef struct
{
string LABEL;
unsigned short QTYPE;
unsigned short QCLASS;
}QUESTTION;
typedef pair<size_t, string> LABELENTRY;
typedef vector<LABELENTRY> LABELLIST;
typedef pair<size_t, LABELLIST> OFFSETLABELLIST;
typedef vector<OFFSETLABELLIST> OFFSETLIST;
class DnsProtoException : exception
{
public:
explicit DnsProtoException(const char* szMsg) : strError(szMsg) {}
virtual ~DnsProtoException() throw() {}
const char* what() const throw() { return strError.c_str(); }
private:
string strError;
};
public:
typedef pair<size_t, string> IDxSTRING;
typedef struct
{
unsigned short Priority;
unsigned short Weight;
unsigned short Port;
IDxSTRING strHost;
}SRVDATA;
typedef union
{
void* pVoid;
IDxSTRING* ptrData;
vector<string>* txtData;
SRVDATA* svData;
const char* szAddr;
}RDATA;
typedef struct
{
IDxSTRING strLabel;
RDATA rData;
unsigned short usType;
unsigned short usClass;
int iTtl;
}ANSWERITEM;
typedef struct
{
string LABEL;
unsigned short TYPE;
unsigned short CLASS;
unsigned int TTL;
unsigned short RDLENGTH;
string RDATA;
}RRECORDS;
public:
DnsProtokol() {};
DnsProtokol(unsigned char* szBuffer, size_t nBytInBuf);
virtual ~DnsProtokol();
size_t BuildSearch(const string& strQuestion, char* szBuffer, size_t& nBuflen);
size_t BuildAnswer(vector<ANSWERITEM>& AnList, vector<ANSWERITEM>& NsList, vector<ANSWERITEM>& ArList, char* szBuffer, size_t& nBuflen);
private:
size_t ExtractLabels(const unsigned char* pLabel, const unsigned char* pBuffer, size_t nBytInBuf, string& strLabel);
size_t ExtractQuestion(const unsigned char* pCurPointer, const unsigned char* pBuffer, size_t nBytInBuf, short nNoQuestion, QUESTTION* pQuestion);
size_t ExtractRRecords(const unsigned char* pCurPointer, const unsigned char* pBuffer, size_t nBytInBuf, short nNoRecords, RRECORDS* pRRecord);
size_t BuildLabelReferenc(const string& strLabel, OFFSETLIST& OffListe);
char* BuildLabels(OFFSETLIST& OffListe, size_t index, char* pBufPointer, size_t& nBufLen, size_t nBufOffset = 0);
char* BuildQuestion(OFFSETLIST& lstOffsetListe, size_t iLabelIndex, short QTYPE, short QCLASS, char* pBufPointer, size_t& nBufLen, const char* pBufStart);
char* BuildRRecord(OFFSETLIST& OffListe, size_t iLabelIndex, unsigned short TYPE, unsigned short CLASS, int TTL, char* pBufPointer, size_t& nBufLen, const char* pBufStart);
char* BuildRData(unsigned short TYPE, RDATA rData, char* pBufPointer, size_t& nBufLen, OFFSETLIST& OffListe, const char* pBufStart);
public:
DNSHEADER m_DnsHeader;
unique_ptr<QUESTTION[]> m_pQuestions;
unique_ptr<RRECORDS[]> m_pAnswers;
unique_ptr<RRECORDS[]> m_pNameServ;
unique_ptr<RRECORDS[]> m_pExtraRec;
string m_strLastErrMsg;
size_t m_nBytesDecodet;
};