Skip to content

Commit

Permalink
Add function to parse IPv4 addresses
Browse files Browse the repository at this point in the history
PR #2807 <#2807>

Signed-off-by: Romain Vimont <[email protected]>
  • Loading branch information
RipleyTom authored and rom1v committed Nov 19, 2021
1 parent 2d6a967 commit d290b44
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/src/util/net.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// For inet_pton() on Windows
#define _WIN32_WINNT 0x0600
#define WINVER 0x0600

#include "net.h"

#include <assert.h>
Expand All @@ -7,6 +11,7 @@
#include "log.h"

#ifdef __WINDOWS__
# include <ws2tcpip.h>
typedef int socklen_t;
typedef SOCKET sc_raw_socket;
#else
Expand Down Expand Up @@ -225,3 +230,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;
}
6 changes: 6 additions & 0 deletions app/src/util/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d290b44

Please sign in to comment.