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