blob: 3a827bc108a53a82be2b47cc78b7b5315e33c43b [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>
Christopher Faulet1f40b912017-02-17 09:32:19 +010037#include <common/chunk.h>
38
39#include <types/spoe.h>
Christopher Fauletf95b1112016-12-21 08:58:16 +010040
41#define DEFAULT_PORT 12345
42#define CONNECTION_BACKLOG 10
43#define NUM_WORKERS 10
44#define MAX_FRAME_SIZE 16384
45#define SPOP_VERSION "1.0"
Christopher Faulet010fded2016-11-03 22:49:37 +010046
47#define SLEN(str) (sizeof(str)-1)
48
Christopher Fauletf95b1112016-12-21 08:58:16 +010049#define LOG(worker, fmt, args...) \
50 do { \
51 struct timeval now; \
52 \
53 gettimeofday(&now, NULL); \
54 fprintf(stderr, "%ld.%06ld [%02d] " fmt "\n", \
55 now.tv_sec, now.tv_usec, (worker)->id, ##args); \
Christopher Faulet010fded2016-11-03 22:49:37 +010056 } while (0)
57
Christopher Fauletf95b1112016-12-21 08:58:16 +010058#define DEBUG(x...) \
Christopher Faulet010fded2016-11-03 22:49:37 +010059 do { \
60 if (debug) \
61 LOG(x); \
62 } while (0)
63
Christopher Faulet010fded2016-11-03 22:49:37 +010064
Christopher Fauletf95b1112016-12-21 08:58:16 +010065enum spoa_state {
66 SPOA_ST_CONNECTING = 0,
67 SPOA_ST_PROCESSING,
68 SPOA_ST_DISCONNECTING,
69};
70
71enum spoa_frame_type {
72 SPOA_FRM_T_UNKNOWN = 0,
73 SPOA_FRM_T_HAPROXY,
74 SPOA_FRM_T_AGENT,
75};
Christopher Faulet010fded2016-11-03 22:49:37 +010076
Christopher Fauletf95b1112016-12-21 08:58:16 +010077struct spoe_engine {
78 char *id;
79
80 struct list processing_frames;
81 struct list outgoing_frames;
82
83 struct list clients;
84 struct list list;
85};
86
87struct spoe_frame {
88 enum spoa_frame_type type;
89 char *buf;
90 unsigned int offset;
91 unsigned int len;
92
93 unsigned int stream_id;
94 unsigned int frame_id;
Christopher Faulet85010352017-02-02 10:14:36 +010095 unsigned int flags;
96 bool hcheck; /* true is the CONNECT frame is a healthcheck */
97 bool fragmented; /* true if the frame is fragmented */
98 int ip_score; /* -1 if unset, else between 0 and 100 */
Christopher Fauletf95b1112016-12-21 08:58:16 +010099
100 struct event process_frame_event;
101 struct worker *worker;
102 struct spoe_engine *engine;
103 struct client *client;
104 struct list list;
105
Christopher Faulet85010352017-02-02 10:14:36 +0100106 char *frag_buf; /* used to accumulate payload of a fragmented frame */
107 unsigned int frag_len;
108
Christopher Fauletf95b1112016-12-21 08:58:16 +0100109 char data[0];
110};
111
112struct client {
113 int fd;
114 unsigned long id;
115 enum spoa_state state;
116
117 struct event read_frame_event;
118 struct event write_frame_event;
119
120 struct spoe_frame *incoming_frame;
121 struct spoe_frame *outgoing_frame;
122
123 struct list processing_frames;
124 struct list outgoing_frames;
125
126 unsigned int max_frame_size;
127 int status_code;
128
129 char *engine_id;
130 struct spoe_engine *engine;
131 bool pipelining;
132 bool async;
Christopher Faulet85010352017-02-02 10:14:36 +0100133 bool fragmentation;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100134
135 struct worker *worker;
136 struct list by_worker;
137 struct list by_engine;
Christopher Faulet010fded2016-11-03 22:49:37 +0100138};
139
140struct worker {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100141 pthread_t thread;
142 int id;
143 struct event_base *base;
144 struct event *monitor_event;
145
146 struct list engines;
147
148 unsigned int nbclients;
149 struct list clients;
150
151 struct list frames;
Christopher Faulet010fded2016-11-03 22:49:37 +0100152};
153
Christopher Fauletf95b1112016-12-21 08:58:16 +0100154
Christopher Faulet010fded2016-11-03 22:49:37 +0100155union spoe_value {
156 bool boolean; /* use for boolean */
157 int32_t sint32; /* used for signed 32bits integers */
158 uint32_t uint32; /* used for signed 32bits integers */
159 int32_t sint64; /* used for signed 64bits integers */
160 uint32_t uint64; /* used for signed 64bits integers */
161 struct in_addr ipv4; /* used for ipv4 addresses */
162 struct in6_addr ipv6; /* used for ipv6 addresses */
163 struct chunk buffer; /* used for char strings or buffers */
164};
165
166/* Used to store sample constant */
167struct spoe_data {
168 enum spoe_data_type type; /* SPOE_DATA_T_* */
169 union spoe_value u; /* spoe data value */
170};
171
Christopher Fauletf95b1112016-12-21 08:58:16 +0100172/* Globals */
173static struct worker *workers = NULL;
174static struct worker null_worker = { .id = 0 };
175static unsigned long clicount = 0;
176static int server_port = DEFAULT_PORT;
177static int num_workers = NUM_WORKERS;
178static unsigned int max_frame_size = MAX_FRAME_SIZE;
179struct timeval processing_delay = {0, 0};
180static bool debug = false;
181static bool pipelining = false;
182static bool async = false;
Christopher Faulet85010352017-02-02 10:14:36 +0100183static bool fragmentation = false;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100184
185
186static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Faulet85010352017-02-02 10:14:36 +0100187 [SPOE_FRM_ERR_NONE] = "normal",
188 [SPOE_FRM_ERR_IO] = "I/O error",
189 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
190 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
191 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
192 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
193 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
194 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
195 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
196 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
197 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
198 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100199 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Faulet85010352017-02-02 10:14:36 +0100200 [SPOE_FRM_ERR_RES] = "resource allocation error",
201 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf95b1112016-12-21 08:58:16 +0100202};
203
204static void signal_cb(evutil_socket_t, short, void *);
205static void accept_cb(evutil_socket_t, short, void *);
206static void worker_monitor_cb(evutil_socket_t, short, void *);
207static void process_frame_cb(evutil_socket_t, short, void *);
208static void read_frame_cb(evutil_socket_t, short, void *);
209static void write_frame_cb(evutil_socket_t, short, void *);
210
211static void use_spoe_engine(struct client *);
212static void unuse_spoe_engine(struct client *);
213static void release_frame(struct spoe_frame *);
214static void release_client(struct client *);
Christopher Faulet010fded2016-11-03 22:49:37 +0100215
216static void
Christopher Fauletf95b1112016-12-21 08:58:16 +0100217check_ipv4_reputation(struct spoe_frame *frame, struct in_addr *ipv4)
Christopher Faulet010fded2016-11-03 22:49:37 +0100218{
219 char str[INET_ADDRSTRLEN];
220
221 if (inet_ntop(AF_INET, ipv4, str, INET_ADDRSTRLEN) == NULL)
222 return;
223
Christopher Fauletf95b1112016-12-21 08:58:16 +0100224 frame->ip_score = random() % 100;
Christopher Faulet010fded2016-11-03 22:49:37 +0100225
Christopher Fauletf95b1112016-12-21 08:58:16 +0100226 DEBUG(frame->worker, "IP score for %.*s is %d",
227 INET_ADDRSTRLEN, str, frame->ip_score);
Christopher Faulet010fded2016-11-03 22:49:37 +0100228}
229
230static void
Christopher Fauletf95b1112016-12-21 08:58:16 +0100231check_ipv6_reputation(struct spoe_frame *frame, struct in6_addr *ipv6)
Christopher Faulet010fded2016-11-03 22:49:37 +0100232{
233 char str[INET6_ADDRSTRLEN];
234
235 if (inet_ntop(AF_INET6, ipv6, str, INET6_ADDRSTRLEN) == NULL)
236 return;
237
Christopher Fauletf95b1112016-12-21 08:58:16 +0100238 frame->ip_score = random() % 100;
Christopher Faulet010fded2016-11-03 22:49:37 +0100239
Christopher Fauletf95b1112016-12-21 08:58:16 +0100240 DEBUG(frame->worker, "IP score for %.*s is %d",
241 INET6_ADDRSTRLEN, str, frame->ip_score);
Christopher Faulet010fded2016-11-03 22:49:37 +0100242}
243
Christopher Faulet010fded2016-11-03 22:49:37 +0100244
245/* Encode a variable-length integer. This function never fails and returns the
246 * number of written bytes. */
247static int
248encode_spoe_varint(uint64_t i, char *buf)
249{
250 int idx;
251
252 if (i < 240) {
253 buf[0] = (unsigned char)i;
254 return 1;
255 }
256
257 buf[0] = (unsigned char)i | 240;
258 i = (i - 240) >> 4;
259 for (idx = 1; i >= 128; ++idx) {
260 buf[idx] = (unsigned char)i | 128;
261 i = (i - 128) >> 7;
262 }
263 buf[idx++] = (unsigned char)i;
264 return idx;
265}
266
267/* Decode a varable-length integer. If the decoding fails, -1 is returned. This
268 * happens when the buffer's end in reached. On success, the number of read
269 * bytes is returned. */
270static int
271decode_spoe_varint(char *buf, char *end, uint64_t *i)
272{
273 unsigned char *msg = (unsigned char *)buf;
274 int idx = 0;
275
276 if (msg > (unsigned char *)end)
277 return -1;
278
279 if (msg[0] < 240) {
280 *i = msg[0];
281 return 1;
282 }
283 *i = msg[0];
284 do {
285 ++idx;
286 if (msg+idx > (unsigned char *)end)
287 return -1;
288 *i += (uint64_t)msg[idx] << (4 + 7 * (idx-1));
289 } while (msg[idx] >= 128);
290 return (idx + 1);
291}
292
293/* Encode a string. The string will be prefix by its length, encoded as a
294 * variable-length integer. This function never fails and returns the number of
295 * written bytes. */
296static int
297encode_spoe_string(const char *str, size_t len, char *dst)
298{
299 int idx = 0;
300
301 if (!len) {
302 dst[0] = 0;
303 return 1;
304 }
305
306 idx += encode_spoe_varint(len, dst);
307 memcpy(dst+idx, str, len);
308 return (idx + len);
309}
310
311/* Decode a string. Its length is decoded first as a variable-length integer. If
312 * it succeeds, and if the string length is valid, the begin of the string is
313 * saved in <*str>, its length is saved in <*len> and the total numbre of bytes
314 * read is returned. If an error occurred, -1 is returned and <*str> remains
315 * NULL. */
316static int
317decode_spoe_string(char *buf, char *end, char **str, uint64_t *len)
318{
319 int r, idx = 0;
320
321 *str = NULL;
322 *len = 0;
323
324 if ((r = decode_spoe_varint(buf, end, len)) == -1)
325 goto error;
326 idx += r;
327 if (buf + idx + *len > end)
328 goto error;
329
330 *str = buf+idx;
331 return (idx + *len);
332
Christopher Fauletf95b1112016-12-21 08:58:16 +0100333 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100334 return -1;
335}
336
337/* Skip a typed data. If an error occurred, -1 is returned, otherwise the number
338 * of bytes read is returned. A types data is composed of a type (1 byte) and
339 * corresponding data:
340 * - boolean: non additional data (0 bytes)
341 * - integers: a variable-length integer (see decode_spoe_varint)
342 * - ipv4: 4 bytes
343 * - ipv6: 16 bytes
344 * - binary and string: a buffer prefixed by its size, a variable-length
345 * integer (see decode_spoe_string) */
346static int
347skip_spoe_data(char *frame, char *end)
348{
349 uint64_t sz = 0;
350 int r, idx = 0;
351
352 if (frame > end)
353 return -1;
354
355 switch (frame[idx++] & SPOE_DATA_T_MASK) {
356 case SPOE_DATA_T_BOOL:
357 idx++;
358 break;
359 case SPOE_DATA_T_INT32:
360 case SPOE_DATA_T_INT64:
361 case SPOE_DATA_T_UINT32:
362 case SPOE_DATA_T_UINT64:
363 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
364 return -1;
365 idx += r;
366 break;
367 case SPOE_DATA_T_IPV4:
368 idx += 4;
369 break;
370 case SPOE_DATA_T_IPV6:
371 idx += 16;
372 break;
373 case SPOE_DATA_T_STR:
374 case SPOE_DATA_T_BIN:
375 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
376 return -1;
377 idx += r + sz;
378 break;
379 }
380
381 if (frame+idx > end)
382 return -1;
383 return idx;
384}
385
386/* Decode a typed data. If an error occurred, -1 is returned, otherwise the
387 * number of read bytes is returned. See skip_spoe_data for details. */
388static int
389decode_spoe_data(char *frame, char *end, struct spoe_data *data)
390{
391 uint64_t sz = 0;
392 int type, r, idx = 0;
393
394 if (frame > end)
395 return -1;
396
397 type = frame[idx++];
398 data->type = (type & SPOE_DATA_T_MASK);
399 switch (data->type) {
400 case SPOE_DATA_T_BOOL:
401 data->u.boolean = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE);
402 break;
403 case SPOE_DATA_T_INT32:
404 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
405 return -1;
406 data->u.sint32 = sz;
407 idx += r;
408 break;
409 case SPOE_DATA_T_INT64:
410 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
411 return -1;
412 data->u.uint32 = sz;
413 idx += r;
414 break;
415 case SPOE_DATA_T_UINT32:
416 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
417 return -1;
418 data->u.sint64 = sz;
419 idx += r;
420 break;
421 case SPOE_DATA_T_UINT64:
422 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
423 return -1;
424 data->u.uint64 = sz;
425 idx += r;
426 break;
427 case SPOE_DATA_T_IPV4:
428 if (frame+idx+4 > end)
429 return -1;
430 memcpy(&data->u.ipv4, frame+idx, 4);
431 idx += 4;
432 break;
433 case SPOE_DATA_T_IPV6:
434 if (frame+idx+16 > end)
435 return -1;
436 memcpy(&data->u.ipv6, frame+idx, 16);
437 idx += 16;
438 break;
439 case SPOE_DATA_T_STR:
440 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
441 return -1;
442 idx += r;
443 if (frame+idx+sz > end)
444 return -1;
445 data->u.buffer.str = frame+idx;
446 data->u.buffer.len = sz;
447 idx += sz;
448 break;
449 case SPOE_DATA_T_BIN:
450 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
451 return -1;
452 idx += r;
453 if (frame+idx+sz > end)
454 return -1;
455 data->u.buffer.str = frame+idx;
456 data->u.buffer.len = sz;
457 idx += sz;
458 break;
459 default:
460 break;
461 }
462
463 if (frame+idx > end)
464 return -1;
465 return idx;
466}
467
468
469/* Check the protocol version. It returns -1 if an error occurred, the number of
470 * read bytes otherwise. */
471static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100472check_proto_version(struct spoe_frame *frame, int idx)
Christopher Faulet010fded2016-11-03 22:49:37 +0100473{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100474 char *str;
475 uint64_t sz;
Christopher Faulet010fded2016-11-03 22:49:37 +0100476
477 /* Get the list of all supported versions by HAProxy */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100478 if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Faulet010fded2016-11-03 22:49:37 +0100479 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100480 idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz);
481 if (str == NULL)
Christopher Faulet010fded2016-11-03 22:49:37 +0100482 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100483
484 DEBUG(frame->worker, "<%lu> Supported versions : %.*s",
485 frame->client->id, (int)sz, str);
Christopher Faulet010fded2016-11-03 22:49:37 +0100486
487 /* TODO: Find the right verion in supported ones */
488
489 return idx;
490}
491
492/* Check max frame size value. It returns -1 if an error occurred, the number of
493 * read bytes otherwise. */
494static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100495check_max_frame_size(struct spoe_frame *frame, int idx)
Christopher Faulet010fded2016-11-03 22:49:37 +0100496{
497 uint64_t sz;
498 int type, i;
499
500 /* Get the max-frame-size value of HAProxy */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100501 type = frame->buf[idx++];
Christopher Faulet010fded2016-11-03 22:49:37 +0100502 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
503 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
504 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
Christopher Fauletf95b1112016-12-21 08:58:16 +0100505 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64)
Christopher Faulet010fded2016-11-03 22:49:37 +0100506 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100507 if ((i = decode_spoe_varint(frame->buf+idx, frame->buf+frame->len, &sz)) == -1)
Christopher Faulet010fded2016-11-03 22:49:37 +0100508 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100509 idx += i;
510
511 /* Keep the lower value */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100512 if (sz < frame->client->max_frame_size)
513 frame->client->max_frame_size = sz;
514
515 DEBUG(frame->worker, "<%lu> HAProxy maximum frame size : %u",
516 frame->client->id, (unsigned int)sz);
Christopher Faulet010fded2016-11-03 22:49:37 +0100517
518 return idx;
519}
520
Christopher Fauletba7bc162016-11-07 21:07:38 +0100521/* Check healthcheck value. It returns -1 if an error occurred, the number of
522 * read bytes otherwise. */
523static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100524check_healthcheck(struct spoe_frame *frame, int idx)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100525{
526 int type;
527
Christopher Fauletf95b1112016-12-21 08:58:16 +0100528 /* Get the "healthcheck" value */
529 type = frame->buf[idx++];
530 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_BOOL)
531 return -1;
532 frame->hcheck = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE);
533
534 DEBUG(frame->worker, "<%lu> HELLO healthcheck : %s",
535 frame->client->id, (frame->hcheck ? "true" : "false"));
536 return idx;
537}
538
539/* Check capabilities value. It returns -1 if an error occurred, the number of
540 * read bytes otherwise. */
541static int
542check_capabilities(struct spoe_frame *frame, int idx)
543{
544 struct client *client = frame->client;
545 char *str;
546 uint64_t sz;
547 int i;
548
549 if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100550 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100551 idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz);
552 if (str == NULL) /* this is not an error */
553 return idx;
554
555 DEBUG(frame->worker, "<%lu> HAProxy capabilities : %.*s",
556 client->id, (int)sz, str);
557
558 i = 0;
559 while (i < sz) {
560 char *delim;
561
562 /* Skip leading spaces */
563 for (; isspace(str[i]) && i < sz; i++);
564
565 if (sz - i >= 10 && !strncmp(str + i, "pipelining", 10)) {
566 i += 10;
567 if (sz == i || isspace(str[i]) || str[i] == ',') {
568 DEBUG(frame->worker,
569 "<%lu> HAProxy supports frame pipelining",
570 client->id);
571 client->pipelining = true;
572 }
573
574 }
575 else if (sz - i >= 5 && !strncmp(str + i, "async", 5)) {
576 i += 5;
577 if (sz == i || isspace(str[i]) || str[i] == ',') {
578 DEBUG(frame->worker,
579 "<%lu> HAProxy supports asynchronous frame",
580 client->id);
581 client->async = true;
582 }
583 }
Christopher Faulet85010352017-02-02 10:14:36 +0100584 else if (sz - i >= 13 && !strncmp(str + i, "fragmentation", 13)) {
585 i += 5;
586 if (sz == i || isspace(str[i]) || str[i] == ',') {
587 DEBUG(frame->worker,
588 "<%lu> HAProxy supports fragmented frame",
589 client->id);
590 client->fragmentation = true;
591 }
592 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100593
594 if (sz == i || (delim = memchr(str + i, ',', sz-i)) == NULL)
595 break;
596 i = (delim - str) + 1;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100597 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100598
599 return idx;
600}
601
602/* Check engine-id value. It returns -1 if an error occurred, the number of
603 * read bytes otherwise. */
604static int
605check_engine_id(struct spoe_frame *frame, int idx)
606{
607 struct client *client = frame->client;
608 char *str;
609 uint64_t sz;
610
611 if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
612 return -1;
613
614 idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz);
615 if (str == NULL) /* this is not an error */
616 return idx;
617
618 if (client->engine != NULL)
619 return idx;
620
621 DEBUG(frame->worker, "<%lu> HAProxy engine id : %.*s",
622 client->id, (int)sz, str);
623
624 client->engine_id = strndup(str, (int)sz);
625 return idx;
626}
627
628/* Check disconnect status code. It returns -1 if an error occurred, the number
629 * of read bytes otherwise. */
630static int
631check_discon_status_code(struct spoe_frame *frame, int idx)
632{
633 uint64_t sz;
634 int type, i;
635
636 /* Get the "status-code" value */
637 type = frame->buf[idx++];
638 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
639 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
640 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
641 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64)
642 return -1;
643 if ((i = decode_spoe_varint(frame->buf+idx, frame->buf+frame->len, &sz)) == -1)
644 return -1;
645 idx += i;
646
647 frame->client->status_code = (unsigned int)sz;
648
649 DEBUG(frame->worker, "<%lu> Disconnect status code : %u",
650 frame->client->id, frame->client->status_code);
651
Christopher Fauletba7bc162016-11-07 21:07:38 +0100652 return idx;
653}
654
Christopher Fauletf95b1112016-12-21 08:58:16 +0100655/* Check the disconnect message. It returns -1 if an error occurred, the number
656 * of read bytes otherwise. */
657static int
658check_discon_message(struct spoe_frame *frame, int idx)
659{
660 char *str;
661 uint64_t sz;
662
663 /* Get the "message" value */
664 if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
665 return -1;
666 idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz);
667 if (str == NULL)
668 return -1;
669
670 DEBUG(frame->worker, "<%lu> Disconnect message : %.*s",
671 frame->client->id, (int)sz, str);
672
673 return idx;
674}
Christopher Fauletba7bc162016-11-07 21:07:38 +0100675
Christopher Faulet010fded2016-11-03 22:49:37 +0100676/* Decode a HELLO frame received from HAProxy. It returns -1 if an error
Christopher Fauletf95b1112016-12-21 08:58:16 +0100677 * occurred, otherwise the number of read bytes. HELLO frame cannot be
678 * ignored and having another frame than a HELLO frame is an error. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100679static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100680handle_hahello(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100681{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100682 struct client *client = frame->client;
683 char *buf = frame->buf;
684 char *end = frame->buf + frame->len;
685 int i, idx = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +0100686
Christopher Fauletf95b1112016-12-21 08:58:16 +0100687 /* Check frame type: we really want a HELLO frame */
688 if (buf[idx++] != SPOE_FRM_T_HAPROXY_HELLO)
689 goto error;
690
691 DEBUG(frame->worker, "<%lu> Decode HAProxy HELLO frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100692
Christopher Faulet85010352017-02-02 10:14:36 +0100693 /* Retrieve flags */
694 memcpy((char *)&(frame->flags), buf+idx, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +0100695 idx += 4;
696
Christopher Faulet85010352017-02-02 10:14:36 +0100697 /* Fragmentation is not supported for HELLO frame */
698 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
699 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
700 goto error;
701 }
702
Christopher Faulet010fded2016-11-03 22:49:37 +0100703 /* stream-id and frame-id must be cleared */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100704 if (buf[idx] != 0 || buf[idx+1] != 0) {
705 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100706 goto error;
707 }
708 idx += 2;
709
710 /* Loop on K/V items */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100711 while (buf+idx < end) {
Christopher Faulet010fded2016-11-03 22:49:37 +0100712 char *str;
713 uint64_t sz;
714
715 /* Decode the item name */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100716 idx += decode_spoe_string(buf+idx, end, &str, &sz);
Christopher Faulet010fded2016-11-03 22:49:37 +0100717 if (str == NULL) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100718 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100719 goto error;
720 }
721
722 /* Check "supported-versions" K/V item */
723 if (!memcmp(str, "supported-versions", sz)) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100724 if ((i = check_proto_version(frame, idx)) == -1) {
725 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100726 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100727 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100728 idx = i;
729 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100730 /* Check "max-frame-size" K/V item */
Christopher Faulet010fded2016-11-03 22:49:37 +0100731 else if (!memcmp(str, "max-frame-size", sz)) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100732 if ((i = check_max_frame_size(frame, idx)) == -1) {
733 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100734 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100735 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100736 idx = i;
737 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100738 /* Check "healthcheck" K/V item */
Christopher Fauletba7bc162016-11-07 21:07:38 +0100739 else if (!memcmp(str, "healthcheck", sz)) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100740 if ((i = check_healthcheck(frame, idx)) == -1) {
741 client->status_code = SPOE_FRM_ERR_INVALID;
742 goto error;
743 }
744 idx = i;
745 }
746 /* Check "capabilities" K/V item */
747 else if (!memcmp(str, "capabilities", sz)) {
748 if ((i = check_capabilities(frame, idx)) == -1) {
749 client->status_code = SPOE_FRM_ERR_INVALID;
750 goto error;
751 }
752 idx = i;
753 }
754 /* Check "engine-id" K/V item */
755 else if (!memcmp(str, "engine-id", sz)) {
756 if ((i = check_engine_id(frame, idx)) == -1) {
757 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100758 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100759 }
Christopher Fauletba7bc162016-11-07 21:07:38 +0100760 idx = i;
761 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100762 else {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100763 DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s",
764 client->id, (int)sz, str);
765
Christopher Faulet010fded2016-11-03 22:49:37 +0100766 /* Silently ignore unknown item */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100767 if ((i = skip_spoe_data(buf+idx, end)) == -1) {
768 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100769 goto error;
770 }
771 idx += i;
772 }
773 }
774
Christopher Fauletf95b1112016-12-21 08:58:16 +0100775 if (async == false || client->engine_id == NULL)
776 client->async = false;
777 if (pipelining == false)
778 client->pipelining = false;
779
780 if (client->async == true)
781 use_spoe_engine(client);
782
Christopher Faulet010fded2016-11-03 22:49:37 +0100783 return idx;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100784
785 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100786 return -1;
787}
788
789/* Decode a DISCONNECT frame received from HAProxy. It returns -1 if an error
Christopher Fauletf95b1112016-12-21 08:58:16 +0100790 * occurred, otherwise the number of read bytes. DISCONNECT frame cannot be
791 * ignored and having another frame than a DISCONNECT frame is an error.*/
Christopher Faulet010fded2016-11-03 22:49:37 +0100792static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100793handle_hadiscon(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100794{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100795 struct client *client = frame->client;
796 char *buf = frame->buf;
797 char *end = frame->buf + frame->len;
798 int i, idx = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +0100799
Christopher Fauletf95b1112016-12-21 08:58:16 +0100800 /* Check frame type: we really want a DISCONNECT frame */
801 if (buf[idx++] != SPOE_FRM_T_HAPROXY_DISCON)
802 goto error;
803
804 DEBUG(frame->worker, "<%lu> Decode HAProxy DISCONNECT frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100805
Christopher Faulet85010352017-02-02 10:14:36 +0100806 /* Retrieve flags */
807 memcpy((char *)&(frame->flags), buf+idx, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +0100808 idx += 4;
809
Christopher Faulet85010352017-02-02 10:14:36 +0100810 /* Fragmentation is not supported for DISCONNECT frame */
811 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
812 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
813 goto error;
814 }
815
Christopher Faulet010fded2016-11-03 22:49:37 +0100816 /* stream-id and frame-id must be cleared */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100817 if (buf[idx] != 0 || buf[idx+1] != 0) {
818 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100819 goto error;
820 }
821 idx += 2;
822
Christopher Fauletf95b1112016-12-21 08:58:16 +0100823 client->status_code = SPOE_FRM_ERR_NONE;
824
Christopher Faulet010fded2016-11-03 22:49:37 +0100825 /* Loop on K/V items */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100826 while (buf+idx < end) {
Christopher Faulet010fded2016-11-03 22:49:37 +0100827 char *str;
828 uint64_t sz;
829
830 /* Decode item key */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100831 idx += decode_spoe_string(buf+idx, end, &str, &sz);
Christopher Faulet010fded2016-11-03 22:49:37 +0100832 if (str == NULL) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100833 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100834 goto error;
835 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100836
837 /* Check "status-code" K/V item */
838 if (!memcmp(str, "status-code", sz)) {
839 if ((i = check_discon_status_code(frame, idx)) == -1) {
840 client->status_code = SPOE_FRM_ERR_INVALID;
841 goto error;
842 }
843 idx = i;
844 }
845 /* Check "message" K/V item */
846 else if (!memcmp(str, "message", sz)) {
847 if ((i = check_discon_message(frame, idx)) == -1) {
848 client->status_code = SPOE_FRM_ERR_INVALID;
849 goto error;
850 }
851 idx = i;
852 }
853 else {
854 DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s",
855 client->id, (int)sz, str);
856
857 /* Silently ignore unknown item */
858 if ((i = skip_spoe_data(buf+idx, end)) == -1) {
859 client->status_code = SPOE_FRM_ERR_INVALID;
860 goto error;
861 }
862 idx += i;
Christopher Faulet010fded2016-11-03 22:49:37 +0100863 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100864 }
865
Christopher Faulet010fded2016-11-03 22:49:37 +0100866 return idx;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100867
868 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100869 return -1;
870}
871
872/* Decode a NOTIFY frame received from HAProxy. It returns -1 if an error
Christopher Faulet85010352017-02-02 10:14:36 +0100873 * occurred, 0 if it must be must be ignored, otherwise the number of read
874 * bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100875static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100876handle_hanotify(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100877{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100878 struct client *client = frame->client;
879 char *buf = frame->buf;
880 char *end = frame->buf + frame->len;
881 uint64_t stream_id, frame_id;
882 int i, idx = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +0100883
884 /* Check frame type */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100885 if (buf[idx++] != SPOE_FRM_T_HAPROXY_NOTIFY)
886 goto ignore;
887
888 DEBUG(frame->worker, "<%lu> Decode HAProxy NOTIFY frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100889
Christopher Faulet85010352017-02-02 10:14:36 +0100890 /* Retrieve flags */
891 memcpy((char *)&(frame->flags), buf+idx, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +0100892 idx += 4;
893
Christopher Faulet85010352017-02-02 10:14:36 +0100894 /* Fragmentation is not supported for DISCONNECT frame */
895 if (!(frame->flags & SPOE_FRM_FL_FIN) && fragmentation == false) {
896 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
897 goto error;
898 }
899
Christopher Faulet010fded2016-11-03 22:49:37 +0100900 /* Read the stream-id */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100901 if ((i = decode_spoe_varint(buf+idx, end, &stream_id)) == -1)
902 goto ignore;
Christopher Faulet010fded2016-11-03 22:49:37 +0100903 idx += i;
904
905 /* Read the frame-id */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100906 if ((i = decode_spoe_varint(buf+idx, end, &frame_id)) == -1)
907 goto ignore;
Christopher Faulet010fded2016-11-03 22:49:37 +0100908 idx += i;
909
Christopher Faulet85010352017-02-02 10:14:36 +0100910 if (frame->fragmented == true) {
911 if (frame->stream_id != (unsigned int)stream_id ||
912 frame->frame_id != (unsigned int)frame_id) {
913 client->status_code = SPOE_FRM_ERR_INTERLACED_FRAMES;
914 goto error;
915 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100916
Christopher Faulet85010352017-02-02 10:14:36 +0100917 if (frame->flags & SPOE_FRM_FL_ABRT) {
918 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
919 " - Abort processing of a fragmented frame"
920 " - frag_len=%u - len=%u - offset=%u",
921 client->id, frame->stream_id, frame->frame_id,
922 frame->frag_len, frame->len, idx);
923 goto ignore;
924 }
925 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
926 " - %s fragment of a fragmented frame received"
927 " - frag_len=%u - len=%u - offset=%u",
928 client->id, frame->stream_id, frame->frame_id,
929 (frame->flags & SPOE_FRM_FL_FIN) ? "last" : "next",
930 frame->frag_len, frame->len, idx);
931 }
932 else {
933 frame->stream_id = (unsigned int)stream_id;
934 frame->frame_id = (unsigned int)frame_id;
Christopher Faulet010fded2016-11-03 22:49:37 +0100935
Christopher Faulet85010352017-02-02 10:14:36 +0100936 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
937 " - %s frame received"
938 " - frag_len=%u - len=%u - offset=%u",
939 client->id, frame->stream_id, frame->frame_id,
940 (frame->flags & SPOE_FRM_FL_FIN) ? "unfragmented" : "fragmented",
941 frame->frag_len, frame->len, idx);
942
943 frame->fragmented = !(frame->flags & SPOE_FRM_FL_FIN);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100944 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100945
Christopher Fauletf95b1112016-12-21 08:58:16 +0100946 frame->offset = idx;
947 return idx;
Christopher Faulet010fded2016-11-03 22:49:37 +0100948
Christopher Fauletf95b1112016-12-21 08:58:16 +0100949 ignore:
Christopher Faulet85010352017-02-02 10:14:36 +0100950 return 0;
951
952 error:
Christopher Fauletf95b1112016-12-21 08:58:16 +0100953 return -1;
954}
Christopher Faulet010fded2016-11-03 22:49:37 +0100955
Christopher Fauletf95b1112016-12-21 08:58:16 +0100956/* Encode a HELLO frame to send it to HAProxy. It returns the number of written
957 * bytes. */
958static int
959prepare_agenthello(struct spoe_frame *frame)
960{
961 struct client *client = frame->client;
962 char *buf = frame->buf;
Christopher Faulet85010352017-02-02 10:14:36 +0100963 char capabilities[64];
964 int n, idx = 0;
965 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet010fded2016-11-03 22:49:37 +0100966
Christopher Fauletf95b1112016-12-21 08:58:16 +0100967 DEBUG(frame->worker, "<%lu> Encode Agent HELLO frame", client->id);
968 frame->type = SPOA_FRM_T_AGENT;
Christopher Faulet010fded2016-11-03 22:49:37 +0100969
970 /* Frame Type */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100971 buf[idx++] = SPOE_FRM_T_AGENT_HELLO;
Christopher Faulet010fded2016-11-03 22:49:37 +0100972
Christopher Faulet85010352017-02-02 10:14:36 +0100973 /* Set flags */
974 memcpy(buf+idx, (char *)&flags, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +0100975 idx += 4;
976
977 /* No stream-id and frame-id for HELLO frames */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100978 buf[idx++] = 0;
979 buf[idx++] = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +0100980
981 /* "version" K/V item */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100982 idx += encode_spoe_string("version", 7, buf+idx);
983 buf[idx++] = SPOE_DATA_T_STR;
984 idx += encode_spoe_string(SPOP_VERSION, SLEN(SPOP_VERSION), buf+idx);
985 DEBUG(frame->worker, "<%lu> Agent version : %s",
986 client->id, SPOP_VERSION);
987
Christopher Faulet010fded2016-11-03 22:49:37 +0100988
989 /* "max-frame-size" K/V item */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100990 idx += encode_spoe_string("max-frame-size", 14, buf+idx);
991 buf[idx++] = SPOE_DATA_T_UINT32;
992 idx += encode_spoe_varint(client->max_frame_size, buf+idx);
993 DEBUG(frame->worker, "<%lu> Agent maximum frame size : %u",
994 client->id, client->max_frame_size);
Christopher Faulet010fded2016-11-03 22:49:37 +0100995
996 /* "capabilities" K/V item */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100997 idx += encode_spoe_string("capabilities", 12, buf+idx);
998 buf[idx++] = SPOE_DATA_T_STR;
Christopher Faulet85010352017-02-02 10:14:36 +0100999
1000 memset(capabilities, 0, sizeof(capabilities));
1001 n = 0;
1002
1003 /* 1. Fragmentation capability ? */
1004 if (fragmentation == true) {
1005 memcpy(capabilities, "fragmentation", 13);
1006 n += 13;
1007 }
1008 /* 2. Pipelining capability ? */
1009 if (client->pipelining == true && n != 0) {
1010 memcpy(capabilities + n, ", pipelining", 12);
1011 n += 12;
1012 }
1013 else if (client->pipelining == true) {
1014 memcpy(capabilities, "pipelining", 10);
1015 n += 10;
1016 }
1017 /* 3. Async capability ? */
1018 if (client->async == true && n != 0) {
1019 memcpy(capabilities + n, ", async", 7);
1020 n += 7;
1021 }
1022 else if (client->async == true) {
1023 memcpy(capabilities, "async", 5);
1024 n += 5;
1025 }
1026 /* 4. Encode capabilities string */
1027 if (n != 0)
1028 idx += encode_spoe_string(capabilities, n, buf+idx);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001029 else
1030 idx += encode_spoe_string(NULL, 0, buf+idx);
Christopher Faulet010fded2016-11-03 22:49:37 +01001031
Christopher Faulet85010352017-02-02 10:14:36 +01001032 DEBUG(frame->worker, "<%lu> Agent capabilities : %.*s",
1033 client->id, n, capabilities);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001034
1035 frame->len = idx;
Christopher Faulet010fded2016-11-03 22:49:37 +01001036 return idx;
1037}
1038
Christopher Fauletf95b1112016-12-21 08:58:16 +01001039/* Encode a DISCONNECT frame to send it to HAProxy. It returns the number of
1040 * written bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +01001041static int
Christopher Fauletf95b1112016-12-21 08:58:16 +01001042prepare_agentdicon(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +01001043{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001044 struct client *client = frame->client;
1045 char *buf = frame->buf;
1046 const char *reason;
1047 int rlen, idx = 0;
Christopher Faulet85010352017-02-02 10:14:36 +01001048 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001049
1050 DEBUG(frame->worker, "<%lu> Encode Agent DISCONNECT frame", client->id);
1051 frame->type = SPOA_FRM_T_AGENT;
1052
1053 if (client->status_code >= SPOE_FRM_ERRS)
1054 client->status_code = SPOE_FRM_ERR_UNKNOWN;
1055 reason = spoe_frm_err_reasons[client->status_code];
1056 rlen = strlen(reason);
Christopher Faulet010fded2016-11-03 22:49:37 +01001057
1058 /* Frame type */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001059 buf[idx++] = SPOE_FRM_T_AGENT_DISCON;
Christopher Faulet010fded2016-11-03 22:49:37 +01001060
Christopher Faulet85010352017-02-02 10:14:36 +01001061 /* Set flags */
1062 memcpy(buf+idx, (char *)&flags, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001063 idx += 4;
1064
Christopher Fauletf95b1112016-12-21 08:58:16 +01001065 /* No stream-id and frame-id for DISCONNECT frames */
1066 buf[idx++] = 0;
1067 buf[idx++] = 0;
1068
1069 /* There are 2 mandatory items: "status-code" and "message" */
1070
1071 /* "status-code" K/V item */
1072 idx += encode_spoe_string("status-code", 11, buf+idx);
1073 buf[idx++] = SPOE_DATA_T_UINT32;
1074 idx += encode_spoe_varint(client->status_code, buf+idx);
1075 DEBUG(frame->worker, "<%lu> Disconnect status code : %u",
1076 client->id, client->status_code);
Christopher Faulet010fded2016-11-03 22:49:37 +01001077
Christopher Fauletf95b1112016-12-21 08:58:16 +01001078 /* "message" K/V item */
1079 idx += encode_spoe_string("message", 7, buf+idx);
1080 buf[idx++] = SPOE_DATA_T_STR;
1081 idx += encode_spoe_string(reason, rlen, buf+idx);
1082 DEBUG(frame->worker, "<%lu> Disconnect message : %s",
1083 client->id, reason);
Christopher Faulet010fded2016-11-03 22:49:37 +01001084
Christopher Fauletf95b1112016-12-21 08:58:16 +01001085 frame->len = idx;
Christopher Faulet010fded2016-11-03 22:49:37 +01001086 return idx;
1087}
1088
Christopher Fauletf95b1112016-12-21 08:58:16 +01001089/* Encode a ACK frame to send it to HAProxy. It returns the number of written
1090 * bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +01001091static int
Christopher Fauletf95b1112016-12-21 08:58:16 +01001092prepare_agentack(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +01001093{
Christopher Faulet85010352017-02-02 10:14:36 +01001094 char *buf = frame->buf;
1095 int idx = 0;
1096 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet010fded2016-11-03 22:49:37 +01001097
Christopher Fauletf95b1112016-12-21 08:58:16 +01001098 /* Be careful here, in async mode, frame->client can be NULL */
1099
1100 DEBUG(frame->worker, "Encode Agent ACK frame");
1101 frame->type = SPOA_FRM_T_AGENT;
Christopher Faulet010fded2016-11-03 22:49:37 +01001102
1103 /* Frame type */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001104 buf[idx++] = SPOE_FRM_T_AGENT_ACK;
Christopher Faulet010fded2016-11-03 22:49:37 +01001105
Christopher Faulet85010352017-02-02 10:14:36 +01001106 /* Set flags */
1107 memcpy(buf+idx, (char *)&flags, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001108 idx += 4;
1109
Christopher Fauletf95b1112016-12-21 08:58:16 +01001110 /* Set stream-id and frame-id for ACK frames */
1111 idx += encode_spoe_varint(frame->stream_id, buf+idx);
1112 idx += encode_spoe_varint(frame->frame_id, buf+idx);
Christopher Faulet010fded2016-11-03 22:49:37 +01001113
Christopher Fauletf95b1112016-12-21 08:58:16 +01001114 DEBUG(frame->worker, "STREAM-ID=%u - FRAME-ID=%u",
1115 frame->stream_id, frame->frame_id);
Christopher Faulet010fded2016-11-03 22:49:37 +01001116
Christopher Fauletf95b1112016-12-21 08:58:16 +01001117 frame->len = idx;
Christopher Faulet010fded2016-11-03 22:49:37 +01001118 return idx;
1119}
1120
1121static int
Christopher Fauletf95b1112016-12-21 08:58:16 +01001122create_server_socket(void)
Christopher Faulet010fded2016-11-03 22:49:37 +01001123{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001124 struct sockaddr_in listen_addr;
1125 int fd, yes = 1;
1126
1127 fd = socket(AF_INET, SOCK_STREAM, 0);
1128 if (fd < 0) {
1129 LOG(&null_worker, "Failed to create service socket : %m");
1130 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001131 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001132
1133 memset(&listen_addr, 0, sizeof(listen_addr));
1134 listen_addr.sin_family = AF_INET;
1135 listen_addr.sin_addr.s_addr = INADDR_ANY;
1136 listen_addr.sin_port = htons(server_port);
1137
1138 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0 ||
1139 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) < 0) {
1140 LOG(&null_worker, "Failed to set option on server socket : %m");
1141 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001142 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001143
1144 if (bind(fd, (struct sockaddr *) &listen_addr, sizeof(listen_addr)) < 0) {
1145 LOG(&null_worker, "Failed to bind server socket : %m");
1146 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001147 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001148
1149 if (listen(fd, CONNECTION_BACKLOG) < 0) {
1150 LOG(&null_worker, "Failed to listen on server socket : %m");
1151 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001152 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001153
1154 return fd;
Christopher Faulet010fded2016-11-03 22:49:37 +01001155}
1156
Christopher Fauletf95b1112016-12-21 08:58:16 +01001157static void
1158release_frame(struct spoe_frame *frame)
1159{
1160 struct worker *worker;
1161
1162 if (frame == NULL)
1163 return;
1164
1165 if (event_pending(&frame->process_frame_event, EV_TIMEOUT, NULL))
1166 event_del(&frame->process_frame_event);
1167
1168 worker = frame->worker;
1169 LIST_DEL(&frame->list);
Christopher Faulet85010352017-02-02 10:14:36 +01001170 if (frame->frag_buf)
1171 free(frame->frag_buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001172 memset(frame, 0, sizeof(*frame)+max_frame_size+4);
1173 LIST_ADDQ(&worker->frames, &frame->list);
1174}
1175
1176static void
1177release_client(struct client *c)
Christopher Faulet010fded2016-11-03 22:49:37 +01001178{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001179 struct spoe_frame *frame, *back;
1180
1181 if (c == NULL)
1182 return;
1183
1184 DEBUG(c->worker, "<%lu> Release client", c->id);
1185
1186 LIST_DEL(&c->by_worker);
1187 c->worker->nbclients--;
1188
1189 unuse_spoe_engine(c);
1190 free(c->engine_id);
1191
1192 if (event_pending(&c->read_frame_event, EV_READ, NULL))
1193 event_del(&c->read_frame_event);
1194 if (event_pending(&c->write_frame_event, EV_WRITE, NULL))
1195 event_del(&c->write_frame_event);
1196
1197 release_frame(c->incoming_frame);
1198 release_frame(c->outgoing_frame);
1199 list_for_each_entry_safe(frame, back, &c->processing_frames, list) {
1200 release_frame(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +01001201 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001202 list_for_each_entry_safe(frame, back, &c->outgoing_frames, list) {
1203 release_frame(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +01001204 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001205
1206 if (c->fd >= 0)
1207 close(c->fd);
1208
1209 free(c);
1210}
1211
1212static void
1213reset_frame(struct spoe_frame *frame)
1214{
1215 if (frame == NULL)
1216 return;
1217
Christopher Faulet85010352017-02-02 10:14:36 +01001218 if (frame->frag_buf)
1219 free(frame->frag_buf);
1220
1221 frame->type = SPOA_FRM_T_UNKNOWN;
1222 frame->buf = (char *)(frame->data);
1223 frame->offset = 0;
1224 frame->len = 0;
1225 frame->stream_id = 0;
1226 frame->frame_id = 0;
1227 frame->flags = 0;
1228 frame->hcheck = false;
1229 frame->fragmented = false;
1230 frame->ip_score = -1;
1231 frame->frag_buf = NULL;
1232 frame->frag_len = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001233 LIST_INIT(&frame->list);
1234}
1235
1236static void
1237use_spoe_engine(struct client *client)
1238{
1239 struct spoe_engine *eng;
1240
1241 if (client->engine_id == NULL)
1242 return;
1243
1244 list_for_each_entry(eng, &client->worker->engines, list) {
1245 if (!strcmp(eng->id, client->engine_id))
1246 goto end;
1247 }
1248
1249 if ((eng = malloc(sizeof(*eng))) == NULL) {
1250 client->async = false;
1251 return;
1252 }
1253
1254 eng->id = strdup(client->engine_id);
1255 LIST_INIT(&eng->clients);
1256 LIST_INIT(&eng->processing_frames);
1257 LIST_INIT(&eng->outgoing_frames);
1258 LIST_ADDQ(&client->worker->engines, &eng->list);
1259 LOG(client->worker, "Add new SPOE engine '%s'", eng->id);
1260
1261 end:
1262 client->engine = eng;
1263 LIST_ADDQ(&eng->clients, &client->by_engine);
1264}
1265
1266static void
1267unuse_spoe_engine(struct client *client)
1268{
1269 struct spoe_engine *eng;
1270 struct spoe_frame *frame, *back;
1271
1272 if (client == NULL || client->engine == NULL)
1273 return;
1274
1275 eng = client->engine;
1276 client->engine = NULL;
1277 LIST_DEL(&client->by_engine);
1278 if (!LIST_ISEMPTY(&eng->clients))
1279 return;
1280
1281 LOG(client->worker, "Remove SPOE engine '%s'", eng->id);
1282 LIST_DEL(&eng->list);
1283
1284 list_for_each_entry_safe(frame, back, &eng->processing_frames, list) {
1285 release_frame(frame);
1286 }
1287 list_for_each_entry_safe(frame, back, &eng->outgoing_frames, list) {
1288 release_frame(frame);
1289 }
1290 free(eng->id);
1291 free(eng);
1292}
1293
1294
1295static struct spoe_frame *
1296acquire_incoming_frame(struct client *client)
1297{
1298 struct spoe_frame *frame;
1299
1300 frame = client->incoming_frame;
1301 if (frame != NULL)
1302 return frame;
1303
1304 if (LIST_ISEMPTY(&client->worker->frames)) {
1305 if ((frame = calloc(1, sizeof(*frame)+max_frame_size+4)) == NULL) {
1306 LOG(client->worker, "Failed to allocate new frame : %m");
1307 return NULL;
1308 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001309 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001310 else {
1311 frame = LIST_NEXT(&client->worker->frames, typeof(frame), list);
1312 LIST_DEL(&frame->list);
Christopher Faulet010fded2016-11-03 22:49:37 +01001313 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001314
1315 reset_frame(frame);
1316 frame->worker = client->worker;
1317 frame->engine = client->engine;
1318 frame->client = client;
1319
1320 if (event_assign(&frame->process_frame_event, client->worker->base, -1,
1321 EV_TIMEOUT|EV_PERSIST, process_frame_cb, frame) < 0) {
1322 LOG(client->worker, "Failed to create frame event");
1323 return NULL;
Christopher Faulet010fded2016-11-03 22:49:37 +01001324 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001325
1326 client->incoming_frame = frame;
1327 return frame;
Christopher Faulet010fded2016-11-03 22:49:37 +01001328}
1329
Christopher Fauletf95b1112016-12-21 08:58:16 +01001330static struct spoe_frame *
1331acquire_outgoing_frame(struct client *client)
Christopher Faulet010fded2016-11-03 22:49:37 +01001332{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001333 struct spoe_engine *engine = client->engine;
1334 struct spoe_frame *frame = NULL;
Christopher Faulet010fded2016-11-03 22:49:37 +01001335
Christopher Fauletf95b1112016-12-21 08:58:16 +01001336 if (client->outgoing_frame != NULL)
1337 frame = client->outgoing_frame;
1338 else if (!LIST_ISEMPTY(&client->outgoing_frames)) {
1339 frame = LIST_NEXT(&client->outgoing_frames, typeof(frame), list);
1340 LIST_DEL(&frame->list);
1341 client->outgoing_frame = frame;
1342 }
1343 else if (engine!= NULL && !LIST_ISEMPTY(&engine->outgoing_frames)) {
1344 frame = LIST_NEXT(&engine->outgoing_frames, typeof(frame), list);
1345 LIST_DEL(&frame->list);
1346 client->outgoing_frame = frame;
1347 }
1348 return frame;
1349}
1350
1351static void
1352write_frame(struct client *client, struct spoe_frame *frame)
1353{
1354 uint32_t netint;
1355
1356 LIST_DEL(&frame->list);
1357
1358 frame->buf = (char *)(frame->data);
1359 frame->offset = 0;
1360 netint = htonl(frame->len);
1361 memcpy(frame->buf, &netint, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001362
Christopher Fauletf95b1112016-12-21 08:58:16 +01001363 if (client != NULL) { /* HELLO or DISCONNECT frames */
1364 event_add(&client->write_frame_event, NULL);
Christopher Faulet010fded2016-11-03 22:49:37 +01001365
Christopher Fauletf95b1112016-12-21 08:58:16 +01001366 /* Try to process the frame as soon as possible, and always
1367 * attach it to the client */
1368 if (client->async || client->pipelining) {
1369 if (client->outgoing_frame == NULL)
1370 client->outgoing_frame = frame;
1371 else
1372 LIST_ADD(&client->outgoing_frames, &frame->list);
1373 }
1374 else {
1375 client->outgoing_frame = frame;
1376 event_del(&client->read_frame_event);
Christopher Faulet010fded2016-11-03 22:49:37 +01001377 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001378 }
1379 else { /* for all other frames */
1380 if (frame->client == NULL) { /* async mode ! */
1381 LIST_ADDQ(&frame->engine->outgoing_frames, &frame->list);
1382 list_for_each_entry(client, &frame->engine->clients, by_engine)
1383 event_add(&client->write_frame_event, NULL);
1384 }
1385 else if (frame->client->pipelining) {
1386 LIST_ADDQ(&frame->client->outgoing_frames, &frame->list);
1387 event_add(&frame->client->write_frame_event, NULL);
1388 }
1389 else {
1390 frame->client->outgoing_frame = frame;
1391 event_add(&frame->client->write_frame_event, NULL);
1392 event_del(&frame->client->read_frame_event);
1393 }
1394 }
1395}
Christopher Faulet010fded2016-11-03 22:49:37 +01001396
Christopher Fauletf95b1112016-12-21 08:58:16 +01001397static void
1398process_incoming_frame(struct spoe_frame *frame)
1399{
1400 struct client *client = frame->client;
Christopher Faulet010fded2016-11-03 22:49:37 +01001401
Christopher Fauletf95b1112016-12-21 08:58:16 +01001402 if (event_add(&frame->process_frame_event, &processing_delay) < 0) {
1403 LOG(client->worker, "Failed to process incoming frame");
1404 release_frame(frame);
1405 return;
1406 }
1407
1408 if (client->async) {
1409 frame->client = NULL;
1410 LIST_ADDQ(&frame->engine->processing_frames, &frame->list);
1411 }
1412 else if (client->pipelining)
1413 LIST_ADDQ(&client->processing_frames, &frame->list);
1414 else
1415 event_del(&client->read_frame_event);
1416}
1417
1418static void
1419signal_cb(evutil_socket_t sig, short events, void *user_data)
1420{
1421 struct event_base *base = user_data;
1422 int i;
1423
1424 DEBUG(&null_worker, "Stopping the server");
1425
1426 event_base_loopbreak(base);
1427 DEBUG(&null_worker, "Main event loop stopped");
1428
1429 for (i = 0; i < num_workers; i++) {
1430 event_base_loopbreak(workers[i].base);
1431 DEBUG(&null_worker, "Event loop stopped for worker %02d",
1432 workers[i].id);
1433 }
1434}
1435
1436static void
1437worker_monitor_cb(evutil_socket_t fd, short events, void *arg)
1438{
1439 struct worker *worker = arg;
1440
1441 LOG(worker, "%u clients connected", worker->nbclients);
1442}
1443
1444static void
1445process_frame_cb(evutil_socket_t fd, short events, void *arg)
1446{
1447 struct spoe_frame *frame = arg;
1448 char *buf = frame->buf;
1449 char *end = frame->buf + frame->len;
1450 int idx = frame->offset;
1451
1452 DEBUG(frame->worker,
Christopher Faulet85010352017-02-02 10:14:36 +01001453 "Process frame messages : STREAM-ID=%u - FRAME-ID=%u - length=%u bytes",
1454 frame->stream_id, frame->frame_id, frame->len - frame->offset);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001455
1456 /* Loop on messages */
1457 while (buf+idx < end) {
1458 char *str;
1459 uint64_t sz;
1460 int nbargs, i;
1461
1462 /* Decode the message name */
1463 idx += decode_spoe_string(buf+idx, end, &str, &sz);
1464 if (str == NULL)
1465 goto stop_processing;
1466
1467 DEBUG(frame->worker, "Process SPOE Message '%.*s'", (int)sz, str);
1468
1469 nbargs = buf[idx++]; /* Get the number of arguments */
1470 frame->offset = idx; /* Save index to handle errors and skip args */
1471 if (!memcmp(str, "check-client-ip", sz)) {
1472 struct spoe_data data;
1473
1474 memset(&data, 0, sizeof(data));
1475
1476 if (nbargs != 1)
1477 goto skip_message;
1478
1479 if ((i = decode_spoe_string(buf+idx, end, &str, &sz)) == -1)
1480 goto stop_processing;
1481 idx += i;
1482
1483 if ((i = decode_spoe_data(buf+idx, end, &data)) == -1)
1484 goto skip_message;
1485 idx += i;
1486
1487 if ((data.type & SPOE_DATA_T_MASK) == SPOE_DATA_T_IPV4)
1488 check_ipv4_reputation(frame, &data.u.ipv4);
1489 else if ((data.type & SPOE_DATA_T_MASK) == SPOE_DATA_T_IPV6)
1490 check_ipv6_reputation(frame, &data.u.ipv6);
1491 }
1492 else {
1493 skip_message:
1494 idx = frame->offset; /* Restore index */
1495
1496 while (nbargs-- > 0) {
1497 /* Silently ignore argument: its name and its value */
1498 if ((i = decode_spoe_string(buf+idx, end, &str, &sz)) == -1)
1499 goto stop_processing;
1500 idx += i;
1501 if ((i = skip_spoe_data(buf+idx, end)) == -1)
1502 goto stop_processing;
1503 idx += i;
1504 }
1505 }
1506 }
1507
1508 stop_processing:
1509 /* Prepare agent ACK frame */
Christopher Faulet85010352017-02-02 10:14:36 +01001510 frame->buf = (char *)(frame->data) + 4;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001511 frame->offset = 0;
Christopher Faulet85010352017-02-02 10:14:36 +01001512 frame->len = 0;
1513 frame->flags = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001514 idx = prepare_agentack(frame);
1515
1516 if (frame->ip_score != -1) {
1517 DEBUG(frame->worker, "Add action : set variable ip_scode=%u",
1518 frame->ip_score);
1519
1520 buf[idx++] = SPOE_ACT_T_SET_VAR; /* Action type */
1521 buf[idx++] = 3; /* Number of args */
1522 buf[idx++] = SPOE_SCOPE_SESS; /* Arg 1: the scope */
1523 idx += encode_spoe_string("ip_score", 8, buf+idx); /* Arg 2: variable name */
1524 buf[idx++] = SPOE_DATA_T_UINT32;
1525 idx += encode_spoe_varint(frame->ip_score, buf+idx); /* Arg 3: variable value */
1526 frame->len = idx;
1527 }
1528 write_frame(NULL, frame);
1529}
1530
1531static void
1532read_frame_cb(evutil_socket_t fd, short events, void *arg)
1533{
1534 struct client *client = arg;
1535 struct spoe_frame *frame;
1536 uint32_t netint;
1537 int n;
1538
1539 DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__);
1540 if ((frame = acquire_incoming_frame(client)) == NULL)
1541 goto close;
1542
1543 frame->type = SPOA_FRM_T_HAPROXY;
1544 if (frame->buf == (char *)(frame->data)) {
1545 /* Read the frame length: frame->buf points on length part (frame->data) */
1546 n = read(client->fd, frame->buf+frame->offset, 4-frame->offset);
1547 if (n <= 0) {
1548 if (n < 0)
1549 LOG(client->worker, "Failed to read frame length : %m");
Christopher Fauletba7bc162016-11-07 21:07:38 +01001550 goto close;
Christopher Faulet010fded2016-11-03 22:49:37 +01001551 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001552 frame->offset += n;
1553 if (frame->offset != 4)
1554 return;
1555 memcpy(&netint, frame->buf, 4);
1556 frame->buf += 4;
1557 frame->offset = 0;
1558 frame->len = ntohl(netint);
1559 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001560
Christopher Fauletf95b1112016-12-21 08:58:16 +01001561 /* Read the frame: frame->buf points on frame part (frame->data+4)*/
1562 n = read(client->fd, frame->buf + frame->offset,
1563 frame->len - frame->offset);
1564 if (n <= 0) {
1565 if (n < 0) {
1566 LOG(client->worker, "Frame to read frame : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001567 goto close;
1568 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001569 return;
1570 }
1571 frame->offset += n;
1572 if (frame->offset != frame->len)
1573 return;
1574 frame->offset = 0;
1575
1576 DEBUG(client->worker, "<%lu> New Frame of %u bytes received",
1577 client->id, frame->len);
1578
1579 switch (client->state) {
1580 case SPOA_ST_CONNECTING:
1581 if (handle_hahello(frame) < 0) {
1582 LOG(client->worker, "Failed to decode HELLO frame");
1583 goto disconnect;
1584 }
1585 prepare_agenthello(frame);
1586 goto write_frame;
1587
1588 case SPOA_ST_PROCESSING:
Christopher Faulet85010352017-02-02 10:14:36 +01001589 if (frame->buf[0] == SPOE_FRM_T_HAPROXY_DISCON) {
Christopher Fauletf95b1112016-12-21 08:58:16 +01001590 client->state = SPOA_ST_DISCONNECTING;
1591 goto disconnecting;
1592 }
Christopher Faulet85010352017-02-02 10:14:36 +01001593 n = handle_hanotify(frame);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001594 if (n < 0) {
Christopher Faulet85010352017-02-02 10:14:36 +01001595 LOG(client->worker, "Failed to decode frame: %s",
1596 spoe_frm_err_reasons[client->status_code]);
1597 goto disconnect;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001598 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001599 else if (n == 0) {
Christopher Faulet85010352017-02-02 10:14:36 +01001600 LOG(client->worker, "Ignore invalid/unknown/aborted frame");
1601 goto ignore_frame;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001602 }
1603 else
1604 goto process_frame;
1605
1606 case SPOA_ST_DISCONNECTING:
1607 disconnecting:
1608 if (handle_hadiscon(frame) < 0) {
1609 LOG(client->worker, "Failed to decode DISCONNECT frame");
1610 goto disconnect;
1611 }
1612 if (client->status_code != SPOE_FRM_ERR_NONE)
1613 LOG(client->worker, "<%lu> Peer closed connection: %s",
1614 client->id, spoe_frm_err_reasons[client->status_code]);
1615 client->status_code = SPOE_FRM_ERR_NONE;
1616 goto disconnect;
1617 }
1618
1619 ignore_frame:
1620 reset_frame(frame);
1621 return;
1622
1623 process_frame:
Christopher Faulet85010352017-02-02 10:14:36 +01001624 if (frame->fragmented == true) {
1625 char *buf;
1626 size_t len = frame->len - frame->offset;
1627
1628 buf = realloc(frame->frag_buf, frame->frag_len + len);
1629 if (buf == NULL) {
1630 client->status_code = SPOE_FRM_ERR_RES;
1631 goto disconnect;
1632 }
1633 memcpy(buf + frame->frag_len, frame->buf + frame->offset, len);
1634 frame->frag_buf = buf;
1635 frame->frag_len += len;
1636
1637 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
1638 /* Wait for next fragments */
1639 frame->buf = (char *)(frame->data);
1640 frame->offset = 0;
1641 frame->len = 0;
1642 frame->flags = 0;
1643 return;
1644 }
1645
1646 frame->buf = frame->frag_buf;
1647 frame->len = frame->frag_len;
1648 frame->offset = 0;
1649 /* fall through */
1650 }
1651
Christopher Fauletf95b1112016-12-21 08:58:16 +01001652 process_incoming_frame(frame);
1653 client->incoming_frame = NULL;
1654 return;
1655
1656 write_frame:
1657 write_frame(client, frame);
1658 client->incoming_frame = NULL;
1659 return;
1660
1661 disconnect:
1662 client->state = SPOA_ST_DISCONNECTING;
1663 if (prepare_agentdicon(frame) < 0) {
1664 LOG(client->worker, "Failed to encode DISCONNECT frame");
1665 goto close;
1666 }
1667 goto write_frame;
1668
1669 close:
1670 release_client(client);
1671}
1672
1673static void
1674write_frame_cb(evutil_socket_t fd, short events, void *arg)
1675{
1676 struct client *client = arg;
1677 struct spoe_frame *frame;
1678 int n;
1679
1680 DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__);
1681 if ((frame = acquire_outgoing_frame(client)) == NULL) {
1682 event_del(&client->write_frame_event);
1683 return;
1684 }
1685
1686 if (frame->buf == (char *)(frame->data)) {
1687 /* Write the frame length: frame->buf points on length part (frame->data) */
1688 n = write(client->fd, frame->buf+frame->offset, 4-frame->offset);
1689 if (n <= 0) {
1690 if (n < 0)
1691 LOG(client->worker, "Failed to write frame length : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001692 goto close;
1693 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001694 frame->offset += n;
1695 if (frame->offset != 4)
1696 return;
1697 frame->buf += 4;
1698 frame->offset = 0;
1699 }
1700
1701 /* Write the frame: frame->buf points on frame part (frame->data+4)*/
1702 n = write(client->fd, frame->buf + frame->offset,
1703 frame->len - frame->offset);
1704 if (n <= 0) {
1705 if (n < 0) {
1706 LOG(client->worker, "Failed to write frame : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001707 goto close;
1708 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001709 return;
1710 }
1711 frame->offset += n;
1712 if (frame->offset != frame->len)
1713 return;
1714
1715 DEBUG(client->worker, "<%lu> Frame of %u bytes send",
1716 client->id, frame->len);
1717
1718 switch (client->state) {
1719 case SPOA_ST_CONNECTING:
1720 if (frame->hcheck == true) {
1721 DEBUG(client->worker,
1722 "<%lu> Close client after healthcheck",
1723 client->id);
1724 goto close;
1725 }
1726 client->state = SPOA_ST_PROCESSING;
1727 break;
1728
1729 case SPOA_ST_PROCESSING:
1730 break;
1731
1732 case SPOA_ST_DISCONNECTING:
1733 goto close;
1734 }
1735
1736 release_frame(frame);
1737 client->outgoing_frame = NULL;
1738 if (!client->async && !client->pipelining) {
1739 event_del(&client->write_frame_event);
1740 event_add(&client->read_frame_event, NULL);
1741 }
1742 return;
1743
1744 close:
1745 release_client(client);
1746}
1747
1748static void
1749accept_cb(int listener, short event, void *arg)
1750{
1751 struct worker *worker;
1752 struct client *client;
1753 int fd;
1754
1755 worker = &workers[clicount++ % num_workers];
1756
1757 if ((fd = accept(listener, NULL, NULL)) < 0) {
1758 if (errno != EAGAIN && errno != EWOULDBLOCK)
1759 LOG(worker, "Failed to accept client connection : %m");
1760 return;
1761 }
1762
1763 DEBUG(&null_worker,
1764 "<%lu> New Client connection accepted and assigned to worker %02d",
1765 clicount, worker->id);
1766
1767 if (evutil_make_socket_nonblocking(fd) < 0) {
1768 LOG(&null_worker, "Failed to set client socket to non-blocking : %m");
1769 close(fd);
1770 return;
1771 }
1772
1773 if ((client = calloc(1, sizeof(*client))) == NULL) {
1774 LOG(&null_worker, "Failed to allocate memory for client state : %m");
1775 close(fd);
1776 return;
1777 }
1778
1779 client->id = clicount;
1780 client->fd = fd;
1781 client->worker = worker;
1782 client->state = SPOA_ST_CONNECTING;
1783 client->status_code = SPOE_FRM_ERR_NONE;
1784 client->max_frame_size = max_frame_size;
1785 client->engine = NULL;
1786 client->pipelining = false;
1787 client->async = false;
1788 client->incoming_frame = NULL;
1789 client->outgoing_frame = NULL;
1790 LIST_INIT(&client->processing_frames);
1791 LIST_INIT(&client->outgoing_frames);
1792
1793 LIST_ADDQ(&worker->clients, &client->by_worker);
1794
1795 worker->nbclients++;
1796
1797 if (event_assign(&client->read_frame_event, worker->base, fd,
1798 EV_READ|EV_PERSIST, read_frame_cb, client) < 0 ||
1799 event_assign(&client->write_frame_event, worker->base, fd,
1800 EV_WRITE|EV_PERSIST, write_frame_cb, client) < 0) {
1801 LOG(&null_worker, "Failed to create client events");
1802 release_client(client);
1803 return;
1804 }
1805 event_add(&client->read_frame_event, NULL);
1806}
1807
1808static void *
1809worker_function(void *data)
1810{
1811 struct client *client, *cback;
1812 struct spoe_frame *frame, *fback;
1813 struct worker *worker = data;
1814
1815 DEBUG(worker, "Worker ready to process client messages");
1816 event_base_dispatch(worker->base);
Christopher Faulet010fded2016-11-03 22:49:37 +01001817
Christopher Fauletf95b1112016-12-21 08:58:16 +01001818 list_for_each_entry_safe(client, cback, &worker->clients, by_worker) {
1819 release_client(client);
Christopher Faulet010fded2016-11-03 22:49:37 +01001820 }
1821
Christopher Fauletf95b1112016-12-21 08:58:16 +01001822 list_for_each_entry_safe(frame, fback, &worker->frames, list) {
1823 LIST_DEL(&frame->list);
1824 free(frame);
1825 }
1826
1827 event_free(worker->monitor_event);
1828 event_base_free(worker->base);
1829 DEBUG(worker, "Worker is stopped");
1830 pthread_exit(&null_worker);
1831}
1832
1833
1834static int
1835parse_processing_delay(const char *str)
1836{
1837 unsigned long value;
1838
1839 value = 0;
1840 while (1) {
1841 unsigned int j;
1842
1843 j = *str - '0';
1844 if (j > 9)
1845 break;
1846 str++;
1847 value *= 10;
1848 value += j;
1849 }
1850
1851 switch (*str) {
1852 case '\0': /* no unit = millisecond */
1853 value *= 1000;
1854 break;
1855 case 's': /* second */
1856 value *= 1000000;
1857 str++;
1858 break;
1859 case 'm': /* millisecond : "ms" */
1860 if (str[1] != 's')
1861 return -1;
1862 value *= 1000;
1863 str += 2;
1864 break;
1865 case 'u': /* microsecond : "us" */
1866 if (str[1] != 's')
1867 return -1;
1868 str += 2;
1869 break;
1870 default:
1871 return -1;
1872 }
1873 if (*str)
1874 return -1;
1875
1876 processing_delay.tv_sec = (time_t)(value / 1000000);
1877 processing_delay.tv_usec = (suseconds_t)(value % 1000000);
1878 return 0;
Christopher Faulet010fded2016-11-03 22:49:37 +01001879}
1880
Christopher Fauletf95b1112016-12-21 08:58:16 +01001881
Christopher Faulet010fded2016-11-03 22:49:37 +01001882static void
1883usage(char *prog)
1884{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001885 fprintf(stderr,
1886 "Usage : %s [OPTION]...\n"
1887 " -h Print this message\n"
1888 " -d Enable the debug mode\n"
1889 " -m <max-frame-size> Specify the maximum frame size (default : %u)\n"
1890 " -p <port> Specify the port to listen on (default : %d)\n"
1891 " -n <num-workers> Specify the number of workers (default : %d)\n"
1892 " -c <capability> Enable the support of the specified capability\n"
1893 " -t <time> Set a delay to process a message (default: 0)\n"
1894 " The value is specified in milliseconds by default,\n"
1895 " but can be in any other unit if the number is suffixed\n"
1896 " by a unit (us, ms, s)\n"
1897 "\n"
Christopher Faulet85010352017-02-02 10:14:36 +01001898 " Supported capabilities: fragmentation, pipelining, async\n",
Christopher Fauletf95b1112016-12-21 08:58:16 +01001899 prog, MAX_FRAME_SIZE, DEFAULT_PORT, NUM_WORKERS);
Christopher Faulet010fded2016-11-03 22:49:37 +01001900}
1901
1902int
1903main(int argc, char **argv)
1904{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001905 struct event_base *base = NULL;
1906 struct event *signal_event = NULL, *accept_event = NULL;
1907 int opt, i, fd = -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001908
Christopher Fauletf95b1112016-12-21 08:58:16 +01001909 // TODO: add '-t <processing-time>' option
1910 while ((opt = getopt(argc, argv, "hdm:n:p:c:t:")) != -1) {
Christopher Faulet010fded2016-11-03 22:49:37 +01001911 switch (opt) {
1912 case 'h':
1913 usage(argv[0]);
1914 return EXIT_SUCCESS;
1915 case 'd':
1916 debug = true;
1917 break;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001918 case 'm':
1919 max_frame_size = atoi(optarg);
1920 break;
Christopher Faulet010fded2016-11-03 22:49:37 +01001921 case 'n':
Christopher Fauletf95b1112016-12-21 08:58:16 +01001922 num_workers = atoi(optarg);
Christopher Faulet010fded2016-11-03 22:49:37 +01001923 break;
1924 case 'p':
Christopher Fauletf95b1112016-12-21 08:58:16 +01001925 server_port = atoi(optarg);
Christopher Faulet010fded2016-11-03 22:49:37 +01001926 break;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001927 case 'c':
1928 if (!strcmp(optarg, "pipelining"))
1929 pipelining = true;
1930 else if (!strcmp(optarg, "async"))
1931 async = true;
Christopher Faulet85010352017-02-02 10:14:36 +01001932 else if (!strcmp(optarg, "fragmentation"))
1933 fragmentation = true;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001934 else
1935 fprintf(stderr, "WARNING: unsupported capability '%s'\n", optarg);
1936 break;
1937 case 't':
1938 if (!parse_processing_delay(optarg))
1939 break;
1940 fprintf(stderr, "%s: failed to parse time '%s'.\n", argv[0], optarg);
1941 fprintf(stderr, "Try '%s -h' for more information.\n", argv[0]);
1942 return EXIT_FAILURE;
Christopher Faulet010fded2016-11-03 22:49:37 +01001943 default:
1944 usage(argv[0]);
1945 return EXIT_FAILURE;
1946 }
1947 }
1948
Christopher Fauletf95b1112016-12-21 08:58:16 +01001949 if (num_workers <= 0) {
1950 LOG(&null_worker, "%s : Invalid number of workers '%d'\n",
1951 argv[0], num_workers);
Christopher Faulet010fded2016-11-03 22:49:37 +01001952 goto error;
1953 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001954
1955 if (server_port <= 0) {
1956 LOG(&null_worker, "%s : Invalid port '%d'\n",
1957 argv[0], server_port);
Christopher Faulet010fded2016-11-03 22:49:37 +01001958 goto error;
1959 }
1960
Christopher Fauletf95b1112016-12-21 08:58:16 +01001961
1962 if (evthread_use_pthreads() < 0) {
1963 LOG(&null_worker, "No pthreads support for libevent");
Christopher Faulet010fded2016-11-03 22:49:37 +01001964 goto error;
1965 }
1966
Christopher Fauletf95b1112016-12-21 08:58:16 +01001967 if ((base = event_base_new()) == NULL) {
1968 LOG(&null_worker, "Failed to initialize libevent : %m");
1969 goto error;
1970 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001971
Christopher Fauletf95b1112016-12-21 08:58:16 +01001972 signal(SIGPIPE, SIG_IGN);
Christopher Faulet010fded2016-11-03 22:49:37 +01001973
Christopher Fauletf95b1112016-12-21 08:58:16 +01001974 if ((fd = create_server_socket()) < 0) {
1975 LOG(&null_worker, "Failed to create server socket");
1976 goto error;
1977 }
1978 if (evutil_make_socket_nonblocking(fd) < 0) {
1979 LOG(&null_worker, "Failed to set server socket to non-blocking");
Christopher Faulet010fded2016-11-03 22:49:37 +01001980 goto error;
1981 }
1982
Christopher Fauletf95b1112016-12-21 08:58:16 +01001983 if ((workers = calloc(num_workers, sizeof(*workers))) == NULL) {
1984 LOG(&null_worker, "Failed to set allocate memory for workers");
Christopher Faulet010fded2016-11-03 22:49:37 +01001985 goto error;
1986 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001987
1988 for (i = 0; i < num_workers; ++i) {
1989 struct worker *w = &workers[i];
Christopher Faulet010fded2016-11-03 22:49:37 +01001990
Christopher Fauletf95b1112016-12-21 08:58:16 +01001991 w->id = i+1;
1992 w->nbclients = 0;
1993 LIST_INIT(&w->engines);
1994 LIST_INIT(&w->clients);
1995 LIST_INIT(&w->frames);
1996
1997 if ((w->base = event_base_new()) == NULL) {
1998 LOG(&null_worker,
1999 "Failed to initialize libevent for worker %02d : %m",
2000 w->id);
2001 goto error;
2002 }
Christopher Faulet010fded2016-11-03 22:49:37 +01002003
Christopher Fauletf95b1112016-12-21 08:58:16 +01002004 w->monitor_event = event_new(w->base, fd, EV_PERSIST,
2005 worker_monitor_cb, (void *)w);
2006 if (w->monitor_event == NULL ||
2007 event_add(w->monitor_event, (struct timeval[]){{5,0}}) < 0) {
2008 LOG(&null_worker,
2009 "Failed to create monitor event for worker %02d",
2010 w->id);
Christopher Faulet010fded2016-11-03 22:49:37 +01002011 goto error;
2012 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01002013
2014 if (pthread_create(&w->thread, NULL, worker_function, (void *)w)) {
2015 LOG(&null_worker,
2016 "Failed to start thread for worker %02d : %m",
2017 w->id);
2018 }
2019 DEBUG(&null_worker, "Worker %02d initialized", w->id);
2020 }
2021
2022 accept_event = event_new(base, fd, EV_READ|EV_PERSIST, accept_cb,
2023 (void *)base);
2024 if (accept_event == NULL || event_add(accept_event, NULL) < 0) {
2025 LOG(&null_worker, "Failed to create accept event : %m");
2026 }
2027
2028 signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
2029 if (signal_event == NULL || event_add(signal_event, NULL) < 0) {
2030 LOG(&null_worker, "Failed to create signal event : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01002031 }
2032
Christopher Fauletf95b1112016-12-21 08:58:16 +01002033 DEBUG(&null_worker,
2034 "Server is ready"
Christopher Faulet85010352017-02-02 10:14:36 +01002035 " [fragmentation=%s - pipelining=%s - async=%s - debug=%s - max-frame-size=%u]",
2036 (fragmentation?"true":"false"), (pipelining?"true":"false"), (async?"true":"false"),
Christopher Fauletf95b1112016-12-21 08:58:16 +01002037 (debug?"true":"false"), max_frame_size);
2038 event_base_dispatch(base);
2039
2040 for (i = 0; i < num_workers; i++) {
2041 struct worker *w = &workers[i];
2042
2043 pthread_join(w->thread, NULL);
2044 DEBUG(&null_worker, "Worker %02d terminated", w->id);
Christopher Faulet010fded2016-11-03 22:49:37 +01002045 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01002046
2047 free(workers);
2048 event_free(signal_event);
2049 event_free(accept_event);
2050 event_base_free(base);
2051 close(fd);
Christopher Faulet010fded2016-11-03 22:49:37 +01002052 return EXIT_SUCCESS;
Christopher Fauletf95b1112016-12-21 08:58:16 +01002053
2054 error:
2055 if (workers != NULL)
2056 free(workers);
2057 if (signal_event != NULL)
2058 event_free(signal_event);
2059 if (accept_event != NULL)
2060 event_free(accept_event);
2061 if (base != NULL)
2062 event_base_free(base);
2063 if (fd != -1)
2064 close(fd);
Christopher Faulet010fded2016-11-03 22:49:37 +01002065 return EXIT_FAILURE;
2066}