-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
270 lines (209 loc) · 7.88 KB
/
client.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
#include <event2/bufferevent.h>
#include "include.h"
#include "aes/aes.h"
static pthread_mutex_t count_lock;
aes_context ctx;
char IV[16];
struct event_base *mainBase = 0L;
static char *Buffer = NULL;
static u_int BufferLen = 0;
void OnBufferedWrite(struct bufferevent *bev, void *arg) {
if (((Server *) arg)->poll->status != STATE_CONNECTED) {
openSession((Server *) arg, EV_WRITE);
}
}
void OnBufferedError(struct bufferevent *bev, short what, void *arg) {
debug("%s:%d -> %02x len in buf %d", ((Server *) arg)->host, ((Server *) arg)->port, what, EVBUFFER_LENGTH(bufferevent_get_input(bev)));
if (what & BEV_EVENT_CONNECTED) {
OnBufferedWrite(bev, arg);
return;
}
closeConnection((Server *) arg, FALSE);
}
void OnBufferedRead(struct bufferevent *bev, void *arg) {
struct evbuffer *buffer = EVBUFFER_INPUT(bev);
// u_char *data = EVBUFFER_DATA(buffer);
u_int len = EVBUFFER_LENGTH(buffer);
debug("%d %d",evbuffer_get_contiguous_space(buffer),len);
if (((Server *) arg)->poll->status == STATE_CONNECTED) {
onEventFromServer((Server *) arg, EV_READ);
} else {
openSession((Server *) arg, EV_READ);
}
}
void openSession(Server *pServer, short action) {
struct Poll *poll = pServer->poll;
struct evbuffer *In = bufferevent_get_input(poll->bev);
struct evbuffer *Out = bufferevent_get_output(poll->bev);
u_char *data = EVBUFFER_DATA(In);
u_int len = evbuffer_get_length(In);
debug("%s:%d -> %s %s", pServer->host, pServer->port, getActionText(action), getStatusText(poll->status));
/*
if (poll->status == STATE_QUIT) {
closeConnection(pServer, FALSE);
return;
}
*/
if (action == EV_READ) {
/*
if (len) {
printf("Client\n---------------------------\n");
hexPrint(data, len);
printf("--------------------------------\n\n\n");
}
*/
genSharedKey(pServer, (u_char *) data);
poll->status = STATE_SESSION;
/*
len = 32;
*/
bufferevent_enable(pServer->poll->bev, EV_WRITE);
} else if (action == EV_WRITE) {
switch (poll->status) {
case STATE_CONNECTING:
evbuffer_add(Out, genPublicKey(pServer), 32);
bufferevent_enable(pServer->poll->bev, EV_READ);
bufferevent_disable(pServer->poll->bev, EV_WRITE);
break;
case STATE_SESSION:
aes_set_key(&ctx, pServer->key.shared, 128);
/*
printf("Client PublicShared\n---------------------------\n");
hexPrint(pServer->key.public, 16);
hexPrint(pServer->key.shared, 16);
printf("--------------------------------\n\n\n");
*/
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);
poll->status = STATE_CONNECTED;
bufferevent_enable(pServer->poll->bev, EV_READ | EV_PERSIST);
if (pServer->flagRetriveConfig) loadTask(pServer);
if (pServer->flagSendReportError) SendReportError(pServer);
if (pServer->flagSendReport) SendReport(pServer);
break;
}
}
if (len > 0) {
evbuffer_drain(In, len);
}
}
void newConnectionTask(Server *pServer) {
struct timeval tv;
struct sockaddr_in sa;
u32 ip;
inet_aton(pServer->host,(struct in_addr *) &ip);
sa.sin_addr = *((struct in_addr *) & ip);
sa.sin_family = AF_INET;
sa.sin_port = htons(pServer->port);
bzero(&sa.sin_zero, 8);
debug("%s:%d", pServer->host, pServer->port);
pServer->poll->status = STATE_CONNECTING;
pServer->poll->type = MODE_SERVER;
pServer->poll->bev = bufferevent_socket_new(mainBase, -1, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(pServer->poll->bev, OnBufferedRead, OnBufferedWrite, OnBufferedError, pServer);
bufferevent_enable(pServer->poll->bev, EV_WRITE);
bufferevent_socket_connect(pServer->poll->bev, (struct sockaddr *) & sa, sizeof (sa));
if (pServer->timeout) {
timerclear(&tv);
tv.tv_sec = pServer->timeout;
bufferevent_enable(pServer->poll->bev, EV_TIMEOUT);
bufferevent_set_timeouts(pServer->poll->bev, &tv, &tv);
}
}
void timerRetrieveTask(int fd, short action, void *arg) {
Server *pServer = (Server *) arg;
static int countRetrieve = 0;
struct timeval tv;
timerclear(&tv);
tv.tv_sec = pServer->periodRetrieve;
evtimer_add(&pServer->evConfig, &tv);
debug("%s:%d,%d -> %s %s", pServer->host, pServer->port, (int) pServer->periodRetrieve, getActionText(action), getStatusText(pServer->poll->status));
pServer->flagRetriveConfig = 1;
if (pServer->poll->status == STATE_DISCONNECTED) {
newConnectionTask(pServer);
} else if (pServer->poll->status == STATE_CONNECTED) {
loadTask(pServer);
}
countRetrieve++;
if (countRetrieve > 100) {
countRetrieve = 0;
restart_handler(0);
}
}
void timerSendReportError(int fd, short action, void *arg) {
Server *pServer = (Server *) arg;
struct timeval tv;
timerclear(&tv);
tv.tv_sec = pServer->periodReportError;
evtimer_add(&pServer->evReportError, &tv);
if (countReportError(pServer) > 0) {
debug("%s:%d -> Errors %d, %s %s [period - %d]", pServer->host, pServer->port,
countReportError(pServer),
getActionText(action), getStatusText(pServer->poll->status),
(int) pServer->periodReportError);
pServer->flagSendReportError = 1;
if (pServer->poll->status == STATE_DISCONNECTED) {
newConnectionTask(pServer);
} else if (pServer->poll->status == STATE_CONNECTED) {
SendReportError(pServer);
}
}
}
void timerSendReport(int fd, short action, void *arg) {
Server *pServer = (Server *) arg;
struct timeval tv;
timerclear(&tv);
tv.tv_sec = pServer->periodReport;
evtimer_add(&pServer->evReport, &tv);
debug("%s:%d -> Report %d, %s %s [period - %d]", pServer->host, pServer->port,
countReport(pServer),
getActionText(action), getStatusText(pServer->poll->status),
(int) pServer->periodReport);
if (countReport(pServer) > 0) {
pServer->flagSendReport = 1;
if (pServer->poll->status == STATE_DISCONNECTED) {
newConnectionTask(pServer);
} else if (pServer->poll->status == STATE_CONNECTED) {
SendReport(pServer);
}
}
}
void initPtr() {
Buffer = getNulledMemory(BUFLEN);
initTester();
initProcess();
initResolv();
}
void freePtr() {
free(Buffer);
freeProcess();
}
#ifndef TESTER
int main(int argc, char **argv) {
initMainVars();
// возможно лучше проверять не сдох ли fd
struct sigaction IgnoreYourSignal;
sigemptyset(&IgnoreYourSignal.sa_mask);
IgnoreYourSignal.sa_handler = SIG_IGN;
IgnoreYourSignal.sa_flags = 0;
sigaction(SIGPIPE, &IgnoreYourSignal, NULL);
if (argc < 2) {
printf(cRED"ERROR: need config name\n"cEND);
printf(cGREEN"\tSample:"cEND""cBLUE" %s /path/to/config.xml\n\n"cEND, argv[0]);
exit(FALSE);
}
if (!openConfiguration(argv[1])) {
printf(cRED"ERROR: incorect config file\n"cEND);
printf(cGREEN"\tDescription:"cEND""cBLUE" nil server loaded\n\n"cEND);
exit(FALSE);
}
pthread_mutex_init(&count_lock, NULL);
evthread_use_pthreads();
evthread_make_base_notifiable(mainBase);
initPtr();
event_dispatch();
event_base_free(mainBase);
freePtr();
return 0;
}
#endif