From 52e5181c841580b582e44dbdbdf08df533637d07 Mon Sep 17 00:00:00 2001 From: RipleyTom Date: Thu, 18 Nov 2021 01:02:53 +0100 Subject: [PATCH] Add function to parse IPv4 addresses PR #2807 Signed-off-by: Romain Vimont --- app/src/util/net.c | 13 +++++++++++++ app/src/util/net.h | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/app/src/util/net.c b/app/src/util/net.c index 8595bc79..824d4886 100644 --- a/app/src/util/net.c +++ b/app/src/util/net.c @@ -7,6 +7,7 @@ #include "log.h" #ifdef __WINDOWS__ +# include typedef int socklen_t; typedef SOCKET sc_raw_socket; #else @@ -225,3 +226,15 @@ net_close(sc_socket socket) { return !close(raw_sock); #endif } + +bool +net_parse_ipv4(const char *s, uint32_t *ipv4) { + struct in_addr addr; + if (!inet_pton(AF_INET, s, &addr)) { + LOGE("Invalid IPv4 address: %s", s); + return false; + } + + *ipv4 = ntohl(addr.s_addr); + return true; +} diff --git a/app/src/util/net.h b/app/src/util/net.h index 57fd6c5e..15979cf9 100644 --- a/app/src/util/net.h +++ b/app/src/util/net.h @@ -68,4 +68,10 @@ net_interrupt(sc_socket socket); bool net_close(sc_socket socket); +/** + * Parse `ip` "xxx.xxx.xxx.xxx" to an IPv4 host representation + */ +bool +net_parse_ipv4(const char *ip, uint32_t *ipv4); + #endif