blob: cbea24da3af392175d6c7118784bdc0bf29042a1 [file] [log] [blame]
Christopher Faulet010fded2016-11-03 22:49:37 +01001/*
2 * A Random IP reputation service acting as a Stream Processing Offload Agent
3 *
4 * This is a very simple service that implement a "random" ip reputation
5 * service. It will return random scores for all checked IP addresses. It only
6 * shows you how to implement a ip reputation service or such kind of services
7 * using the SPOE.
8 *
9 * Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 */
Christopher Fauletf95b1112016-12-21 08:58:16 +010017#include <unistd.h>
Christopher Faulet010fded2016-11-03 22:49:37 +010018#include <stdlib.h>
19#include <string.h>
20#include <stdbool.h>
Christopher Fauletf95b1112016-12-21 08:58:16 +010021#include <errno.h>
22#include <stdio.h>
Christopher Faulet010fded2016-11-03 22:49:37 +010023#include <signal.h>
Christopher Faulet010fded2016-11-03 22:49:37 +010024#include <netinet/in.h>
Christopher Fauletf95b1112016-12-21 08:58:16 +010025#include <sys/socket.h>
26#include <err.h>
27#include <ctype.h>
28
29#include <pthread.h>
30
31#include <event2/util.h>
32#include <event2/event.h>
33#include <event2/event_struct.h>
34#include <event2/thread.h>
Christopher Faulet010fded2016-11-03 22:49:37 +010035
Christopher Fauletf95b1112016-12-21 08:58:16 +010036#include <common/mini-clist.h>
37
38#define DEFAULT_PORT 12345
39#define CONNECTION_BACKLOG 10
40#define NUM_WORKERS 10
41#define MAX_FRAME_SIZE 16384
42#define SPOP_VERSION "1.0"
Christopher Faulet010fded2016-11-03 22:49:37 +010043
44#define SLEN(str) (sizeof(str)-1)
45
Christopher Fauletf95b1112016-12-21 08:58:16 +010046#define LOG(worker, fmt, args...) \
47 do { \
48 struct timeval now; \
49 \
50 gettimeofday(&now, NULL); \
51 fprintf(stderr, "%ld.%06ld [%02d] " fmt "\n", \
52 now.tv_sec, now.tv_usec, (worker)->id, ##args); \
Christopher Faulet010fded2016-11-03 22:49:37 +010053 } while (0)
54
Christopher Fauletf95b1112016-12-21 08:58:16 +010055#define DEBUG(x...) \
Christopher Faulet010fded2016-11-03 22:49:37 +010056 do { \
57 if (debug) \
58 LOG(x); \
59 } while (0)
60
61/* Frame Types sent by HAProxy and by agents */
62enum spoe_frame_type {
63 /* Frames sent by HAProxy */
64 SPOE_FRM_T_HAPROXY_HELLO = 1,
65 SPOE_FRM_T_HAPROXY_DISCON,
66 SPOE_FRM_T_HAPROXY_NOTIFY,
67
68 /* Frames sent by the agents */
69 SPOE_FRM_T_AGENT_HELLO = 101,
70 SPOE_FRM_T_AGENT_DISCON,
71 SPOE_FRM_T_AGENT_ACK
72};
73
74/* All supported data types */
75enum spoe_data_type {
76 SPOE_DATA_T_NULL = 0,
77 SPOE_DATA_T_BOOL,
78 SPOE_DATA_T_INT32,
79 SPOE_DATA_T_UINT32,
80 SPOE_DATA_T_INT64,
81 SPOE_DATA_T_UINT64,
82 SPOE_DATA_T_IPV4,
83 SPOE_DATA_T_IPV6,
84 SPOE_DATA_T_STR,
85 SPOE_DATA_T_BIN,
86 SPOE_DATA_TYPES
87};
88
89/* Errors triggerd by SPOE applet */
90enum spoe_frame_error {
91 SPOE_FRM_ERR_NONE = 0,
92 SPOE_FRM_ERR_IO,
93 SPOE_FRM_ERR_TOUT,
94 SPOE_FRM_ERR_TOO_BIG,
95 SPOE_FRM_ERR_INVALID,
96 SPOE_FRM_ERR_NO_VSN,
97 SPOE_FRM_ERR_NO_FRAME_SIZE,
98 SPOE_FRM_ERR_NO_CAP,
99 SPOE_FRM_ERR_BAD_VSN,
100 SPOE_FRM_ERR_BAD_FRAME_SIZE,
Christopher Faulet85010352017-02-02 10:14:36 +0100101 SPOE_FRM_ERR_FRAG_NOT_SUPPORTED,
102 SPOE_FRM_ERR_INTERLACED_FRAMES,
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100103 SPOE_FRM_ERR_FRAMEID_NOTFOUND,
Christopher Faulet85010352017-02-02 10:14:36 +0100104 SPOE_FRM_ERR_RES,
Christopher Faulet010fded2016-11-03 22:49:37 +0100105 SPOE_FRM_ERR_UNKNOWN = 99,
106 SPOE_FRM_ERRS,
107};
108
109/* All supported SPOE actions */
110enum spoe_action_type {
111 SPOE_ACT_T_SET_VAR = 1,
112 SPOE_ACT_T_UNSET_VAR,
113 SPOE_ACT_TYPES,
114};
115
116/* Scopes used for variables set by agents. It is a way to be agnotic to vars
117 * scope. */
118enum spoe_vars_scope {
119 SPOE_SCOPE_PROC = 0, /* <=> SCOPE_PROC */
120 SPOE_SCOPE_SESS, /* <=> SCOPE_SESS */
121 SPOE_SCOPE_TXN, /* <=> SCOPE_TXN */
122 SPOE_SCOPE_REQ, /* <=> SCOPE_REQ */
123 SPOE_SCOPE_RES, /* <=> SCOPE_RES */
124};
125
Christopher Fauletf95b1112016-12-21 08:58:16 +0100126enum spoa_state {
127 SPOA_ST_CONNECTING = 0,
128 SPOA_ST_PROCESSING,
129 SPOA_ST_DISCONNECTING,
130};
131
132enum spoa_frame_type {
133 SPOA_FRM_T_UNKNOWN = 0,
134 SPOA_FRM_T_HAPROXY,
135 SPOA_FRM_T_AGENT,
136};
Christopher Faulet010fded2016-11-03 22:49:37 +0100137
Christopher Faulet85010352017-02-02 10:14:36 +0100138/* Flags set on the SPOE frame */
139#define SPOE_FRM_FL_FIN 0x00000001
140#define SPOE_FRM_FL_ABRT 0x00000002
141
Christopher Faulet010fded2016-11-03 22:49:37 +0100142/* Masks to get data type or flags value */
143#define SPOE_DATA_T_MASK 0x0F
144#define SPOE_DATA_FL_MASK 0xF0
145
146/* Flags to set Boolean values */
147#define SPOE_DATA_FL_FALSE 0x00
148#define SPOE_DATA_FL_TRUE 0x10
Christopher Fauletf95b1112016-12-21 08:58:16 +0100149
150struct spoe_engine {
151 char *id;
152
153 struct list processing_frames;
154 struct list outgoing_frames;
155
156 struct list clients;
157 struct list list;
158};
159
160struct spoe_frame {
161 enum spoa_frame_type type;
162 char *buf;
163 unsigned int offset;
164 unsigned int len;
165
166 unsigned int stream_id;
167 unsigned int frame_id;
Christopher Faulet85010352017-02-02 10:14:36 +0100168 unsigned int flags;
169 bool hcheck; /* true is the CONNECT frame is a healthcheck */
170 bool fragmented; /* true if the frame is fragmented */
171 int ip_score; /* -1 if unset, else between 0 and 100 */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100172
173 struct event process_frame_event;
174 struct worker *worker;
175 struct spoe_engine *engine;
176 struct client *client;
177 struct list list;
178
Christopher Faulet85010352017-02-02 10:14:36 +0100179 char *frag_buf; /* used to accumulate payload of a fragmented frame */
180 unsigned int frag_len;
181
Christopher Fauletf95b1112016-12-21 08:58:16 +0100182 char data[0];
183};
184
185struct client {
186 int fd;
187 unsigned long id;
188 enum spoa_state state;
189
190 struct event read_frame_event;
191 struct event write_frame_event;
192
193 struct spoe_frame *incoming_frame;
194 struct spoe_frame *outgoing_frame;
195
196 struct list processing_frames;
197 struct list outgoing_frames;
198
199 unsigned int max_frame_size;
200 int status_code;
201
202 char *engine_id;
203 struct spoe_engine *engine;
204 bool pipelining;
205 bool async;
Christopher Faulet85010352017-02-02 10:14:36 +0100206 bool fragmentation;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100207
208 struct worker *worker;
209 struct list by_worker;
210 struct list by_engine;
Christopher Faulet010fded2016-11-03 22:49:37 +0100211};
212
213struct worker {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100214 pthread_t thread;
215 int id;
216 struct event_base *base;
217 struct event *monitor_event;
218
219 struct list engines;
220
221 unsigned int nbclients;
222 struct list clients;
223
224 struct list frames;
Christopher Faulet010fded2016-11-03 22:49:37 +0100225};
226
Christopher Fauletf95b1112016-12-21 08:58:16 +0100227
Christopher Faulet010fded2016-11-03 22:49:37 +0100228struct chunk {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100229 char *str; /* beginning of the string itself. Might not be 0-terminated */
230 int len; /* current size of the string from first to last char */
Christopher Faulet010fded2016-11-03 22:49:37 +0100231};
232
233union spoe_value {
234 bool boolean; /* use for boolean */
235 int32_t sint32; /* used for signed 32bits integers */
236 uint32_t uint32; /* used for signed 32bits integers */
237 int32_t sint64; /* used for signed 64bits integers */
238 uint32_t uint64; /* used for signed 64bits integers */
239 struct in_addr ipv4; /* used for ipv4 addresses */
240 struct in6_addr ipv6; /* used for ipv6 addresses */
241 struct chunk buffer; /* used for char strings or buffers */
242};
243
244/* Used to store sample constant */
245struct spoe_data {
246 enum spoe_data_type type; /* SPOE_DATA_T_* */
247 union spoe_value u; /* spoe data value */
248};
249
Christopher Fauletf95b1112016-12-21 08:58:16 +0100250/* Globals */
251static struct worker *workers = NULL;
252static struct worker null_worker = { .id = 0 };
253static unsigned long clicount = 0;
254static int server_port = DEFAULT_PORT;
255static int num_workers = NUM_WORKERS;
256static unsigned int max_frame_size = MAX_FRAME_SIZE;
257struct timeval processing_delay = {0, 0};
258static bool debug = false;
259static bool pipelining = false;
260static bool async = false;
Christopher Faulet85010352017-02-02 10:14:36 +0100261static bool fragmentation = false;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100262
263
264static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Faulet85010352017-02-02 10:14:36 +0100265 [SPOE_FRM_ERR_NONE] = "normal",
266 [SPOE_FRM_ERR_IO] = "I/O error",
267 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
268 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
269 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
270 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
271 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
272 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
273 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
274 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
275 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
276 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100277 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Faulet85010352017-02-02 10:14:36 +0100278 [SPOE_FRM_ERR_RES] = "resource allocation error",
279 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf95b1112016-12-21 08:58:16 +0100280};
281
282static void signal_cb(evutil_socket_t, short, void *);
283static void accept_cb(evutil_socket_t, short, void *);
284static void worker_monitor_cb(evutil_socket_t, short, void *);
285static void process_frame_cb(evutil_socket_t, short, void *);
286static void read_frame_cb(evutil_socket_t, short, void *);
287static void write_frame_cb(evutil_socket_t, short, void *);
288
289static void use_spoe_engine(struct client *);
290static void unuse_spoe_engine(struct client *);
291static void release_frame(struct spoe_frame *);
292static void release_client(struct client *);
Christopher Faulet010fded2016-11-03 22:49:37 +0100293
294static void
Christopher Fauletf95b1112016-12-21 08:58:16 +0100295check_ipv4_reputation(struct spoe_frame *frame, struct in_addr *ipv4)
Christopher Faulet010fded2016-11-03 22:49:37 +0100296{
297 char str[INET_ADDRSTRLEN];
298
299 if (inet_ntop(AF_INET, ipv4, str, INET_ADDRSTRLEN) == NULL)
300 return;
301
Christopher Fauletf95b1112016-12-21 08:58:16 +0100302 frame->ip_score = random() % 100;
Christopher Faulet010fded2016-11-03 22:49:37 +0100303
Christopher Fauletf95b1112016-12-21 08:58:16 +0100304 DEBUG(frame->worker, "IP score for %.*s is %d",
305 INET_ADDRSTRLEN, str, frame->ip_score);
Christopher Faulet010fded2016-11-03 22:49:37 +0100306}
307
308static void
Christopher Fauletf95b1112016-12-21 08:58:16 +0100309check_ipv6_reputation(struct spoe_frame *frame, struct in6_addr *ipv6)
Christopher Faulet010fded2016-11-03 22:49:37 +0100310{
311 char str[INET6_ADDRSTRLEN];
312
313 if (inet_ntop(AF_INET6, ipv6, str, INET6_ADDRSTRLEN) == NULL)
314 return;
315
Christopher Fauletf95b1112016-12-21 08:58:16 +0100316 frame->ip_score = random() % 100;
Christopher Faulet010fded2016-11-03 22:49:37 +0100317
Christopher Fauletf95b1112016-12-21 08:58:16 +0100318 DEBUG(frame->worker, "IP score for %.*s is %d",
319 INET6_ADDRSTRLEN, str, frame->ip_score);
Christopher Faulet010fded2016-11-03 22:49:37 +0100320}
321
Christopher Faulet010fded2016-11-03 22:49:37 +0100322
323/* Encode a variable-length integer. This function never fails and returns the
324 * number of written bytes. */
325static int
326encode_spoe_varint(uint64_t i, char *buf)
327{
328 int idx;
329
330 if (i < 240) {
331 buf[0] = (unsigned char)i;
332 return 1;
333 }
334
335 buf[0] = (unsigned char)i | 240;
336 i = (i - 240) >> 4;
337 for (idx = 1; i >= 128; ++idx) {
338 buf[idx] = (unsigned char)i | 128;
339 i = (i - 128) >> 7;
340 }
341 buf[idx++] = (unsigned char)i;
342 return idx;
343}
344
345/* Decode a varable-length integer. If the decoding fails, -1 is returned. This
346 * happens when the buffer's end in reached. On success, the number of read
347 * bytes is returned. */
348static int
349decode_spoe_varint(char *buf, char *end, uint64_t *i)
350{
351 unsigned char *msg = (unsigned char *)buf;
352 int idx = 0;
353
354 if (msg > (unsigned char *)end)
355 return -1;
356
357 if (msg[0] < 240) {
358 *i = msg[0];
359 return 1;
360 }
361 *i = msg[0];
362 do {
363 ++idx;
364 if (msg+idx > (unsigned char *)end)
365 return -1;
366 *i += (uint64_t)msg[idx] << (4 + 7 * (idx-1));
367 } while (msg[idx] >= 128);
368 return (idx + 1);
369}
370
371/* Encode a string. The string will be prefix by its length, encoded as a
372 * variable-length integer. This function never fails and returns the number of
373 * written bytes. */
374static int
375encode_spoe_string(const char *str, size_t len, char *dst)
376{
377 int idx = 0;
378
379 if (!len) {
380 dst[0] = 0;
381 return 1;
382 }
383
384 idx += encode_spoe_varint(len, dst);
385 memcpy(dst+idx, str, len);
386 return (idx + len);
387}
388
389/* Decode a string. Its length is decoded first as a variable-length integer. If
390 * it succeeds, and if the string length is valid, the begin of the string is
391 * saved in <*str>, its length is saved in <*len> and the total numbre of bytes
392 * read is returned. If an error occurred, -1 is returned and <*str> remains
393 * NULL. */
394static int
395decode_spoe_string(char *buf, char *end, char **str, uint64_t *len)
396{
397 int r, idx = 0;
398
399 *str = NULL;
400 *len = 0;
401
402 if ((r = decode_spoe_varint(buf, end, len)) == -1)
403 goto error;
404 idx += r;
405 if (buf + idx + *len > end)
406 goto error;
407
408 *str = buf+idx;
409 return (idx + *len);
410
Christopher Fauletf95b1112016-12-21 08:58:16 +0100411 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100412 return -1;
413}
414
415/* Skip a typed data. If an error occurred, -1 is returned, otherwise the number
416 * of bytes read is returned. A types data is composed of a type (1 byte) and
417 * corresponding data:
418 * - boolean: non additional data (0 bytes)
419 * - integers: a variable-length integer (see decode_spoe_varint)
420 * - ipv4: 4 bytes
421 * - ipv6: 16 bytes
422 * - binary and string: a buffer prefixed by its size, a variable-length
423 * integer (see decode_spoe_string) */
424static int
425skip_spoe_data(char *frame, char *end)
426{
427 uint64_t sz = 0;
428 int r, idx = 0;
429
430 if (frame > end)
431 return -1;
432
433 switch (frame[idx++] & SPOE_DATA_T_MASK) {
434 case SPOE_DATA_T_BOOL:
435 idx++;
436 break;
437 case SPOE_DATA_T_INT32:
438 case SPOE_DATA_T_INT64:
439 case SPOE_DATA_T_UINT32:
440 case SPOE_DATA_T_UINT64:
441 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
442 return -1;
443 idx += r;
444 break;
445 case SPOE_DATA_T_IPV4:
446 idx += 4;
447 break;
448 case SPOE_DATA_T_IPV6:
449 idx += 16;
450 break;
451 case SPOE_DATA_T_STR:
452 case SPOE_DATA_T_BIN:
453 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
454 return -1;
455 idx += r + sz;
456 break;
457 }
458
459 if (frame+idx > end)
460 return -1;
461 return idx;
462}
463
464/* Decode a typed data. If an error occurred, -1 is returned, otherwise the
465 * number of read bytes is returned. See skip_spoe_data for details. */
466static int
467decode_spoe_data(char *frame, char *end, struct spoe_data *data)
468{
469 uint64_t sz = 0;
470 int type, r, idx = 0;
471
472 if (frame > end)
473 return -1;
474
475 type = frame[idx++];
476 data->type = (type & SPOE_DATA_T_MASK);
477 switch (data->type) {
478 case SPOE_DATA_T_BOOL:
479 data->u.boolean = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE);
480 break;
481 case SPOE_DATA_T_INT32:
482 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
483 return -1;
484 data->u.sint32 = sz;
485 idx += r;
486 break;
487 case SPOE_DATA_T_INT64:
488 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
489 return -1;
490 data->u.uint32 = sz;
491 idx += r;
492 break;
493 case SPOE_DATA_T_UINT32:
494 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
495 return -1;
496 data->u.sint64 = sz;
497 idx += r;
498 break;
499 case SPOE_DATA_T_UINT64:
500 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
501 return -1;
502 data->u.uint64 = sz;
503 idx += r;
504 break;
505 case SPOE_DATA_T_IPV4:
506 if (frame+idx+4 > end)
507 return -1;
508 memcpy(&data->u.ipv4, frame+idx, 4);
509 idx += 4;
510 break;
511 case SPOE_DATA_T_IPV6:
512 if (frame+idx+16 > end)
513 return -1;
514 memcpy(&data->u.ipv6, frame+idx, 16);
515 idx += 16;
516 break;
517 case SPOE_DATA_T_STR:
518 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
519 return -1;
520 idx += r;
521 if (frame+idx+sz > end)
522 return -1;
523 data->u.buffer.str = frame+idx;
524 data->u.buffer.len = sz;
525 idx += sz;
526 break;
527 case SPOE_DATA_T_BIN:
528 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
529 return -1;
530 idx += r;
531 if (frame+idx+sz > end)
532 return -1;
533 data->u.buffer.str = frame+idx;
534 data->u.buffer.len = sz;
535 idx += sz;
536 break;
537 default:
538 break;
539 }
540
541 if (frame+idx > end)
542 return -1;
543 return idx;
544}
545
546
547/* Check the protocol version. It returns -1 if an error occurred, the number of
548 * read bytes otherwise. */
549static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100550check_proto_version(struct spoe_frame *frame, int idx)
Christopher Faulet010fded2016-11-03 22:49:37 +0100551{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100552 char *str;
553 uint64_t sz;
Christopher Faulet010fded2016-11-03 22:49:37 +0100554
555 /* Get the list of all supported versions by HAProxy */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100556 if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Faulet010fded2016-11-03 22:49:37 +0100557 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100558 idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz);
559 if (str == NULL)
Christopher Faulet010fded2016-11-03 22:49:37 +0100560 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100561
562 DEBUG(frame->worker, "<%lu> Supported versions : %.*s",
563 frame->client->id, (int)sz, str);
Christopher Faulet010fded2016-11-03 22:49:37 +0100564
565 /* TODO: Find the right verion in supported ones */
566
567 return idx;
568}
569
570/* Check max frame size value. It returns -1 if an error occurred, the number of
571 * read bytes otherwise. */
572static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100573check_max_frame_size(struct spoe_frame *frame, int idx)
Christopher Faulet010fded2016-11-03 22:49:37 +0100574{
575 uint64_t sz;
576 int type, i;
577
578 /* Get the max-frame-size value of HAProxy */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100579 type = frame->buf[idx++];
Christopher Faulet010fded2016-11-03 22:49:37 +0100580 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
581 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
582 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
Christopher Fauletf95b1112016-12-21 08:58:16 +0100583 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64)
Christopher Faulet010fded2016-11-03 22:49:37 +0100584 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100585 if ((i = decode_spoe_varint(frame->buf+idx, frame->buf+frame->len, &sz)) == -1)
Christopher Faulet010fded2016-11-03 22:49:37 +0100586 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100587 idx += i;
588
589 /* Keep the lower value */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100590 if (sz < frame->client->max_frame_size)
591 frame->client->max_frame_size = sz;
592
593 DEBUG(frame->worker, "<%lu> HAProxy maximum frame size : %u",
594 frame->client->id, (unsigned int)sz);
Christopher Faulet010fded2016-11-03 22:49:37 +0100595
596 return idx;
597}
598
Christopher Fauletba7bc162016-11-07 21:07:38 +0100599/* Check healthcheck value. It returns -1 if an error occurred, the number of
600 * read bytes otherwise. */
601static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100602check_healthcheck(struct spoe_frame *frame, int idx)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100603{
604 int type;
605
Christopher Fauletf95b1112016-12-21 08:58:16 +0100606 /* Get the "healthcheck" value */
607 type = frame->buf[idx++];
608 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_BOOL)
609 return -1;
610 frame->hcheck = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE);
611
612 DEBUG(frame->worker, "<%lu> HELLO healthcheck : %s",
613 frame->client->id, (frame->hcheck ? "true" : "false"));
614 return idx;
615}
616
617/* Check capabilities value. It returns -1 if an error occurred, the number of
618 * read bytes otherwise. */
619static int
620check_capabilities(struct spoe_frame *frame, int idx)
621{
622 struct client *client = frame->client;
623 char *str;
624 uint64_t sz;
625 int i;
626
627 if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100628 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100629 idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz);
630 if (str == NULL) /* this is not an error */
631 return idx;
632
633 DEBUG(frame->worker, "<%lu> HAProxy capabilities : %.*s",
634 client->id, (int)sz, str);
635
636 i = 0;
637 while (i < sz) {
638 char *delim;
639
640 /* Skip leading spaces */
641 for (; isspace(str[i]) && i < sz; i++);
642
643 if (sz - i >= 10 && !strncmp(str + i, "pipelining", 10)) {
644 i += 10;
645 if (sz == i || isspace(str[i]) || str[i] == ',') {
646 DEBUG(frame->worker,
647 "<%lu> HAProxy supports frame pipelining",
648 client->id);
649 client->pipelining = true;
650 }
651
652 }
653 else if (sz - i >= 5 && !strncmp(str + i, "async", 5)) {
654 i += 5;
655 if (sz == i || isspace(str[i]) || str[i] == ',') {
656 DEBUG(frame->worker,
657 "<%lu> HAProxy supports asynchronous frame",
658 client->id);
659 client->async = true;
660 }
661 }
Christopher Faulet85010352017-02-02 10:14:36 +0100662 else if (sz - i >= 13 && !strncmp(str + i, "fragmentation", 13)) {
663 i += 5;
664 if (sz == i || isspace(str[i]) || str[i] == ',') {
665 DEBUG(frame->worker,
666 "<%lu> HAProxy supports fragmented frame",
667 client->id);
668 client->fragmentation = true;
669 }
670 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100671
672 if (sz == i || (delim = memchr(str + i, ',', sz-i)) == NULL)
673 break;
674 i = (delim - str) + 1;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100675 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100676
677 return idx;
678}
679
680/* Check engine-id value. It returns -1 if an error occurred, the number of
681 * read bytes otherwise. */
682static int
683check_engine_id(struct spoe_frame *frame, int idx)
684{
685 struct client *client = frame->client;
686 char *str;
687 uint64_t sz;
688
689 if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
690 return -1;
691
692 idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz);
693 if (str == NULL) /* this is not an error */
694 return idx;
695
696 if (client->engine != NULL)
697 return idx;
698
699 DEBUG(frame->worker, "<%lu> HAProxy engine id : %.*s",
700 client->id, (int)sz, str);
701
702 client->engine_id = strndup(str, (int)sz);
703 return idx;
704}
705
706/* Check disconnect status code. It returns -1 if an error occurred, the number
707 * of read bytes otherwise. */
708static int
709check_discon_status_code(struct spoe_frame *frame, int idx)
710{
711 uint64_t sz;
712 int type, i;
713
714 /* Get the "status-code" value */
715 type = frame->buf[idx++];
716 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
717 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
718 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
719 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64)
720 return -1;
721 if ((i = decode_spoe_varint(frame->buf+idx, frame->buf+frame->len, &sz)) == -1)
722 return -1;
723 idx += i;
724
725 frame->client->status_code = (unsigned int)sz;
726
727 DEBUG(frame->worker, "<%lu> Disconnect status code : %u",
728 frame->client->id, frame->client->status_code);
729
Christopher Fauletba7bc162016-11-07 21:07:38 +0100730 return idx;
731}
732
Christopher Fauletf95b1112016-12-21 08:58:16 +0100733/* Check the disconnect message. It returns -1 if an error occurred, the number
734 * of read bytes otherwise. */
735static int
736check_discon_message(struct spoe_frame *frame, int idx)
737{
738 char *str;
739 uint64_t sz;
740
741 /* Get the "message" value */
742 if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
743 return -1;
744 idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz);
745 if (str == NULL)
746 return -1;
747
748 DEBUG(frame->worker, "<%lu> Disconnect message : %.*s",
749 frame->client->id, (int)sz, str);
750
751 return idx;
752}
Christopher Fauletba7bc162016-11-07 21:07:38 +0100753
Christopher Faulet010fded2016-11-03 22:49:37 +0100754/* Decode a HELLO frame received from HAProxy. It returns -1 if an error
Christopher Fauletf95b1112016-12-21 08:58:16 +0100755 * occurred, otherwise the number of read bytes. HELLO frame cannot be
756 * ignored and having another frame than a HELLO frame is an error. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100757static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100758handle_hahello(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100759{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100760 struct client *client = frame->client;
761 char *buf = frame->buf;
762 char *end = frame->buf + frame->len;
763 int i, idx = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +0100764
Christopher Fauletf95b1112016-12-21 08:58:16 +0100765 /* Check frame type: we really want a HELLO frame */
766 if (buf[idx++] != SPOE_FRM_T_HAPROXY_HELLO)
767 goto error;
768
769 DEBUG(frame->worker, "<%lu> Decode HAProxy HELLO frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100770
Christopher Faulet85010352017-02-02 10:14:36 +0100771 /* Retrieve flags */
772 memcpy((char *)&(frame->flags), buf+idx, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +0100773 idx += 4;
774
Christopher Faulet85010352017-02-02 10:14:36 +0100775 /* Fragmentation is not supported for HELLO frame */
776 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
777 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
778 goto error;
779 }
780
Christopher Faulet010fded2016-11-03 22:49:37 +0100781 /* stream-id and frame-id must be cleared */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100782 if (buf[idx] != 0 || buf[idx+1] != 0) {
783 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100784 goto error;
785 }
786 idx += 2;
787
788 /* Loop on K/V items */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100789 while (buf+idx < end) {
Christopher Faulet010fded2016-11-03 22:49:37 +0100790 char *str;
791 uint64_t sz;
792
793 /* Decode the item name */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100794 idx += decode_spoe_string(buf+idx, end, &str, &sz);
Christopher Faulet010fded2016-11-03 22:49:37 +0100795 if (str == NULL) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100796 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100797 goto error;
798 }
799
800 /* Check "supported-versions" K/V item */
801 if (!memcmp(str, "supported-versions", sz)) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100802 if ((i = check_proto_version(frame, idx)) == -1) {
803 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100804 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100805 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100806 idx = i;
807 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100808 /* Check "max-frame-size" K/V item */
Christopher Faulet010fded2016-11-03 22:49:37 +0100809 else if (!memcmp(str, "max-frame-size", sz)) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100810 if ((i = check_max_frame_size(frame, idx)) == -1) {
811 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100812 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100813 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100814 idx = i;
815 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100816 /* Check "healthcheck" K/V item */
Christopher Fauletba7bc162016-11-07 21:07:38 +0100817 else if (!memcmp(str, "healthcheck", sz)) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100818 if ((i = check_healthcheck(frame, idx)) == -1) {
819 client->status_code = SPOE_FRM_ERR_INVALID;
820 goto error;
821 }
822 idx = i;
823 }
824 /* Check "capabilities" K/V item */
825 else if (!memcmp(str, "capabilities", sz)) {
826 if ((i = check_capabilities(frame, idx)) == -1) {
827 client->status_code = SPOE_FRM_ERR_INVALID;
828 goto error;
829 }
830 idx = i;
831 }
832 /* Check "engine-id" K/V item */
833 else if (!memcmp(str, "engine-id", sz)) {
834 if ((i = check_engine_id(frame, idx)) == -1) {
835 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100836 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100837 }
Christopher Fauletba7bc162016-11-07 21:07:38 +0100838 idx = i;
839 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100840 else {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100841 DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s",
842 client->id, (int)sz, str);
843
Christopher Faulet010fded2016-11-03 22:49:37 +0100844 /* Silently ignore unknown item */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100845 if ((i = skip_spoe_data(buf+idx, end)) == -1) {
846 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100847 goto error;
848 }
849 idx += i;
850 }
851 }
852
Christopher Fauletf95b1112016-12-21 08:58:16 +0100853 if (async == false || client->engine_id == NULL)
854 client->async = false;
855 if (pipelining == false)
856 client->pipelining = false;
857
858 if (client->async == true)
859 use_spoe_engine(client);
860
Christopher Faulet010fded2016-11-03 22:49:37 +0100861 return idx;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100862
863 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100864 return -1;
865}
866
867/* Decode a DISCONNECT frame received from HAProxy. It returns -1 if an error
Christopher Fauletf95b1112016-12-21 08:58:16 +0100868 * occurred, otherwise the number of read bytes. DISCONNECT frame cannot be
869 * ignored and having another frame than a DISCONNECT frame is an error.*/
Christopher Faulet010fded2016-11-03 22:49:37 +0100870static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100871handle_hadiscon(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100872{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100873 struct client *client = frame->client;
874 char *buf = frame->buf;
875 char *end = frame->buf + frame->len;
876 int i, idx = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +0100877
Christopher Fauletf95b1112016-12-21 08:58:16 +0100878 /* Check frame type: we really want a DISCONNECT frame */
879 if (buf[idx++] != SPOE_FRM_T_HAPROXY_DISCON)
880 goto error;
881
882 DEBUG(frame->worker, "<%lu> Decode HAProxy DISCONNECT frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100883
Christopher Faulet85010352017-02-02 10:14:36 +0100884 /* Retrieve flags */
885 memcpy((char *)&(frame->flags), buf+idx, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +0100886 idx += 4;
887
Christopher Faulet85010352017-02-02 10:14:36 +0100888 /* Fragmentation is not supported for DISCONNECT frame */
889 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
890 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
891 goto error;
892 }
893
Christopher Faulet010fded2016-11-03 22:49:37 +0100894 /* stream-id and frame-id must be cleared */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100895 if (buf[idx] != 0 || buf[idx+1] != 0) {
896 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100897 goto error;
898 }
899 idx += 2;
900
Christopher Fauletf95b1112016-12-21 08:58:16 +0100901 client->status_code = SPOE_FRM_ERR_NONE;
902
Christopher Faulet010fded2016-11-03 22:49:37 +0100903 /* Loop on K/V items */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100904 while (buf+idx < end) {
Christopher Faulet010fded2016-11-03 22:49:37 +0100905 char *str;
906 uint64_t sz;
907
908 /* Decode item key */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100909 idx += decode_spoe_string(buf+idx, end, &str, &sz);
Christopher Faulet010fded2016-11-03 22:49:37 +0100910 if (str == NULL) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100911 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100912 goto error;
913 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100914
915 /* Check "status-code" K/V item */
916 if (!memcmp(str, "status-code", sz)) {
917 if ((i = check_discon_status_code(frame, idx)) == -1) {
918 client->status_code = SPOE_FRM_ERR_INVALID;
919 goto error;
920 }
921 idx = i;
922 }
923 /* Check "message" K/V item */
924 else if (!memcmp(str, "message", sz)) {
925 if ((i = check_discon_message(frame, idx)) == -1) {
926 client->status_code = SPOE_FRM_ERR_INVALID;
927 goto error;
928 }
929 idx = i;
930 }
931 else {
932 DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s",
933 client->id, (int)sz, str);
934
935 /* Silently ignore unknown item */
936 if ((i = skip_spoe_data(buf+idx, end)) == -1) {
937 client->status_code = SPOE_FRM_ERR_INVALID;
938 goto error;
939 }
940 idx += i;
Christopher Faulet010fded2016-11-03 22:49:37 +0100941 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100942 }
943
Christopher Faulet010fded2016-11-03 22:49:37 +0100944 return idx;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100945
946 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100947 return -1;
948}
949
950/* Decode a NOTIFY frame received from HAProxy. It returns -1 if an error
Christopher Faulet85010352017-02-02 10:14:36 +0100951 * occurred, 0 if it must be must be ignored, otherwise the number of read
952 * bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100953static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100954handle_hanotify(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100955{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100956 struct client *client = frame->client;
957 char *buf = frame->buf;
958 char *end = frame->buf + frame->len;
959 uint64_t stream_id, frame_id;
960 int i, idx = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +0100961
962 /* Check frame type */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100963 if (buf[idx++] != SPOE_FRM_T_HAPROXY_NOTIFY)
964 goto ignore;
965
966 DEBUG(frame->worker, "<%lu> Decode HAProxy NOTIFY frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100967
Christopher Faulet85010352017-02-02 10:14:36 +0100968 /* Retrieve flags */
969 memcpy((char *)&(frame->flags), buf+idx, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +0100970 idx += 4;
971
Christopher Faulet85010352017-02-02 10:14:36 +0100972 /* Fragmentation is not supported for DISCONNECT frame */
973 if (!(frame->flags & SPOE_FRM_FL_FIN) && fragmentation == false) {
974 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
975 goto error;
976 }
977
Christopher Faulet010fded2016-11-03 22:49:37 +0100978 /* Read the stream-id */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100979 if ((i = decode_spoe_varint(buf+idx, end, &stream_id)) == -1)
980 goto ignore;
Christopher Faulet010fded2016-11-03 22:49:37 +0100981 idx += i;
982
983 /* Read the frame-id */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100984 if ((i = decode_spoe_varint(buf+idx, end, &frame_id)) == -1)
985 goto ignore;
Christopher Faulet010fded2016-11-03 22:49:37 +0100986 idx += i;
987
Christopher Faulet85010352017-02-02 10:14:36 +0100988 if (frame->fragmented == true) {
989 if (frame->stream_id != (unsigned int)stream_id ||
990 frame->frame_id != (unsigned int)frame_id) {
991 client->status_code = SPOE_FRM_ERR_INTERLACED_FRAMES;
992 goto error;
993 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100994
Christopher Faulet85010352017-02-02 10:14:36 +0100995 if (frame->flags & SPOE_FRM_FL_ABRT) {
996 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
997 " - Abort processing of a fragmented frame"
998 " - frag_len=%u - len=%u - offset=%u",
999 client->id, frame->stream_id, frame->frame_id,
1000 frame->frag_len, frame->len, idx);
1001 goto ignore;
1002 }
1003 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
1004 " - %s fragment of a fragmented frame received"
1005 " - frag_len=%u - len=%u - offset=%u",
1006 client->id, frame->stream_id, frame->frame_id,
1007 (frame->flags & SPOE_FRM_FL_FIN) ? "last" : "next",
1008 frame->frag_len, frame->len, idx);
1009 }
1010 else {
1011 frame->stream_id = (unsigned int)stream_id;
1012 frame->frame_id = (unsigned int)frame_id;
Christopher Faulet010fded2016-11-03 22:49:37 +01001013
Christopher Faulet85010352017-02-02 10:14:36 +01001014 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
1015 " - %s frame received"
1016 " - frag_len=%u - len=%u - offset=%u",
1017 client->id, frame->stream_id, frame->frame_id,
1018 (frame->flags & SPOE_FRM_FL_FIN) ? "unfragmented" : "fragmented",
1019 frame->frag_len, frame->len, idx);
1020
1021 frame->fragmented = !(frame->flags & SPOE_FRM_FL_FIN);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001022 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001023
Christopher Fauletf95b1112016-12-21 08:58:16 +01001024 frame->offset = idx;
1025 return idx;
Christopher Faulet010fded2016-11-03 22:49:37 +01001026
Christopher Fauletf95b1112016-12-21 08:58:16 +01001027 ignore:
Christopher Faulet85010352017-02-02 10:14:36 +01001028 return 0;
1029
1030 error:
Christopher Fauletf95b1112016-12-21 08:58:16 +01001031 return -1;
1032}
Christopher Faulet010fded2016-11-03 22:49:37 +01001033
Christopher Fauletf95b1112016-12-21 08:58:16 +01001034/* Encode a HELLO frame to send it to HAProxy. It returns the number of written
1035 * bytes. */
1036static int
1037prepare_agenthello(struct spoe_frame *frame)
1038{
1039 struct client *client = frame->client;
1040 char *buf = frame->buf;
Christopher Faulet85010352017-02-02 10:14:36 +01001041 char capabilities[64];
1042 int n, idx = 0;
1043 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet010fded2016-11-03 22:49:37 +01001044
Christopher Fauletf95b1112016-12-21 08:58:16 +01001045 DEBUG(frame->worker, "<%lu> Encode Agent HELLO frame", client->id);
1046 frame->type = SPOA_FRM_T_AGENT;
Christopher Faulet010fded2016-11-03 22:49:37 +01001047
1048 /* Frame Type */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001049 buf[idx++] = SPOE_FRM_T_AGENT_HELLO;
Christopher Faulet010fded2016-11-03 22:49:37 +01001050
Christopher Faulet85010352017-02-02 10:14:36 +01001051 /* Set flags */
1052 memcpy(buf+idx, (char *)&flags, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001053 idx += 4;
1054
1055 /* No stream-id and frame-id for HELLO frames */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001056 buf[idx++] = 0;
1057 buf[idx++] = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +01001058
1059 /* "version" K/V item */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001060 idx += encode_spoe_string("version", 7, buf+idx);
1061 buf[idx++] = SPOE_DATA_T_STR;
1062 idx += encode_spoe_string(SPOP_VERSION, SLEN(SPOP_VERSION), buf+idx);
1063 DEBUG(frame->worker, "<%lu> Agent version : %s",
1064 client->id, SPOP_VERSION);
1065
Christopher Faulet010fded2016-11-03 22:49:37 +01001066
1067 /* "max-frame-size" K/V item */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001068 idx += encode_spoe_string("max-frame-size", 14, buf+idx);
1069 buf[idx++] = SPOE_DATA_T_UINT32;
1070 idx += encode_spoe_varint(client->max_frame_size, buf+idx);
1071 DEBUG(frame->worker, "<%lu> Agent maximum frame size : %u",
1072 client->id, client->max_frame_size);
Christopher Faulet010fded2016-11-03 22:49:37 +01001073
1074 /* "capabilities" K/V item */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001075 idx += encode_spoe_string("capabilities", 12, buf+idx);
1076 buf[idx++] = SPOE_DATA_T_STR;
Christopher Faulet85010352017-02-02 10:14:36 +01001077
1078 memset(capabilities, 0, sizeof(capabilities));
1079 n = 0;
1080
1081 /* 1. Fragmentation capability ? */
1082 if (fragmentation == true) {
1083 memcpy(capabilities, "fragmentation", 13);
1084 n += 13;
1085 }
1086 /* 2. Pipelining capability ? */
1087 if (client->pipelining == true && n != 0) {
1088 memcpy(capabilities + n, ", pipelining", 12);
1089 n += 12;
1090 }
1091 else if (client->pipelining == true) {
1092 memcpy(capabilities, "pipelining", 10);
1093 n += 10;
1094 }
1095 /* 3. Async capability ? */
1096 if (client->async == true && n != 0) {
1097 memcpy(capabilities + n, ", async", 7);
1098 n += 7;
1099 }
1100 else if (client->async == true) {
1101 memcpy(capabilities, "async", 5);
1102 n += 5;
1103 }
1104 /* 4. Encode capabilities string */
1105 if (n != 0)
1106 idx += encode_spoe_string(capabilities, n, buf+idx);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001107 else
1108 idx += encode_spoe_string(NULL, 0, buf+idx);
Christopher Faulet010fded2016-11-03 22:49:37 +01001109
Christopher Faulet85010352017-02-02 10:14:36 +01001110 DEBUG(frame->worker, "<%lu> Agent capabilities : %.*s",
1111 client->id, n, capabilities);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001112
1113 frame->len = idx;
Christopher Faulet010fded2016-11-03 22:49:37 +01001114 return idx;
1115}
1116
Christopher Fauletf95b1112016-12-21 08:58:16 +01001117/* Encode a DISCONNECT frame to send it to HAProxy. It returns the number of
1118 * written bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +01001119static int
Christopher Fauletf95b1112016-12-21 08:58:16 +01001120prepare_agentdicon(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +01001121{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001122 struct client *client = frame->client;
1123 char *buf = frame->buf;
1124 const char *reason;
1125 int rlen, idx = 0;
Christopher Faulet85010352017-02-02 10:14:36 +01001126 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001127
1128 DEBUG(frame->worker, "<%lu> Encode Agent DISCONNECT frame", client->id);
1129 frame->type = SPOA_FRM_T_AGENT;
1130
1131 if (client->status_code >= SPOE_FRM_ERRS)
1132 client->status_code = SPOE_FRM_ERR_UNKNOWN;
1133 reason = spoe_frm_err_reasons[client->status_code];
1134 rlen = strlen(reason);
Christopher Faulet010fded2016-11-03 22:49:37 +01001135
1136 /* Frame type */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001137 buf[idx++] = SPOE_FRM_T_AGENT_DISCON;
Christopher Faulet010fded2016-11-03 22:49:37 +01001138
Christopher Faulet85010352017-02-02 10:14:36 +01001139 /* Set flags */
1140 memcpy(buf+idx, (char *)&flags, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001141 idx += 4;
1142
Christopher Fauletf95b1112016-12-21 08:58:16 +01001143 /* No stream-id and frame-id for DISCONNECT frames */
1144 buf[idx++] = 0;
1145 buf[idx++] = 0;
1146
1147 /* There are 2 mandatory items: "status-code" and "message" */
1148
1149 /* "status-code" K/V item */
1150 idx += encode_spoe_string("status-code", 11, buf+idx);
1151 buf[idx++] = SPOE_DATA_T_UINT32;
1152 idx += encode_spoe_varint(client->status_code, buf+idx);
1153 DEBUG(frame->worker, "<%lu> Disconnect status code : %u",
1154 client->id, client->status_code);
Christopher Faulet010fded2016-11-03 22:49:37 +01001155
Christopher Fauletf95b1112016-12-21 08:58:16 +01001156 /* "message" K/V item */
1157 idx += encode_spoe_string("message", 7, buf+idx);
1158 buf[idx++] = SPOE_DATA_T_STR;
1159 idx += encode_spoe_string(reason, rlen, buf+idx);
1160 DEBUG(frame->worker, "<%lu> Disconnect message : %s",
1161 client->id, reason);
Christopher Faulet010fded2016-11-03 22:49:37 +01001162
Christopher Fauletf95b1112016-12-21 08:58:16 +01001163 frame->len = idx;
Christopher Faulet010fded2016-11-03 22:49:37 +01001164 return idx;
1165}
1166
Christopher Fauletf95b1112016-12-21 08:58:16 +01001167/* Encode a ACK frame to send it to HAProxy. It returns the number of written
1168 * bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +01001169static int
Christopher Fauletf95b1112016-12-21 08:58:16 +01001170prepare_agentack(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +01001171{
Christopher Faulet85010352017-02-02 10:14:36 +01001172 char *buf = frame->buf;
1173 int idx = 0;
1174 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet010fded2016-11-03 22:49:37 +01001175
Christopher Fauletf95b1112016-12-21 08:58:16 +01001176 /* Be careful here, in async mode, frame->client can be NULL */
1177
1178 DEBUG(frame->worker, "Encode Agent ACK frame");
1179 frame->type = SPOA_FRM_T_AGENT;
Christopher Faulet010fded2016-11-03 22:49:37 +01001180
1181 /* Frame type */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001182 buf[idx++] = SPOE_FRM_T_AGENT_ACK;
Christopher Faulet010fded2016-11-03 22:49:37 +01001183
Christopher Faulet85010352017-02-02 10:14:36 +01001184 /* Set flags */
1185 memcpy(buf+idx, (char *)&flags, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001186 idx += 4;
1187
Christopher Fauletf95b1112016-12-21 08:58:16 +01001188 /* Set stream-id and frame-id for ACK frames */
1189 idx += encode_spoe_varint(frame->stream_id, buf+idx);
1190 idx += encode_spoe_varint(frame->frame_id, buf+idx);
Christopher Faulet010fded2016-11-03 22:49:37 +01001191
Christopher Fauletf95b1112016-12-21 08:58:16 +01001192 DEBUG(frame->worker, "STREAM-ID=%u - FRAME-ID=%u",
1193 frame->stream_id, frame->frame_id);
Christopher Faulet010fded2016-11-03 22:49:37 +01001194
Christopher Fauletf95b1112016-12-21 08:58:16 +01001195 frame->len = idx;
Christopher Faulet010fded2016-11-03 22:49:37 +01001196 return idx;
1197}
1198
1199static int
Christopher Fauletf95b1112016-12-21 08:58:16 +01001200create_server_socket(void)
Christopher Faulet010fded2016-11-03 22:49:37 +01001201{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001202 struct sockaddr_in listen_addr;
1203 int fd, yes = 1;
1204
1205 fd = socket(AF_INET, SOCK_STREAM, 0);
1206 if (fd < 0) {
1207 LOG(&null_worker, "Failed to create service socket : %m");
1208 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001209 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001210
1211 memset(&listen_addr, 0, sizeof(listen_addr));
1212 listen_addr.sin_family = AF_INET;
1213 listen_addr.sin_addr.s_addr = INADDR_ANY;
1214 listen_addr.sin_port = htons(server_port);
1215
1216 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0 ||
1217 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) < 0) {
1218 LOG(&null_worker, "Failed to set option on server socket : %m");
1219 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001220 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001221
1222 if (bind(fd, (struct sockaddr *) &listen_addr, sizeof(listen_addr)) < 0) {
1223 LOG(&null_worker, "Failed to bind server socket : %m");
1224 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001225 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001226
1227 if (listen(fd, CONNECTION_BACKLOG) < 0) {
1228 LOG(&null_worker, "Failed to listen on server socket : %m");
1229 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001230 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001231
1232 return fd;
Christopher Faulet010fded2016-11-03 22:49:37 +01001233}
1234
Christopher Fauletf95b1112016-12-21 08:58:16 +01001235static void
1236release_frame(struct spoe_frame *frame)
1237{
1238 struct worker *worker;
1239
1240 if (frame == NULL)
1241 return;
1242
1243 if (event_pending(&frame->process_frame_event, EV_TIMEOUT, NULL))
1244 event_del(&frame->process_frame_event);
1245
1246 worker = frame->worker;
1247 LIST_DEL(&frame->list);
Christopher Faulet85010352017-02-02 10:14:36 +01001248 if (frame->frag_buf)
1249 free(frame->frag_buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001250 memset(frame, 0, sizeof(*frame)+max_frame_size+4);
1251 LIST_ADDQ(&worker->frames, &frame->list);
1252}
1253
1254static void
1255release_client(struct client *c)
Christopher Faulet010fded2016-11-03 22:49:37 +01001256{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001257 struct spoe_frame *frame, *back;
1258
1259 if (c == NULL)
1260 return;
1261
1262 DEBUG(c->worker, "<%lu> Release client", c->id);
1263
1264 LIST_DEL(&c->by_worker);
1265 c->worker->nbclients--;
1266
1267 unuse_spoe_engine(c);
1268 free(c->engine_id);
1269
1270 if (event_pending(&c->read_frame_event, EV_READ, NULL))
1271 event_del(&c->read_frame_event);
1272 if (event_pending(&c->write_frame_event, EV_WRITE, NULL))
1273 event_del(&c->write_frame_event);
1274
1275 release_frame(c->incoming_frame);
1276 release_frame(c->outgoing_frame);
1277 list_for_each_entry_safe(frame, back, &c->processing_frames, list) {
1278 release_frame(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +01001279 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001280 list_for_each_entry_safe(frame, back, &c->outgoing_frames, list) {
1281 release_frame(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +01001282 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001283
1284 if (c->fd >= 0)
1285 close(c->fd);
1286
1287 free(c);
1288}
1289
1290static void
1291reset_frame(struct spoe_frame *frame)
1292{
1293 if (frame == NULL)
1294 return;
1295
Christopher Faulet85010352017-02-02 10:14:36 +01001296 if (frame->frag_buf)
1297 free(frame->frag_buf);
1298
1299 frame->type = SPOA_FRM_T_UNKNOWN;
1300 frame->buf = (char *)(frame->data);
1301 frame->offset = 0;
1302 frame->len = 0;
1303 frame->stream_id = 0;
1304 frame->frame_id = 0;
1305 frame->flags = 0;
1306 frame->hcheck = false;
1307 frame->fragmented = false;
1308 frame->ip_score = -1;
1309 frame->frag_buf = NULL;
1310 frame->frag_len = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001311 LIST_INIT(&frame->list);
1312}
1313
1314static void
1315use_spoe_engine(struct client *client)
1316{
1317 struct spoe_engine *eng;
1318
1319 if (client->engine_id == NULL)
1320 return;
1321
1322 list_for_each_entry(eng, &client->worker->engines, list) {
1323 if (!strcmp(eng->id, client->engine_id))
1324 goto end;
1325 }
1326
1327 if ((eng = malloc(sizeof(*eng))) == NULL) {
1328 client->async = false;
1329 return;
1330 }
1331
1332 eng->id = strdup(client->engine_id);
1333 LIST_INIT(&eng->clients);
1334 LIST_INIT(&eng->processing_frames);
1335 LIST_INIT(&eng->outgoing_frames);
1336 LIST_ADDQ(&client->worker->engines, &eng->list);
1337 LOG(client->worker, "Add new SPOE engine '%s'", eng->id);
1338
1339 end:
1340 client->engine = eng;
1341 LIST_ADDQ(&eng->clients, &client->by_engine);
1342}
1343
1344static void
1345unuse_spoe_engine(struct client *client)
1346{
1347 struct spoe_engine *eng;
1348 struct spoe_frame *frame, *back;
1349
1350 if (client == NULL || client->engine == NULL)
1351 return;
1352
1353 eng = client->engine;
1354 client->engine = NULL;
1355 LIST_DEL(&client->by_engine);
1356 if (!LIST_ISEMPTY(&eng->clients))
1357 return;
1358
1359 LOG(client->worker, "Remove SPOE engine '%s'", eng->id);
1360 LIST_DEL(&eng->list);
1361
1362 list_for_each_entry_safe(frame, back, &eng->processing_frames, list) {
1363 release_frame(frame);
1364 }
1365 list_for_each_entry_safe(frame, back, &eng->outgoing_frames, list) {
1366 release_frame(frame);
1367 }
1368 free(eng->id);
1369 free(eng);
1370}
1371
1372
1373static struct spoe_frame *
1374acquire_incoming_frame(struct client *client)
1375{
1376 struct spoe_frame *frame;
1377
1378 frame = client->incoming_frame;
1379 if (frame != NULL)
1380 return frame;
1381
1382 if (LIST_ISEMPTY(&client->worker->frames)) {
1383 if ((frame = calloc(1, sizeof(*frame)+max_frame_size+4)) == NULL) {
1384 LOG(client->worker, "Failed to allocate new frame : %m");
1385 return NULL;
1386 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001387 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001388 else {
1389 frame = LIST_NEXT(&client->worker->frames, typeof(frame), list);
1390 LIST_DEL(&frame->list);
Christopher Faulet010fded2016-11-03 22:49:37 +01001391 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001392
1393 reset_frame(frame);
1394 frame->worker = client->worker;
1395 frame->engine = client->engine;
1396 frame->client = client;
1397
1398 if (event_assign(&frame->process_frame_event, client->worker->base, -1,
1399 EV_TIMEOUT|EV_PERSIST, process_frame_cb, frame) < 0) {
1400 LOG(client->worker, "Failed to create frame event");
1401 return NULL;
Christopher Faulet010fded2016-11-03 22:49:37 +01001402 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001403
1404 client->incoming_frame = frame;
1405 return frame;
Christopher Faulet010fded2016-11-03 22:49:37 +01001406}
1407
Christopher Fauletf95b1112016-12-21 08:58:16 +01001408static struct spoe_frame *
1409acquire_outgoing_frame(struct client *client)
Christopher Faulet010fded2016-11-03 22:49:37 +01001410{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001411 struct spoe_engine *engine = client->engine;
1412 struct spoe_frame *frame = NULL;
Christopher Faulet010fded2016-11-03 22:49:37 +01001413
Christopher Fauletf95b1112016-12-21 08:58:16 +01001414 if (client->outgoing_frame != NULL)
1415 frame = client->outgoing_frame;
1416 else if (!LIST_ISEMPTY(&client->outgoing_frames)) {
1417 frame = LIST_NEXT(&client->outgoing_frames, typeof(frame), list);
1418 LIST_DEL(&frame->list);
1419 client->outgoing_frame = frame;
1420 }
1421 else if (engine!= NULL && !LIST_ISEMPTY(&engine->outgoing_frames)) {
1422 frame = LIST_NEXT(&engine->outgoing_frames, typeof(frame), list);
1423 LIST_DEL(&frame->list);
1424 client->outgoing_frame = frame;
1425 }
1426 return frame;
1427}
1428
1429static void
1430write_frame(struct client *client, struct spoe_frame *frame)
1431{
1432 uint32_t netint;
1433
1434 LIST_DEL(&frame->list);
1435
1436 frame->buf = (char *)(frame->data);
1437 frame->offset = 0;
1438 netint = htonl(frame->len);
1439 memcpy(frame->buf, &netint, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001440
Christopher Fauletf95b1112016-12-21 08:58:16 +01001441 if (client != NULL) { /* HELLO or DISCONNECT frames */
1442 event_add(&client->write_frame_event, NULL);
Christopher Faulet010fded2016-11-03 22:49:37 +01001443
Christopher Fauletf95b1112016-12-21 08:58:16 +01001444 /* Try to process the frame as soon as possible, and always
1445 * attach it to the client */
1446 if (client->async || client->pipelining) {
1447 if (client->outgoing_frame == NULL)
1448 client->outgoing_frame = frame;
1449 else
1450 LIST_ADD(&client->outgoing_frames, &frame->list);
1451 }
1452 else {
1453 client->outgoing_frame = frame;
1454 event_del(&client->read_frame_event);
Christopher Faulet010fded2016-11-03 22:49:37 +01001455 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001456 }
1457 else { /* for all other frames */
1458 if (frame->client == NULL) { /* async mode ! */
1459 LIST_ADDQ(&frame->engine->outgoing_frames, &frame->list);
1460 list_for_each_entry(client, &frame->engine->clients, by_engine)
1461 event_add(&client->write_frame_event, NULL);
1462 }
1463 else if (frame->client->pipelining) {
1464 LIST_ADDQ(&frame->client->outgoing_frames, &frame->list);
1465 event_add(&frame->client->write_frame_event, NULL);
1466 }
1467 else {
1468 frame->client->outgoing_frame = frame;
1469 event_add(&frame->client->write_frame_event, NULL);
1470 event_del(&frame->client->read_frame_event);
1471 }
1472 }
1473}
Christopher Faulet010fded2016-11-03 22:49:37 +01001474
Christopher Fauletf95b1112016-12-21 08:58:16 +01001475static void
1476process_incoming_frame(struct spoe_frame *frame)
1477{
1478 struct client *client = frame->client;
Christopher Faulet010fded2016-11-03 22:49:37 +01001479
Christopher Fauletf95b1112016-12-21 08:58:16 +01001480 if (event_add(&frame->process_frame_event, &processing_delay) < 0) {
1481 LOG(client->worker, "Failed to process incoming frame");
1482 release_frame(frame);
1483 return;
1484 }
1485
1486 if (client->async) {
1487 frame->client = NULL;
1488 LIST_ADDQ(&frame->engine->processing_frames, &frame->list);
1489 }
1490 else if (client->pipelining)
1491 LIST_ADDQ(&client->processing_frames, &frame->list);
1492 else
1493 event_del(&client->read_frame_event);
1494}
1495
1496static void
1497signal_cb(evutil_socket_t sig, short events, void *user_data)
1498{
1499 struct event_base *base = user_data;
1500 int i;
1501
1502 DEBUG(&null_worker, "Stopping the server");
1503
1504 event_base_loopbreak(base);
1505 DEBUG(&null_worker, "Main event loop stopped");
1506
1507 for (i = 0; i < num_workers; i++) {
1508 event_base_loopbreak(workers[i].base);
1509 DEBUG(&null_worker, "Event loop stopped for worker %02d",
1510 workers[i].id);
1511 }
1512}
1513
1514static void
1515worker_monitor_cb(evutil_socket_t fd, short events, void *arg)
1516{
1517 struct worker *worker = arg;
1518
1519 LOG(worker, "%u clients connected", worker->nbclients);
1520}
1521
1522static void
1523process_frame_cb(evutil_socket_t fd, short events, void *arg)
1524{
1525 struct spoe_frame *frame = arg;
1526 char *buf = frame->buf;
1527 char *end = frame->buf + frame->len;
1528 int idx = frame->offset;
1529
1530 DEBUG(frame->worker,
Christopher Faulet85010352017-02-02 10:14:36 +01001531 "Process frame messages : STREAM-ID=%u - FRAME-ID=%u - length=%u bytes",
1532 frame->stream_id, frame->frame_id, frame->len - frame->offset);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001533
1534 /* Loop on messages */
1535 while (buf+idx < end) {
1536 char *str;
1537 uint64_t sz;
1538 int nbargs, i;
1539
1540 /* Decode the message name */
1541 idx += decode_spoe_string(buf+idx, end, &str, &sz);
1542 if (str == NULL)
1543 goto stop_processing;
1544
1545 DEBUG(frame->worker, "Process SPOE Message '%.*s'", (int)sz, str);
1546
1547 nbargs = buf[idx++]; /* Get the number of arguments */
1548 frame->offset = idx; /* Save index to handle errors and skip args */
1549 if (!memcmp(str, "check-client-ip", sz)) {
1550 struct spoe_data data;
1551
1552 memset(&data, 0, sizeof(data));
1553
1554 if (nbargs != 1)
1555 goto skip_message;
1556
1557 if ((i = decode_spoe_string(buf+idx, end, &str, &sz)) == -1)
1558 goto stop_processing;
1559 idx += i;
1560
1561 if ((i = decode_spoe_data(buf+idx, end, &data)) == -1)
1562 goto skip_message;
1563 idx += i;
1564
1565 if ((data.type & SPOE_DATA_T_MASK) == SPOE_DATA_T_IPV4)
1566 check_ipv4_reputation(frame, &data.u.ipv4);
1567 else if ((data.type & SPOE_DATA_T_MASK) == SPOE_DATA_T_IPV6)
1568 check_ipv6_reputation(frame, &data.u.ipv6);
1569 }
1570 else {
1571 skip_message:
1572 idx = frame->offset; /* Restore index */
1573
1574 while (nbargs-- > 0) {
1575 /* Silently ignore argument: its name and its value */
1576 if ((i = decode_spoe_string(buf+idx, end, &str, &sz)) == -1)
1577 goto stop_processing;
1578 idx += i;
1579 if ((i = skip_spoe_data(buf+idx, end)) == -1)
1580 goto stop_processing;
1581 idx += i;
1582 }
1583 }
1584 }
1585
1586 stop_processing:
1587 /* Prepare agent ACK frame */
Christopher Faulet85010352017-02-02 10:14:36 +01001588 frame->buf = (char *)(frame->data) + 4;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001589 frame->offset = 0;
Christopher Faulet85010352017-02-02 10:14:36 +01001590 frame->len = 0;
1591 frame->flags = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001592 idx = prepare_agentack(frame);
1593
1594 if (frame->ip_score != -1) {
1595 DEBUG(frame->worker, "Add action : set variable ip_scode=%u",
1596 frame->ip_score);
1597
1598 buf[idx++] = SPOE_ACT_T_SET_VAR; /* Action type */
1599 buf[idx++] = 3; /* Number of args */
1600 buf[idx++] = SPOE_SCOPE_SESS; /* Arg 1: the scope */
1601 idx += encode_spoe_string("ip_score", 8, buf+idx); /* Arg 2: variable name */
1602 buf[idx++] = SPOE_DATA_T_UINT32;
1603 idx += encode_spoe_varint(frame->ip_score, buf+idx); /* Arg 3: variable value */
1604 frame->len = idx;
1605 }
1606 write_frame(NULL, frame);
1607}
1608
1609static void
1610read_frame_cb(evutil_socket_t fd, short events, void *arg)
1611{
1612 struct client *client = arg;
1613 struct spoe_frame *frame;
1614 uint32_t netint;
1615 int n;
1616
1617 DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__);
1618 if ((frame = acquire_incoming_frame(client)) == NULL)
1619 goto close;
1620
1621 frame->type = SPOA_FRM_T_HAPROXY;
1622 if (frame->buf == (char *)(frame->data)) {
1623 /* Read the frame length: frame->buf points on length part (frame->data) */
1624 n = read(client->fd, frame->buf+frame->offset, 4-frame->offset);
1625 if (n <= 0) {
1626 if (n < 0)
1627 LOG(client->worker, "Failed to read frame length : %m");
Christopher Fauletba7bc162016-11-07 21:07:38 +01001628 goto close;
Christopher Faulet010fded2016-11-03 22:49:37 +01001629 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001630 frame->offset += n;
1631 if (frame->offset != 4)
1632 return;
1633 memcpy(&netint, frame->buf, 4);
1634 frame->buf += 4;
1635 frame->offset = 0;
1636 frame->len = ntohl(netint);
1637 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001638
Christopher Fauletf95b1112016-12-21 08:58:16 +01001639 /* Read the frame: frame->buf points on frame part (frame->data+4)*/
1640 n = read(client->fd, frame->buf + frame->offset,
1641 frame->len - frame->offset);
1642 if (n <= 0) {
1643 if (n < 0) {
1644 LOG(client->worker, "Frame to read frame : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001645 goto close;
1646 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001647 return;
1648 }
1649 frame->offset += n;
1650 if (frame->offset != frame->len)
1651 return;
1652 frame->offset = 0;
1653
1654 DEBUG(client->worker, "<%lu> New Frame of %u bytes received",
1655 client->id, frame->len);
1656
1657 switch (client->state) {
1658 case SPOA_ST_CONNECTING:
1659 if (handle_hahello(frame) < 0) {
1660 LOG(client->worker, "Failed to decode HELLO frame");
1661 goto disconnect;
1662 }
1663 prepare_agenthello(frame);
1664 goto write_frame;
1665
1666 case SPOA_ST_PROCESSING:
Christopher Faulet85010352017-02-02 10:14:36 +01001667 if (frame->buf[0] == SPOE_FRM_T_HAPROXY_DISCON) {
Christopher Fauletf95b1112016-12-21 08:58:16 +01001668 client->state = SPOA_ST_DISCONNECTING;
1669 goto disconnecting;
1670 }
Christopher Faulet85010352017-02-02 10:14:36 +01001671 n = handle_hanotify(frame);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001672 if (n < 0) {
Christopher Faulet85010352017-02-02 10:14:36 +01001673 LOG(client->worker, "Failed to decode frame: %s",
1674 spoe_frm_err_reasons[client->status_code]);
1675 goto disconnect;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001676 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001677 else if (n == 0) {
Christopher Faulet85010352017-02-02 10:14:36 +01001678 LOG(client->worker, "Ignore invalid/unknown/aborted frame");
1679 goto ignore_frame;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001680 }
1681 else
1682 goto process_frame;
1683
1684 case SPOA_ST_DISCONNECTING:
1685 disconnecting:
1686 if (handle_hadiscon(frame) < 0) {
1687 LOG(client->worker, "Failed to decode DISCONNECT frame");
1688 goto disconnect;
1689 }
1690 if (client->status_code != SPOE_FRM_ERR_NONE)
1691 LOG(client->worker, "<%lu> Peer closed connection: %s",
1692 client->id, spoe_frm_err_reasons[client->status_code]);
1693 client->status_code = SPOE_FRM_ERR_NONE;
1694 goto disconnect;
1695 }
1696
1697 ignore_frame:
1698 reset_frame(frame);
1699 return;
1700
1701 process_frame:
Christopher Faulet85010352017-02-02 10:14:36 +01001702 if (frame->fragmented == true) {
1703 char *buf;
1704 size_t len = frame->len - frame->offset;
1705
1706 buf = realloc(frame->frag_buf, frame->frag_len + len);
1707 if (buf == NULL) {
1708 client->status_code = SPOE_FRM_ERR_RES;
1709 goto disconnect;
1710 }
1711 memcpy(buf + frame->frag_len, frame->buf + frame->offset, len);
1712 frame->frag_buf = buf;
1713 frame->frag_len += len;
1714
1715 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
1716 /* Wait for next fragments */
1717 frame->buf = (char *)(frame->data);
1718 frame->offset = 0;
1719 frame->len = 0;
1720 frame->flags = 0;
1721 return;
1722 }
1723
1724 frame->buf = frame->frag_buf;
1725 frame->len = frame->frag_len;
1726 frame->offset = 0;
1727 /* fall through */
1728 }
1729
Christopher Fauletf95b1112016-12-21 08:58:16 +01001730 process_incoming_frame(frame);
1731 client->incoming_frame = NULL;
1732 return;
1733
1734 write_frame:
1735 write_frame(client, frame);
1736 client->incoming_frame = NULL;
1737 return;
1738
1739 disconnect:
1740 client->state = SPOA_ST_DISCONNECTING;
1741 if (prepare_agentdicon(frame) < 0) {
1742 LOG(client->worker, "Failed to encode DISCONNECT frame");
1743 goto close;
1744 }
1745 goto write_frame;
1746
1747 close:
1748 release_client(client);
1749}
1750
1751static void
1752write_frame_cb(evutil_socket_t fd, short events, void *arg)
1753{
1754 struct client *client = arg;
1755 struct spoe_frame *frame;
1756 int n;
1757
1758 DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__);
1759 if ((frame = acquire_outgoing_frame(client)) == NULL) {
1760 event_del(&client->write_frame_event);
1761 return;
1762 }
1763
1764 if (frame->buf == (char *)(frame->data)) {
1765 /* Write the frame length: frame->buf points on length part (frame->data) */
1766 n = write(client->fd, frame->buf+frame->offset, 4-frame->offset);
1767 if (n <= 0) {
1768 if (n < 0)
1769 LOG(client->worker, "Failed to write frame length : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001770 goto close;
1771 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001772 frame->offset += n;
1773 if (frame->offset != 4)
1774 return;
1775 frame->buf += 4;
1776 frame->offset = 0;
1777 }
1778
1779 /* Write the frame: frame->buf points on frame part (frame->data+4)*/
1780 n = write(client->fd, frame->buf + frame->offset,
1781 frame->len - frame->offset);
1782 if (n <= 0) {
1783 if (n < 0) {
1784 LOG(client->worker, "Failed to write frame : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001785 goto close;
1786 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001787 return;
1788 }
1789 frame->offset += n;
1790 if (frame->offset != frame->len)
1791 return;
1792
1793 DEBUG(client->worker, "<%lu> Frame of %u bytes send",
1794 client->id, frame->len);
1795
1796 switch (client->state) {
1797 case SPOA_ST_CONNECTING:
1798 if (frame->hcheck == true) {
1799 DEBUG(client->worker,
1800 "<%lu> Close client after healthcheck",
1801 client->id);
1802 goto close;
1803 }
1804 client->state = SPOA_ST_PROCESSING;
1805 break;
1806
1807 case SPOA_ST_PROCESSING:
1808 break;
1809
1810 case SPOA_ST_DISCONNECTING:
1811 goto close;
1812 }
1813
1814 release_frame(frame);
1815 client->outgoing_frame = NULL;
1816 if (!client->async && !client->pipelining) {
1817 event_del(&client->write_frame_event);
1818 event_add(&client->read_frame_event, NULL);
1819 }
1820 return;
1821
1822 close:
1823 release_client(client);
1824}
1825
1826static void
1827accept_cb(int listener, short event, void *arg)
1828{
1829 struct worker *worker;
1830 struct client *client;
1831 int fd;
1832
1833 worker = &workers[clicount++ % num_workers];
1834
1835 if ((fd = accept(listener, NULL, NULL)) < 0) {
1836 if (errno != EAGAIN && errno != EWOULDBLOCK)
1837 LOG(worker, "Failed to accept client connection : %m");
1838 return;
1839 }
1840
1841 DEBUG(&null_worker,
1842 "<%lu> New Client connection accepted and assigned to worker %02d",
1843 clicount, worker->id);
1844
1845 if (evutil_make_socket_nonblocking(fd) < 0) {
1846 LOG(&null_worker, "Failed to set client socket to non-blocking : %m");
1847 close(fd);
1848 return;
1849 }
1850
1851 if ((client = calloc(1, sizeof(*client))) == NULL) {
1852 LOG(&null_worker, "Failed to allocate memory for client state : %m");
1853 close(fd);
1854 return;
1855 }
1856
1857 client->id = clicount;
1858 client->fd = fd;
1859 client->worker = worker;
1860 client->state = SPOA_ST_CONNECTING;
1861 client->status_code = SPOE_FRM_ERR_NONE;
1862 client->max_frame_size = max_frame_size;
1863 client->engine = NULL;
1864 client->pipelining = false;
1865 client->async = false;
1866 client->incoming_frame = NULL;
1867 client->outgoing_frame = NULL;
1868 LIST_INIT(&client->processing_frames);
1869 LIST_INIT(&client->outgoing_frames);
1870
1871 LIST_ADDQ(&worker->clients, &client->by_worker);
1872
1873 worker->nbclients++;
1874
1875 if (event_assign(&client->read_frame_event, worker->base, fd,
1876 EV_READ|EV_PERSIST, read_frame_cb, client) < 0 ||
1877 event_assign(&client->write_frame_event, worker->base, fd,
1878 EV_WRITE|EV_PERSIST, write_frame_cb, client) < 0) {
1879 LOG(&null_worker, "Failed to create client events");
1880 release_client(client);
1881 return;
1882 }
1883 event_add(&client->read_frame_event, NULL);
1884}
1885
1886static void *
1887worker_function(void *data)
1888{
1889 struct client *client, *cback;
1890 struct spoe_frame *frame, *fback;
1891 struct worker *worker = data;
1892
1893 DEBUG(worker, "Worker ready to process client messages");
1894 event_base_dispatch(worker->base);
Christopher Faulet010fded2016-11-03 22:49:37 +01001895
Christopher Fauletf95b1112016-12-21 08:58:16 +01001896 list_for_each_entry_safe(client, cback, &worker->clients, by_worker) {
1897 release_client(client);
Christopher Faulet010fded2016-11-03 22:49:37 +01001898 }
1899
Christopher Fauletf95b1112016-12-21 08:58:16 +01001900 list_for_each_entry_safe(frame, fback, &worker->frames, list) {
1901 LIST_DEL(&frame->list);
1902 free(frame);
1903 }
1904
1905 event_free(worker->monitor_event);
1906 event_base_free(worker->base);
1907 DEBUG(worker, "Worker is stopped");
1908 pthread_exit(&null_worker);
1909}
1910
1911
1912static int
1913parse_processing_delay(const char *str)
1914{
1915 unsigned long value;
1916
1917 value = 0;
1918 while (1) {
1919 unsigned int j;
1920
1921 j = *str - '0';
1922 if (j > 9)
1923 break;
1924 str++;
1925 value *= 10;
1926 value += j;
1927 }
1928
1929 switch (*str) {
1930 case '\0': /* no unit = millisecond */
1931 value *= 1000;
1932 break;
1933 case 's': /* second */
1934 value *= 1000000;
1935 str++;
1936 break;
1937 case 'm': /* millisecond : "ms" */
1938 if (str[1] != 's')
1939 return -1;
1940 value *= 1000;
1941 str += 2;
1942 break;
1943 case 'u': /* microsecond : "us" */
1944 if (str[1] != 's')
1945 return -1;
1946 str += 2;
1947 break;
1948 default:
1949 return -1;
1950 }
1951 if (*str)
1952 return -1;
1953
1954 processing_delay.tv_sec = (time_t)(value / 1000000);
1955 processing_delay.tv_usec = (suseconds_t)(value % 1000000);
1956 return 0;
Christopher Faulet010fded2016-11-03 22:49:37 +01001957}
1958
Christopher Fauletf95b1112016-12-21 08:58:16 +01001959
Christopher Faulet010fded2016-11-03 22:49:37 +01001960static void
1961usage(char *prog)
1962{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001963 fprintf(stderr,
1964 "Usage : %s [OPTION]...\n"
1965 " -h Print this message\n"
1966 " -d Enable the debug mode\n"
1967 " -m <max-frame-size> Specify the maximum frame size (default : %u)\n"
1968 " -p <port> Specify the port to listen on (default : %d)\n"
1969 " -n <num-workers> Specify the number of workers (default : %d)\n"
1970 " -c <capability> Enable the support of the specified capability\n"
1971 " -t <time> Set a delay to process a message (default: 0)\n"
1972 " The value is specified in milliseconds by default,\n"
1973 " but can be in any other unit if the number is suffixed\n"
1974 " by a unit (us, ms, s)\n"
1975 "\n"
Christopher Faulet85010352017-02-02 10:14:36 +01001976 " Supported capabilities: fragmentation, pipelining, async\n",
Christopher Fauletf95b1112016-12-21 08:58:16 +01001977 prog, MAX_FRAME_SIZE, DEFAULT_PORT, NUM_WORKERS);
Christopher Faulet010fded2016-11-03 22:49:37 +01001978}
1979
1980int
1981main(int argc, char **argv)
1982{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001983 struct event_base *base = NULL;
1984 struct event *signal_event = NULL, *accept_event = NULL;
1985 int opt, i, fd = -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001986
Christopher Fauletf95b1112016-12-21 08:58:16 +01001987 // TODO: add '-t <processing-time>' option
1988 while ((opt = getopt(argc, argv, "hdm:n:p:c:t:")) != -1) {
Christopher Faulet010fded2016-11-03 22:49:37 +01001989 switch (opt) {
1990 case 'h':
1991 usage(argv[0]);
1992 return EXIT_SUCCESS;
1993 case 'd':
1994 debug = true;
1995 break;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001996 case 'm':
1997 max_frame_size = atoi(optarg);
1998 break;
Christopher Faulet010fded2016-11-03 22:49:37 +01001999 case 'n':
Christopher Fauletf95b1112016-12-21 08:58:16 +01002000 num_workers = atoi(optarg);
Christopher Faulet010fded2016-11-03 22:49:37 +01002001 break;
2002 case 'p':
Christopher Fauletf95b1112016-12-21 08:58:16 +01002003 server_port = atoi(optarg);
Christopher Faulet010fded2016-11-03 22:49:37 +01002004 break;
Christopher Fauletf95b1112016-12-21 08:58:16 +01002005 case 'c':
2006 if (!strcmp(optarg, "pipelining"))
2007 pipelining = true;
2008 else if (!strcmp(optarg, "async"))
2009 async = true;
Christopher Faulet85010352017-02-02 10:14:36 +01002010 else if (!strcmp(optarg, "fragmentation"))
2011 fragmentation = true;
Christopher Fauletf95b1112016-12-21 08:58:16 +01002012 else
2013 fprintf(stderr, "WARNING: unsupported capability '%s'\n", optarg);
2014 break;
2015 case 't':
2016 if (!parse_processing_delay(optarg))
2017 break;
2018 fprintf(stderr, "%s: failed to parse time '%s'.\n", argv[0], optarg);
2019 fprintf(stderr, "Try '%s -h' for more information.\n", argv[0]);
2020 return EXIT_FAILURE;
Christopher Faulet010fded2016-11-03 22:49:37 +01002021 default:
2022 usage(argv[0]);
2023 return EXIT_FAILURE;
2024 }
2025 }
2026
Christopher Fauletf95b1112016-12-21 08:58:16 +01002027 if (num_workers <= 0) {
2028 LOG(&null_worker, "%s : Invalid number of workers '%d'\n",
2029 argv[0], num_workers);
Christopher Faulet010fded2016-11-03 22:49:37 +01002030 goto error;
2031 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01002032
2033 if (server_port <= 0) {
2034 LOG(&null_worker, "%s : Invalid port '%d'\n",
2035 argv[0], server_port);
Christopher Faulet010fded2016-11-03 22:49:37 +01002036 goto error;
2037 }
2038
Christopher Fauletf95b1112016-12-21 08:58:16 +01002039
2040 if (evthread_use_pthreads() < 0) {
2041 LOG(&null_worker, "No pthreads support for libevent");
Christopher Faulet010fded2016-11-03 22:49:37 +01002042 goto error;
2043 }
2044
Christopher Fauletf95b1112016-12-21 08:58:16 +01002045 if ((base = event_base_new()) == NULL) {
2046 LOG(&null_worker, "Failed to initialize libevent : %m");
2047 goto error;
2048 }
Christopher Faulet010fded2016-11-03 22:49:37 +01002049
Christopher Fauletf95b1112016-12-21 08:58:16 +01002050 signal(SIGPIPE, SIG_IGN);
Christopher Faulet010fded2016-11-03 22:49:37 +01002051
Christopher Fauletf95b1112016-12-21 08:58:16 +01002052 if ((fd = create_server_socket()) < 0) {
2053 LOG(&null_worker, "Failed to create server socket");
2054 goto error;
2055 }
2056 if (evutil_make_socket_nonblocking(fd) < 0) {
2057 LOG(&null_worker, "Failed to set server socket to non-blocking");
Christopher Faulet010fded2016-11-03 22:49:37 +01002058 goto error;
2059 }
2060
Christopher Fauletf95b1112016-12-21 08:58:16 +01002061 if ((workers = calloc(num_workers, sizeof(*workers))) == NULL) {
2062 LOG(&null_worker, "Failed to set allocate memory for workers");
Christopher Faulet010fded2016-11-03 22:49:37 +01002063 goto error;
2064 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01002065
2066 for (i = 0; i < num_workers; ++i) {
2067 struct worker *w = &workers[i];
Christopher Faulet010fded2016-11-03 22:49:37 +01002068
Christopher Fauletf95b1112016-12-21 08:58:16 +01002069 w->id = i+1;
2070 w->nbclients = 0;
2071 LIST_INIT(&w->engines);
2072 LIST_INIT(&w->clients);
2073 LIST_INIT(&w->frames);
2074
2075 if ((w->base = event_base_new()) == NULL) {
2076 LOG(&null_worker,
2077 "Failed to initialize libevent for worker %02d : %m",
2078 w->id);
2079 goto error;
2080 }
Christopher Faulet010fded2016-11-03 22:49:37 +01002081
Christopher Fauletf95b1112016-12-21 08:58:16 +01002082 w->monitor_event = event_new(w->base, fd, EV_PERSIST,
2083 worker_monitor_cb, (void *)w);
2084 if (w->monitor_event == NULL ||
2085 event_add(w->monitor_event, (struct timeval[]){{5,0}}) < 0) {
2086 LOG(&null_worker,
2087 "Failed to create monitor event for worker %02d",
2088 w->id);
Christopher Faulet010fded2016-11-03 22:49:37 +01002089 goto error;
2090 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01002091
2092 if (pthread_create(&w->thread, NULL, worker_function, (void *)w)) {
2093 LOG(&null_worker,
2094 "Failed to start thread for worker %02d : %m",
2095 w->id);
2096 }
2097 DEBUG(&null_worker, "Worker %02d initialized", w->id);
2098 }
2099
2100 accept_event = event_new(base, fd, EV_READ|EV_PERSIST, accept_cb,
2101 (void *)base);
2102 if (accept_event == NULL || event_add(accept_event, NULL) < 0) {
2103 LOG(&null_worker, "Failed to create accept event : %m");
2104 }
2105
2106 signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
2107 if (signal_event == NULL || event_add(signal_event, NULL) < 0) {
2108 LOG(&null_worker, "Failed to create signal event : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01002109 }
2110
Christopher Fauletf95b1112016-12-21 08:58:16 +01002111 DEBUG(&null_worker,
2112 "Server is ready"
Christopher Faulet85010352017-02-02 10:14:36 +01002113 " [fragmentation=%s - pipelining=%s - async=%s - debug=%s - max-frame-size=%u]",
2114 (fragmentation?"true":"false"), (pipelining?"true":"false"), (async?"true":"false"),
Christopher Fauletf95b1112016-12-21 08:58:16 +01002115 (debug?"true":"false"), max_frame_size);
2116 event_base_dispatch(base);
2117
2118 for (i = 0; i < num_workers; i++) {
2119 struct worker *w = &workers[i];
2120
2121 pthread_join(w->thread, NULL);
2122 DEBUG(&null_worker, "Worker %02d terminated", w->id);
Christopher Faulet010fded2016-11-03 22:49:37 +01002123 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01002124
2125 free(workers);
2126 event_free(signal_event);
2127 event_free(accept_event);
2128 event_base_free(base);
2129 close(fd);
Christopher Faulet010fded2016-11-03 22:49:37 +01002130 return EXIT_SUCCESS;
Christopher Fauletf95b1112016-12-21 08:58:16 +01002131
2132 error:
2133 if (workers != NULL)
2134 free(workers);
2135 if (signal_event != NULL)
2136 event_free(signal_event);
2137 if (accept_event != NULL)
2138 event_free(accept_event);
2139 if (base != NULL)
2140 event_base_free(base);
2141 if (fd != -1)
2142 close(fd);
Christopher Faulet010fded2016-11-03 22:49:37 +01002143 return EXIT_FAILURE;
2144}