blob: 9725078154bdea161a09f27f297dcfa3555b6896 [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>
Willy Tarreau0a4b0ab2020-06-11 11:22:44 +020025#include <sys/types.h>
Christopher Faulet010fded2016-11-03 22:49:37 +010026#include <netinet/in.h>
Eric Salama8a9c6c22017-11-10 11:02:23 +010027#include <netinet/tcp.h>
Christopher Fauletf95b1112016-12-21 08:58:16 +010028#include <sys/socket.h>
29#include <err.h>
30#include <ctype.h>
31
32#include <pthread.h>
33
34#include <event2/util.h>
35#include <event2/event.h>
36#include <event2/event_struct.h>
37#include <event2/thread.h>
Christopher Faulet010fded2016-11-03 22:49:37 +010038
Eric Salama8a9c6c22017-11-10 11:02:23 +010039#include <mini-clist.h>
40#include <spoe_types.h>
41#include <spop_functions.h>
Christopher Fauletf95b1112016-12-21 08:58:16 +010042
43#define DEFAULT_PORT 12345
44#define CONNECTION_BACKLOG 10
45#define NUM_WORKERS 10
46#define MAX_FRAME_SIZE 16384
Christopher Faulet63816502018-05-31 14:56:42 +020047#define SPOP_VERSION "2.0"
Christopher Faulet010fded2016-11-03 22:49:37 +010048
49#define SLEN(str) (sizeof(str)-1)
50
Christopher Fauletf95b1112016-12-21 08:58:16 +010051#define LOG(worker, fmt, args...) \
52 do { \
53 struct timeval now; \
54 \
55 gettimeofday(&now, NULL); \
56 fprintf(stderr, "%ld.%06ld [%02d] " fmt "\n", \
57 now.tv_sec, now.tv_usec, (worker)->id, ##args); \
Christopher Faulet010fded2016-11-03 22:49:37 +010058 } while (0)
59
Christopher Fauletf95b1112016-12-21 08:58:16 +010060#define DEBUG(x...) \
Christopher Faulet010fded2016-11-03 22:49:37 +010061 do { \
62 if (debug) \
63 LOG(x); \
64 } while (0)
65
Christopher Faulet010fded2016-11-03 22:49:37 +010066
Christopher Fauletf95b1112016-12-21 08:58:16 +010067enum spoa_state {
68 SPOA_ST_CONNECTING = 0,
69 SPOA_ST_PROCESSING,
70 SPOA_ST_DISCONNECTING,
71};
72
73enum spoa_frame_type {
74 SPOA_FRM_T_UNKNOWN = 0,
75 SPOA_FRM_T_HAPROXY,
76 SPOA_FRM_T_AGENT,
77};
Christopher Faulet010fded2016-11-03 22:49:37 +010078
Christopher Fauletf95b1112016-12-21 08:58:16 +010079struct spoe_engine {
80 char *id;
81
82 struct list processing_frames;
83 struct list outgoing_frames;
84
85 struct list clients;
86 struct list list;
87};
88
89struct spoe_frame {
90 enum spoa_frame_type type;
91 char *buf;
92 unsigned int offset;
93 unsigned int len;
94
95 unsigned int stream_id;
96 unsigned int frame_id;
Christopher Faulet85010352017-02-02 10:14:36 +010097 unsigned int flags;
98 bool hcheck; /* true is the CONNECT frame is a healthcheck */
99 bool fragmented; /* true if the frame is fragmented */
100 int ip_score; /* -1 if unset, else between 0 and 100 */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100101
102 struct event process_frame_event;
103 struct worker *worker;
104 struct spoe_engine *engine;
105 struct client *client;
106 struct list list;
107
Christopher Faulet85010352017-02-02 10:14:36 +0100108 char *frag_buf; /* used to accumulate payload of a fragmented frame */
109 unsigned int frag_len;
110
Christopher Fauletf95b1112016-12-21 08:58:16 +0100111 char data[0];
112};
113
114struct client {
115 int fd;
116 unsigned long id;
117 enum spoa_state state;
118
119 struct event read_frame_event;
120 struct event write_frame_event;
121
122 struct spoe_frame *incoming_frame;
123 struct spoe_frame *outgoing_frame;
124
125 struct list processing_frames;
126 struct list outgoing_frames;
127
128 unsigned int max_frame_size;
129 int status_code;
130
131 char *engine_id;
132 struct spoe_engine *engine;
133 bool pipelining;
134 bool async;
Christopher Faulet85010352017-02-02 10:14:36 +0100135 bool fragmentation;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100136
137 struct worker *worker;
138 struct list by_worker;
139 struct list by_engine;
Christopher Faulet010fded2016-11-03 22:49:37 +0100140};
141
142struct worker {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100143 pthread_t thread;
144 int id;
145 struct event_base *base;
146 struct event *monitor_event;
147
148 struct list engines;
149
150 unsigned int nbclients;
151 struct list clients;
152
153 struct list frames;
Christopher Faulet0b89f722018-01-23 14:46:51 +0100154 unsigned int nbframes;
Christopher Faulet010fded2016-11-03 22:49:37 +0100155};
156
Christopher Fauletf95b1112016-12-21 08:58:16 +0100157
Christopher Fauletf95b1112016-12-21 08:58:16 +0100158/* Globals */
159static struct worker *workers = NULL;
160static struct worker null_worker = { .id = 0 };
161static unsigned long clicount = 0;
162static int server_port = DEFAULT_PORT;
163static int num_workers = NUM_WORKERS;
164static unsigned int max_frame_size = MAX_FRAME_SIZE;
165struct timeval processing_delay = {0, 0};
166static bool debug = false;
167static bool pipelining = false;
168static bool async = false;
Christopher Faulet85010352017-02-02 10:14:36 +0100169static bool fragmentation = false;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100170
171
172static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Faulet85010352017-02-02 10:14:36 +0100173 [SPOE_FRM_ERR_NONE] = "normal",
174 [SPOE_FRM_ERR_IO] = "I/O error",
175 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
176 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
177 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
178 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
179 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
180 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
181 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
182 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
183 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
184 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100185 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Faulet85010352017-02-02 10:14:36 +0100186 [SPOE_FRM_ERR_RES] = "resource allocation error",
187 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf95b1112016-12-21 08:58:16 +0100188};
189
190static void signal_cb(evutil_socket_t, short, void *);
191static void accept_cb(evutil_socket_t, short, void *);
192static void worker_monitor_cb(evutil_socket_t, short, void *);
193static void process_frame_cb(evutil_socket_t, short, void *);
194static void read_frame_cb(evutil_socket_t, short, void *);
195static void write_frame_cb(evutil_socket_t, short, void *);
196
197static void use_spoe_engine(struct client *);
198static void unuse_spoe_engine(struct client *);
199static void release_frame(struct spoe_frame *);
200static void release_client(struct client *);
Christopher Faulet010fded2016-11-03 22:49:37 +0100201
202static void
Christopher Fauletf95b1112016-12-21 08:58:16 +0100203check_ipv4_reputation(struct spoe_frame *frame, struct in_addr *ipv4)
Christopher Faulet010fded2016-11-03 22:49:37 +0100204{
205 char str[INET_ADDRSTRLEN];
206
207 if (inet_ntop(AF_INET, ipv4, str, INET_ADDRSTRLEN) == NULL)
208 return;
209
Christopher Fauletf95b1112016-12-21 08:58:16 +0100210 frame->ip_score = random() % 100;
Christopher Faulet010fded2016-11-03 22:49:37 +0100211
Christopher Fauletf95b1112016-12-21 08:58:16 +0100212 DEBUG(frame->worker, "IP score for %.*s is %d",
213 INET_ADDRSTRLEN, str, frame->ip_score);
Christopher Faulet010fded2016-11-03 22:49:37 +0100214}
215
216static void
Christopher Fauletf95b1112016-12-21 08:58:16 +0100217check_ipv6_reputation(struct spoe_frame *frame, struct in6_addr *ipv6)
Christopher Faulet010fded2016-11-03 22:49:37 +0100218{
219 char str[INET6_ADDRSTRLEN];
220
221 if (inet_ntop(AF_INET6, ipv6, str, INET6_ADDRSTRLEN) == NULL)
222 return;
223
Christopher Fauletf95b1112016-12-21 08:58:16 +0100224 frame->ip_score = random() % 100;
Christopher Faulet010fded2016-11-03 22:49:37 +0100225
Christopher Fauletf95b1112016-12-21 08:58:16 +0100226 DEBUG(frame->worker, "IP score for %.*s is %d",
227 INET6_ADDRSTRLEN, str, frame->ip_score);
Christopher Faulet010fded2016-11-03 22:49:37 +0100228}
229
Christopher Faulet010fded2016-11-03 22:49:37 +0100230
231/* Check the protocol version. It returns -1 if an error occurred, the number of
232 * read bytes otherwise. */
233static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100234check_proto_version(struct spoe_frame *frame, char **buf, char *end)
Christopher Faulet010fded2016-11-03 22:49:37 +0100235{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100236 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100237 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100238 int ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100239
240 /* Get the list of all supported versions by HAProxy */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100241 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Faulet010fded2016-11-03 22:49:37 +0100242 return -1;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100243 ret = spoe_decode_buffer(&p, end, &str, &sz);
244 if (ret == -1 || !str)
Christopher Faulet010fded2016-11-03 22:49:37 +0100245 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100246
247 DEBUG(frame->worker, "<%lu> Supported versions : %.*s",
248 frame->client->id, (int)sz, str);
Christopher Faulet010fded2016-11-03 22:49:37 +0100249
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500250 /* TODO: Find the right version in supported ones */
Christopher Faulet010fded2016-11-03 22:49:37 +0100251
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100252 ret = (p - *buf);
253 *buf = p;
254 return ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100255}
256
257/* Check max frame size value. It returns -1 if an error occurred, the number of
258 * read bytes otherwise. */
259static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100260check_max_frame_size(struct spoe_frame *frame, char **buf, char *end)
Christopher Faulet010fded2016-11-03 22:49:37 +0100261{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100262 char *p = *buf;
Christopher Faulet010fded2016-11-03 22:49:37 +0100263 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100264 int type, ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100265
266 /* Get the max-frame-size value of HAProxy */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100267 type = *p++;
Christopher Faulet010fded2016-11-03 22:49:37 +0100268 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
269 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
270 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
Christopher Fauletf95b1112016-12-21 08:58:16 +0100271 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64)
Christopher Faulet010fded2016-11-03 22:49:37 +0100272 return -1;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200273 if (decode_varint(&p, end, &sz) == -1)
Christopher Faulet010fded2016-11-03 22:49:37 +0100274 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100275
276 /* Keep the lower value */
Christopher Fauletf95b1112016-12-21 08:58:16 +0100277 if (sz < frame->client->max_frame_size)
278 frame->client->max_frame_size = sz;
279
280 DEBUG(frame->worker, "<%lu> HAProxy maximum frame size : %u",
281 frame->client->id, (unsigned int)sz);
Christopher Faulet010fded2016-11-03 22:49:37 +0100282
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100283 ret = (p - *buf);
284 *buf = p;
285 return ret;
Christopher Faulet010fded2016-11-03 22:49:37 +0100286}
287
Christopher Fauletba7bc162016-11-07 21:07:38 +0100288/* Check healthcheck value. It returns -1 if an error occurred, the number of
289 * read bytes otherwise. */
290static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100291check_healthcheck(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100292{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100293 char *p = *buf;
294 int type, ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100295
Christopher Fauletf95b1112016-12-21 08:58:16 +0100296 /* Get the "healthcheck" value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100297 type = *p++;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100298 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_BOOL)
299 return -1;
300 frame->hcheck = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE);
301
302 DEBUG(frame->worker, "<%lu> HELLO healthcheck : %s",
303 frame->client->id, (frame->hcheck ? "true" : "false"));
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100304
305 ret = (p - *buf);
306 *buf = p;
307 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100308}
309
310/* Check capabilities value. It returns -1 if an error occurred, the number of
311 * read bytes otherwise. */
312static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100313check_capabilities(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100314{
315 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100316 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100317 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100318 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100319
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100320 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100321 return -1;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100322 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
323 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100324 if (str == NULL) /* this is not an error */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100325 goto end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100326
327 DEBUG(frame->worker, "<%lu> HAProxy capabilities : %.*s",
328 client->id, (int)sz, str);
329
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100330 while (sz) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100331 char *delim;
332
333 /* Skip leading spaces */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100334 for (; isspace(*str) && sz; sz--);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100335
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100336 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
337 str += 10; sz -= 10;
338 if (!sz || isspace(*str) || *str == ',') {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100339 DEBUG(frame->worker,
340 "<%lu> HAProxy supports frame pipelining",
341 client->id);
342 client->pipelining = true;
343 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100344 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100345 else if (sz >= 5 && !strncmp(str, "async", 5)) {
346 str += 5; sz -= 5;
347 if (!sz || isspace(*str) || *str == ',') {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100348 DEBUG(frame->worker,
349 "<%lu> HAProxy supports asynchronous frame",
350 client->id);
351 client->async = true;
352 }
353 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100354 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
355 str += 13; sz -= 13;
356 if (!sz || isspace(*str) || *str == ',') {
Christopher Faulet85010352017-02-02 10:14:36 +0100357 DEBUG(frame->worker,
358 "<%lu> HAProxy supports fragmented frame",
359 client->id);
360 client->fragmentation = true;
361 }
362 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100363
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100364 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100365 break;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100366 delim++;
367 sz -= (delim - str);
368 str = delim;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100369 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100370 end:
371 ret = (p - *buf);
372 *buf = p;
373 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100374}
375
376/* Check engine-id value. It returns -1 if an error occurred, the number of
377 * read bytes otherwise. */
378static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100379check_engine_id(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100380{
381 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100382 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100383 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100384 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100385
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100386 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100387 return -1;
388
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100389 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
390 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100391 if (str == NULL) /* this is not an error */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100392 goto end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100393
394 if (client->engine != NULL)
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100395 goto end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100396
397 DEBUG(frame->worker, "<%lu> HAProxy engine id : %.*s",
398 client->id, (int)sz, str);
399
400 client->engine_id = strndup(str, (int)sz);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100401 end:
402 ret = (p - *buf);
403 *buf = p;
404 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100405}
406
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100407static int
408acc_payload(struct spoe_frame *frame)
409{
410 struct client *client = frame->client;
411 char *buf;
412 size_t len = frame->len - frame->offset;
413 int ret = frame->offset;
414
415 /* No need to accumulation payload */
416 if (frame->fragmented == false)
417 return ret;
418
419 buf = realloc(frame->frag_buf, frame->frag_len + len);
420 if (buf == NULL) {
421 client->status_code = SPOE_FRM_ERR_RES;
422 return -1;
423 }
424 memcpy(buf + frame->frag_len, frame->buf + frame->offset, len);
425 frame->frag_buf = buf;
426 frame->frag_len += len;
427
428 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
429 /* Wait for next parts */
430 frame->buf = (char *)(frame->data);
431 frame->offset = 0;
432 frame->len = 0;
433 frame->flags = 0;
434 return 1;
435 }
436
437 frame->buf = frame->frag_buf;
438 frame->len = frame->frag_len;
439 frame->offset = 0;
440 return ret;
441}
442
Christopher Fauletf95b1112016-12-21 08:58:16 +0100443/* Check disconnect status code. It returns -1 if an error occurred, the number
444 * of read bytes otherwise. */
445static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100446check_discon_status_code(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100447{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100448 char *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100449 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100450 int type, ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100451
452 /* Get the "status-code" value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100453 type = *p++;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100454 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
455 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
456 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
457 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64)
458 return -1;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200459 if (decode_varint(&p, end, &sz) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100460 return -1;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100461
462 frame->client->status_code = (unsigned int)sz;
463
464 DEBUG(frame->worker, "<%lu> Disconnect status code : %u",
465 frame->client->id, frame->client->status_code);
466
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100467 ret = (p - *buf);
468 *buf = p;
469 return ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100470}
471
Christopher Fauletf95b1112016-12-21 08:58:16 +0100472/* Check the disconnect message. It returns -1 if an error occurred, the number
473 * of read bytes otherwise. */
474static int
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100475check_discon_message(struct spoe_frame *frame, char **buf, char *end)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100476{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100477 char *str, *p = *buf;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100478 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100479 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100480
481 /* Get the "message" value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100482 if ((*p++ & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100483 return -1;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100484 ret = spoe_decode_buffer(&p, end, &str, &sz);
485 if (ret == -1 || !str)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100486 return -1;
487
488 DEBUG(frame->worker, "<%lu> Disconnect message : %.*s",
489 frame->client->id, (int)sz, str);
490
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100491 ret = (p - *buf);
492 *buf = p;
493 return ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100494}
Christopher Fauletba7bc162016-11-07 21:07:38 +0100495
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100496
497
Christopher Faulet010fded2016-11-03 22:49:37 +0100498/* Decode a HELLO frame received from HAProxy. It returns -1 if an error
Christopher Fauletf95b1112016-12-21 08:58:16 +0100499 * occurred, otherwise the number of read bytes. HELLO frame cannot be
500 * ignored and having another frame than a HELLO frame is an error. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100501static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100502handle_hahello(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100503{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100504 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100505 char *p, *end;
506
507 p = frame->buf;
508 end = frame->buf + frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100509
Christopher Fauletf95b1112016-12-21 08:58:16 +0100510 /* Check frame type: we really want a HELLO frame */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100511 if (*p++ != SPOE_FRM_T_HAPROXY_HELLO)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100512 goto error;
513
514 DEBUG(frame->worker, "<%lu> Decode HAProxy HELLO frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100515
Christopher Faulet85010352017-02-02 10:14:36 +0100516 /* Retrieve flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100517 memcpy((char *)&(frame->flags), p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200518 frame->flags = ntohl(frame->flags);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100519 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100520
Christopher Faulet85010352017-02-02 10:14:36 +0100521 /* Fragmentation is not supported for HELLO frame */
522 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
523 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
524 goto error;
525 }
526
Christopher Faulet010fded2016-11-03 22:49:37 +0100527 /* stream-id and frame-id must be cleared */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100528 if (*p != 0 || *(p+1) != 0) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100529 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100530 goto error;
531 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100532 p += 2;
Christopher Faulet010fded2016-11-03 22:49:37 +0100533
534 /* Loop on K/V items */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100535 while (p < end) {
Christopher Faulet010fded2016-11-03 22:49:37 +0100536 char *str;
537 uint64_t sz;
538
539 /* Decode the item name */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100540 spoe_decode_buffer(&p, end, &str, &sz);
541 if (!str) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100542 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100543 goto error;
544 }
545
546 /* Check "supported-versions" K/V item */
547 if (!memcmp(str, "supported-versions", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100548 if (check_proto_version(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100549 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100550 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100551 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100552 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100553 /* Check "max-frame-size" K/V item */
Christopher Faulet010fded2016-11-03 22:49:37 +0100554 else if (!memcmp(str, "max-frame-size", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100555 if (check_max_frame_size(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100556 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100557 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100558 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100559 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100560 /* Check "healthcheck" K/V item */
Christopher Fauletba7bc162016-11-07 21:07:38 +0100561 else if (!memcmp(str, "healthcheck", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100562 if (check_healthcheck(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100563 client->status_code = SPOE_FRM_ERR_INVALID;
564 goto error;
565 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100566 }
567 /* Check "capabilities" K/V item */
568 else if (!memcmp(str, "capabilities", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100569 if (check_capabilities(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100570 client->status_code = SPOE_FRM_ERR_INVALID;
571 goto error;
572 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100573 }
574 /* Check "engine-id" K/V item */
575 else if (!memcmp(str, "engine-id", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100576 if (check_engine_id(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100577 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100578 goto error;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100579 }
Christopher Fauletba7bc162016-11-07 21:07:38 +0100580 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100581 else {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100582 DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s",
583 client->id, (int)sz, str);
584
Christopher Faulet010fded2016-11-03 22:49:37 +0100585 /* Silently ignore unknown item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100586 if (spoe_skip_data(&p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100587 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100588 goto error;
589 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100590 }
591 }
592
Christopher Fauletf95b1112016-12-21 08:58:16 +0100593 if (async == false || client->engine_id == NULL)
594 client->async = false;
595 if (pipelining == false)
596 client->pipelining = false;
597
598 if (client->async == true)
599 use_spoe_engine(client);
600
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100601 return (p - frame->buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100602 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100603 return -1;
604}
605
606/* Decode a DISCONNECT frame received from HAProxy. It returns -1 if an error
Christopher Fauletf95b1112016-12-21 08:58:16 +0100607 * occurred, otherwise the number of read bytes. DISCONNECT frame cannot be
608 * ignored and having another frame than a DISCONNECT frame is an error.*/
Christopher Faulet010fded2016-11-03 22:49:37 +0100609static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100610handle_hadiscon(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100611{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100612 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100613 char *p, *end;
614
615 p = frame->buf;
616 end = frame->buf + frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100617
Christopher Fauletf95b1112016-12-21 08:58:16 +0100618 /* Check frame type: we really want a DISCONNECT frame */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100619 if (*p++ != SPOE_FRM_T_HAPROXY_DISCON)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100620 goto error;
621
622 DEBUG(frame->worker, "<%lu> Decode HAProxy DISCONNECT frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100623
Christopher Faulet85010352017-02-02 10:14:36 +0100624 /* Retrieve flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100625 memcpy((char *)&(frame->flags), p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200626 frame->flags = ntohl(frame->flags);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100627 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100628
Christopher Faulet85010352017-02-02 10:14:36 +0100629 /* Fragmentation is not supported for DISCONNECT frame */
630 if (!(frame->flags & SPOE_FRM_FL_FIN)) {
631 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
632 goto error;
633 }
634
Christopher Faulet010fded2016-11-03 22:49:37 +0100635 /* stream-id and frame-id must be cleared */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100636 if (*p != 0 || *(p+1) != 0) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100637 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100638 goto error;
639 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100640 p += 2;
Christopher Faulet010fded2016-11-03 22:49:37 +0100641
Christopher Fauletf95b1112016-12-21 08:58:16 +0100642 client->status_code = SPOE_FRM_ERR_NONE;
643
Christopher Faulet010fded2016-11-03 22:49:37 +0100644 /* Loop on K/V items */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100645 while (p < end) {
Christopher Faulet010fded2016-11-03 22:49:37 +0100646 char *str;
647 uint64_t sz;
648
649 /* Decode item key */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100650 spoe_decode_buffer(&p, end, &str, &sz);
651 if (!str) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100652 client->status_code = SPOE_FRM_ERR_INVALID;
Christopher Faulet010fded2016-11-03 22:49:37 +0100653 goto error;
654 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100655
656 /* Check "status-code" K/V item */
657 if (!memcmp(str, "status-code", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100658 if (check_discon_status_code(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100659 client->status_code = SPOE_FRM_ERR_INVALID;
660 goto error;
661 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100662 }
663 /* Check "message" K/V item */
664 else if (!memcmp(str, "message", sz)) {
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100665 if (check_discon_message(frame, &p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100666 client->status_code = SPOE_FRM_ERR_INVALID;
667 goto error;
668 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100669 }
670 else {
671 DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s",
672 client->id, (int)sz, str);
673
674 /* Silently ignore unknown item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100675 if (spoe_skip_data(&p, end) == -1) {
Christopher Fauletf95b1112016-12-21 08:58:16 +0100676 client->status_code = SPOE_FRM_ERR_INVALID;
677 goto error;
678 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100679 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100680 }
681
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100682 return (p - frame->buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100683 error:
Christopher Faulet010fded2016-11-03 22:49:37 +0100684 return -1;
685}
686
687/* Decode a NOTIFY frame received from HAProxy. It returns -1 if an error
Christopher Faulet85010352017-02-02 10:14:36 +0100688 * occurred, 0 if it must be must be ignored, otherwise the number of read
689 * bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100690static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100691handle_hanotify(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100692{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100693 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100694 char *p, *end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100695 uint64_t stream_id, frame_id;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100696
697 p = frame->buf;
698 end = frame->buf + frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100699
700 /* Check frame type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100701 if (*p++ != SPOE_FRM_T_HAPROXY_NOTIFY)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100702 goto ignore;
703
704 DEBUG(frame->worker, "<%lu> Decode HAProxy NOTIFY frame", client->id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100705
Christopher Faulet85010352017-02-02 10:14:36 +0100706 /* Retrieve flags */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100707 memcpy((char *)&(frame->flags), p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200708 frame->flags = ntohl(frame->flags);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100709 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100710
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100711 /* Fragmentation is not supported */
Christopher Faulet85010352017-02-02 10:14:36 +0100712 if (!(frame->flags & SPOE_FRM_FL_FIN) && fragmentation == false) {
713 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
714 goto error;
715 }
716
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100717 /* Read the stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200718 if (decode_varint(&p, end, &stream_id) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100719 goto ignore;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200720 if (decode_varint(&p, end, &frame_id) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +0100721 goto ignore;
Christopher Faulet010fded2016-11-03 22:49:37 +0100722
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100723 frame->stream_id = (unsigned int)stream_id;
724 frame->frame_id = (unsigned int)frame_id;
Christopher Faulet010fded2016-11-03 22:49:37 +0100725
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100726 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
727 " - %s frame received"
728 " - frag_len=%u - len=%u - offset=%ld",
729 client->id, frame->stream_id, frame->frame_id,
730 (frame->flags & SPOE_FRM_FL_FIN) ? "unfragmented" : "fragmented",
731 frame->frag_len, frame->len, p - frame->buf);
732
733 frame->fragmented = !(frame->flags & SPOE_FRM_FL_FIN);
734 frame->offset = (p - frame->buf);
735 return acc_payload(frame);
736
737 ignore:
738 return 0;
739
740 error:
741 return -1;
742}
743
744/* Decode next part of a fragmented frame received from HAProxy. It returns -1
745 * if an error occurred, 0 if it must be must be ignored, otherwise the number
746 * of read bytes. */
747static int
748handle_hafrag(struct spoe_frame *frame)
749{
750 struct client *client = frame->client;
751 char *p, *end;
752 uint64_t stream_id, frame_id;
753
754 p = frame->buf;
755 end = frame->buf + frame->len;
756
757 /* Check frame type */
758 if (*p++ != SPOE_FRM_T_UNSET)
759 goto ignore;
760
761 DEBUG(frame->worker, "<%lu> Decode Next part of a fragmented frame", client->id);
762
763 /* Fragmentation is not supported */
764 if (fragmentation == false) {
765 client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
766 goto error;
Christopher Faulet85010352017-02-02 10:14:36 +0100767 }
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100768
769 /* Retrieve flags */
770 memcpy((char *)&(frame->flags), p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200771 frame->flags = ntohl(frame->flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100772 p+= 4;
773
774 /* Read the stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200775 if (decode_varint(&p, end, &stream_id) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100776 goto ignore;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200777 if (decode_varint(&p, end, &frame_id) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100778 goto ignore;
Christopher Faulet010fded2016-11-03 22:49:37 +0100779
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100780 if (frame->fragmented == false ||
781 frame->stream_id != (unsigned int)stream_id ||
782 frame->frame_id != (unsigned int)frame_id) {
783 client->status_code = SPOE_FRM_ERR_INTERLACED_FRAMES;
784 goto error;
785 }
786
787 if (frame->flags & SPOE_FRM_FL_ABRT) {
Christopher Faulet85010352017-02-02 10:14:36 +0100788 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100789 " - Abort processing of a fragmented frame"
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100790 " - frag_len=%u - len=%u - offset=%ld",
Christopher Faulet85010352017-02-02 10:14:36 +0100791 client->id, frame->stream_id, frame->frame_id,
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100792 frame->frag_len, frame->len, p - frame->buf);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100793 goto ignore;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100794 }
Christopher Faulet010fded2016-11-03 22:49:37 +0100795
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100796 DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u"
797 " - %s fragment of a fragmented frame received"
798 " - frag_len=%u - len=%u - offset=%ld",
799 client->id, frame->stream_id, frame->frame_id,
800 (frame->flags & SPOE_FRM_FL_FIN) ? "last" : "next",
801 frame->frag_len, frame->len, p - frame->buf);
802
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100803 frame->offset = (p - frame->buf);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100804 return acc_payload(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +0100805
Christopher Fauletf95b1112016-12-21 08:58:16 +0100806 ignore:
Christopher Faulet85010352017-02-02 10:14:36 +0100807 return 0;
808
809 error:
Christopher Fauletf95b1112016-12-21 08:58:16 +0100810 return -1;
811}
Christopher Faulet010fded2016-11-03 22:49:37 +0100812
Christopher Fauletf95b1112016-12-21 08:58:16 +0100813/* Encode a HELLO frame to send it to HAProxy. It returns the number of written
814 * bytes. */
815static int
816prepare_agenthello(struct spoe_frame *frame)
817{
818 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100819 char *p, *end;
Christopher Faulet85010352017-02-02 10:14:36 +0100820 char capabilities[64];
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100821 int n;
Christopher Faulet85010352017-02-02 10:14:36 +0100822 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet010fded2016-11-03 22:49:37 +0100823
Christopher Fauletf95b1112016-12-21 08:58:16 +0100824 DEBUG(frame->worker, "<%lu> Encode Agent HELLO frame", client->id);
825 frame->type = SPOA_FRM_T_AGENT;
Christopher Faulet010fded2016-11-03 22:49:37 +0100826
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100827 p = frame->buf;
828 end = frame->buf+max_frame_size;
829
Christopher Faulet010fded2016-11-03 22:49:37 +0100830 /* Frame Type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100831 *p++ = SPOE_FRM_T_AGENT_HELLO;
Christopher Faulet010fded2016-11-03 22:49:37 +0100832
Christopher Faulet85010352017-02-02 10:14:36 +0100833 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200834 flags = htonl(flags);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100835 memcpy(p, (char *)&flags, 4);
836 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100837
838 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100839 *p++ = 0;
840 *p++ = 0;
Christopher Faulet010fded2016-11-03 22:49:37 +0100841
842 /* "version" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100843 spoe_encode_buffer("version", 7, &p, end);
844 *p++ = SPOE_DATA_T_STR;
845 spoe_encode_buffer(SPOP_VERSION, SLEN(SPOP_VERSION), &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100846 DEBUG(frame->worker, "<%lu> Agent version : %s",
847 client->id, SPOP_VERSION);
848
Christopher Faulet010fded2016-11-03 22:49:37 +0100849
850 /* "max-frame-size" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100851 spoe_encode_buffer("max-frame-size", 14, &p ,end);
852 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200853 encode_varint(client->max_frame_size, &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100854 DEBUG(frame->worker, "<%lu> Agent maximum frame size : %u",
855 client->id, client->max_frame_size);
Christopher Faulet010fded2016-11-03 22:49:37 +0100856
857 /* "capabilities" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100858 spoe_encode_buffer("capabilities", 12, &p, end);
859 *p++ = SPOE_DATA_T_STR;
Christopher Faulet85010352017-02-02 10:14:36 +0100860
861 memset(capabilities, 0, sizeof(capabilities));
862 n = 0;
863
864 /* 1. Fragmentation capability ? */
865 if (fragmentation == true) {
866 memcpy(capabilities, "fragmentation", 13);
867 n += 13;
868 }
869 /* 2. Pipelining capability ? */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100870 if (client->pipelining == true) {
871 if (n) capabilities[n++] = ',';
872 memcpy(capabilities + n, "pipelining", 10);
Christopher Faulet85010352017-02-02 10:14:36 +0100873 n += 10;
874 }
875 /* 3. Async capability ? */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100876 if (client->async == true) {
877 if (n) capabilities[n++] = ',';
878 memcpy(capabilities + n, "async", 5);
Christopher Faulet85010352017-02-02 10:14:36 +0100879 n += 5;
880 }
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100881 spoe_encode_buffer(capabilities, n, &p, end);
Christopher Faulet010fded2016-11-03 22:49:37 +0100882
Christopher Faulet85010352017-02-02 10:14:36 +0100883 DEBUG(frame->worker, "<%lu> Agent capabilities : %.*s",
884 client->id, n, capabilities);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100885
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100886 frame->len = (p - frame->buf);
887 return frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100888}
889
Christopher Fauletf95b1112016-12-21 08:58:16 +0100890/* Encode a DISCONNECT frame to send it to HAProxy. It returns the number of
891 * written bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100892static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100893prepare_agentdicon(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100894{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100895 struct client *client = frame->client;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100896 char *p, *end;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100897 const char *reason;
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100898 int rlen;
Christopher Faulet85010352017-02-02 10:14:36 +0100899 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100900
901 DEBUG(frame->worker, "<%lu> Encode Agent DISCONNECT frame", client->id);
902 frame->type = SPOA_FRM_T_AGENT;
903
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100904 p = frame->buf;
905 end = frame->buf+max_frame_size;
906
Christopher Fauletf95b1112016-12-21 08:58:16 +0100907 if (client->status_code >= SPOE_FRM_ERRS)
908 client->status_code = SPOE_FRM_ERR_UNKNOWN;
909 reason = spoe_frm_err_reasons[client->status_code];
910 rlen = strlen(reason);
Christopher Faulet010fded2016-11-03 22:49:37 +0100911
912 /* Frame type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100913 *p++ = SPOE_FRM_T_AGENT_DISCON;
Christopher Faulet010fded2016-11-03 22:49:37 +0100914
Christopher Faulet85010352017-02-02 10:14:36 +0100915 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200916 flags = htonl(flags);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100917 memcpy(p, (char *)&flags, 4);
918 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100919
Christopher Fauletf95b1112016-12-21 08:58:16 +0100920 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100921 *p++ = 0;
922 *p++ = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +0100923
924 /* There are 2 mandatory items: "status-code" and "message" */
925
926 /* "status-code" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100927 spoe_encode_buffer("status-code", 11, &p, end);
928 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200929 encode_varint(client->status_code, &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100930 DEBUG(frame->worker, "<%lu> Disconnect status code : %u",
931 client->id, client->status_code);
Christopher Faulet010fded2016-11-03 22:49:37 +0100932
Christopher Fauletf95b1112016-12-21 08:58:16 +0100933 /* "message" K/V item */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100934 spoe_encode_buffer("message", 7, &p, end);
935 *p++ = SPOE_DATA_T_STR;
936 spoe_encode_buffer(reason, rlen, &p, end);
Christopher Fauletf95b1112016-12-21 08:58:16 +0100937 DEBUG(frame->worker, "<%lu> Disconnect message : %s",
938 client->id, reason);
Christopher Faulet010fded2016-11-03 22:49:37 +0100939
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100940 frame->len = (p - frame->buf);
941 return frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100942}
943
Christopher Fauletf95b1112016-12-21 08:58:16 +0100944/* Encode a ACK frame to send it to HAProxy. It returns the number of written
945 * bytes. */
Christopher Faulet010fded2016-11-03 22:49:37 +0100946static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100947prepare_agentack(struct spoe_frame *frame)
Christopher Faulet010fded2016-11-03 22:49:37 +0100948{
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100949 char *p, *end;
Christopher Faulet85010352017-02-02 10:14:36 +0100950 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet010fded2016-11-03 22:49:37 +0100951
Christopher Fauletf95b1112016-12-21 08:58:16 +0100952 /* Be careful here, in async mode, frame->client can be NULL */
953
954 DEBUG(frame->worker, "Encode Agent ACK frame");
955 frame->type = SPOA_FRM_T_AGENT;
Christopher Faulet010fded2016-11-03 22:49:37 +0100956
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100957 p = frame->buf;
958 end = frame->buf+max_frame_size;
959
Christopher Faulet010fded2016-11-03 22:49:37 +0100960 /* Frame type */
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100961 *p++ = SPOE_FRM_T_AGENT_ACK;
Christopher Faulet010fded2016-11-03 22:49:37 +0100962
Christopher Faulet85010352017-02-02 10:14:36 +0100963 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200964 flags = htonl(flags);
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100965 memcpy(p, (char *)&flags, 4);
966 p += 4;
Christopher Faulet010fded2016-11-03 22:49:37 +0100967
Christopher Fauletf95b1112016-12-21 08:58:16 +0100968 /* Set stream-id and frame-id for ACK frames */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200969 encode_varint(frame->stream_id, &p, end);
970 encode_varint(frame->frame_id, &p, end);
Christopher Faulet010fded2016-11-03 22:49:37 +0100971
Christopher Fauletf95b1112016-12-21 08:58:16 +0100972 DEBUG(frame->worker, "STREAM-ID=%u - FRAME-ID=%u",
973 frame->stream_id, frame->frame_id);
Christopher Faulet010fded2016-11-03 22:49:37 +0100974
Christopher Faulet4ff3e572017-02-24 14:31:11 +0100975 frame->len = (p - frame->buf);
976 return frame->len;
Christopher Faulet010fded2016-11-03 22:49:37 +0100977}
978
979static int
Christopher Fauletf95b1112016-12-21 08:58:16 +0100980create_server_socket(void)
Christopher Faulet010fded2016-11-03 22:49:37 +0100981{
Christopher Fauletf95b1112016-12-21 08:58:16 +0100982 struct sockaddr_in listen_addr;
983 int fd, yes = 1;
984
985 fd = socket(AF_INET, SOCK_STREAM, 0);
986 if (fd < 0) {
987 LOG(&null_worker, "Failed to create service socket : %m");
988 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +0100989 }
Christopher Fauletf95b1112016-12-21 08:58:16 +0100990
991 memset(&listen_addr, 0, sizeof(listen_addr));
992 listen_addr.sin_family = AF_INET;
993 listen_addr.sin_addr.s_addr = INADDR_ANY;
994 listen_addr.sin_port = htons(server_port);
995
996 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0 ||
997 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) < 0) {
998 LOG(&null_worker, "Failed to set option on server socket : %m");
999 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001000 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001001
1002 if (bind(fd, (struct sockaddr *) &listen_addr, sizeof(listen_addr)) < 0) {
1003 LOG(&null_worker, "Failed to bind server socket : %m");
1004 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001005 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001006
1007 if (listen(fd, CONNECTION_BACKLOG) < 0) {
1008 LOG(&null_worker, "Failed to listen on server socket : %m");
1009 return -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001010 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001011
1012 return fd;
Christopher Faulet010fded2016-11-03 22:49:37 +01001013}
1014
Christopher Fauletf95b1112016-12-21 08:58:16 +01001015static void
1016release_frame(struct spoe_frame *frame)
1017{
1018 struct worker *worker;
1019
1020 if (frame == NULL)
1021 return;
1022
1023 if (event_pending(&frame->process_frame_event, EV_TIMEOUT, NULL))
1024 event_del(&frame->process_frame_event);
1025
1026 worker = frame->worker;
1027 LIST_DEL(&frame->list);
Christopher Faulet85010352017-02-02 10:14:36 +01001028 if (frame->frag_buf)
1029 free(frame->frag_buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001030 memset(frame, 0, sizeof(*frame)+max_frame_size+4);
1031 LIST_ADDQ(&worker->frames, &frame->list);
1032}
1033
1034static void
1035release_client(struct client *c)
Christopher Faulet010fded2016-11-03 22:49:37 +01001036{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001037 struct spoe_frame *frame, *back;
1038
1039 if (c == NULL)
1040 return;
1041
1042 DEBUG(c->worker, "<%lu> Release client", c->id);
1043
1044 LIST_DEL(&c->by_worker);
1045 c->worker->nbclients--;
1046
1047 unuse_spoe_engine(c);
1048 free(c->engine_id);
1049
1050 if (event_pending(&c->read_frame_event, EV_READ, NULL))
1051 event_del(&c->read_frame_event);
1052 if (event_pending(&c->write_frame_event, EV_WRITE, NULL))
1053 event_del(&c->write_frame_event);
1054
1055 release_frame(c->incoming_frame);
1056 release_frame(c->outgoing_frame);
1057 list_for_each_entry_safe(frame, back, &c->processing_frames, list) {
1058 release_frame(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +01001059 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001060 list_for_each_entry_safe(frame, back, &c->outgoing_frames, list) {
1061 release_frame(frame);
Christopher Faulet010fded2016-11-03 22:49:37 +01001062 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001063
1064 if (c->fd >= 0)
1065 close(c->fd);
1066
1067 free(c);
1068}
1069
1070static void
1071reset_frame(struct spoe_frame *frame)
1072{
1073 if (frame == NULL)
1074 return;
1075
Christopher Faulet85010352017-02-02 10:14:36 +01001076 if (frame->frag_buf)
1077 free(frame->frag_buf);
1078
1079 frame->type = SPOA_FRM_T_UNKNOWN;
1080 frame->buf = (char *)(frame->data);
1081 frame->offset = 0;
1082 frame->len = 0;
1083 frame->stream_id = 0;
1084 frame->frame_id = 0;
1085 frame->flags = 0;
1086 frame->hcheck = false;
1087 frame->fragmented = false;
1088 frame->ip_score = -1;
1089 frame->frag_buf = NULL;
1090 frame->frag_len = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001091 LIST_INIT(&frame->list);
1092}
1093
1094static void
1095use_spoe_engine(struct client *client)
1096{
1097 struct spoe_engine *eng;
1098
1099 if (client->engine_id == NULL)
1100 return;
1101
1102 list_for_each_entry(eng, &client->worker->engines, list) {
1103 if (!strcmp(eng->id, client->engine_id))
1104 goto end;
1105 }
1106
1107 if ((eng = malloc(sizeof(*eng))) == NULL) {
1108 client->async = false;
1109 return;
1110 }
1111
1112 eng->id = strdup(client->engine_id);
1113 LIST_INIT(&eng->clients);
1114 LIST_INIT(&eng->processing_frames);
1115 LIST_INIT(&eng->outgoing_frames);
1116 LIST_ADDQ(&client->worker->engines, &eng->list);
1117 LOG(client->worker, "Add new SPOE engine '%s'", eng->id);
1118
1119 end:
1120 client->engine = eng;
1121 LIST_ADDQ(&eng->clients, &client->by_engine);
1122}
1123
1124static void
1125unuse_spoe_engine(struct client *client)
1126{
1127 struct spoe_engine *eng;
1128 struct spoe_frame *frame, *back;
1129
1130 if (client == NULL || client->engine == NULL)
1131 return;
1132
1133 eng = client->engine;
1134 client->engine = NULL;
1135 LIST_DEL(&client->by_engine);
1136 if (!LIST_ISEMPTY(&eng->clients))
1137 return;
1138
1139 LOG(client->worker, "Remove SPOE engine '%s'", eng->id);
1140 LIST_DEL(&eng->list);
1141
1142 list_for_each_entry_safe(frame, back, &eng->processing_frames, list) {
1143 release_frame(frame);
1144 }
1145 list_for_each_entry_safe(frame, back, &eng->outgoing_frames, list) {
1146 release_frame(frame);
1147 }
1148 free(eng->id);
1149 free(eng);
1150}
1151
1152
1153static struct spoe_frame *
1154acquire_incoming_frame(struct client *client)
1155{
1156 struct spoe_frame *frame;
1157
1158 frame = client->incoming_frame;
1159 if (frame != NULL)
1160 return frame;
1161
1162 if (LIST_ISEMPTY(&client->worker->frames)) {
1163 if ((frame = calloc(1, sizeof(*frame)+max_frame_size+4)) == NULL) {
1164 LOG(client->worker, "Failed to allocate new frame : %m");
1165 return NULL;
1166 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001167 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001168 else {
1169 frame = LIST_NEXT(&client->worker->frames, typeof(frame), list);
1170 LIST_DEL(&frame->list);
Christopher Faulet010fded2016-11-03 22:49:37 +01001171 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001172
1173 reset_frame(frame);
1174 frame->worker = client->worker;
1175 frame->engine = client->engine;
1176 frame->client = client;
1177
1178 if (event_assign(&frame->process_frame_event, client->worker->base, -1,
1179 EV_TIMEOUT|EV_PERSIST, process_frame_cb, frame) < 0) {
1180 LOG(client->worker, "Failed to create frame event");
1181 return NULL;
Christopher Faulet010fded2016-11-03 22:49:37 +01001182 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001183
1184 client->incoming_frame = frame;
1185 return frame;
Christopher Faulet010fded2016-11-03 22:49:37 +01001186}
1187
Christopher Fauletf95b1112016-12-21 08:58:16 +01001188static struct spoe_frame *
1189acquire_outgoing_frame(struct client *client)
Christopher Faulet010fded2016-11-03 22:49:37 +01001190{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001191 struct spoe_engine *engine = client->engine;
1192 struct spoe_frame *frame = NULL;
Christopher Faulet010fded2016-11-03 22:49:37 +01001193
Christopher Fauletf95b1112016-12-21 08:58:16 +01001194 if (client->outgoing_frame != NULL)
1195 frame = client->outgoing_frame;
1196 else if (!LIST_ISEMPTY(&client->outgoing_frames)) {
1197 frame = LIST_NEXT(&client->outgoing_frames, typeof(frame), list);
1198 LIST_DEL(&frame->list);
1199 client->outgoing_frame = frame;
1200 }
1201 else if (engine!= NULL && !LIST_ISEMPTY(&engine->outgoing_frames)) {
1202 frame = LIST_NEXT(&engine->outgoing_frames, typeof(frame), list);
1203 LIST_DEL(&frame->list);
1204 client->outgoing_frame = frame;
1205 }
1206 return frame;
1207}
1208
1209static void
1210write_frame(struct client *client, struct spoe_frame *frame)
1211{
1212 uint32_t netint;
1213
1214 LIST_DEL(&frame->list);
1215
1216 frame->buf = (char *)(frame->data);
1217 frame->offset = 0;
1218 netint = htonl(frame->len);
1219 memcpy(frame->buf, &netint, 4);
Christopher Faulet010fded2016-11-03 22:49:37 +01001220
Christopher Fauletf95b1112016-12-21 08:58:16 +01001221 if (client != NULL) { /* HELLO or DISCONNECT frames */
1222 event_add(&client->write_frame_event, NULL);
Christopher Faulet010fded2016-11-03 22:49:37 +01001223
Christopher Fauletf95b1112016-12-21 08:58:16 +01001224 /* Try to process the frame as soon as possible, and always
1225 * attach it to the client */
1226 if (client->async || client->pipelining) {
1227 if (client->outgoing_frame == NULL)
1228 client->outgoing_frame = frame;
1229 else
1230 LIST_ADD(&client->outgoing_frames, &frame->list);
1231 }
1232 else {
1233 client->outgoing_frame = frame;
1234 event_del(&client->read_frame_event);
Christopher Faulet010fded2016-11-03 22:49:37 +01001235 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001236 }
1237 else { /* for all other frames */
1238 if (frame->client == NULL) { /* async mode ! */
1239 LIST_ADDQ(&frame->engine->outgoing_frames, &frame->list);
1240 list_for_each_entry(client, &frame->engine->clients, by_engine)
1241 event_add(&client->write_frame_event, NULL);
1242 }
1243 else if (frame->client->pipelining) {
1244 LIST_ADDQ(&frame->client->outgoing_frames, &frame->list);
1245 event_add(&frame->client->write_frame_event, NULL);
1246 }
1247 else {
1248 frame->client->outgoing_frame = frame;
1249 event_add(&frame->client->write_frame_event, NULL);
1250 event_del(&frame->client->read_frame_event);
1251 }
1252 }
1253}
Christopher Faulet010fded2016-11-03 22:49:37 +01001254
Christopher Fauletf95b1112016-12-21 08:58:16 +01001255static void
1256process_incoming_frame(struct spoe_frame *frame)
1257{
1258 struct client *client = frame->client;
Christopher Faulet010fded2016-11-03 22:49:37 +01001259
Christopher Fauletf95b1112016-12-21 08:58:16 +01001260 if (event_add(&frame->process_frame_event, &processing_delay) < 0) {
1261 LOG(client->worker, "Failed to process incoming frame");
1262 release_frame(frame);
1263 return;
1264 }
1265
1266 if (client->async) {
1267 frame->client = NULL;
1268 LIST_ADDQ(&frame->engine->processing_frames, &frame->list);
1269 }
1270 else if (client->pipelining)
1271 LIST_ADDQ(&client->processing_frames, &frame->list);
1272 else
1273 event_del(&client->read_frame_event);
1274}
1275
1276static void
1277signal_cb(evutil_socket_t sig, short events, void *user_data)
1278{
1279 struct event_base *base = user_data;
1280 int i;
1281
1282 DEBUG(&null_worker, "Stopping the server");
1283
1284 event_base_loopbreak(base);
1285 DEBUG(&null_worker, "Main event loop stopped");
1286
1287 for (i = 0; i < num_workers; i++) {
1288 event_base_loopbreak(workers[i].base);
1289 DEBUG(&null_worker, "Event loop stopped for worker %02d",
1290 workers[i].id);
1291 }
1292}
1293
1294static void
1295worker_monitor_cb(evutil_socket_t fd, short events, void *arg)
1296{
1297 struct worker *worker = arg;
1298
Christopher Faulet0b89f722018-01-23 14:46:51 +01001299 LOG(worker, "%u clients connected (%u frames)", worker->nbclients, worker->nbframes);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001300}
1301
1302static void
1303process_frame_cb(evutil_socket_t fd, short events, void *arg)
1304{
1305 struct spoe_frame *frame = arg;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001306 char *p, *end;
1307 int ret;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001308
1309 DEBUG(frame->worker,
Christopher Faulet85010352017-02-02 10:14:36 +01001310 "Process frame messages : STREAM-ID=%u - FRAME-ID=%u - length=%u bytes",
1311 frame->stream_id, frame->frame_id, frame->len - frame->offset);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001312
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001313 p = frame->buf + frame->offset;
1314 end = frame->buf + frame->len;
1315
Christopher Fauletf95b1112016-12-21 08:58:16 +01001316 /* Loop on messages */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001317 while (p < end) {
Christopher Fauletf95b1112016-12-21 08:58:16 +01001318 char *str;
1319 uint64_t sz;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001320 int nbargs;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001321
1322 /* Decode the message name */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001323 spoe_decode_buffer(&p, end, &str, &sz);
1324 if (!str)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001325 goto stop_processing;
1326
1327 DEBUG(frame->worker, "Process SPOE Message '%.*s'", (int)sz, str);
1328
Thierry FOURNIER29a05c12018-02-25 21:33:38 +01001329 nbargs = (unsigned char)*p++; /* Get the number of arguments */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001330 frame->offset = (p - frame->buf); /* Save index to handle errors and skip args */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001331 if (!memcmp(str, "check-client-ip", sz)) {
Willy Tarreau75f42462017-11-14 15:01:22 +01001332 union spoe_data data;
1333 enum spoe_data_type type;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001334
1335 if (nbargs != 1)
1336 goto skip_message;
1337
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001338 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001339 goto stop_processing;
Willy Tarreau75f42462017-11-14 15:01:22 +01001340 if (spoe_decode_data(&p, end, &data, &type) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001341 goto skip_message;
Christopher Faulet0b89f722018-01-23 14:46:51 +01001342 frame->worker->nbframes++;
Willy Tarreau75f42462017-11-14 15:01:22 +01001343 if (type == SPOE_DATA_T_IPV4)
1344 check_ipv4_reputation(frame, &data.ipv4);
1345 if (type == SPOE_DATA_T_IPV6)
1346 check_ipv6_reputation(frame, &data.ipv6);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001347 }
1348 else {
1349 skip_message:
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001350 p = frame->buf + frame->offset; /* Restore index */
Christopher Fauletf95b1112016-12-21 08:58:16 +01001351
1352 while (nbargs-- > 0) {
1353 /* Silently ignore argument: its name and its value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001354 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001355 goto stop_processing;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001356 if (spoe_skip_data(&p, end) == -1)
Christopher Fauletf95b1112016-12-21 08:58:16 +01001357 goto stop_processing;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001358 }
1359 }
1360 }
1361
1362 stop_processing:
1363 /* Prepare agent ACK frame */
Christopher Faulet85010352017-02-02 10:14:36 +01001364 frame->buf = (char *)(frame->data) + 4;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001365 frame->offset = 0;
Christopher Faulet85010352017-02-02 10:14:36 +01001366 frame->len = 0;
1367 frame->flags = 0;
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001368
1369 ret = prepare_agentack(frame);
Christopher Faulet94bb4c62017-09-26 11:49:23 +02001370 p = frame->buf + ret;
1371 end = frame->buf+max_frame_size;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001372
1373 if (frame->ip_score != -1) {
Miroslav Zagorac86e106e2020-02-19 12:12:06 +01001374 DEBUG(frame->worker, "Add action : set variable ip_score=%u",
Christopher Fauletf95b1112016-12-21 08:58:16 +01001375 frame->ip_score);
1376
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001377 *p++ = SPOE_ACT_T_SET_VAR; /* Action type */
1378 *p++ = 3; /* Number of args */
1379 *p++ = SPOE_SCOPE_SESS; /* Arg 1: the scope */
1380 spoe_encode_buffer("ip_score", 8, &p, end); /* Arg 2: variable name */
1381 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +02001382 encode_varint(frame->ip_score, &p, end); /* Arg 3: variable value */
Christopher Faulet4ff3e572017-02-24 14:31:11 +01001383 frame->len = (p - frame->buf);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001384 }
1385 write_frame(NULL, frame);
1386}
1387
1388static void
1389read_frame_cb(evutil_socket_t fd, short events, void *arg)
1390{
1391 struct client *client = arg;
1392 struct spoe_frame *frame;
1393 uint32_t netint;
1394 int n;
1395
1396 DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__);
1397 if ((frame = acquire_incoming_frame(client)) == NULL)
1398 goto close;
1399
1400 frame->type = SPOA_FRM_T_HAPROXY;
1401 if (frame->buf == (char *)(frame->data)) {
1402 /* Read the frame length: frame->buf points on length part (frame->data) */
1403 n = read(client->fd, frame->buf+frame->offset, 4-frame->offset);
1404 if (n <= 0) {
1405 if (n < 0)
1406 LOG(client->worker, "Failed to read frame length : %m");
Christopher Fauletba7bc162016-11-07 21:07:38 +01001407 goto close;
Christopher Faulet010fded2016-11-03 22:49:37 +01001408 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001409 frame->offset += n;
1410 if (frame->offset != 4)
1411 return;
1412 memcpy(&netint, frame->buf, 4);
1413 frame->buf += 4;
1414 frame->offset = 0;
1415 frame->len = ntohl(netint);
1416 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001417
Christopher Fauletf95b1112016-12-21 08:58:16 +01001418 /* Read the frame: frame->buf points on frame part (frame->data+4)*/
1419 n = read(client->fd, frame->buf + frame->offset,
1420 frame->len - frame->offset);
1421 if (n <= 0) {
1422 if (n < 0) {
Miroslav Zagorac86e106e2020-02-19 12:12:06 +01001423 LOG(client->worker, "Failed to read frame : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001424 goto close;
1425 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001426 return;
1427 }
1428 frame->offset += n;
1429 if (frame->offset != frame->len)
1430 return;
1431 frame->offset = 0;
1432
1433 DEBUG(client->worker, "<%lu> New Frame of %u bytes received",
1434 client->id, frame->len);
1435
1436 switch (client->state) {
1437 case SPOA_ST_CONNECTING:
1438 if (handle_hahello(frame) < 0) {
1439 LOG(client->worker, "Failed to decode HELLO frame");
1440 goto disconnect;
1441 }
1442 prepare_agenthello(frame);
1443 goto write_frame;
1444
1445 case SPOA_ST_PROCESSING:
Christopher Faulet85010352017-02-02 10:14:36 +01001446 if (frame->buf[0] == SPOE_FRM_T_HAPROXY_DISCON) {
Christopher Fauletf95b1112016-12-21 08:58:16 +01001447 client->state = SPOA_ST_DISCONNECTING;
1448 goto disconnecting;
1449 }
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001450 if (frame->buf[0] == SPOE_FRM_T_UNSET)
1451 n = handle_hafrag(frame);
1452 else
1453 n = handle_hanotify(frame);
1454
Christopher Fauletf95b1112016-12-21 08:58:16 +01001455 if (n < 0) {
Christopher Faulet85010352017-02-02 10:14:36 +01001456 LOG(client->worker, "Failed to decode frame: %s",
1457 spoe_frm_err_reasons[client->status_code]);
1458 goto disconnect;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001459 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001460 else if (n == 0) {
Christopher Faulet85010352017-02-02 10:14:36 +01001461 LOG(client->worker, "Ignore invalid/unknown/aborted frame");
1462 goto ignore_frame;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001463 }
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001464 else if (n == 1)
1465 goto noop;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001466 else
1467 goto process_frame;
1468
1469 case SPOA_ST_DISCONNECTING:
1470 disconnecting:
1471 if (handle_hadiscon(frame) < 0) {
1472 LOG(client->worker, "Failed to decode DISCONNECT frame");
1473 goto disconnect;
1474 }
1475 if (client->status_code != SPOE_FRM_ERR_NONE)
1476 LOG(client->worker, "<%lu> Peer closed connection: %s",
1477 client->id, spoe_frm_err_reasons[client->status_code]);
Christopher Fauletf95b1112016-12-21 08:58:16 +01001478 goto disconnect;
1479 }
1480
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001481 noop:
1482 return;
1483
Christopher Fauletf95b1112016-12-21 08:58:16 +01001484 ignore_frame:
1485 reset_frame(frame);
1486 return;
1487
1488 process_frame:
1489 process_incoming_frame(frame);
1490 client->incoming_frame = NULL;
1491 return;
1492
1493 write_frame:
1494 write_frame(client, frame);
1495 client->incoming_frame = NULL;
1496 return;
1497
1498 disconnect:
1499 client->state = SPOA_ST_DISCONNECTING;
1500 if (prepare_agentdicon(frame) < 0) {
1501 LOG(client->worker, "Failed to encode DISCONNECT frame");
1502 goto close;
1503 }
1504 goto write_frame;
1505
1506 close:
1507 release_client(client);
1508}
1509
1510static void
1511write_frame_cb(evutil_socket_t fd, short events, void *arg)
1512{
1513 struct client *client = arg;
1514 struct spoe_frame *frame;
1515 int n;
1516
1517 DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__);
1518 if ((frame = acquire_outgoing_frame(client)) == NULL) {
1519 event_del(&client->write_frame_event);
1520 return;
1521 }
1522
1523 if (frame->buf == (char *)(frame->data)) {
1524 /* Write the frame length: frame->buf points on length part (frame->data) */
1525 n = write(client->fd, frame->buf+frame->offset, 4-frame->offset);
1526 if (n <= 0) {
1527 if (n < 0)
1528 LOG(client->worker, "Failed to write frame length : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001529 goto close;
1530 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001531 frame->offset += n;
1532 if (frame->offset != 4)
1533 return;
1534 frame->buf += 4;
1535 frame->offset = 0;
1536 }
1537
1538 /* Write the frame: frame->buf points on frame part (frame->data+4)*/
1539 n = write(client->fd, frame->buf + frame->offset,
1540 frame->len - frame->offset);
1541 if (n <= 0) {
1542 if (n < 0) {
1543 LOG(client->worker, "Failed to write frame : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001544 goto close;
1545 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001546 return;
1547 }
1548 frame->offset += n;
1549 if (frame->offset != frame->len)
1550 return;
1551
Miroslav Zagorac86e106e2020-02-19 12:12:06 +01001552 DEBUG(client->worker, "<%lu> Frame of %u bytes sent",
Christopher Fauletf95b1112016-12-21 08:58:16 +01001553 client->id, frame->len);
1554
1555 switch (client->state) {
1556 case SPOA_ST_CONNECTING:
1557 if (frame->hcheck == true) {
1558 DEBUG(client->worker,
1559 "<%lu> Close client after healthcheck",
1560 client->id);
1561 goto close;
1562 }
1563 client->state = SPOA_ST_PROCESSING;
1564 break;
1565
1566 case SPOA_ST_PROCESSING:
1567 break;
1568
1569 case SPOA_ST_DISCONNECTING:
1570 goto close;
1571 }
1572
1573 release_frame(frame);
1574 client->outgoing_frame = NULL;
1575 if (!client->async && !client->pipelining) {
1576 event_del(&client->write_frame_event);
1577 event_add(&client->read_frame_event, NULL);
1578 }
1579 return;
1580
1581 close:
1582 release_client(client);
1583}
1584
1585static void
1586accept_cb(int listener, short event, void *arg)
1587{
1588 struct worker *worker;
1589 struct client *client;
1590 int fd;
1591
1592 worker = &workers[clicount++ % num_workers];
1593
1594 if ((fd = accept(listener, NULL, NULL)) < 0) {
1595 if (errno != EAGAIN && errno != EWOULDBLOCK)
1596 LOG(worker, "Failed to accept client connection : %m");
1597 return;
1598 }
1599
1600 DEBUG(&null_worker,
1601 "<%lu> New Client connection accepted and assigned to worker %02d",
1602 clicount, worker->id);
1603
1604 if (evutil_make_socket_nonblocking(fd) < 0) {
1605 LOG(&null_worker, "Failed to set client socket to non-blocking : %m");
1606 close(fd);
1607 return;
1608 }
1609
1610 if ((client = calloc(1, sizeof(*client))) == NULL) {
1611 LOG(&null_worker, "Failed to allocate memory for client state : %m");
1612 close(fd);
1613 return;
1614 }
1615
1616 client->id = clicount;
1617 client->fd = fd;
1618 client->worker = worker;
1619 client->state = SPOA_ST_CONNECTING;
1620 client->status_code = SPOE_FRM_ERR_NONE;
1621 client->max_frame_size = max_frame_size;
1622 client->engine = NULL;
1623 client->pipelining = false;
1624 client->async = false;
1625 client->incoming_frame = NULL;
1626 client->outgoing_frame = NULL;
1627 LIST_INIT(&client->processing_frames);
1628 LIST_INIT(&client->outgoing_frames);
1629
1630 LIST_ADDQ(&worker->clients, &client->by_worker);
1631
1632 worker->nbclients++;
1633
1634 if (event_assign(&client->read_frame_event, worker->base, fd,
1635 EV_READ|EV_PERSIST, read_frame_cb, client) < 0 ||
1636 event_assign(&client->write_frame_event, worker->base, fd,
1637 EV_WRITE|EV_PERSIST, write_frame_cb, client) < 0) {
1638 LOG(&null_worker, "Failed to create client events");
1639 release_client(client);
1640 return;
1641 }
1642 event_add(&client->read_frame_event, NULL);
1643}
1644
1645static void *
1646worker_function(void *data)
1647{
1648 struct client *client, *cback;
1649 struct spoe_frame *frame, *fback;
1650 struct worker *worker = data;
1651
1652 DEBUG(worker, "Worker ready to process client messages");
1653 event_base_dispatch(worker->base);
Christopher Faulet010fded2016-11-03 22:49:37 +01001654
Christopher Fauletf95b1112016-12-21 08:58:16 +01001655 list_for_each_entry_safe(client, cback, &worker->clients, by_worker) {
1656 release_client(client);
Christopher Faulet010fded2016-11-03 22:49:37 +01001657 }
1658
Christopher Fauletf95b1112016-12-21 08:58:16 +01001659 list_for_each_entry_safe(frame, fback, &worker->frames, list) {
1660 LIST_DEL(&frame->list);
1661 free(frame);
1662 }
1663
1664 event_free(worker->monitor_event);
1665 event_base_free(worker->base);
1666 DEBUG(worker, "Worker is stopped");
1667 pthread_exit(&null_worker);
1668}
1669
1670
1671static int
1672parse_processing_delay(const char *str)
1673{
1674 unsigned long value;
1675
1676 value = 0;
1677 while (1) {
1678 unsigned int j;
1679
1680 j = *str - '0';
1681 if (j > 9)
1682 break;
1683 str++;
1684 value *= 10;
1685 value += j;
1686 }
1687
1688 switch (*str) {
1689 case '\0': /* no unit = millisecond */
1690 value *= 1000;
1691 break;
1692 case 's': /* second */
1693 value *= 1000000;
1694 str++;
1695 break;
1696 case 'm': /* millisecond : "ms" */
1697 if (str[1] != 's')
1698 return -1;
1699 value *= 1000;
1700 str += 2;
1701 break;
1702 case 'u': /* microsecond : "us" */
1703 if (str[1] != 's')
1704 return -1;
1705 str += 2;
1706 break;
1707 default:
1708 return -1;
1709 }
1710 if (*str)
1711 return -1;
1712
1713 processing_delay.tv_sec = (time_t)(value / 1000000);
1714 processing_delay.tv_usec = (suseconds_t)(value % 1000000);
1715 return 0;
Christopher Faulet010fded2016-11-03 22:49:37 +01001716}
1717
Christopher Fauletf95b1112016-12-21 08:58:16 +01001718
Christopher Faulet010fded2016-11-03 22:49:37 +01001719static void
1720usage(char *prog)
1721{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001722 fprintf(stderr,
1723 "Usage : %s [OPTION]...\n"
1724 " -h Print this message\n"
1725 " -d Enable the debug mode\n"
1726 " -m <max-frame-size> Specify the maximum frame size (default : %u)\n"
1727 " -p <port> Specify the port to listen on (default : %d)\n"
1728 " -n <num-workers> Specify the number of workers (default : %d)\n"
1729 " -c <capability> Enable the support of the specified capability\n"
1730 " -t <time> Set a delay to process a message (default: 0)\n"
1731 " The value is specified in milliseconds by default,\n"
1732 " but can be in any other unit if the number is suffixed\n"
1733 " by a unit (us, ms, s)\n"
1734 "\n"
Christopher Faulet85010352017-02-02 10:14:36 +01001735 " Supported capabilities: fragmentation, pipelining, async\n",
Christopher Fauletf95b1112016-12-21 08:58:16 +01001736 prog, MAX_FRAME_SIZE, DEFAULT_PORT, NUM_WORKERS);
Christopher Faulet010fded2016-11-03 22:49:37 +01001737}
1738
1739int
1740main(int argc, char **argv)
1741{
Christopher Fauletf95b1112016-12-21 08:58:16 +01001742 struct event_base *base = NULL;
1743 struct event *signal_event = NULL, *accept_event = NULL;
1744 int opt, i, fd = -1;
Christopher Faulet010fded2016-11-03 22:49:37 +01001745
Christopher Fauletf95b1112016-12-21 08:58:16 +01001746 // TODO: add '-t <processing-time>' option
1747 while ((opt = getopt(argc, argv, "hdm:n:p:c:t:")) != -1) {
Christopher Faulet010fded2016-11-03 22:49:37 +01001748 switch (opt) {
1749 case 'h':
1750 usage(argv[0]);
1751 return EXIT_SUCCESS;
1752 case 'd':
1753 debug = true;
1754 break;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001755 case 'm':
1756 max_frame_size = atoi(optarg);
1757 break;
Christopher Faulet010fded2016-11-03 22:49:37 +01001758 case 'n':
Christopher Fauletf95b1112016-12-21 08:58:16 +01001759 num_workers = atoi(optarg);
Christopher Faulet010fded2016-11-03 22:49:37 +01001760 break;
1761 case 'p':
Christopher Fauletf95b1112016-12-21 08:58:16 +01001762 server_port = atoi(optarg);
Christopher Faulet010fded2016-11-03 22:49:37 +01001763 break;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001764 case 'c':
1765 if (!strcmp(optarg, "pipelining"))
1766 pipelining = true;
1767 else if (!strcmp(optarg, "async"))
1768 async = true;
Christopher Faulet85010352017-02-02 10:14:36 +01001769 else if (!strcmp(optarg, "fragmentation"))
1770 fragmentation = true;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001771 else
1772 fprintf(stderr, "WARNING: unsupported capability '%s'\n", optarg);
1773 break;
1774 case 't':
1775 if (!parse_processing_delay(optarg))
1776 break;
1777 fprintf(stderr, "%s: failed to parse time '%s'.\n", argv[0], optarg);
1778 fprintf(stderr, "Try '%s -h' for more information.\n", argv[0]);
1779 return EXIT_FAILURE;
Christopher Faulet010fded2016-11-03 22:49:37 +01001780 default:
1781 usage(argv[0]);
1782 return EXIT_FAILURE;
1783 }
1784 }
1785
Christopher Fauletf95b1112016-12-21 08:58:16 +01001786 if (num_workers <= 0) {
1787 LOG(&null_worker, "%s : Invalid number of workers '%d'\n",
1788 argv[0], num_workers);
Christopher Faulet010fded2016-11-03 22:49:37 +01001789 goto error;
1790 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001791
1792 if (server_port <= 0) {
1793 LOG(&null_worker, "%s : Invalid port '%d'\n",
1794 argv[0], server_port);
Christopher Faulet010fded2016-11-03 22:49:37 +01001795 goto error;
1796 }
1797
Christopher Fauletf95b1112016-12-21 08:58:16 +01001798
1799 if (evthread_use_pthreads() < 0) {
1800 LOG(&null_worker, "No pthreads support for libevent");
Christopher Faulet010fded2016-11-03 22:49:37 +01001801 goto error;
1802 }
1803
Christopher Fauletf95b1112016-12-21 08:58:16 +01001804 if ((base = event_base_new()) == NULL) {
1805 LOG(&null_worker, "Failed to initialize libevent : %m");
1806 goto error;
1807 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001808
Christopher Fauletf95b1112016-12-21 08:58:16 +01001809 signal(SIGPIPE, SIG_IGN);
Christopher Faulet010fded2016-11-03 22:49:37 +01001810
Christopher Fauletf95b1112016-12-21 08:58:16 +01001811 if ((fd = create_server_socket()) < 0) {
1812 LOG(&null_worker, "Failed to create server socket");
1813 goto error;
1814 }
1815 if (evutil_make_socket_nonblocking(fd) < 0) {
1816 LOG(&null_worker, "Failed to set server socket to non-blocking");
Christopher Faulet010fded2016-11-03 22:49:37 +01001817 goto error;
1818 }
1819
Christopher Fauletf95b1112016-12-21 08:58:16 +01001820 if ((workers = calloc(num_workers, sizeof(*workers))) == NULL) {
1821 LOG(&null_worker, "Failed to set allocate memory for workers");
Christopher Faulet010fded2016-11-03 22:49:37 +01001822 goto error;
1823 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001824
1825 for (i = 0; i < num_workers; ++i) {
1826 struct worker *w = &workers[i];
Christopher Faulet010fded2016-11-03 22:49:37 +01001827
Christopher Fauletf95b1112016-12-21 08:58:16 +01001828 w->id = i+1;
1829 w->nbclients = 0;
Christopher Faulet0b89f722018-01-23 14:46:51 +01001830 w->nbframes = 0;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001831 LIST_INIT(&w->engines);
1832 LIST_INIT(&w->clients);
1833 LIST_INIT(&w->frames);
1834
1835 if ((w->base = event_base_new()) == NULL) {
1836 LOG(&null_worker,
1837 "Failed to initialize libevent for worker %02d : %m",
1838 w->id);
1839 goto error;
1840 }
Christopher Faulet010fded2016-11-03 22:49:37 +01001841
Christopher Fauletf95b1112016-12-21 08:58:16 +01001842 w->monitor_event = event_new(w->base, fd, EV_PERSIST,
1843 worker_monitor_cb, (void *)w);
1844 if (w->monitor_event == NULL ||
1845 event_add(w->monitor_event, (struct timeval[]){{5,0}}) < 0) {
1846 LOG(&null_worker,
1847 "Failed to create monitor event for worker %02d",
1848 w->id);
Christopher Faulet010fded2016-11-03 22:49:37 +01001849 goto error;
1850 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001851
1852 if (pthread_create(&w->thread, NULL, worker_function, (void *)w)) {
1853 LOG(&null_worker,
1854 "Failed to start thread for worker %02d : %m",
1855 w->id);
1856 }
1857 DEBUG(&null_worker, "Worker %02d initialized", w->id);
1858 }
1859
1860 accept_event = event_new(base, fd, EV_READ|EV_PERSIST, accept_cb,
1861 (void *)base);
1862 if (accept_event == NULL || event_add(accept_event, NULL) < 0) {
1863 LOG(&null_worker, "Failed to create accept event : %m");
1864 }
1865
1866 signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
1867 if (signal_event == NULL || event_add(signal_event, NULL) < 0) {
1868 LOG(&null_worker, "Failed to create signal event : %m");
Christopher Faulet010fded2016-11-03 22:49:37 +01001869 }
1870
Christopher Fauletf95b1112016-12-21 08:58:16 +01001871 DEBUG(&null_worker,
1872 "Server is ready"
Christopher Faulet85010352017-02-02 10:14:36 +01001873 " [fragmentation=%s - pipelining=%s - async=%s - debug=%s - max-frame-size=%u]",
1874 (fragmentation?"true":"false"), (pipelining?"true":"false"), (async?"true":"false"),
Christopher Fauletf95b1112016-12-21 08:58:16 +01001875 (debug?"true":"false"), max_frame_size);
1876 event_base_dispatch(base);
1877
1878 for (i = 0; i < num_workers; i++) {
1879 struct worker *w = &workers[i];
1880
1881 pthread_join(w->thread, NULL);
1882 DEBUG(&null_worker, "Worker %02d terminated", w->id);
Christopher Faulet010fded2016-11-03 22:49:37 +01001883 }
Christopher Fauletf95b1112016-12-21 08:58:16 +01001884
1885 free(workers);
1886 event_free(signal_event);
1887 event_free(accept_event);
1888 event_base_free(base);
1889 close(fd);
Christopher Faulet010fded2016-11-03 22:49:37 +01001890 return EXIT_SUCCESS;
Christopher Fauletf95b1112016-12-21 08:58:16 +01001891
1892 error:
1893 if (workers != NULL)
1894 free(workers);
1895 if (signal_event != NULL)
1896 event_free(signal_event);
1897 if (accept_event != NULL)
1898 event_free(accept_event);
1899 if (base != NULL)
1900 event_base_free(base);
1901 if (fd != -1)
1902 close(fd);
Christopher Faulet010fded2016-11-03 22:49:37 +01001903 return EXIT_FAILURE;
1904}