-
Notifications
You must be signed in to change notification settings - Fork 302
/
dnsparser.h
210 lines (147 loc) · 7.15 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
#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(*(_16BIT_INT *)(ptr)))
#define GET_32_BIT_U_INT(ptr) (ntohl(*(_32BIT_INT *)(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 DNSGetDNSBody(dns_over_tcp_ptr) ((char *)(dns_over_tcp_ptr) + 2)
#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)
/* Handle question record */
char *DNSGetQuestionRecordPosition(char *DNSBody, int Num);
#define DNSJumpOverQuestionRecords(dns_body) DNSGetQuestionRecordPosition((dns_body), DNSGetQuestionCount(dns_body) + 1)
/* Handle resource\answer record */
char *DNSGetAnswerRecordPosition(char *DNSBody, int Num);
#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(ans_start_ptr) + 10)
#define DNSJumpOverAnswerRecords(dns_body) DNSGetAnswerRecordPosition((dns_body), DNSGetAnswerCount(dns_body) + 1)
#define DNSGetARecordLength(record_ptr) (strlen(record_ptr) + 1 + 10 + DNSGetResourceDataLength(record_ptr))
/* Common */
char *DNSJumpOverName(char *NameStart);
int DNSGetHostName(const char *DNSBody, const char *NameStart, char *buffer);
int DNSGetHostNameLength(char *DNSBody, char *NameStart);
#define DNSGetRecordType(rec_start_ptr) GET_16_BIT_U_INT(DNSJumpOverName(rec_start_ptr))
#define DNSGetRecordClass(rec_start_ptr) GET_16_BIT_U_INT(DNSJumpOverName(rec_start_ptr) + 2)
int DNSExpandCName_MoreSpaceNeeded(const char *DNSBody);
void DNSExpandCName(char *DNSBody);
typedef enum _RecordElement{
DNS_UNKNOWN = 0,
DNS_LABELED_NAME,
DNS_32BIT_UINT,
DNS_16BIT_UINT,
DNS_8BIT_UINT,
DNS_PLANT_TEXT,
DNS_IPV4_ADDR = (INT_MAX / 4),
DNS_IPV6_ADDR,
}RecordElement;
typedef struct _ElementDescriptor{
RecordElement element;
char *description;
}ElementDescriptor;
extern const ElementDescriptor DNS_RECORD_A[];
#define NUM_OF_DNS_RECORD_A 1
extern const ElementDescriptor DNS_RECORD_AAAA[];
#define NUM_OF_DNS_RECORD_AAAA 1
extern const ElementDescriptor DNS_RECORD_CNAME[];
#define NUM_OF_DNS_RECORD_CNAME 1
extern const ElementDescriptor DNS_RECORD_SOA[];
#define NUM_OF_DNS_RECORD_SOA 7
extern const ElementDescriptor DNS_RECORD_DOMAIN_POINTER[];
#define NUM_OF_DNS_RECORD_DOMAIN_POINTER 1
extern const ElementDescriptor DNS_RECORD_NAME_SERVER[];
#define NUM_OF_DNS_RECORD_NAME_SERVER 1
int DNSGetDescriptor(DNSRecordType Type, const ElementDescriptor **Buffer);
#ifdef HOST_BIG_ENDIAN
/* DNSMessageFlags, on offset 2(bytes) of a DNS message body, is 2 bytes length.
* For details: http://www.freesoft.org/CIE/RFC/1035/40.htm */
typedef struct _DNSMessageProperties{
_16BIT_UINT 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 */
_16BIT_UINT Type : 4;
_16BIT_UINT AuthoritativeAnswer:1;
_16BIT_UINT TrunCation : 1;
_16BIT_UINT RecursionDesired: 1; /* 0 no, 1 yes */
_16BIT_UINT RecursionAvailable: 1; /* 0 no, 1 yes */
_16BIT_UINT Unused : 3;
/* 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. */
_16BIT_UINT ResponseCode : 4;
}DNSFlags;
#else
typedef struct _DNSMessageProperties{
/* 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. */
_16BIT_UINT ResponseCode : 4;
_16BIT_UINT Unused : 3;
_16BIT_UINT RecursionAvailable: 1; /* 0 no, 1 yes */
_16BIT_UINT RecursionDesired: 1; /* 0 no, 1 yes */
_16BIT_UINT TrunCation : 1;
_16BIT_UINT 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 */
_16BIT_UINT Type : 4;
_16BIT_UINT Direction : 1; /* query (0), or response (1) */
}DNSFlags;
#endif
typedef struct _DNSHeader{
_16BIT_UINT Identifier;
DNSFlags Flags;
_16BIT_UINT QuestionCount;
_16BIT_UINT AnswerCount;
_16BIT_UINT NameServerCount;
_16BIT_UINT AdditionalCount;
}DNSHeader;
#define DNSGetHeader(dns_body_ptr) ((DNSHeader *)(dns_body_ptr))
/* DNS_DataType and DNSDataInfo are for DNSParseData() */
typedef enum _DNS_DataType{
DNS_DATA_TYPE_UNKNOWN = 0,
DNS_DATA_TYPE_INT,
DNS_DATA_TYPE_UINT,
DNS_DATA_TYPE_STRING
}DNS_DataType;
typedef struct _DNSDataInfo{
DNS_DataType DataType;
int DataLength;
}DNSDataInfo;
DNSDataInfo DNSParseData(char *DNSBody,
char *DataBody,
void *Buffer,
int BufferLength,
const ElementDescriptor *Descriptor,
int CountOfDescriptor,
int Num);
/* Convert a DNS message to text */
char *GetAnswer(char *DNSBody, char *DataBody, char *Buffer, DNSRecordType ResourceType);
char *GetAllAnswers(char *DNSBody, char *Buffer);
void DNSParser(char *dns_over_tcp, char *buffer);
#endif /* _DNS_PARSER_H_ */