blob: 7b40e81ed62082b781a95004da5a81d11c39512b [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>
Eric Salama8a9c6c22017-11-10 11:02:23 +010024#include <arpa/inet.h>
Christopher Faulet010fded2016-11-03 22:49:37 +010025#include <netinet/in.h>
Eric Salama8a9c6c22017-11-10 11:02:23 +010026#include <netinet/tcp.h>
Christopher Fauletf95b1112016-12-21 08:58:16 +010027#include <sys/socket.h>
28#include <err.h>
29#include <ctype.h>
30
31#include <pthread.h>
32
33#include <event2/util.h>
34#include <event2/event.h>
35#include <event2/event_struct.h>
36#include <event2/thread.h>
Christopher Faulet010fded2016-11-03 22:49:37 +010037
Eric Salama8a9c6c22017-11-10 11:02:23 +010038#include <mini-clist.h>
39#include <spoe_types.h>
40#include <spop_functions.h>
Christopher Fauletf95b1112016-12-21 08:58:16 +010041
42#define DEFAULT_PORT 12345
43#define CONNECTION_BACKLOG 10
44#define NUM_WORKERS 10
45#define MAX_FRAME_SIZE 16384
Christopher Faulet63816502018-05-31 14:56:42 +020046#define SPOP_VERSION "2.0"
Christopher Faulet010fded2016-11-03 22:49:37 +010047
48#define SLEN(str) (sizeof(str)-1)
49
Christopher Fauletf95b1112016-12-21 08:58:16 +010050#define LOG(worker, fmt, args...) \
51 do { \
52 struct timeval now; \
53 \
54 gettimeofday(&now, NULL); \
55 fprintf(stderr, "%ld.%06ld [%02d] " fmt "\n", \
56 now.tv_sec, now.tv_usec, (worker)->id, ##args); \
Christopher Faulet010fded2016-11-03 22:49:37 +010057 } while (0)
58
Christopher Fauletf95b1112016-12-21 08:58:16 +010059#define DEBUG(x...) \
Christopher Faulet010fded2016-11-03 22:49:37 +010060 do { \
61 if (debug) \
62 LOG(x); \
63 } while (0)
64
Christopher Faulet010fded2016-11-03 22:49:37 +010065
Christopher Fauletf95b1112016-12-21 08:58:16 +010066enum spoa_state {
67 SPOA_ST_CONNECTING = 0,
68 SPOA_ST_PROCESSING,
69 SPOA_ST_DISCONNECTING,
70};
71
72enum spoa_frame_type {
73 SPOA_FRM_T_UNKNOWN = 0,
74 SPOA_FRM_T_HAPROXY,
75 SPOA_FRM_T_AGENT,
76};
Christopher Faulet010fded2016-11-03 22:49:37 +010077
Christopher Fauletf95b1112016-12-21 08:58:16 +010078struct spoe_engine {
79 char *id;
80
81 struct list processing_frames;
82 struct list outgoing_frames;
83
84 struct list clients;
85 struct list list;
86};
87
88struct spoe_frame {
89 enum spoa_frame_type type;
90 char *buf;
91 unsigned int offset;
92 unsigned int len;
93
94 unsigned int stream_id;
95 unsigned int frame_id;
Christopher Faulet85010352017-02-02 10:14:36 +010096 unsigned int flags;
97 bool hcheck; /* true is the CONNECT frame is a healthcheck */
98 bool fragmented; /* true if the frame is fragmented */
99 int ip_score; /* -1 if unset, else between 0 and 100 */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100100
101 struct event process_frame_event;
102 struct worker *worker;
103 struct spoe_engine *engine;
104 struct client *client;
105 struct list list;
106
Christopher Faulet85010352017-02-02 10:14:36 +0100107 char *frag_buf; /* used to accumulate payload of a fragmented frame */
108 unsigned int frag_len;
109
Christopher Fauletf95b1112016-12-21 08:58:16 +0100110 char data[0];
111};
112
113struct client {
114 int fd;
115 unsigned long id;
116 enum spoa_state state;
117
118 struct event read_frame_event;
119 struct event write_frame_event;
120
121 struct spoe_frame *incoming_frame;
122 struct spoe_frame *outgoing_frame;
123
124 struct list processing_frames;
125 struct list outgoing_frames;
126
127 unsigned int max_frame_size;
128 int status_code;
129
130 char *engine_id;
131 struct spoe_engine *engine;
132 bool pipelining;
133 bool async;
Christopher Faulet85010352017-02-02 10:14:36 +0100134 bool fragmentation;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100135
136 struct worker *worker;
137 struct list by_worker;
138 struct list by_engine;
Christopher Faulet010fded2016-11-03 22:49:37 +0100139};
140
141struct worker {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100142 pthread_t thread;
143 int id;
144 struct event_base *base;
145 struct event *monitor_event;
146
147 struct list engines;
148
149 unsigned int nbclients;
150 struct list clients;
151
152 struct list frames;
Christopher Faulet0b89f722018-01-23 14:46:51 +0100153 unsigned int nbframes;
Christopher Faulet010fded2016-11-03 22:49:37 +0100154};
155
Christopher Fauletf95b1112016-12-21 08:58:16 +0100156
Christopher Fauletf95b1112016-12-21 08:58:16 +0100157/* Globals */
158static struct worker *workers = NULL;
159static struct worker null_worker = { .id = 0 };
160static unsigned long clicount = 0;
161static int server_port = DEFAULT_PORT;
162static int num_workers = NUM_WORKERS;
163static unsigned int max_frame_size = MAX_FRAME_SIZE;
164struct timeval processing_delay = {0, 0};
165static bool debug = false;
166static bool pipelining = false;
167static bool async = false;
Christopher Faulet85010352017-02-02 10:14:36 +0100168static bool fragmentation = false;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100169
170
171static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Faulet85010352017-02-02 10:14:36 +0100172 [SPOE_FRM_ERR_NONE] = "normal",
173 [SPOE_FRM_ERR_IO] = "I/O error",
174 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
175 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
176 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
177 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
178 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
179 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
180 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
181 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
182 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
183 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100184 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Faulet85010352017-02-02 10:14:36 +0100185 [SPOE_FRM_ERR_RES] = "resource allocation error",
186 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf95b1112016-12-21 08:58:16 +0100187};
188
189static void signal_cb(evutil_socket_t, short, void *);
190static void accept_cb(evutil_socket_t, short, void *);
191static void worker_monitor_cb(evutil_socket_t, short, void *);
192static void process_frame_cb(evutil_socket_t, short, void *);
193static void read_frame_cb(evutil_socket_t, short, void *);
194static void write_frame_cb(evutil_socket_t, short, void *);
195
196static void use_spoe_engine(struct client *);
197static void unuse_spoe_engine(struct client *);
198static void release_frame(struct spoe_frame *);
199static void release_client(struct client *);
Christopher Faulet010fded2016-11-03 22:49:37 +0100200
201static void
Christopher Fauletf95b1112016-12-21 08:58:16 +0100202check_ipv4_reputation(struct spoe_frame *frame, struct in_addr *ipv4)
Christopher Faulet010fded2016-11-03 22:49:37 +0100203{
204 char str[INET_ADDRSTRLEN];
205
206 if (inet_ntop(AF_INET, ipv4, str, INET_ADDRSTRLEN) == NULL)
207 return;
208
Christopher Fauletf95b1112016-12-21 08:58:16 +0100209 frame->ip_score = random() % 100;
Christopher Faulet010fded2016-11-03 22:49:37 +0100210
Christopher Fauletf95b1112016-12-21 08:58:16 +0100211 DEBUG(frame->worker, "IP score for %.*s is %d",
212 INET_ADDRSTRLEN, str, frame->ip_score);
Christopher Faulet010fded2016-11-03 22:49:37 +0100213}
214
215static void
Christopher Fauletf95b1112016-12-21 08:58:16 +0100216check_ipv6_reputation(struct spoe_frame *frame, struct in6_addr *ipv6)
Christopher Faulet010fded2016-11-03 22:49:37 +0100217{
218 char str[INET6_ADDRSTRLEN];
219
220 if (inet_ntop(AF_INET6, ipv6, str, INET6_ADDRSTRLEN) == NULL)
221 return;
222
Christopher Fauletf95b1112016-12-21 08:58:16 +0100223 frame->ip_score = random() % 100;
Christopher Faulet010fded2016-11-03 22:49:37 +0100224
Christopher Fauletf95b1112016-12-21 08:58:16 +0100225 DEBUG(frame->worker, "IP score for %.*s is %d",
226 INET6_ADDRSTRLEN, str, frame->ip_score);
Christopher Faulet010fded2016-11-03 22:49:37 +0100227}
228
Christopher Faulet010fded2016-11-03 22:49:37 +0100229
230/* Check the protocol version. It returns -1 if an error occurred, the number of
231 * read bytes otherwise. */
232static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100233check_proto_version(struct spoe_frame *frame, char **buf, char *end)
Christopher Faulet010fded2016-11-03 22:49:37 +0100234{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100235 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100236 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100237 int ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100238
239 /* Get the list of all supported versions by HAProxy */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100240 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Faulet010fded2016-11-03 22:49:37 +0100241 return -1;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100242 ret = spoe_decode_buffer(&p, end, &str, &sz);
243 if (ret == -1 || !str)
Christopher Faulet010fded2016-11-03 22:49:37 +0100244 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100245
246 DEBUG(frame->worker, "<%lu> Supported versions : %.*s",
247 frame->client->id, (int)sz, str);
Christopher Faulet010fded2016-11-03 22:49:37 +0100248
249 /* TODO: Find the right verion in supported ones */
250
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100251 ret = (p - *buf);
252 *buf = p;
253 return ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100254}
255
256/* Check max frame size value. It returns -1 if an error occurred, the number of
257 * read bytes otherwise. */
258static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100259check_max_frame_size(struct spoe_frame *frame, char **buf, char *end)
Christopher Faulet010fded2016-11-03 22:49:37 +0100260{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100261 char *p = *buf;
Christopher Faulet010fded2016-11-03 22:49:37 +0100262 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100263 int type, ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100264
265 /* Get the max-frame-size value of HAProxy */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100266 type = *p++;
Christopher Faulet010fded2016-11-03 22:49:37 +0100267 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
268 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
269 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
Christopher Fauletf95b1112016-12-21 08:58:16 +0100270 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64)
Christopher Faulet010fded2016-11-03 22:49:37 +0100271 return -1;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200272 if (decode_varint(&p, end, &sz) == -1)
Christopher Faulet010fded2016-11-03 22:49:37 +0100273 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100274
275 /* Keep the lower value */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100276 if (sz < frame->client->max_frame_size)
277 frame->client->max_frame_size = sz;
278
279 DEBUG(frame->worker, "<%lu> HAProxy maximum frame size : %u",
280 frame->client->id, (unsigned int)sz);
Christopher Faulet010fded2016-11-03 22:49:37 +0100281
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100282 ret = (p - *buf);
283 *buf = p;
284 return ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100285}
286
Christopher Fauletba7bc162016-11-07 21:07:38 +0100287/* Check healthcheck value. It returns -1 if an error occurred, the number of
288 * read bytes otherwise. */
289static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100290check_healthcheck(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100291{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100292 char *p = *buf;
293 int type, ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100294
Christopher Fauletf95b1112016-12-21 08:58:16 +0100295 /* Get the "healthcheck" value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100296 type = *p++;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100297 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_BOOL)
298 return -1;
299 frame->hcheck = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE);
300
301 DEBUG(frame->worker, "<%lu> HELLO healthcheck : %s",
302 frame->client->id, (frame->hcheck ? "true" : "false"));
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100303
304 ret = (p - *buf);
305 *buf = p;
306 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100307}
308
309/* Check capabilities value. It returns -1 if an error occurred, the number of
310 * read bytes otherwise. */
311static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100312check_capabilities(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100313{
314 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100315 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100316 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100317 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100318
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100319 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100320 return -1;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100321 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
322 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100323 if (str == NULL) /* this is not an error */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100324 goto end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100325
326 DEBUG(frame->worker, "<%lu> HAProxy capabilities : %.*s",
327 client->id, (int)sz, str);
328
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100329 while (sz) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100330 char *delim;
331
332 /* Skip leading spaces */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100333 for (; isspace(*str) && sz; sz--);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100334
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100335 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
336 str += 10; sz -= 10;
337 if (!sz || isspace(*str) || *str == ',') {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100338 DEBUG(frame->worker,
339 "<%lu> HAProxy supports frame pipelining",
340 client->id);
341 client->pipelining = true;
342 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100343 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100344 else if (sz >= 5 && !strncmp(str, "async", 5)) {
345 str += 5; sz -= 5;
346 if (!sz || isspace(*str) || *str == ',') {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100347 DEBUG(frame->worker,
348 "<%lu> HAProxy supports asynchronous frame",
349 client->id);
350 client->async = true;
351 }
352 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100353 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
354 str += 13; sz -= 13;
355 if (!sz || isspace(*str) || *str == ',') {
Christopher Faulet85010352017-02-02 10:14:36 +0100356 DEBUG(frame->worker,
357 "<%lu> HAProxy supports fragmented frame",
358 client->id);
359 client->fragmentation = true;
360 }
361 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100362
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100363 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100364 break;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100365 delim++;
366 sz -= (delim - str);
367 str = delim;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100368 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100369 end:
370 ret = (p - *buf);
371 *buf = p;
372 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100373}
374
375/* Check engine-id value. It returns -1 if an error occurred, the number of
376 * read bytes otherwise. */
377static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100378check_engine_id(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100379{
380 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100381 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100382 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100383 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100384
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100385 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100386 return -1;
387
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100388 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
389 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100390 if (str == NULL) /* this is not an error */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100391 goto end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100392
393 if (client->engine != NULL)
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100394 goto end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100395
396 DEBUG(frame->worker, "<%lu> HAProxy engine id : %.*s",
397 client->id, (int)sz, str);
398
399 client->engine_id = strndup(str, (int)sz);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100400 end:
401 ret = (p - *buf);
402 *buf = p;
403 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100404}
405
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100406static int
407acc_payload(struct spoe_frame *frame)
408{
409 struct client *client = frame->client;
410 char *buf;
411 size_t len = frame->len - frame->offset;
412 int ret = frame->offset;
413
414 /* No need to accumulation payload */
415 if (frame->fragmented == false)
416 return ret;
417
418 buf = realloc(frame->frag_buf, frame->frag_len + len);
419 if (buf == NULL) {
420 client->status_code = SPOE_FRM_ERR_RES;
421 return -1;
422 }
423 memcpy(buf + frame->frag_len, frame->buf + frame->offset, len);
424 frame->frag_buf = buf;
425 frame->frag_len += len;
426
427 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
428 /* Wait for next parts */
429 frame->buf = (char *)(frame->data);
430 frame->offset = 0;
431 frame->len = 0;
432 frame->flags = 0;
433 return 1;
434 }
435
436 frame->buf = frame->frag_buf;
437 frame->len = frame->frag_len;
438 frame->offset = 0;
439 return ret;
440}
441
Christopher Fauletf95b1112016-12-21 08:58:16 +0100442/* Check disconnect status code. It returns -1 if an error occurred, the number
443 * of read bytes otherwise. */
444static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100445check_discon_status_code(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100446{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100447 char *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100448 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100449 int type, ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100450
451 /* Get the "status-code" value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100452 type = *p++;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100453 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
454 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
455 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
456 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64)
457 return -1;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200458 if (decode_varint(&p, end, &sz) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100459 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100460
461 frame->client->status_code = (unsigned int)sz;
462
463 DEBUG(frame->worker, "<%lu> Disconnect status code : %u",
464 frame->client->id, frame->client->status_code);
465
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100466 ret = (p - *buf);
467 *buf = p;
468 return ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100469}
470
Christopher Fauletf95b1112016-12-21 08:58:16 +0100471/* Check the disconnect message. It returns -1 if an error occurred, the number
472 * of read bytes otherwise. */
473static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100474check_discon_message(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100475{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100476 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100477 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100478 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100479
480 /* Get the "message" value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100481 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100482 return -1;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100483 ret = spoe_decode_buffer(&p, end, &str, &sz);
484 if (ret == -1 || !str)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100485 return -1;
486
487 DEBUG(frame->worker, "<%lu> Disconnect message : %.*s",
488 frame->client->id, (int)sz, str);
489
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100490 ret = (p - *buf);
491 *buf = p;
492 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100493}
Christopher Fauletba7bc162016-11-07 21:07:38 +0100494
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100495
496
Christopher Faulet010fded2016-11-03 22:49:37 +0100497/* Decode a HELLO frame received from HAProxy. It returns -1 if an error
Christopher Fauletf95b1112016-12-21 08:58:16 +0100498 * occurred, otherwise the number of read bytes. HELLO frame cannot be
499 * ignored and having another frame than a HELLO frame is an error. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100500static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100501handle_hahello(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100502{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100503 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100504 char *p, *end;
505
506 p = frame->buf;
507 end = frame->buf + frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100508
Christopher Fauletf95b1112016-12-21 08:58:16 +0100509 /* Check frame type: we really want a HELLO frame */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100510 if (*p++ != SPOE_FRM_T_HAPROXY_HELLO)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100511 goto error;
512
513 DEBUG(frame->worker, "<%lu> Decode HAProxy HELLO frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100514
Christopher Faulet85010352017-02-02 10:14:36 +0100515 /* Retrieve flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100516 memcpy((char *)&(frame->flags), p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200517 frame->flags = ntohl(frame->flags);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100518 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100519
Christopher Faulet85010352017-02-02 10:14:36 +0100520 /* Fragmentation is not supported for HELLO frame */
521 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
522 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
523 goto error;
524 }
525
Christopher Faulet010fded2016-11-03 22:49:37 +0100526 /* stream-id and frame-id must be cleared */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100527 if (*p != 0 || *(p+1) != 0) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100528 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100529 goto error;
530 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100531 p += 2;
Christopher Faulet010fded2016-11-03 22:49:37 +0100532
533 /* Loop on K/V items */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100534 while (p < end) {
Christopher Faulet010fded2016-11-03 22:49:37 +0100535 char *str;
536 uint64_t sz;
537
538 /* Decode the item name */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100539 spoe_decode_buffer(&p, end, &str, &sz);
540 if (!str) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100541 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100542 goto error;
543 }
544
545 /* Check "supported-versions" K/V item */
546 if (!memcmp(str, "supported-versions", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100547 if (check_proto_version(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100548 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100549 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100550 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100551 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100552 /* Check "max-frame-size" K/V item */
Christopher Faulet010fded2016-11-03 22:49:37 +0100553 else if (!memcmp(str, "max-frame-size", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100554 if (check_max_frame_size(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100555 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100556 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100557 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100558 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100559 /* Check "healthcheck" K/V item */
Christopher Fauletba7bc162016-11-07 21:07:38 +0100560 else if (!memcmp(str, "healthcheck", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100561 if (check_healthcheck(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100562 client->status_code = SPOE_FRM_ERR_INVALID;
563 goto error;
564 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100565 }
566 /* Check "capabilities" K/V item */
567 else if (!memcmp(str, "capabilities", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100568 if (check_capabilities(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100569 client->status_code = SPOE_FRM_ERR_INVALID;
570 goto error;
571 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100572 }
573 /* Check "engine-id" K/V item */
574 else if (!memcmp(str, "engine-id", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100575 if (check_engine_id(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100576 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100577 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100578 }
Christopher Fauletba7bc162016-11-07 21:07:38 +0100579 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100580 else {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100581 DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s",
582 client->id, (int)sz, str);
583
Christopher Faulet010fded2016-11-03 22:49:37 +0100584 /* Silently ignore unknown item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100585 if (spoe_skip_data(&p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100586 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100587 goto error;
588 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100589 }
590 }
591
Christopher Fauletf95b1112016-12-21 08:58:16 +0100592 if (async == false || client->engine_id == NULL)
593 client->async = false;
594 if (pipelining == false)
595 client->pipelining = false;
596
597 if (client->async == true)
598 use_spoe_engine(client);
599
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100600 return (p - frame->buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100601 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100602 return -1;
603}
604
605/* Decode a DISCONNECT frame received from HAProxy. It returns -1 if an error
Christopher Fauletf95b1112016-12-21 08:58:16 +0100606 * occurred, otherwise the number of read bytes. DISCONNECT frame cannot be
607 * ignored and having another frame than a DISCONNECT frame is an error.*/
Christopher Faulet010fded2016-11-03 22:49:37 +0100608static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100609handle_hadiscon(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100610{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100611 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100612 char *p, *end;
613
614 p = frame->buf;
615 end = frame->buf + frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100616
Christopher Fauletf95b1112016-12-21 08:58:16 +0100617 /* Check frame type: we really want a DISCONNECT frame */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100618 if (*p++ != SPOE_FRM_T_HAPROXY_DISCON)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100619 goto error;
620
621 DEBUG(frame->worker, "<%lu> Decode HAProxy DISCONNECT frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100622
Christopher Faulet85010352017-02-02 10:14:36 +0100623 /* Retrieve flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100624 memcpy((char *)&(frame->flags), p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200625 frame->flags = ntohl(frame->flags);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100626 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100627
Christopher Faulet85010352017-02-02 10:14:36 +0100628 /* Fragmentation is not supported for DISCONNECT frame */
629 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
630 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
631 goto error;
632 }
633
Christopher Faulet010fded2016-11-03 22:49:37 +0100634 /* stream-id and frame-id must be cleared */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100635 if (*p != 0 || *(p+1) != 0) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100636 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100637 goto error;
638 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100639 p += 2;
Christopher Faulet010fded2016-11-03 22:49:37 +0100640
Christopher Fauletf95b1112016-12-21 08:58:16 +0100641 client->status_code = SPOE_FRM_ERR_NONE;
642
Christopher Faulet010fded2016-11-03 22:49:37 +0100643 /* Loop on K/V items */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100644 while (p < end) {
Christopher Faulet010fded2016-11-03 22:49:37 +0100645 char *str;
646 uint64_t sz;
647
648 /* Decode item key */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100649 spoe_decode_buffer(&p, end, &str, &sz);
650 if (!str) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100651 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100652 goto error;
653 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100654
655 /* Check "status-code" K/V item */
656 if (!memcmp(str, "status-code", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100657 if (check_discon_status_code(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100658 client->status_code = SPOE_FRM_ERR_INVALID;
659 goto error;
660 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100661 }
662 /* Check "message" K/V item */
663 else if (!memcmp(str, "message", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100664 if (check_discon_message(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100665 client->status_code = SPOE_FRM_ERR_INVALID;
666 goto error;
667 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100668 }
669 else {
670 DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s",
671 client->id, (int)sz, str);
672
673 /* Silently ignore unknown item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100674 if (spoe_skip_data(&p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100675 client->status_code = SPOE_FRM_ERR_INVALID;
676 goto error;
677 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100678 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100679 }
680
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100681 return (p - frame->buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100682 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100683 return -1;
684}
685
686/* Decode a NOTIFY frame received from HAProxy. It returns -1 if an error
Christopher Faulet85010352017-02-02 10:14:36 +0100687 * occurred, 0 if it must be must be ignored, otherwise the number of read
688 * bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100689static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100690handle_hanotify(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100691{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100692 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100693 char *p, *end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100694 uint64_t stream_id, frame_id;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100695
696 p = frame->buf;
697 end = frame->buf + frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100698
699 /* Check frame type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100700 if (*p++ != SPOE_FRM_T_HAPROXY_NOTIFY)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100701 goto ignore;
702
703 DEBUG(frame->worker, "<%lu> Decode HAProxy NOTIFY frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100704
Christopher Faulet85010352017-02-02 10:14:36 +0100705 /* Retrieve flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100706 memcpy((char *)&(frame->flags), p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200707 frame->flags = ntohl(frame->flags);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100708 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100709
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100710 /* Fragmentation is not supported */
Christopher Faulet85010352017-02-02 10:14:36 +0100711 if (!(frame->flags & SPOE_FRM_FL_FIN) && fragmentation == false) {
712 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
713 goto error;
714 }
715
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100716 /* Read the stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200717 if (decode_varint(&p, end, &stream_id) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100718 goto ignore;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200719 if (decode_varint(&p, end, &frame_id) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100720 goto ignore;
Christopher Faulet010fded2016-11-03 22:49:37 +0100721
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100722 frame->stream_id = (unsigned int)stream_id;
723 frame->frame_id = (unsigned int)frame_id;
Christopher Faulet010fded2016-11-03 22:49:37 +0100724
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100725 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
726 " - %s frame received"
727 " - frag_len=%u - len=%u - offset=%ld",
728 client->id, frame->stream_id, frame->frame_id,
729 (frame->flags & SPOE_FRM_FL_FIN) ? "unfragmented" : "fragmented",
730 frame->frag_len, frame->len, p - frame->buf);
731
732 frame->fragmented = !(frame->flags & SPOE_FRM_FL_FIN);
733 frame->offset = (p - frame->buf);
734 return acc_payload(frame);
735
736 ignore:
737 return 0;
738
739 error:
740 return -1;
741}
742
743/* Decode next part of a fragmented frame received from HAProxy. It returns -1
744 * if an error occurred, 0 if it must be must be ignored, otherwise the number
745 * of read bytes. */
746static int
747handle_hafrag(struct spoe_frame *frame)
748{
749 struct client *client = frame->client;
750 char *p, *end;
751 uint64_t stream_id, frame_id;
752
753 p = frame->buf;
754 end = frame->buf + frame->len;
755
756 /* Check frame type */
757 if (*p++ != SPOE_FRM_T_UNSET)
758 goto ignore;
759
760 DEBUG(frame->worker, "<%lu> Decode Next part of a fragmented frame", client->id);
761
762 /* Fragmentation is not supported */
763 if (fragmentation == false) {
764 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
765 goto error;
Christopher Faulet85010352017-02-02 10:14:36 +0100766 }
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100767
768 /* Retrieve flags */
769 memcpy((char *)&(frame->flags), p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200770 frame->flags = ntohl(frame->flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100771 p+= 4;
772
773 /* Read the stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200774 if (decode_varint(&p, end, &stream_id) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100775 goto ignore;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200776 if (decode_varint(&p, end, &frame_id) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100777 goto ignore;
Christopher Faulet010fded2016-11-03 22:49:37 +0100778
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100779 if (frame->fragmented == false ||
780 frame->stream_id != (unsigned int)stream_id ||
781 frame->frame_id != (unsigned int)frame_id) {
782 client->status_code = SPOE_FRM_ERR_INTERLACED_FRAMES;
783 goto error;
784 }
785
786 if (frame->flags & SPOE_FRM_FL_ABRT) {
Christopher Faulet85010352017-02-02 10:14:36 +0100787 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100788 " - Abort processing of a fragmented frame"
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100789 " - frag_len=%u - len=%u - offset=%ld",
Christopher Faulet85010352017-02-02 10:14:36 +0100790 client->id, frame->stream_id, frame->frame_id,
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100791 frame->frag_len, frame->len, p - frame->buf);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100792 goto ignore;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100793 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100794
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100795 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
796 " - %s fragment of a fragmented frame received"
797 " - frag_len=%u - len=%u - offset=%ld",
798 client->id, frame->stream_id, frame->frame_id,
799 (frame->flags & SPOE_FRM_FL_FIN) ? "last" : "next",
800 frame->frag_len, frame->len, p - frame->buf);
801
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100802 frame->offset = (p - frame->buf);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100803 return acc_payload(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +0100804
Christopher Fauletf95b1112016-12-21 08:58:16 +0100805 ignore:
Christopher Faulet85010352017-02-02 10:14:36 +0100806 return 0;
807
808 error:
Christopher Fauletf95b1112016-12-21 08:58:16 +0100809 return -1;
810}
Christopher Faulet010fded2016-11-03 22:49:37 +0100811
Christopher Fauletf95b1112016-12-21 08:58:16 +0100812/* Encode a HELLO frame to send it to HAProxy. It returns the number of written
813 * bytes. */
814static int
815prepare_agenthello(struct spoe_frame *frame)
816{
817 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100818 char *p, *end;
Christopher Faulet85010352017-02-02 10:14:36 +0100819 char capabilities[64];
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100820 int n;
Christopher Faulet85010352017-02-02 10:14:36 +0100821 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet010fded2016-11-03 22:49:37 +0100822
Christopher Fauletf95b1112016-12-21 08:58:16 +0100823 DEBUG(frame->worker, "<%lu> Encode Agent HELLO frame", client->id);
824 frame->type = SPOA_FRM_T_AGENT;
Christopher Faulet010fded2016-11-03 22:49:37 +0100825
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100826 p = frame->buf;
827 end = frame->buf+max_frame_size;
828
Christopher Faulet010fded2016-11-03 22:49:37 +0100829 /* Frame Type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100830 *p++ = SPOE_FRM_T_AGENT_HELLO;
Christopher Faulet010fded2016-11-03 22:49:37 +0100831
Christopher Faulet85010352017-02-02 10:14:36 +0100832 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200833 flags = htonl(flags);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100834 memcpy(p, (char *)&flags, 4);
835 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100836
837 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100838 *p++ = 0;
839 *p++ = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +0100840
841 /* "version" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100842 spoe_encode_buffer("version", 7, &p, end);
843 *p++ = SPOE_DATA_T_STR;
844 spoe_encode_buffer(SPOP_VERSION, SLEN(SPOP_VERSION), &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100845 DEBUG(frame->worker, "<%lu> Agent version : %s",
846 client->id, SPOP_VERSION);
847
Christopher Faulet010fded2016-11-03 22:49:37 +0100848
849 /* "max-frame-size" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100850 spoe_encode_buffer("max-frame-size", 14, &p ,end);
851 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200852 encode_varint(client->max_frame_size, &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100853 DEBUG(frame->worker, "<%lu> Agent maximum frame size : %u",
854 client->id, client->max_frame_size);
Christopher Faulet010fded2016-11-03 22:49:37 +0100855
856 /* "capabilities" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100857 spoe_encode_buffer("capabilities", 12, &p, end);
858 *p++ = SPOE_DATA_T_STR;
Christopher Faulet85010352017-02-02 10:14:36 +0100859
860 memset(capabilities, 0, sizeof(capabilities));
861 n = 0;
862
863 /* 1. Fragmentation capability ? */
864 if (fragmentation == true) {
865 memcpy(capabilities, "fragmentation", 13);
866 n += 13;
867 }
868 /* 2. Pipelining capability ? */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100869 if (client->pipelining == true) {
870 if (n) capabilities[n++] = ',';
871 memcpy(capabilities + n, "pipelining", 10);
Christopher Faulet85010352017-02-02 10:14:36 +0100872 n += 10;
873 }
874 /* 3. Async capability ? */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100875 if (client->async == true) {
876 if (n) capabilities[n++] = ',';
877 memcpy(capabilities + n, "async", 5);
Christopher Faulet85010352017-02-02 10:14:36 +0100878 n += 5;
879 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100880 spoe_encode_buffer(capabilities, n, &p, end);
Christopher Faulet010fded2016-11-03 22:49:37 +0100881
Christopher Faulet85010352017-02-02 10:14:36 +0100882 DEBUG(frame->worker, "<%lu> Agent capabilities : %.*s",
883 client->id, n, capabilities);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100884
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100885 frame->len = (p - frame->buf);
886 return frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100887}
888
Christopher Fauletf95b1112016-12-21 08:58:16 +0100889/* Encode a DISCONNECT frame to send it to HAProxy. It returns the number of
890 * written bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100891static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100892prepare_agentdicon(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100893{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100894 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100895 char *p, *end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100896 const char *reason;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100897 int rlen;
Christopher Faulet85010352017-02-02 10:14:36 +0100898 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100899
900 DEBUG(frame->worker, "<%lu> Encode Agent DISCONNECT frame", client->id);
901 frame->type = SPOA_FRM_T_AGENT;
902
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100903 p = frame->buf;
904 end = frame->buf+max_frame_size;
905
Christopher Fauletf95b1112016-12-21 08:58:16 +0100906 if (client->status_code >= SPOE_FRM_ERRS)
907 client->status_code = SPOE_FRM_ERR_UNKNOWN;
908 reason = spoe_frm_err_reasons[client->status_code];
909 rlen = strlen(reason);
Christopher Faulet010fded2016-11-03 22:49:37 +0100910
911 /* Frame type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100912 *p++ = SPOE_FRM_T_AGENT_DISCON;
Christopher Faulet010fded2016-11-03 22:49:37 +0100913
Christopher Faulet85010352017-02-02 10:14:36 +0100914 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200915 flags = htonl(flags);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100916 memcpy(p, (char *)&flags, 4);
917 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100918
Christopher Fauletf95b1112016-12-21 08:58:16 +0100919 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100920 *p++ = 0;
921 *p++ = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100922
923 /* There are 2 mandatory items: "status-code" and "message" */
924
925 /* "status-code" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100926 spoe_encode_buffer("status-code", 11, &p, end);
927 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200928 encode_varint(client->status_code, &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100929 DEBUG(frame->worker, "<%lu> Disconnect status code : %u",
930 client->id, client->status_code);
Christopher Faulet010fded2016-11-03 22:49:37 +0100931
Christopher Fauletf95b1112016-12-21 08:58:16 +0100932 /* "message" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100933 spoe_encode_buffer("message", 7, &p, end);
934 *p++ = SPOE_DATA_T_STR;
935 spoe_encode_buffer(reason, rlen, &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100936 DEBUG(frame->worker, "<%lu> Disconnect message : %s",
937 client->id, reason);
Christopher Faulet010fded2016-11-03 22:49:37 +0100938
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100939 frame->len = (p - frame->buf);
940 return frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100941}
942
Christopher Fauletf95b1112016-12-21 08:58:16 +0100943/* Encode a ACK frame to send it to HAProxy. It returns the number of written
944 * bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100945static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100946prepare_agentack(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100947{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100948 char *p, *end;
Christopher Faulet85010352017-02-02 10:14:36 +0100949 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet010fded2016-11-03 22:49:37 +0100950
Christopher Fauletf95b1112016-12-21 08:58:16 +0100951 /* Be careful here, in async mode, frame->client can be NULL */
952
953 DEBUG(frame->worker, "Encode Agent ACK frame");
954 frame->type = SPOA_FRM_T_AGENT;
Christopher Faulet010fded2016-11-03 22:49:37 +0100955
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100956 p = frame->buf;
957 end = frame->buf+max_frame_size;
958
Christopher Faulet010fded2016-11-03 22:49:37 +0100959 /* Frame type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100960 *p++ = SPOE_FRM_T_AGENT_ACK;
Christopher Faulet010fded2016-11-03 22:49:37 +0100961
Christopher Faulet85010352017-02-02 10:14:36 +0100962 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200963 flags = htonl(flags);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100964 memcpy(p, (char *)&flags, 4);
965 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100966
Christopher Fauletf95b1112016-12-21 08:58:16 +0100967 /* Set stream-id and frame-id for ACK frames */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200968 encode_varint(frame->stream_id, &p, end);
969 encode_varint(frame->frame_id, &p, end);
Christopher Faulet010fded2016-11-03 22:49:37 +0100970
Christopher Fauletf95b1112016-12-21 08:58:16 +0100971 DEBUG(frame->worker, "STREAM-ID=%u - FRAME-ID=%u",
972 frame->stream_id, frame->frame_id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100973
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100974 frame->len = (p - frame->buf);
975 return frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100976}
977
978static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100979create_server_socket(void)
Christopher Faulet010fded2016-11-03 22:49:37 +0100980{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100981 struct sockaddr_in listen_addr;
982 int fd, yes = 1;
983
984 fd = socket(AF_INET, SOCK_STREAM, 0);
985 if (fd < 0) {
986 LOG(&null_worker, "Failed to create service socket : %m");
987 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100988 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100989
990 memset(&listen_addr, 0, sizeof(listen_addr));
991 listen_addr.sin_family = AF_INET;
992 listen_addr.sin_addr.s_addr = INADDR_ANY;
993 listen_addr.sin_port = htons(server_port);
994
995 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0 ||
996 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) < 0) {
997 LOG(&null_worker, "Failed to set option on server socket : %m");
998 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100999 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001000
1001 if (bind(fd, (struct sockaddr *) &listen_addr, sizeof(listen_addr)) < 0) {
1002 LOG(&null_worker, "Failed to bind server socket : %m");
1003 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001004 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001005
1006 if (listen(fd, CONNECTION_BACKLOG) < 0) {
1007 LOG(&null_worker, "Failed to listen on server socket : %m");
1008 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001009 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001010
1011 return fd;
Christopher Faulet010fded2016-11-03 22:49:37 +01001012}
1013
Christopher Fauletf95b1112016-12-21 08:58:16 +01001014static void
1015release_frame(struct spoe_frame *frame)
1016{
1017 struct worker *worker;
1018
1019 if (frame == NULL)
1020 return;
1021
1022 if (event_pending(&frame->process_frame_event, EV_TIMEOUT, NULL))
1023 event_del(&frame->process_frame_event);
1024
1025 worker = frame->worker;
1026 LIST_DEL(&frame->list);
Christopher Faulet85010352017-02-02 10:14:36 +01001027 if (frame->frag_buf)
1028 free(frame->frag_buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001029 memset(frame, 0, sizeof(*frame)+max_frame_size+4);
1030 LIST_ADDQ(&worker->frames, &frame->list);
1031}
1032
1033static void
1034release_client(struct client *c)
Christopher Faulet010fded2016-11-03 22:49:37 +01001035{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001036 struct spoe_frame *frame, *back;
1037
1038 if (c == NULL)
1039 return;
1040
1041 DEBUG(c->worker, "<%lu> Release client", c->id);
1042
1043 LIST_DEL(&c->by_worker);
1044 c->worker->nbclients--;
1045
1046 unuse_spoe_engine(c);
1047 free(c->engine_id);
1048
1049 if (event_pending(&c->read_frame_event, EV_READ, NULL))
1050 event_del(&c->read_frame_event);
1051 if (event_pending(&c->write_frame_event, EV_WRITE, NULL))
1052 event_del(&c->write_frame_event);
1053
1054 release_frame(c->incoming_frame);
1055 release_frame(c->outgoing_frame);
1056 list_for_each_entry_safe(frame, back, &c->processing_frames, list) {
1057 release_frame(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +01001058 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001059 list_for_each_entry_safe(frame, back, &c->outgoing_frames, list) {
1060 release_frame(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +01001061 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001062
1063 if (c->fd >= 0)
1064 close(c->fd);
1065
1066 free(c);
1067}
1068
1069static void
1070reset_frame(struct spoe_frame *frame)
1071{
1072 if (frame == NULL)
1073 return;
1074
Christopher Faulet85010352017-02-02 10:14:36 +01001075 if (frame->frag_buf)
1076 free(frame->frag_buf);
1077
1078 frame->type = SPOA_FRM_T_UNKNOWN;
1079 frame->buf = (char *)(frame->data);
1080 frame->offset = 0;
1081 frame->len = 0;
1082 frame->stream_id = 0;
1083 frame->frame_id = 0;
1084 frame->flags = 0;
1085 frame->hcheck = false;
1086 frame->fragmented = false;
1087 frame->ip_score = -1;
1088 frame->frag_buf = NULL;
1089 frame->frag_len = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001090 LIST_INIT(&frame->list);
1091}
1092
1093static void
1094use_spoe_engine(struct client *client)
1095{
1096 struct spoe_engine *eng;
1097
1098 if (client->engine_id == NULL)
1099 return;
1100
1101 list_for_each_entry(eng, &client->worker->engines, list) {
1102 if (!strcmp(eng->id, client->engine_id))
1103 goto end;
1104 }
1105
1106 if ((eng = malloc(sizeof(*eng))) == NULL) {
1107 client->async = false;
1108 return;
1109 }
1110
1111 eng->id = strdup(client->engine_id);
1112 LIST_INIT(&eng->clients);
1113 LIST_INIT(&eng->processing_frames);
1114 LIST_INIT(&eng->outgoing_frames);
1115 LIST_ADDQ(&client->worker->engines, &eng->list);
1116 LOG(client->worker, "Add new SPOE engine '%s'", eng->id);
1117
1118 end:
1119 client->engine = eng;
1120 LIST_ADDQ(&eng->clients, &client->by_engine);
1121}
1122
1123static void
1124unuse_spoe_engine(struct client *client)
1125{
1126 struct spoe_engine *eng;
1127 struct spoe_frame *frame, *back;
1128
1129 if (client == NULL || client->engine == NULL)
1130 return;
1131
1132 eng = client->engine;
1133 client->engine = NULL;
1134 LIST_DEL(&client->by_engine);
1135 if (!LIST_ISEMPTY(&eng->clients))
1136 return;
1137
1138 LOG(client->worker, "Remove SPOE engine '%s'", eng->id);
1139 LIST_DEL(&eng->list);
1140
1141 list_for_each_entry_safe(frame, back, &eng->processing_frames, list) {
1142 release_frame(frame);
1143 }
1144 list_for_each_entry_safe(frame, back, &eng->outgoing_frames, list) {
1145 release_frame(frame);
1146 }
1147 free(eng->id);
1148 free(eng);
1149}
1150
1151
1152static struct spoe_frame *
1153acquire_incoming_frame(struct client *client)
1154{
1155 struct spoe_frame *frame;
1156
1157 frame = client->incoming_frame;
1158 if (frame != NULL)
1159 return frame;
1160
1161 if (LIST_ISEMPTY(&client->worker->frames)) {
1162 if ((frame = calloc(1, sizeof(*frame)+max_frame_size+4)) == NULL) {
1163 LOG(client->worker, "Failed to allocate new frame : %m");
1164 return NULL;
1165 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001166 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001167 else {
1168 frame = LIST_NEXT(&client->worker->frames, typeof(frame), list);
1169 LIST_DEL(&frame->list);
Christopher Faulet010fded2016-11-03 22:49:37 +01001170 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001171
1172 reset_frame(frame);
1173 frame->worker = client->worker;
1174 frame->engine = client->engine;
1175 frame->client = client;
1176
1177 if (event_assign(&frame->process_frame_event, client->worker->base, -1,
1178 EV_TIMEOUT|EV_PERSIST, process_frame_cb, frame) < 0) {
1179 LOG(client->worker, "Failed to create frame event");
1180 return NULL;
Christopher Faulet010fded2016-11-03 22:49:37 +01001181 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001182
1183 client->incoming_frame = frame;
1184 return frame;
Christopher Faulet010fded2016-11-03 22:49:37 +01001185}
1186
Christopher Fauletf95b1112016-12-21 08:58:16 +01001187static struct spoe_frame *
1188acquire_outgoing_frame(struct client *client)
Christopher Faulet010fded2016-11-03 22:49:37 +01001189{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001190 struct spoe_engine *engine = client->engine;
1191 struct spoe_frame *frame = NULL;
Christopher Faulet010fded2016-11-03 22:49:37 +01001192
Christopher Fauletf95b1112016-12-21 08:58:16 +01001193 if (client->outgoing_frame != NULL)
1194 frame = client->outgoing_frame;
1195 else if (!LIST_ISEMPTY(&client->outgoing_frames)) {
1196 frame = LIST_NEXT(&client->outgoing_frames, typeof(frame), list);
1197 LIST_DEL(&frame->list);
1198 client->outgoing_frame = frame;
1199 }
1200 else if (engine!= NULL && !LIST_ISEMPTY(&engine->outgoing_frames)) {
1201 frame = LIST_NEXT(&engine->outgoing_frames, typeof(frame), list);
1202 LIST_DEL(&frame->list);
1203 client->outgoing_frame = frame;
1204 }
1205 return frame;
1206}
1207
1208static void
1209write_frame(struct client *client, struct spoe_frame *frame)
1210{
1211 uint32_t netint;
1212
1213 LIST_DEL(&frame->list);
1214
1215 frame->buf = (char *)(frame->data);
1216 frame->offset = 0;
1217 netint = htonl(frame->len);
1218 memcpy(frame->buf, &netint, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001219
Christopher Fauletf95b1112016-12-21 08:58:16 +01001220 if (client != NULL) { /* HELLO or DISCONNECT frames */
1221 event_add(&client->write_frame_event, NULL);
Christopher Faulet010fded2016-11-03 22:49:37 +01001222
Christopher Fauletf95b1112016-12-21 08:58:16 +01001223 /* Try to process the frame as soon as possible, and always
1224 * attach it to the client */
1225 if (client->async || client->pipelining) {
1226 if (client->outgoing_frame == NULL)
1227 client->outgoing_frame = frame;
1228 else
1229 LIST_ADD(&client->outgoing_frames, &frame->list);
1230 }
1231 else {
1232 client->outgoing_frame = frame;
1233 event_del(&client->read_frame_event);
Christopher Faulet010fded2016-11-03 22:49:37 +01001234 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001235 }
1236 else { /* for all other frames */
1237 if (frame->client == NULL) { /* async mode ! */
1238 LIST_ADDQ(&frame->engine->outgoing_frames, &frame->list);
1239 list_for_each_entry(client, &frame->engine->clients, by_engine)
1240 event_add(&client->write_frame_event, NULL);
1241 }
1242 else if (frame->client->pipelining) {
1243 LIST_ADDQ(&frame->client->outgoing_frames, &frame->list);
1244 event_add(&frame->client->write_frame_event, NULL);
1245 }
1246 else {
1247 frame->client->outgoing_frame = frame;
1248 event_add(&frame->client->write_frame_event, NULL);
1249 event_del(&frame->client->read_frame_event);
1250 }
1251 }
1252}
Christopher Faulet010fded2016-11-03 22:49:37 +01001253
Christopher Fauletf95b1112016-12-21 08:58:16 +01001254static void
1255process_incoming_frame(struct spoe_frame *frame)
1256{
1257 struct client *client = frame->client;
Christopher Faulet010fded2016-11-03 22:49:37 +01001258
Christopher Fauletf95b1112016-12-21 08:58:16 +01001259 if (event_add(&frame->process_frame_event, &processing_delay) < 0) {
1260 LOG(client->worker, "Failed to process incoming frame");
1261 release_frame(frame);
1262 return;
1263 }
1264
1265 if (client->async) {
1266 frame->client = NULL;
1267 LIST_ADDQ(&frame->engine->processing_frames, &frame->list);
1268 }
1269 else if (client->pipelining)
1270 LIST_ADDQ(&client->processing_frames, &frame->list);
1271 else
1272 event_del(&client->read_frame_event);
1273}
1274
1275static void
1276signal_cb(evutil_socket_t sig, short events, void *user_data)
1277{
1278 struct event_base *base = user_data;
1279 int i;
1280
1281 DEBUG(&null_worker, "Stopping the server");
1282
1283 event_base_loopbreak(base);
1284 DEBUG(&null_worker, "Main event loop stopped");
1285
1286 for (i = 0; i < num_workers; i++) {
1287 event_base_loopbreak(workers[i].base);
1288 DEBUG(&null_worker, "Event loop stopped for worker %02d",
1289 workers[i].id);
1290 }
1291}
1292
1293static void
1294worker_monitor_cb(evutil_socket_t fd, short events, void *arg)
1295{
1296 struct worker *worker = arg;
1297
Christopher Faulet0b89f722018-01-23 14:46:51 +01001298 LOG(worker, "%u clients connected (%u frames)", worker->nbclients, worker->nbframes);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001299}
1300
1301static void
1302process_frame_cb(evutil_socket_t fd, short events, void *arg)
1303{
1304 struct spoe_frame *frame = arg;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001305 char *p, *end;
1306 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001307
1308 DEBUG(frame->worker,
Christopher Faulet85010352017-02-02 10:14:36 +01001309 "Process frame messages : STREAM-ID=%u - FRAME-ID=%u - length=%u bytes",
1310 frame->stream_id, frame->frame_id, frame->len - frame->offset);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001311
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001312 p = frame->buf + frame->offset;
1313 end = frame->buf + frame->len;
1314
Christopher Fauletf95b1112016-12-21 08:58:16 +01001315 /* Loop on messages */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001316 while (p < end) {
Christopher Fauletf95b1112016-12-21 08:58:16 +01001317 char *str;
1318 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001319 int nbargs;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001320
1321 /* Decode the message name */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001322 spoe_decode_buffer(&p, end, &str, &sz);
1323 if (!str)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001324 goto stop_processing;
1325
1326 DEBUG(frame->worker, "Process SPOE Message '%.*s'", (int)sz, str);
1327
Thierry FOURNIER29a05c12018-02-25 21:33:38 +01001328 nbargs = (unsigned char)*p++; /* Get the number of arguments */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001329 frame->offset = (p - frame->buf); /* Save index to handle errors and skip args */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001330 if (!memcmp(str, "check-client-ip", sz)) {
Willy Tarreau75f42462017-11-14 15:01:22 +01001331 union spoe_data data;
1332 enum spoe_data_type type;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001333
1334 if (nbargs != 1)
1335 goto skip_message;
1336
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001337 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001338 goto stop_processing;
Willy Tarreau75f42462017-11-14 15:01:22 +01001339 if (spoe_decode_data(&p, end, &data, &type) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001340 goto skip_message;
Christopher Faulet0b89f722018-01-23 14:46:51 +01001341 frame->worker->nbframes++;
Willy Tarreau75f42462017-11-14 15:01:22 +01001342 if (type == SPOE_DATA_T_IPV4)
1343 check_ipv4_reputation(frame, &data.ipv4);
1344 if (type == SPOE_DATA_T_IPV6)
1345 check_ipv6_reputation(frame, &data.ipv6);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001346 }
1347 else {
1348 skip_message:
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001349 p = frame->buf + frame->offset; /* Restore index */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001350
1351 while (nbargs-- > 0) {
1352 /* Silently ignore argument: its name and its value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001353 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001354 goto stop_processing;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001355 if (spoe_skip_data(&p, end) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001356 goto stop_processing;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001357 }
1358 }
1359 }
1360
1361 stop_processing:
1362 /* Prepare agent ACK frame */
Christopher Faulet85010352017-02-02 10:14:36 +01001363 frame->buf = (char *)(frame->data) + 4;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001364 frame->offset = 0;
Christopher Faulet85010352017-02-02 10:14:36 +01001365 frame->len = 0;
1366 frame->flags = 0;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001367
1368 ret = prepare_agentack(frame);
Christopher Faulet94bb4c62017-09-26 11:49:23 +02001369 p = frame->buf + ret;
1370 end = frame->buf+max_frame_size;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001371
1372 if (frame->ip_score != -1) {
1373 DEBUG(frame->worker, "Add action : set variable ip_scode=%u",
1374 frame->ip_score);
1375
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001376 *p++ = SPOE_ACT_T_SET_VAR; /* Action type */
1377 *p++ = 3; /* Number of args */
1378 *p++ = SPOE_SCOPE_SESS; /* Arg 1: the scope */
1379 spoe_encode_buffer("ip_score", 8, &p, end); /* Arg 2: variable name */
1380 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +02001381 encode_varint(frame->ip_score, &p, end); /* Arg 3: variable value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001382 frame->len = (p - frame->buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001383 }
1384 write_frame(NULL, frame);
1385}
1386
1387static void
1388read_frame_cb(evutil_socket_t fd, short events, void *arg)
1389{
1390 struct client *client = arg;
1391 struct spoe_frame *frame;
1392 uint32_t netint;
1393 int n;
1394
1395 DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__);
1396 if ((frame = acquire_incoming_frame(client)) == NULL)
1397 goto close;
1398
1399 frame->type = SPOA_FRM_T_HAPROXY;
1400 if (frame->buf == (char *)(frame->data)) {
1401 /* Read the frame length: frame->buf points on length part (frame->data) */
1402 n = read(client->fd, frame->buf+frame->offset, 4-frame->offset);
1403 if (n <= 0) {
1404 if (n < 0)
1405 LOG(client->worker, "Failed to read frame length : %m");
Christopher Fauletba7bc162016-11-07 21:07:38 +01001406 goto close;
Christopher Faulet010fded2016-11-03 22:49:37 +01001407 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001408 frame->offset += n;
1409 if (frame->offset != 4)
1410 return;
1411 memcpy(&netint, frame->buf, 4);
1412 frame->buf += 4;
1413 frame->offset = 0;
1414 frame->len = ntohl(netint);
1415 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001416
Christopher Fauletf95b1112016-12-21 08:58:16 +01001417 /* Read the frame: frame->buf points on frame part (frame->data+4)*/
1418 n = read(client->fd, frame->buf + frame->offset,
1419 frame->len - frame->offset);
1420 if (n <= 0) {
1421 if (n < 0) {
1422 LOG(client->worker, "Frame to read frame : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001423 goto close;
1424 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001425 return;
1426 }
1427 frame->offset += n;
1428 if (frame->offset != frame->len)
1429 return;
1430 frame->offset = 0;
1431
1432 DEBUG(client->worker, "<%lu> New Frame of %u bytes received",
1433 client->id, frame->len);
1434
1435 switch (client->state) {
1436 case SPOA_ST_CONNECTING:
1437 if (handle_hahello(frame) < 0) {
1438 LOG(client->worker, "Failed to decode HELLO frame");
1439 goto disconnect;
1440 }
1441 prepare_agenthello(frame);
1442 goto write_frame;
1443
1444 case SPOA_ST_PROCESSING:
Christopher Faulet85010352017-02-02 10:14:36 +01001445 if (frame->buf[0] == SPOE_FRM_T_HAPROXY_DISCON) {
Christopher Fauletf95b1112016-12-21 08:58:16 +01001446 client->state = SPOA_ST_DISCONNECTING;
1447 goto disconnecting;
1448 }
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001449 if (frame->buf[0] == SPOE_FRM_T_UNSET)
1450 n = handle_hafrag(frame);
1451 else
1452 n = handle_hanotify(frame);
1453
Christopher Fauletf95b1112016-12-21 08:58:16 +01001454 if (n < 0) {
Christopher Faulet85010352017-02-02 10:14:36 +01001455 LOG(client->worker, "Failed to decode frame: %s",
1456 spoe_frm_err_reasons[client->status_code]);
1457 goto disconnect;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001458 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001459 else if (n == 0) {
Christopher Faulet85010352017-02-02 10:14:36 +01001460 LOG(client->worker, "Ignore invalid/unknown/aborted frame");
1461 goto ignore_frame;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001462 }
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001463 else if (n == 1)
1464 goto noop;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001465 else
1466 goto process_frame;
1467
1468 case SPOA_ST_DISCONNECTING:
1469 disconnecting:
1470 if (handle_hadiscon(frame) < 0) {
1471 LOG(client->worker, "Failed to decode DISCONNECT frame");
1472 goto disconnect;
1473 }
1474 if (client->status_code != SPOE_FRM_ERR_NONE)
1475 LOG(client->worker, "<%lu> Peer closed connection: %s",
1476 client->id, spoe_frm_err_reasons[client->status_code]);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001477 goto disconnect;
1478 }
1479
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001480 noop:
1481 return;
1482
Christopher Fauletf95b1112016-12-21 08:58:16 +01001483 ignore_frame:
1484 reset_frame(frame);
1485 return;
1486
1487 process_frame:
1488 process_incoming_frame(frame);
1489 client->incoming_frame = NULL;
1490 return;
1491
1492 write_frame:
1493 write_frame(client, frame);
1494 client->incoming_frame = NULL;
1495 return;
1496
1497 disconnect:
1498 client->state = SPOA_ST_DISCONNECTING;
1499 if (prepare_agentdicon(frame) < 0) {
1500 LOG(client->worker, "Failed to encode DISCONNECT frame");
1501 goto close;
1502 }
1503 goto write_frame;
1504
1505 close:
1506 release_client(client);
1507}
1508
1509static void
1510write_frame_cb(evutil_socket_t fd, short events, void *arg)
1511{
1512 struct client *client = arg;
1513 struct spoe_frame *frame;
1514 int n;
1515
1516 DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__);
1517 if ((frame = acquire_outgoing_frame(client)) == NULL) {
1518 event_del(&client->write_frame_event);
1519 return;
1520 }
1521
1522 if (frame->buf == (char *)(frame->data)) {
1523 /* Write the frame length: frame->buf points on length part (frame->data) */
1524 n = write(client->fd, frame->buf+frame->offset, 4-frame->offset);
1525 if (n <= 0) {
1526 if (n < 0)
1527 LOG(client->worker, "Failed to write frame length : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001528 goto close;
1529 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001530 frame->offset += n;
1531 if (frame->offset != 4)
1532 return;
1533 frame->buf += 4;
1534 frame->offset = 0;
1535 }
1536
1537 /* Write the frame: frame->buf points on frame part (frame->data+4)*/
1538 n = write(client->fd, frame->buf + frame->offset,
1539 frame->len - frame->offset);
1540 if (n <= 0) {
1541 if (n < 0) {
1542 LOG(client->worker, "Failed to write frame : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001543 goto close;
1544 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001545 return;
1546 }
1547 frame->offset += n;
1548 if (frame->offset != frame->len)
1549 return;
1550
1551 DEBUG(client->worker, "<%lu> Frame of %u bytes send",
1552 client->id, frame->len);
1553
1554 switch (client->state) {
1555 case SPOA_ST_CONNECTING:
1556 if (frame->hcheck == true) {
1557 DEBUG(client->worker,
1558 "<%lu> Close client after healthcheck",
1559 client->id);
1560 goto close;
1561 }
1562 client->state = SPOA_ST_PROCESSING;
1563 break;
1564
1565 case SPOA_ST_PROCESSING:
1566 break;
1567
1568 case SPOA_ST_DISCONNECTING:
1569 goto close;
1570 }
1571
1572 release_frame(frame);
1573 client->outgoing_frame = NULL;
1574 if (!client->async && !client->pipelining) {
1575 event_del(&client->write_frame_event);
1576 event_add(&client->read_frame_event, NULL);
1577 }
1578 return;
1579
1580 close:
1581 release_client(client);
1582}
1583
1584static void
1585accept_cb(int listener, short event, void *arg)
1586{
1587 struct worker *worker;
1588 struct client *client;
1589 int fd;
1590
1591 worker = &workers[clicount++ % num_workers];
1592
1593 if ((fd = accept(listener, NULL, NULL)) < 0) {
1594 if (errno != EAGAIN && errno != EWOULDBLOCK)
1595 LOG(worker, "Failed to accept client connection : %m");
1596 return;
1597 }
1598
1599 DEBUG(&null_worker,
1600 "<%lu> New Client connection accepted and assigned to worker %02d",
1601 clicount, worker->id);
1602
1603 if (evutil_make_socket_nonblocking(fd) < 0) {
1604 LOG(&null_worker, "Failed to set client socket to non-blocking : %m");
1605 close(fd);
1606 return;
1607 }
1608
1609 if ((client = calloc(1, sizeof(*client))) == NULL) {
1610 LOG(&null_worker, "Failed to allocate memory for client state : %m");
1611 close(fd);
1612 return;
1613 }
1614
1615 client->id = clicount;
1616 client->fd = fd;
1617 client->worker = worker;
1618 client->state = SPOA_ST_CONNECTING;
1619 client->status_code = SPOE_FRM_ERR_NONE;
1620 client->max_frame_size = max_frame_size;
1621 client->engine = NULL;
1622 client->pipelining = false;
1623 client->async = false;
1624 client->incoming_frame = NULL;
1625 client->outgoing_frame = NULL;
1626 LIST_INIT(&client->processing_frames);
1627 LIST_INIT(&client->outgoing_frames);
1628
1629 LIST_ADDQ(&worker->clients, &client->by_worker);
1630
1631 worker->nbclients++;
1632
1633 if (event_assign(&client->read_frame_event, worker->base, fd,
1634 EV_READ|EV_PERSIST, read_frame_cb, client) < 0 ||
1635 event_assign(&client->write_frame_event, worker->base, fd,
1636 EV_WRITE|EV_PERSIST, write_frame_cb, client) < 0) {
1637 LOG(&null_worker, "Failed to create client events");
1638 release_client(client);
1639 return;
1640 }
1641 event_add(&client->read_frame_event, NULL);
1642}
1643
1644static void *
1645worker_function(void *data)
1646{
1647 struct client *client, *cback;
1648 struct spoe_frame *frame, *fback;
1649 struct worker *worker = data;
1650
1651 DEBUG(worker, "Worker ready to process client messages");
1652 event_base_dispatch(worker->base);
Christopher Faulet010fded2016-11-03 22:49:37 +01001653
Christopher Fauletf95b1112016-12-21 08:58:16 +01001654 list_for_each_entry_safe(client, cback, &worker->clients, by_worker) {
1655 release_client(client);
Christopher Faulet010fded2016-11-03 22:49:37 +01001656 }
1657
Christopher Fauletf95b1112016-12-21 08:58:16 +01001658 list_for_each_entry_safe(frame, fback, &worker->frames, list) {
1659 LIST_DEL(&frame->list);
1660 free(frame);
1661 }
1662
1663 event_free(worker->monitor_event);
1664 event_base_free(worker->base);
1665 DEBUG(worker, "Worker is stopped");
1666 pthread_exit(&null_worker);
1667}
1668
1669
1670static int
1671parse_processing_delay(const char *str)
1672{
1673 unsigned long value;
1674
1675 value = 0;
1676 while (1) {
1677 unsigned int j;
1678
1679 j = *str - '0';
1680 if (j > 9)
1681 break;
1682 str++;
1683 value *= 10;
1684 value += j;
1685 }
1686
1687 switch (*str) {
1688 case '\0': /* no unit = millisecond */
1689 value *= 1000;
1690 break;
1691 case 's': /* second */
1692 value *= 1000000;
1693 str++;
1694 break;
1695 case 'm': /* millisecond : "ms" */
1696 if (str[1] != 's')
1697 return -1;
1698 value *= 1000;
1699 str += 2;
1700 break;
1701 case 'u': /* microsecond : "us" */
1702 if (str[1] != 's')
1703 return -1;
1704 str += 2;
1705 break;
1706 default:
1707 return -1;
1708 }
1709 if (*str)
1710 return -1;
1711
1712 processing_delay.tv_sec = (time_t)(value / 1000000);
1713 processing_delay.tv_usec = (suseconds_t)(value % 1000000);
1714 return 0;
Christopher Faulet010fded2016-11-03 22:49:37 +01001715}
1716
Christopher Fauletf95b1112016-12-21 08:58:16 +01001717
Christopher Faulet010fded2016-11-03 22:49:37 +01001718static void
1719usage(char *prog)
1720{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001721 fprintf(stderr,
1722 "Usage : %s [OPTION]...\n"
1723 " -h Print this message\n"
1724 " -d Enable the debug mode\n"
1725 " -m <max-frame-size> Specify the maximum frame size (default : %u)\n"
1726 " -p <port> Specify the port to listen on (default : %d)\n"
1727 " -n <num-workers> Specify the number of workers (default : %d)\n"
1728 " -c <capability> Enable the support of the specified capability\n"
1729 " -t <time> Set a delay to process a message (default: 0)\n"
1730 " The value is specified in milliseconds by default,\n"
1731 " but can be in any other unit if the number is suffixed\n"
1732 " by a unit (us, ms, s)\n"
1733 "\n"
Christopher Faulet85010352017-02-02 10:14:36 +01001734 " Supported capabilities: fragmentation, pipelining, async\n",
Christopher Fauletf95b1112016-12-21 08:58:16 +01001735 prog, MAX_FRAME_SIZE, DEFAULT_PORT, NUM_WORKERS);
Christopher Faulet010fded2016-11-03 22:49:37 +01001736}
1737
1738int
1739main(int argc, char **argv)
1740{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001741 struct event_base *base = NULL;
1742 struct event *signal_event = NULL, *accept_event = NULL;
1743 int opt, i, fd = -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001744
Christopher Fauletf95b1112016-12-21 08:58:16 +01001745 // TODO: add '-t <processing-time>' option
1746 while ((opt = getopt(argc, argv, "hdm:n:p:c:t:")) != -1) {
Christopher Faulet010fded2016-11-03 22:49:37 +01001747 switch (opt) {
1748 case 'h':
1749 usage(argv[0]);
1750 return EXIT_SUCCESS;
1751 case 'd':
1752 debug = true;
1753 break;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001754 case 'm':
1755 max_frame_size = atoi(optarg);
1756 break;
Christopher Faulet010fded2016-11-03 22:49:37 +01001757 case 'n':
Christopher Fauletf95b1112016-12-21 08:58:16 +01001758 num_workers = atoi(optarg);
Christopher Faulet010fded2016-11-03 22:49:37 +01001759 break;
1760 case 'p':
Christopher Fauletf95b1112016-12-21 08:58:16 +01001761 server_port = atoi(optarg);
Christopher Faulet010fded2016-11-03 22:49:37 +01001762 break;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001763 case 'c':
1764 if (!strcmp(optarg, "pipelining"))
1765 pipelining = true;
1766 else if (!strcmp(optarg, "async"))
1767 async = true;
Christopher Faulet85010352017-02-02 10:14:36 +01001768 else if (!strcmp(optarg, "fragmentation"))
1769 fragmentation = true;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001770 else
1771 fprintf(stderr, "WARNING: unsupported capability '%s'\n", optarg);
1772 break;
1773 case 't':
1774 if (!parse_processing_delay(optarg))
1775 break;
1776 fprintf(stderr, "%s: failed to parse time '%s'.\n", argv[0], optarg);
1777 fprintf(stderr, "Try '%s -h' for more information.\n", argv[0]);
1778 return EXIT_FAILURE;
Christopher Faulet010fded2016-11-03 22:49:37 +01001779 default:
1780 usage(argv[0]);
1781 return EXIT_FAILURE;
1782 }
1783 }
1784
Christopher Fauletf95b1112016-12-21 08:58:16 +01001785 if (num_workers <= 0) {
1786 LOG(&null_worker, "%s : Invalid number of workers '%d'\n",
1787 argv[0], num_workers);
Christopher Faulet010fded2016-11-03 22:49:37 +01001788 goto error;
1789 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001790
1791 if (server_port <= 0) {
1792 LOG(&null_worker, "%s : Invalid port '%d'\n",
1793 argv[0], server_port);
Christopher Faulet010fded2016-11-03 22:49:37 +01001794 goto error;
1795 }
1796
Christopher Fauletf95b1112016-12-21 08:58:16 +01001797
1798 if (evthread_use_pthreads() < 0) {
1799 LOG(&null_worker, "No pthreads support for libevent");
Christopher Faulet010fded2016-11-03 22:49:37 +01001800 goto error;
1801 }
1802
Christopher Fauletf95b1112016-12-21 08:58:16 +01001803 if ((base = event_base_new()) == NULL) {
1804 LOG(&null_worker, "Failed to initialize libevent : %m");
1805 goto error;
1806 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001807
Christopher Fauletf95b1112016-12-21 08:58:16 +01001808 signal(SIGPIPE, SIG_IGN);
Christopher Faulet010fded2016-11-03 22:49:37 +01001809
Christopher Fauletf95b1112016-12-21 08:58:16 +01001810 if ((fd = create_server_socket()) < 0) {
1811 LOG(&null_worker, "Failed to create server socket");
1812 goto error;
1813 }
1814 if (evutil_make_socket_nonblocking(fd) < 0) {
1815 LOG(&null_worker, "Failed to set server socket to non-blocking");
Christopher Faulet010fded2016-11-03 22:49:37 +01001816 goto error;
1817 }
1818
Christopher Fauletf95b1112016-12-21 08:58:16 +01001819 if ((workers = calloc(num_workers, sizeof(*workers))) == NULL) {
1820 LOG(&null_worker, "Failed to set allocate memory for workers");
Christopher Faulet010fded2016-11-03 22:49:37 +01001821 goto error;
1822 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001823
1824 for (i = 0; i < num_workers; ++i) {
1825 struct worker *w = &workers[i];
Christopher Faulet010fded2016-11-03 22:49:37 +01001826
Christopher Fauletf95b1112016-12-21 08:58:16 +01001827 w->id = i+1;
1828 w->nbclients = 0;
Christopher Faulet0b89f722018-01-23 14:46:51 +01001829 w->nbframes = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001830 LIST_INIT(&w->engines);
1831 LIST_INIT(&w->clients);
1832 LIST_INIT(&w->frames);
1833
1834 if ((w->base = event_base_new()) == NULL) {
1835 LOG(&null_worker,
1836 "Failed to initialize libevent for worker %02d : %m",
1837 w->id);
1838 goto error;
1839 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001840
Christopher Fauletf95b1112016-12-21 08:58:16 +01001841 w->monitor_event = event_new(w->base, fd, EV_PERSIST,
1842 worker_monitor_cb, (void *)w);
1843 if (w->monitor_event == NULL ||
1844 event_add(w->monitor_event, (struct timeval[]){{5,0}}) < 0) {
1845 LOG(&null_worker,
1846 "Failed to create monitor event for worker %02d",
1847 w->id);
Christopher Faulet010fded2016-11-03 22:49:37 +01001848 goto error;
1849 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001850
1851 if (pthread_create(&w->thread, NULL, worker_function, (void *)w)) {
1852 LOG(&null_worker,
1853 "Failed to start thread for worker %02d : %m",
1854 w->id);
1855 }
1856 DEBUG(&null_worker, "Worker %02d initialized", w->id);
1857 }
1858
1859 accept_event = event_new(base, fd, EV_READ|EV_PERSIST, accept_cb,
1860 (void *)base);
1861 if (accept_event == NULL || event_add(accept_event, NULL) < 0) {
1862 LOG(&null_worker, "Failed to create accept event : %m");
1863 }
1864
1865 signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
1866 if (signal_event == NULL || event_add(signal_event, NULL) < 0) {
1867 LOG(&null_worker, "Failed to create signal event : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001868 }
1869
Christopher Fauletf95b1112016-12-21 08:58:16 +01001870 DEBUG(&null_worker,
1871 "Server is ready"
Christopher Faulet85010352017-02-02 10:14:36 +01001872 " [fragmentation=%s - pipelining=%s - async=%s - debug=%s - max-frame-size=%u]",
1873 (fragmentation?"true":"false"), (pipelining?"true":"false"), (async?"true":"false"),
Christopher Fauletf95b1112016-12-21 08:58:16 +01001874 (debug?"true":"false"), max_frame_size);
1875 event_base_dispatch(base);
1876
1877 for (i = 0; i < num_workers; i++) {
1878 struct worker *w = &workers[i];
1879
1880 pthread_join(w->thread, NULL);
1881 DEBUG(&null_worker, "Worker %02d terminated", w->id);
Christopher Faulet010fded2016-11-03 22:49:37 +01001882 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001883
1884 free(workers);
1885 event_free(signal_event);
1886 event_free(accept_event);
1887 event_base_free(base);
1888 close(fd);
Christopher Faulet010fded2016-11-03 22:49:37 +01001889 return EXIT_SUCCESS;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001890
1891 error:
1892 if (workers != NULL)
1893 free(workers);
1894 if (signal_event != NULL)
1895 event_free(signal_event);
1896 if (accept_event != NULL)
1897 event_free(accept_event);
1898 if (base != NULL)
1899 event_base_free(base);
1900 if (fd != -1)
1901 close(fd);
Christopher Faulet010fded2016-11-03 22:49:37 +01001902 return EXIT_FAILURE;
1903}