-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.c
366 lines (318 loc) · 12.9 KB
/
tester.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
#include "include.h"
#include <math.h>
extern struct event_base *mainBase;
struct timeval tv;
static void *rootForId = NULL;
struct DNSTask *dnstask;
struct struct_LuaTask *luaTask;
struct Task *task, *ptask;
char *ptr;
int isNewConfig = 0;
int isNewTimer = 0;
int isNewResolv = 0;
int compareId(const void *left, const void *right) {
if (((struct Task *) left)->LObjId > ((struct Task *) right)->LObjId) return 1;
if (((struct Task *) left)->LObjId < ((struct Task *) right)->LObjId) return -1;
return 0;
}
struct Task * searchTask(int LObjId, short flag) {
task = getNulledMemory(sizeof (*task));
task->LObjId = LObjId;
ptask = *(struct Task **) tsearch((void *) task, &rootForId, compareId);
if (ptask != task) {
free(task);
task = ptask;
} else if (flag) {
deleteTask(task);
return NULL;
}
return task;
};
void deleteTask(struct Task *task) {
debug("id is %d%s%s%s",task->LObjId,task->isSub?", isSub":"",task->isEnd?", isEnd":"",task->isVerifyTask?", isVerifyTask":"");
if (task->LObjId and task->isSub == 0) {
tdelete((void *) task, &rootForId, compareId);
}
if (task->ptr and task->isSub == 0) {
if (task->Record.ModType != MODULE_DNS and((struct struct_LuaTask *) task->ptr)->ptr) {
((struct struct_LuaTask *) task->ptr)->state = 0;
free(((struct struct_LuaTask *) task->ptr)->ptr);
}
free(task->ptr);
task->ptr = 0;
}
if (task->poll) {
free(task->poll);
}
if (task->resolv) {
evtimer_del(&task->resolv->timer);
event_del(&task->resolv->ev);
free(task->resolv);
}
free(task);
}
#define addSubTaskResolv(method) \
if (isNewResolv) {\
if (Record->ResolvePeriod or method != DNS_RESOLV) {\
dnstask = getNulledMemory(sizeof (*dnstask)); \
dnstask->task = task; \
task->resolv = dnstask; \
dnstask->role = method; \
tv.tv_usec = tv.tv_sec = 0; \
event_assign(&dnstask->timer, getResolvBase(), -1, 0, timerResolv, task);\
evtimer_add(&dnstask->timer, &tv); \
}\
}
#define addTimer(callback,base) \
if (isNewTimer) {\
timerclear(&tv);\
tv.tv_sec = Record->NextCheckDt ? Record->NextCheckDt - pServer->timeOfLastUpdate : 0;\
event_assign(&task->time_ev, base?base:mainBase, -1, 0, callback, task);\
evtimer_add(&task->time_ev, &tv);\
task->timeShift=newShift;\
}
/*
tv.tv_usec=0;\
tv.tv_sec = task->newTimer;\
task->newTimer=0;\
evtimer_del(&task->time_ev);\
evtimer_set(&task->time_ev, callback, task);\
evtimer_add(&task->time_ev, &tv);\
*/
#define modifyCallbackTimer(callback) \
debug("Lobj %d NewRemainder = %d, OldRemainder = %d, prevNextCheckDt = %d, NextCheckDt = %d", Record->LObjId, newShift, task->timeShift, task->Record.NextCheckDt, Record->NextCheckDt);\
if (newShift != task->timeShift and task->newTimer == 0 and task->isShiftActive == 0) {\
if ((task->Record.NextCheckDt - pServer->timeOfLastUpdate) > config.minRecheckPeriod and (Record->NextCheckDt - pServer->timeOfLastUpdate) > config.minRecheckPeriod ) {\
evtimer_del(&task->time_ev);\
isNewTimer = 1;\
} else {\
task->newTimer = (Record->CheckPeriod - (task->timeShift - newShift)) % Record->CheckPeriod;\
if (task->newTimer <= config.minRecheckPeriod) {\
task->newTimer += task->Record.CheckPeriod;\
}\
debug("\tAs Remainder Modify id %d, set Timer - %d, prevNextCheckDt - %d, NextCheckDt - %d ",\
Record->LObjId, task->newTimer,\
task->Record.NextCheckDt, Record->NextCheckDt);\
}\
task->timeShift=newShift;\
}
#define removeifChangeTask() \
if (task->Record.ModType != Record->ModType or task->Record.ModType != Record->ModType or strcmp(Record->HostName,task->Record.HostName)) {\
task->Record.CheckPeriod = 0;\
setEnd(task);\
pthread_mutex_unlock(mutex);\
return;\
}
void _addTCPTask(Server *pServer, struct _Tester_Cfg_Record * Record, int newShift, int isVerifyTask) {
debug("mutex_lock")
static pthread_mutex_t *mutex = NULL;
if (!mutex) {
mutex = calloc(1, sizeof (*mutex));
pthread_mutex_init(mutex, NULL);
}
pthread_mutex_lock(mutex);
isNewTimer = 0;
isNewResolv = 0;
task = createTask(Record->LObjId);
if (task->Record.LObjId) {
removeifChangeTask();
modifyCallbackTimer(timerTCPTask);
debug("REPLACE TCP id %d, Module %s for %s [%s:%d]", Record->LObjId, getModuleText(Record->ModType), Record->HostName, ipString(Record->IP), Record->Port);
} else {
task->pServer = pServer;
task->poll = getNulledMemory(sizeof (struct stTCPUDPInfo));
task->callback = addTCPReport;
if(isVerifyTask) task->isVerifyTask = 1;
isNewTimer = 1;
isNewResolv = 1;
debug("NEW TCP id %d, Module %s for %s [%s:%d]", Record->LObjId, getModuleText(Record->ModType), Record->HostName, ipString(Record->IP), Record->Port);
}
task->timeOfLastUpdate = task->pServer->timeOfLastUpdate;
memcpy(&((task)->Record), Record, sizeof (*Record));
addSubTaskResolv(DNS_RESOLV);
addTimer(timerTCPTask, getTCPBase());
debug("mutex_unlock")
pthread_mutex_unlock(mutex);
};
void _addICMPTask(Server *pServer, struct _Tester_Cfg_Record * Record, int newShift,int isVerifyTask) {
debug("mutex_lock")
static pthread_mutex_t *mutex = NULL;
if (!mutex) {
mutex = calloc(1, sizeof (*mutex));
pthread_mutex_init(mutex, NULL);
}
pthread_mutex_lock(mutex);
isNewTimer = 0;
isNewResolv = 0;
task = createTask(Record->LObjId);
if (task->Record.LObjId) {
removeifChangeTask();
modifyCallbackTimer(timerICMPTask);
debug("REPLACE ICMP id %d, Module %s for %s [%s:%d]", Record->LObjId, getModuleText(Record->ModType), Record->HostName, ipString(Record->IP), Record->Port);
} else {
task->pServer = pServer;
task->poll = getNulledMemory(sizeof (struct stTCPUDPInfo));
task->callback = addICMPReport;
if(isVerifyTask) task->isVerifyTask = 1;
isNewTimer = 1;
isNewResolv = 1;
debug("NEW ICMP id %d, Module %s for %s [%s:%d]", Record->LObjId, getModuleText(Record->ModType), Record->HostName, ipString(Record->IP), Record->Port);
}
task->timeOfLastUpdate = task->pServer->timeOfLastUpdate;
memcpy(&((task)->Record), Record, sizeof (*Record));
addSubTaskResolv(DNS_RESOLV);
addTimer(timerICMPTask, getICMPBase());
debug("mutex_unlock")
pthread_mutex_unlock(mutex);
}
void _addLuaTask(Server *pServer, struct _Tester_Cfg_Record * Record, int newShift,int isVerifyTask, char *data) {
debug("mutex_lock")
static pthread_mutex_t *mutex = NULL;
if (!mutex) {
mutex = calloc(1, sizeof (*mutex));
pthread_mutex_init(mutex, NULL);
}
pthread_mutex_lock(mutex);
isNewConfig = 0;
isNewTimer = 0;
isNewResolv = 0;
task = createTask(Record->LObjId);
if (task->Record.LObjId) {
removeifChangeTask();
modifyCallbackTimer(timerLuaTask);
debug("TRY TO REPLACE LUA -> id %d, Module %s for %s [%s:%d] [config %s]", Record->LObjId, getModuleText(Record->ModType), Record->HostName, ipString(Record->IP), Record->Port, config);
if (Record->ConfigLen != task->Record.ConfigLen) {
debug("REPLACE LUA -> id %d, Module %s for %s [%s:%d] [config %s]", Record->LObjId, getModuleText(Record->ModType), Record->HostName, ipString(Record->IP), Record->Port, config);
free(((struct struct_LuaTask *) task->ptr)->ptr);
((struct struct_LuaTask *) task->ptr)->ptr = getNulledMemory(Record->ConfigLen + 1);
isNewConfig = 1;
} else if (memcmp(data, ((struct struct_LuaTask *) task->ptr)->ptr, Record->ConfigLen)) {
isNewConfig = 1;
}
} else {
task->pServer = pServer;
task->poll = getNulledMemory(sizeof (struct stTCPUDPInfo));
task->callback = addTCPReport;
if(isVerifyTask) task->isVerifyTask = 1;
task->ptr = getNulledMemory(sizeof (struct struct_LuaTask));
((struct struct_LuaTask *) task->ptr)->ptr = getNulledMemory(Record->ConfigLen + 1);
isNewTimer = 1;
isNewResolv = 1;
isNewConfig = 1;
debug("NEW LUA -> id %d, Module %s for %s [%s:%d] [config %s]", Record->LObjId, getModuleText(Record->ModType), Record->HostName, ipString(Record->IP), Record->Port, data);
}
luaTask = (struct struct_LuaTask *) task->ptr;
memcpy(&((task)->Record), Record, sizeof (*Record));
task->timeOfLastUpdate = task->pServer->timeOfLastUpdate;
if (isNewConfig) memcpy(luaTask->ptr, data, Record->ConfigLen);
addSubTaskResolv(DNS_RESOLV);
addTimer(timerLuaTask, getLuaBase(Record->ModType));
debug("mutex_unlock")
pthread_mutex_unlock(mutex);
}
void _addDNSTask(Server *pServer, struct _Tester_Cfg_Record * Record, int newShift,int isVerifyTask, char *data) {
debug("mutex_lock")
static pthread_mutex_t *mutex = NULL;
if (!mutex) {
mutex = calloc(1, sizeof (*mutex));
pthread_mutex_init(mutex, NULL);
}
pthread_mutex_lock(mutex);
u_short countByte = 0, countCountByte = 0;
isNewConfig = 0;
isNewTimer = 0;
isNewResolv = 0;
task = createTask(Record->LObjId);
if (task->Record.LObjId) {
removeifChangeTask();
modifyCallbackTimer(timerDNSTask);
debug("REPLACE DNS -> id %d, Module %s for %s [%s:%d] [config %s]", Record->LObjId, getModuleText(Record->ModType), Record->HostName, ipString(Record->IP), Record->Port, data);
if (Record->ConfigLen != task->Record.ConfigLen) {
free(task->ptr);
task->ptr = getNulledMemory(Record->ConfigLen + 1);
isNewConfig = 1;
} else if (memcmp(data, task->ptr, Record->ConfigLen)) {
isNewConfig = 1;
}
if (isNewConfig) {
if (inet_addr(Record->HostName) != -1) task->isHostAsIp = 1;
else task->isHostAsIp = 0;
}
} else {
if (inet_addr(Record->HostName) != -1) task->isHostAsIp = 1;
else task->isHostAsIp = 0;
if(isVerifyTask) task->isVerifyTask = 1;
task->pServer = pServer;
dnstask = getNulledMemory(sizeof (*dnstask));
dnstask->task = task;
dnstask->role = DNS_TASK;
task->poll = dnstask;
task->ptr = getNulledMemory(Record->ConfigLen + 1);
task->callback = addDNSReport;
debug("NEW DNS -> id %d, Module %s for %s [%s:%d] [config %s]", Record->LObjId, getModuleText(Record->ModType), Record->HostName, ipString(Record->IP), Record->Port, data);
isNewConfig = 1;
isNewResolv = 1;
isNewTimer = 1;
}
memcpy(&((task)->Record), Record, sizeof (*Record));
task->timeOfLastUpdate = task->pServer->timeOfLastUpdate;
if (isNewConfig) {
memcpy(task->ptr, data, Record->ConfigLen);
ptr = task->ptr;
dnstask = (struct DNSTask *) task->poll;
if (ptr[1] != 0x33) {
setEnd(task);
goto end;
}
ptr += 2;
if (!memcmp(ptr, "11a", 3)) {
dnstask->taskType = T_A;
ptr += 3;
} else if (!memcmp(ptr, "15cname", 7)) {
dnstask->taskType = T_CNAME;
ptr += 7;
} else if (!memcmp(ptr, "12ns", 4)) {
dnstask->taskType = T_NS;
ptr += 4;
} else if (!memcmp(ptr, "12mx", 4)) {
dnstask->taskType = T_MX;
ptr += 4;
} else if (!memcmp(ptr, "13ptr", 5)) {
dnstask->taskType = T_PTR;
ptr += 5;
} else {
setEnd(task);
goto end;
}
countCountByte = ptr[0] - 0x30;
ptr++;
for (; countCountByte > 0; countCountByte--, ++ptr) {
countByte += (ptr[0] - 0x30) * pow(10, countCountByte - 1);
}
for (dnstask->taskTTL = 0; countByte > 0; countByte--, ++ptr) {
dnstask->taskTTL += (ptr[0] - 0x30) * pow(10, countByte - 1);
}
countCountByte = ptr[0] - 0x30;
ptr++;
for (dnstask->taskPatternLen = 0; countCountByte > 0; countCountByte--, ++ptr) {
dnstask->taskPatternLen += (ptr[0] - 0x30) * pow(10, countCountByte - 1);
}
dnstask->taskPattern = ptr;
}
end:
addSubTaskResolv(DNS_GETNS);
addTimer(timerDNSTask, getDNSBase());
/*
11 a
14 7200
214 82.179.196.194
*/
debug("mutex_unlock")
pthread_mutex_unlock(mutex);
};
void initTester() {
initICMPTester();
initTCPTester();
initDNSTester();
initLUATester();
}