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