blob: e2459844adadbf0b4caea665b3011a0ced60e29d [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
Christopher Faulet4ff3e572017-02-24 14:31:11 +010039#include <proto/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 Fauletf95b1112016-12-21 08:58:16 +0100155/* Globals */
156static struct worker *workers = NULL;
157static struct worker null_worker = { .id = 0 };
158static unsigned long clicount = 0;
159static int server_port = DEFAULT_PORT;
160static int num_workers = NUM_WORKERS;
161static unsigned int max_frame_size = MAX_FRAME_SIZE;
162struct timeval processing_delay = {0, 0};
163static bool debug = false;
164static bool pipelining = false;
165static bool async = false;
Christopher Faulet85010352017-02-02 10:14:36 +0100166static bool fragmentation = false;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100167
168
169static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Faulet85010352017-02-02 10:14:36 +0100170 [SPOE_FRM_ERR_NONE] = "normal",
171 [SPOE_FRM_ERR_IO] = "I/O error",
172 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
173 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
174 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
175 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
176 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
177 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
178 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
179 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
180 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
181 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100182 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Faulet85010352017-02-02 10:14:36 +0100183 [SPOE_FRM_ERR_RES] = "resource allocation error",
184 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf95b1112016-12-21 08:58:16 +0100185};
186
187static void signal_cb(evutil_socket_t, short, void *);
188static void accept_cb(evutil_socket_t, short, void *);
189static void worker_monitor_cb(evutil_socket_t, short, void *);
190static void process_frame_cb(evutil_socket_t, short, void *);
191static void read_frame_cb(evutil_socket_t, short, void *);
192static void write_frame_cb(evutil_socket_t, short, void *);
193
194static void use_spoe_engine(struct client *);
195static void unuse_spoe_engine(struct client *);
196static void release_frame(struct spoe_frame *);
197static void release_client(struct client *);
Christopher Faulet010fded2016-11-03 22:49:37 +0100198
199static void
Christopher Fauletf95b1112016-12-21 08:58:16 +0100200check_ipv4_reputation(struct spoe_frame *frame, struct in_addr *ipv4)
Christopher Faulet010fded2016-11-03 22:49:37 +0100201{
202 char str[INET_ADDRSTRLEN];
203
204 if (inet_ntop(AF_INET, ipv4, str, INET_ADDRSTRLEN) == NULL)
205 return;
206
Christopher Fauletf95b1112016-12-21 08:58:16 +0100207 frame->ip_score = random() % 100;
Christopher Faulet010fded2016-11-03 22:49:37 +0100208
Christopher Fauletf95b1112016-12-21 08:58:16 +0100209 DEBUG(frame->worker, "IP score for %.*s is %d",
210 INET_ADDRSTRLEN, str, frame->ip_score);
Christopher Faulet010fded2016-11-03 22:49:37 +0100211}
212
213static void
Christopher Fauletf95b1112016-12-21 08:58:16 +0100214check_ipv6_reputation(struct spoe_frame *frame, struct in6_addr *ipv6)
Christopher Faulet010fded2016-11-03 22:49:37 +0100215{
216 char str[INET6_ADDRSTRLEN];
217
218 if (inet_ntop(AF_INET6, ipv6, str, INET6_ADDRSTRLEN) == NULL)
219 return;
220
Christopher Fauletf95b1112016-12-21 08:58:16 +0100221 frame->ip_score = random() % 100;
Christopher Faulet010fded2016-11-03 22:49:37 +0100222
Christopher Fauletf95b1112016-12-21 08:58:16 +0100223 DEBUG(frame->worker, "IP score for %.*s is %d",
224 INET6_ADDRSTRLEN, str, frame->ip_score);
Christopher Faulet010fded2016-11-03 22:49:37 +0100225}
226
Christopher Faulet010fded2016-11-03 22:49:37 +0100227
228/* Check the protocol version. It returns -1 if an error occurred, the number of
229 * read bytes otherwise. */
230static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100231check_proto_version(struct spoe_frame *frame, char **buf, char *end)
Christopher Faulet010fded2016-11-03 22:49:37 +0100232{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100233 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100234 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100235 int ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100236
237 /* Get the list of all supported versions by HAProxy */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100238 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Faulet010fded2016-11-03 22:49:37 +0100239 return -1;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100240 ret = spoe_decode_buffer(&p, end, &str, &sz);
241 if (ret == -1 || !str)
Christopher Faulet010fded2016-11-03 22:49:37 +0100242 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100243
244 DEBUG(frame->worker, "<%lu> Supported versions : %.*s",
245 frame->client->id, (int)sz, str);
Christopher Faulet010fded2016-11-03 22:49:37 +0100246
247 /* TODO: Find the right verion in supported ones */
248
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100249 ret = (p - *buf);
250 *buf = p;
251 return ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100252}
253
254/* Check max frame size value. It returns -1 if an error occurred, the number of
255 * read bytes otherwise. */
256static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100257check_max_frame_size(struct spoe_frame *frame, char **buf, char *end)
Christopher Faulet010fded2016-11-03 22:49:37 +0100258{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100259 char *p = *buf;
Christopher Faulet010fded2016-11-03 22:49:37 +0100260 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100261 int type, ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100262
263 /* Get the max-frame-size value of HAProxy */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100264 type = *p++;
Christopher Faulet010fded2016-11-03 22:49:37 +0100265 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
266 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
267 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
Christopher Fauletf95b1112016-12-21 08:58:16 +0100268 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64)
Christopher Faulet010fded2016-11-03 22:49:37 +0100269 return -1;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100270 if (spoe_decode_varint(&p, end, &sz) == -1)
Christopher Faulet010fded2016-11-03 22:49:37 +0100271 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100272
273 /* Keep the lower value */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100274 if (sz < frame->client->max_frame_size)
275 frame->client->max_frame_size = sz;
276
277 DEBUG(frame->worker, "<%lu> HAProxy maximum frame size : %u",
278 frame->client->id, (unsigned int)sz);
Christopher Faulet010fded2016-11-03 22:49:37 +0100279
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100280 ret = (p - *buf);
281 *buf = p;
282 return ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100283}
284
Christopher Fauletba7bc162016-11-07 21:07:38 +0100285/* Check healthcheck value. It returns -1 if an error occurred, the number of
286 * read bytes otherwise. */
287static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100288check_healthcheck(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100289{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100290 char *p = *buf;
291 int type, ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100292
Christopher Fauletf95b1112016-12-21 08:58:16 +0100293 /* Get the "healthcheck" value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100294 type = *p++;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100295 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_BOOL)
296 return -1;
297 frame->hcheck = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE);
298
299 DEBUG(frame->worker, "<%lu> HELLO healthcheck : %s",
300 frame->client->id, (frame->hcheck ? "true" : "false"));
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100301
302 ret = (p - *buf);
303 *buf = p;
304 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100305}
306
307/* Check capabilities value. It returns -1 if an error occurred, the number of
308 * read bytes otherwise. */
309static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100310check_capabilities(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100311{
312 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100313 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100314 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100315 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100316
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100317 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100318 return -1;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100319 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
320 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100321 if (str == NULL) /* this is not an error */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100322 goto end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100323
324 DEBUG(frame->worker, "<%lu> HAProxy capabilities : %.*s",
325 client->id, (int)sz, str);
326
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100327 while (sz) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100328 char *delim;
329
330 /* Skip leading spaces */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100331 for (; isspace(*str) && sz; sz--);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100332
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100333 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
334 str += 10; sz -= 10;
335 if (!sz || isspace(*str) || *str == ',') {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100336 DEBUG(frame->worker,
337 "<%lu> HAProxy supports frame pipelining",
338 client->id);
339 client->pipelining = true;
340 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100341 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100342 else if (sz >= 5 && !strncmp(str, "async", 5)) {
343 str += 5; sz -= 5;
344 if (!sz || isspace(*str) || *str == ',') {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100345 DEBUG(frame->worker,
346 "<%lu> HAProxy supports asynchronous frame",
347 client->id);
348 client->async = true;
349 }
350 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100351 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
352 str += 13; sz -= 13;
353 if (!sz || isspace(*str) || *str == ',') {
Christopher Faulet85010352017-02-02 10:14:36 +0100354 DEBUG(frame->worker,
355 "<%lu> HAProxy supports fragmented frame",
356 client->id);
357 client->fragmentation = true;
358 }
359 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100360
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100361 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100362 break;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100363 delim++;
364 sz -= (delim - str);
365 str = delim;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100366 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100367 end:
368 ret = (p - *buf);
369 *buf = p;
370 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100371}
372
373/* Check engine-id value. It returns -1 if an error occurred, the number of
374 * read bytes otherwise. */
375static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100376check_engine_id(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100377{
378 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100379 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100380 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100381 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100382
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100383 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100384 return -1;
385
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100386 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
387 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100388 if (str == NULL) /* this is not an error */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100389 goto end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100390
391 if (client->engine != NULL)
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100392 goto end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100393
394 DEBUG(frame->worker, "<%lu> HAProxy engine id : %.*s",
395 client->id, (int)sz, str);
396
397 client->engine_id = strndup(str, (int)sz);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100398 end:
399 ret = (p - *buf);
400 *buf = p;
401 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100402}
403
404/* Check disconnect status code. It returns -1 if an error occurred, the number
405 * of read bytes otherwise. */
406static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100407check_discon_status_code(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100408{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100409 char *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100410 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100411 int type, ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100412
413 /* Get the "status-code" value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100414 type = *p++;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100415 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
416 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
417 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
418 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64)
419 return -1;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100420 if (spoe_decode_varint(&p, end, &sz) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100421 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100422
423 frame->client->status_code = (unsigned int)sz;
424
425 DEBUG(frame->worker, "<%lu> Disconnect status code : %u",
426 frame->client->id, frame->client->status_code);
427
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100428 ret = (p - *buf);
429 *buf = p;
430 return ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100431}
432
Christopher Fauletf95b1112016-12-21 08:58:16 +0100433/* Check the disconnect message. It returns -1 if an error occurred, the number
434 * of read bytes otherwise. */
435static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100436check_discon_message(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100437{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100438 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100439 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100440 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100441
442 /* Get the "message" value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100443 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100444 return -1;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100445 ret = spoe_decode_buffer(&p, end, &str, &sz);
446 if (ret == -1 || !str)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100447 return -1;
448
449 DEBUG(frame->worker, "<%lu> Disconnect message : %.*s",
450 frame->client->id, (int)sz, str);
451
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100452 ret = (p - *buf);
453 *buf = p;
454 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100455}
Christopher Fauletba7bc162016-11-07 21:07:38 +0100456
Christopher Faulet010fded2016-11-03 22:49:37 +0100457/* Decode a HELLO frame received from HAProxy. It returns -1 if an error
Christopher Fauletf95b1112016-12-21 08:58:16 +0100458 * occurred, otherwise the number of read bytes. HELLO frame cannot be
459 * ignored and having another frame than a HELLO frame is an error. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100460static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100461handle_hahello(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100462{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100463 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100464 char *p, *end;
465
466 p = frame->buf;
467 end = frame->buf + frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100468
Christopher Fauletf95b1112016-12-21 08:58:16 +0100469 /* Check frame type: we really want a HELLO frame */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100470 if (*p++ != SPOE_FRM_T_HAPROXY_HELLO)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100471 goto error;
472
473 DEBUG(frame->worker, "<%lu> Decode HAProxy HELLO frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100474
Christopher Faulet85010352017-02-02 10:14:36 +0100475 /* Retrieve flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100476 memcpy((char *)&(frame->flags), p, 4);
477 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100478
Christopher Faulet85010352017-02-02 10:14:36 +0100479 /* Fragmentation is not supported for HELLO frame */
480 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
481 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
482 goto error;
483 }
484
Christopher Faulet010fded2016-11-03 22:49:37 +0100485 /* stream-id and frame-id must be cleared */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100486 if (*p != 0 || *(p+1) != 0) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100487 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100488 goto error;
489 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100490 p += 2;
Christopher Faulet010fded2016-11-03 22:49:37 +0100491
492 /* Loop on K/V items */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100493 while (p < end) {
Christopher Faulet010fded2016-11-03 22:49:37 +0100494 char *str;
495 uint64_t sz;
496
497 /* Decode the item name */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100498 spoe_decode_buffer(&p, end, &str, &sz);
499 if (!str) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100500 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100501 goto error;
502 }
503
504 /* Check "supported-versions" K/V item */
505 if (!memcmp(str, "supported-versions", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100506 if (check_proto_version(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100507 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100508 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100509 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100510 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100511 /* Check "max-frame-size" K/V item */
Christopher Faulet010fded2016-11-03 22:49:37 +0100512 else if (!memcmp(str, "max-frame-size", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100513 if (check_max_frame_size(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100514 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100515 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100516 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100517 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100518 /* Check "healthcheck" K/V item */
Christopher Fauletba7bc162016-11-07 21:07:38 +0100519 else if (!memcmp(str, "healthcheck", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100520 if (check_healthcheck(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100521 client->status_code = SPOE_FRM_ERR_INVALID;
522 goto error;
523 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100524 }
525 /* Check "capabilities" K/V item */
526 else if (!memcmp(str, "capabilities", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100527 if (check_capabilities(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100528 client->status_code = SPOE_FRM_ERR_INVALID;
529 goto error;
530 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100531 }
532 /* Check "engine-id" K/V item */
533 else if (!memcmp(str, "engine-id", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100534 if (check_engine_id(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100535 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100536 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100537 }
Christopher Fauletba7bc162016-11-07 21:07:38 +0100538 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100539 else {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100540 DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s",
541 client->id, (int)sz, str);
542
Christopher Faulet010fded2016-11-03 22:49:37 +0100543 /* Silently ignore unknown item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100544 if (spoe_skip_data(&p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100545 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100546 goto error;
547 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100548 }
549 }
550
Christopher Fauletf95b1112016-12-21 08:58:16 +0100551 if (async == false || client->engine_id == NULL)
552 client->async = false;
553 if (pipelining == false)
554 client->pipelining = false;
555
556 if (client->async == true)
557 use_spoe_engine(client);
558
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100559 return (p - frame->buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100560 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100561 return -1;
562}
563
564/* Decode a DISCONNECT frame received from HAProxy. It returns -1 if an error
Christopher Fauletf95b1112016-12-21 08:58:16 +0100565 * occurred, otherwise the number of read bytes. DISCONNECT frame cannot be
566 * ignored and having another frame than a DISCONNECT frame is an error.*/
Christopher Faulet010fded2016-11-03 22:49:37 +0100567static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100568handle_hadiscon(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100569{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100570 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100571 char *p, *end;
572
573 p = frame->buf;
574 end = frame->buf + frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100575
Christopher Fauletf95b1112016-12-21 08:58:16 +0100576 /* Check frame type: we really want a DISCONNECT frame */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100577 if (*p++ != SPOE_FRM_T_HAPROXY_DISCON)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100578 goto error;
579
580 DEBUG(frame->worker, "<%lu> Decode HAProxy DISCONNECT frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100581
Christopher Faulet85010352017-02-02 10:14:36 +0100582 /* Retrieve flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100583 memcpy((char *)&(frame->flags), p, 4);
584 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100585
Christopher Faulet85010352017-02-02 10:14:36 +0100586 /* Fragmentation is not supported for DISCONNECT frame */
587 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
588 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
589 goto error;
590 }
591
Christopher Faulet010fded2016-11-03 22:49:37 +0100592 /* stream-id and frame-id must be cleared */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100593 if (*p != 0 || *(p+1) != 0) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100594 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100595 goto error;
596 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100597 p += 2;
Christopher Faulet010fded2016-11-03 22:49:37 +0100598
Christopher Fauletf95b1112016-12-21 08:58:16 +0100599 client->status_code = SPOE_FRM_ERR_NONE;
600
Christopher Faulet010fded2016-11-03 22:49:37 +0100601 /* Loop on K/V items */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100602 while (p < end) {
Christopher Faulet010fded2016-11-03 22:49:37 +0100603 char *str;
604 uint64_t sz;
605
606 /* Decode item key */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100607 spoe_decode_buffer(&p, end, &str, &sz);
608 if (!str) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100609 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100610 goto error;
611 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100612
613 /* Check "status-code" K/V item */
614 if (!memcmp(str, "status-code", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100615 if (check_discon_status_code(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100616 client->status_code = SPOE_FRM_ERR_INVALID;
617 goto error;
618 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100619 }
620 /* Check "message" K/V item */
621 else if (!memcmp(str, "message", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100622 if (check_discon_message(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100623 client->status_code = SPOE_FRM_ERR_INVALID;
624 goto error;
625 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100626 }
627 else {
628 DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s",
629 client->id, (int)sz, str);
630
631 /* Silently ignore unknown item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100632 if (spoe_skip_data(&p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100633 client->status_code = SPOE_FRM_ERR_INVALID;
634 goto error;
635 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100636 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100637 }
638
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100639 return (p - frame->buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100640 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100641 return -1;
642}
643
644/* Decode a NOTIFY frame received from HAProxy. It returns -1 if an error
Christopher Faulet85010352017-02-02 10:14:36 +0100645 * occurred, 0 if it must be must be ignored, otherwise the number of read
646 * bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100647static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100648handle_hanotify(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100649{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100650 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100651 char *p, *end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100652 uint64_t stream_id, frame_id;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100653
654 p = frame->buf;
655 end = frame->buf + frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100656
657 /* Check frame type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100658 if (*p++ != SPOE_FRM_T_HAPROXY_NOTIFY)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100659 goto ignore;
660
661 DEBUG(frame->worker, "<%lu> Decode HAProxy NOTIFY frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100662
Christopher Faulet85010352017-02-02 10:14:36 +0100663 /* Retrieve flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100664 memcpy((char *)&(frame->flags), p, 4);
665 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100666
Christopher Faulet85010352017-02-02 10:14:36 +0100667 /* Fragmentation is not supported for DISCONNECT frame */
668 if (!(frame->flags & SPOE_FRM_FL_FIN) && fragmentation == false) {
669 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
670 goto error;
671 }
672
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100673 /* Read the stream-id and frame-id */
674 if (spoe_decode_varint(&p, end, &stream_id) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100675 goto ignore;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100676 if (spoe_decode_varint(&p, end, &frame_id) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100677 goto ignore;
Christopher Faulet010fded2016-11-03 22:49:37 +0100678
Christopher Faulet85010352017-02-02 10:14:36 +0100679 if (frame->fragmented == true) {
680 if (frame->stream_id != (unsigned int)stream_id ||
681 frame->frame_id != (unsigned int)frame_id) {
682 client->status_code = SPOE_FRM_ERR_INTERLACED_FRAMES;
683 goto error;
684 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100685
Christopher Faulet85010352017-02-02 10:14:36 +0100686 if (frame->flags & SPOE_FRM_FL_ABRT) {
687 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
688 " - Abort processing of a fragmented frame"
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100689 " - frag_len=%u - len=%u - offset=%ld",
Christopher Faulet85010352017-02-02 10:14:36 +0100690 client->id, frame->stream_id, frame->frame_id,
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100691 frame->frag_len, frame->len, p - frame->buf);
Christopher Faulet85010352017-02-02 10:14:36 +0100692 goto ignore;
693 }
694 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
695 " - %s fragment of a fragmented frame received"
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100696 " - frag_len=%u - len=%u - offset=%ld",
Christopher Faulet85010352017-02-02 10:14:36 +0100697 client->id, frame->stream_id, frame->frame_id,
698 (frame->flags & SPOE_FRM_FL_FIN) ? "last" : "next",
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100699 frame->frag_len, frame->len, p - frame->buf);
Christopher Faulet85010352017-02-02 10:14:36 +0100700 }
701 else {
702 frame->stream_id = (unsigned int)stream_id;
703 frame->frame_id = (unsigned int)frame_id;
Christopher Faulet010fded2016-11-03 22:49:37 +0100704
Christopher Faulet85010352017-02-02 10:14:36 +0100705 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
706 " - %s frame received"
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100707 " - frag_len=%u - len=%u - offset=%ld",
Christopher Faulet85010352017-02-02 10:14:36 +0100708 client->id, frame->stream_id, frame->frame_id,
709 (frame->flags & SPOE_FRM_FL_FIN) ? "unfragmented" : "fragmented",
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100710 frame->frag_len, frame->len, p - frame->buf);
Christopher Faulet85010352017-02-02 10:14:36 +0100711
712 frame->fragmented = !(frame->flags & SPOE_FRM_FL_FIN);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100713 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100714
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100715 frame->offset = (p - frame->buf);
716 return frame->offset;
Christopher Faulet010fded2016-11-03 22:49:37 +0100717
Christopher Fauletf95b1112016-12-21 08:58:16 +0100718 ignore:
Christopher Faulet85010352017-02-02 10:14:36 +0100719 return 0;
720
721 error:
Christopher Fauletf95b1112016-12-21 08:58:16 +0100722 return -1;
723}
Christopher Faulet010fded2016-11-03 22:49:37 +0100724
Christopher Fauletf95b1112016-12-21 08:58:16 +0100725/* Encode a HELLO frame to send it to HAProxy. It returns the number of written
726 * bytes. */
727static int
728prepare_agenthello(struct spoe_frame *frame)
729{
730 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100731 char *p, *end;
Christopher Faulet85010352017-02-02 10:14:36 +0100732 char capabilities[64];
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100733 int n;
Christopher Faulet85010352017-02-02 10:14:36 +0100734 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet010fded2016-11-03 22:49:37 +0100735
Christopher Fauletf95b1112016-12-21 08:58:16 +0100736 DEBUG(frame->worker, "<%lu> Encode Agent HELLO frame", client->id);
737 frame->type = SPOA_FRM_T_AGENT;
Christopher Faulet010fded2016-11-03 22:49:37 +0100738
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100739 p = frame->buf;
740 end = frame->buf+max_frame_size;
741
Christopher Faulet010fded2016-11-03 22:49:37 +0100742 /* Frame Type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100743 *p++ = SPOE_FRM_T_AGENT_HELLO;
Christopher Faulet010fded2016-11-03 22:49:37 +0100744
Christopher Faulet85010352017-02-02 10:14:36 +0100745 /* Set flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100746 memcpy(p, (char *)&flags, 4);
747 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100748
749 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100750 *p++ = 0;
751 *p++ = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +0100752
753 /* "version" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100754 spoe_encode_buffer("version", 7, &p, end);
755 *p++ = SPOE_DATA_T_STR;
756 spoe_encode_buffer(SPOP_VERSION, SLEN(SPOP_VERSION), &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100757 DEBUG(frame->worker, "<%lu> Agent version : %s",
758 client->id, SPOP_VERSION);
759
Christopher Faulet010fded2016-11-03 22:49:37 +0100760
761 /* "max-frame-size" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100762 spoe_encode_buffer("max-frame-size", 14, &p ,end);
763 *p++ = SPOE_DATA_T_UINT32;
764 spoe_encode_varint(client->max_frame_size, &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100765 DEBUG(frame->worker, "<%lu> Agent maximum frame size : %u",
766 client->id, client->max_frame_size);
Christopher Faulet010fded2016-11-03 22:49:37 +0100767
768 /* "capabilities" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100769 spoe_encode_buffer("capabilities", 12, &p, end);
770 *p++ = SPOE_DATA_T_STR;
Christopher Faulet85010352017-02-02 10:14:36 +0100771
772 memset(capabilities, 0, sizeof(capabilities));
773 n = 0;
774
775 /* 1. Fragmentation capability ? */
776 if (fragmentation == true) {
777 memcpy(capabilities, "fragmentation", 13);
778 n += 13;
779 }
780 /* 2. Pipelining capability ? */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100781 if (client->pipelining == true) {
782 if (n) capabilities[n++] = ',';
783 memcpy(capabilities + n, "pipelining", 10);
Christopher Faulet85010352017-02-02 10:14:36 +0100784 n += 10;
785 }
786 /* 3. Async capability ? */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100787 if (client->async == true) {
788 if (n) capabilities[n++] = ',';
789 memcpy(capabilities + n, "async", 5);
Christopher Faulet85010352017-02-02 10:14:36 +0100790 n += 5;
791 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100792 spoe_encode_buffer(capabilities, n, &p, end);
Christopher Faulet010fded2016-11-03 22:49:37 +0100793
Christopher Faulet85010352017-02-02 10:14:36 +0100794 DEBUG(frame->worker, "<%lu> Agent capabilities : %.*s",
795 client->id, n, capabilities);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100796
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100797 frame->len = (p - frame->buf);
798 return frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100799}
800
Christopher Fauletf95b1112016-12-21 08:58:16 +0100801/* Encode a DISCONNECT frame to send it to HAProxy. It returns the number of
802 * written bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100803static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100804prepare_agentdicon(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100805{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100806 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100807 char *p, *end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100808 const char *reason;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100809 int rlen;
Christopher Faulet85010352017-02-02 10:14:36 +0100810 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100811
812 DEBUG(frame->worker, "<%lu> Encode Agent DISCONNECT frame", client->id);
813 frame->type = SPOA_FRM_T_AGENT;
814
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100815 p = frame->buf;
816 end = frame->buf+max_frame_size;
817
Christopher Fauletf95b1112016-12-21 08:58:16 +0100818 if (client->status_code >= SPOE_FRM_ERRS)
819 client->status_code = SPOE_FRM_ERR_UNKNOWN;
820 reason = spoe_frm_err_reasons[client->status_code];
821 rlen = strlen(reason);
Christopher Faulet010fded2016-11-03 22:49:37 +0100822
823 /* Frame type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100824 *p++ = SPOE_FRM_T_AGENT_DISCON;
Christopher Faulet010fded2016-11-03 22:49:37 +0100825
Christopher Faulet85010352017-02-02 10:14:36 +0100826 /* Set flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100827 memcpy(p, (char *)&flags, 4);
828 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100829
Christopher Fauletf95b1112016-12-21 08:58:16 +0100830 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100831 *p++ = 0;
832 *p++ = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100833
834 /* There are 2 mandatory items: "status-code" and "message" */
835
836 /* "status-code" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100837 spoe_encode_buffer("status-code", 11, &p, end);
838 *p++ = SPOE_DATA_T_UINT32;
839 spoe_encode_varint(client->status_code, &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100840 DEBUG(frame->worker, "<%lu> Disconnect status code : %u",
841 client->id, client->status_code);
Christopher Faulet010fded2016-11-03 22:49:37 +0100842
Christopher Fauletf95b1112016-12-21 08:58:16 +0100843 /* "message" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100844 spoe_encode_buffer("message", 7, &p, end);
845 *p++ = SPOE_DATA_T_STR;
846 spoe_encode_buffer(reason, rlen, &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100847 DEBUG(frame->worker, "<%lu> Disconnect message : %s",
848 client->id, reason);
Christopher Faulet010fded2016-11-03 22:49:37 +0100849
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100850 frame->len = (p - frame->buf);
851 return frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100852}
853
Christopher Fauletf95b1112016-12-21 08:58:16 +0100854/* Encode a ACK frame to send it to HAProxy. It returns the number of written
855 * bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100856static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100857prepare_agentack(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100858{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100859 char *p, *end;
Christopher Faulet85010352017-02-02 10:14:36 +0100860 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet010fded2016-11-03 22:49:37 +0100861
Christopher Fauletf95b1112016-12-21 08:58:16 +0100862 /* Be careful here, in async mode, frame->client can be NULL */
863
864 DEBUG(frame->worker, "Encode Agent ACK frame");
865 frame->type = SPOA_FRM_T_AGENT;
Christopher Faulet010fded2016-11-03 22:49:37 +0100866
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100867 p = frame->buf;
868 end = frame->buf+max_frame_size;
869
Christopher Faulet010fded2016-11-03 22:49:37 +0100870 /* Frame type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100871 *p++ = SPOE_FRM_T_AGENT_ACK;
Christopher Faulet010fded2016-11-03 22:49:37 +0100872
Christopher Faulet85010352017-02-02 10:14:36 +0100873 /* Set flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100874 memcpy(p, (char *)&flags, 4);
875 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100876
Christopher Fauletf95b1112016-12-21 08:58:16 +0100877 /* Set stream-id and frame-id for ACK frames */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100878 spoe_encode_varint(frame->stream_id, &p, end);
879 spoe_encode_varint(frame->frame_id, &p, end);
Christopher Faulet010fded2016-11-03 22:49:37 +0100880
Christopher Fauletf95b1112016-12-21 08:58:16 +0100881 DEBUG(frame->worker, "STREAM-ID=%u - FRAME-ID=%u",
882 frame->stream_id, frame->frame_id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100883
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100884 frame->len = (p - frame->buf);
885 return frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100886}
887
888static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100889create_server_socket(void)
Christopher Faulet010fded2016-11-03 22:49:37 +0100890{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100891 struct sockaddr_in listen_addr;
892 int fd, yes = 1;
893
894 fd = socket(AF_INET, SOCK_STREAM, 0);
895 if (fd < 0) {
896 LOG(&null_worker, "Failed to create service socket : %m");
897 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100898 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100899
900 memset(&listen_addr, 0, sizeof(listen_addr));
901 listen_addr.sin_family = AF_INET;
902 listen_addr.sin_addr.s_addr = INADDR_ANY;
903 listen_addr.sin_port = htons(server_port);
904
905 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0 ||
906 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) < 0) {
907 LOG(&null_worker, "Failed to set option on server socket : %m");
908 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100909 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100910
911 if (bind(fd, (struct sockaddr *) &listen_addr, sizeof(listen_addr)) < 0) {
912 LOG(&null_worker, "Failed to bind server socket : %m");
913 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100914 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100915
916 if (listen(fd, CONNECTION_BACKLOG) < 0) {
917 LOG(&null_worker, "Failed to listen on server socket : %m");
918 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100919 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100920
921 return fd;
Christopher Faulet010fded2016-11-03 22:49:37 +0100922}
923
Christopher Fauletf95b1112016-12-21 08:58:16 +0100924static void
925release_frame(struct spoe_frame *frame)
926{
927 struct worker *worker;
928
929 if (frame == NULL)
930 return;
931
932 if (event_pending(&frame->process_frame_event, EV_TIMEOUT, NULL))
933 event_del(&frame->process_frame_event);
934
935 worker = frame->worker;
936 LIST_DEL(&frame->list);
Christopher Faulet85010352017-02-02 10:14:36 +0100937 if (frame->frag_buf)
938 free(frame->frag_buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100939 memset(frame, 0, sizeof(*frame)+max_frame_size+4);
940 LIST_ADDQ(&worker->frames, &frame->list);
941}
942
943static void
944release_client(struct client *c)
Christopher Faulet010fded2016-11-03 22:49:37 +0100945{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100946 struct spoe_frame *frame, *back;
947
948 if (c == NULL)
949 return;
950
951 DEBUG(c->worker, "<%lu> Release client", c->id);
952
953 LIST_DEL(&c->by_worker);
954 c->worker->nbclients--;
955
956 unuse_spoe_engine(c);
957 free(c->engine_id);
958
959 if (event_pending(&c->read_frame_event, EV_READ, NULL))
960 event_del(&c->read_frame_event);
961 if (event_pending(&c->write_frame_event, EV_WRITE, NULL))
962 event_del(&c->write_frame_event);
963
964 release_frame(c->incoming_frame);
965 release_frame(c->outgoing_frame);
966 list_for_each_entry_safe(frame, back, &c->processing_frames, list) {
967 release_frame(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +0100968 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100969 list_for_each_entry_safe(frame, back, &c->outgoing_frames, list) {
970 release_frame(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +0100971 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100972
973 if (c->fd >= 0)
974 close(c->fd);
975
976 free(c);
977}
978
979static void
980reset_frame(struct spoe_frame *frame)
981{
982 if (frame == NULL)
983 return;
984
Christopher Faulet85010352017-02-02 10:14:36 +0100985 if (frame->frag_buf)
986 free(frame->frag_buf);
987
988 frame->type = SPOA_FRM_T_UNKNOWN;
989 frame->buf = (char *)(frame->data);
990 frame->offset = 0;
991 frame->len = 0;
992 frame->stream_id = 0;
993 frame->frame_id = 0;
994 frame->flags = 0;
995 frame->hcheck = false;
996 frame->fragmented = false;
997 frame->ip_score = -1;
998 frame->frag_buf = NULL;
999 frame->frag_len = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001000 LIST_INIT(&frame->list);
1001}
1002
1003static void
1004use_spoe_engine(struct client *client)
1005{
1006 struct spoe_engine *eng;
1007
1008 if (client->engine_id == NULL)
1009 return;
1010
1011 list_for_each_entry(eng, &client->worker->engines, list) {
1012 if (!strcmp(eng->id, client->engine_id))
1013 goto end;
1014 }
1015
1016 if ((eng = malloc(sizeof(*eng))) == NULL) {
1017 client->async = false;
1018 return;
1019 }
1020
1021 eng->id = strdup(client->engine_id);
1022 LIST_INIT(&eng->clients);
1023 LIST_INIT(&eng->processing_frames);
1024 LIST_INIT(&eng->outgoing_frames);
1025 LIST_ADDQ(&client->worker->engines, &eng->list);
1026 LOG(client->worker, "Add new SPOE engine '%s'", eng->id);
1027
1028 end:
1029 client->engine = eng;
1030 LIST_ADDQ(&eng->clients, &client->by_engine);
1031}
1032
1033static void
1034unuse_spoe_engine(struct client *client)
1035{
1036 struct spoe_engine *eng;
1037 struct spoe_frame *frame, *back;
1038
1039 if (client == NULL || client->engine == NULL)
1040 return;
1041
1042 eng = client->engine;
1043 client->engine = NULL;
1044 LIST_DEL(&client->by_engine);
1045 if (!LIST_ISEMPTY(&eng->clients))
1046 return;
1047
1048 LOG(client->worker, "Remove SPOE engine '%s'", eng->id);
1049 LIST_DEL(&eng->list);
1050
1051 list_for_each_entry_safe(frame, back, &eng->processing_frames, list) {
1052 release_frame(frame);
1053 }
1054 list_for_each_entry_safe(frame, back, &eng->outgoing_frames, list) {
1055 release_frame(frame);
1056 }
1057 free(eng->id);
1058 free(eng);
1059}
1060
1061
1062static struct spoe_frame *
1063acquire_incoming_frame(struct client *client)
1064{
1065 struct spoe_frame *frame;
1066
1067 frame = client->incoming_frame;
1068 if (frame != NULL)
1069 return frame;
1070
1071 if (LIST_ISEMPTY(&client->worker->frames)) {
1072 if ((frame = calloc(1, sizeof(*frame)+max_frame_size+4)) == NULL) {
1073 LOG(client->worker, "Failed to allocate new frame : %m");
1074 return NULL;
1075 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001076 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001077 else {
1078 frame = LIST_NEXT(&client->worker->frames, typeof(frame), list);
1079 LIST_DEL(&frame->list);
Christopher Faulet010fded2016-11-03 22:49:37 +01001080 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001081
1082 reset_frame(frame);
1083 frame->worker = client->worker;
1084 frame->engine = client->engine;
1085 frame->client = client;
1086
1087 if (event_assign(&frame->process_frame_event, client->worker->base, -1,
1088 EV_TIMEOUT|EV_PERSIST, process_frame_cb, frame) < 0) {
1089 LOG(client->worker, "Failed to create frame event");
1090 return NULL;
Christopher Faulet010fded2016-11-03 22:49:37 +01001091 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001092
1093 client->incoming_frame = frame;
1094 return frame;
Christopher Faulet010fded2016-11-03 22:49:37 +01001095}
1096
Christopher Fauletf95b1112016-12-21 08:58:16 +01001097static struct spoe_frame *
1098acquire_outgoing_frame(struct client *client)
Christopher Faulet010fded2016-11-03 22:49:37 +01001099{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001100 struct spoe_engine *engine = client->engine;
1101 struct spoe_frame *frame = NULL;
Christopher Faulet010fded2016-11-03 22:49:37 +01001102
Christopher Fauletf95b1112016-12-21 08:58:16 +01001103 if (client->outgoing_frame != NULL)
1104 frame = client->outgoing_frame;
1105 else if (!LIST_ISEMPTY(&client->outgoing_frames)) {
1106 frame = LIST_NEXT(&client->outgoing_frames, typeof(frame), list);
1107 LIST_DEL(&frame->list);
1108 client->outgoing_frame = frame;
1109 }
1110 else if (engine!= NULL && !LIST_ISEMPTY(&engine->outgoing_frames)) {
1111 frame = LIST_NEXT(&engine->outgoing_frames, typeof(frame), list);
1112 LIST_DEL(&frame->list);
1113 client->outgoing_frame = frame;
1114 }
1115 return frame;
1116}
1117
1118static void
1119write_frame(struct client *client, struct spoe_frame *frame)
1120{
1121 uint32_t netint;
1122
1123 LIST_DEL(&frame->list);
1124
1125 frame->buf = (char *)(frame->data);
1126 frame->offset = 0;
1127 netint = htonl(frame->len);
1128 memcpy(frame->buf, &netint, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001129
Christopher Fauletf95b1112016-12-21 08:58:16 +01001130 if (client != NULL) { /* HELLO or DISCONNECT frames */
1131 event_add(&client->write_frame_event, NULL);
Christopher Faulet010fded2016-11-03 22:49:37 +01001132
Christopher Fauletf95b1112016-12-21 08:58:16 +01001133 /* Try to process the frame as soon as possible, and always
1134 * attach it to the client */
1135 if (client->async || client->pipelining) {
1136 if (client->outgoing_frame == NULL)
1137 client->outgoing_frame = frame;
1138 else
1139 LIST_ADD(&client->outgoing_frames, &frame->list);
1140 }
1141 else {
1142 client->outgoing_frame = frame;
1143 event_del(&client->read_frame_event);
Christopher Faulet010fded2016-11-03 22:49:37 +01001144 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001145 }
1146 else { /* for all other frames */
1147 if (frame->client == NULL) { /* async mode ! */
1148 LIST_ADDQ(&frame->engine->outgoing_frames, &frame->list);
1149 list_for_each_entry(client, &frame->engine->clients, by_engine)
1150 event_add(&client->write_frame_event, NULL);
1151 }
1152 else if (frame->client->pipelining) {
1153 LIST_ADDQ(&frame->client->outgoing_frames, &frame->list);
1154 event_add(&frame->client->write_frame_event, NULL);
1155 }
1156 else {
1157 frame->client->outgoing_frame = frame;
1158 event_add(&frame->client->write_frame_event, NULL);
1159 event_del(&frame->client->read_frame_event);
1160 }
1161 }
1162}
Christopher Faulet010fded2016-11-03 22:49:37 +01001163
Christopher Fauletf95b1112016-12-21 08:58:16 +01001164static void
1165process_incoming_frame(struct spoe_frame *frame)
1166{
1167 struct client *client = frame->client;
Christopher Faulet010fded2016-11-03 22:49:37 +01001168
Christopher Fauletf95b1112016-12-21 08:58:16 +01001169 if (event_add(&frame->process_frame_event, &processing_delay) < 0) {
1170 LOG(client->worker, "Failed to process incoming frame");
1171 release_frame(frame);
1172 return;
1173 }
1174
1175 if (client->async) {
1176 frame->client = NULL;
1177 LIST_ADDQ(&frame->engine->processing_frames, &frame->list);
1178 }
1179 else if (client->pipelining)
1180 LIST_ADDQ(&client->processing_frames, &frame->list);
1181 else
1182 event_del(&client->read_frame_event);
1183}
1184
1185static void
1186signal_cb(evutil_socket_t sig, short events, void *user_data)
1187{
1188 struct event_base *base = user_data;
1189 int i;
1190
1191 DEBUG(&null_worker, "Stopping the server");
1192
1193 event_base_loopbreak(base);
1194 DEBUG(&null_worker, "Main event loop stopped");
1195
1196 for (i = 0; i < num_workers; i++) {
1197 event_base_loopbreak(workers[i].base);
1198 DEBUG(&null_worker, "Event loop stopped for worker %02d",
1199 workers[i].id);
1200 }
1201}
1202
1203static void
1204worker_monitor_cb(evutil_socket_t fd, short events, void *arg)
1205{
1206 struct worker *worker = arg;
1207
1208 LOG(worker, "%u clients connected", worker->nbclients);
1209}
1210
1211static void
1212process_frame_cb(evutil_socket_t fd, short events, void *arg)
1213{
1214 struct spoe_frame *frame = arg;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001215 char *p, *end;
1216 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001217
1218 DEBUG(frame->worker,
Christopher Faulet85010352017-02-02 10:14:36 +01001219 "Process frame messages : STREAM-ID=%u - FRAME-ID=%u - length=%u bytes",
1220 frame->stream_id, frame->frame_id, frame->len - frame->offset);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001221
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001222 p = frame->buf + frame->offset;
1223 end = frame->buf + frame->len;
1224
Christopher Fauletf95b1112016-12-21 08:58:16 +01001225 /* Loop on messages */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001226 while (p < end) {
Christopher Fauletf95b1112016-12-21 08:58:16 +01001227 char *str;
1228 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001229 int nbargs;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001230
1231 /* Decode the message name */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001232 spoe_decode_buffer(&p, end, &str, &sz);
1233 if (!str)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001234 goto stop_processing;
1235
1236 DEBUG(frame->worker, "Process SPOE Message '%.*s'", (int)sz, str);
1237
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001238 nbargs = *p++; /* Get the number of arguments */
1239 frame->offset = (p - frame->buf); /* Save index to handle errors and skip args */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001240 if (!memcmp(str, "check-client-ip", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001241 struct sample smp;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001242
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001243 memset(&smp, 0, sizeof(smp));
Christopher Fauletf95b1112016-12-21 08:58:16 +01001244
1245 if (nbargs != 1)
1246 goto skip_message;
1247
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001248 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001249 goto stop_processing;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001250 if (spoe_decode_data(&p, end, &smp) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001251 goto skip_message;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001252
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001253 if (smp.data.type == SMP_T_IPV4)
1254 check_ipv4_reputation(frame, &smp.data.u.ipv4);
1255 if (smp.data.type == SMP_T_IPV6)
1256 check_ipv6_reputation(frame, &smp.data.u.ipv6);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001257 }
1258 else {
1259 skip_message:
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001260 p = frame->buf + frame->offset; /* Restore index */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001261
1262 while (nbargs-- > 0) {
1263 /* Silently ignore argument: its name and its value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001264 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001265 goto stop_processing;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001266 if (spoe_skip_data(&p, end) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001267 goto stop_processing;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001268 }
1269 }
1270 }
1271
1272 stop_processing:
1273 /* Prepare agent ACK frame */
Christopher Faulet85010352017-02-02 10:14:36 +01001274 frame->buf = (char *)(frame->data) + 4;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001275 frame->offset = 0;
Christopher Faulet85010352017-02-02 10:14:36 +01001276 frame->len = 0;
1277 frame->flags = 0;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001278
1279 ret = prepare_agentack(frame);
1280 p = frame->buf + ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001281
1282 if (frame->ip_score != -1) {
1283 DEBUG(frame->worker, "Add action : set variable ip_scode=%u",
1284 frame->ip_score);
1285
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001286 *p++ = SPOE_ACT_T_SET_VAR; /* Action type */
1287 *p++ = 3; /* Number of args */
1288 *p++ = SPOE_SCOPE_SESS; /* Arg 1: the scope */
1289 spoe_encode_buffer("ip_score", 8, &p, end); /* Arg 2: variable name */
1290 *p++ = SPOE_DATA_T_UINT32;
1291 spoe_encode_varint(frame->ip_score, &p, end); /* Arg 3: variable value */
1292 frame->len = (p - frame->buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001293 }
1294 write_frame(NULL, frame);
1295}
1296
1297static void
1298read_frame_cb(evutil_socket_t fd, short events, void *arg)
1299{
1300 struct client *client = arg;
1301 struct spoe_frame *frame;
1302 uint32_t netint;
1303 int n;
1304
1305 DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__);
1306 if ((frame = acquire_incoming_frame(client)) == NULL)
1307 goto close;
1308
1309 frame->type = SPOA_FRM_T_HAPROXY;
1310 if (frame->buf == (char *)(frame->data)) {
1311 /* Read the frame length: frame->buf points on length part (frame->data) */
1312 n = read(client->fd, frame->buf+frame->offset, 4-frame->offset);
1313 if (n <= 0) {
1314 if (n < 0)
1315 LOG(client->worker, "Failed to read frame length : %m");
Christopher Fauletba7bc162016-11-07 21:07:38 +01001316 goto close;
Christopher Faulet010fded2016-11-03 22:49:37 +01001317 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001318 frame->offset += n;
1319 if (frame->offset != 4)
1320 return;
1321 memcpy(&netint, frame->buf, 4);
1322 frame->buf += 4;
1323 frame->offset = 0;
1324 frame->len = ntohl(netint);
1325 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001326
Christopher Fauletf95b1112016-12-21 08:58:16 +01001327 /* Read the frame: frame->buf points on frame part (frame->data+4)*/
1328 n = read(client->fd, frame->buf + frame->offset,
1329 frame->len - frame->offset);
1330 if (n <= 0) {
1331 if (n < 0) {
1332 LOG(client->worker, "Frame to read frame : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001333 goto close;
1334 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001335 return;
1336 }
1337 frame->offset += n;
1338 if (frame->offset != frame->len)
1339 return;
1340 frame->offset = 0;
1341
1342 DEBUG(client->worker, "<%lu> New Frame of %u bytes received",
1343 client->id, frame->len);
1344
1345 switch (client->state) {
1346 case SPOA_ST_CONNECTING:
1347 if (handle_hahello(frame) < 0) {
1348 LOG(client->worker, "Failed to decode HELLO frame");
1349 goto disconnect;
1350 }
1351 prepare_agenthello(frame);
1352 goto write_frame;
1353
1354 case SPOA_ST_PROCESSING:
Christopher Faulet85010352017-02-02 10:14:36 +01001355 if (frame->buf[0] == SPOE_FRM_T_HAPROXY_DISCON) {
Christopher Fauletf95b1112016-12-21 08:58:16 +01001356 client->state = SPOA_ST_DISCONNECTING;
1357 goto disconnecting;
1358 }
Christopher Faulet85010352017-02-02 10:14:36 +01001359 n = handle_hanotify(frame);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001360 if (n < 0) {
Christopher Faulet85010352017-02-02 10:14:36 +01001361 LOG(client->worker, "Failed to decode frame: %s",
1362 spoe_frm_err_reasons[client->status_code]);
1363 goto disconnect;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001364 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001365 else if (n == 0) {
Christopher Faulet85010352017-02-02 10:14:36 +01001366 LOG(client->worker, "Ignore invalid/unknown/aborted frame");
1367 goto ignore_frame;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001368 }
1369 else
1370 goto process_frame;
1371
1372 case SPOA_ST_DISCONNECTING:
1373 disconnecting:
1374 if (handle_hadiscon(frame) < 0) {
1375 LOG(client->worker, "Failed to decode DISCONNECT frame");
1376 goto disconnect;
1377 }
1378 if (client->status_code != SPOE_FRM_ERR_NONE)
1379 LOG(client->worker, "<%lu> Peer closed connection: %s",
1380 client->id, spoe_frm_err_reasons[client->status_code]);
1381 client->status_code = SPOE_FRM_ERR_NONE;
1382 goto disconnect;
1383 }
1384
1385 ignore_frame:
1386 reset_frame(frame);
1387 return;
1388
1389 process_frame:
Christopher Faulet85010352017-02-02 10:14:36 +01001390 if (frame->fragmented == true) {
1391 char *buf;
1392 size_t len = frame->len - frame->offset;
1393
1394 buf = realloc(frame->frag_buf, frame->frag_len + len);
1395 if (buf == NULL) {
1396 client->status_code = SPOE_FRM_ERR_RES;
1397 goto disconnect;
1398 }
1399 memcpy(buf + frame->frag_len, frame->buf + frame->offset, len);
1400 frame->frag_buf = buf;
1401 frame->frag_len += len;
1402
1403 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
1404 /* Wait for next fragments */
1405 frame->buf = (char *)(frame->data);
1406 frame->offset = 0;
1407 frame->len = 0;
1408 frame->flags = 0;
1409 return;
1410 }
1411
1412 frame->buf = frame->frag_buf;
1413 frame->len = frame->frag_len;
1414 frame->offset = 0;
1415 /* fall through */
1416 }
1417
Christopher Fauletf95b1112016-12-21 08:58:16 +01001418 process_incoming_frame(frame);
1419 client->incoming_frame = NULL;
1420 return;
1421
1422 write_frame:
1423 write_frame(client, frame);
1424 client->incoming_frame = NULL;
1425 return;
1426
1427 disconnect:
1428 client->state = SPOA_ST_DISCONNECTING;
1429 if (prepare_agentdicon(frame) < 0) {
1430 LOG(client->worker, "Failed to encode DISCONNECT frame");
1431 goto close;
1432 }
1433 goto write_frame;
1434
1435 close:
1436 release_client(client);
1437}
1438
1439static void
1440write_frame_cb(evutil_socket_t fd, short events, void *arg)
1441{
1442 struct client *client = arg;
1443 struct spoe_frame *frame;
1444 int n;
1445
1446 DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__);
1447 if ((frame = acquire_outgoing_frame(client)) == NULL) {
1448 event_del(&client->write_frame_event);
1449 return;
1450 }
1451
1452 if (frame->buf == (char *)(frame->data)) {
1453 /* Write the frame length: frame->buf points on length part (frame->data) */
1454 n = write(client->fd, frame->buf+frame->offset, 4-frame->offset);
1455 if (n <= 0) {
1456 if (n < 0)
1457 LOG(client->worker, "Failed to write frame length : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001458 goto close;
1459 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001460 frame->offset += n;
1461 if (frame->offset != 4)
1462 return;
1463 frame->buf += 4;
1464 frame->offset = 0;
1465 }
1466
1467 /* Write the frame: frame->buf points on frame part (frame->data+4)*/
1468 n = write(client->fd, frame->buf + frame->offset,
1469 frame->len - frame->offset);
1470 if (n <= 0) {
1471 if (n < 0) {
1472 LOG(client->worker, "Failed to write frame : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001473 goto close;
1474 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001475 return;
1476 }
1477 frame->offset += n;
1478 if (frame->offset != frame->len)
1479 return;
1480
1481 DEBUG(client->worker, "<%lu> Frame of %u bytes send",
1482 client->id, frame->len);
1483
1484 switch (client->state) {
1485 case SPOA_ST_CONNECTING:
1486 if (frame->hcheck == true) {
1487 DEBUG(client->worker,
1488 "<%lu> Close client after healthcheck",
1489 client->id);
1490 goto close;
1491 }
1492 client->state = SPOA_ST_PROCESSING;
1493 break;
1494
1495 case SPOA_ST_PROCESSING:
1496 break;
1497
1498 case SPOA_ST_DISCONNECTING:
1499 goto close;
1500 }
1501
1502 release_frame(frame);
1503 client->outgoing_frame = NULL;
1504 if (!client->async && !client->pipelining) {
1505 event_del(&client->write_frame_event);
1506 event_add(&client->read_frame_event, NULL);
1507 }
1508 return;
1509
1510 close:
1511 release_client(client);
1512}
1513
1514static void
1515accept_cb(int listener, short event, void *arg)
1516{
1517 struct worker *worker;
1518 struct client *client;
1519 int fd;
1520
1521 worker = &workers[clicount++ % num_workers];
1522
1523 if ((fd = accept(listener, NULL, NULL)) < 0) {
1524 if (errno != EAGAIN && errno != EWOULDBLOCK)
1525 LOG(worker, "Failed to accept client connection : %m");
1526 return;
1527 }
1528
1529 DEBUG(&null_worker,
1530 "<%lu> New Client connection accepted and assigned to worker %02d",
1531 clicount, worker->id);
1532
1533 if (evutil_make_socket_nonblocking(fd) < 0) {
1534 LOG(&null_worker, "Failed to set client socket to non-blocking : %m");
1535 close(fd);
1536 return;
1537 }
1538
1539 if ((client = calloc(1, sizeof(*client))) == NULL) {
1540 LOG(&null_worker, "Failed to allocate memory for client state : %m");
1541 close(fd);
1542 return;
1543 }
1544
1545 client->id = clicount;
1546 client->fd = fd;
1547 client->worker = worker;
1548 client->state = SPOA_ST_CONNECTING;
1549 client->status_code = SPOE_FRM_ERR_NONE;
1550 client->max_frame_size = max_frame_size;
1551 client->engine = NULL;
1552 client->pipelining = false;
1553 client->async = false;
1554 client->incoming_frame = NULL;
1555 client->outgoing_frame = NULL;
1556 LIST_INIT(&client->processing_frames);
1557 LIST_INIT(&client->outgoing_frames);
1558
1559 LIST_ADDQ(&worker->clients, &client->by_worker);
1560
1561 worker->nbclients++;
1562
1563 if (event_assign(&client->read_frame_event, worker->base, fd,
1564 EV_READ|EV_PERSIST, read_frame_cb, client) < 0 ||
1565 event_assign(&client->write_frame_event, worker->base, fd,
1566 EV_WRITE|EV_PERSIST, write_frame_cb, client) < 0) {
1567 LOG(&null_worker, "Failed to create client events");
1568 release_client(client);
1569 return;
1570 }
1571 event_add(&client->read_frame_event, NULL);
1572}
1573
1574static void *
1575worker_function(void *data)
1576{
1577 struct client *client, *cback;
1578 struct spoe_frame *frame, *fback;
1579 struct worker *worker = data;
1580
1581 DEBUG(worker, "Worker ready to process client messages");
1582 event_base_dispatch(worker->base);
Christopher Faulet010fded2016-11-03 22:49:37 +01001583
Christopher Fauletf95b1112016-12-21 08:58:16 +01001584 list_for_each_entry_safe(client, cback, &worker->clients, by_worker) {
1585 release_client(client);
Christopher Faulet010fded2016-11-03 22:49:37 +01001586 }
1587
Christopher Fauletf95b1112016-12-21 08:58:16 +01001588 list_for_each_entry_safe(frame, fback, &worker->frames, list) {
1589 LIST_DEL(&frame->list);
1590 free(frame);
1591 }
1592
1593 event_free(worker->monitor_event);
1594 event_base_free(worker->base);
1595 DEBUG(worker, "Worker is stopped");
1596 pthread_exit(&null_worker);
1597}
1598
1599
1600static int
1601parse_processing_delay(const char *str)
1602{
1603 unsigned long value;
1604
1605 value = 0;
1606 while (1) {
1607 unsigned int j;
1608
1609 j = *str - '0';
1610 if (j > 9)
1611 break;
1612 str++;
1613 value *= 10;
1614 value += j;
1615 }
1616
1617 switch (*str) {
1618 case '\0': /* no unit = millisecond */
1619 value *= 1000;
1620 break;
1621 case 's': /* second */
1622 value *= 1000000;
1623 str++;
1624 break;
1625 case 'm': /* millisecond : "ms" */
1626 if (str[1] != 's')
1627 return -1;
1628 value *= 1000;
1629 str += 2;
1630 break;
1631 case 'u': /* microsecond : "us" */
1632 if (str[1] != 's')
1633 return -1;
1634 str += 2;
1635 break;
1636 default:
1637 return -1;
1638 }
1639 if (*str)
1640 return -1;
1641
1642 processing_delay.tv_sec = (time_t)(value / 1000000);
1643 processing_delay.tv_usec = (suseconds_t)(value % 1000000);
1644 return 0;
Christopher Faulet010fded2016-11-03 22:49:37 +01001645}
1646
Christopher Fauletf95b1112016-12-21 08:58:16 +01001647
Christopher Faulet010fded2016-11-03 22:49:37 +01001648static void
1649usage(char *prog)
1650{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001651 fprintf(stderr,
1652 "Usage : %s [OPTION]...\n"
1653 " -h Print this message\n"
1654 " -d Enable the debug mode\n"
1655 " -m <max-frame-size> Specify the maximum frame size (default : %u)\n"
1656 " -p <port> Specify the port to listen on (default : %d)\n"
1657 " -n <num-workers> Specify the number of workers (default : %d)\n"
1658 " -c <capability> Enable the support of the specified capability\n"
1659 " -t <time> Set a delay to process a message (default: 0)\n"
1660 " The value is specified in milliseconds by default,\n"
1661 " but can be in any other unit if the number is suffixed\n"
1662 " by a unit (us, ms, s)\n"
1663 "\n"
Christopher Faulet85010352017-02-02 10:14:36 +01001664 " Supported capabilities: fragmentation, pipelining, async\n",
Christopher Fauletf95b1112016-12-21 08:58:16 +01001665 prog, MAX_FRAME_SIZE, DEFAULT_PORT, NUM_WORKERS);
Christopher Faulet010fded2016-11-03 22:49:37 +01001666}
1667
1668int
1669main(int argc, char **argv)
1670{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001671 struct event_base *base = NULL;
1672 struct event *signal_event = NULL, *accept_event = NULL;
1673 int opt, i, fd = -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001674
Christopher Fauletf95b1112016-12-21 08:58:16 +01001675 // TODO: add '-t <processing-time>' option
1676 while ((opt = getopt(argc, argv, "hdm:n:p:c:t:")) != -1) {
Christopher Faulet010fded2016-11-03 22:49:37 +01001677 switch (opt) {
1678 case 'h':
1679 usage(argv[0]);
1680 return EXIT_SUCCESS;
1681 case 'd':
1682 debug = true;
1683 break;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001684 case 'm':
1685 max_frame_size = atoi(optarg);
1686 break;
Christopher Faulet010fded2016-11-03 22:49:37 +01001687 case 'n':
Christopher Fauletf95b1112016-12-21 08:58:16 +01001688 num_workers = atoi(optarg);
Christopher Faulet010fded2016-11-03 22:49:37 +01001689 break;
1690 case 'p':
Christopher Fauletf95b1112016-12-21 08:58:16 +01001691 server_port = atoi(optarg);
Christopher Faulet010fded2016-11-03 22:49:37 +01001692 break;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001693 case 'c':
1694 if (!strcmp(optarg, "pipelining"))
1695 pipelining = true;
1696 else if (!strcmp(optarg, "async"))
1697 async = true;
Christopher Faulet85010352017-02-02 10:14:36 +01001698 else if (!strcmp(optarg, "fragmentation"))
1699 fragmentation = true;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001700 else
1701 fprintf(stderr, "WARNING: unsupported capability '%s'\n", optarg);
1702 break;
1703 case 't':
1704 if (!parse_processing_delay(optarg))
1705 break;
1706 fprintf(stderr, "%s: failed to parse time '%s'.\n", argv[0], optarg);
1707 fprintf(stderr, "Try '%s -h' for more information.\n", argv[0]);
1708 return EXIT_FAILURE;
Christopher Faulet010fded2016-11-03 22:49:37 +01001709 default:
1710 usage(argv[0]);
1711 return EXIT_FAILURE;
1712 }
1713 }
1714
Christopher Fauletf95b1112016-12-21 08:58:16 +01001715 if (num_workers <= 0) {
1716 LOG(&null_worker, "%s : Invalid number of workers '%d'\n",
1717 argv[0], num_workers);
Christopher Faulet010fded2016-11-03 22:49:37 +01001718 goto error;
1719 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001720
1721 if (server_port <= 0) {
1722 LOG(&null_worker, "%s : Invalid port '%d'\n",
1723 argv[0], server_port);
Christopher Faulet010fded2016-11-03 22:49:37 +01001724 goto error;
1725 }
1726
Christopher Fauletf95b1112016-12-21 08:58:16 +01001727
1728 if (evthread_use_pthreads() < 0) {
1729 LOG(&null_worker, "No pthreads support for libevent");
Christopher Faulet010fded2016-11-03 22:49:37 +01001730 goto error;
1731 }
1732
Christopher Fauletf95b1112016-12-21 08:58:16 +01001733 if ((base = event_base_new()) == NULL) {
1734 LOG(&null_worker, "Failed to initialize libevent : %m");
1735 goto error;
1736 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001737
Christopher Fauletf95b1112016-12-21 08:58:16 +01001738 signal(SIGPIPE, SIG_IGN);
Christopher Faulet010fded2016-11-03 22:49:37 +01001739
Christopher Fauletf95b1112016-12-21 08:58:16 +01001740 if ((fd = create_server_socket()) < 0) {
1741 LOG(&null_worker, "Failed to create server socket");
1742 goto error;
1743 }
1744 if (evutil_make_socket_nonblocking(fd) < 0) {
1745 LOG(&null_worker, "Failed to set server socket to non-blocking");
Christopher Faulet010fded2016-11-03 22:49:37 +01001746 goto error;
1747 }
1748
Christopher Fauletf95b1112016-12-21 08:58:16 +01001749 if ((workers = calloc(num_workers, sizeof(*workers))) == NULL) {
1750 LOG(&null_worker, "Failed to set allocate memory for workers");
Christopher Faulet010fded2016-11-03 22:49:37 +01001751 goto error;
1752 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001753
1754 for (i = 0; i < num_workers; ++i) {
1755 struct worker *w = &workers[i];
Christopher Faulet010fded2016-11-03 22:49:37 +01001756
Christopher Fauletf95b1112016-12-21 08:58:16 +01001757 w->id = i+1;
1758 w->nbclients = 0;
1759 LIST_INIT(&w->engines);
1760 LIST_INIT(&w->clients);
1761 LIST_INIT(&w->frames);
1762
1763 if ((w->base = event_base_new()) == NULL) {
1764 LOG(&null_worker,
1765 "Failed to initialize libevent for worker %02d : %m",
1766 w->id);
1767 goto error;
1768 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001769
Christopher Fauletf95b1112016-12-21 08:58:16 +01001770 w->monitor_event = event_new(w->base, fd, EV_PERSIST,
1771 worker_monitor_cb, (void *)w);
1772 if (w->monitor_event == NULL ||
1773 event_add(w->monitor_event, (struct timeval[]){{5,0}}) < 0) {
1774 LOG(&null_worker,
1775 "Failed to create monitor event for worker %02d",
1776 w->id);
Christopher Faulet010fded2016-11-03 22:49:37 +01001777 goto error;
1778 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001779
1780 if (pthread_create(&w->thread, NULL, worker_function, (void *)w)) {
1781 LOG(&null_worker,
1782 "Failed to start thread for worker %02d : %m",
1783 w->id);
1784 }
1785 DEBUG(&null_worker, "Worker %02d initialized", w->id);
1786 }
1787
1788 accept_event = event_new(base, fd, EV_READ|EV_PERSIST, accept_cb,
1789 (void *)base);
1790 if (accept_event == NULL || event_add(accept_event, NULL) < 0) {
1791 LOG(&null_worker, "Failed to create accept event : %m");
1792 }
1793
1794 signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
1795 if (signal_event == NULL || event_add(signal_event, NULL) < 0) {
1796 LOG(&null_worker, "Failed to create signal event : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001797 }
1798
Christopher Fauletf95b1112016-12-21 08:58:16 +01001799 DEBUG(&null_worker,
1800 "Server is ready"
Christopher Faulet85010352017-02-02 10:14:36 +01001801 " [fragmentation=%s - pipelining=%s - async=%s - debug=%s - max-frame-size=%u]",
1802 (fragmentation?"true":"false"), (pipelining?"true":"false"), (async?"true":"false"),
Christopher Fauletf95b1112016-12-21 08:58:16 +01001803 (debug?"true":"false"), max_frame_size);
1804 event_base_dispatch(base);
1805
1806 for (i = 0; i < num_workers; i++) {
1807 struct worker *w = &workers[i];
1808
1809 pthread_join(w->thread, NULL);
1810 DEBUG(&null_worker, "Worker %02d terminated", w->id);
Christopher Faulet010fded2016-11-03 22:49:37 +01001811 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001812
1813 free(workers);
1814 event_free(signal_event);
1815 event_free(accept_event);
1816 event_base_free(base);
1817 close(fd);
Christopher Faulet010fded2016-11-03 22:49:37 +01001818 return EXIT_SUCCESS;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001819
1820 error:
1821 if (workers != NULL)
1822 free(workers);
1823 if (signal_event != NULL)
1824 event_free(signal_event);
1825 if (accept_event != NULL)
1826 event_free(accept_event);
1827 if (base != NULL)
1828 event_base_free(base);
1829 if (fd != -1)
1830 close(fd);
Christopher Faulet010fded2016-11-03 22:49:37 +01001831 return EXIT_FAILURE;
1832}