From a86c5f7cae7ec9a3398300555a0b644689d946a1 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 19 Sep 2024 06:14:53 +0200 Subject: Merging upstream version 4.4.0. Signed-off-by: Daniel Baumann --- wsutil/inet_addr.h | 94 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 85 insertions(+), 9 deletions(-) (limited to 'wsutil/inet_addr.h') diff --git a/wsutil/inet_addr.h b/wsutil/inet_addr.h index 7147c2ae..6c7feb9b 100644 --- a/wsutil/inet_addr.h +++ b/wsutil/inet_addr.h @@ -12,8 +12,79 @@ #include -#include "inet_ipv4.h" -#include "inet_ipv6.h" +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef uint32_t ws_in4_addr; /* 32 bit IPv4 address, in network byte order */ + +typedef struct e_in6_addr { + uint8_t bytes[16]; /* 128 bit IPv6 address */ +} ws_in6_addr; + + +/** + * Unicast Local + * Returns true if the address is in the 224.0.0.0/24 local network + * control block + */ +#define in4_addr_is_local_network_control_block(addr) \ + ((addr & 0xffffff00) == 0xe0000000) + +/** + * Multicast + * Returns true if the address is in the 224.0.0.0/4 network block + */ +#define in4_addr_is_multicast(addr) \ + ((addr & 0xf0000000) == 0xe0000000) + +/** + * Private address + * Returns true if the address is in one of the three blocks reserved + * for private IPv4 addresses by section 3 of RFC 1918, namely: + * 10/8, 172.16/12, and 192.168/16 + */ +#define in4_addr_is_private(addr) \ + (((addr & 0xff000000) == 0x0a000000) || \ + ((addr & 0xfff00000) == 0xac100000) || \ + ((addr & 0xffff0000) == 0xc0a80000)) + +/** + * Link-local address + * Returns true if the address is in the 169.254/16 network block + */ +#define in4_addr_is_link_local(addr) \ + ((addr & 0xffff0000) == 0xa9fe0000) + +/** + * Unicast Scope + * Note that we must check topmost 10 bits only, not 16 bits (see RFC2373). + */ +static inline bool +in6_addr_is_linklocal(const ws_in6_addr *a) +{ + return (a->bytes[0] == 0xfe) && ((a->bytes[1] & 0xc0) == 0x80); +} + +static inline bool +in6_addr_is_sitelocal(const ws_in6_addr *a) +{ + return (a->bytes[0] == 0xfe) && ((a->bytes[1] & 0xc0) == 0xc0); +} + +static inline bool in6_addr_is_uniquelocal(const ws_in6_addr *a) +{ + return (a->bytes[0] & 0xfe) == 0xfc; +} + +/** + * Multicast + */ +static inline bool +in6_addr_is_multicast(const ws_in6_addr *a) +{ + return a->bytes[0] == 0xff; +} /* * These are the values specified by RFC 2133 and its successors for @@ -48,24 +119,29 @@ #define WS_INET_ADDRSTRLEN 16 #define WS_INET6_ADDRSTRLEN 46 -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ +/* + * Utility for CIDR notation of subnets + */ +#define WS_INET_CIDRADDRSTRLEN 19 /* * To check for errors set errno to zero before calling ws_inet_ntop{4,6}. * ENOSPC is set if the result exceeds the given buffer size. */ -WS_DLL_PUBLIC WS_RETNONNULL const char * +WS_DLL_PUBLIC WS_RETNONNULL +const char * ws_inet_ntop4(const void *src, char *dst, size_t dst_size); -WS_DLL_PUBLIC WS_RETNONNULL const char * +WS_DLL_PUBLIC WS_RETNONNULL +const char * ws_inet_ntop6(const void *src, char *dst, size_t dst_size); -WS_DLL_PUBLIC bool +WS_DLL_PUBLIC +bool ws_inet_pton4(const char *src, ws_in4_addr *dst); -WS_DLL_PUBLIC bool +WS_DLL_PUBLIC +bool ws_inet_pton6(const char *src, ws_in6_addr *dst); #ifdef __cplusplus -- cgit v1.2.3