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