-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.c
249 lines (206 loc) · 9.2 KB
/
process.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
#include "include.h"
#include <zlib.h>
#ifndef __TIMESTAMP__
#define __TIMESTAMP__ __DATE__" "__TIME__
#endif
const char version[] = "Tester v0.09/l"PLATFORM" ["LUA_RELEASE",c-ares "ARES_VERSION_STR","_EVENT_PACKAGE" "_EVENT_VERSION"], Compiled:"__DATE__" "__TIME__", Source:"__FILE__","__TIMESTAMP__"";
static struct evbuffer *evBuffer = NULL;
static struct timeval tv;
void initProcess() {
}
void freeProcess() {
}
void onErrorFromServer(Server *pServer, int code) {
switch (code) {
case SOCK_RC_AUTH_FAILURE:
closeConnection(pServer, FALSE);
break;
case SOCK_RC_TIMEOUT:
closeConnection(pServer, FALSE);
break;
}
debug("%s[%d] host [%s:%d]", getErrorText(code), code, pServer->host, pServer->port);
}
void RequestSend(Server *pServer, u32 type, struct evbuffer *evSend) {
struct Poll *poll = pServer->poll;
//#define COMPRESS
struct evbuffer *evReq = evbuffer_new();
struct Request Req, *pReq;
bzero((char *) & Req, Size_Request);
evbuffer_add(evReq, (char *) & Req, Size_Request);
if (evSend > 0) {
evbuffer_add_buffer(evReq, evSend);
}
debug("len:%d to %s:%d 0x%08x", evbuffer_get_length(evReq), pServer->host, pServer->port, pServer->poll->bev);
pReq = (struct Request *) (EVBUFFER_DATA(evReq));
pReq->sizes.UncmprSize = evbuffer_get_length(evReq) - Size_Request_sizes;
pReq->hdr.TesterId = config.testerid;
pReq->hdr.ReqType = type;
#ifdef COMPRESS
if (pReq->sizes.UncmprSize > 200) {
int lenAlloc = (pReq->sizes.UncmprSize)*1.01 + 12 + Size_Request_sizes;
int lenCmpr = lenAlloc - Size_Request_sizes;
char *ptrCmpr;
ptrCmpr = getNulledMemory(lenAlloc);
memcpy(ptrCmpr, pReq, Size_Request_sizes);
compress2((Bytef *) ptrCmpr + Size_Request_sizes, (uLongf *) & lenCmpr, (Bytef *) & pReq->hdr, evbuffer_get_length(evReq) - Size_Request_sizes, Z_DEFAULT_COMPRESSION);
evbuffer_free(evReq);
evReq = evbuffer_new();
evbuffer_add(evReq, ptrCmpr, lenCmpr + Size_Request_sizes);
free(ptrCmpr);
pReq = (struct Request *) EVBUFFER_DATA(evReq);
pReq->sizes.CmprSize = lenCmpr;
}
#endif
pReq->sizes.crc = crc32(0xffffffff, (const Bytef *) pReq, evbuffer_get_length(evReq));
/*
#ifdef DEBUG
printf(cBLUE"\treq->sizes.CmprSize=%d"cEND, pReq->sizes.CmprSize);
printf(cBLUE"\treq->sizes.UncmprSize=%d"cEND, pReq->sizes.UncmprSize);
printf(cBLUE"\treq->sizes.crc=0x%08x\n"cEND, pReq->sizes.crc);
#ifdef HEXPRINT
hexPrint((char *) EVBUFFER_DATA(evReq), evbuffer_get_length(evReq));
#endif
#endif
*/
bufferevent_write_buffer(poll->bev, evReq);
evbuffer_free(evReq);
}
void loadTask(Server *pServer) {
debug("from %s:%d", pServer->host, pServer->port);
if (!evBuffer) {
evBuffer = evbuffer_new();
}
evbuffer_add(evBuffer, version, sizeof (version));
RequestSend(pServer, MODULE_READ_OBJECTS_CFG, evBuffer);
};
int onLoadTask(Server *pServer) {
struct Poll *poll = pServer->poll;
struct _Tester_Cfg_AddData *Cfg_AddData;
struct _Tester_Cfg_Record *Cfg_Record;
struct Request *pReq, *pReqCmpr;
struct evbuffer *buffer = EVBUFFER_INPUT(poll->bev);
u_char *data = EVBUFFER_DATA(buffer);
u_int len = EVBUFFER_LENGTH(buffer);
char *ptr = 0;
u32 crc = 0;
pReq = (struct Request *) data;
if (len < Size_Request) {
return TRUE;
}
if (pReq->sizes.UncmprSize < Size_Request_hdr or pReq->sizes.UncmprSize > MAX_UNCMPR or(pReq->sizes.CmprSize > 0 and pReq->sizes.CmprSize > MAX_CMPR)) {
warn("mismatch length of packet");
return TRUE;
}
crc = pReq->sizes.crc;
pReq->sizes.crc = 0;
if (crc32(0xffffffff, (const Bytef *) pReq, (pReq->sizes.CmprSize > 0 ? pReq->sizes.CmprSize : pReq->sizes.UncmprSize) + Size_Request_sizes) != crc) {
pReq->sizes.crc = crc;
return FALSE;
}
if (pReq->sizes.CmprSize) {
ptr = malloc(pReq->sizes.UncmprSize + Size_Request_sizes);
bzero(ptr, pReq->sizes.UncmprSize + Size_Request_sizes);
memcpy(ptr, data, Size_Request_sizes);
pReqCmpr = (struct Request *) ptr;
uncompress((Bytef *) (ptr + Size_Request_sizes), (uLongf *) & pReqCmpr->sizes.UncmprSize, (const Bytef *) & pReq->hdr, (uLongf) pReq->sizes.CmprSize);
if (pReqCmpr->sizes.UncmprSize != pReq->sizes.UncmprSize) {
return TRUE;
}
pReq = pReqCmpr;
}
Cfg_AddData = (struct _Tester_Cfg_AddData *) & pReq->Data;
#ifdef CFG_3
if (Cfg_AddData->ProtocolVersion != PROTOCOL_VERSION) {
return TRUE;
}
#endif
if (Cfg_AddData->CfgRereadPeriod != pServer->periodRetrieve) {
debug("set new period reread config from %d to %d", pServer->periodRetrieve, Cfg_AddData->CfgRereadPeriod);
pServer->periodRetrieve = Cfg_AddData->CfgRereadPeriod;
tv.tv_usec = 0;
tv.tv_sec = pServer->periodRetrieve;
evtimer_del(&pServer->evConfig);
evtimer_set(&pServer->evConfig, timerRetrieveTask, pServer);
evtimer_add(&pServer->evConfig, &tv);
}
gettimeofday(&tv, NULL);
config.TimeStabilization = tv.tv_sec - Cfg_AddData->ServerTime;
pServer->timeOfLastUpdate = tv.tv_sec;
debug("remote time %d, new offset %d", Cfg_AddData->ServerTime, config.TimeStabilization);
int RecordsLen = (pReq->sizes.UncmprSize - sizeof (* Cfg_AddData) - Size_Request);
int RecordOffset;
int newShift;
for (
RecordOffset = sizeof (* Cfg_AddData);
RecordOffset < RecordsLen;
RecordOffset += sizeof (* Cfg_Record)
) {
// Cfg_Record = (struct _Tester_Cfg_Record *) (&pReq->Data + RecordOffset);
Cfg_Record = (void *) & pReq->Data + RecordOffset;
if (Cfg_Record->TimeOut == 0 or Cfg_Record->TimeOut >= config.timeout * 950)
Cfg_Record->TimeOut = config.timeout * 950;
#ifdef DEBUG
debug("\tid объекта тестирования = %d", Cfg_Record->LObjId);
debug("\tтип модуля тестирования (пинг-порт-хттп-...) = %s", getModuleText(Cfg_Record->ModType));
debug("\tпорт првоерки = %hd", (short) Cfg_Record->Port);
debug("\tпериодичность проверки объекта в секундах = %d", Cfg_Record->CheckPeriod);
debug("\tпериодичность проверки ИП-адреса в секундах, или 0, если проверку делать не нужно = %d", Cfg_Record->ResolvePeriod);
debug("\tип объекта тестирования = %s", ipString(Cfg_Record->IP));
debug("\tдата ближайшей проверки. unixtime utc = %d", Cfg_Record->NextCheckDt);
debug("\tимя хоста = %s", Cfg_Record->HostName);
debug("\tLObjId предыдущего хоста в folded-цепочке или 0 = %d", Cfg_Record->FoldedNext);
debug("\tLObjId следующего хоста в folded-цепочке или 0 = %d", Cfg_Record->FoldedPrev);
debug("\tТаймаут для проверки = %d", Cfg_Record->TimeOut);
debug("\tРазмер дополнительных данных = %d", Cfg_Record->ConfigLen);
#endif
if (Cfg_Record->ConfigLen > RecordsLen) return TRUE;
newShift = Cfg_Record->NextCheckDt % Cfg_Record->CheckPeriod;
Cfg_Record->NextCheckDt = ((tv.tv_sec / Cfg_Record->CheckPeriod) * Cfg_Record->CheckPeriod) + RemoteToLocalTime(Cfg_Record->NextCheckDt) % Cfg_Record->CheckPeriod;
if (Cfg_Record->NextCheckDt < tv.tv_sec) Cfg_Record->NextCheckDt += Cfg_Record->CheckPeriod;
switch (Cfg_Record->ModType) {
case MODULE_PING:
addICMPTask(pServer, Cfg_Record, newShift);
break;
case MODULE_TCP_PORT:
addTCPTask(pServer, Cfg_Record, newShift);
break;
case MODULE_DNS:
RecordOffset += Cfg_Record->ConfigLen;
addDNSTask(pServer, Cfg_Record, newShift, (char *) (Cfg_Record + 1));
break;
default:
RecordOffset += Cfg_Record->ConfigLen;
addLuaTask(pServer, Cfg_Record, newShift, (char *) (Cfg_Record + 1));
break;
}
}
pServer->flagRetriveConfig = 0;
if (pReq->sizes.CmprSize > 0) {
free(ptr);
}
return TRUE;
}
void onEventFromServer(Server *pServer, short action) {
struct Poll *poll = pServer->poll;
struct evbuffer *buffer = EVBUFFER_INPUT(poll->bev);
u_char *data = EVBUFFER_DATA(buffer);
u_int len = EVBUFFER_LENGTH(buffer);
if (len <= 4) {
if (data[0] == '-')
onErrorFromServer(pServer, atoi((char *) data));
} else {
switch (poll->type) {
case MODE_SERVER:
if (pServer->flagRetriveConfig) {
if (!onLoadTask(pServer)) return;
}
if (pServer->flagSendReport) SendReport(pServer);
if (pServer->flagSendReportError) SendReportError(pServer);
break;
}
}
if (len > 0) {
evbuffer_drain(buffer, EVBUFFER_LENGTH(buffer));
}
}