Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1 | /* |
| 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 Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 17 | #include <unistd.h> |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <stdbool.h> |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | #include <stdio.h> |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 23 | #include <signal.h> |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 24 | #include <netinet/in.h> |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 25 | #include <sys/socket.h> |
| 26 | #include <err.h> |
| 27 | #include <ctype.h> |
| 28 | |
| 29 | #include <pthread.h> |
| 30 | |
| 31 | #include <event2/util.h> |
| 32 | #include <event2/event.h> |
| 33 | #include <event2/event_struct.h> |
| 34 | #include <event2/thread.h> |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 35 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 36 | #include <common/mini-clist.h> |
Christopher Faulet | 1f40b91 | 2017-02-17 09:32:19 +0100 | [diff] [blame^] | 37 | #include <common/chunk.h> |
| 38 | |
| 39 | #include <types/spoe.h> |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 40 | |
| 41 | #define DEFAULT_PORT 12345 |
| 42 | #define CONNECTION_BACKLOG 10 |
| 43 | #define NUM_WORKERS 10 |
| 44 | #define MAX_FRAME_SIZE 16384 |
| 45 | #define SPOP_VERSION "1.0" |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 46 | |
| 47 | #define SLEN(str) (sizeof(str)-1) |
| 48 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 49 | #define LOG(worker, fmt, args...) \ |
| 50 | do { \ |
| 51 | struct timeval now; \ |
| 52 | \ |
| 53 | gettimeofday(&now, NULL); \ |
| 54 | fprintf(stderr, "%ld.%06ld [%02d] " fmt "\n", \ |
| 55 | now.tv_sec, now.tv_usec, (worker)->id, ##args); \ |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 56 | } while (0) |
| 57 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 58 | #define DEBUG(x...) \ |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 59 | do { \ |
| 60 | if (debug) \ |
| 61 | LOG(x); \ |
| 62 | } while (0) |
| 63 | |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 64 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 65 | enum spoa_state { |
| 66 | SPOA_ST_CONNECTING = 0, |
| 67 | SPOA_ST_PROCESSING, |
| 68 | SPOA_ST_DISCONNECTING, |
| 69 | }; |
| 70 | |
| 71 | enum spoa_frame_type { |
| 72 | SPOA_FRM_T_UNKNOWN = 0, |
| 73 | SPOA_FRM_T_HAPROXY, |
| 74 | SPOA_FRM_T_AGENT, |
| 75 | }; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 76 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 77 | struct spoe_engine { |
| 78 | char *id; |
| 79 | |
| 80 | struct list processing_frames; |
| 81 | struct list outgoing_frames; |
| 82 | |
| 83 | struct list clients; |
| 84 | struct list list; |
| 85 | }; |
| 86 | |
| 87 | struct spoe_frame { |
| 88 | enum spoa_frame_type type; |
| 89 | char *buf; |
| 90 | unsigned int offset; |
| 91 | unsigned int len; |
| 92 | |
| 93 | unsigned int stream_id; |
| 94 | unsigned int frame_id; |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 95 | unsigned int flags; |
| 96 | bool hcheck; /* true is the CONNECT frame is a healthcheck */ |
| 97 | bool fragmented; /* true if the frame is fragmented */ |
| 98 | int ip_score; /* -1 if unset, else between 0 and 100 */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 99 | |
| 100 | struct event process_frame_event; |
| 101 | struct worker *worker; |
| 102 | struct spoe_engine *engine; |
| 103 | struct client *client; |
| 104 | struct list list; |
| 105 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 106 | char *frag_buf; /* used to accumulate payload of a fragmented frame */ |
| 107 | unsigned int frag_len; |
| 108 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 109 | char data[0]; |
| 110 | }; |
| 111 | |
| 112 | struct client { |
| 113 | int fd; |
| 114 | unsigned long id; |
| 115 | enum spoa_state state; |
| 116 | |
| 117 | struct event read_frame_event; |
| 118 | struct event write_frame_event; |
| 119 | |
| 120 | struct spoe_frame *incoming_frame; |
| 121 | struct spoe_frame *outgoing_frame; |
| 122 | |
| 123 | struct list processing_frames; |
| 124 | struct list outgoing_frames; |
| 125 | |
| 126 | unsigned int max_frame_size; |
| 127 | int status_code; |
| 128 | |
| 129 | char *engine_id; |
| 130 | struct spoe_engine *engine; |
| 131 | bool pipelining; |
| 132 | bool async; |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 133 | bool fragmentation; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 134 | |
| 135 | struct worker *worker; |
| 136 | struct list by_worker; |
| 137 | struct list by_engine; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | struct worker { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 141 | pthread_t thread; |
| 142 | int id; |
| 143 | struct event_base *base; |
| 144 | struct event *monitor_event; |
| 145 | |
| 146 | struct list engines; |
| 147 | |
| 148 | unsigned int nbclients; |
| 149 | struct list clients; |
| 150 | |
| 151 | struct list frames; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 152 | }; |
| 153 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 154 | |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 155 | union spoe_value { |
| 156 | bool boolean; /* use for boolean */ |
| 157 | int32_t sint32; /* used for signed 32bits integers */ |
| 158 | uint32_t uint32; /* used for signed 32bits integers */ |
| 159 | int32_t sint64; /* used for signed 64bits integers */ |
| 160 | uint32_t uint64; /* used for signed 64bits integers */ |
| 161 | struct in_addr ipv4; /* used for ipv4 addresses */ |
| 162 | struct in6_addr ipv6; /* used for ipv6 addresses */ |
| 163 | struct chunk buffer; /* used for char strings or buffers */ |
| 164 | }; |
| 165 | |
| 166 | /* Used to store sample constant */ |
| 167 | struct spoe_data { |
| 168 | enum spoe_data_type type; /* SPOE_DATA_T_* */ |
| 169 | union spoe_value u; /* spoe data value */ |
| 170 | }; |
| 171 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 172 | /* Globals */ |
| 173 | static struct worker *workers = NULL; |
| 174 | static struct worker null_worker = { .id = 0 }; |
| 175 | static unsigned long clicount = 0; |
| 176 | static int server_port = DEFAULT_PORT; |
| 177 | static int num_workers = NUM_WORKERS; |
| 178 | static unsigned int max_frame_size = MAX_FRAME_SIZE; |
| 179 | struct timeval processing_delay = {0, 0}; |
| 180 | static bool debug = false; |
| 181 | static bool pipelining = false; |
| 182 | static bool async = false; |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 183 | static bool fragmentation = false; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 184 | |
| 185 | |
| 186 | static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = { |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 187 | [SPOE_FRM_ERR_NONE] = "normal", |
| 188 | [SPOE_FRM_ERR_IO] = "I/O error", |
| 189 | [SPOE_FRM_ERR_TOUT] = "a timeout occurred", |
| 190 | [SPOE_FRM_ERR_TOO_BIG] = "frame is too big", |
| 191 | [SPOE_FRM_ERR_INVALID] = "invalid frame received", |
| 192 | [SPOE_FRM_ERR_NO_VSN] = "version value not found", |
| 193 | [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found", |
| 194 | [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found", |
| 195 | [SPOE_FRM_ERR_BAD_VSN] = "unsupported version", |
| 196 | [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small", |
| 197 | [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported", |
| 198 | [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames", |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 199 | [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found", |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 200 | [SPOE_FRM_ERR_RES] = "resource allocation error", |
| 201 | [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred", |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | static void signal_cb(evutil_socket_t, short, void *); |
| 205 | static void accept_cb(evutil_socket_t, short, void *); |
| 206 | static void worker_monitor_cb(evutil_socket_t, short, void *); |
| 207 | static void process_frame_cb(evutil_socket_t, short, void *); |
| 208 | static void read_frame_cb(evutil_socket_t, short, void *); |
| 209 | static void write_frame_cb(evutil_socket_t, short, void *); |
| 210 | |
| 211 | static void use_spoe_engine(struct client *); |
| 212 | static void unuse_spoe_engine(struct client *); |
| 213 | static void release_frame(struct spoe_frame *); |
| 214 | static void release_client(struct client *); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 215 | |
| 216 | static void |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 217 | check_ipv4_reputation(struct spoe_frame *frame, struct in_addr *ipv4) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 218 | { |
| 219 | char str[INET_ADDRSTRLEN]; |
| 220 | |
| 221 | if (inet_ntop(AF_INET, ipv4, str, INET_ADDRSTRLEN) == NULL) |
| 222 | return; |
| 223 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 224 | frame->ip_score = random() % 100; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 225 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 226 | DEBUG(frame->worker, "IP score for %.*s is %d", |
| 227 | INET_ADDRSTRLEN, str, frame->ip_score); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static void |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 231 | check_ipv6_reputation(struct spoe_frame *frame, struct in6_addr *ipv6) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 232 | { |
| 233 | char str[INET6_ADDRSTRLEN]; |
| 234 | |
| 235 | if (inet_ntop(AF_INET6, ipv6, str, INET6_ADDRSTRLEN) == NULL) |
| 236 | return; |
| 237 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 238 | frame->ip_score = random() % 100; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 239 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 240 | DEBUG(frame->worker, "IP score for %.*s is %d", |
| 241 | INET6_ADDRSTRLEN, str, frame->ip_score); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 244 | |
| 245 | /* Encode a variable-length integer. This function never fails and returns the |
| 246 | * number of written bytes. */ |
| 247 | static int |
| 248 | encode_spoe_varint(uint64_t i, char *buf) |
| 249 | { |
| 250 | int idx; |
| 251 | |
| 252 | if (i < 240) { |
| 253 | buf[0] = (unsigned char)i; |
| 254 | return 1; |
| 255 | } |
| 256 | |
| 257 | buf[0] = (unsigned char)i | 240; |
| 258 | i = (i - 240) >> 4; |
| 259 | for (idx = 1; i >= 128; ++idx) { |
| 260 | buf[idx] = (unsigned char)i | 128; |
| 261 | i = (i - 128) >> 7; |
| 262 | } |
| 263 | buf[idx++] = (unsigned char)i; |
| 264 | return idx; |
| 265 | } |
| 266 | |
| 267 | /* Decode a varable-length integer. If the decoding fails, -1 is returned. This |
| 268 | * happens when the buffer's end in reached. On success, the number of read |
| 269 | * bytes is returned. */ |
| 270 | static int |
| 271 | decode_spoe_varint(char *buf, char *end, uint64_t *i) |
| 272 | { |
| 273 | unsigned char *msg = (unsigned char *)buf; |
| 274 | int idx = 0; |
| 275 | |
| 276 | if (msg > (unsigned char *)end) |
| 277 | return -1; |
| 278 | |
| 279 | if (msg[0] < 240) { |
| 280 | *i = msg[0]; |
| 281 | return 1; |
| 282 | } |
| 283 | *i = msg[0]; |
| 284 | do { |
| 285 | ++idx; |
| 286 | if (msg+idx > (unsigned char *)end) |
| 287 | return -1; |
| 288 | *i += (uint64_t)msg[idx] << (4 + 7 * (idx-1)); |
| 289 | } while (msg[idx] >= 128); |
| 290 | return (idx + 1); |
| 291 | } |
| 292 | |
| 293 | /* Encode a string. The string will be prefix by its length, encoded as a |
| 294 | * variable-length integer. This function never fails and returns the number of |
| 295 | * written bytes. */ |
| 296 | static int |
| 297 | encode_spoe_string(const char *str, size_t len, char *dst) |
| 298 | { |
| 299 | int idx = 0; |
| 300 | |
| 301 | if (!len) { |
| 302 | dst[0] = 0; |
| 303 | return 1; |
| 304 | } |
| 305 | |
| 306 | idx += encode_spoe_varint(len, dst); |
| 307 | memcpy(dst+idx, str, len); |
| 308 | return (idx + len); |
| 309 | } |
| 310 | |
| 311 | /* Decode a string. Its length is decoded first as a variable-length integer. If |
| 312 | * it succeeds, and if the string length is valid, the begin of the string is |
| 313 | * saved in <*str>, its length is saved in <*len> and the total numbre of bytes |
| 314 | * read is returned. If an error occurred, -1 is returned and <*str> remains |
| 315 | * NULL. */ |
| 316 | static int |
| 317 | decode_spoe_string(char *buf, char *end, char **str, uint64_t *len) |
| 318 | { |
| 319 | int r, idx = 0; |
| 320 | |
| 321 | *str = NULL; |
| 322 | *len = 0; |
| 323 | |
| 324 | if ((r = decode_spoe_varint(buf, end, len)) == -1) |
| 325 | goto error; |
| 326 | idx += r; |
| 327 | if (buf + idx + *len > end) |
| 328 | goto error; |
| 329 | |
| 330 | *str = buf+idx; |
| 331 | return (idx + *len); |
| 332 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 333 | error: |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | /* Skip a typed data. If an error occurred, -1 is returned, otherwise the number |
| 338 | * of bytes read is returned. A types data is composed of a type (1 byte) and |
| 339 | * corresponding data: |
| 340 | * - boolean: non additional data (0 bytes) |
| 341 | * - integers: a variable-length integer (see decode_spoe_varint) |
| 342 | * - ipv4: 4 bytes |
| 343 | * - ipv6: 16 bytes |
| 344 | * - binary and string: a buffer prefixed by its size, a variable-length |
| 345 | * integer (see decode_spoe_string) */ |
| 346 | static int |
| 347 | skip_spoe_data(char *frame, char *end) |
| 348 | { |
| 349 | uint64_t sz = 0; |
| 350 | int r, idx = 0; |
| 351 | |
| 352 | if (frame > end) |
| 353 | return -1; |
| 354 | |
| 355 | switch (frame[idx++] & SPOE_DATA_T_MASK) { |
| 356 | case SPOE_DATA_T_BOOL: |
| 357 | idx++; |
| 358 | break; |
| 359 | case SPOE_DATA_T_INT32: |
| 360 | case SPOE_DATA_T_INT64: |
| 361 | case SPOE_DATA_T_UINT32: |
| 362 | case SPOE_DATA_T_UINT64: |
| 363 | if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1) |
| 364 | return -1; |
| 365 | idx += r; |
| 366 | break; |
| 367 | case SPOE_DATA_T_IPV4: |
| 368 | idx += 4; |
| 369 | break; |
| 370 | case SPOE_DATA_T_IPV6: |
| 371 | idx += 16; |
| 372 | break; |
| 373 | case SPOE_DATA_T_STR: |
| 374 | case SPOE_DATA_T_BIN: |
| 375 | if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1) |
| 376 | return -1; |
| 377 | idx += r + sz; |
| 378 | break; |
| 379 | } |
| 380 | |
| 381 | if (frame+idx > end) |
| 382 | return -1; |
| 383 | return idx; |
| 384 | } |
| 385 | |
| 386 | /* Decode a typed data. If an error occurred, -1 is returned, otherwise the |
| 387 | * number of read bytes is returned. See skip_spoe_data for details. */ |
| 388 | static int |
| 389 | decode_spoe_data(char *frame, char *end, struct spoe_data *data) |
| 390 | { |
| 391 | uint64_t sz = 0; |
| 392 | int type, r, idx = 0; |
| 393 | |
| 394 | if (frame > end) |
| 395 | return -1; |
| 396 | |
| 397 | type = frame[idx++]; |
| 398 | data->type = (type & SPOE_DATA_T_MASK); |
| 399 | switch (data->type) { |
| 400 | case SPOE_DATA_T_BOOL: |
| 401 | data->u.boolean = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE); |
| 402 | break; |
| 403 | case SPOE_DATA_T_INT32: |
| 404 | if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1) |
| 405 | return -1; |
| 406 | data->u.sint32 = sz; |
| 407 | idx += r; |
| 408 | break; |
| 409 | case SPOE_DATA_T_INT64: |
| 410 | if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1) |
| 411 | return -1; |
| 412 | data->u.uint32 = sz; |
| 413 | idx += r; |
| 414 | break; |
| 415 | case SPOE_DATA_T_UINT32: |
| 416 | if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1) |
| 417 | return -1; |
| 418 | data->u.sint64 = sz; |
| 419 | idx += r; |
| 420 | break; |
| 421 | case SPOE_DATA_T_UINT64: |
| 422 | if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1) |
| 423 | return -1; |
| 424 | data->u.uint64 = sz; |
| 425 | idx += r; |
| 426 | break; |
| 427 | case SPOE_DATA_T_IPV4: |
| 428 | if (frame+idx+4 > end) |
| 429 | return -1; |
| 430 | memcpy(&data->u.ipv4, frame+idx, 4); |
| 431 | idx += 4; |
| 432 | break; |
| 433 | case SPOE_DATA_T_IPV6: |
| 434 | if (frame+idx+16 > end) |
| 435 | return -1; |
| 436 | memcpy(&data->u.ipv6, frame+idx, 16); |
| 437 | idx += 16; |
| 438 | break; |
| 439 | case SPOE_DATA_T_STR: |
| 440 | if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1) |
| 441 | return -1; |
| 442 | idx += r; |
| 443 | if (frame+idx+sz > end) |
| 444 | return -1; |
| 445 | data->u.buffer.str = frame+idx; |
| 446 | data->u.buffer.len = sz; |
| 447 | idx += sz; |
| 448 | break; |
| 449 | case SPOE_DATA_T_BIN: |
| 450 | if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1) |
| 451 | return -1; |
| 452 | idx += r; |
| 453 | if (frame+idx+sz > end) |
| 454 | return -1; |
| 455 | data->u.buffer.str = frame+idx; |
| 456 | data->u.buffer.len = sz; |
| 457 | idx += sz; |
| 458 | break; |
| 459 | default: |
| 460 | break; |
| 461 | } |
| 462 | |
| 463 | if (frame+idx > end) |
| 464 | return -1; |
| 465 | return idx; |
| 466 | } |
| 467 | |
| 468 | |
| 469 | /* Check the protocol version. It returns -1 if an error occurred, the number of |
| 470 | * read bytes otherwise. */ |
| 471 | static int |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 472 | check_proto_version(struct spoe_frame *frame, int idx) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 473 | { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 474 | char *str; |
| 475 | uint64_t sz; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 476 | |
| 477 | /* Get the list of all supported versions by HAProxy */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 478 | if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 479 | return -1; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 480 | idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz); |
| 481 | if (str == NULL) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 482 | return -1; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 483 | |
| 484 | DEBUG(frame->worker, "<%lu> Supported versions : %.*s", |
| 485 | frame->client->id, (int)sz, str); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 486 | |
| 487 | /* TODO: Find the right verion in supported ones */ |
| 488 | |
| 489 | return idx; |
| 490 | } |
| 491 | |
| 492 | /* Check max frame size value. It returns -1 if an error occurred, the number of |
| 493 | * read bytes otherwise. */ |
| 494 | static int |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 495 | check_max_frame_size(struct spoe_frame *frame, int idx) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 496 | { |
| 497 | uint64_t sz; |
| 498 | int type, i; |
| 499 | |
| 500 | /* Get the max-frame-size value of HAProxy */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 501 | type = frame->buf[idx++]; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 502 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 503 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 504 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 505 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 506 | return -1; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 507 | if ((i = decode_spoe_varint(frame->buf+idx, frame->buf+frame->len, &sz)) == -1) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 508 | return -1; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 509 | idx += i; |
| 510 | |
| 511 | /* Keep the lower value */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 512 | if (sz < frame->client->max_frame_size) |
| 513 | frame->client->max_frame_size = sz; |
| 514 | |
| 515 | DEBUG(frame->worker, "<%lu> HAProxy maximum frame size : %u", |
| 516 | frame->client->id, (unsigned int)sz); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 517 | |
| 518 | return idx; |
| 519 | } |
| 520 | |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 521 | /* Check healthcheck value. It returns -1 if an error occurred, the number of |
| 522 | * read bytes otherwise. */ |
| 523 | static int |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 524 | check_healthcheck(struct spoe_frame *frame, int idx) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 525 | { |
| 526 | int type; |
| 527 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 528 | /* Get the "healthcheck" value */ |
| 529 | type = frame->buf[idx++]; |
| 530 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_BOOL) |
| 531 | return -1; |
| 532 | frame->hcheck = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE); |
| 533 | |
| 534 | DEBUG(frame->worker, "<%lu> HELLO healthcheck : %s", |
| 535 | frame->client->id, (frame->hcheck ? "true" : "false")); |
| 536 | return idx; |
| 537 | } |
| 538 | |
| 539 | /* Check capabilities value. It returns -1 if an error occurred, the number of |
| 540 | * read bytes otherwise. */ |
| 541 | static int |
| 542 | check_capabilities(struct spoe_frame *frame, int idx) |
| 543 | { |
| 544 | struct client *client = frame->client; |
| 545 | char *str; |
| 546 | uint64_t sz; |
| 547 | int i; |
| 548 | |
| 549 | if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 550 | return -1; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 551 | idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz); |
| 552 | if (str == NULL) /* this is not an error */ |
| 553 | return idx; |
| 554 | |
| 555 | DEBUG(frame->worker, "<%lu> HAProxy capabilities : %.*s", |
| 556 | client->id, (int)sz, str); |
| 557 | |
| 558 | i = 0; |
| 559 | while (i < sz) { |
| 560 | char *delim; |
| 561 | |
| 562 | /* Skip leading spaces */ |
| 563 | for (; isspace(str[i]) && i < sz; i++); |
| 564 | |
| 565 | if (sz - i >= 10 && !strncmp(str + i, "pipelining", 10)) { |
| 566 | i += 10; |
| 567 | if (sz == i || isspace(str[i]) || str[i] == ',') { |
| 568 | DEBUG(frame->worker, |
| 569 | "<%lu> HAProxy supports frame pipelining", |
| 570 | client->id); |
| 571 | client->pipelining = true; |
| 572 | } |
| 573 | |
| 574 | } |
| 575 | else if (sz - i >= 5 && !strncmp(str + i, "async", 5)) { |
| 576 | i += 5; |
| 577 | if (sz == i || isspace(str[i]) || str[i] == ',') { |
| 578 | DEBUG(frame->worker, |
| 579 | "<%lu> HAProxy supports asynchronous frame", |
| 580 | client->id); |
| 581 | client->async = true; |
| 582 | } |
| 583 | } |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 584 | else if (sz - i >= 13 && !strncmp(str + i, "fragmentation", 13)) { |
| 585 | i += 5; |
| 586 | if (sz == i || isspace(str[i]) || str[i] == ',') { |
| 587 | DEBUG(frame->worker, |
| 588 | "<%lu> HAProxy supports fragmented frame", |
| 589 | client->id); |
| 590 | client->fragmentation = true; |
| 591 | } |
| 592 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 593 | |
| 594 | if (sz == i || (delim = memchr(str + i, ',', sz-i)) == NULL) |
| 595 | break; |
| 596 | i = (delim - str) + 1; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 597 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 598 | |
| 599 | return idx; |
| 600 | } |
| 601 | |
| 602 | /* Check engine-id value. It returns -1 if an error occurred, the number of |
| 603 | * read bytes otherwise. */ |
| 604 | static int |
| 605 | check_engine_id(struct spoe_frame *frame, int idx) |
| 606 | { |
| 607 | struct client *client = frame->client; |
| 608 | char *str; |
| 609 | uint64_t sz; |
| 610 | |
| 611 | if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) |
| 612 | return -1; |
| 613 | |
| 614 | idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz); |
| 615 | if (str == NULL) /* this is not an error */ |
| 616 | return idx; |
| 617 | |
| 618 | if (client->engine != NULL) |
| 619 | return idx; |
| 620 | |
| 621 | DEBUG(frame->worker, "<%lu> HAProxy engine id : %.*s", |
| 622 | client->id, (int)sz, str); |
| 623 | |
| 624 | client->engine_id = strndup(str, (int)sz); |
| 625 | return idx; |
| 626 | } |
| 627 | |
| 628 | /* Check disconnect status code. It returns -1 if an error occurred, the number |
| 629 | * of read bytes otherwise. */ |
| 630 | static int |
| 631 | check_discon_status_code(struct spoe_frame *frame, int idx) |
| 632 | { |
| 633 | uint64_t sz; |
| 634 | int type, i; |
| 635 | |
| 636 | /* Get the "status-code" value */ |
| 637 | type = frame->buf[idx++]; |
| 638 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 639 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 640 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 641 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) |
| 642 | return -1; |
| 643 | if ((i = decode_spoe_varint(frame->buf+idx, frame->buf+frame->len, &sz)) == -1) |
| 644 | return -1; |
| 645 | idx += i; |
| 646 | |
| 647 | frame->client->status_code = (unsigned int)sz; |
| 648 | |
| 649 | DEBUG(frame->worker, "<%lu> Disconnect status code : %u", |
| 650 | frame->client->id, frame->client->status_code); |
| 651 | |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 652 | return idx; |
| 653 | } |
| 654 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 655 | /* Check the disconnect message. It returns -1 if an error occurred, the number |
| 656 | * of read bytes otherwise. */ |
| 657 | static int |
| 658 | check_discon_message(struct spoe_frame *frame, int idx) |
| 659 | { |
| 660 | char *str; |
| 661 | uint64_t sz; |
| 662 | |
| 663 | /* Get the "message" value */ |
| 664 | if ((frame->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) |
| 665 | return -1; |
| 666 | idx += decode_spoe_string(frame->buf+idx, frame->buf+frame->len, &str, &sz); |
| 667 | if (str == NULL) |
| 668 | return -1; |
| 669 | |
| 670 | DEBUG(frame->worker, "<%lu> Disconnect message : %.*s", |
| 671 | frame->client->id, (int)sz, str); |
| 672 | |
| 673 | return idx; |
| 674 | } |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 675 | |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 676 | /* Decode a HELLO frame received from HAProxy. It returns -1 if an error |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 677 | * occurred, otherwise the number of read bytes. HELLO frame cannot be |
| 678 | * ignored and having another frame than a HELLO frame is an error. */ |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 679 | static int |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 680 | handle_hahello(struct spoe_frame *frame) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 681 | { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 682 | struct client *client = frame->client; |
| 683 | char *buf = frame->buf; |
| 684 | char *end = frame->buf + frame->len; |
| 685 | int i, idx = 0; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 686 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 687 | /* Check frame type: we really want a HELLO frame */ |
| 688 | if (buf[idx++] != SPOE_FRM_T_HAPROXY_HELLO) |
| 689 | goto error; |
| 690 | |
| 691 | DEBUG(frame->worker, "<%lu> Decode HAProxy HELLO frame", client->id); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 692 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 693 | /* Retrieve flags */ |
| 694 | memcpy((char *)&(frame->flags), buf+idx, 4); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 695 | idx += 4; |
| 696 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 697 | /* Fragmentation is not supported for HELLO frame */ |
| 698 | if (!(frame->flags & SPOE_FRM_FL_FIN)) { |
| 699 | client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
| 700 | goto error; |
| 701 | } |
| 702 | |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 703 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 704 | if (buf[idx] != 0 || buf[idx+1] != 0) { |
| 705 | client->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 706 | goto error; |
| 707 | } |
| 708 | idx += 2; |
| 709 | |
| 710 | /* Loop on K/V items */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 711 | while (buf+idx < end) { |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 712 | char *str; |
| 713 | uint64_t sz; |
| 714 | |
| 715 | /* Decode the item name */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 716 | idx += decode_spoe_string(buf+idx, end, &str, &sz); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 717 | if (str == NULL) { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 718 | client->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 719 | goto error; |
| 720 | } |
| 721 | |
| 722 | /* Check "supported-versions" K/V item */ |
| 723 | if (!memcmp(str, "supported-versions", sz)) { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 724 | if ((i = check_proto_version(frame, idx)) == -1) { |
| 725 | client->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 726 | goto error; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 727 | } |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 728 | idx = i; |
| 729 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 730 | /* Check "max-frame-size" K/V item */ |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 731 | else if (!memcmp(str, "max-frame-size", sz)) { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 732 | if ((i = check_max_frame_size(frame, idx)) == -1) { |
| 733 | client->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 734 | goto error; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 735 | } |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 736 | idx = i; |
| 737 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 738 | /* Check "healthcheck" K/V item */ |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 739 | else if (!memcmp(str, "healthcheck", sz)) { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 740 | if ((i = check_healthcheck(frame, idx)) == -1) { |
| 741 | client->status_code = SPOE_FRM_ERR_INVALID; |
| 742 | goto error; |
| 743 | } |
| 744 | idx = i; |
| 745 | } |
| 746 | /* Check "capabilities" K/V item */ |
| 747 | else if (!memcmp(str, "capabilities", sz)) { |
| 748 | if ((i = check_capabilities(frame, idx)) == -1) { |
| 749 | client->status_code = SPOE_FRM_ERR_INVALID; |
| 750 | goto error; |
| 751 | } |
| 752 | idx = i; |
| 753 | } |
| 754 | /* Check "engine-id" K/V item */ |
| 755 | else if (!memcmp(str, "engine-id", sz)) { |
| 756 | if ((i = check_engine_id(frame, idx)) == -1) { |
| 757 | client->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 758 | goto error; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 759 | } |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 760 | idx = i; |
| 761 | } |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 762 | else { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 763 | DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s", |
| 764 | client->id, (int)sz, str); |
| 765 | |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 766 | /* Silently ignore unknown item */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 767 | if ((i = skip_spoe_data(buf+idx, end)) == -1) { |
| 768 | client->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 769 | goto error; |
| 770 | } |
| 771 | idx += i; |
| 772 | } |
| 773 | } |
| 774 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 775 | if (async == false || client->engine_id == NULL) |
| 776 | client->async = false; |
| 777 | if (pipelining == false) |
| 778 | client->pipelining = false; |
| 779 | |
| 780 | if (client->async == true) |
| 781 | use_spoe_engine(client); |
| 782 | |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 783 | return idx; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 784 | |
| 785 | error: |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 786 | return -1; |
| 787 | } |
| 788 | |
| 789 | /* Decode a DISCONNECT frame received from HAProxy. It returns -1 if an error |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 790 | * occurred, otherwise the number of read bytes. DISCONNECT frame cannot be |
| 791 | * ignored and having another frame than a DISCONNECT frame is an error.*/ |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 792 | static int |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 793 | handle_hadiscon(struct spoe_frame *frame) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 794 | { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 795 | struct client *client = frame->client; |
| 796 | char *buf = frame->buf; |
| 797 | char *end = frame->buf + frame->len; |
| 798 | int i, idx = 0; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 799 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 800 | /* Check frame type: we really want a DISCONNECT frame */ |
| 801 | if (buf[idx++] != SPOE_FRM_T_HAPROXY_DISCON) |
| 802 | goto error; |
| 803 | |
| 804 | DEBUG(frame->worker, "<%lu> Decode HAProxy DISCONNECT frame", client->id); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 805 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 806 | /* Retrieve flags */ |
| 807 | memcpy((char *)&(frame->flags), buf+idx, 4); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 808 | idx += 4; |
| 809 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 810 | /* Fragmentation is not supported for DISCONNECT frame */ |
| 811 | if (!(frame->flags & SPOE_FRM_FL_FIN)) { |
| 812 | client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
| 813 | goto error; |
| 814 | } |
| 815 | |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 816 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 817 | if (buf[idx] != 0 || buf[idx+1] != 0) { |
| 818 | client->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 819 | goto error; |
| 820 | } |
| 821 | idx += 2; |
| 822 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 823 | client->status_code = SPOE_FRM_ERR_NONE; |
| 824 | |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 825 | /* Loop on K/V items */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 826 | while (buf+idx < end) { |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 827 | char *str; |
| 828 | uint64_t sz; |
| 829 | |
| 830 | /* Decode item key */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 831 | idx += decode_spoe_string(buf+idx, end, &str, &sz); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 832 | if (str == NULL) { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 833 | client->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 834 | goto error; |
| 835 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 836 | |
| 837 | /* Check "status-code" K/V item */ |
| 838 | if (!memcmp(str, "status-code", sz)) { |
| 839 | if ((i = check_discon_status_code(frame, idx)) == -1) { |
| 840 | client->status_code = SPOE_FRM_ERR_INVALID; |
| 841 | goto error; |
| 842 | } |
| 843 | idx = i; |
| 844 | } |
| 845 | /* Check "message" K/V item */ |
| 846 | else if (!memcmp(str, "message", sz)) { |
| 847 | if ((i = check_discon_message(frame, idx)) == -1) { |
| 848 | client->status_code = SPOE_FRM_ERR_INVALID; |
| 849 | goto error; |
| 850 | } |
| 851 | idx = i; |
| 852 | } |
| 853 | else { |
| 854 | DEBUG(frame->worker, "<%lu> Skip K/V item : key=%.*s", |
| 855 | client->id, (int)sz, str); |
| 856 | |
| 857 | /* Silently ignore unknown item */ |
| 858 | if ((i = skip_spoe_data(buf+idx, end)) == -1) { |
| 859 | client->status_code = SPOE_FRM_ERR_INVALID; |
| 860 | goto error; |
| 861 | } |
| 862 | idx += i; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 863 | } |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 864 | } |
| 865 | |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 866 | return idx; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 867 | |
| 868 | error: |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 869 | return -1; |
| 870 | } |
| 871 | |
| 872 | /* Decode a NOTIFY frame received from HAProxy. It returns -1 if an error |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 873 | * occurred, 0 if it must be must be ignored, otherwise the number of read |
| 874 | * bytes. */ |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 875 | static int |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 876 | handle_hanotify(struct spoe_frame *frame) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 877 | { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 878 | struct client *client = frame->client; |
| 879 | char *buf = frame->buf; |
| 880 | char *end = frame->buf + frame->len; |
| 881 | uint64_t stream_id, frame_id; |
| 882 | int i, idx = 0; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 883 | |
| 884 | /* Check frame type */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 885 | if (buf[idx++] != SPOE_FRM_T_HAPROXY_NOTIFY) |
| 886 | goto ignore; |
| 887 | |
| 888 | DEBUG(frame->worker, "<%lu> Decode HAProxy NOTIFY frame", client->id); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 889 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 890 | /* Retrieve flags */ |
| 891 | memcpy((char *)&(frame->flags), buf+idx, 4); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 892 | idx += 4; |
| 893 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 894 | /* Fragmentation is not supported for DISCONNECT frame */ |
| 895 | if (!(frame->flags & SPOE_FRM_FL_FIN) && fragmentation == false) { |
| 896 | client->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
| 897 | goto error; |
| 898 | } |
| 899 | |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 900 | /* Read the stream-id */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 901 | if ((i = decode_spoe_varint(buf+idx, end, &stream_id)) == -1) |
| 902 | goto ignore; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 903 | idx += i; |
| 904 | |
| 905 | /* Read the frame-id */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 906 | if ((i = decode_spoe_varint(buf+idx, end, &frame_id)) == -1) |
| 907 | goto ignore; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 908 | idx += i; |
| 909 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 910 | if (frame->fragmented == true) { |
| 911 | if (frame->stream_id != (unsigned int)stream_id || |
| 912 | frame->frame_id != (unsigned int)frame_id) { |
| 913 | client->status_code = SPOE_FRM_ERR_INTERLACED_FRAMES; |
| 914 | goto error; |
| 915 | } |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 916 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 917 | if (frame->flags & SPOE_FRM_FL_ABRT) { |
| 918 | DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u" |
| 919 | " - Abort processing of a fragmented frame" |
| 920 | " - frag_len=%u - len=%u - offset=%u", |
| 921 | client->id, frame->stream_id, frame->frame_id, |
| 922 | frame->frag_len, frame->len, idx); |
| 923 | goto ignore; |
| 924 | } |
| 925 | DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u" |
| 926 | " - %s fragment of a fragmented frame received" |
| 927 | " - frag_len=%u - len=%u - offset=%u", |
| 928 | client->id, frame->stream_id, frame->frame_id, |
| 929 | (frame->flags & SPOE_FRM_FL_FIN) ? "last" : "next", |
| 930 | frame->frag_len, frame->len, idx); |
| 931 | } |
| 932 | else { |
| 933 | frame->stream_id = (unsigned int)stream_id; |
| 934 | frame->frame_id = (unsigned int)frame_id; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 935 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 936 | DEBUG(frame->worker, "<%lu> STREAM-ID=%u - FRAME-ID=%u" |
| 937 | " - %s frame received" |
| 938 | " - frag_len=%u - len=%u - offset=%u", |
| 939 | client->id, frame->stream_id, frame->frame_id, |
| 940 | (frame->flags & SPOE_FRM_FL_FIN) ? "unfragmented" : "fragmented", |
| 941 | frame->frag_len, frame->len, idx); |
| 942 | |
| 943 | frame->fragmented = !(frame->flags & SPOE_FRM_FL_FIN); |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 944 | } |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 945 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 946 | frame->offset = idx; |
| 947 | return idx; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 948 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 949 | ignore: |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 950 | return 0; |
| 951 | |
| 952 | error: |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 953 | return -1; |
| 954 | } |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 955 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 956 | /* Encode a HELLO frame to send it to HAProxy. It returns the number of written |
| 957 | * bytes. */ |
| 958 | static int |
| 959 | prepare_agenthello(struct spoe_frame *frame) |
| 960 | { |
| 961 | struct client *client = frame->client; |
| 962 | char *buf = frame->buf; |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 963 | char capabilities[64]; |
| 964 | int n, idx = 0; |
| 965 | unsigned int flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 966 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 967 | DEBUG(frame->worker, "<%lu> Encode Agent HELLO frame", client->id); |
| 968 | frame->type = SPOA_FRM_T_AGENT; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 969 | |
| 970 | /* Frame Type */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 971 | buf[idx++] = SPOE_FRM_T_AGENT_HELLO; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 972 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 973 | /* Set flags */ |
| 974 | memcpy(buf+idx, (char *)&flags, 4); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 975 | idx += 4; |
| 976 | |
| 977 | /* No stream-id and frame-id for HELLO frames */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 978 | buf[idx++] = 0; |
| 979 | buf[idx++] = 0; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 980 | |
| 981 | /* "version" K/V item */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 982 | idx += encode_spoe_string("version", 7, buf+idx); |
| 983 | buf[idx++] = SPOE_DATA_T_STR; |
| 984 | idx += encode_spoe_string(SPOP_VERSION, SLEN(SPOP_VERSION), buf+idx); |
| 985 | DEBUG(frame->worker, "<%lu> Agent version : %s", |
| 986 | client->id, SPOP_VERSION); |
| 987 | |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 988 | |
| 989 | /* "max-frame-size" K/V item */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 990 | idx += encode_spoe_string("max-frame-size", 14, buf+idx); |
| 991 | buf[idx++] = SPOE_DATA_T_UINT32; |
| 992 | idx += encode_spoe_varint(client->max_frame_size, buf+idx); |
| 993 | DEBUG(frame->worker, "<%lu> Agent maximum frame size : %u", |
| 994 | client->id, client->max_frame_size); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 995 | |
| 996 | /* "capabilities" K/V item */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 997 | idx += encode_spoe_string("capabilities", 12, buf+idx); |
| 998 | buf[idx++] = SPOE_DATA_T_STR; |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 999 | |
| 1000 | memset(capabilities, 0, sizeof(capabilities)); |
| 1001 | n = 0; |
| 1002 | |
| 1003 | /* 1. Fragmentation capability ? */ |
| 1004 | if (fragmentation == true) { |
| 1005 | memcpy(capabilities, "fragmentation", 13); |
| 1006 | n += 13; |
| 1007 | } |
| 1008 | /* 2. Pipelining capability ? */ |
| 1009 | if (client->pipelining == true && n != 0) { |
| 1010 | memcpy(capabilities + n, ", pipelining", 12); |
| 1011 | n += 12; |
| 1012 | } |
| 1013 | else if (client->pipelining == true) { |
| 1014 | memcpy(capabilities, "pipelining", 10); |
| 1015 | n += 10; |
| 1016 | } |
| 1017 | /* 3. Async capability ? */ |
| 1018 | if (client->async == true && n != 0) { |
| 1019 | memcpy(capabilities + n, ", async", 7); |
| 1020 | n += 7; |
| 1021 | } |
| 1022 | else if (client->async == true) { |
| 1023 | memcpy(capabilities, "async", 5); |
| 1024 | n += 5; |
| 1025 | } |
| 1026 | /* 4. Encode capabilities string */ |
| 1027 | if (n != 0) |
| 1028 | idx += encode_spoe_string(capabilities, n, buf+idx); |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1029 | else |
| 1030 | idx += encode_spoe_string(NULL, 0, buf+idx); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1031 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1032 | DEBUG(frame->worker, "<%lu> Agent capabilities : %.*s", |
| 1033 | client->id, n, capabilities); |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1034 | |
| 1035 | frame->len = idx; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1036 | return idx; |
| 1037 | } |
| 1038 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1039 | /* Encode a DISCONNECT frame to send it to HAProxy. It returns the number of |
| 1040 | * written bytes. */ |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1041 | static int |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1042 | prepare_agentdicon(struct spoe_frame *frame) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1043 | { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1044 | struct client *client = frame->client; |
| 1045 | char *buf = frame->buf; |
| 1046 | const char *reason; |
| 1047 | int rlen, idx = 0; |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1048 | unsigned int flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1049 | |
| 1050 | DEBUG(frame->worker, "<%lu> Encode Agent DISCONNECT frame", client->id); |
| 1051 | frame->type = SPOA_FRM_T_AGENT; |
| 1052 | |
| 1053 | if (client->status_code >= SPOE_FRM_ERRS) |
| 1054 | client->status_code = SPOE_FRM_ERR_UNKNOWN; |
| 1055 | reason = spoe_frm_err_reasons[client->status_code]; |
| 1056 | rlen = strlen(reason); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1057 | |
| 1058 | /* Frame type */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1059 | buf[idx++] = SPOE_FRM_T_AGENT_DISCON; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1060 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1061 | /* Set flags */ |
| 1062 | memcpy(buf+idx, (char *)&flags, 4); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1063 | idx += 4; |
| 1064 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1065 | /* No stream-id and frame-id for DISCONNECT frames */ |
| 1066 | buf[idx++] = 0; |
| 1067 | buf[idx++] = 0; |
| 1068 | |
| 1069 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 1070 | |
| 1071 | /* "status-code" K/V item */ |
| 1072 | idx += encode_spoe_string("status-code", 11, buf+idx); |
| 1073 | buf[idx++] = SPOE_DATA_T_UINT32; |
| 1074 | idx += encode_spoe_varint(client->status_code, buf+idx); |
| 1075 | DEBUG(frame->worker, "<%lu> Disconnect status code : %u", |
| 1076 | client->id, client->status_code); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1077 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1078 | /* "message" K/V item */ |
| 1079 | idx += encode_spoe_string("message", 7, buf+idx); |
| 1080 | buf[idx++] = SPOE_DATA_T_STR; |
| 1081 | idx += encode_spoe_string(reason, rlen, buf+idx); |
| 1082 | DEBUG(frame->worker, "<%lu> Disconnect message : %s", |
| 1083 | client->id, reason); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1084 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1085 | frame->len = idx; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1086 | return idx; |
| 1087 | } |
| 1088 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1089 | /* Encode a ACK frame to send it to HAProxy. It returns the number of written |
| 1090 | * bytes. */ |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1091 | static int |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1092 | prepare_agentack(struct spoe_frame *frame) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1093 | { |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1094 | char *buf = frame->buf; |
| 1095 | int idx = 0; |
| 1096 | unsigned int flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1097 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1098 | /* Be careful here, in async mode, frame->client can be NULL */ |
| 1099 | |
| 1100 | DEBUG(frame->worker, "Encode Agent ACK frame"); |
| 1101 | frame->type = SPOA_FRM_T_AGENT; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1102 | |
| 1103 | /* Frame type */ |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1104 | buf[idx++] = SPOE_FRM_T_AGENT_ACK; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1105 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1106 | /* Set flags */ |
| 1107 | memcpy(buf+idx, (char *)&flags, 4); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1108 | idx += 4; |
| 1109 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1110 | /* Set stream-id and frame-id for ACK frames */ |
| 1111 | idx += encode_spoe_varint(frame->stream_id, buf+idx); |
| 1112 | idx += encode_spoe_varint(frame->frame_id, buf+idx); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1113 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1114 | DEBUG(frame->worker, "STREAM-ID=%u - FRAME-ID=%u", |
| 1115 | frame->stream_id, frame->frame_id); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1116 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1117 | frame->len = idx; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1118 | return idx; |
| 1119 | } |
| 1120 | |
| 1121 | static int |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1122 | create_server_socket(void) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1123 | { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1124 | struct sockaddr_in listen_addr; |
| 1125 | int fd, yes = 1; |
| 1126 | |
| 1127 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 1128 | if (fd < 0) { |
| 1129 | LOG(&null_worker, "Failed to create service socket : %m"); |
| 1130 | return -1; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1131 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1132 | |
| 1133 | memset(&listen_addr, 0, sizeof(listen_addr)); |
| 1134 | listen_addr.sin_family = AF_INET; |
| 1135 | listen_addr.sin_addr.s_addr = INADDR_ANY; |
| 1136 | listen_addr.sin_port = htons(server_port); |
| 1137 | |
| 1138 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0 || |
| 1139 | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) < 0) { |
| 1140 | LOG(&null_worker, "Failed to set option on server socket : %m"); |
| 1141 | return -1; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1142 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1143 | |
| 1144 | if (bind(fd, (struct sockaddr *) &listen_addr, sizeof(listen_addr)) < 0) { |
| 1145 | LOG(&null_worker, "Failed to bind server socket : %m"); |
| 1146 | return -1; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1147 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1148 | |
| 1149 | if (listen(fd, CONNECTION_BACKLOG) < 0) { |
| 1150 | LOG(&null_worker, "Failed to listen on server socket : %m"); |
| 1151 | return -1; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1152 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1153 | |
| 1154 | return fd; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1155 | } |
| 1156 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1157 | static void |
| 1158 | release_frame(struct spoe_frame *frame) |
| 1159 | { |
| 1160 | struct worker *worker; |
| 1161 | |
| 1162 | if (frame == NULL) |
| 1163 | return; |
| 1164 | |
| 1165 | if (event_pending(&frame->process_frame_event, EV_TIMEOUT, NULL)) |
| 1166 | event_del(&frame->process_frame_event); |
| 1167 | |
| 1168 | worker = frame->worker; |
| 1169 | LIST_DEL(&frame->list); |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1170 | if (frame->frag_buf) |
| 1171 | free(frame->frag_buf); |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1172 | memset(frame, 0, sizeof(*frame)+max_frame_size+4); |
| 1173 | LIST_ADDQ(&worker->frames, &frame->list); |
| 1174 | } |
| 1175 | |
| 1176 | static void |
| 1177 | release_client(struct client *c) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1178 | { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1179 | struct spoe_frame *frame, *back; |
| 1180 | |
| 1181 | if (c == NULL) |
| 1182 | return; |
| 1183 | |
| 1184 | DEBUG(c->worker, "<%lu> Release client", c->id); |
| 1185 | |
| 1186 | LIST_DEL(&c->by_worker); |
| 1187 | c->worker->nbclients--; |
| 1188 | |
| 1189 | unuse_spoe_engine(c); |
| 1190 | free(c->engine_id); |
| 1191 | |
| 1192 | if (event_pending(&c->read_frame_event, EV_READ, NULL)) |
| 1193 | event_del(&c->read_frame_event); |
| 1194 | if (event_pending(&c->write_frame_event, EV_WRITE, NULL)) |
| 1195 | event_del(&c->write_frame_event); |
| 1196 | |
| 1197 | release_frame(c->incoming_frame); |
| 1198 | release_frame(c->outgoing_frame); |
| 1199 | list_for_each_entry_safe(frame, back, &c->processing_frames, list) { |
| 1200 | release_frame(frame); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1201 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1202 | list_for_each_entry_safe(frame, back, &c->outgoing_frames, list) { |
| 1203 | release_frame(frame); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1204 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1205 | |
| 1206 | if (c->fd >= 0) |
| 1207 | close(c->fd); |
| 1208 | |
| 1209 | free(c); |
| 1210 | } |
| 1211 | |
| 1212 | static void |
| 1213 | reset_frame(struct spoe_frame *frame) |
| 1214 | { |
| 1215 | if (frame == NULL) |
| 1216 | return; |
| 1217 | |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1218 | if (frame->frag_buf) |
| 1219 | free(frame->frag_buf); |
| 1220 | |
| 1221 | frame->type = SPOA_FRM_T_UNKNOWN; |
| 1222 | frame->buf = (char *)(frame->data); |
| 1223 | frame->offset = 0; |
| 1224 | frame->len = 0; |
| 1225 | frame->stream_id = 0; |
| 1226 | frame->frame_id = 0; |
| 1227 | frame->flags = 0; |
| 1228 | frame->hcheck = false; |
| 1229 | frame->fragmented = false; |
| 1230 | frame->ip_score = -1; |
| 1231 | frame->frag_buf = NULL; |
| 1232 | frame->frag_len = 0; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1233 | LIST_INIT(&frame->list); |
| 1234 | } |
| 1235 | |
| 1236 | static void |
| 1237 | use_spoe_engine(struct client *client) |
| 1238 | { |
| 1239 | struct spoe_engine *eng; |
| 1240 | |
| 1241 | if (client->engine_id == NULL) |
| 1242 | return; |
| 1243 | |
| 1244 | list_for_each_entry(eng, &client->worker->engines, list) { |
| 1245 | if (!strcmp(eng->id, client->engine_id)) |
| 1246 | goto end; |
| 1247 | } |
| 1248 | |
| 1249 | if ((eng = malloc(sizeof(*eng))) == NULL) { |
| 1250 | client->async = false; |
| 1251 | return; |
| 1252 | } |
| 1253 | |
| 1254 | eng->id = strdup(client->engine_id); |
| 1255 | LIST_INIT(&eng->clients); |
| 1256 | LIST_INIT(&eng->processing_frames); |
| 1257 | LIST_INIT(&eng->outgoing_frames); |
| 1258 | LIST_ADDQ(&client->worker->engines, &eng->list); |
| 1259 | LOG(client->worker, "Add new SPOE engine '%s'", eng->id); |
| 1260 | |
| 1261 | end: |
| 1262 | client->engine = eng; |
| 1263 | LIST_ADDQ(&eng->clients, &client->by_engine); |
| 1264 | } |
| 1265 | |
| 1266 | static void |
| 1267 | unuse_spoe_engine(struct client *client) |
| 1268 | { |
| 1269 | struct spoe_engine *eng; |
| 1270 | struct spoe_frame *frame, *back; |
| 1271 | |
| 1272 | if (client == NULL || client->engine == NULL) |
| 1273 | return; |
| 1274 | |
| 1275 | eng = client->engine; |
| 1276 | client->engine = NULL; |
| 1277 | LIST_DEL(&client->by_engine); |
| 1278 | if (!LIST_ISEMPTY(&eng->clients)) |
| 1279 | return; |
| 1280 | |
| 1281 | LOG(client->worker, "Remove SPOE engine '%s'", eng->id); |
| 1282 | LIST_DEL(&eng->list); |
| 1283 | |
| 1284 | list_for_each_entry_safe(frame, back, &eng->processing_frames, list) { |
| 1285 | release_frame(frame); |
| 1286 | } |
| 1287 | list_for_each_entry_safe(frame, back, &eng->outgoing_frames, list) { |
| 1288 | release_frame(frame); |
| 1289 | } |
| 1290 | free(eng->id); |
| 1291 | free(eng); |
| 1292 | } |
| 1293 | |
| 1294 | |
| 1295 | static struct spoe_frame * |
| 1296 | acquire_incoming_frame(struct client *client) |
| 1297 | { |
| 1298 | struct spoe_frame *frame; |
| 1299 | |
| 1300 | frame = client->incoming_frame; |
| 1301 | if (frame != NULL) |
| 1302 | return frame; |
| 1303 | |
| 1304 | if (LIST_ISEMPTY(&client->worker->frames)) { |
| 1305 | if ((frame = calloc(1, sizeof(*frame)+max_frame_size+4)) == NULL) { |
| 1306 | LOG(client->worker, "Failed to allocate new frame : %m"); |
| 1307 | return NULL; |
| 1308 | } |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1309 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1310 | else { |
| 1311 | frame = LIST_NEXT(&client->worker->frames, typeof(frame), list); |
| 1312 | LIST_DEL(&frame->list); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1313 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1314 | |
| 1315 | reset_frame(frame); |
| 1316 | frame->worker = client->worker; |
| 1317 | frame->engine = client->engine; |
| 1318 | frame->client = client; |
| 1319 | |
| 1320 | if (event_assign(&frame->process_frame_event, client->worker->base, -1, |
| 1321 | EV_TIMEOUT|EV_PERSIST, process_frame_cb, frame) < 0) { |
| 1322 | LOG(client->worker, "Failed to create frame event"); |
| 1323 | return NULL; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1324 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1325 | |
| 1326 | client->incoming_frame = frame; |
| 1327 | return frame; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1328 | } |
| 1329 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1330 | static struct spoe_frame * |
| 1331 | acquire_outgoing_frame(struct client *client) |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1332 | { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1333 | struct spoe_engine *engine = client->engine; |
| 1334 | struct spoe_frame *frame = NULL; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1335 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1336 | if (client->outgoing_frame != NULL) |
| 1337 | frame = client->outgoing_frame; |
| 1338 | else if (!LIST_ISEMPTY(&client->outgoing_frames)) { |
| 1339 | frame = LIST_NEXT(&client->outgoing_frames, typeof(frame), list); |
| 1340 | LIST_DEL(&frame->list); |
| 1341 | client->outgoing_frame = frame; |
| 1342 | } |
| 1343 | else if (engine!= NULL && !LIST_ISEMPTY(&engine->outgoing_frames)) { |
| 1344 | frame = LIST_NEXT(&engine->outgoing_frames, typeof(frame), list); |
| 1345 | LIST_DEL(&frame->list); |
| 1346 | client->outgoing_frame = frame; |
| 1347 | } |
| 1348 | return frame; |
| 1349 | } |
| 1350 | |
| 1351 | static void |
| 1352 | write_frame(struct client *client, struct spoe_frame *frame) |
| 1353 | { |
| 1354 | uint32_t netint; |
| 1355 | |
| 1356 | LIST_DEL(&frame->list); |
| 1357 | |
| 1358 | frame->buf = (char *)(frame->data); |
| 1359 | frame->offset = 0; |
| 1360 | netint = htonl(frame->len); |
| 1361 | memcpy(frame->buf, &netint, 4); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1362 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1363 | if (client != NULL) { /* HELLO or DISCONNECT frames */ |
| 1364 | event_add(&client->write_frame_event, NULL); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1365 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1366 | /* Try to process the frame as soon as possible, and always |
| 1367 | * attach it to the client */ |
| 1368 | if (client->async || client->pipelining) { |
| 1369 | if (client->outgoing_frame == NULL) |
| 1370 | client->outgoing_frame = frame; |
| 1371 | else |
| 1372 | LIST_ADD(&client->outgoing_frames, &frame->list); |
| 1373 | } |
| 1374 | else { |
| 1375 | client->outgoing_frame = frame; |
| 1376 | event_del(&client->read_frame_event); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1377 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1378 | } |
| 1379 | else { /* for all other frames */ |
| 1380 | if (frame->client == NULL) { /* async mode ! */ |
| 1381 | LIST_ADDQ(&frame->engine->outgoing_frames, &frame->list); |
| 1382 | list_for_each_entry(client, &frame->engine->clients, by_engine) |
| 1383 | event_add(&client->write_frame_event, NULL); |
| 1384 | } |
| 1385 | else if (frame->client->pipelining) { |
| 1386 | LIST_ADDQ(&frame->client->outgoing_frames, &frame->list); |
| 1387 | event_add(&frame->client->write_frame_event, NULL); |
| 1388 | } |
| 1389 | else { |
| 1390 | frame->client->outgoing_frame = frame; |
| 1391 | event_add(&frame->client->write_frame_event, NULL); |
| 1392 | event_del(&frame->client->read_frame_event); |
| 1393 | } |
| 1394 | } |
| 1395 | } |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1396 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1397 | static void |
| 1398 | process_incoming_frame(struct spoe_frame *frame) |
| 1399 | { |
| 1400 | struct client *client = frame->client; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1401 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1402 | if (event_add(&frame->process_frame_event, &processing_delay) < 0) { |
| 1403 | LOG(client->worker, "Failed to process incoming frame"); |
| 1404 | release_frame(frame); |
| 1405 | return; |
| 1406 | } |
| 1407 | |
| 1408 | if (client->async) { |
| 1409 | frame->client = NULL; |
| 1410 | LIST_ADDQ(&frame->engine->processing_frames, &frame->list); |
| 1411 | } |
| 1412 | else if (client->pipelining) |
| 1413 | LIST_ADDQ(&client->processing_frames, &frame->list); |
| 1414 | else |
| 1415 | event_del(&client->read_frame_event); |
| 1416 | } |
| 1417 | |
| 1418 | static void |
| 1419 | signal_cb(evutil_socket_t sig, short events, void *user_data) |
| 1420 | { |
| 1421 | struct event_base *base = user_data; |
| 1422 | int i; |
| 1423 | |
| 1424 | DEBUG(&null_worker, "Stopping the server"); |
| 1425 | |
| 1426 | event_base_loopbreak(base); |
| 1427 | DEBUG(&null_worker, "Main event loop stopped"); |
| 1428 | |
| 1429 | for (i = 0; i < num_workers; i++) { |
| 1430 | event_base_loopbreak(workers[i].base); |
| 1431 | DEBUG(&null_worker, "Event loop stopped for worker %02d", |
| 1432 | workers[i].id); |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | static void |
| 1437 | worker_monitor_cb(evutil_socket_t fd, short events, void *arg) |
| 1438 | { |
| 1439 | struct worker *worker = arg; |
| 1440 | |
| 1441 | LOG(worker, "%u clients connected", worker->nbclients); |
| 1442 | } |
| 1443 | |
| 1444 | static void |
| 1445 | process_frame_cb(evutil_socket_t fd, short events, void *arg) |
| 1446 | { |
| 1447 | struct spoe_frame *frame = arg; |
| 1448 | char *buf = frame->buf; |
| 1449 | char *end = frame->buf + frame->len; |
| 1450 | int idx = frame->offset; |
| 1451 | |
| 1452 | DEBUG(frame->worker, |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1453 | "Process frame messages : STREAM-ID=%u - FRAME-ID=%u - length=%u bytes", |
| 1454 | frame->stream_id, frame->frame_id, frame->len - frame->offset); |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1455 | |
| 1456 | /* Loop on messages */ |
| 1457 | while (buf+idx < end) { |
| 1458 | char *str; |
| 1459 | uint64_t sz; |
| 1460 | int nbargs, i; |
| 1461 | |
| 1462 | /* Decode the message name */ |
| 1463 | idx += decode_spoe_string(buf+idx, end, &str, &sz); |
| 1464 | if (str == NULL) |
| 1465 | goto stop_processing; |
| 1466 | |
| 1467 | DEBUG(frame->worker, "Process SPOE Message '%.*s'", (int)sz, str); |
| 1468 | |
| 1469 | nbargs = buf[idx++]; /* Get the number of arguments */ |
| 1470 | frame->offset = idx; /* Save index to handle errors and skip args */ |
| 1471 | if (!memcmp(str, "check-client-ip", sz)) { |
| 1472 | struct spoe_data data; |
| 1473 | |
| 1474 | memset(&data, 0, sizeof(data)); |
| 1475 | |
| 1476 | if (nbargs != 1) |
| 1477 | goto skip_message; |
| 1478 | |
| 1479 | if ((i = decode_spoe_string(buf+idx, end, &str, &sz)) == -1) |
| 1480 | goto stop_processing; |
| 1481 | idx += i; |
| 1482 | |
| 1483 | if ((i = decode_spoe_data(buf+idx, end, &data)) == -1) |
| 1484 | goto skip_message; |
| 1485 | idx += i; |
| 1486 | |
| 1487 | if ((data.type & SPOE_DATA_T_MASK) == SPOE_DATA_T_IPV4) |
| 1488 | check_ipv4_reputation(frame, &data.u.ipv4); |
| 1489 | else if ((data.type & SPOE_DATA_T_MASK) == SPOE_DATA_T_IPV6) |
| 1490 | check_ipv6_reputation(frame, &data.u.ipv6); |
| 1491 | } |
| 1492 | else { |
| 1493 | skip_message: |
| 1494 | idx = frame->offset; /* Restore index */ |
| 1495 | |
| 1496 | while (nbargs-- > 0) { |
| 1497 | /* Silently ignore argument: its name and its value */ |
| 1498 | if ((i = decode_spoe_string(buf+idx, end, &str, &sz)) == -1) |
| 1499 | goto stop_processing; |
| 1500 | idx += i; |
| 1501 | if ((i = skip_spoe_data(buf+idx, end)) == -1) |
| 1502 | goto stop_processing; |
| 1503 | idx += i; |
| 1504 | } |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | stop_processing: |
| 1509 | /* Prepare agent ACK frame */ |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1510 | frame->buf = (char *)(frame->data) + 4; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1511 | frame->offset = 0; |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1512 | frame->len = 0; |
| 1513 | frame->flags = 0; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1514 | idx = prepare_agentack(frame); |
| 1515 | |
| 1516 | if (frame->ip_score != -1) { |
| 1517 | DEBUG(frame->worker, "Add action : set variable ip_scode=%u", |
| 1518 | frame->ip_score); |
| 1519 | |
| 1520 | buf[idx++] = SPOE_ACT_T_SET_VAR; /* Action type */ |
| 1521 | buf[idx++] = 3; /* Number of args */ |
| 1522 | buf[idx++] = SPOE_SCOPE_SESS; /* Arg 1: the scope */ |
| 1523 | idx += encode_spoe_string("ip_score", 8, buf+idx); /* Arg 2: variable name */ |
| 1524 | buf[idx++] = SPOE_DATA_T_UINT32; |
| 1525 | idx += encode_spoe_varint(frame->ip_score, buf+idx); /* Arg 3: variable value */ |
| 1526 | frame->len = idx; |
| 1527 | } |
| 1528 | write_frame(NULL, frame); |
| 1529 | } |
| 1530 | |
| 1531 | static void |
| 1532 | read_frame_cb(evutil_socket_t fd, short events, void *arg) |
| 1533 | { |
| 1534 | struct client *client = arg; |
| 1535 | struct spoe_frame *frame; |
| 1536 | uint32_t netint; |
| 1537 | int n; |
| 1538 | |
| 1539 | DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__); |
| 1540 | if ((frame = acquire_incoming_frame(client)) == NULL) |
| 1541 | goto close; |
| 1542 | |
| 1543 | frame->type = SPOA_FRM_T_HAPROXY; |
| 1544 | if (frame->buf == (char *)(frame->data)) { |
| 1545 | /* Read the frame length: frame->buf points on length part (frame->data) */ |
| 1546 | n = read(client->fd, frame->buf+frame->offset, 4-frame->offset); |
| 1547 | if (n <= 0) { |
| 1548 | if (n < 0) |
| 1549 | LOG(client->worker, "Failed to read frame length : %m"); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1550 | goto close; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1551 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1552 | frame->offset += n; |
| 1553 | if (frame->offset != 4) |
| 1554 | return; |
| 1555 | memcpy(&netint, frame->buf, 4); |
| 1556 | frame->buf += 4; |
| 1557 | frame->offset = 0; |
| 1558 | frame->len = ntohl(netint); |
| 1559 | } |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1560 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1561 | /* Read the frame: frame->buf points on frame part (frame->data+4)*/ |
| 1562 | n = read(client->fd, frame->buf + frame->offset, |
| 1563 | frame->len - frame->offset); |
| 1564 | if (n <= 0) { |
| 1565 | if (n < 0) { |
| 1566 | LOG(client->worker, "Frame to read frame : %m"); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1567 | goto close; |
| 1568 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1569 | return; |
| 1570 | } |
| 1571 | frame->offset += n; |
| 1572 | if (frame->offset != frame->len) |
| 1573 | return; |
| 1574 | frame->offset = 0; |
| 1575 | |
| 1576 | DEBUG(client->worker, "<%lu> New Frame of %u bytes received", |
| 1577 | client->id, frame->len); |
| 1578 | |
| 1579 | switch (client->state) { |
| 1580 | case SPOA_ST_CONNECTING: |
| 1581 | if (handle_hahello(frame) < 0) { |
| 1582 | LOG(client->worker, "Failed to decode HELLO frame"); |
| 1583 | goto disconnect; |
| 1584 | } |
| 1585 | prepare_agenthello(frame); |
| 1586 | goto write_frame; |
| 1587 | |
| 1588 | case SPOA_ST_PROCESSING: |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1589 | if (frame->buf[0] == SPOE_FRM_T_HAPROXY_DISCON) { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1590 | client->state = SPOA_ST_DISCONNECTING; |
| 1591 | goto disconnecting; |
| 1592 | } |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1593 | n = handle_hanotify(frame); |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1594 | if (n < 0) { |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1595 | LOG(client->worker, "Failed to decode frame: %s", |
| 1596 | spoe_frm_err_reasons[client->status_code]); |
| 1597 | goto disconnect; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1598 | } |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1599 | else if (n == 0) { |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1600 | LOG(client->worker, "Ignore invalid/unknown/aborted frame"); |
| 1601 | goto ignore_frame; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1602 | } |
| 1603 | else |
| 1604 | goto process_frame; |
| 1605 | |
| 1606 | case SPOA_ST_DISCONNECTING: |
| 1607 | disconnecting: |
| 1608 | if (handle_hadiscon(frame) < 0) { |
| 1609 | LOG(client->worker, "Failed to decode DISCONNECT frame"); |
| 1610 | goto disconnect; |
| 1611 | } |
| 1612 | if (client->status_code != SPOE_FRM_ERR_NONE) |
| 1613 | LOG(client->worker, "<%lu> Peer closed connection: %s", |
| 1614 | client->id, spoe_frm_err_reasons[client->status_code]); |
| 1615 | client->status_code = SPOE_FRM_ERR_NONE; |
| 1616 | goto disconnect; |
| 1617 | } |
| 1618 | |
| 1619 | ignore_frame: |
| 1620 | reset_frame(frame); |
| 1621 | return; |
| 1622 | |
| 1623 | process_frame: |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1624 | if (frame->fragmented == true) { |
| 1625 | char *buf; |
| 1626 | size_t len = frame->len - frame->offset; |
| 1627 | |
| 1628 | buf = realloc(frame->frag_buf, frame->frag_len + len); |
| 1629 | if (buf == NULL) { |
| 1630 | client->status_code = SPOE_FRM_ERR_RES; |
| 1631 | goto disconnect; |
| 1632 | } |
| 1633 | memcpy(buf + frame->frag_len, frame->buf + frame->offset, len); |
| 1634 | frame->frag_buf = buf; |
| 1635 | frame->frag_len += len; |
| 1636 | |
| 1637 | if (!(frame->flags & SPOE_FRM_FL_FIN)) { |
| 1638 | /* Wait for next fragments */ |
| 1639 | frame->buf = (char *)(frame->data); |
| 1640 | frame->offset = 0; |
| 1641 | frame->len = 0; |
| 1642 | frame->flags = 0; |
| 1643 | return; |
| 1644 | } |
| 1645 | |
| 1646 | frame->buf = frame->frag_buf; |
| 1647 | frame->len = frame->frag_len; |
| 1648 | frame->offset = 0; |
| 1649 | /* fall through */ |
| 1650 | } |
| 1651 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1652 | process_incoming_frame(frame); |
| 1653 | client->incoming_frame = NULL; |
| 1654 | return; |
| 1655 | |
| 1656 | write_frame: |
| 1657 | write_frame(client, frame); |
| 1658 | client->incoming_frame = NULL; |
| 1659 | return; |
| 1660 | |
| 1661 | disconnect: |
| 1662 | client->state = SPOA_ST_DISCONNECTING; |
| 1663 | if (prepare_agentdicon(frame) < 0) { |
| 1664 | LOG(client->worker, "Failed to encode DISCONNECT frame"); |
| 1665 | goto close; |
| 1666 | } |
| 1667 | goto write_frame; |
| 1668 | |
| 1669 | close: |
| 1670 | release_client(client); |
| 1671 | } |
| 1672 | |
| 1673 | static void |
| 1674 | write_frame_cb(evutil_socket_t fd, short events, void *arg) |
| 1675 | { |
| 1676 | struct client *client = arg; |
| 1677 | struct spoe_frame *frame; |
| 1678 | int n; |
| 1679 | |
| 1680 | DEBUG(client->worker, "<%lu> %s", client->id, __FUNCTION__); |
| 1681 | if ((frame = acquire_outgoing_frame(client)) == NULL) { |
| 1682 | event_del(&client->write_frame_event); |
| 1683 | return; |
| 1684 | } |
| 1685 | |
| 1686 | if (frame->buf == (char *)(frame->data)) { |
| 1687 | /* Write the frame length: frame->buf points on length part (frame->data) */ |
| 1688 | n = write(client->fd, frame->buf+frame->offset, 4-frame->offset); |
| 1689 | if (n <= 0) { |
| 1690 | if (n < 0) |
| 1691 | LOG(client->worker, "Failed to write frame length : %m"); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1692 | goto close; |
| 1693 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1694 | frame->offset += n; |
| 1695 | if (frame->offset != 4) |
| 1696 | return; |
| 1697 | frame->buf += 4; |
| 1698 | frame->offset = 0; |
| 1699 | } |
| 1700 | |
| 1701 | /* Write the frame: frame->buf points on frame part (frame->data+4)*/ |
| 1702 | n = write(client->fd, frame->buf + frame->offset, |
| 1703 | frame->len - frame->offset); |
| 1704 | if (n <= 0) { |
| 1705 | if (n < 0) { |
| 1706 | LOG(client->worker, "Failed to write frame : %m"); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1707 | goto close; |
| 1708 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1709 | return; |
| 1710 | } |
| 1711 | frame->offset += n; |
| 1712 | if (frame->offset != frame->len) |
| 1713 | return; |
| 1714 | |
| 1715 | DEBUG(client->worker, "<%lu> Frame of %u bytes send", |
| 1716 | client->id, frame->len); |
| 1717 | |
| 1718 | switch (client->state) { |
| 1719 | case SPOA_ST_CONNECTING: |
| 1720 | if (frame->hcheck == true) { |
| 1721 | DEBUG(client->worker, |
| 1722 | "<%lu> Close client after healthcheck", |
| 1723 | client->id); |
| 1724 | goto close; |
| 1725 | } |
| 1726 | client->state = SPOA_ST_PROCESSING; |
| 1727 | break; |
| 1728 | |
| 1729 | case SPOA_ST_PROCESSING: |
| 1730 | break; |
| 1731 | |
| 1732 | case SPOA_ST_DISCONNECTING: |
| 1733 | goto close; |
| 1734 | } |
| 1735 | |
| 1736 | release_frame(frame); |
| 1737 | client->outgoing_frame = NULL; |
| 1738 | if (!client->async && !client->pipelining) { |
| 1739 | event_del(&client->write_frame_event); |
| 1740 | event_add(&client->read_frame_event, NULL); |
| 1741 | } |
| 1742 | return; |
| 1743 | |
| 1744 | close: |
| 1745 | release_client(client); |
| 1746 | } |
| 1747 | |
| 1748 | static void |
| 1749 | accept_cb(int listener, short event, void *arg) |
| 1750 | { |
| 1751 | struct worker *worker; |
| 1752 | struct client *client; |
| 1753 | int fd; |
| 1754 | |
| 1755 | worker = &workers[clicount++ % num_workers]; |
| 1756 | |
| 1757 | if ((fd = accept(listener, NULL, NULL)) < 0) { |
| 1758 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
| 1759 | LOG(worker, "Failed to accept client connection : %m"); |
| 1760 | return; |
| 1761 | } |
| 1762 | |
| 1763 | DEBUG(&null_worker, |
| 1764 | "<%lu> New Client connection accepted and assigned to worker %02d", |
| 1765 | clicount, worker->id); |
| 1766 | |
| 1767 | if (evutil_make_socket_nonblocking(fd) < 0) { |
| 1768 | LOG(&null_worker, "Failed to set client socket to non-blocking : %m"); |
| 1769 | close(fd); |
| 1770 | return; |
| 1771 | } |
| 1772 | |
| 1773 | if ((client = calloc(1, sizeof(*client))) == NULL) { |
| 1774 | LOG(&null_worker, "Failed to allocate memory for client state : %m"); |
| 1775 | close(fd); |
| 1776 | return; |
| 1777 | } |
| 1778 | |
| 1779 | client->id = clicount; |
| 1780 | client->fd = fd; |
| 1781 | client->worker = worker; |
| 1782 | client->state = SPOA_ST_CONNECTING; |
| 1783 | client->status_code = SPOE_FRM_ERR_NONE; |
| 1784 | client->max_frame_size = max_frame_size; |
| 1785 | client->engine = NULL; |
| 1786 | client->pipelining = false; |
| 1787 | client->async = false; |
| 1788 | client->incoming_frame = NULL; |
| 1789 | client->outgoing_frame = NULL; |
| 1790 | LIST_INIT(&client->processing_frames); |
| 1791 | LIST_INIT(&client->outgoing_frames); |
| 1792 | |
| 1793 | LIST_ADDQ(&worker->clients, &client->by_worker); |
| 1794 | |
| 1795 | worker->nbclients++; |
| 1796 | |
| 1797 | if (event_assign(&client->read_frame_event, worker->base, fd, |
| 1798 | EV_READ|EV_PERSIST, read_frame_cb, client) < 0 || |
| 1799 | event_assign(&client->write_frame_event, worker->base, fd, |
| 1800 | EV_WRITE|EV_PERSIST, write_frame_cb, client) < 0) { |
| 1801 | LOG(&null_worker, "Failed to create client events"); |
| 1802 | release_client(client); |
| 1803 | return; |
| 1804 | } |
| 1805 | event_add(&client->read_frame_event, NULL); |
| 1806 | } |
| 1807 | |
| 1808 | static void * |
| 1809 | worker_function(void *data) |
| 1810 | { |
| 1811 | struct client *client, *cback; |
| 1812 | struct spoe_frame *frame, *fback; |
| 1813 | struct worker *worker = data; |
| 1814 | |
| 1815 | DEBUG(worker, "Worker ready to process client messages"); |
| 1816 | event_base_dispatch(worker->base); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1817 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1818 | list_for_each_entry_safe(client, cback, &worker->clients, by_worker) { |
| 1819 | release_client(client); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1820 | } |
| 1821 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1822 | list_for_each_entry_safe(frame, fback, &worker->frames, list) { |
| 1823 | LIST_DEL(&frame->list); |
| 1824 | free(frame); |
| 1825 | } |
| 1826 | |
| 1827 | event_free(worker->monitor_event); |
| 1828 | event_base_free(worker->base); |
| 1829 | DEBUG(worker, "Worker is stopped"); |
| 1830 | pthread_exit(&null_worker); |
| 1831 | } |
| 1832 | |
| 1833 | |
| 1834 | static int |
| 1835 | parse_processing_delay(const char *str) |
| 1836 | { |
| 1837 | unsigned long value; |
| 1838 | |
| 1839 | value = 0; |
| 1840 | while (1) { |
| 1841 | unsigned int j; |
| 1842 | |
| 1843 | j = *str - '0'; |
| 1844 | if (j > 9) |
| 1845 | break; |
| 1846 | str++; |
| 1847 | value *= 10; |
| 1848 | value += j; |
| 1849 | } |
| 1850 | |
| 1851 | switch (*str) { |
| 1852 | case '\0': /* no unit = millisecond */ |
| 1853 | value *= 1000; |
| 1854 | break; |
| 1855 | case 's': /* second */ |
| 1856 | value *= 1000000; |
| 1857 | str++; |
| 1858 | break; |
| 1859 | case 'm': /* millisecond : "ms" */ |
| 1860 | if (str[1] != 's') |
| 1861 | return -1; |
| 1862 | value *= 1000; |
| 1863 | str += 2; |
| 1864 | break; |
| 1865 | case 'u': /* microsecond : "us" */ |
| 1866 | if (str[1] != 's') |
| 1867 | return -1; |
| 1868 | str += 2; |
| 1869 | break; |
| 1870 | default: |
| 1871 | return -1; |
| 1872 | } |
| 1873 | if (*str) |
| 1874 | return -1; |
| 1875 | |
| 1876 | processing_delay.tv_sec = (time_t)(value / 1000000); |
| 1877 | processing_delay.tv_usec = (suseconds_t)(value % 1000000); |
| 1878 | return 0; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1879 | } |
| 1880 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1881 | |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1882 | static void |
| 1883 | usage(char *prog) |
| 1884 | { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1885 | fprintf(stderr, |
| 1886 | "Usage : %s [OPTION]...\n" |
| 1887 | " -h Print this message\n" |
| 1888 | " -d Enable the debug mode\n" |
| 1889 | " -m <max-frame-size> Specify the maximum frame size (default : %u)\n" |
| 1890 | " -p <port> Specify the port to listen on (default : %d)\n" |
| 1891 | " -n <num-workers> Specify the number of workers (default : %d)\n" |
| 1892 | " -c <capability> Enable the support of the specified capability\n" |
| 1893 | " -t <time> Set a delay to process a message (default: 0)\n" |
| 1894 | " The value is specified in milliseconds by default,\n" |
| 1895 | " but can be in any other unit if the number is suffixed\n" |
| 1896 | " by a unit (us, ms, s)\n" |
| 1897 | "\n" |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1898 | " Supported capabilities: fragmentation, pipelining, async\n", |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1899 | prog, MAX_FRAME_SIZE, DEFAULT_PORT, NUM_WORKERS); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | int |
| 1903 | main(int argc, char **argv) |
| 1904 | { |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1905 | struct event_base *base = NULL; |
| 1906 | struct event *signal_event = NULL, *accept_event = NULL; |
| 1907 | int opt, i, fd = -1; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1908 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1909 | // TODO: add '-t <processing-time>' option |
| 1910 | while ((opt = getopt(argc, argv, "hdm:n:p:c:t:")) != -1) { |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1911 | switch (opt) { |
| 1912 | case 'h': |
| 1913 | usage(argv[0]); |
| 1914 | return EXIT_SUCCESS; |
| 1915 | case 'd': |
| 1916 | debug = true; |
| 1917 | break; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1918 | case 'm': |
| 1919 | max_frame_size = atoi(optarg); |
| 1920 | break; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1921 | case 'n': |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1922 | num_workers = atoi(optarg); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1923 | break; |
| 1924 | case 'p': |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1925 | server_port = atoi(optarg); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1926 | break; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1927 | case 'c': |
| 1928 | if (!strcmp(optarg, "pipelining")) |
| 1929 | pipelining = true; |
| 1930 | else if (!strcmp(optarg, "async")) |
| 1931 | async = true; |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 1932 | else if (!strcmp(optarg, "fragmentation")) |
| 1933 | fragmentation = true; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1934 | else |
| 1935 | fprintf(stderr, "WARNING: unsupported capability '%s'\n", optarg); |
| 1936 | break; |
| 1937 | case 't': |
| 1938 | if (!parse_processing_delay(optarg)) |
| 1939 | break; |
| 1940 | fprintf(stderr, "%s: failed to parse time '%s'.\n", argv[0], optarg); |
| 1941 | fprintf(stderr, "Try '%s -h' for more information.\n", argv[0]); |
| 1942 | return EXIT_FAILURE; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1943 | default: |
| 1944 | usage(argv[0]); |
| 1945 | return EXIT_FAILURE; |
| 1946 | } |
| 1947 | } |
| 1948 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1949 | if (num_workers <= 0) { |
| 1950 | LOG(&null_worker, "%s : Invalid number of workers '%d'\n", |
| 1951 | argv[0], num_workers); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1952 | goto error; |
| 1953 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1954 | |
| 1955 | if (server_port <= 0) { |
| 1956 | LOG(&null_worker, "%s : Invalid port '%d'\n", |
| 1957 | argv[0], server_port); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1958 | goto error; |
| 1959 | } |
| 1960 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1961 | |
| 1962 | if (evthread_use_pthreads() < 0) { |
| 1963 | LOG(&null_worker, "No pthreads support for libevent"); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1964 | goto error; |
| 1965 | } |
| 1966 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1967 | if ((base = event_base_new()) == NULL) { |
| 1968 | LOG(&null_worker, "Failed to initialize libevent : %m"); |
| 1969 | goto error; |
| 1970 | } |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1971 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1972 | signal(SIGPIPE, SIG_IGN); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1973 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1974 | if ((fd = create_server_socket()) < 0) { |
| 1975 | LOG(&null_worker, "Failed to create server socket"); |
| 1976 | goto error; |
| 1977 | } |
| 1978 | if (evutil_make_socket_nonblocking(fd) < 0) { |
| 1979 | LOG(&null_worker, "Failed to set server socket to non-blocking"); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1980 | goto error; |
| 1981 | } |
| 1982 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1983 | if ((workers = calloc(num_workers, sizeof(*workers))) == NULL) { |
| 1984 | LOG(&null_worker, "Failed to set allocate memory for workers"); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1985 | goto error; |
| 1986 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1987 | |
| 1988 | for (i = 0; i < num_workers; ++i) { |
| 1989 | struct worker *w = &workers[i]; |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 1990 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 1991 | w->id = i+1; |
| 1992 | w->nbclients = 0; |
| 1993 | LIST_INIT(&w->engines); |
| 1994 | LIST_INIT(&w->clients); |
| 1995 | LIST_INIT(&w->frames); |
| 1996 | |
| 1997 | if ((w->base = event_base_new()) == NULL) { |
| 1998 | LOG(&null_worker, |
| 1999 | "Failed to initialize libevent for worker %02d : %m", |
| 2000 | w->id); |
| 2001 | goto error; |
| 2002 | } |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 2003 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 2004 | w->monitor_event = event_new(w->base, fd, EV_PERSIST, |
| 2005 | worker_monitor_cb, (void *)w); |
| 2006 | if (w->monitor_event == NULL || |
| 2007 | event_add(w->monitor_event, (struct timeval[]){{5,0}}) < 0) { |
| 2008 | LOG(&null_worker, |
| 2009 | "Failed to create monitor event for worker %02d", |
| 2010 | w->id); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 2011 | goto error; |
| 2012 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 2013 | |
| 2014 | if (pthread_create(&w->thread, NULL, worker_function, (void *)w)) { |
| 2015 | LOG(&null_worker, |
| 2016 | "Failed to start thread for worker %02d : %m", |
| 2017 | w->id); |
| 2018 | } |
| 2019 | DEBUG(&null_worker, "Worker %02d initialized", w->id); |
| 2020 | } |
| 2021 | |
| 2022 | accept_event = event_new(base, fd, EV_READ|EV_PERSIST, accept_cb, |
| 2023 | (void *)base); |
| 2024 | if (accept_event == NULL || event_add(accept_event, NULL) < 0) { |
| 2025 | LOG(&null_worker, "Failed to create accept event : %m"); |
| 2026 | } |
| 2027 | |
| 2028 | signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base); |
| 2029 | if (signal_event == NULL || event_add(signal_event, NULL) < 0) { |
| 2030 | LOG(&null_worker, "Failed to create signal event : %m"); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 2031 | } |
| 2032 | |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 2033 | DEBUG(&null_worker, |
| 2034 | "Server is ready" |
Christopher Faulet | 8501035 | 2017-02-02 10:14:36 +0100 | [diff] [blame] | 2035 | " [fragmentation=%s - pipelining=%s - async=%s - debug=%s - max-frame-size=%u]", |
| 2036 | (fragmentation?"true":"false"), (pipelining?"true":"false"), (async?"true":"false"), |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 2037 | (debug?"true":"false"), max_frame_size); |
| 2038 | event_base_dispatch(base); |
| 2039 | |
| 2040 | for (i = 0; i < num_workers; i++) { |
| 2041 | struct worker *w = &workers[i]; |
| 2042 | |
| 2043 | pthread_join(w->thread, NULL); |
| 2044 | DEBUG(&null_worker, "Worker %02d terminated", w->id); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 2045 | } |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 2046 | |
| 2047 | free(workers); |
| 2048 | event_free(signal_event); |
| 2049 | event_free(accept_event); |
| 2050 | event_base_free(base); |
| 2051 | close(fd); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 2052 | return EXIT_SUCCESS; |
Christopher Faulet | f95b111 | 2016-12-21 08:58:16 +0100 | [diff] [blame] | 2053 | |
| 2054 | error: |
| 2055 | if (workers != NULL) |
| 2056 | free(workers); |
| 2057 | if (signal_event != NULL) |
| 2058 | event_free(signal_event); |
| 2059 | if (accept_event != NULL) |
| 2060 | event_free(accept_event); |
| 2061 | if (base != NULL) |
| 2062 | event_base_free(base); |
| 2063 | if (fd != -1) |
| 2064 | close(fd); |
Christopher Faulet | 010fded | 2016-11-03 22:49:37 +0100 | [diff] [blame] | 2065 | return EXIT_FAILURE; |
| 2066 | } |