forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts.c
327 lines (251 loc) · 5.79 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 <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "hosts.h"
#include "dnsrelated.h"
#include "dnsgenerator.h"
#include "common.h"
#include "utils.h"
#include "downloader.h"
#include "readline.h"
#include "rwlock.h"
static BOOL Internet = FALSE;
static int UpdateInterval;
static time_t LastUpdate = 0;
static const char *File = NULL;
static ThreadHandle GetHosts_Thread;
static RWLock HostsLock;
static volatile HostsContainer *MainContainer = NULL;
static void DynamicHosts_FreeHostsContainer(HostsContainer *Container)
{
StringChunk_Free(&(Container -> Ipv4Hosts), FALSE);
StringChunk_Free(&(Container -> Ipv6Hosts), FALSE);
StringChunk_Free(&(Container -> CNameHosts), FALSE);
StringChunk_Free(&(Container -> ExcludedDomains), FALSE);
StringList_Free(&(Container -> Domains));
ExtendableBuffer_Free(&(Container -> IPs));
}
static int DynamicHosts_Load(void)
{
FILE *fp;
char Buffer[320];
ReadLineStatus Status;
int IPv4Count = 0, IPv6Count = 0, CNameCount = 0, ExcludedCount = 0;
HostsContainer *TempContainer;
fp = fopen(File, "r");
if( fp == NULL )
{
return -1;
}
TempContainer = (HostsContainer *)SafeMalloc(sizeof(HostsContainer));
if( TempContainer == NULL )
{
return -1;
}
if( Hosts_InitContainer(TempContainer) != 0 )
{
fclose(fp);
SafeFree(TempContainer);
return -1;
}
while( TRUE )
{
Status = ReadLine(fp, Buffer, sizeof(Buffer));
if( Status == READ_FAILED_OR_END )
break;
switch( Hosts_LoadFromMetaLine(TempContainer, Buffer) )
{
case HOSTS_TYPE_AAAA:
++IPv6Count;
break;
case HOSTS_TYPE_A:
++IPv4Count;
break;
case HOSTS_TYPE_CNAME:
++CNameCount;
break;
case HOSTS_TYPE_EXCLUEDE:
++ExcludedCount;
break;
default:
break;
}
if( Status == READ_TRUNCATED )
{
ERRORMSG("Hosts is too long : %s\n", Buffer);
ReadLine_GoToNextLine(fp);
}
}
RWLock_WrLock(HostsLock);
if( MainContainer != NULL )
{
DynamicHosts_FreeHostsContainer((HostsContainer *)MainContainer);
SafeFree((void *)MainContainer);
}
MainContainer = TempContainer;
RWLock_UnWLock(HostsLock);
INFO("Loading Hosts completed, %d IPv4 Hosts, %d IPv6 Hosts, %d CName Redirections, %d items are excluded.\n",
IPv4Count,
IPv6Count,
CNameCount,
ExcludedCount);
return 0;
}
static BOOL NeedReload(void)
{
if( File == NULL )
{
return FALSE;
}
if( time(NULL) - LastUpdate > UpdateInterval )
{
#ifdef WIN32
static FILETIME LastFileTime = {0, 0};
WIN32_FIND_DATA Finddata;
HANDLE Handle;
Handle = FindFirstFile(File, &Finddata);
if( Handle == INVALID_HANDLE_VALUE )
{
return FALSE;
}
if( memcmp(&LastFileTime, &(Finddata.ftLastWriteTime), sizeof(FILETIME)) != 0 )
{
LastUpdate = time(NULL);
LastFileTime = Finddata.ftLastWriteTime;
FindClose(Handle);
return TRUE;
} else {
LastUpdate = time(NULL);
FindClose(Handle);
return FALSE;
}
#else /* WIN32 */
static time_t LastFileTime = 0;
struct stat FileStat;
if( stat(File, &FileStat) != 0 )
{
return FALSE;
}
if( LastFileTime != FileStat.st_mtime )
{
LastUpdate = time(NULL);
LastFileTime = FileStat.st_mtime;
return TRUE;
} else {
LastUpdate = time(NULL);
return FALSE;
}
#endif /* WIN32 */
} else {
return FALSE;
}
}
static int TryLoadHosts(void)
{
if( NeedReload() == TRUE )
{
ThreadHandle t = INVALID_THREAD;
CREATE_THREAD(DynamicHosts_Load, NULL, t);
DETACH_THREAD(t);
}
return 0;
}
static void GetHostsFromInternet_Thread(void *Unused)
{
const char *URL = ConfigGetRawString(&ConfigInfo, "Hosts");
const char *Script = ConfigGetRawString(&ConfigInfo, "HostsScript");
int HostsRetryInterval = ConfigGetInt32(&ConfigInfo, "HostsRetryInterval");
INFO("Hosts File : \"%s\" -> \"%s\"\n", URL, File);
while(1)
{
INFO("Getting Hosts From %s ...\n", URL);
if( GetFromInternet(URL, File) == 0 )
{
INFO("Hosts saved at %s.\n", File);
if( Script != NULL )
{
INFO("Running script ...\n");
system(Script);
}
DynamicHosts_Load();
if( UpdateInterval < 0 )
{
return;
}
SLEEP(UpdateInterval * 1000);
} else {
ERRORMSG("Getting Hosts from Internet failed. Waiting %d second(s) for retry.\n", HostsRetryInterval);
SLEEP(HostsRetryInterval * 1000);
}
}
}
int DynamicHosts_Init(void)
{
const char *Path;
StaticHosts_Init();
Path = ConfigGetRawString(&ConfigInfo, "Hosts");
if( Path == NULL )
{
File = NULL;
return 0;
}
UpdateInterval = ConfigGetInt32(&ConfigInfo, "HostsUpdateInterval");
RWLock_Init(HostsLock);
if( strncmp(Path, "http", 4) != 0 && strncmp(Path, "ftp", 3) != 0 )
{
/* Local file */
File = Path;
if( DynamicHosts_Load() != 0 )
{
ERRORMSG("Loading Hosts failed.\n");
File = NULL;
return 1;
}
} else {
/* Internet file */
File = ConfigGetRawString(&ConfigInfo, "HostsDownloadPath");
if( ConfigGetInt32(&ConfigInfo, "HostsRetryInterval") < 1 )
{
ERRORMSG("`HostsFlushTimeOnFailed' is too small (< 1).\n");
File = NULL;
return 1;
}
Internet = TRUE;
if( FileIsReadable(File) )
{
INFO("Loading the existing Hosts ...\n");
DynamicHosts_Load();
} else {
INFO("Hosts file is unreadable, this may cause some failures.\n");
}
CREATE_THREAD(GetHostsFromInternet_Thread, NULL, GetHosts_Thread);
}
LastUpdate = time(NULL);
return 0;
}
int DynamicHosts_GetByQuestion(ThreadContext *Context, int *AnswerCount)
{
int ret = -1;
ret = StaticHosts_GetByQuestion(Context, AnswerCount);
if( ret > 0 )
{
return ret;
}
if( DynamicHosts_Inited() )
{
if( Internet == TRUE )
{
TryLoadHosts();
}
RWLock_RdLock(HostsLock);
ret = Hosts_GetFromContainer(MainContainer, Context, AnswerCount);
RWLock_UnRLock(HostsLock);
return ret;
} else {
return -1;
}
}
BOOL DynamicHosts_Inited(void)
{
return (File != NULL && MainContainer != NULL);
}