Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010-2022 Willy Tarreau <w@1wt.eu> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 17 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 19 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 20 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 22 | * OTHER DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
| 28 | #include <string.h> |
| 29 | #include <ctype.h> |
| 30 | #include <sys/time.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <netinet/tcp.h> |
| 34 | #include <netinet/in.h> |
| 35 | #include <arpa/inet.h> |
| 36 | #include <netdb.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <errno.h> |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 39 | #include <getopt.h> |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 40 | #include <signal.h> |
| 41 | #include <stdarg.h> |
| 42 | #include <sys/stat.h> |
| 43 | #include <time.h> |
| 44 | #include <limits.h> |
| 45 | #include <poll.h> |
| 46 | #include <stdio.h> |
| 47 | #include <stdlib.h> |
| 48 | #include <stdarg.h> |
| 49 | |
| 50 | #define MAXCONN 1 |
| 51 | |
| 52 | const int zero = 0; |
| 53 | const int one = 1; |
| 54 | |
| 55 | struct conn { |
| 56 | struct sockaddr_storage cli_addr; |
| 57 | int fd_bck; |
| 58 | }; |
| 59 | |
| 60 | struct errmsg { |
| 61 | char *msg; |
| 62 | int size; |
| 63 | int len; |
| 64 | }; |
| 65 | |
| 66 | struct sockaddr_storage frt_addr; // listen address |
| 67 | struct sockaddr_storage srv_addr; // server address |
| 68 | |
| 69 | #define MAXPKTSIZE 16384 |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 70 | #define MAXREORDER 20 |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 71 | char trash[MAXPKTSIZE]; |
| 72 | |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 73 | /* history buffer, to resend random packets */ |
| 74 | struct { |
| 75 | char buf[MAXPKTSIZE]; |
| 76 | size_t len; |
| 77 | } history[MAXREORDER]; |
| 78 | int history_idx = 0; |
| 79 | unsigned int rand_rate = 0; |
Willy Tarreau | 42cef2a | 2022-03-16 15:07:51 +0100 | [diff] [blame] | 80 | unsigned int corr_rate = 0; |
| 81 | unsigned int corr_span = 1; |
| 82 | unsigned int corr_base = 0; |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 83 | |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 84 | struct conn conns[MAXCONN]; // sole connection for now |
| 85 | int fd_frt; |
| 86 | |
| 87 | int nbfd = 0; |
| 88 | int nbconn = MAXCONN; |
| 89 | |
| 90 | |
| 91 | /* display the message and exit with the code */ |
| 92 | __attribute__((noreturn)) void die(int code, const char *format, ...) |
| 93 | { |
| 94 | va_list args; |
| 95 | |
| 96 | va_start(args, format); |
| 97 | vfprintf(stderr, format, args); |
| 98 | va_end(args); |
| 99 | exit(code); |
| 100 | } |
| 101 | |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 102 | /* Xorshift RNG */ |
| 103 | unsigned int prng_state = ~0U/3; // half bits set, but any seed will fit |
| 104 | static inline unsigned int prng(unsigned int range) |
| 105 | { |
| 106 | unsigned int x = prng_state; |
| 107 | |
| 108 | x ^= x << 13; |
| 109 | x ^= x >> 17; |
| 110 | x ^= x << 5; |
| 111 | prng_state = x; |
| 112 | return ((unsigned long long)x * (range - 1) + x) >> 32; |
| 113 | } |
| 114 | |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 115 | /* converts str in the form [<ipv4>|<ipv6>|<hostname>]:port to struct sockaddr_storage. |
| 116 | * Returns < 0 with err set in case of error. |
| 117 | */ |
| 118 | int addr_to_ss(char *str, struct sockaddr_storage *ss, struct errmsg *err) |
| 119 | { |
| 120 | char *port_str; |
| 121 | int port; |
| 122 | |
| 123 | /* look for the addr/port delimiter, it's the last colon. */ |
| 124 | if ((port_str = strrchr(str, ':')) == NULL) |
| 125 | port_str = str; |
| 126 | else |
| 127 | *port_str++ = 0; |
| 128 | |
| 129 | port = atoi(port_str); |
| 130 | if (port <= 0 || port > 65535) { |
| 131 | err->len = snprintf(err->msg, err->size, "Missing/invalid port number: '%s'\n", port_str); |
| 132 | return -1; |
| 133 | } |
| 134 | *port_str = 0; // present an empty address if none was set |
| 135 | |
| 136 | memset(ss, 0, sizeof(*ss)); |
| 137 | |
| 138 | if (strrchr(str, ':') != NULL) { |
| 139 | /* IPv6 address contains ':' */ |
| 140 | ss->ss_family = AF_INET6; |
| 141 | ((struct sockaddr_in6 *)ss)->sin6_port = htons(port); |
| 142 | |
| 143 | if (!inet_pton(ss->ss_family, str, &((struct sockaddr_in6 *)ss)->sin6_addr)) { |
| 144 | err->len = snprintf(err->msg, err->size, "Invalid IPv6 server address: '%s'", str); |
| 145 | return -1; |
| 146 | } |
| 147 | } |
| 148 | else { |
| 149 | ss->ss_family = AF_INET; |
| 150 | ((struct sockaddr_in *)ss)->sin_port = htons(port); |
| 151 | |
| 152 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 153 | ((struct sockaddr_in *)ss)->sin_addr.s_addr = INADDR_ANY; |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | if (!inet_pton(ss->ss_family, str, &((struct sockaddr_in *)ss)->sin_addr)) { |
| 158 | struct hostent *he = gethostbyname(str); |
| 159 | |
| 160 | if (he == NULL) { |
| 161 | err->len = snprintf(err->msg, err->size, "Invalid IPv4 server name: '%s'", str); |
| 162 | return -1; |
| 163 | } |
| 164 | ((struct sockaddr_in *)ss)->sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 165 | } |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* returns <0 with err in case of error or the front FD */ |
| 171 | int create_udp_listener(struct sockaddr_storage *addr, struct errmsg *err) |
| 172 | { |
| 173 | int fd; |
| 174 | |
| 175 | if ((fd = socket(addr->ss_family, SOCK_DGRAM, 0)) == -1) { |
| 176 | err->len = snprintf(err->msg, err->size, "socket(): '%s'", strerror(errno)); |
| 177 | goto fail; |
| 178 | } |
| 179 | |
| 180 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
| 181 | err->len = snprintf(err->msg, err->size, "fcntl(O_NONBLOCK): '%s'", strerror(errno)); |
| 182 | goto fail; |
| 183 | } |
| 184 | |
| 185 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) { |
| 186 | err->len = snprintf(err->msg, err->size, "setsockopt(SO_REUSEADDR): '%s'", strerror(errno)); |
| 187 | goto fail; |
| 188 | } |
| 189 | |
| 190 | #ifdef SO_REUSEPORT |
| 191 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one)) == -1) { |
| 192 | err->len = snprintf(err->msg, err->size, "setsockopt(SO_REUSEPORT): '%s'", strerror(errno)); |
| 193 | goto fail; |
| 194 | } |
| 195 | #endif |
| 196 | if (bind(fd, (struct sockaddr *)&frt_addr, addr->ss_family == AF_INET6 ? |
| 197 | sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) == -1) { |
| 198 | err->len = snprintf(err->msg, err->size, "bind(): '%s'", strerror(errno)); |
| 199 | goto fail; |
| 200 | } |
| 201 | |
| 202 | /* the socket is ready */ |
| 203 | return fd; |
| 204 | |
| 205 | fail: |
| 206 | if (fd > -1) |
| 207 | close(fd); |
| 208 | fd = -1; |
| 209 | return fd; |
| 210 | } |
| 211 | |
| 212 | /* recompute pollfds using frt_fd and scanning nbconn connections. |
| 213 | * Returns the number of FDs in the set. |
| 214 | */ |
| 215 | int update_pfd(struct pollfd *pfd, int frt_fd, struct conn *conns, int nbconn) |
| 216 | { |
| 217 | int nbfd = 0; |
| 218 | int i; |
| 219 | |
| 220 | pfd[nbfd].fd = frt_fd; |
| 221 | pfd[nbfd].events = POLLIN; |
| 222 | nbfd++; |
| 223 | |
| 224 | for (i = 0; i < nbconn; i++) { |
| 225 | if (conns[i].fd_bck < 0) |
| 226 | continue; |
| 227 | pfd[nbfd].fd = conns[i].fd_bck; |
| 228 | pfd[nbfd].events = POLLIN; |
| 229 | nbfd++; |
| 230 | } |
| 231 | return nbfd; |
| 232 | } |
| 233 | |
| 234 | /* searches a connection using fd <fd> as back connection, returns it if found |
| 235 | * otherwise NULL. |
| 236 | */ |
| 237 | struct conn *conn_bck_lookup(struct conn *conns, int nbconn, int fd) |
| 238 | { |
| 239 | int i; |
| 240 | |
| 241 | for (i = 0; i < nbconn; i++) { |
| 242 | if (conns[i].fd_bck < 0) |
| 243 | continue; |
| 244 | if (conns[i].fd_bck == fd) |
| 245 | return &conns[i]; |
| 246 | } |
| 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | /* Try to establish a connection to <sa>. Return the fd or -1 in case of error */ |
| 251 | int add_connection(struct sockaddr_storage *ss) |
| 252 | { |
| 253 | int fd; |
| 254 | |
| 255 | fd = socket(ss->ss_family, SOCK_DGRAM, 0); |
| 256 | if (fd < 0) |
| 257 | goto fail; |
| 258 | |
| 259 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) |
| 260 | goto fail; |
| 261 | |
| 262 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) |
| 263 | goto fail; |
| 264 | |
| 265 | if (connect(fd, (struct sockaddr *)ss, ss->ss_family == AF_INET6 ? |
| 266 | sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) == -1) { |
| 267 | if (errno != EINPROGRESS) |
| 268 | goto fail; |
| 269 | } |
| 270 | |
| 271 | return fd; |
| 272 | fail: |
| 273 | if (fd > -1) |
| 274 | close(fd); |
| 275 | return -1; |
| 276 | } |
| 277 | |
| 278 | /* Handle a read operation on an front FD. Will either reuse the existing |
| 279 | * connection if the source is found, or will allocate a new one, possibly |
| 280 | * replacing the oldest one. Returns <0 on error or the number of bytes |
| 281 | * transmitted. |
| 282 | */ |
| 283 | int handle_frt(int fd, struct pollfd *pfd, struct conn *conns, int nbconn) |
| 284 | { |
| 285 | struct sockaddr_storage addr; |
| 286 | socklen_t addrlen; |
| 287 | struct conn *conn; |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 288 | char *pktbuf = trash; |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 289 | int ret; |
| 290 | int i; |
| 291 | |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 292 | if (rand_rate > 0) { |
| 293 | /* keep a copy of this packet */ |
| 294 | history_idx++; |
| 295 | if (history_idx >= MAXREORDER) |
| 296 | history_idx = 0; |
| 297 | pktbuf = history[history_idx].buf; |
| 298 | } |
| 299 | |
| 300 | ret = recvfrom(fd, pktbuf, MAXPKTSIZE, MSG_DONTWAIT | MSG_NOSIGNAL, |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 301 | (struct sockaddr *)&addr, &addrlen); |
| 302 | |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 303 | if (rand_rate > 0) { |
| 304 | history[history_idx].len = ret; // note: we may store -1/EAGAIN |
| 305 | if (prng(100) < rand_rate) { |
| 306 | /* return a random buffer or nothing */ |
| 307 | int idx = prng(MAXREORDER + 1) - 1; |
| 308 | if (idx < 0) { |
| 309 | /* pretend we didn't receive anything */ |
| 310 | return 0; |
| 311 | } |
| 312 | pktbuf = history[idx].buf; |
| 313 | ret = history[idx].len; |
| 314 | if (ret < 0) |
| 315 | errno = EAGAIN; |
| 316 | } |
| 317 | } |
| 318 | |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 319 | if (ret == 0) |
| 320 | return 0; |
| 321 | |
| 322 | if (ret < 0) |
| 323 | return errno == EAGAIN ? 0 : -1; |
| 324 | |
Willy Tarreau | 42cef2a | 2022-03-16 15:07:51 +0100 | [diff] [blame] | 325 | if (corr_rate > 0 && prng(100) < corr_rate) { |
| 326 | unsigned int rnd = prng(corr_span * 256); // pos and value |
| 327 | unsigned int pos = corr_base + (rnd >> 8); |
| 328 | |
| 329 | if (pos < ret) |
| 330 | pktbuf[pos] ^= rnd; |
| 331 | } |
| 332 | |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 333 | conn = NULL; |
| 334 | for (i = 0; i < nbconn; i++) { |
| 335 | if (addr.ss_family != conns[i].cli_addr.ss_family) |
| 336 | continue; |
| 337 | if (memcmp(&conns[i].cli_addr, &addr, |
| 338 | (addr.ss_family == AF_INET6) ? |
| 339 | sizeof(struct sockaddr_in6) : |
| 340 | sizeof(struct sockaddr_in)) != 0) |
| 341 | continue; |
| 342 | conn = &conns[i]; |
| 343 | break; |
| 344 | } |
| 345 | |
| 346 | if (!conn) { |
| 347 | /* address not found, create a new conn or replace the oldest |
| 348 | * one. For now we support a single one. |
| 349 | */ |
| 350 | conn = &conns[0]; |
| 351 | |
| 352 | memcpy(&conn->cli_addr, &addr, |
| 353 | (addr.ss_family == AF_INET6) ? |
| 354 | sizeof(struct sockaddr_in6) : |
| 355 | sizeof(struct sockaddr_in)); |
| 356 | |
| 357 | if (conn->fd_bck < 0) { |
| 358 | /* try to create a new connection */ |
| 359 | conn->fd_bck = add_connection(&srv_addr); |
| 360 | nbfd = update_pfd(pfd, fd, conns, nbconn); // FIXME: MAXCONN instead ? |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if (conn->fd_bck < 0) |
| 365 | return 0; |
| 366 | |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 367 | ret = send(conn->fd_bck, pktbuf, ret, MSG_DONTWAIT | MSG_NOSIGNAL); |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | /* Handle a read operation on an FD. Close and return 0 when the read returns zero or an error */ |
| 372 | int handle_bck(int fd, struct pollfd *pfd, struct conn *conns, int nbconn) |
| 373 | { |
| 374 | struct sockaddr_storage addr; |
| 375 | socklen_t addrlen; |
| 376 | struct conn *conn; |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 377 | char *pktbuf = trash; |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 378 | int ret; |
| 379 | |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 380 | if (rand_rate > 0) { |
| 381 | /* keep a copy of this packet */ |
| 382 | history_idx++; |
| 383 | if (history_idx >= MAXREORDER) |
| 384 | history_idx = 0; |
| 385 | pktbuf = history[history_idx].buf; |
| 386 | } |
| 387 | |
| 388 | ret = recvfrom(fd, pktbuf, MAXPKTSIZE, MSG_DONTWAIT | MSG_NOSIGNAL, |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 389 | (struct sockaddr *)&addr, &addrlen); |
| 390 | |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 391 | if (rand_rate > 0) { |
| 392 | history[history_idx].len = ret; // note: we may store -1/EAGAIN |
| 393 | if (prng(100) < rand_rate) { |
| 394 | /* return a random buffer or nothing */ |
| 395 | int idx = prng(MAXREORDER + 1) - 1; |
| 396 | if (idx < 0) { |
| 397 | /* pretend we didn't receive anything */ |
| 398 | return 0; |
| 399 | } |
| 400 | pktbuf = history[idx].buf; |
| 401 | ret = history[idx].len; |
| 402 | if (ret < 0) |
| 403 | errno = EAGAIN; |
| 404 | } |
| 405 | } |
| 406 | |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 407 | if (ret == 0) |
| 408 | return 0; |
| 409 | |
| 410 | if (ret < 0) |
| 411 | return errno == EAGAIN ? 0 : -1; |
| 412 | |
| 413 | conn = conn_bck_lookup(conns, nbconn, fd); |
| 414 | if (!conn) |
| 415 | return 0; |
| 416 | |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 417 | ret = sendto(fd_frt, pktbuf, ret, MSG_DONTWAIT | MSG_NOSIGNAL, |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 418 | (struct sockaddr *)&conn->cli_addr, |
| 419 | conn->cli_addr.ss_family == AF_INET6 ? |
| 420 | sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)); |
| 421 | return ret; |
| 422 | } |
| 423 | |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 424 | /* print the usage message for program named <name> and exit with status <status> */ |
| 425 | void usage(int status, const char *name) |
| 426 | { |
| 427 | if (strchr(name, '/')) |
| 428 | name = strrchr(name, '/') + 1; |
| 429 | die(status, |
| 430 | "Usage: %s [-h] [options] [<laddr>:]<lport> [<saddr>:]<sport>\n" |
| 431 | "Options:\n" |
| 432 | " -h display this help\n" |
| 433 | " -r rate reorder/duplicate/lose around <rate>%% of packets\n" |
| 434 | " -s seed force initial random seed (currently %#x)\n" |
Willy Tarreau | 42cef2a | 2022-03-16 15:07:51 +0100 | [diff] [blame] | 435 | " -c rate corrupt around <rate>%% of packets\n" |
| 436 | " -o ofs start offset of corrupted area (def: 0)\n" |
| 437 | " -w width width of the corrupted area (def: 1)\n" |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 438 | "", name, prng_state); |
| 439 | } |
| 440 | |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 441 | int main(int argc, char **argv) |
| 442 | { |
| 443 | struct errmsg err; |
| 444 | struct pollfd *pfd; |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 445 | int opt; |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 446 | int i; |
| 447 | |
| 448 | err.len = 0; |
| 449 | err.size = 100; |
| 450 | err.msg = malloc(err.size); |
| 451 | |
Willy Tarreau | 42cef2a | 2022-03-16 15:07:51 +0100 | [diff] [blame] | 452 | while ((opt = getopt(argc, argv, "hr:s:c:o:w:")) != -1) { |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 453 | switch (opt) { |
| 454 | case 'r': // rand_rate% |
| 455 | rand_rate = atoi(optarg); |
| 456 | break; |
| 457 | case 's': // seed |
| 458 | prng_state = atol(optarg); |
| 459 | break; |
Willy Tarreau | 42cef2a | 2022-03-16 15:07:51 +0100 | [diff] [blame] | 460 | case 'c': // corruption rate |
| 461 | corr_rate = atol(optarg); |
| 462 | break; |
| 463 | case 'o': // corruption offset |
| 464 | corr_base = atol(optarg); |
| 465 | break; |
| 466 | case 'w': // corruption width |
| 467 | corr_span = atol(optarg); |
| 468 | break; |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 469 | default: // help, anything else |
| 470 | usage(0, argv[0]); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | if (argc - optind < 2) |
| 475 | usage(1, argv[0]); |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 476 | |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 477 | if (addr_to_ss(argv[optind], &frt_addr, &err) < 0) |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 478 | die(1, "parsing listen address: %s\n", err.msg); |
| 479 | |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 480 | if (addr_to_ss(argv[optind+1], &srv_addr, &err) < 0) |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 481 | die(1, "parsing server address: %s\n", err.msg); |
| 482 | |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 483 | pfd = calloc(sizeof(struct pollfd), MAXCONN + 1); |
| 484 | if (!pfd) |
| 485 | die(1, "out of memory\n"); |
| 486 | |
| 487 | fd_frt = create_udp_listener(&frt_addr, &err); |
| 488 | if (fd_frt < 0) |
| 489 | die(1, "binding listener: %s\n", err.msg); |
| 490 | |
| 491 | |
| 492 | for (i = 0; i < MAXCONN; i++) |
| 493 | conns[i].fd_bck = -1; |
| 494 | |
| 495 | nbfd = update_pfd(pfd, fd_frt, conns, MAXCONN); |
| 496 | |
| 497 | while (1) { |
| 498 | /* listen for incoming packets */ |
| 499 | int ret, i; |
| 500 | |
| 501 | ret = poll(pfd, nbfd, 1000); |
| 502 | if (ret <= 0) |
| 503 | continue; |
| 504 | |
| 505 | for (i = 0; ret; i++) { |
| 506 | if (!pfd[i].revents) |
| 507 | continue; |
| 508 | ret--; |
| 509 | |
| 510 | if (pfd[i].fd == fd_frt) { |
| 511 | handle_frt(pfd[i].fd, pfd, conns, nbconn); |
| 512 | continue; |
| 513 | } |
| 514 | |
| 515 | handle_bck(pfd[i].fd, pfd, conns, nbconn); |
| 516 | } |
| 517 | } |
| 518 | } |