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