forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsgenerator.c
executable file
·330 lines (276 loc) · 7.54 KB
/
dnsgenerator.c
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <stdio.h>
#include "dnsgenerator.h"
#include "utils.h"
#include "dnsparser.h"
const char OptPseudoRecord[] = {
0x00,
0x00, 0x29,
0x05, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00
};
/* Other Codes */
char *DNSLabelizedName(__inout char *Origin, __in size_t OriginSpaceLength){
unsigned char *LabelLength = (unsigned char *)Origin;
if( *Origin == '\0' )
return Origin + 1;
if( OriginSpaceLength < strlen(Origin) + 2 )
return NULL;
memmove(Origin + 1, Origin, strlen(Origin) + 1);
*LabelLength = 0;
while(1){
++Origin;
if(*Origin == 0) break;
if(*Origin != '.') ++(*LabelLength);
else {
LabelLength = (unsigned char *)Origin;
*LabelLength = 0;
}
}
return Origin + 1;
}
/* No checking */
int DNSCompress(__inout char *DNSBody, __in int DNSBodyLength)
{
char *DNSEnd = DNSBody + DNSBodyLength;
int AnswerCount;
int loop;
char *LastName, *CurName;
char *LastData;
char *NameEnd;
DNSRecordType LastType;
AnswerCount = DNSGetAnswerCount(DNSBody);
if(AnswerCount == 0) return DNSBodyLength;
/* The First Stage */
LastName = (char *)DNSJumpHeader(DNSBody);
CurName = (char *)DNSGetAnswerRecordPosition(DNSBody, 1);
NameEnd = (char *)DNSJumpOverName(CurName);
*(unsigned char *)CurName = 0xC0;
*(unsigned char *)(CurName + 1) = (unsigned char)(LastName - DNSBody);
DNSBodyLength -= (NameEnd - CurName) - 2;
memmove(CurName + 2, NameEnd, DNSEnd - NameEnd);
DNSEnd -= (NameEnd - CurName) - 2;
/* The Second Stage */
LastData = LastName;
for(loop = 2; loop <= AnswerCount; ++loop)
{
LastName = CurName;
CurName = (char *)DNSGetAnswerRecordPosition(DNSBody, loop);
LastType = (DNSRecordType)DNSGetRecordType(LastName);
if(LastType == DNS_TYPE_CNAME)
{
LastData = (char *)DNSGetResourceDataPos(LastName);
}
NameEnd = (char *)DNSJumpOverName(CurName);
*(unsigned char *)CurName = 0xC0;
*(unsigned char *)(CurName + 1) = (unsigned char)(LastData - DNSBody);
DNSBodyLength -= (NameEnd - CurName) - 2;
memmove(CurName + 2, NameEnd, DNSEnd - NameEnd);
DNSEnd -= (NameEnd - CurName) - 2;
}
return DNSEnd - DNSBody;
}
char *DNSGenHeader( __out char *Buffer,
__in uint16_t QueryIdentifier,
__in DNSFlags Flags,
__in uint16_t QuestionCount,
__in uint16_t AnswerCount,
__in uint16_t NameServerCount,
__in uint16_t AdditionalCount
){
DNSSetQueryIdentifier(Buffer, QueryIdentifier);
DNSSetFlags(Buffer, *(uint16_t *)&Flags);
DNSSetQuestionCount(Buffer, QuestionCount);
DNSSetAnswerCount(Buffer, AnswerCount);
DNSSetNameServerCount(Buffer, NameServerCount);
DNSSetAdditionalCount(Buffer, AdditionalCount);
return DNSJumpHeader(Buffer);
}
int DNSGenQuestionRecord(__out char *Buffer,
__in int BufferLength,
__in const char *Name,
__in uint16_t Type,
__in uint16_t Class
){
int NameLen;
NameLen = strlen(Name);
if(BufferLength < NameLen + 2 + 2 + 2) return 0;
strcpy(Buffer, Name);
if( DNSLabelizedName(Buffer, NameLen + 2) == NULL )
{
return 0;
}
SET_16_BIT_U_INT(Buffer + NameLen + 2, Type);
SET_16_BIT_U_INT(Buffer + NameLen + 4, Class);
return NameLen + 2 + 2 + 2;
}
int DNSGenResourceRecord( __out char *Buffer,
__in int BufferLength,
__in const char *Name,
__in uint16_t Type,
__in uint16_t Class,
__in uint32_t TTL,
__in const void *Data,
__in uint16_t DataLength,
__in BOOL LablelizedData
)
{
int NameLen;
int DataNeededLength;
if(*Name != '\0')
NameLen = strlen(Name) + 1;
else
NameLen = 0;
if( LablelizedData == TRUE )
{
DataNeededLength = DataLength + 1;
} else {
DataNeededLength = DataLength;
}
if( Buffer != NULL )
{
if(BufferLength < NameLen + 1 + 2 + 2 + 4 + 2 + DataNeededLength)
return 0;
strcpy(Buffer, Name);
if( DNSLabelizedName(Buffer, BufferLength) == NULL )
return 0;
SET_16_BIT_U_INT(Buffer + NameLen + 1, Type);
SET_16_BIT_U_INT(Buffer + NameLen + 3, Class);
SET_32_BIT_U_INT(Buffer + NameLen + 5, TTL);
SET_16_BIT_U_INT(Buffer + NameLen + 9, DataNeededLength);
if( Data != NULL && DataLength != 0 )
{
memcpy(Buffer + NameLen + 11, Data, DataLength);
}
if( LablelizedData == TRUE )
{
if( DNSLabelizedName(Buffer + NameLen + 11, DataNeededLength) == NULL )
return 0;
}
}
return NameLen + 1 + 2 + 2 + 4 + 2 + DataNeededLength;
}
int DNSGenerateData(__in char *Data,
__out void *Buffer,
__in size_t BufferLength,
__in const ElementDescriptor *Descriptor
)
{
switch(Descriptor -> element)
{
case DNS_LABELED_NAME:
if( Buffer != NULL )
{
if(BufferLength < strlen(Data) + 2) return -1;
strcpy((char *)Buffer, Data);
DNSLabelizedName((char *)Buffer, BufferLength);
}
return strlen(Data) + 2;
break;
case DNS_CHARACTER_STRING:
if( Buffer != NULL )
{
if( BufferLength < strlen(Data) + 1 )
{
return -1;
}
*(unsigned char *)Buffer = strlen(Data);
memcpy(((char *)Buffer) + 1, Data, strlen(Data));
}
return strlen(Data) + 1;
break;
case DNS_32BIT_UINT:
if( Buffer != NULL )
{
int a;
if(BufferLength < 4) return -1;
sscanf(Data, "%d", &a);
SET_32_BIT_U_INT(Buffer, a);
}
return 4;
break;
case DNS_16BIT_UINT:
if( Buffer != NULL )
{
int a;
if(BufferLength < 2) return -1;
sscanf(Data, "%d", &a);
SET_16_BIT_U_INT(Buffer, a);
}
return 2;
break;
case DNS_8BIT_UINT:
if( Buffer != NULL )
{
if(BufferLength < 1) return -1;
*(char *)Buffer = *(char *)Data;
}
return 1;
break;
case DNS_IPV4_ADDR:
if( Buffer != NULL )
{
int a[4];
if(BufferLength < 4) return -1;
sscanf(Data, "%d.%d.%d.%d", a, a + 1, a + 2, a + 3);
((unsigned char *)Buffer)[0] = a[0];
((unsigned char *)Buffer)[1] = a[1];
((unsigned char *)Buffer)[2] = a[2];
((unsigned char *)Buffer)[3] = a[3];
}
return 4;
break;
case DNS_IPV6_ADDR:
if( Buffer != NULL )
{
int a[8];
if(BufferLength < 16) return -1;
sscanf(Data, "%x:%x:%x:%x:%x:%x:%x:%x",
a, a + 1, a + 2, a + 3, a + 4, a + 5, a + 6, a + 7
);
SET_16_BIT_U_INT((uint16_t *)Buffer, a[0]);
SET_16_BIT_U_INT((uint16_t *)Buffer + 1, a[1]);
SET_16_BIT_U_INT((uint16_t *)Buffer + 2, a[2]);
SET_16_BIT_U_INT((uint16_t *)Buffer + 3, a[3]);
SET_16_BIT_U_INT((uint16_t *)Buffer + 4, a[4]);
SET_16_BIT_U_INT((uint16_t *)Buffer + 5, a[5]);
SET_16_BIT_U_INT((uint16_t *)Buffer + 6, a[6]);
SET_16_BIT_U_INT((uint16_t *)Buffer + 7, a[7]);
}
return 16;
break;
default:
return 0;
break;
}
}
int DNSAppendAnswerRecord(__inout char *OriginBody, __in char *Record, __in int RecordLength)
{
memcpy((void *)DNSJumpOverAnswerRecords(OriginBody), Record, RecordLength);
DNSSetAnswerCount(OriginBody, DNSGetAnswerCount(OriginBody) + 1);
return DNSJumpOverAnswerRecords(OriginBody) - OriginBody + RecordLength;
}
int DNSRemoveEDNSPseudoRecord(char *RequestContent, int *RequestLength)
{
if( DNSGetAdditionalCount(RequestContent) == 1 )
{
const char *AdditionalRecords;
AdditionalRecords = DNSJumpOverQuestionRecords(RequestContent);
if( DNSGetRecordType(AdditionalRecords) == DNS_TYPE_OPT )
{
DNSSetAdditionalCount(RequestContent, 0);
*RequestLength -= OPT_PSEUDORECORD_LENGTH;
return EDNS_REMOVED;
} else {
return EDNS_NOT_EDNS;
}
} else {
return EDNS_NO_AR;
}
}
void DNSAppendEDNSPseudoRecord(char *RequestContent, int *RequestLength)
{
memcpy((char *)RequestContent + *RequestLength, OptPseudoRecord, OPT_PSEUDORECORD_LENGTH);
DNSSetAdditionalCount(RequestContent, 1);
*RequestLength += OPT_PSEUDORECORD_LENGTH;
}