]> git.ipfire.org Git - fireperf.git/commitdiff
Make port configurable
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 25 Jan 2021 13:06:41 +0000 (13:06 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 25 Jan 2021 13:06:41 +0000 (13:06 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
configure.ac
src/main.c
src/main.h

index b519fc62d45adb4eb1a31b3dfb8d839b04acbf73..35feb9b9efadd4f198f84780a07f53ae98ab8192 100644 (file)
@@ -66,6 +66,7 @@ AC_CHECK_HEADERS_ONCE([
        getopt.h \
        netinet/in.h \
        stdio.h \
+       stdlib.h \
        string.h \
 ])
 
index c31e2c4bc1376047c2dcb451a9027c354802fdd1..0cc8578f9af7f212f3ff45105151246f59867a09 100644 (file)
 
 #include <getopt.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 #include "main.h"
 
 static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) {
        static struct option long_options[] = {
                {"client",     required_argument, 0, 'c'},
+               {"port",       required_argument, 0, 'p'},
                {"server",     no_argument,       0, 's'},
                {0, 0, 0, 0},
        };
@@ -34,7 +36,7 @@ static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) {
        int done = 0;
 
        while (!done) {
-               int c = getopt_long(argc, argv, "c:s", long_options, &option_index);
+               int c = getopt_long(argc, argv, "c:p:s", long_options, &option_index);
 
                // End
                if (c == -1)
@@ -61,6 +63,16 @@ static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) {
                                conf->mode = FIREPERF_MODE_CLIENT;
                                break;
 
+                       case 'p':
+                               conf->port = atoi(optarg);
+
+                               // Validate input
+                               if (conf->port <= 0 || conf->port >= 65536) {
+                                       fprintf(stderr, "Invalid port number: %u\n", conf->port);
+                                       return 2;
+                               }
+                               break;
+
                        case 's':
                                conf->mode = FIREPERF_MODE_SERVER;
                                break;
index 22c900f6ac36f1e2d63a1f681c85a4b708f8f9e1..4a84771d778fd5f7c7a904d5984d3e5db8a33f5d 100644 (file)
@@ -24,4 +24,5 @@ struct fireperf_config {
                FIREPERF_MODE_CLIENT,
                FIREPERF_MODE_SERVER,
        } mode;
+       int port;
 };