DEV: udp: switch parser to getopt() instead of positional arguments

In order to ease addition of new types of perturbations to udp-perturb,
let's first switch to getopt() and get rid of the positional arguments.
The random seed was already a conditional option of the rate which was
a conditional option as well. We add -r and -s for the rate and the
seed, and new options will follow.
This commit is contained in:
Willy Tarreau 2022-03-16 14:49:33 +01:00
parent 53fa787a07
commit 04d3c5cd1f

View File

@ -36,6 +36,7 @@
#include <netdb.h> #include <netdb.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <getopt.h>
#include <signal.h> #include <signal.h>
#include <stdarg.h> #include <stdarg.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -409,31 +410,53 @@ int handle_bck(int fd, struct pollfd *pfd, struct conn *conns, int nbconn)
return ret; return ret;
} }
/* print the usage message for program named <name> and exit with status <status> */
void usage(int status, const char *name)
{
if (strchr(name, '/'))
name = strrchr(name, '/') + 1;
die(status,
"Usage: %s [-h] [options] [<laddr>:]<lport> [<saddr>:]<sport>\n"
"Options:\n"
" -h display this help\n"
" -r rate reorder/duplicate/lose around <rate>%% of packets\n"
" -s seed force initial random seed (currently %#x)\n"
"", name, prng_state);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct errmsg err; struct errmsg err;
struct pollfd *pfd; struct pollfd *pfd;
int opt;
int i; int i;
err.len = 0; err.len = 0;
err.size = 100; err.size = 100;
err.msg = malloc(err.size); err.msg = malloc(err.size);
if (argc < 3) while ((opt = getopt(argc, argv, "hr:s:")) != -1) {
die(1, "Usage: %s [<laddr>:]<lport> [<saddr>:]<sport> [rand_rate%% [seed]]\n", argv[0]); switch (opt) {
case 'r': // rand_rate%
rand_rate = atoi(optarg);
break;
case 's': // seed
prng_state = atol(optarg);
break;
default: // help, anything else
usage(0, argv[0]);
}
}
if (addr_to_ss(argv[1], &frt_addr, &err) < 0) if (argc - optind < 2)
usage(1, argv[0]);
if (addr_to_ss(argv[optind], &frt_addr, &err) < 0)
die(1, "parsing listen address: %s\n", err.msg); die(1, "parsing listen address: %s\n", err.msg);
if (addr_to_ss(argv[2], &srv_addr, &err) < 0) if (addr_to_ss(argv[optind+1], &srv_addr, &err) < 0)
die(1, "parsing server address: %s\n", err.msg); die(1, "parsing server address: %s\n", err.msg);
if (argc > 3)
rand_rate = atoi(argv[3]);
if (argc > 4)
prng_state = atol(argv[4]);
pfd = calloc(sizeof(struct pollfd), MAXCONN + 1); pfd = calloc(sizeof(struct pollfd), MAXCONN + 1);
if (!pfd) if (!pfd)
die(1, "out of memory\n"); die(1, "out of memory\n");