forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domainstatistic.c
executable file
·315 lines (236 loc) · 6.3 KB
/
domainstatistic.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
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "common.h"
#include "stringchunk.h"
#include "extendablebuffer.h"
#include "domainstatistic.h"
#include "utils.h"
#include "querydnsbase.h"
typedef struct _DomainInfo{
int Count;
int Refused;
int Hosts;
int Cache;
int Udp;
int Tcp;
int Spoofed;
} DomainInfo;
typedef struct _RankList{
const char *Domain;
DomainInfo *Info;
} RankList;
static EFFECTIVE_LOCK StatisticLock;
static StringChunk MainChunk;
static int Interval = 0;
static FILE *MainFile = NULL;
static char InitTime_Str[32];
static time_t InitTime_Num;
volatile static BOOL SkipStatistic = FALSE;
int DomainStatistic_Init(int OutputInterval)
{
char FilePath[1024];
if( OutputInterval < 1 )
{
return 1;
}
GetFileDirectory(FilePath);
strcat(FilePath, PATH_SLASH_STR);
strcat(FilePath, "statistic.txt");
MainFile = fopen(FilePath, "w");
if( MainFile == NULL )
{
return 2;
}
EFFECTIVE_LOCK_INIT(StatisticLock);
StringChunk_Init(&MainChunk, NULL);
Interval = OutputInterval * 1000;
GetCurDateAndTime(InitTime_Str, sizeof(InitTime_Str));
InitTime_Num = time(NULL);
return 0;
}
int DomainStatistic_Add(const char *Domain, int *HashValue, StatisticType Type)
{
DomainInfo *ExistInfo;
if( Interval == 0 || Domain == NULL )
{
return 0;
}
EFFECTIVE_LOCK_GET(StatisticLock);
if( SkipStatistic == FALSE )
{
if( StringChunk_Match(&MainChunk, Domain, HashValue, (char **)&ExistInfo) == FALSE )
{
DomainInfo NewInfo;
memset(&NewInfo, 0, sizeof(DomainInfo));
switch( Type )
{
case STATISTIC_TYPE_REFUSED:
NewInfo.Count = 1;
NewInfo.Refused = 1;
break;
case STATISTIC_TYPE_HOSTS:
NewInfo.Count = 1;
NewInfo.Hosts = 1;
break;
case STATISTIC_TYPE_CACHE:
NewInfo.Count = 1;
NewInfo.Cache = 1;
break;
case STATISTIC_TYPE_UDP:
NewInfo.Count = 1;
NewInfo.Udp = 1;
break;
case STATISTIC_TYPE_TCP:
NewInfo.Count = 1;
NewInfo.Tcp = 1;
break;
case STATISTIC_TYPE_POISONED:
NewInfo.Count = 0;
NewInfo.Spoofed = TRUE;
break;
}
StringChunk_Add(&MainChunk, Domain, (const char *)&NewInfo, sizeof(DomainInfo));
} else {
if( ExistInfo != NULL )
{
switch( Type )
{
case STATISTIC_TYPE_REFUSED:
++(ExistInfo -> Count);
++(ExistInfo -> Refused);
break;
case STATISTIC_TYPE_HOSTS:
++(ExistInfo -> Count);
++(ExistInfo -> Hosts);
break;
case STATISTIC_TYPE_CACHE:
++(ExistInfo -> Count);
++(ExistInfo -> Cache);
break;
case STATISTIC_TYPE_UDP:
++(ExistInfo -> Count);
++(ExistInfo -> Udp);
break;
case STATISTIC_TYPE_TCP:
++(ExistInfo -> Count);
++(ExistInfo -> Tcp);
break;
case STATISTIC_TYPE_POISONED:
ExistInfo -> Spoofed = TRUE;
break;
}
}
}
}
EFFECTIVE_LOCK_RELEASE(StatisticLock);
return 0;
}
static int CountCompare(const RankList *_1, const RankList *_2)
{
return (-1) * (_1 -> Info -> Count - _2 -> Info -> Count);
}
int DomainStatistic_Hold(void)
{
const char *Str;
int32_t Enum_Start;
DomainInfo *Info;
DomainInfo Sum;
RankList New;
int DomainCount;
RankList *ARank;
Array Ranks;
int Loop;
char GenerateTime_Str[32];
time_t GenerateTime_Num;
Array_Init(&Ranks, sizeof(RankList), 0, FALSE, NULL);
while(TRUE)
{
SLEEP(Interval);
rewind(MainFile);
memset(&Sum, 0, sizeof(DomainInfo));
Array_Clear(&Ranks);
GetCurDateAndTime(GenerateTime_Str, sizeof(GenerateTime_Str));
GenerateTime_Num = time(NULL);
fprintf(MainFile,
"-----------------------------------------\n"
"Program startup time : %s\n"
"Last statistic : %s\n"
"Elapsed time : %ds\n"
"\n"
"Domain Statistic:\n"
" Refused&Failed Spoofed?\n"
" Domain Total | Hosts Cache UDP TCP |\n",
InitTime_Str,
GenerateTime_Str,
(int)(GenerateTime_Num - InitTime_Num)
);
DomainCount = 0;
Enum_Start = 0;
EFFECTIVE_LOCK_GET(StatisticLock);
SkipStatistic = TRUE;
EFFECTIVE_LOCK_RELEASE(StatisticLock);
Str = StringChunk_Enum_NoWildCard(&MainChunk, &Enum_Start, (char **)&Info);
while( Str != NULL )
{
++DomainCount;
New.Domain = Str;
New.Info = Info;
Array_PushBack(&Ranks, &New, NULL);
Sum.Count += Info -> Count;
Sum.Refused += Info -> Refused;
Sum.Hosts += Info -> Hosts;
Sum.Cache += Info -> Cache;
Sum.Udp += Info -> Udp;
Sum.Tcp += Info -> Tcp;
Sum.Spoofed += Info -> Spoofed;
Str = StringChunk_Enum_NoWildCard(&MainChunk, &Enum_Start, (char **)&Info);
}
Array_Sort(&Ranks, (int (*)(const void *, const void *))CountCompare);
Loop = 0;
ARank = Array_GetBySubscript(&Ranks, 0);
while( ARank != NULL )
{
fprintf(MainFile,
"%55s : %5d %5d %5d %5d %5d %5d %s\n",
ARank -> Domain,
ARank -> Info -> Count,
ARank -> Info -> Refused,
ARank -> Info -> Hosts,
ARank -> Info -> Cache,
ARank -> Info -> Udp,
ARank -> Info -> Tcp,
ARank -> Info -> Spoofed != FALSE ? " Yes" : ""
);
++Loop;
ARank = Array_GetBySubscript(&Ranks, Loop);
}
EFFECTIVE_LOCK_GET(StatisticLock);
SkipStatistic = FALSE;
EFFECTIVE_LOCK_RELEASE(StatisticLock);
fprintf(MainFile, "Total number of : Queried domains : %d\n"
" Requests : %d\n"
" Known spoofed domains : %d\n"
" Refused&Failed : %d\n"
" Responses from hosts : %d\n"
" Responses from cache : %d\n"
" Responses via UDP : %d\n"
" Responses via TCP : %d\n",
DomainCount,
Sum.Count,
Sum.Spoofed,
Sum.Refused,
Sum.Hosts,
Sum.Cache,
Sum.Udp,
Sum.Tcp
);
fprintf(MainFile, "Requests per minute : %.1f\n", (double)Sum.Count / (double)(GenerateTime_Num - InitTime_Num) * 60.0);
if( Sum.Udp + Sum.Tcp + Sum.Cache != 0 )
{
fprintf(MainFile, "Cache utilization : %.1f%%\n", ((double)Sum.Cache / (double)(Sum.Udp + Sum.Tcp + Sum.Cache)) * 100);
}
fprintf(MainFile, "\n-----------------------------------------\n");
fflush(MainFile);
}
}