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 |
Willy Tarreau | 3ff9610 | 2022-08-31 08:55:12 +0200 | [diff] [blame] | 196 | if (bind(fd, (struct sockaddr *)addr, addr->ss_family == AF_INET6 ? |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 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 | |
Willy Tarreau | 3ff9610 | 2022-08-31 08:55:12 +0200 | [diff] [blame] | 300 | addrlen = sizeof(addr); |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 301 | ret = recvfrom(fd, pktbuf, MAXPKTSIZE, MSG_DONTWAIT | MSG_NOSIGNAL, |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 302 | (struct sockaddr *)&addr, &addrlen); |
| 303 | |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 304 | if (rand_rate > 0) { |
| 305 | history[history_idx].len = ret; // note: we may store -1/EAGAIN |
| 306 | if (prng(100) < rand_rate) { |
| 307 | /* return a random buffer or nothing */ |
| 308 | int idx = prng(MAXREORDER + 1) - 1; |
| 309 | if (idx < 0) { |
| 310 | /* pretend we didn't receive anything */ |
| 311 | return 0; |
| 312 | } |
| 313 | pktbuf = history[idx].buf; |
| 314 | ret = history[idx].len; |
| 315 | if (ret < 0) |
| 316 | errno = EAGAIN; |
| 317 | } |
| 318 | } |
| 319 | |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 320 | if (ret == 0) |
| 321 | return 0; |
| 322 | |
| 323 | if (ret < 0) |
| 324 | return errno == EAGAIN ? 0 : -1; |
| 325 | |
Willy Tarreau | 42cef2a | 2022-03-16 15:07:51 +0100 | [diff] [blame] | 326 | if (corr_rate > 0 && prng(100) < corr_rate) { |
| 327 | unsigned int rnd = prng(corr_span * 256); // pos and value |
| 328 | unsigned int pos = corr_base + (rnd >> 8); |
| 329 | |
| 330 | if (pos < ret) |
| 331 | pktbuf[pos] ^= rnd; |
| 332 | } |
| 333 | |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 334 | conn = NULL; |
| 335 | for (i = 0; i < nbconn; i++) { |
| 336 | if (addr.ss_family != conns[i].cli_addr.ss_family) |
| 337 | continue; |
| 338 | if (memcmp(&conns[i].cli_addr, &addr, |
| 339 | (addr.ss_family == AF_INET6) ? |
| 340 | sizeof(struct sockaddr_in6) : |
| 341 | sizeof(struct sockaddr_in)) != 0) |
| 342 | continue; |
| 343 | conn = &conns[i]; |
| 344 | break; |
| 345 | } |
| 346 | |
| 347 | if (!conn) { |
| 348 | /* address not found, create a new conn or replace the oldest |
| 349 | * one. For now we support a single one. |
| 350 | */ |
| 351 | conn = &conns[0]; |
| 352 | |
| 353 | memcpy(&conn->cli_addr, &addr, |
| 354 | (addr.ss_family == AF_INET6) ? |
| 355 | sizeof(struct sockaddr_in6) : |
| 356 | sizeof(struct sockaddr_in)); |
| 357 | |
| 358 | if (conn->fd_bck < 0) { |
| 359 | /* try to create a new connection */ |
| 360 | conn->fd_bck = add_connection(&srv_addr); |
| 361 | nbfd = update_pfd(pfd, fd, conns, nbconn); // FIXME: MAXCONN instead ? |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | if (conn->fd_bck < 0) |
| 366 | return 0; |
| 367 | |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 368 | ret = send(conn->fd_bck, pktbuf, ret, MSG_DONTWAIT | MSG_NOSIGNAL); |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | /* Handle a read operation on an FD. Close and return 0 when the read returns zero or an error */ |
| 373 | int handle_bck(int fd, struct pollfd *pfd, struct conn *conns, int nbconn) |
| 374 | { |
| 375 | struct sockaddr_storage addr; |
| 376 | socklen_t addrlen; |
| 377 | struct conn *conn; |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 378 | char *pktbuf = trash; |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 379 | int ret; |
| 380 | |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 381 | if (rand_rate > 0) { |
| 382 | /* keep a copy of this packet */ |
| 383 | history_idx++; |
| 384 | if (history_idx >= MAXREORDER) |
| 385 | history_idx = 0; |
| 386 | pktbuf = history[history_idx].buf; |
| 387 | } |
| 388 | |
| 389 | ret = recvfrom(fd, pktbuf, MAXPKTSIZE, MSG_DONTWAIT | MSG_NOSIGNAL, |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 390 | (struct sockaddr *)&addr, &addrlen); |
| 391 | |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 392 | if (rand_rate > 0) { |
| 393 | history[history_idx].len = ret; // note: we may store -1/EAGAIN |
| 394 | if (prng(100) < rand_rate) { |
| 395 | /* return a random buffer or nothing */ |
| 396 | int idx = prng(MAXREORDER + 1) - 1; |
| 397 | if (idx < 0) { |
| 398 | /* pretend we didn't receive anything */ |
| 399 | return 0; |
| 400 | } |
| 401 | pktbuf = history[idx].buf; |
| 402 | ret = history[idx].len; |
| 403 | if (ret < 0) |
| 404 | errno = EAGAIN; |
| 405 | } |
| 406 | } |
| 407 | |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 408 | if (ret == 0) |
| 409 | return 0; |
| 410 | |
| 411 | if (ret < 0) |
| 412 | return errno == EAGAIN ? 0 : -1; |
| 413 | |
| 414 | conn = conn_bck_lookup(conns, nbconn, fd); |
| 415 | if (!conn) |
| 416 | return 0; |
| 417 | |
Willy Tarreau | e7a7fb4 | 2022-03-03 17:36:53 +0100 | [diff] [blame] | 418 | ret = sendto(fd_frt, pktbuf, ret, MSG_DONTWAIT | MSG_NOSIGNAL, |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 419 | (struct sockaddr *)&conn->cli_addr, |
| 420 | conn->cli_addr.ss_family == AF_INET6 ? |
| 421 | sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)); |
| 422 | return ret; |
| 423 | } |
| 424 | |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 425 | /* print the usage message for program named <name> and exit with status <status> */ |
| 426 | void usage(int status, const char *name) |
| 427 | { |
| 428 | if (strchr(name, '/')) |
| 429 | name = strrchr(name, '/') + 1; |
| 430 | die(status, |
| 431 | "Usage: %s [-h] [options] [<laddr>:]<lport> [<saddr>:]<sport>\n" |
| 432 | "Options:\n" |
| 433 | " -h display this help\n" |
| 434 | " -r rate reorder/duplicate/lose around <rate>%% of packets\n" |
| 435 | " -s seed force initial random seed (currently %#x)\n" |
Willy Tarreau | 42cef2a | 2022-03-16 15:07:51 +0100 | [diff] [blame] | 436 | " -c rate corrupt around <rate>%% of packets\n" |
| 437 | " -o ofs start offset of corrupted area (def: 0)\n" |
| 438 | " -w width width of the corrupted area (def: 1)\n" |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 439 | "", name, prng_state); |
| 440 | } |
| 441 | |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 442 | int main(int argc, char **argv) |
| 443 | { |
| 444 | struct errmsg err; |
| 445 | struct pollfd *pfd; |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 446 | int opt; |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 447 | int i; |
| 448 | |
| 449 | err.len = 0; |
| 450 | err.size = 100; |
| 451 | err.msg = malloc(err.size); |
| 452 | |
Willy Tarreau | 42cef2a | 2022-03-16 15:07:51 +0100 | [diff] [blame] | 453 | while ((opt = getopt(argc, argv, "hr:s:c:o:w:")) != -1) { |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 454 | switch (opt) { |
| 455 | case 'r': // rand_rate% |
| 456 | rand_rate = atoi(optarg); |
| 457 | break; |
| 458 | case 's': // seed |
| 459 | prng_state = atol(optarg); |
| 460 | break; |
Willy Tarreau | 42cef2a | 2022-03-16 15:07:51 +0100 | [diff] [blame] | 461 | case 'c': // corruption rate |
| 462 | corr_rate = atol(optarg); |
| 463 | break; |
| 464 | case 'o': // corruption offset |
| 465 | corr_base = atol(optarg); |
| 466 | break; |
| 467 | case 'w': // corruption width |
| 468 | corr_span = atol(optarg); |
| 469 | break; |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 470 | default: // help, anything else |
| 471 | usage(0, argv[0]); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | if (argc - optind < 2) |
| 476 | usage(1, argv[0]); |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 477 | |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 478 | if (addr_to_ss(argv[optind], &frt_addr, &err) < 0) |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 479 | die(1, "parsing listen address: %s\n", err.msg); |
| 480 | |
Willy Tarreau | 04d3c5c | 2022-03-16 14:49:33 +0100 | [diff] [blame] | 481 | if (addr_to_ss(argv[optind+1], &srv_addr, &err) < 0) |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 482 | die(1, "parsing server address: %s\n", err.msg); |
| 483 | |
Willy Tarreau | c927137 | 2022-03-03 16:53:46 +0100 | [diff] [blame] | 484 | pfd = calloc(sizeof(struct pollfd), MAXCONN + 1); |
| 485 | if (!pfd) |
| 486 | die(1, "out of memory\n"); |
| 487 | |
| 488 | fd_frt = create_udp_listener(&frt_addr, &err); |
| 489 | if (fd_frt < 0) |
| 490 | die(1, "binding listener: %s\n", err.msg); |
| 491 | |
| 492 | |
| 493 | for (i = 0; i < MAXCONN; i++) |
| 494 | conns[i].fd_bck = -1; |
| 495 | |
| 496 | nbfd = update_pfd(pfd, fd_frt, conns, MAXCONN); |
| 497 | |
| 498 | while (1) { |
| 499 | /* listen for incoming packets */ |
| 500 | int ret, i; |
| 501 | |
| 502 | ret = poll(pfd, nbfd, 1000); |
| 503 | if (ret <= 0) |
| 504 | continue; |
| 505 | |
| 506 | for (i = 0; ret; i++) { |
| 507 | if (!pfd[i].revents) |
| 508 | continue; |
| 509 | ret--; |
| 510 | |
| 511 | if (pfd[i].fd == fd_frt) { |
| 512 | handle_frt(pfd[i].fd, pfd, conns, nbconn); |
| 513 | continue; |
| 514 | } |
| 515 | |
| 516 | handle_bck(pfd[i].fd, pfd, conns, nbconn); |
| 517 | } |
| 518 | } |
| 519 | } |