forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 9
/
dnsparser.h
executable file
·277 lines (205 loc) · 9.76 KB
/
dnsparser.h
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#ifndef _DNS_PARSER_H_
#define _DNS_PARSER_H_
#include <limits.h>
#include "common.h"
#include "dnsrelated.h"
#define GET_16_BIT_U_INT(ptr) (ntohs(*(int16_t *)(ptr)))
#define GET_32_BIT_U_INT(ptr) (ntohl(*(int32_t *)(ptr)))
#define GET_8_BIT_U_INT(ptr) (*(unsigned char*)(ptr))
#define DNS_HEADER_LENGTH 12
/* Handle DNS header*/
#define DNSGetTCPLength(dns_over_tcp_ptr) GET_16_BIT_U_INT(dns_over_tcp_ptr)
#define DNSGetQueryIdentifier(dns_body) GET_16_BIT_U_INT((char *)(dns_body))
#define DNSGetFlags(dns_body) GET_16_BIT_U_INT((char *)(dns_body) + 2)
#define DNSGetQuestionCount(dns_body) GET_16_BIT_U_INT((char *)(dns_body) + 4)
#define DNSGetAnswerCount(dns_body) GET_16_BIT_U_INT((char *)(dns_body) + 6)
#define DNSGetNameServerCount(dns_body) GET_16_BIT_U_INT((char *)(dns_body) + 8)
#define DNSGetAdditionalCount(dns_body) GET_16_BIT_U_INT((char *)(dns_body) + 10)
#define DNSJumpHeader(dns_body) ((char *)(dns_body) + DNS_HEADER_LENGTH)
#define DNSGetTTL(ans_start_ptr) GET_32_BIT_U_INT(DNSJumpOverName(ans_start_ptr) + 4)
#define DNSGetResourceDataLength(ans_start_ptr) GET_16_BIT_U_INT(DNSJumpOverName(ans_start_ptr) + 8)
#define DNSGetResourceDataPos(ans_start_ptr) (DNSJumpOverName((char *)(ans_start_ptr)) + 10)
/* Common */
char *DNSJumpOverName(char *NameStart);
int DNSGetHostName(const char *DNSBody, int DNSBodyLength, const char *NameStart, char *buffer, int BufferLength);
int DNSGetHostNameLength(const char *DNSBody, int DNSBodyLength, const char *NameStart);
#define DNSGetRecordType(rec_start_ptr) ((rec_start_ptr) == NULL ? DNS_TYPE_UNKNOWN : GET_16_BIT_U_INT(DNSJumpOverName(rec_start_ptr)))
#define DNSIsLabelPointerStart(num) (((num) & 0xC0) == 0xC0)
#define DNSLabelGetPointer(rec_start_ptr) ((rec_start_ptr) == NULL ? 0 : (int)((unsigned char *)(rec_start_ptr))[1] + (int)(((unsigned char *)(rec_start_ptr))[0] - 192) * 256)
#define DNSGetRecordClass(rec_start_ptr) ((rec_start_ptr) == NULL ? DNS_CLASS_UNKNOWN : GET_16_BIT_U_INT(DNSJumpOverName(rec_start_ptr) + 2))
#ifdef HOST_BIG_ENDIAN
/* DNSMessageFlags, at 2-byte offset of a DNS header, is 2 bytes length.
* https://tools.ietf.org/html/rfc6895
*/
typedef struct _DNSMessageProperties{
uint16_t Direction : 1; /* query (0), or response (1) */
/* Type:
* 0 a standard query (QUERY).
* 1 an inverse query (IQUERY).
* 2 a server status request (STATUS).
* 3-15 reserved for future use */
uint16_t Type : 4;
uint16_t AuthoritativeAnswer:1;
uint16_t TrunCation : 1;
uint16_t RecursionDesired: 1; /* 0 no, 1 yes */
uint16_t RecursionAvailable: 1; /* 0 no, 1 yes */
uint16_t Unused : 1;
uint16_t AuthenticData : 1;
uint16_t CheckingDisabled: 1;
/* ResponseCode:
* 0 No error condition.
* 1 Format error - The name server was unable to interpret the query.
* 2 Server failure - The name server was unable to process this query due to a problem with the name server.
* 3 Name Error - Meaningful only for responses from an authoritative name server, this code signifies that the domain name referenced in the query does not exist.
* 4 Not Implemented - The name server does not support the requested kind of query.
* 5 Refused - The name server refuses to perform the specified operation for policy reasons. For example, a name server may not wish to provide the information to the particular requester, or a name server may not wish to perform a particular operation (e.g., zone transfer) for particular data.
* 6-15 Reserved for future use. */
uint16_t ResponseCode : 4;
}DNSFlags;
#else
typedef struct _DNSMessageProperties{
uint16_t RecursionDesired: 1; /* 0 no, 1 yes */
uint16_t TrunCation : 1;
uint16_t AuthoritativeAnswer:1;
/* Type:
* 0 a standard query (QUERY).
* 1 an inverse query (IQUERY).
* 2 a server status request (STATUS).
* 3-15 reserved for future use */
uint16_t Type : 4;
uint16_t Direction : 1; /* query (0), or response (1) */
/* ResponseCode:
* 0 No error condition.
* 1 Format error - The name server was unable to interpret the query.
* 2 Server failure - The name server was unable to process this query due to a problem with the name server.
* 3 Name Error - Meaningful only for responses from an authoritative name server, this code signifies that the domain name referenced in the query does not exist.
* 4 Not Implemented - The name server does not support the requested kind of query.
* 5 Refused - The name server refuses to perform the specified operation for policy reasons. For example, a name server may not wish to provide the information to the particular requester, or a name server may not wish to perform a particular operation (e.g., zone transfer) for particular data.
* 6-15 Reserved for future use. */
uint16_t ResponseCode : 4;
uint16_t CheckingDisabled: 1;
uint16_t AuthenticData : 1;
uint16_t Unused : 1;
uint16_t RecursionAvailable: 1; /* 0 no, 1 yes */
}DNSFlags;
#endif
typedef struct _DNSHeader{
uint16_t Identifier;
DNSFlags Flags;
uint16_t QuestionCount;
uint16_t AnswerCount;
uint16_t NameServerCount;
uint16_t AdditionalCount;
}DNSHeader;
#define DNSGetHeader(dns_body_ptr) ((DNSHeader *)(dns_body_ptr))
/* Convert a DNS message to text */
char *GetAllAnswers(char *DNSBody, int DNSBodyLength, char *Buffer, int BufferLength);
int DNSCopyLable(const char *DNSBody, char *here, const char *src);
/**
New Implementation
*/
typedef enum _DnsDirection{
DNS_DIRECTION_QUERY = 0,
DNS_DIRECTION_RESPONSE = 1,
} DnsDirection;
typedef enum _DnsOperation{
DNS_OPERATION_QUERY = 0,
DNS_OPERATION_IQUERY = 1,
DNS_OPERATION_STATUS = 2,
} DnsOperation;
typedef enum _ResponseCode{
RESPONSE_CODE_NO_ERROR = 0,
RESPONSE_CODE_FORMAT_ERROR = 1,
RESPONSE_CODE_SERVER_FAILURE = 2,
RESPONSE_CODE_NAME_ERROR = 3,
RESPONSE_CODE_NOT_IMPLEMENTED = 4,
RESPONSE_CODE_REFUSED = 5,
} ResponseCode;
typedef enum _DnsRecordPurpose{
/* Their values are not important */
DNS_RECORD_PURPOSE_UNKNOWN = 0,
DNS_RECORD_PURPOSE_QUESTION,
DNS_RECORD_PURPOSE_ANSWER,
DNS_RECORD_PURPOSE_NAME_SERVER,
DNS_RECORD_PURPOSE_ADDITIONAL,
} DnsRecordPurpose;
typedef struct _DnsSimpleParser DnsSimpleParser;
struct _DnsSimpleParser{
/* public, but don't modify outside, unless you know what are you doing */
char *RawDns;
int RawDnsLength;
struct {
/* private */
const DNSFlags *Flags;
/* public */
DnsDirection (*Direction)(const DnsSimpleParser *p);
DnsOperation (*Operation)(const DnsSimpleParser *p);
BOOL (*IsAuthoritative)(const DnsSimpleParser *p);
BOOL (*Truncated)(const DnsSimpleParser *p);
BOOL (*RecursionDesired)(const DnsSimpleParser *p);
BOOL (*RecursionAvailable)(const DnsSimpleParser *p);
ResponseCode (*ResponseCode)(const DnsSimpleParser *p);
} _Flags;
/* public */
uint16_t (*QueryIdentifier)(const DnsSimpleParser *p);
int (*QuestionCount)(const DnsSimpleParser *p);
int (*AnswerCount)(const DnsSimpleParser *p);
int (*NameServerCount)(const DnsSimpleParser *p);
int (*AdditionalCount)(const DnsSimpleParser *p);
BOOL (*HasType)(const DnsSimpleParser *p,
DnsRecordPurpose Purpose,
DNSRecordClass Klass,
DNSRecordType Type
);
};
int DnsSimpleParser_Init(DnsSimpleParser *p,
char *RawDns,
int Length,
BOOL IsTcp);
/**
Iterator
*/
typedef struct _DnsSimpleParserIterator DnsSimpleParserIterator;
struct _DnsSimpleParserIterator{
/* public, but don't modify outside */
DnsSimpleParser *Parser;
/* private */
char *CurrentPosition;
int RecordPosition; /* Starts at 1 */
int QuestionFirst; /* Starts at 1; 0 means no such record */
int QuestionLast;
int AnswerFirst; /* Starts at 1; 0 means no such record */
int AnswerLast;
int NameServerFirst; /* Starts at 1; 0 means no such record */
int NameServerLast;
int AdditionalFirst; /* Starts at 1; 0 means no such record */
int AdditionalLast;
int AllRecordCount;
/* public, but don't modify outside */
/* Current record informations */
DnsRecordPurpose Purpose;
DNSRecordType Type;
DNSRecordClass Klass;
int DataLength;
char *(*Next)(DnsSimpleParserIterator *i);
void (*GotoAnswers)(DnsSimpleParserIterator *i);
int (*GetName)(DnsSimpleParserIterator *i,
char *Buffer, /* Could be NULL */
int BufferLength
);
char *(*RowData)(DnsSimpleParserIterator *i);
int (*TextifyData)(DnsSimpleParserIterator *i,
const char *Format, /* "%t: %v\n" */
char *Buffer,
int BufferLength
);
int (*ToCacheData)(DnsSimpleParserIterator *i,
char *Buffer,
int BufferLength
);
uint32_t (*GetTTL)(DnsSimpleParserIterator *i);
};
int DnsSimpleParserIterator_Init(DnsSimpleParserIterator *i,
DnsSimpleParser *p
);
#endif /* _DNS_PARSER_H_ */