forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 9
/
hosts.c
executable file
·327 lines (264 loc) · 8.67 KB
/
hosts.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
#include "hosts.h"
#include "addresslist.h"
#include "mcontext.h"
#include "socketpuller.h"
#include "goodiplist.h"
#include "logs.h"
#include "domainstatistic.h"
#include "mmgr.h"
extern BOOL Ipv6_Enabled;
static BOOL BlockIpv6WhenIpv4Exists = FALSE;
static SOCKET InnerSocket;
static Address_Type InnerAddress;
static SocketPuller Puller;
BOOL Hosts_TypeExisting(const char *Domain, HostsRecordType Type)
{
return StaticHosts_TypeExisting(Domain, Type) ||
DynamicHosts_TypeExisting(Domain, Type);
}
static HostsUtilsTryResult Hosts_Try_Inner(MsgContext *MsgCtx, int BufferLength)
{
HostsUtilsTryResult ret;
ret = StaticHosts_Try(MsgCtx, BufferLength);
if( ret != HOSTSUTILS_TRY_NONE )
{
return ret;
}
return DynamicHosts_Try(MsgCtx, BufferLength);
}
static int Hosts_GetCName(const char *Domain, char *Buffer)
{
return !(StaticHosts_GetCName(Domain, Buffer) == 0 ||
DynamicHosts_GetCName(Domain, Buffer) == 0);
}
HostsUtilsTryResult Hosts_Try(MsgContext *MsgCtx, int BufferLength)
{
HostsUtilsTryResult ret;
IHeader *Header = (IHeader *)MsgCtx;
if( BlockIpv6WhenIpv4Exists )
{
if( Header->Type == DNS_TYPE_AAAA &&
(Hosts_TypeExisting(Header->Domain, HOSTS_TYPE_A) ||
Hosts_TypeExisting(Header->Domain, HOSTS_TYPE_GOOD_IP_LIST)
)
)
{
/** TODO: Show blocked message */
return HOSTSUTILS_TRY_BLOCKED;
}
}
if( Hosts_TypeExisting(Header->Domain, HOSTS_TYPE_EXCLUEDE) )
{
return HOSTSUTILS_TRY_NONE;
}
ret = Hosts_Try_Inner(MsgCtx, BufferLength);
if( ret == HOSTSUTILS_TRY_RECURSED )
{
if( sendto(InnerSocket,
(const char *)Header, /* Only send header and identifier */
sizeof(IHeader) + sizeof(uint16_t), /* Only send header and identifier */
MSG_NOSIGNAL,
(const struct sockaddr *)&(InnerAddress.Addr),
GetAddressLength(InnerAddress.family)
)
< 0 )
{
return HOSTSUTILS_TRY_NONE;
}
}
return ret;
}
int Hosts_Get(MsgContext *MsgCtx, int BufferLength)
{
IHeader *Header = (IHeader *)MsgCtx;
switch( Hosts_Try(MsgCtx, BufferLength) )
{
case HOSTSUTILS_TRY_BLOCKED:
MsgContext_SendBackRefusedMessage(MsgCtx);
ShowRefusingMessage(Header, "Disabled because of existing IPv4 host");
DomainStatistic_Add(Header, STATISTIC_TYPE_REFUSED);
return 0;
break;
case HOSTSUTILS_TRY_NONE:
return -126;
break;
case HOSTSUTILS_TRY_RECURSED:
/** TODO: Show hosts message */
return 0;
case HOSTSUTILS_TRY_OK:
ShowNormalMessage(Header, 'H');
DomainStatistic_Add(Header, STATISTIC_TYPE_HOSTS);
return 0;
break;
default:
return -139;
break;
}
}
static int
#ifdef WIN32
WINAPI
#endif
Hosts_SocketLoop(void *Unused)
{
ModuleContext Context;
SOCKET OuterSocket;
Address_Type OuterAddress;
const struct timeval LongTime = {3600, 0};
const struct timeval ShortTime = {10, 0};
struct timeval TimeLimit = LongTime;
#define LEFT_LENGTH_SL (SOCKET_CONTEXT_LENGTH - sizeof(IHeader))
char InnerBuffer[SOCKET_CONTEXT_LENGTH];
MsgContext *InnerMsgCtx = (MsgContext *)InnerBuffer;
IHeader *InnerHeader = (IHeader *)InnerBuffer;
/* char *InnerEntity = InnerBuffer + sizeof(IHeader); */
char OuterBuffer[SOCKET_CONTEXT_LENGTH];
/* MsgContext *OuterMsgCtx = (MsgContext *)OuterBuffer; */
IHeader *OuterHeader = (IHeader *)OuterBuffer;
char *OuterEntity = OuterBuffer + sizeof(IHeader);
int State;
int ret = 0;
OuterSocket = TryBindLocal(Ipv6_Enabled, 10300, &OuterAddress);
if( OuterSocket == INVALID_SOCKET )
{
return -416;
}
if( SocketPuller_Init(&Puller, 0) != 0 )
{
ret = -423;
goto EXIT_1;
}
Puller.Add(&Puller, InnerSocket, NULL, 0);
Puller.Add(&Puller, OuterSocket, NULL, 0);
if( ModuleContext_Init(&Context, SOCKET_CONTEXT_LENGTH) != 0 )
{
ret = -431;
goto EXIT_1;
}
srand(time(NULL));
while( TRUE )
{
SOCKET Pulled;
int Err;
Pulled = Puller.Select(&Puller, &TimeLimit, NULL, TRUE, FALSE, &Err);
if( Pulled == INVALID_SOCKET )
{
if( Err != 0 )
{
ERRORMSG("Fatal error 70.\n");
break;
}
TimeLimit = LongTime;
Context.Sweep(&Context, NULL, NULL);
} else if( Pulled == InnerSocket )
{
/* Recursive query */
MsgContext *MsgCtxStored;
char RecursedDomain[DOMAIN_NAME_LENGTH_MAX + 1];
uint16_t NewIdentifier;
TimeLimit = ShortTime;
State = recvfrom(InnerSocket,
InnerBuffer, /* Receiving a header */
sizeof(InnerBuffer),
0,
NULL,
NULL
);
if( State < 1 )
{
continue;
}
if( Hosts_GetCName(InnerHeader->Domain, RecursedDomain) != 0 )
{
ERRORMSG("Fatal error 221.\n");
continue;
}
NewIdentifier = rand();
if( HostsUtils_GenerateQuery(OuterBuffer,
SOCKET_CONTEXT_LENGTH,
OuterSocket,
&OuterAddress,
MsgContext_IsFromTCP(InnerMsgCtx),
NewIdentifier,
RecursedDomain,
InnerHeader->Type
)
!= 0 )
{
/** TODO: Show an error */
continue;
}
MsgCtxStored = Context.Add(&Context, InnerMsgCtx);
if( MsgCtxStored == NULL )
{
ERRORMSG("Fatal error 230.\n");
continue;
}
OuterHeader->Parent = (IHeader *)MsgCtxStored;
MMgr_Send(OuterBuffer, SOCKET_CONTEXT_LENGTH);
} else if( Pulled == OuterSocket )
{
MsgContext *BackTraceMsgCtx;
IHeader *BackTraceHeader;
TimeLimit = ShortTime;
State = recvfrom(OuterSocket,
OuterBuffer, /* Receiving a header */
sizeof(OuterBuffer),
0,
NULL,
NULL
);
if( State < 1 )
{
continue;
}
BackTraceHeader = OuterHeader->Parent;
BackTraceMsgCtx = (MsgContext *)BackTraceHeader;
DNSCopyQueryIdentifier(InnerHeader + 1, BackTraceHeader + 1);
if( Context.GenAnswerHeaderAndRemove(&Context, BackTraceMsgCtx, InnerMsgCtx) != 0 )
{
ERRORMSG("Fatal error 267.\n");
continue;
}
if( HostsUtils_CombineRecursedResponse((MsgContext *)InnerBuffer,
SOCKET_CONTEXT_LENGTH,
OuterEntity,
State,
OuterHeader->Domain
)
!= 0 )
{
ERRORMSG("Fatal error 279.\n");
continue;
}
if( MsgContext_SendBack((MsgContext *)InnerBuffer) != 0 )
{
ERRORMSG("Fatal error 285.\n");
continue;
}
ShowNormalMessage(InnerHeader, 'H');
}
}
ModuleContext_Free(&Context);
EXIT_1:
Puller.Free(&Puller);
return ret;
}
int Hosts_Init(ConfigFileInfo *ConfigInfo)
{
ThreadHandle t;
StaticHosts_Init(ConfigInfo);
DynamicHosts_Init(ConfigInfo);
GoodIpList_Init(ConfigInfo);
BlockIpv6WhenIpv4Exists = ConfigGetBoolean(ConfigInfo,
"BlockIpv6WhenIpv4Exists"
);
InnerSocket = TryBindLocal(Ipv6_Enabled, 10200, &InnerAddress);
if( InnerSocket == INVALID_SOCKET )
{
return -25;
}
CREATE_THREAD(Hosts_SocketLoop, NULL, t);
DETACH_THREAD(t);
return 0;
}