add options for udp test
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Tue, 30 Sep 2025 08:53:20 +0000 (10:53 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Tue, 30 Sep 2025 08:53:20 +0000 (10:53 +0200)
test.c
udp.c

diff --git a/test.c b/test.c
index 1754eb0a75e42da1b82e93064641363c14d31ef0..48e6407bca8356b45b9bf7469b6d507fce9ac417 100644 (file)
--- 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 69a4f45ab01eb989cb150d6456f6aee1096ac54f..a66511afb198d009cf461cebf8bc63396885cd8a 100644 (file)
--- a/udp.c
+++ b/udp.c
 
 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");