forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
querydnsbase.c
executable file
·340 lines (284 loc) · 8.24 KB
/
querydnsbase.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
331
332
333
334
335
336
337
338
339
340
#include <time.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#endif
#include "querydnsbase.h"
#include "dnsparser.h"
#include "dnsgenerator.h"
#include "hosts.h"
#include "utils.h"
#include "excludedlist.h"
#include "stringlist.h"
#include "domainstatistic.h"
#include "request_response.h"
void ShowRefusingMassage(const char *Agent, DNSRecordType Type, const char *Domain, const char *Massage)
{
char DateAndTime[32];
if( ShowMassages == TRUE || DEBUGMODE )
{
GetCurDateAndTime(DateAndTime, sizeof(DateAndTime));
}
if( ShowMassages == TRUE )
{
printf("%s[R][%s][%s][%s] %s.\n",
DateAndTime,
Agent,
DNSGetTypeName(Type),
Domain,
Massage
);
}
DEBUG_FILE("%s[R][%s][%s][%s] %s.\n",
DateAndTime,
Agent,
DNSGetTypeName(Type),
Domain,
Massage
);
}
void ShowTimeOutMassage(const char *Agent, DNSRecordType Type, const char *Domain, char Protocol)
{
char DateAndTime[32];
if( ShowMassages == TRUE || DEBUGMODE )
{
GetCurDateAndTime(DateAndTime, sizeof(DateAndTime));
}
if( ShowMassages == TRUE )
{
printf("%s[%c][%s][%s][%s] Timed out.\n",
DateAndTime,
Protocol,
Agent,
DNSGetTypeName(Type),
Domain
);
}
DEBUG_FILE("%s[%c][%s][%s][%s] Timed out.\n",
DateAndTime,
Protocol,
Agent,
DNSGetTypeName(Type),
Domain
);
}
void ShowErrorMassage(const char *Agent, DNSRecordType Type, const char *Domain, char ProtocolCharacter)
{
char DateAndTime[32];
int ErrorNum = GET_LAST_ERROR();
char ErrorMessage[320];
if( ErrorMessages == TRUE || DEBUGMODE )
{
GetCurDateAndTime(DateAndTime, sizeof(DateAndTime));
ErrorMessage[0] ='\0';
GetErrorMsg(ErrorNum, ErrorMessage, sizeof(ErrorMessage));
}
if( ErrorMessages == TRUE )
{
printf("%s[%c][%s][%s][%s] An error occured : %d : %s .\n",
DateAndTime,
ProtocolCharacter,
Agent,
DNSGetTypeName(Type),
Domain,
ErrorNum,
ErrorMessage
);
}
DEBUG_FILE("%s[%c][%s][%s][%s] An error occured : %d : %s .\n",
DateAndTime,
ProtocolCharacter,
Agent,
DNSGetTypeName(Type),
Domain,
ErrorNum,
ErrorMessage
);
}
void ShowNormalMassage(const char *Agent, const char *RequestingDomain, char *Package, int PackageLength, char ProtocolCharacter)
{
DNSRecordType Type = DNS_TYPE_UNKNOWN;
char DateAndTime[32];
char InfoBuffer[1024];
if( ShowMassages == TRUE || DEBUGMODE )
{
GetCurDateAndTime(DateAndTime, sizeof(DateAndTime));
InfoBuffer[0] = '\0';
GetAllAnswers(Package, PackageLength, InfoBuffer, sizeof(InfoBuffer));
Type = (DNSRecordType)DNSGetRecordType(DNSJumpHeader(Package));
}
if( ShowMassages == TRUE )
{
printf("%s[%c][%s][%s][%s] : %d bytes\n%s",
DateAndTime,
ProtocolCharacter,
Agent,
DNSGetTypeName(Type),
RequestingDomain,
PackageLength,
InfoBuffer
);
}
DEBUG_FILE("%s[%c][%s][%s][%s] : %d bytes\n%s",
DateAndTime,
ProtocolCharacter,
Agent,
DNSGetTypeName(Type),
RequestingDomain,
PackageLength,
InfoBuffer
);
}
void ShowBlockedMessage(const char *RequestingDomain, char *Package, int PackageLength, const char *Message)
{
char DateAndTime[32];
char InfoBuffer[1024];
if( ShowMassages == TRUE || DEBUGMODE )
{
GetCurDateAndTime(DateAndTime, sizeof(DateAndTime));
InfoBuffer[0] = '\0';
GetAllAnswers(Package, PackageLength, InfoBuffer, sizeof(InfoBuffer));
}
if( ShowMassages == TRUE )
{
printf("%s[B][%s] %s :\n%s", DateAndTime, RequestingDomain, Message == NULL ? "" : Message, InfoBuffer);
}
DEBUG_FILE("%s[B][%s] %s :\n%s", DateAndTime, RequestingDomain, Message == NULL ? "" : Message, InfoBuffer);
}
void ShowFatalMessage(const char *Message, int ErrorCode)
{
char ErrorMessage[320];
ErrorMessage[0] = '\0';
if( ErrorMessages == TRUE || DEBUGMODE )
{
GetErrorMsg(ErrorCode, ErrorMessage, sizeof(ErrorMessage));
}
if( ErrorMessages == TRUE )
{
printf("[ERROR] %s %d : %s\n", Message, ErrorCode, ErrorMessage);
}
DEBUG_FILE("[ERROR] %s %d : %s\n", Message, ErrorCode, ErrorMessage);
}
static int QueryFromServer(char *Content, int ContentLength, SOCKET ThisSocket)
{
ControlHeader *Header = (ControlHeader *)Content;
int Interface;
/* Determine whether the secondaries are used */
if( IsExcludedDomain(Header -> RequestingDomain, &(Header -> RequestingDomainHashValue)))
{
Interface = INTERNAL_INTERFACE_SECONDARY;
} else {
Interface = INTERNAL_INTERFACE_PRIMARY;
}
return InternalInterface_SendTo(Interface, ThisSocket, Content, ContentLength);
}
#define DNS_FETCH_FROM_HOSTS_OK 0
#define DNS_FETCH_FROM_HOSTS_NONE_RESULT (-1)
#define DNS_FETCH_FROM_HOSTS_DISABLE_IPV6 (-2)
static int DNSFetchFromHosts(char *Content, int ContentLength, SOCKET ThisSocket)
{
switch ( Hosts_Try(Content, &ContentLength) )
{
case MATCH_STATE_NONE:
case MATCH_STATE_DISABLED:
return DNS_FETCH_FROM_HOSTS_NONE_RESULT;
break;
case MATCH_STATE_DISABLE_IPV6:
return DNS_FETCH_FROM_HOSTS_DISABLE_IPV6;
break;
case MATCH_STATE_ONLY_CNAME:
if( InternalInterface_SendTo(INTERNAL_INTERFACE_HOSTS, ThisSocket, Content, ContentLength) > 0 )
{
return DNS_FETCH_FROM_HOSTS_OK;
} else {
return DNS_FETCH_FROM_HOSTS_NONE_RESULT;
}
break;
case MATCH_STATE_PERFECT:
return ContentLength;
break;
default:
return DNS_FETCH_FROM_HOSTS_NONE_RESULT;
break;
}
}
int QueryBase(char *Content, int ContentLength, int BufferLength, SOCKET ThisSocket)
{
ControlHeader *Header = (ControlHeader *)Content;
char *RequestEntity = Content + sizeof(ControlHeader);
int StateOfReceiving = -1;
/* Check if this domain or type is disabled */
if( IsDisabledType(Header -> RequestingType) )
{
DomainStatistic_Add(Header -> RequestingDomain, &(Header -> RequestingDomainHashValue), STATISTIC_TYPE_REFUSED);
ShowRefusingMassage(Header -> Agent, Header -> RequestingType, Header -> RequestingDomain, "Disabled type");
return QUERY_RESULT_DISABLE;
}
if( IsDisabledDomain(Header -> RequestingDomain, &(Header -> RequestingDomainHashValue)) )
{
DomainStatistic_Add(Header -> RequestingDomain, &(Header -> RequestingDomainHashValue), STATISTIC_TYPE_REFUSED);
ShowRefusingMassage(Header -> Agent, Header -> RequestingType, Header -> RequestingDomain, "Disabled domain");
return QUERY_RESULT_DISABLE;
}
/* Get the QuestionCount */
if( DNSGetQuestionCount(RequestEntity) == 1 )
{
/* First query from hosts and cache */
StateOfReceiving = DNSFetchFromHosts(Content, ContentLength, ThisSocket);
if( StateOfReceiving == DNS_FETCH_FROM_HOSTS_NONE_RESULT )
{
StateOfReceiving = DNSCache_FetchFromCache(RequestEntity, ContentLength - sizeof(ControlHeader), BufferLength - sizeof(ControlHeader));
if( StateOfReceiving > 0 )
{
ShowNormalMassage(Header -> Agent, Header -> RequestingDomain, RequestEntity, StateOfReceiving, 'C');
DomainStatistic_Add(Header -> RequestingDomain, &(Header -> RequestingDomainHashValue), STATISTIC_TYPE_CACHE);
return StateOfReceiving;
}
} else if( StateOfReceiving == DNS_FETCH_FROM_HOSTS_DISABLE_IPV6 )
{
DomainStatistic_Add(Header -> RequestingDomain, &(Header -> RequestingDomainHashValue), STATISTIC_TYPE_REFUSED);
ShowRefusingMassage(Header -> Agent, Header -> RequestingType, Header -> RequestingDomain, "Disabled by hosts");
return QUERY_RESULT_DISABLE;
} else {
DomainStatistic_Add(Header -> RequestingDomain, &(Header -> RequestingDomainHashValue), STATISTIC_TYPE_HOSTS);
if( StateOfReceiving > 0 )
{
ShowNormalMassage(Header -> Agent,
Header -> RequestingDomain,
RequestEntity,
StateOfReceiving - sizeof(ControlHeader),
'H'
);
return StateOfReceiving;
}
}
} else {
StateOfReceiving = -1;
}
/* If hosts or cache has no record, then query from server */
if( StateOfReceiving < 0 )
{
StateOfReceiving = QueryFromServer(Content, ContentLength, ThisSocket);
}
if( StateOfReceiving < 0 )
{
return QUERY_RESULT_ERROR;
} else {
return QUERY_RESULT_SUCCESS;
}
}
int GetMaximumMessageSize(SOCKET sock)
{
#ifdef WIN32
int mms = 0;
int LengthOfInt = sizeof(mms);
getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&mms, &LengthOfInt);
return mms;
#else
return INT_MAX;
#endif
}