From: Laurent MAZET Date: Tue, 30 Sep 2025 08:53:20 +0000 (+0200) Subject: add options for udp test X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=1e2f7fe9625d4f10d0c73743c7e0d88100ffbe10;p=benchmarks.git add options for udp test --- diff --git a/test.c b/test.c index 1754eb0..48e6407 100644 --- a/test.c +++ b/test.c @@ -68,9 +68,12 @@ int main (int argc, char *argv[]) while (argc-- > 1) { char *arg = *(++argv); - if ((arg[0] != '-') && ((!parse_arg_ext) || (parse_arg_ext (arg)))) { - fprintf (stderr, "%s: invalid option -- %s\n", progname, arg); - return usage (1); + if (arg[0] != '-') { + if ((!parse_arg_ext) || (parse_arg_ext (arg))) { + fprintf (stderr, "%s: invalid option -- %s\n", progname, arg); + return usage (1); + } + continue; } char c = arg[1]; switch (c) { diff --git a/udp.c b/udp.c index 69a4f45..a66511a 100644 --- a/udp.c +++ b/udp.c @@ -21,18 +21,44 @@ dts_t *deltas = NULL; int nb_measurements = 0; + +char *local_host = "localhost"; +int local_port = 1024; +char *remote_host = "localhost"; +int remote_port = 1025; int mode = 0; char *message = "udp socket"; void _usage_ext (FILE *fd) { - fprintf (fd, " localip localport remoteip remoteport [mode]\n"); - fprintf (fd, " mode (%d): 0 for ping-pong, 1 for ping, 2 for pong\n", mode); + fprintf (fd, "... lhost lport rhost rport [mode]\n"); + fprintf (fd, " lhost: local host name (%s)\n", local_host); + fprintf (fd, " lport: local port number (%d)\n", local_port); + fprintf (fd, " rhost: remote host name (%s)\n", remote_host); + fprintf (fd, " rport: remote port number (%d)\n", remote_port); + fprintf (fd, " mode: 0 for ping-pong, 1 for ping, 2 for pong (%d)\n", mode); } void (*usage_ext) (FILE *) = _usage_ext; -int (*parse_arg_ext) (char *) = NULL; +int _parse_arg_ext (char *arg) +{ + static int narg = 0; + + int rc = 0; + switch (narg) { + case 0: local_host= arg; break; + case 1: local_port = atoi (arg); if (local_port < 0) rc = 1; break; + case 2: remote_host = arg; break; + case 3: remote_port = atoi (arg); if (remote_port < 0) rc = 1; break; + case 4: mode = atoi (arg); break; + default: rc = 1; + } + narg++; + + return rc; +} +int (*parse_arg_ext) (char *) = _parse_arg_ext; #define MAXBUF 1024 @@ -152,10 +178,6 @@ int get_ip (char *hostname) { int test (dts_t *buffer, int nb) { - char *local_host = "localhost"; - int local_port = 1024; - char *remote_host = "localhost"; - int remote_port = 1025; /* set global variables */ @@ -171,19 +193,19 @@ int test (dts_t *buffer, int nb) if (remote_ip == -1) { return 1; } - - pid_t pid = fork (); + + pid_t pid = (mode == 0 ) ? fork () : -2; if (pid == -1) { fprintf (stderr, "error: fork\n"); return 1; - } else if (pid == 0) { + } else if ((pid == 0) || (mode == 2)) { int rc = pong (remote_port, local_ip, local_port); exit (rc); } int rc = ping (local_port, remote_ip, remote_port); - if (kill (pid, SIGTERM) == 0) { + if ((pid > 0) && (kill (pid, SIGTERM) == 0)) { int wstatus; if (waitpid (pid, &wstatus, WUNTRACED | WCONTINUED) == -1) { fprintf (stderr, "error: waitpid\n");