diff --git a/dev/udp/udp-perturb.c b/dev/udp/udp-perturb.c index 824628d36..3df0f797f 100644 --- a/dev/udp/udp-perturb.c +++ b/dev/udp/udp-perturb.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -409,31 +410,53 @@ int handle_bck(int fd, struct pollfd *pfd, struct conn *conns, int nbconn) return ret; } +/* print the usage message for program named and exit with status */ +void usage(int status, const char *name) +{ + if (strchr(name, '/')) + name = strrchr(name, '/') + 1; + die(status, + "Usage: %s [-h] [options] [:] [:]\n" + "Options:\n" + " -h display this help\n" + " -r rate reorder/duplicate/lose around %% of packets\n" + " -s seed force initial random seed (currently %#x)\n" + "", name, prng_state); +} + int main(int argc, char **argv) { struct errmsg err; struct pollfd *pfd; + int opt; int i; err.len = 0; err.size = 100; err.msg = malloc(err.size); - if (argc < 3) - die(1, "Usage: %s [:] [:] [rand_rate%% [seed]]\n", argv[0]); + while ((opt = getopt(argc, argv, "hr:s:")) != -1) { + 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); - 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); - if (argc > 3) - rand_rate = atoi(argv[3]); - - if (argc > 4) - prng_state = atol(argv[4]); - pfd = calloc(sizeof(struct pollfd), MAXCONN + 1); if (!pfd) die(1, "out of memory\n");