From ffcb07db1c2017ab6537eeba14d1a9cedfa31bb0 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Mon, 25 Jan 2021 14:09:02 +0000 Subject: [PATCH] Parse IP address which we need in client mode Signed-off-by: Michael Tremer --- src/main.c | 34 ++++++++++++++++++++++++++++++++++ src/main.h | 3 +++ 2 files changed, 37 insertions(+) diff --git a/src/main.c b/src/main.c index aeb3a97..fae9b7b 100644 --- a/src/main.c +++ b/src/main.c @@ -18,13 +18,40 @@ # # #############################################################################*/ +#include #include +#include #include #include #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': diff --git a/src/main.h b/src/main.h index 9e0f629..9349036 100644 --- a/src/main.h +++ b/src/main.h @@ -21,6 +21,8 @@ #ifndef FIREPERF_MAIN_H #define FIREPERF_MAIN_H +#include + #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; }; -- 2.47.2