-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprotocol.h
376 lines (336 loc) · 10.3 KB
/
protocol.h
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
367
368
369
370
371
372
373
374
375
376
//
// Created by Francesco Laurita on 12/31/16.
//
#ifndef SPEEDTESTSERVER_PROTOCOL_H
#define SPEEDTESTSERVER_PROTOCOL_H
#include <stddef.h>
#include <arpa/inet.h>
#include <stdbool.h>
#include <errno.h>
#include <syslog.h>
#define PROTO_VER "2.4"
#define PROTO_BANNER "unofficial_open_source_server"
typedef struct _protocol_config {
size_t upload_max;
size_t download_max;
size_t idle_timeout;
int max_client_error;
char *junk_data;
size_t junk_data_len;
int backlog;
int tcp_port;
} protocol_config;
typedef struct _server_context {
size_t started_at;
size_t total_client_served;
size_t current_connected_client;
size_t byte_sent;
size_t byte_received;
const protocol_config *config;
} server_context;
typedef struct _client {
char hoststr[NI_MAXHOST];
char portstr[NI_MAXSERV];
size_t connected_at;
size_t idle;
bool quitting;
bool flush_and_quit;
int errors_no;
struct event *read_event;
struct event *write_event;
struct event *timeout_event;
char *buffer;
size_t buffer_len;
size_t buffer_size;
size_t initial_buffer_size;
size_t request_download_size;
bool download_started;
size_t request_upload_size;
size_t request_upload_size_missing;
server_context *srv_ctx;
} client;
size_t now(){
struct timeval tp;
gettimeofday(&tp, NULL);
return (size_t)tp.tv_sec * 1000 + tp.tv_usec / 1000;
}
server_context* server_context_new(const protocol_config *config){
server_context *ctx = malloc(sizeof(server_context));
if (ctx == NULL){
perror("malloc");
return NULL;
}
ctx->config = config;
ctx->byte_received = 0;
ctx->byte_sent = 0;
ctx->current_connected_client = 0;
ctx->total_client_served = 0;
ctx->started_at = now();
return ctx;
}
void server_context_free(server_context *ctx){
if (ctx != NULL)
free(ctx);
}
protocol_config* protocol_config_new(){
protocol_config *cfg = malloc(sizeof(protocol_config));
if (cfg == NULL){
perror("malloc");
return NULL;
}
const char *pseudo_rnd_data = "ABCDEFGH";
size_t pseudo_rnd_data_len = strlen(pseudo_rnd_data);
size_t rnd_data_idx = 0;
cfg->junk_data_len = 2097152;
cfg->junk_data = malloc(cfg->junk_data_len);
for (size_t i = 0; i < cfg->junk_data_len; i++){
cfg->junk_data[i] = pseudo_rnd_data[rnd_data_idx++];
if (rnd_data_idx == pseudo_rnd_data_len)
rnd_data_idx = 0;
}
return cfg;
}
void protocol_config_free(protocol_config *cfg){
free(cfg->junk_data);
free(cfg);
}
client* client_new(
struct event_base *evb,
int fd,
server_context *srv_ctx,
void *read_cb,
void *write_cb,
void *timeout_cb
){
client *c = malloc(sizeof(client));
if (c == NULL){
perror("malloc");
return NULL;
}
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
c->idle = 0;
c->quitting = false;
c->flush_and_quit = false;
c->errors_no = 0;
c->read_event = event_new(evb, fd, EV_READ|EV_PERSIST, read_cb, c);
c->write_event = event_new(evb, fd, EV_WRITE|EV_PERSIST, write_cb, c);
c->timeout_event = event_new(evb, fd, EV_TIMEOUT|EV_PERSIST, timeout_cb, c);
c->buffer = malloc(1024);
c->buffer_len = 0;
c->buffer_size = 1024;
c->initial_buffer_size = c->buffer_size;
c->request_download_size = 0;
c->download_started = false;
c->request_upload_size = 0;
c->request_upload_size_missing = 0;
c->srv_ctx = srv_ctx;
event_add(c->timeout_event, &tv);
return c;
}
void client_free(client *c){
c->srv_ctx->current_connected_client--;
event_del(c->write_event);
event_del(c->read_event);
event_del(c->timeout_event);
event_free(c->write_event);
event_free(c->read_event);
event_free(c->timeout_event);
free(c->buffer);
free(c);
}
bool client_get_state(const client *c){
if (c == NULL)
return false;
return !c->quitting && c->errors_no < c->srv_ctx->config->max_client_error;
}
char* parse_command(void *ctx){
client *c = (client *)ctx;
char *command = calloc(c->buffer_size, 1);
// A command should be found between the first c->buffer_len bytes
char *pch = (char*) memchr(c->buffer, '\n', c->buffer_len);
if (pch == NULL )
pch = (char*) memchr(c->buffer, '\0', c->buffer_len);
if (pch != NULL){
memmove(command, c->buffer, pch - c->buffer);
return command;
}
free(command);
return NULL;
}
void error_handler(void *ctx){
client *c = (client *)ctx;
c->errors_no++;
const char *err_str = "ERROR\n";
memmove(c->buffer, err_str, strlen(err_str));
c->buffer_len = strlen(err_str);
}
void ping_handler(void *ctx){
client *c = (client *)ctx;
char *response = calloc(100, 1);
snprintf(response, 100, "PONG %zu\n", now());
memmove(c->buffer, response, strlen(response));
c->buffer_len = strlen(response);
free(response);
}
void hi_handler(void *ctx){
client *c = (client *)ctx;
char *response = calloc(100, 1);
snprintf(response, 100, "HELLO %s %s\n", PROTO_VER, PROTO_BANNER);
memmove(c->buffer, response, strlen(response));
c->buffer_len = strlen(response);
free(response);
}
void getip_handler(void *ctx){
client *c = (client *)ctx;
char *response = calloc(100, 1);
snprintf(response, 100, "YOURIP %s\n", c->hoststr);
memmove(c->buffer, response, strlen(response));
c->buffer_len = strlen(response);
free(response);
}
void quit_handler(void *ctx){
client *c = (client *)ctx;
c->quitting = true;
}
void download_request_handler(const size_t download, void *ctx){
client *c = (client *)ctx;
const char *payload = "DOWNLOAD ";
if (download == 0 || download > c->srv_ctx->config->download_max || download < strlen(payload)){
error_handler(ctx);
return;
}
c->request_download_size = download;
c->download_started = false;
}
void upload_request_handler(const size_t upload, const size_t cmd_size, void *ctx){
client *c = (client *)ctx;
if (upload == 0 || upload > c->srv_ctx->config->upload_max || upload <= cmd_size || (upload - cmd_size) == 0){
error_handler(ctx);
c->request_upload_size = 0;
c->request_upload_size_missing = 0;
return;
}
c->request_upload_size = upload;
c->request_upload_size_missing = c->request_upload_size - cmd_size;
// XXX Check
c->buffer = realloc(c->buffer, 8192);
if (c->buffer != NULL){
c->buffer_size = 8192;
c->buffer_len = 0;
}
event_del(c->write_event);
}
void upload_complete_handler(void *ctx){
client *c = (client *)ctx;
// XXX Check for command termination
// if (c->buffer[c->buffer_len - 1] == '\n'){
// printf("Upload terminated OK!\n");
// } else {
// printf("Upload terminated KO!\n");
// }
if (c->buffer_size != c->initial_buffer_size){
// XXX Check
c->buffer = realloc(c->buffer, c->initial_buffer_size);
if (c->buffer != NULL){
c->buffer_size = c->initial_buffer_size;
c->buffer_len = 0;
}
}
char *response = calloc(100, 1);
snprintf(response, 100, "OK %zu %zu\n", c->request_upload_size, now());
memmove(c->buffer, response, strlen(response));
c->buffer_len = strlen(response);
free(response);
c->request_upload_size = 0;
event_add(c->write_event, NULL);
}
void download_execute_handler(void *ctx){
client *c = (client *)ctx;
const char *payload = "DOWNLOAD ";
ssize_t result;
int wfd = event_get_fd(c->write_event);
if (!c->download_started){
size_t hdr_len = strlen(payload);
result = send(wfd, payload, hdr_len, 0);
if (result != -1){
c->download_started = true;
c->request_download_size -= result;
c->srv_ctx->byte_sent += result;
return;
} else {
if (errno == EAGAIN)
return;
else {
syslog(LOG_ERR,
"%s:%s send error on download_execute_handler: %s",
c->hoststr,
c->portstr,
strerror(errno)
);
evutil_closesocket(wfd);
client_free(c);
return;
}
}
}
size_t missing = c->request_download_size - 1;
if (missing == 0){
result = send(wfd, "\n", 1, 0);
if (result == 1){
c->srv_ctx->byte_sent += result;
c->request_download_size = 0;
c->download_started = false;
event_del(c->write_event);
} else {
if (errno == EAGAIN)
return;
else {
syslog(LOG_ERR,
"%s:%s send error on download_execute_handler: %s",
c->hoststr,
c->portstr,
strerror(errno)
);
evutil_closesocket(wfd);
client_free(c);
return;
}
}
} else {
size_t to_send = (missing >= c->srv_ctx->config->junk_data_len) ? c->srv_ctx->config->junk_data_len : missing;
result = send(wfd, c->srv_ctx->config->junk_data, to_send, 0);
if (result != -1){
c->srv_ctx->byte_sent += result;
c->request_download_size -= result;
} else {
if (errno == EAGAIN)
return;
else {
syslog(LOG_ERR,
"%s:%s send error on download_execute_handler: %s",
c->hoststr,
c->portstr,
strerror(errno)
);
evutil_closesocket(wfd);
client_free(c);
return;
}
}
}
}
void policy_file_request_handler(void *ctx){
client *c = (client *)ctx;
const char *payload = "<cross-domain-policy>\n"
"<allow-access-from domain=\"*\" to-ports=\"%d\"/>\n"
"</cross-domain-policy>\n";
char *response = calloc(255, 1);
snprintf(response, 255, payload, c->srv_ctx->config->tcp_port);
memmove(c->buffer, response, strlen(response));
c->buffer_len = strlen(response);
c->flush_and_quit = true;
free(response);
}
#endif //SPEEDTESTSERVER_PROTOCOL_H