blob: 8e234b58fd723ccc8cdb3ac3163d3068ee442658 [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,
103 SPOE_FRM_ERR_RES,
Christopher Faulet010fded2016-11-03 22:49:37 +0100104 SPOE_FRM_ERR_UNKNOWN = 99,
105 SPOE_FRM_ERRS,
106};
107
108/* All supported SPOE actions */
109enum spoe_action_type {
110 SPOE_ACT_T_SET_VAR = 1,
111 SPOE_ACT_T_UNSET_VAR,
112 SPOE_ACT_TYPES,
113};
114
115/* Scopes used for variables set by agents. It is a way to be agnotic to vars
116 * scope. */
117enum spoe_vars_scope {
118 SPOE_SCOPE_PROC = 0, /* <=> SCOPE_PROC */
119 SPOE_SCOPE_SESS, /* <=> SCOPE_SESS */
120 SPOE_SCOPE_TXN, /* <=> SCOPE_TXN */
121 SPOE_SCOPE_REQ, /* <=> SCOPE_REQ */
122 SPOE_SCOPE_RES, /* <=> SCOPE_RES */
123};
124
Christopher Fauletf95b1112016-12-21 08:58:16 +0100125enum spoa_state {
126 SPOA_ST_CONNECTING = 0,
127 SPOA_ST_PROCESSING,
128 SPOA_ST_DISCONNECTING,
129};
130
131enum spoa_frame_type {
132 SPOA_FRM_T_UNKNOWN = 0,
133 SPOA_FRM_T_HAPROXY,
134 SPOA_FRM_T_AGENT,
135};
Christopher Faulet010fded2016-11-03 22:49:37 +0100136
Christopher Faulet85010352017-02-02 10:14:36 +0100137/* Flags set on the SPOE frame */
138#define SPOE_FRM_FL_FIN 0x00000001
139#define SPOE_FRM_FL_ABRT 0x00000002
140
Christopher Faulet010fded2016-11-03 22:49:37 +0100141/* Masks to get data type or flags value */
142#define SPOE_DATA_T_MASK 0x0F
143#define SPOE_DATA_FL_MASK 0xF0
144
145/* Flags to set Boolean values */
146#define SPOE_DATA_FL_FALSE 0x00
147#define SPOE_DATA_FL_TRUE 0x10
Christopher Fauletf95b1112016-12-21 08:58:16 +0100148
149struct spoe_engine {
150 char *id;
151
152 struct list processing_frames;
153 struct list outgoing_frames;
154
155 struct list clients;
156 struct list list;
157};
158
159struct spoe_frame {
160 enum spoa_frame_type type;
161 char *buf;
162 unsigned int offset;
163 unsigned int len;
164
165 unsigned int stream_id;
166 unsigned int frame_id;
Christopher Faulet85010352017-02-02 10:14:36 +0100167 unsigned int flags;
168 bool hcheck; /* true is the CONNECT frame is a healthcheck */
169 bool fragmented; /* true if the frame is fragmented */
170 int ip_score; /* -1 if unset, else between 0 and 100 */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100171
172 struct event process_frame_event;
173 struct worker *worker;
174 struct spoe_engine *engine;
175 struct client *client;
176 struct list list;
177
Christopher Faulet85010352017-02-02 10:14:36 +0100178 char *frag_buf; /* used to accumulate payload of a fragmented frame */
179 unsigned int frag_len;
180
Christopher Fauletf95b1112016-12-21 08:58:16 +0100181 char data[0];
182};
183
184struct client {
185 int fd;
186 unsigned long id;
187 enum spoa_state state;
188
189 struct event read_frame_event;
190 struct event write_frame_event;
191
192 struct spoe_frame *incoming_frame;
193 struct spoe_frame *outgoing_frame;
194
195 struct list processing_frames;
196 struct list outgoing_frames;
197
198 unsigned int max_frame_size;
199 int status_code;
200
201 char *engine_id;
202 struct spoe_engine *engine;
203 bool pipelining;
204 bool async;
Christopher Faulet85010352017-02-02 10:14:36 +0100205 bool fragmentation;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100206
207 struct worker *worker;
208 struct list by_worker;
209 struct list by_engine;
Christopher Faulet010fded2016-11-03 22:49:37 +0100210};
211
212struct worker {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100213 pthread_t thread;
214 int id;
215 struct event_base *base;
216 struct event *monitor_event;
217
218 struct list engines;
219
220 unsigned int nbclients;
221 struct list clients;
222
223 struct list frames;
Christopher Faulet010fded2016-11-03 22:49:37 +0100224};
225
Christopher Fauletf95b1112016-12-21 08:58:16 +0100226
Christopher Faulet010fded2016-11-03 22:49:37 +0100227struct chunk {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100228 char *str; /* beginning of the string itself. Might not be 0-terminated */
229 int len; /* current size of the string from first to last char */
Christopher Faulet010fded2016-11-03 22:49:37 +0100230};
231
232union spoe_value {
233 bool boolean; /* use for boolean */
234 int32_t sint32; /* used for signed 32bits integers */
235 uint32_t uint32; /* used for signed 32bits integers */
236 int32_t sint64; /* used for signed 64bits integers */
237 uint32_t uint64; /* used for signed 64bits integers */
238 struct in_addr ipv4; /* used for ipv4 addresses */
239 struct in6_addr ipv6; /* used for ipv6 addresses */
240 struct chunk buffer; /* used for char strings or buffers */
241};
242
243/* Used to store sample constant */
244struct spoe_data {
245 enum spoe_data_type type; /* SPOE_DATA_T_* */
246 union spoe_value u; /* spoe data value */
247};
248
Christopher Fauletf95b1112016-12-21 08:58:16 +0100249/* Globals */
250static struct worker *workers = NULL;
251static struct worker null_worker = { .id = 0 };
252static unsigned long clicount = 0;
253static int server_port = DEFAULT_PORT;
254static int num_workers = NUM_WORKERS;
255static unsigned int max_frame_size = MAX_FRAME_SIZE;
256struct timeval processing_delay = {0, 0};
257static bool debug = false;
258static bool pipelining = false;
259static bool async = false;
Christopher Faulet85010352017-02-02 10:14:36 +0100260static bool fragmentation = false;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100261
262
263static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Faulet85010352017-02-02 10:14:36 +0100264 [SPOE_FRM_ERR_NONE] = "normal",
265 [SPOE_FRM_ERR_IO] = "I/O error",
266 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
267 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
268 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
269 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
270 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
271 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
272 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
273 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
274 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
275 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
276 [SPOE_FRM_ERR_RES] = "resource allocation error",
277 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf95b1112016-12-21 08:58:16 +0100278};
279
280static void signal_cb(evutil_socket_t, short, void *);
281static void accept_cb(evutil_socket_t, short, void *);
282static void worker_monitor_cb(evutil_socket_t, short, void *);
283static void process_frame_cb(evutil_socket_t, short, void *);
284static void read_frame_cb(evutil_socket_t, short, void *);
285static void write_frame_cb(evutil_socket_t, short, void *);
286
287static void use_spoe_engine(struct client *);
288static void unuse_spoe_engine(struct client *);
289static void release_frame(struct spoe_frame *);
290static void release_client(struct client *);
Christopher Faulet010fded2016-11-03 22:49:37 +0100291
292static void
Christopher Fauletf95b1112016-12-21 08:58:16 +0100293check_ipv4_reputation(struct spoe_frame *frame, struct in_addr *ipv4)
Christopher Faulet010fded2016-11-03 22:49:37 +0100294{
295 char str[INET_ADDRSTRLEN];
296
297 if (inet_ntop(AF_INET, ipv4, str, INET_ADDRSTRLEN) == NULL)
298 return;
299
Christopher Fauletf95b1112016-12-21 08:58:16 +0100300 frame->ip_score = random() % 100;
Christopher Faulet010fded2016-11-03 22:49:37 +0100301
Christopher Fauletf95b1112016-12-21 08:58:16 +0100302 DEBUG(frame->worker, "IP score for %.*s is %d",
303 INET_ADDRSTRLEN, str, frame->ip_score);
Christopher Faulet010fded2016-11-03 22:49:37 +0100304}
305
306static void
Christopher Fauletf95b1112016-12-21 08:58:16 +0100307check_ipv6_reputation(struct spoe_frame *frame, struct in6_addr *ipv6)
Christopher Faulet010fded2016-11-03 22:49:37 +0100308{
309 char str[INET6_ADDRSTRLEN];
310
311 if (inet_ntop(AF_INET6, ipv6, str, INET6_ADDRSTRLEN) == NULL)
312 return;
313
Christopher Fauletf95b1112016-12-21 08:58:16 +0100314 frame->ip_score = random() % 100;
Christopher Faulet010fded2016-11-03 22:49:37 +0100315
Christopher Fauletf95b1112016-12-21 08:58:16 +0100316 DEBUG(frame->worker, "IP score for %.*s is %d",
317 INET6_ADDRSTRLEN, str, frame->ip_score);
Christopher Faulet010fded2016-11-03 22:49:37 +0100318}
319
Christopher Faulet010fded2016-11-03 22:49:37 +0100320
321/* Encode a variable-length integer. This function never fails and returns the
322 * number of written bytes. */
323static int
324encode_spoe_varint(uint64_t i, char *buf)
325{
326 int idx;
327
328 if (i < 240) {
329 buf[0] = (unsigned char)i;
330 return 1;
331 }
332
333 buf[0] = (unsigned char)i | 240;
334 i = (i - 240) >> 4;
335 for (idx = 1; i >= 128; ++idx) {
336 buf[idx] = (unsigned char)i | 128;
337 i = (i - 128) >> 7;
338 }
339 buf[idx++] = (unsigned char)i;
340 return idx;
341}
342
343/* Decode a varable-length integer. If the decoding fails, -1 is returned. This
344 * happens when the buffer's end in reached. On success, the number of read
345 * bytes is returned. */
346static int
347decode_spoe_varint(char *buf, char *end, uint64_t *i)
348{
349 unsigned char *msg = (unsigned char *)buf;
350 int idx = 0;
351
352 if (msg > (unsigned char *)end)
353 return -1;
354
355 if (msg[0] < 240) {
356 *i = msg[0];
357 return 1;
358 }
359 *i = msg[0];
360 do {
361 ++idx;
362 if (msg+idx > (unsigned char *)end)
363 return -1;
364 *i += (uint64_t)msg[idx] << (4 + 7 * (idx-1));
365 } while (msg[idx] >= 128);
366 return (idx + 1);
367}
368
369/* Encode a string. The string will be prefix by its length, encoded as a
370 * variable-length integer. This function never fails and returns the number of
371 * written bytes. */
372static int
373encode_spoe_string(const char *str, size_t len, char *dst)
374{
375 int idx = 0;
376
377 if (!len) {
378 dst[0] = 0;
379 return 1;
380 }
381
382 idx += encode_spoe_varint(len, dst);
383 memcpy(dst+idx, str, len);
384 return (idx + len);
385}
386
387/* Decode a string. Its length is decoded first as a variable-length integer. If
388 * it succeeds, and if the string length is valid, the begin of the string is
389 * saved in <*str>, its length is saved in <*len> and the total numbre of bytes
390 * read is returned. If an error occurred, -1 is returned and <*str> remains
391 * NULL. */
392static int
393decode_spoe_string(char *buf, char *end, char **str, uint64_t *len)
394{
395 int r, idx = 0;
396
397 *str = NULL;
398 *len = 0;
399
400 if ((r = decode_spoe_varint(buf, end, len)) == -1)
401 goto error;
402 idx += r;
403 if (buf + idx + *len > end)
404 goto error;
405
406 *str = buf+idx;
407 return (idx + *len);
408
Christopher Fauletf95b1112016-12-21 08:58:16 +0100409 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100410 return -1;
411}
412
413/* Skip a typed data. If an error occurred, -1 is returned, otherwise the number
414 * of bytes read is returned. A types data is composed of a type (1 byte) and
415 * corresponding data:
416 * - boolean: non additional data (0 bytes)
417 * - integers: a variable-length integer (see decode_spoe_varint)
418 * - ipv4: 4 bytes
419 * - ipv6: 16 bytes
420 * - binary and string: a buffer prefixed by its size, a variable-length
421 * integer (see decode_spoe_string) */
422static int
423skip_spoe_data(char *frame, char *end)
424{
425 uint64_t sz = 0;
426 int r, idx = 0;
427
428 if (frame > end)
429 return -1;
430
431 switch (frame[idx++] & SPOE_DATA_T_MASK) {
432 case SPOE_DATA_T_BOOL:
433 idx++;
434 break;
435 case SPOE_DATA_T_INT32:
436 case SPOE_DATA_T_INT64:
437 case SPOE_DATA_T_UINT32:
438 case SPOE_DATA_T_UINT64:
439 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
440 return -1;
441 idx += r;
442 break;
443 case SPOE_DATA_T_IPV4:
444 idx += 4;
445 break;
446 case SPOE_DATA_T_IPV6:
447 idx += 16;
448 break;
449 case SPOE_DATA_T_STR:
450 case SPOE_DATA_T_BIN:
451 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
452 return -1;
453 idx += r + sz;
454 break;
455 }
456
457 if (frame+idx > end)
458 return -1;
459 return idx;
460}
461
462/* Decode a typed data. If an error occurred, -1 is returned, otherwise the
463 * number of read bytes is returned. See skip_spoe_data for details. */
464static int
465decode_spoe_data(char *frame, char *end, struct spoe_data *data)
466{
467 uint64_t sz = 0;
468 int type, r, idx = 0;
469
470 if (frame > end)
471 return -1;
472
473 type = frame[idx++];
474 data->type = (type & SPOE_DATA_T_MASK);
475 switch (data->type) {
476 case SPOE_DATA_T_BOOL:
477 data->u.boolean = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE);
478 break;
479 case SPOE_DATA_T_INT32:
480 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
481 return -1;
482 data->u.sint32 = sz;
483 idx += r;
484 break;
485 case SPOE_DATA_T_INT64:
486 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
487 return -1;
488 data->u.uint32 = sz;
489 idx += r;
490 break;
491 case SPOE_DATA_T_UINT32:
492 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
493 return -1;
494 data->u.sint64 = sz;
495 idx += r;
496 break;
497 case SPOE_DATA_T_UINT64:
498 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
499 return -1;
500 data->u.uint64 = sz;
501 idx += r;
502 break;
503 case SPOE_DATA_T_IPV4:
504 if (frame+idx+4 > end)
505 return -1;
506 memcpy(&data->u.ipv4, frame+idx, 4);
507 idx += 4;
508 break;
509 case SPOE_DATA_T_IPV6:
510 if (frame+idx+16 > end)
511 return -1;
512 memcpy(&data->u.ipv6, frame+idx, 16);
513 idx += 16;
514 break;
515 case SPOE_DATA_T_STR:
516 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
517 return -1;
518 idx += r;
519 if (frame+idx+sz > end)
520 return -1;
521 data->u.buffer.str = frame+idx;
522 data->u.buffer.len = sz;
523 idx += sz;
524 break;
525 case SPOE_DATA_T_BIN:
526 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
527 return -1;
528 idx += r;
529 if (frame+idx+sz > end)
530 return -1;
531 data->u.buffer.str = frame+idx;
532 data->u.buffer.len = sz;
533 idx += sz;
534 break;
535 default:
536 break;
537 }
538
539 if (frame+idx > end)
540 return -1;
541 return idx;
542}
543
544
545/* Check the protocol version. It returns -1 if an error occurred, the number of
546 * read bytes otherwise. */
547static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100548check_proto_version(struct spoe_frame *frame, int idx)
Christopher Faulet010fded2016-11-03 22:49:37 +0100549{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100550 char *str;
551 uint64_t sz;
Christopher Faulet010fded2016-11-03 22:49:37 +0100552
553 /* Get the list of all supported versions by HAProxy */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100554 if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Faulet010fded2016-11-03 22:49:37 +0100555 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100556 idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz);
557 if (str == NULL)
Christopher Faulet010fded2016-11-03 22:49:37 +0100558 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100559
560 DEBUG(frame->worker, "<%lu> Supported versions : %.*s",
561 frame->client->id, (int)sz, str);
Christopher Faulet010fded2016-11-03 22:49:37 +0100562
563 /* TODO: Find the right verion in supported ones */
564
565 return idx;
566}
567
568/* Check max frame size value. It returns -1 if an error occurred, the number of
569 * read bytes otherwise. */
570static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100571check_max_frame_size(struct spoe_frame *frame, int idx)
Christopher Faulet010fded2016-11-03 22:49:37 +0100572{
573 uint64_t sz;
574 int type, i;
575
576 /* Get the max-frame-size value of HAProxy */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100577 type = frame->buf[idx++];
Christopher Faulet010fded2016-11-03 22:49:37 +0100578 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
579 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
580 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
Christopher Fauletf95b1112016-12-21 08:58:16 +0100581 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64)
Christopher Faulet010fded2016-11-03 22:49:37 +0100582 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100583 if ((i = decode_spoe_varint(frame->buf+idx, frame->buf+frame->len, &sz)) == -1)
Christopher Faulet010fded2016-11-03 22:49:37 +0100584 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100585 idx += i;
586
587 /* Keep the lower value */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100588 if (sz < frame->client->max_frame_size)
589 frame->client->max_frame_size = sz;
590
591 DEBUG(frame->worker, "<%lu> HAProxy maximum frame size : %u",
592 frame->client->id, (unsigned int)sz);
Christopher Faulet010fded2016-11-03 22:49:37 +0100593
594 return idx;
595}
596
Christopher Fauletba7bc162016-11-07 21:07:38 +0100597/* Check healthcheck value. It returns -1 if an error occurred, the number of
598 * read bytes otherwise. */
599static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100600check_healthcheck(struct spoe_frame *frame, int idx)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100601{
602 int type;
603
Christopher Fauletf95b1112016-12-21 08:58:16 +0100604 /* Get the "healthcheck" value */
605 type = frame->buf[idx++];
606 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_BOOL)
607 return -1;
608 frame->hcheck = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE);
609
610 DEBUG(frame->worker, "<%lu> HELLO healthcheck : %s",
611 frame->client->id, (frame->hcheck ? "true" : "false"));
612 return idx;
613}
614
615/* Check capabilities value. It returns -1 if an error occurred, the number of
616 * read bytes otherwise. */
617static int
618check_capabilities(struct spoe_frame *frame, int idx)
619{
620 struct client *client = frame->client;
621 char *str;
622 uint64_t sz;
623 int i;
624
625 if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100626 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100627 idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz);
628 if (str == NULL) /* this is not an error */
629 return idx;
630
631 DEBUG(frame->worker, "<%lu> HAProxy capabilities : %.*s",
632 client->id, (int)sz, str);
633
634 i = 0;
635 while (i < sz) {
636 char *delim;
637
638 /* Skip leading spaces */
639 for (; isspace(str[i]) && i < sz; i++);
640
641 if (sz - i >= 10 && !strncmp(str + i, "pipelining", 10)) {
642 i += 10;
643 if (sz == i || isspace(str[i]) || str[i] == ',') {
644 DEBUG(frame->worker,
645 "<%lu> HAProxy supports frame pipelining",
646 client->id);
647 client->pipelining = true;
648 }
649
650 }
651 else if (sz - i >= 5 && !strncmp(str + i, "async", 5)) {
652 i += 5;
653 if (sz == i || isspace(str[i]) || str[i] == ',') {
654 DEBUG(frame->worker,
655 "<%lu> HAProxy supports asynchronous frame",
656 client->id);
657 client->async = true;
658 }
659 }
Christopher Faulet85010352017-02-02 10:14:36 +0100660 else if (sz - i >= 13 && !strncmp(str + i, "fragmentation", 13)) {
661 i += 5;
662 if (sz == i || isspace(str[i]) || str[i] == ',') {
663 DEBUG(frame->worker,
664 "<%lu> HAProxy supports fragmented frame",
665 client->id);
666 client->fragmentation = true;
667 }
668 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100669
670 if (sz == i || (delim = memchr(str + i, ',', sz-i)) == NULL)
671 break;
672 i = (delim - str) + 1;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100673 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100674
675 return idx;
676}
677
678/* Check engine-id value. It returns -1 if an error occurred, the number of
679 * read bytes otherwise. */
680static int
681check_engine_id(struct spoe_frame *frame, int idx)
682{
683 struct client *client = frame->client;
684 char *str;
685 uint64_t sz;
686
687 if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
688 return -1;
689
690 idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz);
691 if (str == NULL) /* this is not an error */
692 return idx;
693
694 if (client->engine != NULL)
695 return idx;
696
697 DEBUG(frame->worker, "<%lu> HAProxy engine id : %.*s",
698 client->id, (int)sz, str);
699
700 client->engine_id = strndup(str, (int)sz);
701 return idx;
702}
703
704/* Check disconnect status code. It returns -1 if an error occurred, the number
705 * of read bytes otherwise. */
706static int
707check_discon_status_code(struct spoe_frame *frame, int idx)
708{
709 uint64_t sz;
710 int type, i;
711
712 /* Get the "status-code" value */
713 type = frame->buf[idx++];
714 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
715 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
716 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
717 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64)
718 return -1;
719 if ((i = decode_spoe_varint(frame->buf+idx, frame->buf+frame->len, &sz)) == -1)
720 return -1;
721 idx += i;
722
723 frame->client->status_code = (unsigned int)sz;
724
725 DEBUG(frame->worker, "<%lu> Disconnect status code : %u",
726 frame->client->id, frame->client->status_code);
727
Christopher Fauletba7bc162016-11-07 21:07:38 +0100728 return idx;
729}
730
Christopher Fauletf95b1112016-12-21 08:58:16 +0100731/* Check the disconnect message. It returns -1 if an error occurred, the number
732 * of read bytes otherwise. */
733static int
734check_discon_message(struct spoe_frame *frame, int idx)
735{
736 char *str;
737 uint64_t sz;
738
739 /* Get the "message" value */
740 if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
741 return -1;
742 idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz);
743 if (str == NULL)
744 return -1;
745
746 DEBUG(frame->worker, "<%lu> Disconnect message : %.*s",
747 frame->client->id, (int)sz, str);
748
749 return idx;
750}
Christopher Fauletba7bc162016-11-07 21:07:38 +0100751
Christopher Faulet010fded2016-11-03 22:49:37 +0100752/* Decode a HELLO frame received from HAProxy. It returns -1 if an error
Christopher Fauletf95b1112016-12-21 08:58:16 +0100753 * occurred, otherwise the number of read bytes. HELLO frame cannot be
754 * ignored and having another frame than a HELLO frame is an error. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100755static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100756handle_hahello(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100757{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100758 struct client *client = frame->client;
759 char *buf = frame->buf;
760 char *end = frame->buf + frame->len;
761 int i, idx = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +0100762
Christopher Fauletf95b1112016-12-21 08:58:16 +0100763 /* Check frame type: we really want a HELLO frame */
764 if (buf[idx++] != SPOE_FRM_T_HAPROXY_HELLO)
765 goto error;
766
767 DEBUG(frame->worker, "<%lu> Decode HAProxy HELLO frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100768
Christopher Faulet85010352017-02-02 10:14:36 +0100769 /* Retrieve flags */
770 memcpy((char *)&(frame->flags), buf+idx, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +0100771 idx += 4;
772
Christopher Faulet85010352017-02-02 10:14:36 +0100773 /* Fragmentation is not supported for HELLO frame */
774 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
775 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
776 goto error;
777 }
778
Christopher Faulet010fded2016-11-03 22:49:37 +0100779 /* stream-id and frame-id must be cleared */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100780 if (buf[idx] != 0 || buf[idx+1] != 0) {
781 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100782 goto error;
783 }
784 idx += 2;
785
786 /* Loop on K/V items */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100787 while (buf+idx < end) {
Christopher Faulet010fded2016-11-03 22:49:37 +0100788 char *str;
789 uint64_t sz;
790
791 /* Decode the item name */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100792 idx += decode_spoe_string(buf+idx, end, &str, &sz);
Christopher Faulet010fded2016-11-03 22:49:37 +0100793 if (str == NULL) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100794 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100795 goto error;
796 }
797
798 /* Check "supported-versions" K/V item */
799 if (!memcmp(str, "supported-versions", sz)) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100800 if ((i = check_proto_version(frame, idx)) == -1) {
801 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100802 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100803 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100804 idx = i;
805 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100806 /* Check "max-frame-size" K/V item */
Christopher Faulet010fded2016-11-03 22:49:37 +0100807 else if (!memcmp(str, "max-frame-size", sz)) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100808 if ((i = check_max_frame_size(frame, idx)) == -1) {
809 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100810 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100811 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100812 idx = i;
813 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100814 /* Check "healthcheck" K/V item */
Christopher Fauletba7bc162016-11-07 21:07:38 +0100815 else if (!memcmp(str, "healthcheck", sz)) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100816 if ((i = check_healthcheck(frame, idx)) == -1) {
817 client->status_code = SPOE_FRM_ERR_INVALID;
818 goto error;
819 }
820 idx = i;
821 }
822 /* Check "capabilities" K/V item */
823 else if (!memcmp(str, "capabilities", sz)) {
824 if ((i = check_capabilities(frame, idx)) == -1) {
825 client->status_code = SPOE_FRM_ERR_INVALID;
826 goto error;
827 }
828 idx = i;
829 }
830 /* Check "engine-id" K/V item */
831 else if (!memcmp(str, "engine-id", sz)) {
832 if ((i = check_engine_id(frame, idx)) == -1) {
833 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100834 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100835 }
Christopher Fauletba7bc162016-11-07 21:07:38 +0100836 idx = i;
837 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100838 else {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100839 DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s",
840 client->id, (int)sz, str);
841
Christopher Faulet010fded2016-11-03 22:49:37 +0100842 /* Silently ignore unknown item */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100843 if ((i = skip_spoe_data(buf+idx, end)) == -1) {
844 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100845 goto error;
846 }
847 idx += i;
848 }
849 }
850
Christopher Fauletf95b1112016-12-21 08:58:16 +0100851 if (async == false || client->engine_id == NULL)
852 client->async = false;
853 if (pipelining == false)
854 client->pipelining = false;
855
856 if (client->async == true)
857 use_spoe_engine(client);
858
Christopher Faulet010fded2016-11-03 22:49:37 +0100859 return idx;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100860
861 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100862 return -1;
863}
864
865/* Decode a DISCONNECT frame received from HAProxy. It returns -1 if an error
Christopher Fauletf95b1112016-12-21 08:58:16 +0100866 * occurred, otherwise the number of read bytes. DISCONNECT frame cannot be
867 * ignored and having another frame than a DISCONNECT frame is an error.*/
Christopher Faulet010fded2016-11-03 22:49:37 +0100868static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100869handle_hadiscon(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100870{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100871 struct client *client = frame->client;
872 char *buf = frame->buf;
873 char *end = frame->buf + frame->len;
874 int i, idx = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +0100875
Christopher Fauletf95b1112016-12-21 08:58:16 +0100876 /* Check frame type: we really want a DISCONNECT frame */
877 if (buf[idx++] != SPOE_FRM_T_HAPROXY_DISCON)
878 goto error;
879
880 DEBUG(frame->worker, "<%lu> Decode HAProxy DISCONNECT frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100881
Christopher Faulet85010352017-02-02 10:14:36 +0100882 /* Retrieve flags */
883 memcpy((char *)&(frame->flags), buf+idx, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +0100884 idx += 4;
885
Christopher Faulet85010352017-02-02 10:14:36 +0100886 /* Fragmentation is not supported for DISCONNECT frame */
887 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
888 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
889 goto error;
890 }
891
Christopher Faulet010fded2016-11-03 22:49:37 +0100892 /* stream-id and frame-id must be cleared */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100893 if (buf[idx] != 0 || buf[idx+1] != 0) {
894 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100895 goto error;
896 }
897 idx += 2;
898
Christopher Fauletf95b1112016-12-21 08:58:16 +0100899 client->status_code = SPOE_FRM_ERR_NONE;
900
Christopher Faulet010fded2016-11-03 22:49:37 +0100901 /* Loop on K/V items */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100902 while (buf+idx < end) {
Christopher Faulet010fded2016-11-03 22:49:37 +0100903 char *str;
904 uint64_t sz;
905
906 /* Decode item key */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100907 idx += decode_spoe_string(buf+idx, end, &str, &sz);
Christopher Faulet010fded2016-11-03 22:49:37 +0100908 if (str == NULL) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100909 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100910 goto error;
911 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100912
913 /* Check "status-code" K/V item */
914 if (!memcmp(str, "status-code", sz)) {
915 if ((i = check_discon_status_code(frame, idx)) == -1) {
916 client->status_code = SPOE_FRM_ERR_INVALID;
917 goto error;
918 }
919 idx = i;
920 }
921 /* Check "message" K/V item */
922 else if (!memcmp(str, "message", sz)) {
923 if ((i = check_discon_message(frame, idx)) == -1) {
924 client->status_code = SPOE_FRM_ERR_INVALID;
925 goto error;
926 }
927 idx = i;
928 }
929 else {
930 DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s",
931 client->id, (int)sz, str);
932
933 /* Silently ignore unknown item */
934 if ((i = skip_spoe_data(buf+idx, end)) == -1) {
935 client->status_code = SPOE_FRM_ERR_INVALID;
936 goto error;
937 }
938 idx += i;
Christopher Faulet010fded2016-11-03 22:49:37 +0100939 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100940 }
941
Christopher Faulet010fded2016-11-03 22:49:37 +0100942 return idx;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100943
944 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100945 return -1;
946}
947
948/* Decode a NOTIFY frame received from HAProxy. It returns -1 if an error
Christopher Faulet85010352017-02-02 10:14:36 +0100949 * occurred, 0 if it must be must be ignored, otherwise the number of read
950 * bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100951static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100952handle_hanotify(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100953{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100954 struct client *client = frame->client;
955 char *buf = frame->buf;
956 char *end = frame->buf + frame->len;
957 uint64_t stream_id, frame_id;
958 int i, idx = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +0100959
960 /* Check frame type */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100961 if (buf[idx++] != SPOE_FRM_T_HAPROXY_NOTIFY)
962 goto ignore;
963
964 DEBUG(frame->worker, "<%lu> Decode HAProxy NOTIFY frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100965
Christopher Faulet85010352017-02-02 10:14:36 +0100966 /* Retrieve flags */
967 memcpy((char *)&(frame->flags), buf+idx, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +0100968 idx += 4;
969
Christopher Faulet85010352017-02-02 10:14:36 +0100970 /* Fragmentation is not supported for DISCONNECT frame */
971 if (!(frame->flags & SPOE_FRM_FL_FIN) && fragmentation == false) {
972 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
973 goto error;
974 }
975
Christopher Faulet010fded2016-11-03 22:49:37 +0100976 /* Read the stream-id */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100977 if ((i = decode_spoe_varint(buf+idx, end, &stream_id)) == -1)
978 goto ignore;
Christopher Faulet010fded2016-11-03 22:49:37 +0100979 idx += i;
980
981 /* Read the frame-id */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100982 if ((i = decode_spoe_varint(buf+idx, end, &frame_id)) == -1)
983 goto ignore;
Christopher Faulet010fded2016-11-03 22:49:37 +0100984 idx += i;
985
Christopher Faulet85010352017-02-02 10:14:36 +0100986 if (frame->fragmented == true) {
987 if (frame->stream_id != (unsigned int)stream_id ||
988 frame->frame_id != (unsigned int)frame_id) {
989 client->status_code = SPOE_FRM_ERR_INTERLACED_FRAMES;
990 goto error;
991 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100992
Christopher Faulet85010352017-02-02 10:14:36 +0100993 if (frame->flags & SPOE_FRM_FL_ABRT) {
994 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
995 " - Abort processing of a fragmented frame"
996 " - frag_len=%u - len=%u - offset=%u",
997 client->id, frame->stream_id, frame->frame_id,
998 frame->frag_len, frame->len, idx);
999 goto ignore;
1000 }
1001 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
1002 " - %s fragment of a fragmented frame received"
1003 " - frag_len=%u - len=%u - offset=%u",
1004 client->id, frame->stream_id, frame->frame_id,
1005 (frame->flags & SPOE_FRM_FL_FIN) ? "last" : "next",
1006 frame->frag_len, frame->len, idx);
1007 }
1008 else {
1009 frame->stream_id = (unsigned int)stream_id;
1010 frame->frame_id = (unsigned int)frame_id;
Christopher Faulet010fded2016-11-03 22:49:37 +01001011
Christopher Faulet85010352017-02-02 10:14:36 +01001012 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
1013 " - %s frame received"
1014 " - frag_len=%u - len=%u - offset=%u",
1015 client->id, frame->stream_id, frame->frame_id,
1016 (frame->flags & SPOE_FRM_FL_FIN) ? "unfragmented" : "fragmented",
1017 frame->frag_len, frame->len, idx);
1018
1019 frame->fragmented = !(frame->flags & SPOE_FRM_FL_FIN);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001020 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001021
Christopher Fauletf95b1112016-12-21 08:58:16 +01001022 frame->offset = idx;
1023 return idx;
Christopher Faulet010fded2016-11-03 22:49:37 +01001024
Christopher Fauletf95b1112016-12-21 08:58:16 +01001025 ignore:
Christopher Faulet85010352017-02-02 10:14:36 +01001026 return 0;
1027
1028 error:
Christopher Fauletf95b1112016-12-21 08:58:16 +01001029 return -1;
1030}
Christopher Faulet010fded2016-11-03 22:49:37 +01001031
Christopher Fauletf95b1112016-12-21 08:58:16 +01001032/* Encode a HELLO frame to send it to HAProxy. It returns the number of written
1033 * bytes. */
1034static int
1035prepare_agenthello(struct spoe_frame *frame)
1036{
1037 struct client *client = frame->client;
1038 char *buf = frame->buf;
Christopher Faulet85010352017-02-02 10:14:36 +01001039 char capabilities[64];
1040 int n, idx = 0;
1041 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet010fded2016-11-03 22:49:37 +01001042
Christopher Fauletf95b1112016-12-21 08:58:16 +01001043 DEBUG(frame->worker, "<%lu> Encode Agent HELLO frame", client->id);
1044 frame->type = SPOA_FRM_T_AGENT;
Christopher Faulet010fded2016-11-03 22:49:37 +01001045
1046 /* Frame Type */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001047 buf[idx++] = SPOE_FRM_T_AGENT_HELLO;
Christopher Faulet010fded2016-11-03 22:49:37 +01001048
Christopher Faulet85010352017-02-02 10:14:36 +01001049 /* Set flags */
1050 memcpy(buf+idx, (char *)&flags, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001051 idx += 4;
1052
1053 /* No stream-id and frame-id for HELLO frames */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001054 buf[idx++] = 0;
1055 buf[idx++] = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +01001056
1057 /* "version" K/V item */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001058 idx += encode_spoe_string("version", 7, buf+idx);
1059 buf[idx++] = SPOE_DATA_T_STR;
1060 idx += encode_spoe_string(SPOP_VERSION, SLEN(SPOP_VERSION), buf+idx);
1061 DEBUG(frame->worker, "<%lu> Agent version : %s",
1062 client->id, SPOP_VERSION);
1063
Christopher Faulet010fded2016-11-03 22:49:37 +01001064
1065 /* "max-frame-size" K/V item */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001066 idx += encode_spoe_string("max-frame-size", 14, buf+idx);
1067 buf[idx++] = SPOE_DATA_T_UINT32;
1068 idx += encode_spoe_varint(client->max_frame_size, buf+idx);
1069 DEBUG(frame->worker, "<%lu> Agent maximum frame size : %u",
1070 client->id, client->max_frame_size);
Christopher Faulet010fded2016-11-03 22:49:37 +01001071
1072 /* "capabilities" K/V item */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001073 idx += encode_spoe_string("capabilities", 12, buf+idx);
1074 buf[idx++] = SPOE_DATA_T_STR;
Christopher Faulet85010352017-02-02 10:14:36 +01001075
1076 memset(capabilities, 0, sizeof(capabilities));
1077 n = 0;
1078
1079 /* 1. Fragmentation capability ? */
1080 if (fragmentation == true) {
1081 memcpy(capabilities, "fragmentation", 13);
1082 n += 13;
1083 }
1084 /* 2. Pipelining capability ? */
1085 if (client->pipelining == true && n != 0) {
1086 memcpy(capabilities + n, ", pipelining", 12);
1087 n += 12;
1088 }
1089 else if (client->pipelining == true) {
1090 memcpy(capabilities, "pipelining", 10);
1091 n += 10;
1092 }
1093 /* 3. Async capability ? */
1094 if (client->async == true && n != 0) {
1095 memcpy(capabilities + n, ", async", 7);
1096 n += 7;
1097 }
1098 else if (client->async == true) {
1099 memcpy(capabilities, "async", 5);
1100 n += 5;
1101 }
1102 /* 4. Encode capabilities string */
1103 if (n != 0)
1104 idx += encode_spoe_string(capabilities, n, buf+idx);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001105 else
1106 idx += encode_spoe_string(NULL, 0, buf+idx);
Christopher Faulet010fded2016-11-03 22:49:37 +01001107
Christopher Faulet85010352017-02-02 10:14:36 +01001108 DEBUG(frame->worker, "<%lu> Agent capabilities : %.*s",
1109 client->id, n, capabilities);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001110
1111 frame->len = idx;
Christopher Faulet010fded2016-11-03 22:49:37 +01001112 return idx;
1113}
1114
Christopher Fauletf95b1112016-12-21 08:58:16 +01001115/* Encode a DISCONNECT frame to send it to HAProxy. It returns the number of
1116 * written bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +01001117static int
Christopher Fauletf95b1112016-12-21 08:58:16 +01001118prepare_agentdicon(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +01001119{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001120 struct client *client = frame->client;
1121 char *buf = frame->buf;
1122 const char *reason;
1123 int rlen, idx = 0;
Christopher Faulet85010352017-02-02 10:14:36 +01001124 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001125
1126 DEBUG(frame->worker, "<%lu> Encode Agent DISCONNECT frame", client->id);
1127 frame->type = SPOA_FRM_T_AGENT;
1128
1129 if (client->status_code >= SPOE_FRM_ERRS)
1130 client->status_code = SPOE_FRM_ERR_UNKNOWN;
1131 reason = spoe_frm_err_reasons[client->status_code];
1132 rlen = strlen(reason);
Christopher Faulet010fded2016-11-03 22:49:37 +01001133
1134 /* Frame type */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001135 buf[idx++] = SPOE_FRM_T_AGENT_DISCON;
Christopher Faulet010fded2016-11-03 22:49:37 +01001136
Christopher Faulet85010352017-02-02 10:14:36 +01001137 /* Set flags */
1138 memcpy(buf+idx, (char *)&flags, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001139 idx += 4;
1140
Christopher Fauletf95b1112016-12-21 08:58:16 +01001141 /* No stream-id and frame-id for DISCONNECT frames */
1142 buf[idx++] = 0;
1143 buf[idx++] = 0;
1144
1145 /* There are 2 mandatory items: "status-code" and "message" */
1146
1147 /* "status-code" K/V item */
1148 idx += encode_spoe_string("status-code", 11, buf+idx);
1149 buf[idx++] = SPOE_DATA_T_UINT32;
1150 idx += encode_spoe_varint(client->status_code, buf+idx);
1151 DEBUG(frame->worker, "<%lu> Disconnect status code : %u",
1152 client->id, client->status_code);
Christopher Faulet010fded2016-11-03 22:49:37 +01001153
Christopher Fauletf95b1112016-12-21 08:58:16 +01001154 /* "message" K/V item */
1155 idx += encode_spoe_string("message", 7, buf+idx);
1156 buf[idx++] = SPOE_DATA_T_STR;
1157 idx += encode_spoe_string(reason, rlen, buf+idx);
1158 DEBUG(frame->worker, "<%lu> Disconnect message : %s",
1159 client->id, reason);
Christopher Faulet010fded2016-11-03 22:49:37 +01001160
Christopher Fauletf95b1112016-12-21 08:58:16 +01001161 frame->len = idx;
Christopher Faulet010fded2016-11-03 22:49:37 +01001162 return idx;
1163}
1164
Christopher Fauletf95b1112016-12-21 08:58:16 +01001165/* Encode a ACK frame to send it to HAProxy. It returns the number of written
1166 * bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +01001167static int
Christopher Fauletf95b1112016-12-21 08:58:16 +01001168prepare_agentack(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +01001169{
Christopher Faulet85010352017-02-02 10:14:36 +01001170 char *buf = frame->buf;
1171 int idx = 0;
1172 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet010fded2016-11-03 22:49:37 +01001173
Christopher Fauletf95b1112016-12-21 08:58:16 +01001174 /* Be careful here, in async mode, frame->client can be NULL */
1175
1176 DEBUG(frame->worker, "Encode Agent ACK frame");
1177 frame->type = SPOA_FRM_T_AGENT;
Christopher Faulet010fded2016-11-03 22:49:37 +01001178
1179 /* Frame type */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001180 buf[idx++] = SPOE_FRM_T_AGENT_ACK;
Christopher Faulet010fded2016-11-03 22:49:37 +01001181
Christopher Faulet85010352017-02-02 10:14:36 +01001182 /* Set flags */
1183 memcpy(buf+idx, (char *)&flags, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001184 idx += 4;
1185
Christopher Fauletf95b1112016-12-21 08:58:16 +01001186 /* Set stream-id and frame-id for ACK frames */
1187 idx += encode_spoe_varint(frame->stream_id, buf+idx);
1188 idx += encode_spoe_varint(frame->frame_id, buf+idx);
Christopher Faulet010fded2016-11-03 22:49:37 +01001189
Christopher Fauletf95b1112016-12-21 08:58:16 +01001190 DEBUG(frame->worker, "STREAM-ID=%u - FRAME-ID=%u",
1191 frame->stream_id, frame->frame_id);
Christopher Faulet010fded2016-11-03 22:49:37 +01001192
Christopher Fauletf95b1112016-12-21 08:58:16 +01001193 frame->len = idx;
Christopher Faulet010fded2016-11-03 22:49:37 +01001194 return idx;
1195}
1196
1197static int
Christopher Fauletf95b1112016-12-21 08:58:16 +01001198create_server_socket(void)
Christopher Faulet010fded2016-11-03 22:49:37 +01001199{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001200 struct sockaddr_in listen_addr;
1201 int fd, yes = 1;
1202
1203 fd = socket(AF_INET, SOCK_STREAM, 0);
1204 if (fd < 0) {
1205 LOG(&null_worker, "Failed to create service socket : %m");
1206 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001207 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001208
1209 memset(&listen_addr, 0, sizeof(listen_addr));
1210 listen_addr.sin_family = AF_INET;
1211 listen_addr.sin_addr.s_addr = INADDR_ANY;
1212 listen_addr.sin_port = htons(server_port);
1213
1214 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0 ||
1215 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) < 0) {
1216 LOG(&null_worker, "Failed to set option on server socket : %m");
1217 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001218 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001219
1220 if (bind(fd, (struct sockaddr *) &listen_addr, sizeof(listen_addr)) < 0) {
1221 LOG(&null_worker, "Failed to bind server socket : %m");
1222 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001223 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001224
1225 if (listen(fd, CONNECTION_BACKLOG) < 0) {
1226 LOG(&null_worker, "Failed to listen on server socket : %m");
1227 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001228 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001229
1230 return fd;
Christopher Faulet010fded2016-11-03 22:49:37 +01001231}
1232
Christopher Fauletf95b1112016-12-21 08:58:16 +01001233static void
1234release_frame(struct spoe_frame *frame)
1235{
1236 struct worker *worker;
1237
1238 if (frame == NULL)
1239 return;
1240
1241 if (event_pending(&frame->process_frame_event, EV_TIMEOUT, NULL))
1242 event_del(&frame->process_frame_event);
1243
1244 worker = frame->worker;
1245 LIST_DEL(&frame->list);
Christopher Faulet85010352017-02-02 10:14:36 +01001246 if (frame->frag_buf)
1247 free(frame->frag_buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001248 memset(frame, 0, sizeof(*frame)+max_frame_size+4);
1249 LIST_ADDQ(&worker->frames, &frame->list);
1250}
1251
1252static void
1253release_client(struct client *c)
Christopher Faulet010fded2016-11-03 22:49:37 +01001254{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001255 struct spoe_frame *frame, *back;
1256
1257 if (c == NULL)
1258 return;
1259
1260 DEBUG(c->worker, "<%lu> Release client", c->id);
1261
1262 LIST_DEL(&c->by_worker);
1263 c->worker->nbclients--;
1264
1265 unuse_spoe_engine(c);
1266 free(c->engine_id);
1267
1268 if (event_pending(&c->read_frame_event, EV_READ, NULL))
1269 event_del(&c->read_frame_event);
1270 if (event_pending(&c->write_frame_event, EV_WRITE, NULL))
1271 event_del(&c->write_frame_event);
1272
1273 release_frame(c->incoming_frame);
1274 release_frame(c->outgoing_frame);
1275 list_for_each_entry_safe(frame, back, &c->processing_frames, list) {
1276 release_frame(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +01001277 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001278 list_for_each_entry_safe(frame, back, &c->outgoing_frames, list) {
1279 release_frame(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +01001280 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001281
1282 if (c->fd >= 0)
1283 close(c->fd);
1284
1285 free(c);
1286}
1287
1288static void
1289reset_frame(struct spoe_frame *frame)
1290{
1291 if (frame == NULL)
1292 return;
1293
Christopher Faulet85010352017-02-02 10:14:36 +01001294 if (frame->frag_buf)
1295 free(frame->frag_buf);
1296
1297 frame->type = SPOA_FRM_T_UNKNOWN;
1298 frame->buf = (char *)(frame->data);
1299 frame->offset = 0;
1300 frame->len = 0;
1301 frame->stream_id = 0;
1302 frame->frame_id = 0;
1303 frame->flags = 0;
1304 frame->hcheck = false;
1305 frame->fragmented = false;
1306 frame->ip_score = -1;
1307 frame->frag_buf = NULL;
1308 frame->frag_len = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001309 LIST_INIT(&frame->list);
1310}
1311
1312static void
1313use_spoe_engine(struct client *client)
1314{
1315 struct spoe_engine *eng;
1316
1317 if (client->engine_id == NULL)
1318 return;
1319
1320 list_for_each_entry(eng, &client->worker->engines, list) {
1321 if (!strcmp(eng->id, client->engine_id))
1322 goto end;
1323 }
1324
1325 if ((eng = malloc(sizeof(*eng))) == NULL) {
1326 client->async = false;
1327 return;
1328 }
1329
1330 eng->id = strdup(client->engine_id);
1331 LIST_INIT(&eng->clients);
1332 LIST_INIT(&eng->processing_frames);
1333 LIST_INIT(&eng->outgoing_frames);
1334 LIST_ADDQ(&client->worker->engines, &eng->list);
1335 LOG(client->worker, "Add new SPOE engine '%s'", eng->id);
1336
1337 end:
1338 client->engine = eng;
1339 LIST_ADDQ(&eng->clients, &client->by_engine);
1340}
1341
1342static void
1343unuse_spoe_engine(struct client *client)
1344{
1345 struct spoe_engine *eng;
1346 struct spoe_frame *frame, *back;
1347
1348 if (client == NULL || client->engine == NULL)
1349 return;
1350
1351 eng = client->engine;
1352 client->engine = NULL;
1353 LIST_DEL(&client->by_engine);
1354 if (!LIST_ISEMPTY(&eng->clients))
1355 return;
1356
1357 LOG(client->worker, "Remove SPOE engine '%s'", eng->id);
1358 LIST_DEL(&eng->list);
1359
1360 list_for_each_entry_safe(frame, back, &eng->processing_frames, list) {
1361 release_frame(frame);
1362 }
1363 list_for_each_entry_safe(frame, back, &eng->outgoing_frames, list) {
1364 release_frame(frame);
1365 }
1366 free(eng->id);
1367 free(eng);
1368}
1369
1370
1371static struct spoe_frame *
1372acquire_incoming_frame(struct client *client)
1373{
1374 struct spoe_frame *frame;
1375
1376 frame = client->incoming_frame;
1377 if (frame != NULL)
1378 return frame;
1379
1380 if (LIST_ISEMPTY(&client->worker->frames)) {
1381 if ((frame = calloc(1, sizeof(*frame)+max_frame_size+4)) == NULL) {
1382 LOG(client->worker, "Failed to allocate new frame : %m");
1383 return NULL;
1384 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001385 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001386 else {
1387 frame = LIST_NEXT(&client->worker->frames, typeof(frame), list);
1388 LIST_DEL(&frame->list);
Christopher Faulet010fded2016-11-03 22:49:37 +01001389 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001390
1391 reset_frame(frame);
1392 frame->worker = client->worker;
1393 frame->engine = client->engine;
1394 frame->client = client;
1395
1396 if (event_assign(&frame->process_frame_event, client->worker->base, -1,
1397 EV_TIMEOUT|EV_PERSIST, process_frame_cb, frame) < 0) {
1398 LOG(client->worker, "Failed to create frame event");
1399 return NULL;
Christopher Faulet010fded2016-11-03 22:49:37 +01001400 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001401
1402 client->incoming_frame = frame;
1403 return frame;
Christopher Faulet010fded2016-11-03 22:49:37 +01001404}
1405
Christopher Fauletf95b1112016-12-21 08:58:16 +01001406static struct spoe_frame *
1407acquire_outgoing_frame(struct client *client)
Christopher Faulet010fded2016-11-03 22:49:37 +01001408{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001409 struct spoe_engine *engine = client->engine;
1410 struct spoe_frame *frame = NULL;
Christopher Faulet010fded2016-11-03 22:49:37 +01001411
Christopher Fauletf95b1112016-12-21 08:58:16 +01001412 if (client->outgoing_frame != NULL)
1413 frame = client->outgoing_frame;
1414 else if (!LIST_ISEMPTY(&client->outgoing_frames)) {
1415 frame = LIST_NEXT(&client->outgoing_frames, typeof(frame), list);
1416 LIST_DEL(&frame->list);
1417 client->outgoing_frame = frame;
1418 }
1419 else if (engine!= NULL && !LIST_ISEMPTY(&engine->outgoing_frames)) {
1420 frame = LIST_NEXT(&engine->outgoing_frames, typeof(frame), list);
1421 LIST_DEL(&frame->list);
1422 client->outgoing_frame = frame;
1423 }
1424 return frame;
1425}
1426
1427static void
1428write_frame(struct client *client, struct spoe_frame *frame)
1429{
1430 uint32_t netint;
1431
1432 LIST_DEL(&frame->list);
1433
1434 frame->buf = (char *)(frame->data);
1435 frame->offset = 0;
1436 netint = htonl(frame->len);
1437 memcpy(frame->buf, &netint, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001438
Christopher Fauletf95b1112016-12-21 08:58:16 +01001439 if (client != NULL) { /* HELLO or DISCONNECT frames */
1440 event_add(&client->write_frame_event, NULL);
Christopher Faulet010fded2016-11-03 22:49:37 +01001441
Christopher Fauletf95b1112016-12-21 08:58:16 +01001442 /* Try to process the frame as soon as possible, and always
1443 * attach it to the client */
1444 if (client->async || client->pipelining) {
1445 if (client->outgoing_frame == NULL)
1446 client->outgoing_frame = frame;
1447 else
1448 LIST_ADD(&client->outgoing_frames, &frame->list);
1449 }
1450 else {
1451 client->outgoing_frame = frame;
1452 event_del(&client->read_frame_event);
Christopher Faulet010fded2016-11-03 22:49:37 +01001453 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001454 }
1455 else { /* for all other frames */
1456 if (frame->client == NULL) { /* async mode ! */
1457 LIST_ADDQ(&frame->engine->outgoing_frames, &frame->list);
1458 list_for_each_entry(client, &frame->engine->clients, by_engine)
1459 event_add(&client->write_frame_event, NULL);
1460 }
1461 else if (frame->client->pipelining) {
1462 LIST_ADDQ(&frame->client->outgoing_frames, &frame->list);
1463 event_add(&frame->client->write_frame_event, NULL);
1464 }
1465 else {
1466 frame->client->outgoing_frame = frame;
1467 event_add(&frame->client->write_frame_event, NULL);
1468 event_del(&frame->client->read_frame_event);
1469 }
1470 }
1471}
Christopher Faulet010fded2016-11-03 22:49:37 +01001472
Christopher Fauletf95b1112016-12-21 08:58:16 +01001473static void
1474process_incoming_frame(struct spoe_frame *frame)
1475{
1476 struct client *client = frame->client;
Christopher Faulet010fded2016-11-03 22:49:37 +01001477
Christopher Fauletf95b1112016-12-21 08:58:16 +01001478 if (event_add(&frame->process_frame_event, &processing_delay) < 0) {
1479 LOG(client->worker, "Failed to process incoming frame");
1480 release_frame(frame);
1481 return;
1482 }
1483
1484 if (client->async) {
1485 frame->client = NULL;
1486 LIST_ADDQ(&frame->engine->processing_frames, &frame->list);
1487 }
1488 else if (client->pipelining)
1489 LIST_ADDQ(&client->processing_frames, &frame->list);
1490 else
1491 event_del(&client->read_frame_event);
1492}
1493
1494static void
1495signal_cb(evutil_socket_t sig, short events, void *user_data)
1496{
1497 struct event_base *base = user_data;
1498 int i;
1499
1500 DEBUG(&null_worker, "Stopping the server");
1501
1502 event_base_loopbreak(base);
1503 DEBUG(&null_worker, "Main event loop stopped");
1504
1505 for (i = 0; i < num_workers; i++) {
1506 event_base_loopbreak(workers[i].base);
1507 DEBUG(&null_worker, "Event loop stopped for worker %02d",
1508 workers[i].id);
1509 }
1510}
1511
1512static void
1513worker_monitor_cb(evutil_socket_t fd, short events, void *arg)
1514{
1515 struct worker *worker = arg;
1516
1517 LOG(worker, "%u clients connected", worker->nbclients);
1518}
1519
1520static void
1521process_frame_cb(evutil_socket_t fd, short events, void *arg)
1522{
1523 struct spoe_frame *frame = arg;
1524 char *buf = frame->buf;
1525 char *end = frame->buf + frame->len;
1526 int idx = frame->offset;
1527
1528 DEBUG(frame->worker,
Christopher Faulet85010352017-02-02 10:14:36 +01001529 "Process frame messages : STREAM-ID=%u - FRAME-ID=%u - length=%u bytes",
1530 frame->stream_id, frame->frame_id, frame->len - frame->offset);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001531
1532 /* Loop on messages */
1533 while (buf+idx < end) {
1534 char *str;
1535 uint64_t sz;
1536 int nbargs, i;
1537
1538 /* Decode the message name */
1539 idx += decode_spoe_string(buf+idx, end, &str, &sz);
1540 if (str == NULL)
1541 goto stop_processing;
1542
1543 DEBUG(frame->worker, "Process SPOE Message '%.*s'", (int)sz, str);
1544
1545 nbargs = buf[idx++]; /* Get the number of arguments */
1546 frame->offset = idx; /* Save index to handle errors and skip args */
1547 if (!memcmp(str, "check-client-ip", sz)) {
1548 struct spoe_data data;
1549
1550 memset(&data, 0, sizeof(data));
1551
1552 if (nbargs != 1)
1553 goto skip_message;
1554
1555 if ((i = decode_spoe_string(buf+idx, end, &str, &sz)) == -1)
1556 goto stop_processing;
1557 idx += i;
1558
1559 if ((i = decode_spoe_data(buf+idx, end, &data)) == -1)
1560 goto skip_message;
1561 idx += i;
1562
1563 if ((data.type & SPOE_DATA_T_MASK) == SPOE_DATA_T_IPV4)
1564 check_ipv4_reputation(frame, &data.u.ipv4);
1565 else if ((data.type & SPOE_DATA_T_MASK) == SPOE_DATA_T_IPV6)
1566 check_ipv6_reputation(frame, &data.u.ipv6);
1567 }
1568 else {
1569 skip_message:
1570 idx = frame->offset; /* Restore index */
1571
1572 while (nbargs-- > 0) {
1573 /* Silently ignore argument: its name and its value */
1574 if ((i = decode_spoe_string(buf+idx, end, &str, &sz)) == -1)
1575 goto stop_processing;
1576 idx += i;
1577 if ((i = skip_spoe_data(buf+idx, end)) == -1)
1578 goto stop_processing;
1579 idx += i;
1580 }
1581 }
1582 }
1583
1584 stop_processing:
1585 /* Prepare agent ACK frame */
Christopher Faulet85010352017-02-02 10:14:36 +01001586 frame->buf = (char *)(frame->data) + 4;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001587 frame->offset = 0;
Christopher Faulet85010352017-02-02 10:14:36 +01001588 frame->len = 0;
1589 frame->flags = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001590 idx = prepare_agentack(frame);
1591
1592 if (frame->ip_score != -1) {
1593 DEBUG(frame->worker, "Add action : set variable ip_scode=%u",
1594 frame->ip_score);
1595
1596 buf[idx++] = SPOE_ACT_T_SET_VAR; /* Action type */
1597 buf[idx++] = 3; /* Number of args */
1598 buf[idx++] = SPOE_SCOPE_SESS; /* Arg 1: the scope */
1599 idx += encode_spoe_string("ip_score", 8, buf+idx); /* Arg 2: variable name */
1600 buf[idx++] = SPOE_DATA_T_UINT32;
1601 idx += encode_spoe_varint(frame->ip_score, buf+idx); /* Arg 3: variable value */
1602 frame->len = idx;
1603 }
1604 write_frame(NULL, frame);
1605}
1606
1607static void
1608read_frame_cb(evutil_socket_t fd, short events, void *arg)
1609{
1610 struct client *client = arg;
1611 struct spoe_frame *frame;
1612 uint32_t netint;
1613 int n;
1614
1615 DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__);
1616 if ((frame = acquire_incoming_frame(client)) == NULL)
1617 goto close;
1618
1619 frame->type = SPOA_FRM_T_HAPROXY;
1620 if (frame->buf == (char *)(frame->data)) {
1621 /* Read the frame length: frame->buf points on length part (frame->data) */
1622 n = read(client->fd, frame->buf+frame->offset, 4-frame->offset);
1623 if (n <= 0) {
1624 if (n < 0)
1625 LOG(client->worker, "Failed to read frame length : %m");
Christopher Fauletba7bc162016-11-07 21:07:38 +01001626 goto close;
Christopher Faulet010fded2016-11-03 22:49:37 +01001627 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001628 frame->offset += n;
1629 if (frame->offset != 4)
1630 return;
1631 memcpy(&netint, frame->buf, 4);
1632 frame->buf += 4;
1633 frame->offset = 0;
1634 frame->len = ntohl(netint);
1635 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001636
Christopher Fauletf95b1112016-12-21 08:58:16 +01001637 /* Read the frame: frame->buf points on frame part (frame->data+4)*/
1638 n = read(client->fd, frame->buf + frame->offset,
1639 frame->len - frame->offset);
1640 if (n <= 0) {
1641 if (n < 0) {
1642 LOG(client->worker, "Frame to read frame : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001643 goto close;
1644 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001645 return;
1646 }
1647 frame->offset += n;
1648 if (frame->offset != frame->len)
1649 return;
1650 frame->offset = 0;
1651
1652 DEBUG(client->worker, "<%lu> New Frame of %u bytes received",
1653 client->id, frame->len);
1654
1655 switch (client->state) {
1656 case SPOA_ST_CONNECTING:
1657 if (handle_hahello(frame) < 0) {
1658 LOG(client->worker, "Failed to decode HELLO frame");
1659 goto disconnect;
1660 }
1661 prepare_agenthello(frame);
1662 goto write_frame;
1663
1664 case SPOA_ST_PROCESSING:
Christopher Faulet85010352017-02-02 10:14:36 +01001665 if (frame->buf[0] == SPOE_FRM_T_HAPROXY_DISCON) {
Christopher Fauletf95b1112016-12-21 08:58:16 +01001666 client->state = SPOA_ST_DISCONNECTING;
1667 goto disconnecting;
1668 }
Christopher Faulet85010352017-02-02 10:14:36 +01001669 n = handle_hanotify(frame);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001670 if (n < 0) {
Christopher Faulet85010352017-02-02 10:14:36 +01001671 LOG(client->worker, "Failed to decode frame: %s",
1672 spoe_frm_err_reasons[client->status_code]);
1673 goto disconnect;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001674 }
1675 if (n == 0) {
Christopher Faulet85010352017-02-02 10:14:36 +01001676 LOG(client->worker, "Ignore invalid/unknown/aborted frame");
1677 goto ignore_frame;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001678 }
1679 else
1680 goto process_frame;
1681
1682 case SPOA_ST_DISCONNECTING:
1683 disconnecting:
1684 if (handle_hadiscon(frame) < 0) {
1685 LOG(client->worker, "Failed to decode DISCONNECT frame");
1686 goto disconnect;
1687 }
1688 if (client->status_code != SPOE_FRM_ERR_NONE)
1689 LOG(client->worker, "<%lu> Peer closed connection: %s",
1690 client->id, spoe_frm_err_reasons[client->status_code]);
1691 client->status_code = SPOE_FRM_ERR_NONE;
1692 goto disconnect;
1693 }
1694
1695 ignore_frame:
1696 reset_frame(frame);
1697 return;
1698
1699 process_frame:
Christopher Faulet85010352017-02-02 10:14:36 +01001700 if (frame->fragmented == true) {
1701 char *buf;
1702 size_t len = frame->len - frame->offset;
1703
1704 buf = realloc(frame->frag_buf, frame->frag_len + len);
1705 if (buf == NULL) {
1706 client->status_code = SPOE_FRM_ERR_RES;
1707 goto disconnect;
1708 }
1709 memcpy(buf + frame->frag_len, frame->buf + frame->offset, len);
1710 frame->frag_buf = buf;
1711 frame->frag_len += len;
1712
1713 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
1714 /* Wait for next fragments */
1715 frame->buf = (char *)(frame->data);
1716 frame->offset = 0;
1717 frame->len = 0;
1718 frame->flags = 0;
1719 return;
1720 }
1721
1722 frame->buf = frame->frag_buf;
1723 frame->len = frame->frag_len;
1724 frame->offset = 0;
1725 /* fall through */
1726 }
1727
Christopher Fauletf95b1112016-12-21 08:58:16 +01001728 process_incoming_frame(frame);
1729 client->incoming_frame = NULL;
1730 return;
1731
1732 write_frame:
1733 write_frame(client, frame);
1734 client->incoming_frame = NULL;
1735 return;
1736
1737 disconnect:
1738 client->state = SPOA_ST_DISCONNECTING;
1739 if (prepare_agentdicon(frame) < 0) {
1740 LOG(client->worker, "Failed to encode DISCONNECT frame");
1741 goto close;
1742 }
1743 goto write_frame;
1744
1745 close:
1746 release_client(client);
1747}
1748
1749static void
1750write_frame_cb(evutil_socket_t fd, short events, void *arg)
1751{
1752 struct client *client = arg;
1753 struct spoe_frame *frame;
1754 int n;
1755
1756 DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__);
1757 if ((frame = acquire_outgoing_frame(client)) == NULL) {
1758 event_del(&client->write_frame_event);
1759 return;
1760 }
1761
1762 if (frame->buf == (char *)(frame->data)) {
1763 /* Write the frame length: frame->buf points on length part (frame->data) */
1764 n = write(client->fd, frame->buf+frame->offset, 4-frame->offset);
1765 if (n <= 0) {
1766 if (n < 0)
1767 LOG(client->worker, "Failed to write frame length : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001768 goto close;
1769 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001770 frame->offset += n;
1771 if (frame->offset != 4)
1772 return;
1773 frame->buf += 4;
1774 frame->offset = 0;
1775 }
1776
1777 /* Write the frame: frame->buf points on frame part (frame->data+4)*/
1778 n = write(client->fd, frame->buf + frame->offset,
1779 frame->len - frame->offset);
1780 if (n <= 0) {
1781 if (n < 0) {
1782 LOG(client->worker, "Failed to write frame : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001783 goto close;
1784 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001785 return;
1786 }
1787 frame->offset += n;
1788 if (frame->offset != frame->len)
1789 return;
1790
1791 DEBUG(client->worker, "<%lu> Frame of %u bytes send",
1792 client->id, frame->len);
1793
1794 switch (client->state) {
1795 case SPOA_ST_CONNECTING:
1796 if (frame->hcheck == true) {
1797 DEBUG(client->worker,
1798 "<%lu> Close client after healthcheck",
1799 client->id);
1800 goto close;
1801 }
1802 client->state = SPOA_ST_PROCESSING;
1803 break;
1804
1805 case SPOA_ST_PROCESSING:
1806 break;
1807
1808 case SPOA_ST_DISCONNECTING:
1809 goto close;
1810 }
1811
1812 release_frame(frame);
1813 client->outgoing_frame = NULL;
1814 if (!client->async && !client->pipelining) {
1815 event_del(&client->write_frame_event);
1816 event_add(&client->read_frame_event, NULL);
1817 }
1818 return;
1819
1820 close:
1821 release_client(client);
1822}
1823
1824static void
1825accept_cb(int listener, short event, void *arg)
1826{
1827 struct worker *worker;
1828 struct client *client;
1829 int fd;
1830
1831 worker = &workers[clicount++ % num_workers];
1832
1833 if ((fd = accept(listener, NULL, NULL)) < 0) {
1834 if (errno != EAGAIN && errno != EWOULDBLOCK)
1835 LOG(worker, "Failed to accept client connection : %m");
1836 return;
1837 }
1838
1839 DEBUG(&null_worker,
1840 "<%lu> New Client connection accepted and assigned to worker %02d",
1841 clicount, worker->id);
1842
1843 if (evutil_make_socket_nonblocking(fd) < 0) {
1844 LOG(&null_worker, "Failed to set client socket to non-blocking : %m");
1845 close(fd);
1846 return;
1847 }
1848
1849 if ((client = calloc(1, sizeof(*client))) == NULL) {
1850 LOG(&null_worker, "Failed to allocate memory for client state : %m");
1851 close(fd);
1852 return;
1853 }
1854
1855 client->id = clicount;
1856 client->fd = fd;
1857 client->worker = worker;
1858 client->state = SPOA_ST_CONNECTING;
1859 client->status_code = SPOE_FRM_ERR_NONE;
1860 client->max_frame_size = max_frame_size;
1861 client->engine = NULL;
1862 client->pipelining = false;
1863 client->async = false;
1864 client->incoming_frame = NULL;
1865 client->outgoing_frame = NULL;
1866 LIST_INIT(&client->processing_frames);
1867 LIST_INIT(&client->outgoing_frames);
1868
1869 LIST_ADDQ(&worker->clients, &client->by_worker);
1870
1871 worker->nbclients++;
1872
1873 if (event_assign(&client->read_frame_event, worker->base, fd,
1874 EV_READ|EV_PERSIST, read_frame_cb, client) < 0 ||
1875 event_assign(&client->write_frame_event, worker->base, fd,
1876 EV_WRITE|EV_PERSIST, write_frame_cb, client) < 0) {
1877 LOG(&null_worker, "Failed to create client events");
1878 release_client(client);
1879 return;
1880 }
1881 event_add(&client->read_frame_event, NULL);
1882}
1883
1884static void *
1885worker_function(void *data)
1886{
1887 struct client *client, *cback;
1888 struct spoe_frame *frame, *fback;
1889 struct worker *worker = data;
1890
1891 DEBUG(worker, "Worker ready to process client messages");
1892 event_base_dispatch(worker->base);
Christopher Faulet010fded2016-11-03 22:49:37 +01001893
Christopher Fauletf95b1112016-12-21 08:58:16 +01001894 list_for_each_entry_safe(client, cback, &worker->clients, by_worker) {
1895 release_client(client);
Christopher Faulet010fded2016-11-03 22:49:37 +01001896 }
1897
Christopher Fauletf95b1112016-12-21 08:58:16 +01001898 list_for_each_entry_safe(frame, fback, &worker->frames, list) {
1899 LIST_DEL(&frame->list);
1900 free(frame);
1901 }
1902
1903 event_free(worker->monitor_event);
1904 event_base_free(worker->base);
1905 DEBUG(worker, "Worker is stopped");
1906 pthread_exit(&null_worker);
1907}
1908
1909
1910static int
1911parse_processing_delay(const char *str)
1912{
1913 unsigned long value;
1914
1915 value = 0;
1916 while (1) {
1917 unsigned int j;
1918
1919 j = *str - '0';
1920 if (j > 9)
1921 break;
1922 str++;
1923 value *= 10;
1924 value += j;
1925 }
1926
1927 switch (*str) {
1928 case '\0': /* no unit = millisecond */
1929 value *= 1000;
1930 break;
1931 case 's': /* second */
1932 value *= 1000000;
1933 str++;
1934 break;
1935 case 'm': /* millisecond : "ms" */
1936 if (str[1] != 's')
1937 return -1;
1938 value *= 1000;
1939 str += 2;
1940 break;
1941 case 'u': /* microsecond : "us" */
1942 if (str[1] != 's')
1943 return -1;
1944 str += 2;
1945 break;
1946 default:
1947 return -1;
1948 }
1949 if (*str)
1950 return -1;
1951
1952 processing_delay.tv_sec = (time_t)(value / 1000000);
1953 processing_delay.tv_usec = (suseconds_t)(value % 1000000);
1954 return 0;
Christopher Faulet010fded2016-11-03 22:49:37 +01001955}
1956
Christopher Fauletf95b1112016-12-21 08:58:16 +01001957
Christopher Faulet010fded2016-11-03 22:49:37 +01001958static void
1959usage(char *prog)
1960{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001961 fprintf(stderr,
1962 "Usage : %s [OPTION]...\n"
1963 " -h Print this message\n"
1964 " -d Enable the debug mode\n"
1965 " -m <max-frame-size> Specify the maximum frame size (default : %u)\n"
1966 " -p <port> Specify the port to listen on (default : %d)\n"
1967 " -n <num-workers> Specify the number of workers (default : %d)\n"
1968 " -c <capability> Enable the support of the specified capability\n"
1969 " -t <time> Set a delay to process a message (default: 0)\n"
1970 " The value is specified in milliseconds by default,\n"
1971 " but can be in any other unit if the number is suffixed\n"
1972 " by a unit (us, ms, s)\n"
1973 "\n"
Christopher Faulet85010352017-02-02 10:14:36 +01001974 " Supported capabilities: fragmentation, pipelining, async\n",
Christopher Fauletf95b1112016-12-21 08:58:16 +01001975 prog, MAX_FRAME_SIZE, DEFAULT_PORT, NUM_WORKERS);
Christopher Faulet010fded2016-11-03 22:49:37 +01001976}
1977
1978int
1979main(int argc, char **argv)
1980{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001981 struct event_base *base = NULL;
1982 struct event *signal_event = NULL, *accept_event = NULL;
1983 int opt, i, fd = -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001984
Christopher Fauletf95b1112016-12-21 08:58:16 +01001985 // TODO: add '-t <processing-time>' option
1986 while ((opt = getopt(argc, argv, "hdm:n:p:c:t:")) != -1) {
Christopher Faulet010fded2016-11-03 22:49:37 +01001987 switch (opt) {
1988 case 'h':
1989 usage(argv[0]);
1990 return EXIT_SUCCESS;
1991 case 'd':
1992 debug = true;
1993 break;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001994 case 'm':
1995 max_frame_size = atoi(optarg);
1996 break;
Christopher Faulet010fded2016-11-03 22:49:37 +01001997 case 'n':
Christopher Fauletf95b1112016-12-21 08:58:16 +01001998 num_workers = atoi(optarg);
Christopher Faulet010fded2016-11-03 22:49:37 +01001999 break;
2000 case 'p':
Christopher Fauletf95b1112016-12-21 08:58:16 +01002001 server_port = atoi(optarg);
Christopher Faulet010fded2016-11-03 22:49:37 +01002002 break;
Christopher Fauletf95b1112016-12-21 08:58:16 +01002003 case 'c':
2004 if (!strcmp(optarg, "pipelining"))
2005 pipelining = true;
2006 else if (!strcmp(optarg, "async"))
2007 async = true;
Christopher Faulet85010352017-02-02 10:14:36 +01002008 else if (!strcmp(optarg, "fragmentation"))
2009 fragmentation = true;
Christopher Fauletf95b1112016-12-21 08:58:16 +01002010 else
2011 fprintf(stderr, "WARNING: unsupported capability '%s'\n", optarg);
2012 break;
2013 case 't':
2014 if (!parse_processing_delay(optarg))
2015 break;
2016 fprintf(stderr, "%s: failed to parse time '%s'.\n", argv[0], optarg);
2017 fprintf(stderr, "Try '%s -h' for more information.\n", argv[0]);
2018 return EXIT_FAILURE;
Christopher Faulet010fded2016-11-03 22:49:37 +01002019 default:
2020 usage(argv[0]);
2021 return EXIT_FAILURE;
2022 }
2023 }
2024
Christopher Fauletf95b1112016-12-21 08:58:16 +01002025 if (num_workers <= 0) {
2026 LOG(&null_worker, "%s : Invalid number of workers '%d'\n",
2027 argv[0], num_workers);
Christopher Faulet010fded2016-11-03 22:49:37 +01002028 goto error;
2029 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01002030
2031 if (server_port <= 0) {
2032 LOG(&null_worker, "%s : Invalid port '%d'\n",
2033 argv[0], server_port);
Christopher Faulet010fded2016-11-03 22:49:37 +01002034 goto error;
2035 }
2036
Christopher Fauletf95b1112016-12-21 08:58:16 +01002037
2038 if (evthread_use_pthreads() < 0) {
2039 LOG(&null_worker, "No pthreads support for libevent");
Christopher Faulet010fded2016-11-03 22:49:37 +01002040 goto error;
2041 }
2042
Christopher Fauletf95b1112016-12-21 08:58:16 +01002043 if ((base = event_base_new()) == NULL) {
2044 LOG(&null_worker, "Failed to initialize libevent : %m");
2045 goto error;
2046 }
Christopher Faulet010fded2016-11-03 22:49:37 +01002047
Christopher Fauletf95b1112016-12-21 08:58:16 +01002048 signal(SIGPIPE, SIG_IGN);
Christopher Faulet010fded2016-11-03 22:49:37 +01002049
Christopher Fauletf95b1112016-12-21 08:58:16 +01002050 if ((fd = create_server_socket()) < 0) {
2051 LOG(&null_worker, "Failed to create server socket");
2052 goto error;
2053 }
2054 if (evutil_make_socket_nonblocking(fd) < 0) {
2055 LOG(&null_worker, "Failed to set server socket to non-blocking");
Christopher Faulet010fded2016-11-03 22:49:37 +01002056 goto error;
2057 }
2058
Christopher Fauletf95b1112016-12-21 08:58:16 +01002059 if ((workers = calloc(num_workers, sizeof(*workers))) == NULL) {
2060 LOG(&null_worker, "Failed to set allocate memory for workers");
Christopher Faulet010fded2016-11-03 22:49:37 +01002061 goto error;
2062 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01002063
2064 for (i = 0; i < num_workers; ++i) {
2065 struct worker *w = &workers[i];
Christopher Faulet010fded2016-11-03 22:49:37 +01002066
Christopher Fauletf95b1112016-12-21 08:58:16 +01002067 w->id = i+1;
2068 w->nbclients = 0;
2069 LIST_INIT(&w->engines);
2070 LIST_INIT(&w->clients);
2071 LIST_INIT(&w->frames);
2072
2073 if ((w->base = event_base_new()) == NULL) {
2074 LOG(&null_worker,
2075 "Failed to initialize libevent for worker %02d : %m",
2076 w->id);
2077 goto error;
2078 }
Christopher Faulet010fded2016-11-03 22:49:37 +01002079
Christopher Fauletf95b1112016-12-21 08:58:16 +01002080 w->monitor_event = event_new(w->base, fd, EV_PERSIST,
2081 worker_monitor_cb, (void *)w);
2082 if (w->monitor_event == NULL ||
2083 event_add(w->monitor_event, (struct timeval[]){{5,0}}) < 0) {
2084 LOG(&null_worker,
2085 "Failed to create monitor event for worker %02d",
2086 w->id);
Christopher Faulet010fded2016-11-03 22:49:37 +01002087 goto error;
2088 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01002089
2090 if (pthread_create(&w->thread, NULL, worker_function, (void *)w)) {
2091 LOG(&null_worker,
2092 "Failed to start thread for worker %02d : %m",
2093 w->id);
2094 }
2095 DEBUG(&null_worker, "Worker %02d initialized", w->id);
2096 }
2097
2098 accept_event = event_new(base, fd, EV_READ|EV_PERSIST, accept_cb,
2099 (void *)base);
2100 if (accept_event == NULL || event_add(accept_event, NULL) < 0) {
2101 LOG(&null_worker, "Failed to create accept event : %m");
2102 }
2103
2104 signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
2105 if (signal_event == NULL || event_add(signal_event, NULL) < 0) {
2106 LOG(&null_worker, "Failed to create signal event : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01002107 }
2108
Christopher Fauletf95b1112016-12-21 08:58:16 +01002109 DEBUG(&null_worker,
2110 "Server is ready"
Christopher Faulet85010352017-02-02 10:14:36 +01002111 " [fragmentation=%s - pipelining=%s - async=%s - debug=%s - max-frame-size=%u]",
2112 (fragmentation?"true":"false"), (pipelining?"true":"false"), (async?"true":"false"),
Christopher Fauletf95b1112016-12-21 08:58:16 +01002113 (debug?"true":"false"), max_frame_size);
2114 event_base_dispatch(base);
2115
2116 for (i = 0; i < num_workers; i++) {
2117 struct worker *w = &workers[i];
2118
2119 pthread_join(w->thread, NULL);
2120 DEBUG(&null_worker, "Worker %02d terminated", w->id);
Christopher Faulet010fded2016-11-03 22:49:37 +01002121 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01002122
2123 free(workers);
2124 event_free(signal_event);
2125 event_free(accept_event);
2126 event_base_free(base);
2127 close(fd);
Christopher Faulet010fded2016-11-03 22:49:37 +01002128 return EXIT_SUCCESS;
Christopher Fauletf95b1112016-12-21 08:58:16 +01002129
2130 error:
2131 if (workers != NULL)
2132 free(workers);
2133 if (signal_event != NULL)
2134 event_free(signal_event);
2135 if (accept_event != NULL)
2136 event_free(accept_event);
2137 if (base != NULL)
2138 event_base_free(base);
2139 if (fd != -1)
2140 close(fd);
Christopher Faulet010fded2016-11-03 22:49:37 +01002141 return EXIT_FAILURE;
2142}