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