20 #ifndef UTIL_IP_ADDRESS_H
21 #define UTIL_IP_ADDRESS_H
25 #include <libkern/OSByteOrder.h>
26 #define __bswap_constant_32(x) OSSwapInt32(x)
27 #include <machine/endian.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
48 #define bytes_to_bits(x) (x * 8)
49 #define IPV6_ADDR_LEN 16
50 #define IPV4_ADDR_LEN 4
51 #define IPV6_ADDR_LEN_BITS bytes_to_bits(IPV6_ADDR_LEN)
52 #define IPV4_ADDR_LEN_BITS bytes_to_bits(IPV4_ADDR_LEN)
55 #ifndef INET_ADDRSTRLEN
56 #define INET_ADDRSTRLEN 16
59 #ifndef INET6_ADDRSTRLEN
60 #define INET6_ADDRSTRLEN 46
64 #define IP_MAX_ADDR_LEN IPV6_ADDR_LEN
66 #define DUMMY_PORT 1234
72 struct in_addr as_inaddr;
80 struct in6_addr as_in6addr;
88 u8 buffer[IP_MAX_ADDR_LEN];
89 u8 as_u8[IP_MAX_ADDR_LEN];
90 u16 as_u16[IP_MAX_ADDR_LEN >> 1];
91 u32 as_u32[IP_MAX_ADDR_LEN >> 2];
92 u64 as_u64[IP_MAX_ADDR_LEN >> 3];
96 #define MAXSZ_IP4_ADDRESS_ INET_ADDRSTRLEN - 1
97 #define MAXSZ_IP6_ADDRESS_ INET6_ADDRSTRLEN - 1
98 #define MAXSZ_IP_ADDRESS_ MAXSZ_IP6_ADDRESS_
99 #define MAXSZ_IP4_ADDRESS MAXSZ_IP4_ADDRESS_ + 1
100 #define MAXSZ_IP6_ADDRESS MAXSZ_IP6_ADDRESS_ + 1
101 #define MAXSZ_IP_ADDRESS MAXSZ_IP_ADDRESS_ + 1
109 #define MAXSZ_PREFIX_ MAXSZ_IP_ADDRESS_ + 1 + 3
110 #define MAXSZ_PREFIX MAXSZ_PREFIX_ + 1
118 #define IP_ANY(family) (family == AF_INET) ? IPV4_ANY : IPV6_ANY
121 #define MAX_PORT 1 << (8 * sizeof(u16))
122 #define IS_VALID_PORT(x) ((x > 0) && ((int)x < MAX_PORT))
124 #define MAXSZ_PORT_ 5
125 #define MAXSZ_PORT MAXSZ_PORT_ + 1
127 #define IS_VALID_FAMILY(x) ((x == AF_INET) || (x == AF_INET6))
131 int ip_address_get_family (
const char * ip_address);
132 int ip_address_len (
int family);
133 const u8 * ip_address_get_buffer(
const ip_address_t * ip_address,
int family);
134 int ip_address_ntop (
const ip_address_t * ip_address,
char *dst,
135 const size_t len,
int family);
136 int ip_address_pton (
const char *ip_address_str,
ip_address_t * ip_address);
137 int ip_address_snprintf(
char * s,
size_t size,
const ip_address_t * ip_address,
139 int ip_address_to_sockaddr(
const ip_address_t * ip_address,
struct sockaddr *sa,
146 int ip_prefix_pton (
const char *ip_address_str,
ip_prefix_t * ip_prefix);
147 int ip_prefix_ntop_short (
const ip_prefix_t * ip_prefix,
char *dst,
size_t size);
148 int ip_prefix_ntop (
const ip_prefix_t * ip_prefix,
char *dst,
size_t size);
151 int ip_prefix_to_sockaddr(
const ip_prefix_t * prefix,
struct sockaddr *sa);
156 #define MAXSZ_PROTO_ 8
157 #define MAXSZ_PROTO MAXSZ_PROTO_ + NULLTERM
159 #define MAXSZ_URL4_ MAXSZ_PROTO_ + MAXSZ_IP4_ADDRESS_ + MAXSZ_PORT_
160 #define MAXSZ_URL6_ MAXSZ_PROTO_ + MAXSZ_IP6_ADDRESS_ + MAXSZ_PORT_
161 #define MAXSZ_URL_ MAXSZ_URL6_
162 #define MAXSZ_URL4 MAXSZ_URL4_ + NULLTERM
163 #define MAXSZ_URL6 MAXSZ_URL6_ + NULLTERM
164 #define MAXSZ_URL MAXSZ_URL_ + NULLTERM
166 int url_snprintf(
char * s,
size_t size,
int family,