]> git.ipfire.org Git - fireperf.git/commitdiff
Parse IP address which we need in client mode
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 25 Jan 2021 14:09:02 +0000 (14:09 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 25 Jan 2021 14:09:02 +0000 (14:09 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/main.c
src/main.h

index aeb3a970efa73f797c40c38f5ab99c557ba2bc60..fae9b7b4a3c7dcd28e74cd981e7ad95813f0b4af 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <arpa/inet.h>
 #include <getopt.h>
+#include <netinet/in.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 #include "main.h"
 #include "logging.h"
 
+static int parse_address(const char* string, struct in6_addr* address6) {
+       // Try parsing this address
+       int r = inet_pton(AF_INET6, string, address6);
+
+       // Success!
+       if (r == 1)
+               return 0;
+
+       // Try parsing this as an IPv4 address
+       struct in_addr address4;
+       r = inet_pton(AF_INET, string, &address4);
+       if (r == 1) {
+               // Convert to IPv6-mapped address
+               address6->s6_addr32[0] = htonl(0x0000);
+               address6->s6_addr32[1] = htonl(0x0000);
+               address6->s6_addr32[2] = htonl(0xffff);
+               address6->s6_addr32[3] = address4.s_addr;
+
+               return 0;
+       }
+
+       // Could not parse this
+       return 1;
+}
+
 static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) {
        static struct option long_options[] = {
                {"client",     required_argument, 0, 'c'},
@@ -63,6 +90,13 @@ static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) {
 
                        case 'c':
                                conf->mode = FIREPERF_MODE_CLIENT;
+
+                               // Parse the given IP address
+                               int r = parse_address(optarg, &conf->address);
+                               if (r) {
+                                       fprintf(stderr, "Could not parse IP address %s\n", optarg);
+                                       return 2;
+                               }
                                break;
 
                        case 'd':
index 9e0f6296bb0d7242b24c197fbf50d1b68df00188..9349036e814b95384ee2598031935bb7bf984042 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef FIREPERF_MAIN_H
 #define FIREPERF_MAIN_H
 
+#include <netinet/in.h>
+
 #define DEFAULT_LOG_LEVEL LOG_INFO
 #define DEFAULT_PORT 5001
 
@@ -31,6 +33,7 @@ struct fireperf_config {
                FIREPERF_MODE_CLIENT,
                FIREPERF_MODE_SERVER,
        } mode;
+       struct in6_addr address;
        int port;
 };