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