forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 10
/
filter.c
executable file
·317 lines (252 loc) · 6.24 KB
/
filter.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
#include <stdio.h>
#include <stdlib.h>
#include "filter.h"
#include "stringchunk.h"
#include "bst.h"
#include "common.h"
#include "logs.h"
#include "readline.h"
#include "domainstatistic.h"
#include "rwlock.h"
static Bst *DisabledTypes = NULL;
static StringChunk *DisabledDomain = NULL;
static RWLock DisabledDomainLock = NULL_RWLOCK;
static ConfigFileInfo *CurrConfigInfo = NULL;
static int TypeCompare(const int *_1, const int *_2)
{
return *_1 - *_2;
}
static int InitBst(Bst **t, int (*CompareFunc)(const void *, const void *))
{
*t = malloc(sizeof(Bst));
if( *t == NULL )
{
return -93;
}
if( Bst_Init(*t, sizeof(int), CompareFunc) != 0 )
{
free(*t);
*t = NULL;
return -102;
}
return 0;
}
static int LoadDomainsFromList(StringChunk *List, StringList *Domains)
{
const char *Str;
StringListIterator sli;
if( List == NULL || Domains == NULL )
{
return 0;
}
if( StringListIterator_Init(&sli, Domains) != 0 )
{
return -1;
}
Str = sli.Next(&sli);
while( Str != NULL )
{
StringChunk_Add_Domain(List, Str, NULL, 0);
Str = sli.Next(&sli);
}
return 0;
}
static int FilterDomain_Init(StringChunk **List, ConfigFileInfo *ConfigInfo)
{
StringList *dd = ConfigGetStringList(ConfigInfo, "DisabledDomain");
if( dd == NULL )
{
return 0;
}
if( InitChunk(List) != 0 )
{
return -120;
}
LoadDomainsFromList(*List, dd);
return 0;
}
static int LoadDomainsFromFile(StringChunk *List, const char *FilePath)
{
FILE *fp;
char Domain[512];
if( List == NULL || FilePath == NULL )
{
return 0;
}
fp = fopen(FilePath, "r");
if( fp == NULL )
{
return -118;
}
while( TRUE )
{
ReadLineStatus Status;
Status = ReadLine(fp, Domain, sizeof(Domain));
if( Status == READ_FAILED_OR_END )
{
break;
}
if( Status == READ_DONE )
{
StringChunk_Add_Domain(List, Domain, NULL, 0);
} else {
ReadLine_GoToNextLine(fp);
}
}
fclose(fp);
return 0;
}
static int FilterDomain_InitFromFile(StringChunk **List, ConfigFileInfo *ConfigInfo)
{
StringList *FilePaths = ConfigGetStringList(ConfigInfo, "DisabledList");
const char *FilePath;
StringListIterator sli;
if( FilePaths == NULL )
{
return 0;
}
if( StringListIterator_Init(&sli, FilePaths) != 0 )
{
return -116;
}
if( InitChunk(List) != 0 )
{
return -117;
}
while( (FilePath = sli.Next(&sli)) != NULL )
{
LoadDomainsFromFile(*List, FilePath);
}
return 0;
}
static void FilterType_Cleanup(void)
{
if(DisabledTypes != NULL)
{
DisabledTypes->Free(DisabledTypes);
free(DisabledTypes);
}
RWLock_Destroy(DisabledDomainLock);
}
static int FilterType_Init(ConfigFileInfo *ConfigInfo)
{
StringList *DisableType_Str =
ConfigGetStringList(ConfigInfo, "DisabledType");
const char *OneTypePendingToAdd_Str;
int OneTypePendingToAdd;
StringListIterator sli;
if( DisableType_Str == NULL )
{
return 0;
}
if( InitBst(&DisabledTypes,
(int (*)(const void *, const void *))TypeCompare
) != 0 )
{
return -146;
}
if( StringListIterator_Init(&sli, DisableType_Str) != 0 )
{
return -2;
}
OneTypePendingToAdd_Str = sli.Next(&sli);
while( OneTypePendingToAdd_Str != NULL )
{
sscanf(OneTypePendingToAdd_Str, "%d", &OneTypePendingToAdd);
DisabledTypes->Add(DisabledTypes, &OneTypePendingToAdd);
OneTypePendingToAdd_Str = sli.Next(&sli);
}
DisableType_Str->Free(DisableType_Str);
return 0;
}
static void DisabledDomain_Cleanup(void)
{
if( DisabledDomain != NULL )
{
StringChunk_Free(DisabledDomain, TRUE);
SafeFree(DisabledDomain);
}
}
static int DisabledDomain_Init(ConfigFileInfo *ConfigInfo)
{
StringChunk *TempDisabledDomain = NULL;
if( FilterDomain_Init(&TempDisabledDomain, ConfigInfo) != 0 )
{
INFO("Loading DisabledDomain failed.\n");
return -1;
} else {
INFO("Loading DisabledDomain completed.\n");
}
if( FilterDomain_InitFromFile(&TempDisabledDomain, ConfigInfo) != 0 )
{
StringChunk_Free(TempDisabledDomain, TRUE);
SafeFree(TempDisabledDomain);
INFO("Loading DisabledList failed.\n");
return -1;
} else {
INFO("Loading DisabledList completed.\n");
}
RWLock_WrLock(DisabledDomainLock);
DisabledDomain_Cleanup();
DisabledDomain = TempDisabledDomain;
RWLock_UnWLock(DisabledDomainLock);
return 0;
}
int Filter_Init(ConfigFileInfo *ConfigInfo)
{
CurrConfigInfo = ConfigInfo;
if( FilterType_Init(ConfigInfo) != 0 )
{
INFO("Setting DisabledType failed.\n");
} else {
INFO("Setting DisabledType succeeded.\n");
}
RWLock_Init(DisabledDomainLock);
atexit(FilterType_Cleanup);
DisabledDomain_Init(ConfigInfo);
atexit(DisabledDomain_Cleanup);
return 0;
}
int Filter_Update(void)
{
if ( ConfigGetBoolean(CurrConfigInfo, "ReloadDisabledList") )
{
DisabledDomain_Init(CurrConfigInfo);
}
return 0;
}
static BOOL IsDisabledType(int Type)
{
if( DisabledTypes != NULL &&
DisabledTypes->Search(DisabledTypes, &Type, NULL) != NULL )
{
return TRUE;
} else {
return FALSE;
}
}
static BOOL IsDisabledDomain(const char *Domain, uint32_t HashValue)
{
int ret;
if (DisabledDomain == NULL)
{
return FALSE;
}
RWLock_RdLock(DisabledDomainLock);
ret = StringChunk_Domain_Match(DisabledDomain, Domain, &HashValue, NULL, NULL, NULL);
RWLock_UnRLock(DisabledDomainLock);
return ret;
}
BOOL Filter_Out(MsgContext *MsgCtx)
{
IHeader *h = (IHeader *)MsgCtx;
if(IsDisabledType(h->Type) || IsDisabledDomain(h->Domain, h->HashValue) )
{
MsgContext_SendBackRefusedMessage(MsgCtx);
ShowRefusingMessage(h, "Disabled type or domain");
DomainStatistic_Add(h, STATISTIC_TYPE_REFUSED);
return TRUE;
} else {
return FALSE;
}
}