-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverifer.c
286 lines (231 loc) · 9.73 KB
/
verifer.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
#include "include.h"
#include "aes/aes.h"
#include <zlib.h>
struct timeval tv;
aes_context ctx;
static struct event_base *base = 0L;
struct evbuffer *In, *Out;
static void eventcb(struct bufferevent *bev, short what, void *ptr) {
if (what & (BEV_EVENT_TIMEOUT | BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
closeConnection((Server *) ptr, TRUE);
free(ptr);
}
}
static void onLoadTask(Server *pServer) {
struct _Verifer_Cfg_Record *Cfg_Record;
struct _Tester_Cfg_Record FakeCfg_Record = {0};
struct Request *pReq, *pReqCmpr;
u_char *data = EVBUFFER_DATA(In);
u_int len = EVBUFFER_LENGTH(In);
char *ptr = 0;
u32 crc = 0;
int Readed = 0;
pReq = (struct Request *) data;
/*
printf("2 EVBUFFER_LENGTH(In) = %d\n", EVBUFFER_LENGTH(In));
printf("0 %08x. %d vs %d\n", pReq, len, Size_Request);
*/
if (len < Size_Request) {
return;
}
/*
printf("1 %08x. %d vs %d\n", pReq, len, Size_Request);
*/
if (pReq->sizes.UncmprSize < Size_Request_hdr or pReq->sizes.UncmprSize > MAX_UNCMPR or(pReq->sizes.CmprSize > 0 and pReq->sizes.CmprSize > MAX_CMPR)) {
return;
}
/*
printf("2 %08x. %d vs %d\n", pReq, len, Size_Request);
*/
crc = pReq->sizes.crc;
/*
printf("3 %08x\n", crc);
*/
pReq->sizes.crc = 0;
/*
printf("Compress %d, Un %d\n", pReq->sizes.CmprSize, pReq->sizes.UncmprSize);
*/
if (crc32(0xffffffff, (const Bytef *) pReq, (pReq->sizes.CmprSize > 0 ? pReq->sizes.CmprSize : pReq->sizes.UncmprSize) + Size_Request_sizes) != crc) {
return;
}
/*
printf("CRC OK\n");
*/
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;
}
pReq = pReqCmpr;
}
gettimeofday(&tv, NULL);
int RecordsLen = pReq->sizes.UncmprSize - Size_Request;
int RecordOffset = 0;
for (
;
RecordOffset < RecordsLen;
RecordOffset += sizeof (* Cfg_Record), Readed += 1
) {
Cfg_Record = (void *) & pReq->Data + RecordOffset;
if (Cfg_Record->TimeOut == 0 or Cfg_Record->TimeOut >= config.timeout * 950)
Cfg_Record->TimeOut = config.timeout * 950;
FakeCfg_Record.LObjId = Cfg_Record->LObjId;
FakeCfg_Record.ModType = Cfg_Record->ModType;
FakeCfg_Record.Port = Cfg_Record->Port;
FakeCfg_Record.IP = Cfg_Record->IP;
FakeCfg_Record.ConfigLen = Cfg_Record->ConfigLen;
snprintf(FakeCfg_Record.HostName, sizeof (FakeCfg_Record.HostName), "%s", Cfg_Record->HostName);
FakeCfg_Record.TimeOut = Cfg_Record->TimeOut;
FakeCfg_Record.NextCheckDt = RemoteToLocalTime(Cfg_Record->NextCheckDt);
if (FakeCfg_Record.NextCheckDt < tv.tv_sec) FakeCfg_Record.NextCheckDt = tv.tv_sec + 1;
#ifdef DEBUG
debug("\tid объекта тестирования = %d", Cfg_Record->LObjId);
debug("\tтип модуля тестирования (пинг-порт-хттп-...) = %s", getModuleText(Cfg_Record->ModType));
debug("\tпорт првоерки = %hd", (short) Cfg_Record->Port);
debug("\tип объекта тестирования = %s", ipString(Cfg_Record->IP));
debug("\tдата ближайшей проверки. unixtime utc = %d", FakeCfg_Record.NextCheckDt);
debug("\tимя хоста = %s", Cfg_Record->HostName);
debug("\tТаймаут для проверки = %d", Cfg_Record->TimeOut);
debug("\tРазмер дополнительных данных = %d", Cfg_Record->ConfigLen);
#endif
switch (Cfg_Record->ModType) {
case MODULE_PING:
addICMPVerifyTask(config.pVeriferDC, &FakeCfg_Record);
break;
case MODULE_TCP_PORT:
addTCPVerifyTask(config.pVeriferDC, &FakeCfg_Record);
break;
case MODULE_DNS:
addDNSVerifyTask(config.pVeriferDC, &FakeCfg_Record, (char *) (Cfg_Record + 1));
RecordOffset += Cfg_Record->ConfigLen;
break;
default:
addLuaVerifyTask(config.pVeriferDC, &FakeCfg_Record, (char *) (Cfg_Record + 1));
RecordOffset += Cfg_Record->ConfigLen;
break;
}
}
if (pReq->sizes.CmprSize > 0) {
free(ptr);
}
evbuffer_add(Out, &Readed, sizeof (int));
/*
hexPrint(&Readed, sizeof (int));
*/
}
static void callbackRead(struct bufferevent *bev, void *ptr) {
Server *pServer = (Server *) ptr;
struct Poll *poll = pServer->poll;
struct st_session session;
size_t len;
char Buffer[BUFLEN] = {0};
u_int BufferLen = 0;
debug("%s -> %s", ConnectedIpString(pServer), getStatusText(poll->status));
In = bufferevent_get_input(bev);
Out = bufferevent_get_output(bev);
len = evbuffer_get_length(In);
/*
hexPrint((u_char *) EVBUFFER_DATA(In), EVBUFFER_LENGTH(In));
printf("EVBUFFER_LENGTH(In) = %d\n", EVBUFFER_LENGTH(In));
*/
switch (poll->status) {
case STATE_CONNECTED:
genSharedKey(pServer, (u_char *) EVBUFFER_DATA(In));
len = 32;
poll->status = STATE_SESSION;
aes_set_key(&ctx, pServer->key.shared, 128);
BufferLen = aes_cbc_encrypt(&ctx, pServer->key.shared + 16, (u_char*) & pServer->session, (u_char *) Buffer, sizeof (struct st_session));
evbuffer_add(Out, Buffer, BufferLen);
break;
case STATE_SESSION:
aes_set_key(&ctx, pServer->key.shared, 128);
bzero(&session, sizeof (struct st_session));
BufferLen = aes_cbc_decrypt(&ctx, pServer->key.shared + 16, EVBUFFER_DATA(In), (unsigned char*) & session, sizeof (struct st_session));
if (memcmp(session.password, pServer->passwordRecv, sizeof (pServer->passwordRecv))) {
/*
debug("session.password: %s -> %s", bin2hex(session.password, sizeof (session.password)), session.password);
debug(" local.password: %s -> %s", bin2hex(pServer->passwordRecv, sizeof (pServer->passwordRecv)), pServer->passwordRecv);
*/
BufferLen = snprintf(Buffer, BUFLEN, "%d", SOCK_RC_AUTH_FAILURE);
write(bufferevent_getfd(bev), Buffer, BufferLen);
closeConnection(pServer, TRUE);
return;
}
poll->status = STATE_DONE;
break;
default:
/*
printf("1 EVBUFFER_LENGTH(In) = %d\n", EVBUFFER_LENGTH(In));
*/
onLoadTask(pServer);
break;
}
evbuffer_drain(In, len);
if (evbuffer_get_length(In) > 0) {
callbackRead(bev, ptr);
}
}
static void callbackWrite(struct bufferevent *bev, void *ptr) {
Server *pServer = (Server *) ptr;
debug("%s", ConnectedIpString(pServer));
struct Poll *poll = pServer->poll;
struct evbuffer *Out = bufferevent_get_output(bev);
evbuffer_add(Out, genPublicKey(pServer), 32);
poll->status = STATE_CONNECTED;
bufferevent_setcb(pServer->poll->bev, callbackRead, NULL, eventcb, (void *) pServer);
}
void OnAcceptVeriferTask(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *a, int slen, void *p) {
struct sockaddr_in *sa;
sa = (struct sockaddr_in *) a;
Server *pServer = getNulledMemory(sizeof (*pServer));
pServer->isVerifer = 1;
pServer->poll = getNulledMemory(sizeof (struct Poll));
pServer->poll->client.sin_addr = ((struct sockaddr_in *) a)->sin_addr;
pServer->poll->client.sin_port = ((struct sockaddr_in *) a)->sin_port;
debug("%s", ConnectedIpString(pServer));
memcpy(pServer->session.password, config.pVerifer->session.password, sizeof (pServer->session.password));
memcpy(pServer->passwordRecv, config.pVerifer->passwordRecv, sizeof (config.pVerifer->passwordRecv));
pServer->poll->bev = bufferevent_socket_new(evconnlistener_get_base(listener), fd, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_CLOSE_ON_EXEC | BEV_OPT_DEFER_CALLBACKS);
pServer->poll->fd = bufferevent_getfd(pServer->poll->bev);
bufferevent_setcb(pServer->poll->bev, NULL, callbackWrite, eventcb, (void *) pServer);
bufferevent_enable(pServer->poll->bev, EV_READ | EV_WRITE | EV_TIMEOUT);
timerclear(&tv);
if (pServer->timeout) {
tv.tv_sec = pServer->timeout;
} else {
tv.tv_sec = 10;
}
bufferevent_set_timeouts(pServer->poll->bev, &tv, &tv);
}
void runVeriferTask() {
struct sockaddr_in sa;
u32 ip;
if (config.pVerifer->host) {
inet_aton(config.pVerifer->host, (struct in_addr *) &ip);
sa.sin_addr = *((struct in_addr *) & ip);
} else {
sa.sin_addr.s_addr = 0;
}
sa.sin_family = AF_INET;
sa.sin_port = htons(config.pVerifer->port);
bzero(&sa.sin_zero, 8);
config.pVerifer->poll->type = MODE_VERIFER;
evconnlistener_new_bind(base, OnAcceptVeriferTask, NULL, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_CLOSE_ON_EXEC | LEV_OPT_REUSEABLE, -1, (struct sockaddr*) & sa, sizeof (sa));
}
static void *initThread(void *in) {
base = event_base_new();
runVeriferTask();
event_base_dispatch(base);
return NULL;
}
void initVerifer() {
pthread_t threads;
pthread_create(&threads, NULL, initThread, NULL);
}
struct event_base *getVeriferBase() {
return base;
}