blob: ee2e3de54975ca681dda3e5bb73870795848c951 [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
46#define SPOP_VERSION "1.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 Faulet010fded2016-11-03 22:49:37 +0100153};
154
Christopher Fauletf95b1112016-12-21 08:58:16 +0100155
Christopher Fauletf95b1112016-12-21 08:58:16 +0100156/* Globals */
157static struct worker *workers = NULL;
158static struct worker null_worker = { .id = 0 };
159static unsigned long clicount = 0;
160static int server_port = DEFAULT_PORT;
161static int num_workers = NUM_WORKERS;
162static unsigned int max_frame_size = MAX_FRAME_SIZE;
163struct timeval processing_delay = {0, 0};
164static bool debug = false;
165static bool pipelining = false;
166static bool async = false;
Christopher Faulet85010352017-02-02 10:14:36 +0100167static bool fragmentation = false;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100168
169
170static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Faulet85010352017-02-02 10:14:36 +0100171 [SPOE_FRM_ERR_NONE] = "normal",
172 [SPOE_FRM_ERR_IO] = "I/O error",
173 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
174 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
175 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
176 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
177 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
178 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
179 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
180 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
181 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
182 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100183 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Faulet85010352017-02-02 10:14:36 +0100184 [SPOE_FRM_ERR_RES] = "resource allocation error",
185 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf95b1112016-12-21 08:58:16 +0100186};
187
188static void signal_cb(evutil_socket_t, short, void *);
189static void accept_cb(evutil_socket_t, short, void *);
190static void worker_monitor_cb(evutil_socket_t, short, void *);
191static void process_frame_cb(evutil_socket_t, short, void *);
192static void read_frame_cb(evutil_socket_t, short, void *);
193static void write_frame_cb(evutil_socket_t, short, void *);
194
195static void use_spoe_engine(struct client *);
196static void unuse_spoe_engine(struct client *);
197static void release_frame(struct spoe_frame *);
198static void release_client(struct client *);
Christopher Faulet010fded2016-11-03 22:49:37 +0100199
200static void
Christopher Fauletf95b1112016-12-21 08:58:16 +0100201check_ipv4_reputation(struct spoe_frame *frame, struct in_addr *ipv4)
Christopher Faulet010fded2016-11-03 22:49:37 +0100202{
203 char str[INET_ADDRSTRLEN];
204
205 if (inet_ntop(AF_INET, ipv4, str, INET_ADDRSTRLEN) == NULL)
206 return;
207
Christopher Fauletf95b1112016-12-21 08:58:16 +0100208 frame->ip_score = random() % 100;
Christopher Faulet010fded2016-11-03 22:49:37 +0100209
Christopher Fauletf95b1112016-12-21 08:58:16 +0100210 DEBUG(frame->worker, "IP score for %.*s is %d",
211 INET_ADDRSTRLEN, str, frame->ip_score);
Christopher Faulet010fded2016-11-03 22:49:37 +0100212}
213
214static void
Christopher Fauletf95b1112016-12-21 08:58:16 +0100215check_ipv6_reputation(struct spoe_frame *frame, struct in6_addr *ipv6)
Christopher Faulet010fded2016-11-03 22:49:37 +0100216{
217 char str[INET6_ADDRSTRLEN];
218
219 if (inet_ntop(AF_INET6, ipv6, str, INET6_ADDRSTRLEN) == NULL)
220 return;
221
Christopher Fauletf95b1112016-12-21 08:58:16 +0100222 frame->ip_score = random() % 100;
Christopher Faulet010fded2016-11-03 22:49:37 +0100223
Christopher Fauletf95b1112016-12-21 08:58:16 +0100224 DEBUG(frame->worker, "IP score for %.*s is %d",
225 INET6_ADDRSTRLEN, str, frame->ip_score);
Christopher Faulet010fded2016-11-03 22:49:37 +0100226}
227
Christopher Faulet010fded2016-11-03 22:49:37 +0100228
229/* Check the protocol version. It returns -1 if an error occurred, the number of
230 * read bytes otherwise. */
231static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100232check_proto_version(struct spoe_frame *frame, char **buf, char *end)
Christopher Faulet010fded2016-11-03 22:49:37 +0100233{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100234 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100235 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100236 int ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100237
238 /* Get the list of all supported versions by HAProxy */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100239 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Faulet010fded2016-11-03 22:49:37 +0100240 return -1;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100241 ret = spoe_decode_buffer(&p, end, &str, &sz);
242 if (ret == -1 || !str)
Christopher Faulet010fded2016-11-03 22:49:37 +0100243 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100244
245 DEBUG(frame->worker, "<%lu> Supported versions : %.*s",
246 frame->client->id, (int)sz, str);
Christopher Faulet010fded2016-11-03 22:49:37 +0100247
248 /* TODO: Find the right verion in supported ones */
249
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100250 ret = (p - *buf);
251 *buf = p;
252 return ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100253}
254
255/* Check max frame size value. It returns -1 if an error occurred, the number of
256 * read bytes otherwise. */
257static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100258check_max_frame_size(struct spoe_frame *frame, char **buf, char *end)
Christopher Faulet010fded2016-11-03 22:49:37 +0100259{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100260 char *p = *buf;
Christopher Faulet010fded2016-11-03 22:49:37 +0100261 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100262 int type, ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100263
264 /* Get the max-frame-size value of HAProxy */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100265 type = *p++;
Christopher Faulet010fded2016-11-03 22:49:37 +0100266 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
267 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
268 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
Christopher Fauletf95b1112016-12-21 08:58:16 +0100269 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64)
Christopher Faulet010fded2016-11-03 22:49:37 +0100270 return -1;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200271 if (decode_varint(&p, end, &sz) == -1)
Christopher Faulet010fded2016-11-03 22:49:37 +0100272 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100273
274 /* Keep the lower value */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100275 if (sz < frame->client->max_frame_size)
276 frame->client->max_frame_size = sz;
277
278 DEBUG(frame->worker, "<%lu> HAProxy maximum frame size : %u",
279 frame->client->id, (unsigned int)sz);
Christopher Faulet010fded2016-11-03 22:49:37 +0100280
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100281 ret = (p - *buf);
282 *buf = p;
283 return ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100284}
285
Christopher Fauletba7bc162016-11-07 21:07:38 +0100286/* Check healthcheck value. It returns -1 if an error occurred, the number of
287 * read bytes otherwise. */
288static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100289check_healthcheck(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100290{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100291 char *p = *buf;
292 int type, ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100293
Christopher Fauletf95b1112016-12-21 08:58:16 +0100294 /* Get the "healthcheck" value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100295 type = *p++;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100296 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_BOOL)
297 return -1;
298 frame->hcheck = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE);
299
300 DEBUG(frame->worker, "<%lu> HELLO healthcheck : %s",
301 frame->client->id, (frame->hcheck ? "true" : "false"));
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100302
303 ret = (p - *buf);
304 *buf = p;
305 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100306}
307
308/* Check capabilities value. It returns -1 if an error occurred, the number of
309 * read bytes otherwise. */
310static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100311check_capabilities(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100312{
313 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100314 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100315 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100316 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100317
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100318 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100319 return -1;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100320 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
321 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100322 if (str == NULL) /* this is not an error */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100323 goto end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100324
325 DEBUG(frame->worker, "<%lu> HAProxy capabilities : %.*s",
326 client->id, (int)sz, str);
327
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100328 while (sz) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100329 char *delim;
330
331 /* Skip leading spaces */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100332 for (; isspace(*str) && sz; sz--);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100333
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100334 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
335 str += 10; sz -= 10;
336 if (!sz || isspace(*str) || *str == ',') {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100337 DEBUG(frame->worker,
338 "<%lu> HAProxy supports frame pipelining",
339 client->id);
340 client->pipelining = true;
341 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100342 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100343 else if (sz >= 5 && !strncmp(str, "async", 5)) {
344 str += 5; sz -= 5;
345 if (!sz || isspace(*str) || *str == ',') {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100346 DEBUG(frame->worker,
347 "<%lu> HAProxy supports asynchronous frame",
348 client->id);
349 client->async = true;
350 }
351 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100352 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
353 str += 13; sz -= 13;
354 if (!sz || isspace(*str) || *str == ',') {
Christopher Faulet85010352017-02-02 10:14:36 +0100355 DEBUG(frame->worker,
356 "<%lu> HAProxy supports fragmented frame",
357 client->id);
358 client->fragmentation = true;
359 }
360 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100361
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100362 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100363 break;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100364 delim++;
365 sz -= (delim - str);
366 str = delim;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100367 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100368 end:
369 ret = (p - *buf);
370 *buf = p;
371 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100372}
373
374/* Check engine-id value. It returns -1 if an error occurred, the number of
375 * read bytes otherwise. */
376static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100377check_engine_id(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100378{
379 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100380 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100381 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100382 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100383
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100384 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100385 return -1;
386
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100387 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
388 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100389 if (str == NULL) /* this is not an error */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100390 goto end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100391
392 if (client->engine != NULL)
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100393 goto end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100394
395 DEBUG(frame->worker, "<%lu> HAProxy engine id : %.*s",
396 client->id, (int)sz, str);
397
398 client->engine_id = strndup(str, (int)sz);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100399 end:
400 ret = (p - *buf);
401 *buf = p;
402 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100403}
404
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100405static int
406acc_payload(struct spoe_frame *frame)
407{
408 struct client *client = frame->client;
409 char *buf;
410 size_t len = frame->len - frame->offset;
411 int ret = frame->offset;
412
413 /* No need to accumulation payload */
414 if (frame->fragmented == false)
415 return ret;
416
417 buf = realloc(frame->frag_buf, frame->frag_len + len);
418 if (buf == NULL) {
419 client->status_code = SPOE_FRM_ERR_RES;
420 return -1;
421 }
422 memcpy(buf + frame->frag_len, frame->buf + frame->offset, len);
423 frame->frag_buf = buf;
424 frame->frag_len += len;
425
426 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
427 /* Wait for next parts */
428 frame->buf = (char *)(frame->data);
429 frame->offset = 0;
430 frame->len = 0;
431 frame->flags = 0;
432 return 1;
433 }
434
435 frame->buf = frame->frag_buf;
436 frame->len = frame->frag_len;
437 frame->offset = 0;
438 return ret;
439}
440
Christopher Fauletf95b1112016-12-21 08:58:16 +0100441/* Check disconnect status code. It returns -1 if an error occurred, the number
442 * of read bytes otherwise. */
443static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100444check_discon_status_code(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100445{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100446 char *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100447 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100448 int type, ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100449
450 /* Get the "status-code" value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100451 type = *p++;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100452 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
453 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
454 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
455 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64)
456 return -1;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200457 if (decode_varint(&p, end, &sz) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100458 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100459
460 frame->client->status_code = (unsigned int)sz;
461
462 DEBUG(frame->worker, "<%lu> Disconnect status code : %u",
463 frame->client->id, frame->client->status_code);
464
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100465 ret = (p - *buf);
466 *buf = p;
467 return ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100468}
469
Christopher Fauletf95b1112016-12-21 08:58:16 +0100470/* Check the disconnect message. It returns -1 if an error occurred, the number
471 * of read bytes otherwise. */
472static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100473check_discon_message(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100474{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100475 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100476 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100477 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100478
479 /* Get the "message" value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100480 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100481 return -1;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100482 ret = spoe_decode_buffer(&p, end, &str, &sz);
483 if (ret == -1 || !str)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100484 return -1;
485
486 DEBUG(frame->worker, "<%lu> Disconnect message : %.*s",
487 frame->client->id, (int)sz, str);
488
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100489 ret = (p - *buf);
490 *buf = p;
491 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100492}
Christopher Fauletba7bc162016-11-07 21:07:38 +0100493
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100494
495
Christopher Faulet010fded2016-11-03 22:49:37 +0100496/* Decode a HELLO frame received from HAProxy. It returns -1 if an error
Christopher Fauletf95b1112016-12-21 08:58:16 +0100497 * occurred, otherwise the number of read bytes. HELLO frame cannot be
498 * ignored and having another frame than a HELLO frame is an error. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100499static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100500handle_hahello(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100501{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100502 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100503 char *p, *end;
504
505 p = frame->buf;
506 end = frame->buf + frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100507
Christopher Fauletf95b1112016-12-21 08:58:16 +0100508 /* Check frame type: we really want a HELLO frame */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100509 if (*p++ != SPOE_FRM_T_HAPROXY_HELLO)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100510 goto error;
511
512 DEBUG(frame->worker, "<%lu> Decode HAProxy HELLO frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100513
Christopher Faulet85010352017-02-02 10:14:36 +0100514 /* Retrieve flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100515 memcpy((char *)&(frame->flags), p, 4);
516 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100517
Christopher Faulet85010352017-02-02 10:14:36 +0100518 /* Fragmentation is not supported for HELLO frame */
519 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
520 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
521 goto error;
522 }
523
Christopher Faulet010fded2016-11-03 22:49:37 +0100524 /* stream-id and frame-id must be cleared */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100525 if (*p != 0 || *(p+1) != 0) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100526 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100527 goto error;
528 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100529 p += 2;
Christopher Faulet010fded2016-11-03 22:49:37 +0100530
531 /* Loop on K/V items */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100532 while (p < end) {
Christopher Faulet010fded2016-11-03 22:49:37 +0100533 char *str;
534 uint64_t sz;
535
536 /* Decode the item name */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100537 spoe_decode_buffer(&p, end, &str, &sz);
538 if (!str) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100539 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100540 goto error;
541 }
542
543 /* Check "supported-versions" K/V item */
544 if (!memcmp(str, "supported-versions", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100545 if (check_proto_version(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100546 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100547 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100548 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100549 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100550 /* Check "max-frame-size" K/V item */
Christopher Faulet010fded2016-11-03 22:49:37 +0100551 else if (!memcmp(str, "max-frame-size", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100552 if (check_max_frame_size(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100553 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100554 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100555 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100556 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100557 /* Check "healthcheck" K/V item */
Christopher Fauletba7bc162016-11-07 21:07:38 +0100558 else if (!memcmp(str, "healthcheck", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100559 if (check_healthcheck(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100560 client->status_code = SPOE_FRM_ERR_INVALID;
561 goto error;
562 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100563 }
564 /* Check "capabilities" K/V item */
565 else if (!memcmp(str, "capabilities", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100566 if (check_capabilities(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100567 client->status_code = SPOE_FRM_ERR_INVALID;
568 goto error;
569 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100570 }
571 /* Check "engine-id" K/V item */
572 else if (!memcmp(str, "engine-id", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100573 if (check_engine_id(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100574 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100575 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100576 }
Christopher Fauletba7bc162016-11-07 21:07:38 +0100577 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100578 else {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100579 DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s",
580 client->id, (int)sz, str);
581
Christopher Faulet010fded2016-11-03 22:49:37 +0100582 /* Silently ignore unknown item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100583 if (spoe_skip_data(&p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100584 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100585 goto error;
586 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100587 }
588 }
589
Christopher Fauletf95b1112016-12-21 08:58:16 +0100590 if (async == false || client->engine_id == NULL)
591 client->async = false;
592 if (pipelining == false)
593 client->pipelining = false;
594
595 if (client->async == true)
596 use_spoe_engine(client);
597
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100598 return (p - frame->buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100599 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100600 return -1;
601}
602
603/* Decode a DISCONNECT frame received from HAProxy. It returns -1 if an error
Christopher Fauletf95b1112016-12-21 08:58:16 +0100604 * occurred, otherwise the number of read bytes. DISCONNECT frame cannot be
605 * ignored and having another frame than a DISCONNECT frame is an error.*/
Christopher Faulet010fded2016-11-03 22:49:37 +0100606static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100607handle_hadiscon(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100608{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100609 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100610 char *p, *end;
611
612 p = frame->buf;
613 end = frame->buf + frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100614
Christopher Fauletf95b1112016-12-21 08:58:16 +0100615 /* Check frame type: we really want a DISCONNECT frame */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100616 if (*p++ != SPOE_FRM_T_HAPROXY_DISCON)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100617 goto error;
618
619 DEBUG(frame->worker, "<%lu> Decode HAProxy DISCONNECT frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100620
Christopher Faulet85010352017-02-02 10:14:36 +0100621 /* Retrieve flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100622 memcpy((char *)&(frame->flags), p, 4);
623 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100624
Christopher Faulet85010352017-02-02 10:14:36 +0100625 /* Fragmentation is not supported for DISCONNECT frame */
626 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
627 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
628 goto error;
629 }
630
Christopher Faulet010fded2016-11-03 22:49:37 +0100631 /* stream-id and frame-id must be cleared */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100632 if (*p != 0 || *(p+1) != 0) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100633 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100634 goto error;
635 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100636 p += 2;
Christopher Faulet010fded2016-11-03 22:49:37 +0100637
Christopher Fauletf95b1112016-12-21 08:58:16 +0100638 client->status_code = SPOE_FRM_ERR_NONE;
639
Christopher Faulet010fded2016-11-03 22:49:37 +0100640 /* Loop on K/V items */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100641 while (p < end) {
Christopher Faulet010fded2016-11-03 22:49:37 +0100642 char *str;
643 uint64_t sz;
644
645 /* Decode item key */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100646 spoe_decode_buffer(&p, end, &str, &sz);
647 if (!str) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100648 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100649 goto error;
650 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100651
652 /* Check "status-code" K/V item */
653 if (!memcmp(str, "status-code", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100654 if (check_discon_status_code(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100655 client->status_code = SPOE_FRM_ERR_INVALID;
656 goto error;
657 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100658 }
659 /* Check "message" K/V item */
660 else if (!memcmp(str, "message", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100661 if (check_discon_message(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100662 client->status_code = SPOE_FRM_ERR_INVALID;
663 goto error;
664 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100665 }
666 else {
667 DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s",
668 client->id, (int)sz, str);
669
670 /* Silently ignore unknown item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100671 if (spoe_skip_data(&p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100672 client->status_code = SPOE_FRM_ERR_INVALID;
673 goto error;
674 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100675 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100676 }
677
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100678 return (p - frame->buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100679 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100680 return -1;
681}
682
683/* Decode a NOTIFY frame received from HAProxy. It returns -1 if an error
Christopher Faulet85010352017-02-02 10:14:36 +0100684 * occurred, 0 if it must be must be ignored, otherwise the number of read
685 * bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100686static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100687handle_hanotify(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100688{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100689 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100690 char *p, *end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100691 uint64_t stream_id, frame_id;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100692
693 p = frame->buf;
694 end = frame->buf + frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100695
696 /* Check frame type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100697 if (*p++ != SPOE_FRM_T_HAPROXY_NOTIFY)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100698 goto ignore;
699
700 DEBUG(frame->worker, "<%lu> Decode HAProxy NOTIFY frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100701
Christopher Faulet85010352017-02-02 10:14:36 +0100702 /* Retrieve flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100703 memcpy((char *)&(frame->flags), p, 4);
704 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100705
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100706 /* Fragmentation is not supported */
Christopher Faulet85010352017-02-02 10:14:36 +0100707 if (!(frame->flags & SPOE_FRM_FL_FIN) && fragmentation == false) {
708 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
709 goto error;
710 }
711
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100712 /* Read the stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200713 if (decode_varint(&p, end, &stream_id) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100714 goto ignore;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200715 if (decode_varint(&p, end, &frame_id) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100716 goto ignore;
Christopher Faulet010fded2016-11-03 22:49:37 +0100717
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100718 frame->stream_id = (unsigned int)stream_id;
719 frame->frame_id = (unsigned int)frame_id;
Christopher Faulet010fded2016-11-03 22:49:37 +0100720
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100721 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
722 " - %s frame received"
723 " - frag_len=%u - len=%u - offset=%ld",
724 client->id, frame->stream_id, frame->frame_id,
725 (frame->flags & SPOE_FRM_FL_FIN) ? "unfragmented" : "fragmented",
726 frame->frag_len, frame->len, p - frame->buf);
727
728 frame->fragmented = !(frame->flags & SPOE_FRM_FL_FIN);
729 frame->offset = (p - frame->buf);
730 return acc_payload(frame);
731
732 ignore:
733 return 0;
734
735 error:
736 return -1;
737}
738
739/* Decode next part of a fragmented frame received from HAProxy. It returns -1
740 * if an error occurred, 0 if it must be must be ignored, otherwise the number
741 * of read bytes. */
742static int
743handle_hafrag(struct spoe_frame *frame)
744{
745 struct client *client = frame->client;
746 char *p, *end;
747 uint64_t stream_id, frame_id;
748
749 p = frame->buf;
750 end = frame->buf + frame->len;
751
752 /* Check frame type */
753 if (*p++ != SPOE_FRM_T_UNSET)
754 goto ignore;
755
756 DEBUG(frame->worker, "<%lu> Decode Next part of a fragmented frame", client->id);
757
758 /* Fragmentation is not supported */
759 if (fragmentation == false) {
760 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
761 goto error;
Christopher Faulet85010352017-02-02 10:14:36 +0100762 }
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100763
764 /* Retrieve flags */
765 memcpy((char *)&(frame->flags), p, 4);
766 p+= 4;
767
768 /* Read the stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200769 if (decode_varint(&p, end, &stream_id) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100770 goto ignore;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200771 if (decode_varint(&p, end, &frame_id) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100772 goto ignore;
Christopher Faulet010fded2016-11-03 22:49:37 +0100773
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100774 if (frame->fragmented == false ||
775 frame->stream_id != (unsigned int)stream_id ||
776 frame->frame_id != (unsigned int)frame_id) {
777 client->status_code = SPOE_FRM_ERR_INTERLACED_FRAMES;
778 goto error;
779 }
780
781 if (frame->flags & SPOE_FRM_FL_ABRT) {
Christopher Faulet85010352017-02-02 10:14:36 +0100782 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100783 " - Abort processing of a fragmented frame"
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100784 " - frag_len=%u - len=%u - offset=%ld",
Christopher Faulet85010352017-02-02 10:14:36 +0100785 client->id, frame->stream_id, frame->frame_id,
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100786 frame->frag_len, frame->len, p - frame->buf);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100787 goto ignore;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100788 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100789
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100790 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
791 " - %s fragment of a fragmented frame received"
792 " - frag_len=%u - len=%u - offset=%ld",
793 client->id, frame->stream_id, frame->frame_id,
794 (frame->flags & SPOE_FRM_FL_FIN) ? "last" : "next",
795 frame->frag_len, frame->len, p - frame->buf);
796
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100797 frame->offset = (p - frame->buf);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100798 return acc_payload(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +0100799
Christopher Fauletf95b1112016-12-21 08:58:16 +0100800 ignore:
Christopher Faulet85010352017-02-02 10:14:36 +0100801 return 0;
802
803 error:
Christopher Fauletf95b1112016-12-21 08:58:16 +0100804 return -1;
805}
Christopher Faulet010fded2016-11-03 22:49:37 +0100806
Christopher Fauletf95b1112016-12-21 08:58:16 +0100807/* Encode a HELLO frame to send it to HAProxy. It returns the number of written
808 * bytes. */
809static int
810prepare_agenthello(struct spoe_frame *frame)
811{
812 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100813 char *p, *end;
Christopher Faulet85010352017-02-02 10:14:36 +0100814 char capabilities[64];
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100815 int n;
Christopher Faulet85010352017-02-02 10:14:36 +0100816 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet010fded2016-11-03 22:49:37 +0100817
Christopher Fauletf95b1112016-12-21 08:58:16 +0100818 DEBUG(frame->worker, "<%lu> Encode Agent HELLO frame", client->id);
819 frame->type = SPOA_FRM_T_AGENT;
Christopher Faulet010fded2016-11-03 22:49:37 +0100820
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100821 p = frame->buf;
822 end = frame->buf+max_frame_size;
823
Christopher Faulet010fded2016-11-03 22:49:37 +0100824 /* Frame Type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100825 *p++ = SPOE_FRM_T_AGENT_HELLO;
Christopher Faulet010fded2016-11-03 22:49:37 +0100826
Christopher Faulet85010352017-02-02 10:14:36 +0100827 /* Set flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100828 memcpy(p, (char *)&flags, 4);
829 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100830
831 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100832 *p++ = 0;
833 *p++ = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +0100834
835 /* "version" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100836 spoe_encode_buffer("version", 7, &p, end);
837 *p++ = SPOE_DATA_T_STR;
838 spoe_encode_buffer(SPOP_VERSION, SLEN(SPOP_VERSION), &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100839 DEBUG(frame->worker, "<%lu> Agent version : %s",
840 client->id, SPOP_VERSION);
841
Christopher Faulet010fded2016-11-03 22:49:37 +0100842
843 /* "max-frame-size" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100844 spoe_encode_buffer("max-frame-size", 14, &p ,end);
845 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200846 encode_varint(client->max_frame_size, &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100847 DEBUG(frame->worker, "<%lu> Agent maximum frame size : %u",
848 client->id, client->max_frame_size);
Christopher Faulet010fded2016-11-03 22:49:37 +0100849
850 /* "capabilities" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100851 spoe_encode_buffer("capabilities", 12, &p, end);
852 *p++ = SPOE_DATA_T_STR;
Christopher Faulet85010352017-02-02 10:14:36 +0100853
854 memset(capabilities, 0, sizeof(capabilities));
855 n = 0;
856
857 /* 1. Fragmentation capability ? */
858 if (fragmentation == true) {
859 memcpy(capabilities, "fragmentation", 13);
860 n += 13;
861 }
862 /* 2. Pipelining capability ? */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100863 if (client->pipelining == true) {
864 if (n) capabilities[n++] = ',';
865 memcpy(capabilities + n, "pipelining", 10);
Christopher Faulet85010352017-02-02 10:14:36 +0100866 n += 10;
867 }
868 /* 3. Async capability ? */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100869 if (client->async == true) {
870 if (n) capabilities[n++] = ',';
871 memcpy(capabilities + n, "async", 5);
Christopher Faulet85010352017-02-02 10:14:36 +0100872 n += 5;
873 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100874 spoe_encode_buffer(capabilities, n, &p, end);
Christopher Faulet010fded2016-11-03 22:49:37 +0100875
Christopher Faulet85010352017-02-02 10:14:36 +0100876 DEBUG(frame->worker, "<%lu> Agent capabilities : %.*s",
877 client->id, n, capabilities);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100878
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100879 frame->len = (p - frame->buf);
880 return frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100881}
882
Christopher Fauletf95b1112016-12-21 08:58:16 +0100883/* Encode a DISCONNECT frame to send it to HAProxy. It returns the number of
884 * written bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100885static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100886prepare_agentdicon(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100887{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100888 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100889 char *p, *end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100890 const char *reason;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100891 int rlen;
Christopher Faulet85010352017-02-02 10:14:36 +0100892 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100893
894 DEBUG(frame->worker, "<%lu> Encode Agent DISCONNECT frame", client->id);
895 frame->type = SPOA_FRM_T_AGENT;
896
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100897 p = frame->buf;
898 end = frame->buf+max_frame_size;
899
Christopher Fauletf95b1112016-12-21 08:58:16 +0100900 if (client->status_code >= SPOE_FRM_ERRS)
901 client->status_code = SPOE_FRM_ERR_UNKNOWN;
902 reason = spoe_frm_err_reasons[client->status_code];
903 rlen = strlen(reason);
Christopher Faulet010fded2016-11-03 22:49:37 +0100904
905 /* Frame type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100906 *p++ = SPOE_FRM_T_AGENT_DISCON;
Christopher Faulet010fded2016-11-03 22:49:37 +0100907
Christopher Faulet85010352017-02-02 10:14:36 +0100908 /* Set flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100909 memcpy(p, (char *)&flags, 4);
910 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100911
Christopher Fauletf95b1112016-12-21 08:58:16 +0100912 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100913 *p++ = 0;
914 *p++ = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100915
916 /* There are 2 mandatory items: "status-code" and "message" */
917
918 /* "status-code" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100919 spoe_encode_buffer("status-code", 11, &p, end);
920 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200921 encode_varint(client->status_code, &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100922 DEBUG(frame->worker, "<%lu> Disconnect status code : %u",
923 client->id, client->status_code);
Christopher Faulet010fded2016-11-03 22:49:37 +0100924
Christopher Fauletf95b1112016-12-21 08:58:16 +0100925 /* "message" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100926 spoe_encode_buffer("message", 7, &p, end);
927 *p++ = SPOE_DATA_T_STR;
928 spoe_encode_buffer(reason, rlen, &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100929 DEBUG(frame->worker, "<%lu> Disconnect message : %s",
930 client->id, reason);
Christopher Faulet010fded2016-11-03 22:49:37 +0100931
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100932 frame->len = (p - frame->buf);
933 return frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100934}
935
Christopher Fauletf95b1112016-12-21 08:58:16 +0100936/* Encode a ACK frame to send it to HAProxy. It returns the number of written
937 * bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100938static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100939prepare_agentack(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100940{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100941 char *p, *end;
Christopher Faulet85010352017-02-02 10:14:36 +0100942 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet010fded2016-11-03 22:49:37 +0100943
Christopher Fauletf95b1112016-12-21 08:58:16 +0100944 /* Be careful here, in async mode, frame->client can be NULL */
945
946 DEBUG(frame->worker, "Encode Agent ACK frame");
947 frame->type = SPOA_FRM_T_AGENT;
Christopher Faulet010fded2016-11-03 22:49:37 +0100948
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100949 p = frame->buf;
950 end = frame->buf+max_frame_size;
951
Christopher Faulet010fded2016-11-03 22:49:37 +0100952 /* Frame type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100953 *p++ = SPOE_FRM_T_AGENT_ACK;
Christopher Faulet010fded2016-11-03 22:49:37 +0100954
Christopher Faulet85010352017-02-02 10:14:36 +0100955 /* Set flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100956 memcpy(p, (char *)&flags, 4);
957 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100958
Christopher Fauletf95b1112016-12-21 08:58:16 +0100959 /* Set stream-id and frame-id for ACK frames */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200960 encode_varint(frame->stream_id, &p, end);
961 encode_varint(frame->frame_id, &p, end);
Christopher Faulet010fded2016-11-03 22:49:37 +0100962
Christopher Fauletf95b1112016-12-21 08:58:16 +0100963 DEBUG(frame->worker, "STREAM-ID=%u - FRAME-ID=%u",
964 frame->stream_id, frame->frame_id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100965
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100966 frame->len = (p - frame->buf);
967 return frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100968}
969
970static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100971create_server_socket(void)
Christopher Faulet010fded2016-11-03 22:49:37 +0100972{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100973 struct sockaddr_in listen_addr;
974 int fd, yes = 1;
975
976 fd = socket(AF_INET, SOCK_STREAM, 0);
977 if (fd < 0) {
978 LOG(&null_worker, "Failed to create service socket : %m");
979 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100980 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100981
982 memset(&listen_addr, 0, sizeof(listen_addr));
983 listen_addr.sin_family = AF_INET;
984 listen_addr.sin_addr.s_addr = INADDR_ANY;
985 listen_addr.sin_port = htons(server_port);
986
987 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0 ||
988 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) < 0) {
989 LOG(&null_worker, "Failed to set option on server socket : %m");
990 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100991 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100992
993 if (bind(fd, (struct sockaddr *) &listen_addr, sizeof(listen_addr)) < 0) {
994 LOG(&null_worker, "Failed to bind server socket : %m");
995 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100996 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100997
998 if (listen(fd, CONNECTION_BACKLOG) < 0) {
999 LOG(&null_worker, "Failed to listen on server socket : %m");
1000 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001001 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001002
1003 return fd;
Christopher Faulet010fded2016-11-03 22:49:37 +01001004}
1005
Christopher Fauletf95b1112016-12-21 08:58:16 +01001006static void
1007release_frame(struct spoe_frame *frame)
1008{
1009 struct worker *worker;
1010
1011 if (frame == NULL)
1012 return;
1013
1014 if (event_pending(&frame->process_frame_event, EV_TIMEOUT, NULL))
1015 event_del(&frame->process_frame_event);
1016
1017 worker = frame->worker;
1018 LIST_DEL(&frame->list);
Christopher Faulet85010352017-02-02 10:14:36 +01001019 if (frame->frag_buf)
1020 free(frame->frag_buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001021 memset(frame, 0, sizeof(*frame)+max_frame_size+4);
1022 LIST_ADDQ(&worker->frames, &frame->list);
1023}
1024
1025static void
1026release_client(struct client *c)
Christopher Faulet010fded2016-11-03 22:49:37 +01001027{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001028 struct spoe_frame *frame, *back;
1029
1030 if (c == NULL)
1031 return;
1032
1033 DEBUG(c->worker, "<%lu> Release client", c->id);
1034
1035 LIST_DEL(&c->by_worker);
1036 c->worker->nbclients--;
1037
1038 unuse_spoe_engine(c);
1039 free(c->engine_id);
1040
1041 if (event_pending(&c->read_frame_event, EV_READ, NULL))
1042 event_del(&c->read_frame_event);
1043 if (event_pending(&c->write_frame_event, EV_WRITE, NULL))
1044 event_del(&c->write_frame_event);
1045
1046 release_frame(c->incoming_frame);
1047 release_frame(c->outgoing_frame);
1048 list_for_each_entry_safe(frame, back, &c->processing_frames, list) {
1049 release_frame(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +01001050 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001051 list_for_each_entry_safe(frame, back, &c->outgoing_frames, list) {
1052 release_frame(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +01001053 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001054
1055 if (c->fd >= 0)
1056 close(c->fd);
1057
1058 free(c);
1059}
1060
1061static void
1062reset_frame(struct spoe_frame *frame)
1063{
1064 if (frame == NULL)
1065 return;
1066
Christopher Faulet85010352017-02-02 10:14:36 +01001067 if (frame->frag_buf)
1068 free(frame->frag_buf);
1069
1070 frame->type = SPOA_FRM_T_UNKNOWN;
1071 frame->buf = (char *)(frame->data);
1072 frame->offset = 0;
1073 frame->len = 0;
1074 frame->stream_id = 0;
1075 frame->frame_id = 0;
1076 frame->flags = 0;
1077 frame->hcheck = false;
1078 frame->fragmented = false;
1079 frame->ip_score = -1;
1080 frame->frag_buf = NULL;
1081 frame->frag_len = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001082 LIST_INIT(&frame->list);
1083}
1084
1085static void
1086use_spoe_engine(struct client *client)
1087{
1088 struct spoe_engine *eng;
1089
1090 if (client->engine_id == NULL)
1091 return;
1092
1093 list_for_each_entry(eng, &client->worker->engines, list) {
1094 if (!strcmp(eng->id, client->engine_id))
1095 goto end;
1096 }
1097
1098 if ((eng = malloc(sizeof(*eng))) == NULL) {
1099 client->async = false;
1100 return;
1101 }
1102
1103 eng->id = strdup(client->engine_id);
1104 LIST_INIT(&eng->clients);
1105 LIST_INIT(&eng->processing_frames);
1106 LIST_INIT(&eng->outgoing_frames);
1107 LIST_ADDQ(&client->worker->engines, &eng->list);
1108 LOG(client->worker, "Add new SPOE engine '%s'", eng->id);
1109
1110 end:
1111 client->engine = eng;
1112 LIST_ADDQ(&eng->clients, &client->by_engine);
1113}
1114
1115static void
1116unuse_spoe_engine(struct client *client)
1117{
1118 struct spoe_engine *eng;
1119 struct spoe_frame *frame, *back;
1120
1121 if (client == NULL || client->engine == NULL)
1122 return;
1123
1124 eng = client->engine;
1125 client->engine = NULL;
1126 LIST_DEL(&client->by_engine);
1127 if (!LIST_ISEMPTY(&eng->clients))
1128 return;
1129
1130 LOG(client->worker, "Remove SPOE engine '%s'", eng->id);
1131 LIST_DEL(&eng->list);
1132
1133 list_for_each_entry_safe(frame, back, &eng->processing_frames, list) {
1134 release_frame(frame);
1135 }
1136 list_for_each_entry_safe(frame, back, &eng->outgoing_frames, list) {
1137 release_frame(frame);
1138 }
1139 free(eng->id);
1140 free(eng);
1141}
1142
1143
1144static struct spoe_frame *
1145acquire_incoming_frame(struct client *client)
1146{
1147 struct spoe_frame *frame;
1148
1149 frame = client->incoming_frame;
1150 if (frame != NULL)
1151 return frame;
1152
1153 if (LIST_ISEMPTY(&client->worker->frames)) {
1154 if ((frame = calloc(1, sizeof(*frame)+max_frame_size+4)) == NULL) {
1155 LOG(client->worker, "Failed to allocate new frame : %m");
1156 return NULL;
1157 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001158 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001159 else {
1160 frame = LIST_NEXT(&client->worker->frames, typeof(frame), list);
1161 LIST_DEL(&frame->list);
Christopher Faulet010fded2016-11-03 22:49:37 +01001162 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001163
1164 reset_frame(frame);
1165 frame->worker = client->worker;
1166 frame->engine = client->engine;
1167 frame->client = client;
1168
1169 if (event_assign(&frame->process_frame_event, client->worker->base, -1,
1170 EV_TIMEOUT|EV_PERSIST, process_frame_cb, frame) < 0) {
1171 LOG(client->worker, "Failed to create frame event");
1172 return NULL;
Christopher Faulet010fded2016-11-03 22:49:37 +01001173 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001174
1175 client->incoming_frame = frame;
1176 return frame;
Christopher Faulet010fded2016-11-03 22:49:37 +01001177}
1178
Christopher Fauletf95b1112016-12-21 08:58:16 +01001179static struct spoe_frame *
1180acquire_outgoing_frame(struct client *client)
Christopher Faulet010fded2016-11-03 22:49:37 +01001181{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001182 struct spoe_engine *engine = client->engine;
1183 struct spoe_frame *frame = NULL;
Christopher Faulet010fded2016-11-03 22:49:37 +01001184
Christopher Fauletf95b1112016-12-21 08:58:16 +01001185 if (client->outgoing_frame != NULL)
1186 frame = client->outgoing_frame;
1187 else if (!LIST_ISEMPTY(&client->outgoing_frames)) {
1188 frame = LIST_NEXT(&client->outgoing_frames, typeof(frame), list);
1189 LIST_DEL(&frame->list);
1190 client->outgoing_frame = frame;
1191 }
1192 else if (engine!= NULL && !LIST_ISEMPTY(&engine->outgoing_frames)) {
1193 frame = LIST_NEXT(&engine->outgoing_frames, typeof(frame), list);
1194 LIST_DEL(&frame->list);
1195 client->outgoing_frame = frame;
1196 }
1197 return frame;
1198}
1199
1200static void
1201write_frame(struct client *client, struct spoe_frame *frame)
1202{
1203 uint32_t netint;
1204
1205 LIST_DEL(&frame->list);
1206
1207 frame->buf = (char *)(frame->data);
1208 frame->offset = 0;
1209 netint = htonl(frame->len);
1210 memcpy(frame->buf, &netint, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001211
Christopher Fauletf95b1112016-12-21 08:58:16 +01001212 if (client != NULL) { /* HELLO or DISCONNECT frames */
1213 event_add(&client->write_frame_event, NULL);
Christopher Faulet010fded2016-11-03 22:49:37 +01001214
Christopher Fauletf95b1112016-12-21 08:58:16 +01001215 /* Try to process the frame as soon as possible, and always
1216 * attach it to the client */
1217 if (client->async || client->pipelining) {
1218 if (client->outgoing_frame == NULL)
1219 client->outgoing_frame = frame;
1220 else
1221 LIST_ADD(&client->outgoing_frames, &frame->list);
1222 }
1223 else {
1224 client->outgoing_frame = frame;
1225 event_del(&client->read_frame_event);
Christopher Faulet010fded2016-11-03 22:49:37 +01001226 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001227 }
1228 else { /* for all other frames */
1229 if (frame->client == NULL) { /* async mode ! */
1230 LIST_ADDQ(&frame->engine->outgoing_frames, &frame->list);
1231 list_for_each_entry(client, &frame->engine->clients, by_engine)
1232 event_add(&client->write_frame_event, NULL);
1233 }
1234 else if (frame->client->pipelining) {
1235 LIST_ADDQ(&frame->client->outgoing_frames, &frame->list);
1236 event_add(&frame->client->write_frame_event, NULL);
1237 }
1238 else {
1239 frame->client->outgoing_frame = frame;
1240 event_add(&frame->client->write_frame_event, NULL);
1241 event_del(&frame->client->read_frame_event);
1242 }
1243 }
1244}
Christopher Faulet010fded2016-11-03 22:49:37 +01001245
Christopher Fauletf95b1112016-12-21 08:58:16 +01001246static void
1247process_incoming_frame(struct spoe_frame *frame)
1248{
1249 struct client *client = frame->client;
Christopher Faulet010fded2016-11-03 22:49:37 +01001250
Christopher Fauletf95b1112016-12-21 08:58:16 +01001251 if (event_add(&frame->process_frame_event, &processing_delay) < 0) {
1252 LOG(client->worker, "Failed to process incoming frame");
1253 release_frame(frame);
1254 return;
1255 }
1256
1257 if (client->async) {
1258 frame->client = NULL;
1259 LIST_ADDQ(&frame->engine->processing_frames, &frame->list);
1260 }
1261 else if (client->pipelining)
1262 LIST_ADDQ(&client->processing_frames, &frame->list);
1263 else
1264 event_del(&client->read_frame_event);
1265}
1266
1267static void
1268signal_cb(evutil_socket_t sig, short events, void *user_data)
1269{
1270 struct event_base *base = user_data;
1271 int i;
1272
1273 DEBUG(&null_worker, "Stopping the server");
1274
1275 event_base_loopbreak(base);
1276 DEBUG(&null_worker, "Main event loop stopped");
1277
1278 for (i = 0; i < num_workers; i++) {
1279 event_base_loopbreak(workers[i].base);
1280 DEBUG(&null_worker, "Event loop stopped for worker %02d",
1281 workers[i].id);
1282 }
1283}
1284
1285static void
1286worker_monitor_cb(evutil_socket_t fd, short events, void *arg)
1287{
1288 struct worker *worker = arg;
1289
1290 LOG(worker, "%u clients connected", worker->nbclients);
1291}
1292
1293static void
1294process_frame_cb(evutil_socket_t fd, short events, void *arg)
1295{
1296 struct spoe_frame *frame = arg;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001297 char *p, *end;
1298 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001299
1300 DEBUG(frame->worker,
Christopher Faulet85010352017-02-02 10:14:36 +01001301 "Process frame messages : STREAM-ID=%u - FRAME-ID=%u - length=%u bytes",
1302 frame->stream_id, frame->frame_id, frame->len - frame->offset);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001303
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001304 p = frame->buf + frame->offset;
1305 end = frame->buf + frame->len;
1306
Christopher Fauletf95b1112016-12-21 08:58:16 +01001307 /* Loop on messages */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001308 while (p < end) {
Christopher Fauletf95b1112016-12-21 08:58:16 +01001309 char *str;
1310 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001311 int nbargs;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001312
1313 /* Decode the message name */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001314 spoe_decode_buffer(&p, end, &str, &sz);
1315 if (!str)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001316 goto stop_processing;
1317
1318 DEBUG(frame->worker, "Process SPOE Message '%.*s'", (int)sz, str);
1319
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001320 nbargs = *p++; /* Get the number of arguments */
1321 frame->offset = (p - frame->buf); /* Save index to handle errors and skip args */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001322 if (!memcmp(str, "check-client-ip", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001323 struct sample smp;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001324
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001325 memset(&smp, 0, sizeof(smp));
Christopher Fauletf95b1112016-12-21 08:58:16 +01001326
1327 if (nbargs != 1)
1328 goto skip_message;
1329
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001330 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001331 goto stop_processing;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001332 if (spoe_decode_data(&p, end, &smp) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001333 goto skip_message;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001334
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001335 if (smp.data.type == SMP_T_IPV4)
1336 check_ipv4_reputation(frame, &smp.data.u.ipv4);
1337 if (smp.data.type == SMP_T_IPV6)
1338 check_ipv6_reputation(frame, &smp.data.u.ipv6);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001339 }
1340 else {
1341 skip_message:
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001342 p = frame->buf + frame->offset; /* Restore index */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001343
1344 while (nbargs-- > 0) {
1345 /* Silently ignore argument: its name and its value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001346 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001347 goto stop_processing;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001348 if (spoe_skip_data(&p, end) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001349 goto stop_processing;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001350 }
1351 }
1352 }
1353
1354 stop_processing:
1355 /* Prepare agent ACK frame */
Christopher Faulet85010352017-02-02 10:14:36 +01001356 frame->buf = (char *)(frame->data) + 4;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001357 frame->offset = 0;
Christopher Faulet85010352017-02-02 10:14:36 +01001358 frame->len = 0;
1359 frame->flags = 0;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001360
1361 ret = prepare_agentack(frame);
Christopher Faulet94bb4c62017-09-26 11:49:23 +02001362 p = frame->buf + ret;
1363 end = frame->buf+max_frame_size;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001364
1365 if (frame->ip_score != -1) {
1366 DEBUG(frame->worker, "Add action : set variable ip_scode=%u",
1367 frame->ip_score);
1368
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001369 *p++ = SPOE_ACT_T_SET_VAR; /* Action type */
1370 *p++ = 3; /* Number of args */
1371 *p++ = SPOE_SCOPE_SESS; /* Arg 1: the scope */
1372 spoe_encode_buffer("ip_score", 8, &p, end); /* Arg 2: variable name */
1373 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +02001374 encode_varint(frame->ip_score, &p, end); /* Arg 3: variable value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001375 frame->len = (p - frame->buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001376 }
1377 write_frame(NULL, frame);
1378}
1379
1380static void
1381read_frame_cb(evutil_socket_t fd, short events, void *arg)
1382{
1383 struct client *client = arg;
1384 struct spoe_frame *frame;
1385 uint32_t netint;
1386 int n;
1387
1388 DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__);
1389 if ((frame = acquire_incoming_frame(client)) == NULL)
1390 goto close;
1391
1392 frame->type = SPOA_FRM_T_HAPROXY;
1393 if (frame->buf == (char *)(frame->data)) {
1394 /* Read the frame length: frame->buf points on length part (frame->data) */
1395 n = read(client->fd, frame->buf+frame->offset, 4-frame->offset);
1396 if (n <= 0) {
1397 if (n < 0)
1398 LOG(client->worker, "Failed to read frame length : %m");
Christopher Fauletba7bc162016-11-07 21:07:38 +01001399 goto close;
Christopher Faulet010fded2016-11-03 22:49:37 +01001400 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001401 frame->offset += n;
1402 if (frame->offset != 4)
1403 return;
1404 memcpy(&netint, frame->buf, 4);
1405 frame->buf += 4;
1406 frame->offset = 0;
1407 frame->len = ntohl(netint);
1408 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001409
Christopher Fauletf95b1112016-12-21 08:58:16 +01001410 /* Read the frame: frame->buf points on frame part (frame->data+4)*/
1411 n = read(client->fd, frame->buf + frame->offset,
1412 frame->len - frame->offset);
1413 if (n <= 0) {
1414 if (n < 0) {
1415 LOG(client->worker, "Frame to read frame : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001416 goto close;
1417 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001418 return;
1419 }
1420 frame->offset += n;
1421 if (frame->offset != frame->len)
1422 return;
1423 frame->offset = 0;
1424
1425 DEBUG(client->worker, "<%lu> New Frame of %u bytes received",
1426 client->id, frame->len);
1427
1428 switch (client->state) {
1429 case SPOA_ST_CONNECTING:
1430 if (handle_hahello(frame) < 0) {
1431 LOG(client->worker, "Failed to decode HELLO frame");
1432 goto disconnect;
1433 }
1434 prepare_agenthello(frame);
1435 goto write_frame;
1436
1437 case SPOA_ST_PROCESSING:
Christopher Faulet85010352017-02-02 10:14:36 +01001438 if (frame->buf[0] == SPOE_FRM_T_HAPROXY_DISCON) {
Christopher Fauletf95b1112016-12-21 08:58:16 +01001439 client->state = SPOA_ST_DISCONNECTING;
1440 goto disconnecting;
1441 }
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001442 if (frame->buf[0] == SPOE_FRM_T_UNSET)
1443 n = handle_hafrag(frame);
1444 else
1445 n = handle_hanotify(frame);
1446
Christopher Fauletf95b1112016-12-21 08:58:16 +01001447 if (n < 0) {
Christopher Faulet85010352017-02-02 10:14:36 +01001448 LOG(client->worker, "Failed to decode frame: %s",
1449 spoe_frm_err_reasons[client->status_code]);
1450 goto disconnect;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001451 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001452 else if (n == 0) {
Christopher Faulet85010352017-02-02 10:14:36 +01001453 LOG(client->worker, "Ignore invalid/unknown/aborted frame");
1454 goto ignore_frame;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001455 }
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001456 else if (n == 1)
1457 goto noop;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001458 else
1459 goto process_frame;
1460
1461 case SPOA_ST_DISCONNECTING:
1462 disconnecting:
1463 if (handle_hadiscon(frame) < 0) {
1464 LOG(client->worker, "Failed to decode DISCONNECT frame");
1465 goto disconnect;
1466 }
1467 if (client->status_code != SPOE_FRM_ERR_NONE)
1468 LOG(client->worker, "<%lu> Peer closed connection: %s",
1469 client->id, spoe_frm_err_reasons[client->status_code]);
1470 client->status_code = SPOE_FRM_ERR_NONE;
1471 goto disconnect;
1472 }
1473
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001474 noop:
1475 return;
1476
Christopher Fauletf95b1112016-12-21 08:58:16 +01001477 ignore_frame:
1478 reset_frame(frame);
1479 return;
1480
1481 process_frame:
1482 process_incoming_frame(frame);
1483 client->incoming_frame = NULL;
1484 return;
1485
1486 write_frame:
1487 write_frame(client, frame);
1488 client->incoming_frame = NULL;
1489 return;
1490
1491 disconnect:
1492 client->state = SPOA_ST_DISCONNECTING;
1493 if (prepare_agentdicon(frame) < 0) {
1494 LOG(client->worker, "Failed to encode DISCONNECT frame");
1495 goto close;
1496 }
1497 goto write_frame;
1498
1499 close:
1500 release_client(client);
1501}
1502
1503static void
1504write_frame_cb(evutil_socket_t fd, short events, void *arg)
1505{
1506 struct client *client = arg;
1507 struct spoe_frame *frame;
1508 int n;
1509
1510 DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__);
1511 if ((frame = acquire_outgoing_frame(client)) == NULL) {
1512 event_del(&client->write_frame_event);
1513 return;
1514 }
1515
1516 if (frame->buf == (char *)(frame->data)) {
1517 /* Write the frame length: frame->buf points on length part (frame->data) */
1518 n = write(client->fd, frame->buf+frame->offset, 4-frame->offset);
1519 if (n <= 0) {
1520 if (n < 0)
1521 LOG(client->worker, "Failed to write frame length : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001522 goto close;
1523 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001524 frame->offset += n;
1525 if (frame->offset != 4)
1526 return;
1527 frame->buf += 4;
1528 frame->offset = 0;
1529 }
1530
1531 /* Write the frame: frame->buf points on frame part (frame->data+4)*/
1532 n = write(client->fd, frame->buf + frame->offset,
1533 frame->len - frame->offset);
1534 if (n <= 0) {
1535 if (n < 0) {
1536 LOG(client->worker, "Failed to write frame : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001537 goto close;
1538 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001539 return;
1540 }
1541 frame->offset += n;
1542 if (frame->offset != frame->len)
1543 return;
1544
1545 DEBUG(client->worker, "<%lu> Frame of %u bytes send",
1546 client->id, frame->len);
1547
1548 switch (client->state) {
1549 case SPOA_ST_CONNECTING:
1550 if (frame->hcheck == true) {
1551 DEBUG(client->worker,
1552 "<%lu> Close client after healthcheck",
1553 client->id);
1554 goto close;
1555 }
1556 client->state = SPOA_ST_PROCESSING;
1557 break;
1558
1559 case SPOA_ST_PROCESSING:
1560 break;
1561
1562 case SPOA_ST_DISCONNECTING:
1563 goto close;
1564 }
1565
1566 release_frame(frame);
1567 client->outgoing_frame = NULL;
1568 if (!client->async && !client->pipelining) {
1569 event_del(&client->write_frame_event);
1570 event_add(&client->read_frame_event, NULL);
1571 }
1572 return;
1573
1574 close:
1575 release_client(client);
1576}
1577
1578static void
1579accept_cb(int listener, short event, void *arg)
1580{
1581 struct worker *worker;
1582 struct client *client;
1583 int fd;
1584
1585 worker = &workers[clicount++ % num_workers];
1586
1587 if ((fd = accept(listener, NULL, NULL)) < 0) {
1588 if (errno != EAGAIN && errno != EWOULDBLOCK)
1589 LOG(worker, "Failed to accept client connection : %m");
1590 return;
1591 }
1592
1593 DEBUG(&null_worker,
1594 "<%lu> New Client connection accepted and assigned to worker %02d",
1595 clicount, worker->id);
1596
1597 if (evutil_make_socket_nonblocking(fd) < 0) {
1598 LOG(&null_worker, "Failed to set client socket to non-blocking : %m");
1599 close(fd);
1600 return;
1601 }
1602
1603 if ((client = calloc(1, sizeof(*client))) == NULL) {
1604 LOG(&null_worker, "Failed to allocate memory for client state : %m");
1605 close(fd);
1606 return;
1607 }
1608
1609 client->id = clicount;
1610 client->fd = fd;
1611 client->worker = worker;
1612 client->state = SPOA_ST_CONNECTING;
1613 client->status_code = SPOE_FRM_ERR_NONE;
1614 client->max_frame_size = max_frame_size;
1615 client->engine = NULL;
1616 client->pipelining = false;
1617 client->async = false;
1618 client->incoming_frame = NULL;
1619 client->outgoing_frame = NULL;
1620 LIST_INIT(&client->processing_frames);
1621 LIST_INIT(&client->outgoing_frames);
1622
1623 LIST_ADDQ(&worker->clients, &client->by_worker);
1624
1625 worker->nbclients++;
1626
1627 if (event_assign(&client->read_frame_event, worker->base, fd,
1628 EV_READ|EV_PERSIST, read_frame_cb, client) < 0 ||
1629 event_assign(&client->write_frame_event, worker->base, fd,
1630 EV_WRITE|EV_PERSIST, write_frame_cb, client) < 0) {
1631 LOG(&null_worker, "Failed to create client events");
1632 release_client(client);
1633 return;
1634 }
1635 event_add(&client->read_frame_event, NULL);
1636}
1637
1638static void *
1639worker_function(void *data)
1640{
1641 struct client *client, *cback;
1642 struct spoe_frame *frame, *fback;
1643 struct worker *worker = data;
1644
1645 DEBUG(worker, "Worker ready to process client messages");
1646 event_base_dispatch(worker->base);
Christopher Faulet010fded2016-11-03 22:49:37 +01001647
Christopher Fauletf95b1112016-12-21 08:58:16 +01001648 list_for_each_entry_safe(client, cback, &worker->clients, by_worker) {
1649 release_client(client);
Christopher Faulet010fded2016-11-03 22:49:37 +01001650 }
1651
Christopher Fauletf95b1112016-12-21 08:58:16 +01001652 list_for_each_entry_safe(frame, fback, &worker->frames, list) {
1653 LIST_DEL(&frame->list);
1654 free(frame);
1655 }
1656
1657 event_free(worker->monitor_event);
1658 event_base_free(worker->base);
1659 DEBUG(worker, "Worker is stopped");
1660 pthread_exit(&null_worker);
1661}
1662
1663
1664static int
1665parse_processing_delay(const char *str)
1666{
1667 unsigned long value;
1668
1669 value = 0;
1670 while (1) {
1671 unsigned int j;
1672
1673 j = *str - '0';
1674 if (j > 9)
1675 break;
1676 str++;
1677 value *= 10;
1678 value += j;
1679 }
1680
1681 switch (*str) {
1682 case '\0': /* no unit = millisecond */
1683 value *= 1000;
1684 break;
1685 case 's': /* second */
1686 value *= 1000000;
1687 str++;
1688 break;
1689 case 'm': /* millisecond : "ms" */
1690 if (str[1] != 's')
1691 return -1;
1692 value *= 1000;
1693 str += 2;
1694 break;
1695 case 'u': /* microsecond : "us" */
1696 if (str[1] != 's')
1697 return -1;
1698 str += 2;
1699 break;
1700 default:
1701 return -1;
1702 }
1703 if (*str)
1704 return -1;
1705
1706 processing_delay.tv_sec = (time_t)(value / 1000000);
1707 processing_delay.tv_usec = (suseconds_t)(value % 1000000);
1708 return 0;
Christopher Faulet010fded2016-11-03 22:49:37 +01001709}
1710
Christopher Fauletf95b1112016-12-21 08:58:16 +01001711
Christopher Faulet010fded2016-11-03 22:49:37 +01001712static void
1713usage(char *prog)
1714{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001715 fprintf(stderr,
1716 "Usage : %s [OPTION]...\n"
1717 " -h Print this message\n"
1718 " -d Enable the debug mode\n"
1719 " -m <max-frame-size> Specify the maximum frame size (default : %u)\n"
1720 " -p <port> Specify the port to listen on (default : %d)\n"
1721 " -n <num-workers> Specify the number of workers (default : %d)\n"
1722 " -c <capability> Enable the support of the specified capability\n"
1723 " -t <time> Set a delay to process a message (default: 0)\n"
1724 " The value is specified in milliseconds by default,\n"
1725 " but can be in any other unit if the number is suffixed\n"
1726 " by a unit (us, ms, s)\n"
1727 "\n"
Christopher Faulet85010352017-02-02 10:14:36 +01001728 " Supported capabilities: fragmentation, pipelining, async\n",
Christopher Fauletf95b1112016-12-21 08:58:16 +01001729 prog, MAX_FRAME_SIZE, DEFAULT_PORT, NUM_WORKERS);
Christopher Faulet010fded2016-11-03 22:49:37 +01001730}
1731
1732int
1733main(int argc, char **argv)
1734{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001735 struct event_base *base = NULL;
1736 struct event *signal_event = NULL, *accept_event = NULL;
1737 int opt, i, fd = -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001738
Christopher Fauletf95b1112016-12-21 08:58:16 +01001739 // TODO: add '-t <processing-time>' option
1740 while ((opt = getopt(argc, argv, "hdm:n:p:c:t:")) != -1) {
Christopher Faulet010fded2016-11-03 22:49:37 +01001741 switch (opt) {
1742 case 'h':
1743 usage(argv[0]);
1744 return EXIT_SUCCESS;
1745 case 'd':
1746 debug = true;
1747 break;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001748 case 'm':
1749 max_frame_size = atoi(optarg);
1750 break;
Christopher Faulet010fded2016-11-03 22:49:37 +01001751 case 'n':
Christopher Fauletf95b1112016-12-21 08:58:16 +01001752 num_workers = atoi(optarg);
Christopher Faulet010fded2016-11-03 22:49:37 +01001753 break;
1754 case 'p':
Christopher Fauletf95b1112016-12-21 08:58:16 +01001755 server_port = atoi(optarg);
Christopher Faulet010fded2016-11-03 22:49:37 +01001756 break;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001757 case 'c':
1758 if (!strcmp(optarg, "pipelining"))
1759 pipelining = true;
1760 else if (!strcmp(optarg, "async"))
1761 async = true;
Christopher Faulet85010352017-02-02 10:14:36 +01001762 else if (!strcmp(optarg, "fragmentation"))
1763 fragmentation = true;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001764 else
1765 fprintf(stderr, "WARNING: unsupported capability '%s'\n", optarg);
1766 break;
1767 case 't':
1768 if (!parse_processing_delay(optarg))
1769 break;
1770 fprintf(stderr, "%s: failed to parse time '%s'.\n", argv[0], optarg);
1771 fprintf(stderr, "Try '%s -h' for more information.\n", argv[0]);
1772 return EXIT_FAILURE;
Christopher Faulet010fded2016-11-03 22:49:37 +01001773 default:
1774 usage(argv[0]);
1775 return EXIT_FAILURE;
1776 }
1777 }
1778
Christopher Fauletf95b1112016-12-21 08:58:16 +01001779 if (num_workers <= 0) {
1780 LOG(&null_worker, "%s : Invalid number of workers '%d'\n",
1781 argv[0], num_workers);
Christopher Faulet010fded2016-11-03 22:49:37 +01001782 goto error;
1783 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001784
1785 if (server_port <= 0) {
1786 LOG(&null_worker, "%s : Invalid port '%d'\n",
1787 argv[0], server_port);
Christopher Faulet010fded2016-11-03 22:49:37 +01001788 goto error;
1789 }
1790
Christopher Fauletf95b1112016-12-21 08:58:16 +01001791
1792 if (evthread_use_pthreads() < 0) {
1793 LOG(&null_worker, "No pthreads support for libevent");
Christopher Faulet010fded2016-11-03 22:49:37 +01001794 goto error;
1795 }
1796
Christopher Fauletf95b1112016-12-21 08:58:16 +01001797 if ((base = event_base_new()) == NULL) {
1798 LOG(&null_worker, "Failed to initialize libevent : %m");
1799 goto error;
1800 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001801
Christopher Fauletf95b1112016-12-21 08:58:16 +01001802 signal(SIGPIPE, SIG_IGN);
Christopher Faulet010fded2016-11-03 22:49:37 +01001803
Christopher Fauletf95b1112016-12-21 08:58:16 +01001804 if ((fd = create_server_socket()) < 0) {
1805 LOG(&null_worker, "Failed to create server socket");
1806 goto error;
1807 }
1808 if (evutil_make_socket_nonblocking(fd) < 0) {
1809 LOG(&null_worker, "Failed to set server socket to non-blocking");
Christopher Faulet010fded2016-11-03 22:49:37 +01001810 goto error;
1811 }
1812
Christopher Fauletf95b1112016-12-21 08:58:16 +01001813 if ((workers = calloc(num_workers, sizeof(*workers))) == NULL) {
1814 LOG(&null_worker, "Failed to set allocate memory for workers");
Christopher Faulet010fded2016-11-03 22:49:37 +01001815 goto error;
1816 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001817
1818 for (i = 0; i < num_workers; ++i) {
1819 struct worker *w = &workers[i];
Christopher Faulet010fded2016-11-03 22:49:37 +01001820
Christopher Fauletf95b1112016-12-21 08:58:16 +01001821 w->id = i+1;
1822 w->nbclients = 0;
1823 LIST_INIT(&w->engines);
1824 LIST_INIT(&w->clients);
1825 LIST_INIT(&w->frames);
1826
1827 if ((w->base = event_base_new()) == NULL) {
1828 LOG(&null_worker,
1829 "Failed to initialize libevent for worker %02d : %m",
1830 w->id);
1831 goto error;
1832 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001833
Christopher Fauletf95b1112016-12-21 08:58:16 +01001834 w->monitor_event = event_new(w->base, fd, EV_PERSIST,
1835 worker_monitor_cb, (void *)w);
1836 if (w->monitor_event == NULL ||
1837 event_add(w->monitor_event, (struct timeval[]){{5,0}}) < 0) {
1838 LOG(&null_worker,
1839 "Failed to create monitor event for worker %02d",
1840 w->id);
Christopher Faulet010fded2016-11-03 22:49:37 +01001841 goto error;
1842 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001843
1844 if (pthread_create(&w->thread, NULL, worker_function, (void *)w)) {
1845 LOG(&null_worker,
1846 "Failed to start thread for worker %02d : %m",
1847 w->id);
1848 }
1849 DEBUG(&null_worker, "Worker %02d initialized", w->id);
1850 }
1851
1852 accept_event = event_new(base, fd, EV_READ|EV_PERSIST, accept_cb,
1853 (void *)base);
1854 if (accept_event == NULL || event_add(accept_event, NULL) < 0) {
1855 LOG(&null_worker, "Failed to create accept event : %m");
1856 }
1857
1858 signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
1859 if (signal_event == NULL || event_add(signal_event, NULL) < 0) {
1860 LOG(&null_worker, "Failed to create signal event : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001861 }
1862
Christopher Fauletf95b1112016-12-21 08:58:16 +01001863 DEBUG(&null_worker,
1864 "Server is ready"
Christopher Faulet85010352017-02-02 10:14:36 +01001865 " [fragmentation=%s - pipelining=%s - async=%s - debug=%s - max-frame-size=%u]",
1866 (fragmentation?"true":"false"), (pipelining?"true":"false"), (async?"true":"false"),
Christopher Fauletf95b1112016-12-21 08:58:16 +01001867 (debug?"true":"false"), max_frame_size);
1868 event_base_dispatch(base);
1869
1870 for (i = 0; i < num_workers; i++) {
1871 struct worker *w = &workers[i];
1872
1873 pthread_join(w->thread, NULL);
1874 DEBUG(&null_worker, "Worker %02d terminated", w->id);
Christopher Faulet010fded2016-11-03 22:49:37 +01001875 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001876
1877 free(workers);
1878 event_free(signal_event);
1879 event_free(accept_event);
1880 event_base_free(base);
1881 close(fd);
Christopher Faulet010fded2016-11-03 22:49:37 +01001882 return EXIT_SUCCESS;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001883
1884 error:
1885 if (workers != NULL)
1886 free(workers);
1887 if (signal_event != NULL)
1888 event_free(signal_event);
1889 if (accept_event != NULL)
1890 event_free(accept_event);
1891 if (base != NULL)
1892 event_base_free(base);
1893 if (fd != -1)
1894 close(fd);
Christopher Faulet010fded2016-11-03 22:49:37 +01001895 return EXIT_FAILURE;
1896}