forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
querydnslistentcp.c
executable file
·398 lines (311 loc) · 9.24 KB
/
querydnslistentcp.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "querydnslistentcp.h"
#include "querydnsbase.h"
#include "dnsrelated.h"
#include "dnsparser.h"
#include "dnsgenerator.h"
#include "common.h"
#include "utils.h"
#include "stringlist.h"
#include "excludedlist.h"
#include "addresslist.h"
#include "internalsocket.h"
/* Variables */
static BOOL Inited = FALSE;
static SOCKET TCPIncomeSocket;
static SOCKET TCPOutcomeSocket;
static Address_Type TCPOutcomeAddress;
static int RefusingResponseCode = 0;
static QueryContext Context;
/* Functions */
int QueryDNSListenTCPInit(ConfigFileInfo *ConfigInfo)
{
RefusingResponseCode = ConfigGetInt32(ConfigInfo, "RefusingResponseCode");
TCPIncomeSocket = InternalInterface_OpenTCP(MAIN_WORKING_ADDRESS, INTERNAL_INTERFACE_TCP_INCOME, MAIN_WORKING_PORT);
if( TCPIncomeSocket == INVALID_SOCKET )
{
ShowFatalMessage("Opening TCP socket failed.", GET_LAST_ERROR());
return -1;
} else {
INFO("TCP socket %s:%d created.\n", MAIN_WORKING_ADDRESS, MAIN_WORKING_PORT);
}
InternalInterface_GetAddress(INTERNAL_INTERFACE_TCP_INCOME, NULL);
TCPOutcomeSocket = InternalInterface_TryBindLocal(10000, &TCPOutcomeAddress);
InternalInterface_InitQueryContext(&Context);
Inited = TRUE;
return 0;
}
static int Query(char *Content, int ContentLength, int BufferLength, SOCKET ThisSocket)
{
int State;
uint16_t TCPLength = htons(ContentLength - sizeof(ControlHeader));
ControlHeader *Header = (ControlHeader *)Content;
char *RequestEntity = Content + sizeof(ControlHeader);
Header -> RequestingDomain[0] = '\0';
DNSGetHostName(RequestEntity,
DNSJumpHeader(RequestEntity),
Header -> RequestingDomain,
sizeof(Header -> RequestingDomain)
);
StrToLower(Header -> RequestingDomain);
Header -> RequestingType =
(DNSRecordType)DNSGetRecordType(DNSJumpHeader(RequestEntity));
Header -> RequestingDomainHashValue = ELFHash(Header -> RequestingDomain, 0);
State = QueryBase(Content, ContentLength, BufferLength, TCPOutcomeSocket);
switch( State )
{
case QUERY_RESULT_SUCCESS:
InternalInterface_QueryContextAddTCP(&Context, Header, ThisSocket);
return 0;
break;
case QUERY_RESULT_DISABLE:
((DNSHeader *)(RequestEntity)) -> Flags.Direction = 1;
((DNSHeader *)(RequestEntity)) -> Flags.RecursionAvailable = 1;
((DNSHeader *)(RequestEntity)) -> Flags.ResponseCode = RefusingResponseCode;
send(ThisSocket, (const char *)&TCPLength, 2, 0);
send(ThisSocket, RequestEntity, ContentLength - sizeof(ControlHeader), 0);
return -1;
break;
case QUERY_RESULT_ERROR:
return -1;
break;
default: /* Cache */
{
uint16_t NewTCPLength = htons(State);
send(ThisSocket, (const char *)&NewTCPLength, 2, 0);
send(ThisSocket, RequestEntity, State, 0);
return 0;
}
break;
}
}
typedef struct _SocketInfo {
SOCKET Socket;
char Address[LENGTH_OF_IPV6_ADDRESS_ASCII + 1];
time_t TimeAdd;
} SocketInfo;
static Bst si;
static int SocketInfoCompare(const SocketInfo *_1, const SocketInfo *_2)
{
return (int)(_1 -> Socket) - (int)(_2 -> Socket);
}
static int InitSocketInfo(void)
{
return Bst_Init(&si, NULL, sizeof(SocketInfo), (int (*)(const void *, const void *))SocketInfoCompare);
}
static SOCKET SocketInfoMatch(fd_set *ReadySet, fd_set *ReadSet, char *ClientAddress, int32_t *Number)
{
int32_t Start = -1;
SocketInfo *Info;
time_t Now = time(NULL);
Info = Bst_Enum(&si, &Start);
while( Info != NULL )
{
if( FD_ISSET(Info -> Socket, ReadySet) )
{
Info -> TimeAdd = Now;
strcpy(ClientAddress, Info -> Address);
if( Number != NULL )
{
*Number = Start;
}
return Info -> Socket;
}
if( Now - Info -> TimeAdd > 2 )
{
CLOSE_SOCKET(Info -> Socket);
FD_CLR(Info -> Socket, ReadSet);
Bst_Delete_ByNumber(&si, Start);
}
Info = Bst_Enum(&si, &Start);
}
return INVALID_SOCKET;
}
static int SocketInfoAdd(SOCKET Socket, const char *Address)
{
SocketInfo New;
New.Socket = Socket;
strcpy(New.Address, Address);
New.TimeAdd = time(NULL);
return Bst_Add(&si, &New);
}
static BOOL SocketInfoSwep(fd_set *ReadSet)
{
int32_t Start = -1;
SocketInfo *Info;
time_t Now = time(NULL);
Info = Bst_Enum(&si, &Start);
while( Info != NULL )
{
if( Now - Info -> TimeAdd > 2 )
{
CLOSE_SOCKET(Info -> Socket);
FD_CLR(Info -> Socket, ReadSet);
INFO("TCP connection to client %s closed.\n", Info -> Address);
Bst_Delete_ByNumber(&si, Start);
}
Info = Bst_Enum(&si, &Start);
}
return Bst_IsEmpty(&si);
}
static void SendBack(char *Result, int Length)
{
ControlHeader *Header = (ControlHeader *)Result;
char *RequestEntity = Result + sizeof(ControlHeader);
uint16_t Identifier = *(uint16_t *)RequestEntity;
int32_t Number;
QueryContextEntry *Entry;
uint16_t EntityLength = Length - sizeof(ControlHeader);
uint16_t EntityLength_n = htons(EntityLength);
Number = InternalInterface_QueryContextFind(&Context, Identifier, Header -> RequestingDomainHashValue);
if( Number < 0 )
{
return;
}
Entry = Bst_GetDataByNumber(&Context, Number);
send(Entry -> Context.Socket, (const char *)&EntityLength_n, 2, 0);
send(Entry -> Context.Socket, RequestEntity, EntityLength, 0);
InternalInterface_QueryContextRemoveByNumber(&Context, Number);
}
static int QueryDNSListenTCP(void)
{
int MaxFd = TCPIncomeSocket > TCPOutcomeSocket ? TCPIncomeSocket : TCPOutcomeSocket;
static fd_set ReadSet, ReadySet;
int NumberOfQueryBeforeSwep = 0;
static const struct timeval LongTime = {3600, 0};
static const struct timeval ShortTime = {10, 0};
struct timeval TimeLimit = LongTime;
static char RequestEntity[2048];
ControlHeader *Header = (ControlHeader *)RequestEntity;
InternalInterface_InitControlHeader(Header);
memcpy(&(Header -> BackAddress), &TCPOutcomeAddress, sizeof(Address_Type));
Header -> NeededHeader = TRUE;
InitSocketInfo();
FD_ZERO(&ReadSet);
FD_ZERO(&ReadySet);
FD_SET(TCPIncomeSocket, &ReadSet);
FD_SET(TCPOutcomeSocket, &ReadSet);
while( TRUE )
{
ReadySet = ReadSet;
switch( select(MaxFd + 1, &ReadySet, NULL, NULL, &TimeLimit) )
{
case SOCKET_ERROR:
{
int LastError = GET_LAST_ERROR();
ERRORMSG("SOCKET_ERROR Reached, 1.\n");
if( FatalErrorDecideding(LastError) != 0 )
{
ERRORMSG("\n\n\n\n\n\n\n\n\n\n");
ERRORMSG(" !!!!! Something bad happend, please restart this program. %d\n", LastError);
while( TRUE ) SLEEP(100000);
}
}
break;
case 0:
if( SocketInfoSwep(&ReadSet) == TRUE )
{
Bst_Reset(&Context);
TimeLimit = LongTime;
} else {
InternalInterface_QueryContextSwep(&Context, 10, NULL);
TimeLimit = ShortTime;
}
NumberOfQueryBeforeSwep = 0;
break;
default:
TimeLimit = ShortTime;
++NumberOfQueryBeforeSwep;
if( NumberOfQueryBeforeSwep > 1024 )
{
InternalInterface_QueryContextSwep(&Context, 2, NULL);
SocketInfoSwep(&ReadSet);
NumberOfQueryBeforeSwep = 0;
}
if( FD_ISSET(TCPIncomeSocket, &ReadySet) )
{
SOCKET NewSocket;
Address_Type Address;
socklen_t AddrLen;
char AddressString[LENGTH_OF_IPV6_ADDRESS_ASCII + 1];
if( MAIN_FAMILY == AF_INET )
{
AddrLen = sizeof(struct sockaddr);
NewSocket = accept(TCPIncomeSocket,
(struct sockaddr *)&(Address.Addr.Addr4),
(socklen_t *)&AddrLen
);
} else {
AddrLen = sizeof(struct sockaddr_in6);
NewSocket = accept(TCPIncomeSocket,
(struct sockaddr *)&(Address.Addr.Addr6),
(socklen_t *)&AddrLen
);
}
if( NewSocket != INVALID_SOCKET )
{
FD_SET(NewSocket, &ReadSet);
if( MAIN_FAMILY == AF_INET )
{
strcpy(AddressString, inet_ntoa(Address.Addr.Addr4.sin_addr));
} else {
IPv6AddressToAsc(&(Address.Addr.Addr6.sin6_addr), AddressString);
}
if( NewSocket > MaxFd )
{
MaxFd = NewSocket;
}
SocketInfoAdd(NewSocket, AddressString);
INFO("TCP connection to client %s established.\n", AddressString);
}
} else if( FD_ISSET(TCPOutcomeSocket, &ReadySet) )
{
static char Result[2048];
int State;
State = recvfrom(TCPOutcomeSocket, Result, sizeof(Result), 0, NULL, NULL);
if( State > 0 )
{
SendBack(Result, State);
}
} else {
SOCKET Socket;
int State;
int32_t Number;
Socket = SocketInfoMatch(&ReadySet, &ReadSet, Header -> Agent, &Number);
if( Socket != INVALID_SOCKET )
{
uint16_t TCPLength;
if( recv(Socket, (char *)&TCPLength, 2, MSG_NOSIGNAL) < 2 )
{
Bst_Delete_ByNumber(&si, Number);
FD_CLR(Socket, &ReadSet);
CLOSE_SOCKET(Socket);
break;
}
TCPLength = ntohs(TCPLength);
State = recv(Socket, RequestEntity + sizeof(ControlHeader), sizeof(RequestEntity) - sizeof(ControlHeader), MSG_NOSIGNAL);
if( State < 1 )
{
Bst_Delete_ByNumber(&si, Number);
FD_CLR(Socket, &ReadSet);
CLOSE_SOCKET(Socket);
INFO("Lost TCP connection to client %s.\n", Header -> Agent);
break;
}
Query(RequestEntity, State + sizeof(ControlHeader), sizeof(RequestEntity), Socket);
}
}
break;
}
}
return 0;
}
void QueryDNSListenTCPStart(void)
{
ThreadHandle t;
CREATE_THREAD(QueryDNSListenTCP, NULL, t);
DETACH_THREAD(t);
}