blob: 8a851fc49c344e3158f184f783dfd900291f657a [file] [log] [blame]
Willy Tarreauc9271372022-03-03 16:53:46 +01001/*
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 Tarreau04d3c5c2022-03-16 14:49:33 +010039#include <getopt.h>
Willy Tarreauc9271372022-03-03 16:53:46 +010040#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
52const int zero = 0;
53const int one = 1;
54
55struct conn {
56 struct sockaddr_storage cli_addr;
57 int fd_bck;
58};
59
60struct errmsg {
61 char *msg;
62 int size;
63 int len;
64};
65
66struct sockaddr_storage frt_addr; // listen address
67struct sockaddr_storage srv_addr; // server address
68
69#define MAXPKTSIZE 16384
Willy Tarreaue7a7fb42022-03-03 17:36:53 +010070#define MAXREORDER 20
Willy Tarreauc9271372022-03-03 16:53:46 +010071char trash[MAXPKTSIZE];
72
Willy Tarreaue7a7fb42022-03-03 17:36:53 +010073/* history buffer, to resend random packets */
74struct {
75 char buf[MAXPKTSIZE];
76 size_t len;
77} history[MAXREORDER];
78int history_idx = 0;
79unsigned int rand_rate = 0;
Willy Tarreau42cef2a2022-03-16 15:07:51 +010080unsigned int corr_rate = 0;
81unsigned int corr_span = 1;
82unsigned int corr_base = 0;
Willy Tarreaue7a7fb42022-03-03 17:36:53 +010083
Willy Tarreauc9271372022-03-03 16:53:46 +010084struct conn conns[MAXCONN]; // sole connection for now
85int fd_frt;
86
87int nbfd = 0;
88int 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 Tarreaue7a7fb42022-03-03 17:36:53 +0100102/* Xorshift RNG */
103unsigned int prng_state = ~0U/3; // half bits set, but any seed will fit
104static 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 Tarreauc9271372022-03-03 16:53:46 +0100115/* converts str in the form [<ipv4>|<ipv6>|<hostname>]:port to struct sockaddr_storage.
116 * Returns < 0 with err set in case of error.
117 */
118int 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 */
171int 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 Tarreau3ff96102022-08-31 08:55:12 +0200196 if (bind(fd, (struct sockaddr *)addr, addr->ss_family == AF_INET6 ?
Willy Tarreauc9271372022-03-03 16:53:46 +0100197 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 */
215int 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 */
237struct 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 */
251int 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
Frédéric Lécaille192093b2022-09-08 20:38:59 +0200278/* Corrupt <buf> buffer with <buflen> as length if required */
279static void pktbuf_apply_corruption(char *buf, size_t buflen)
280{
281 if (corr_rate > 0 && prng(100) < corr_rate) {
282 unsigned int rnd = prng(corr_span * 256); // pos and value
283 unsigned int pos = corr_base + (rnd >> 8);
284
285 if (pos < buflen)
286 buf[pos] ^= rnd;
287 }
288}
289
Willy Tarreauc9271372022-03-03 16:53:46 +0100290/* Handle a read operation on an front FD. Will either reuse the existing
291 * connection if the source is found, or will allocate a new one, possibly
292 * replacing the oldest one. Returns <0 on error or the number of bytes
293 * transmitted.
294 */
295int handle_frt(int fd, struct pollfd *pfd, struct conn *conns, int nbconn)
296{
297 struct sockaddr_storage addr;
298 socklen_t addrlen;
299 struct conn *conn;
Willy Tarreaue7a7fb42022-03-03 17:36:53 +0100300 char *pktbuf = trash;
Willy Tarreauc9271372022-03-03 16:53:46 +0100301 int ret;
302 int i;
303
Willy Tarreaue7a7fb42022-03-03 17:36:53 +0100304 if (rand_rate > 0) {
305 /* keep a copy of this packet */
306 history_idx++;
307 if (history_idx >= MAXREORDER)
308 history_idx = 0;
309 pktbuf = history[history_idx].buf;
310 }
311
Willy Tarreau3ff96102022-08-31 08:55:12 +0200312 addrlen = sizeof(addr);
Willy Tarreaue7a7fb42022-03-03 17:36:53 +0100313 ret = recvfrom(fd, pktbuf, MAXPKTSIZE, MSG_DONTWAIT | MSG_NOSIGNAL,
Willy Tarreauc9271372022-03-03 16:53:46 +0100314 (struct sockaddr *)&addr, &addrlen);
315
Willy Tarreaue7a7fb42022-03-03 17:36:53 +0100316 if (rand_rate > 0) {
317 history[history_idx].len = ret; // note: we may store -1/EAGAIN
318 if (prng(100) < rand_rate) {
319 /* return a random buffer or nothing */
320 int idx = prng(MAXREORDER + 1) - 1;
321 if (idx < 0) {
322 /* pretend we didn't receive anything */
323 return 0;
324 }
325 pktbuf = history[idx].buf;
326 ret = history[idx].len;
327 if (ret < 0)
328 errno = EAGAIN;
329 }
330 }
331
Willy Tarreauc9271372022-03-03 16:53:46 +0100332 if (ret == 0)
333 return 0;
334
335 if (ret < 0)
336 return errno == EAGAIN ? 0 : -1;
337
Frédéric Lécaille192093b2022-09-08 20:38:59 +0200338 pktbuf_apply_corruption(pktbuf, ret);
Willy Tarreau42cef2a2022-03-16 15:07:51 +0100339
Willy Tarreauc9271372022-03-03 16:53:46 +0100340 conn = NULL;
341 for (i = 0; i < nbconn; i++) {
342 if (addr.ss_family != conns[i].cli_addr.ss_family)
343 continue;
344 if (memcmp(&conns[i].cli_addr, &addr,
345 (addr.ss_family == AF_INET6) ?
346 sizeof(struct sockaddr_in6) :
347 sizeof(struct sockaddr_in)) != 0)
348 continue;
349 conn = &conns[i];
350 break;
351 }
352
353 if (!conn) {
354 /* address not found, create a new conn or replace the oldest
355 * one. For now we support a single one.
356 */
357 conn = &conns[0];
358
359 memcpy(&conn->cli_addr, &addr,
360 (addr.ss_family == AF_INET6) ?
361 sizeof(struct sockaddr_in6) :
362 sizeof(struct sockaddr_in));
363
364 if (conn->fd_bck < 0) {
365 /* try to create a new connection */
366 conn->fd_bck = add_connection(&srv_addr);
367 nbfd = update_pfd(pfd, fd, conns, nbconn); // FIXME: MAXCONN instead ?
368 }
369 }
370
371 if (conn->fd_bck < 0)
372 return 0;
373
Willy Tarreaue7a7fb42022-03-03 17:36:53 +0100374 ret = send(conn->fd_bck, pktbuf, ret, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreauc9271372022-03-03 16:53:46 +0100375 return ret;
376}
377
378/* Handle a read operation on an FD. Close and return 0 when the read returns zero or an error */
379int handle_bck(int fd, struct pollfd *pfd, struct conn *conns, int nbconn)
380{
381 struct sockaddr_storage addr;
382 socklen_t addrlen;
383 struct conn *conn;
Willy Tarreaue7a7fb42022-03-03 17:36:53 +0100384 char *pktbuf = trash;
Willy Tarreauc9271372022-03-03 16:53:46 +0100385 int ret;
386
Willy Tarreaue7a7fb42022-03-03 17:36:53 +0100387 if (rand_rate > 0) {
388 /* keep a copy of this packet */
389 history_idx++;
390 if (history_idx >= MAXREORDER)
391 history_idx = 0;
392 pktbuf = history[history_idx].buf;
393 }
394
395 ret = recvfrom(fd, pktbuf, MAXPKTSIZE, MSG_DONTWAIT | MSG_NOSIGNAL,
Willy Tarreauc9271372022-03-03 16:53:46 +0100396 (struct sockaddr *)&addr, &addrlen);
397
Willy Tarreaue7a7fb42022-03-03 17:36:53 +0100398 if (rand_rate > 0) {
399 history[history_idx].len = ret; // note: we may store -1/EAGAIN
400 if (prng(100) < rand_rate) {
401 /* return a random buffer or nothing */
402 int idx = prng(MAXREORDER + 1) - 1;
403 if (idx < 0) {
404 /* pretend we didn't receive anything */
405 return 0;
406 }
407 pktbuf = history[idx].buf;
408 ret = history[idx].len;
409 if (ret < 0)
410 errno = EAGAIN;
411 }
412 }
413
Willy Tarreauc9271372022-03-03 16:53:46 +0100414 if (ret == 0)
415 return 0;
416
417 if (ret < 0)
418 return errno == EAGAIN ? 0 : -1;
419
Frédéric Lécaille192093b2022-09-08 20:38:59 +0200420 pktbuf_apply_corruption(pktbuf, ret);
421
Willy Tarreauc9271372022-03-03 16:53:46 +0100422 conn = conn_bck_lookup(conns, nbconn, fd);
423 if (!conn)
424 return 0;
425
Willy Tarreaue7a7fb42022-03-03 17:36:53 +0100426 ret = sendto(fd_frt, pktbuf, ret, MSG_DONTWAIT | MSG_NOSIGNAL,
Willy Tarreauc9271372022-03-03 16:53:46 +0100427 (struct sockaddr *)&conn->cli_addr,
428 conn->cli_addr.ss_family == AF_INET6 ?
429 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
430 return ret;
431}
432
Willy Tarreau04d3c5c2022-03-16 14:49:33 +0100433/* print the usage message for program named <name> and exit with status <status> */
434void usage(int status, const char *name)
435{
436 if (strchr(name, '/'))
437 name = strrchr(name, '/') + 1;
438 die(status,
439 "Usage: %s [-h] [options] [<laddr>:]<lport> [<saddr>:]<sport>\n"
440 "Options:\n"
441 " -h display this help\n"
442 " -r rate reorder/duplicate/lose around <rate>%% of packets\n"
443 " -s seed force initial random seed (currently %#x)\n"
Willy Tarreau42cef2a2022-03-16 15:07:51 +0100444 " -c rate corrupt around <rate>%% of packets\n"
445 " -o ofs start offset of corrupted area (def: 0)\n"
446 " -w width width of the corrupted area (def: 1)\n"
Willy Tarreau04d3c5c2022-03-16 14:49:33 +0100447 "", name, prng_state);
448}
449
Willy Tarreauc9271372022-03-03 16:53:46 +0100450int main(int argc, char **argv)
451{
452 struct errmsg err;
453 struct pollfd *pfd;
Willy Tarreau04d3c5c2022-03-16 14:49:33 +0100454 int opt;
Willy Tarreauc9271372022-03-03 16:53:46 +0100455 int i;
456
457 err.len = 0;
458 err.size = 100;
459 err.msg = malloc(err.size);
460
Willy Tarreau42cef2a2022-03-16 15:07:51 +0100461 while ((opt = getopt(argc, argv, "hr:s:c:o:w:")) != -1) {
Willy Tarreau04d3c5c2022-03-16 14:49:33 +0100462 switch (opt) {
463 case 'r': // rand_rate%
464 rand_rate = atoi(optarg);
465 break;
466 case 's': // seed
467 prng_state = atol(optarg);
468 break;
Willy Tarreau42cef2a2022-03-16 15:07:51 +0100469 case 'c': // corruption rate
470 corr_rate = atol(optarg);
471 break;
472 case 'o': // corruption offset
473 corr_base = atol(optarg);
474 break;
475 case 'w': // corruption width
476 corr_span = atol(optarg);
477 break;
Willy Tarreau04d3c5c2022-03-16 14:49:33 +0100478 default: // help, anything else
479 usage(0, argv[0]);
480 }
481 }
482
483 if (argc - optind < 2)
484 usage(1, argv[0]);
Willy Tarreauc9271372022-03-03 16:53:46 +0100485
Willy Tarreau04d3c5c2022-03-16 14:49:33 +0100486 if (addr_to_ss(argv[optind], &frt_addr, &err) < 0)
Willy Tarreauc9271372022-03-03 16:53:46 +0100487 die(1, "parsing listen address: %s\n", err.msg);
488
Willy Tarreau04d3c5c2022-03-16 14:49:33 +0100489 if (addr_to_ss(argv[optind+1], &srv_addr, &err) < 0)
Willy Tarreauc9271372022-03-03 16:53:46 +0100490 die(1, "parsing server address: %s\n", err.msg);
491
Willy Tarreauc9271372022-03-03 16:53:46 +0100492 pfd = calloc(sizeof(struct pollfd), MAXCONN + 1);
493 if (!pfd)
494 die(1, "out of memory\n");
495
496 fd_frt = create_udp_listener(&frt_addr, &err);
497 if (fd_frt < 0)
498 die(1, "binding listener: %s\n", err.msg);
499
500
501 for (i = 0; i < MAXCONN; i++)
502 conns[i].fd_bck = -1;
503
504 nbfd = update_pfd(pfd, fd_frt, conns, MAXCONN);
505
506 while (1) {
507 /* listen for incoming packets */
508 int ret, i;
509
510 ret = poll(pfd, nbfd, 1000);
511 if (ret <= 0)
512 continue;
513
514 for (i = 0; ret; i++) {
515 if (!pfd[i].revents)
516 continue;
517 ret--;
518
519 if (pfd[i].fd == fd_frt) {
520 handle_frt(pfd[i].fd, pfd, conns, nbconn);
521 continue;
522 }
523
524 handle_bck(pfd[i].fd, pfd, conns, nbconn);
525 }
526 }
527}