34 const struct sockaddr_in *netaddr,
35 const struct sockaddr_in *netmask);
38 const struct sockaddr_in6 *netaddr,
39 const struct sockaddr_in6 *netmask);
50 const struct sockaddr_storage *netaddr,
51 const struct sockaddr_storage *netmask)
53 if (addr->ss_family == AF_INET)
55 (
const struct sockaddr_in *) netaddr,
56 (
const struct sockaddr_in *) netmask);
57 else if (addr->ss_family == AF_INET6)
59 (
const struct sockaddr_in6 *) netaddr,
60 (
const struct sockaddr_in6 *) netmask);
67 const struct sockaddr_in *netaddr,
68 const struct sockaddr_in *netmask)
70 if (((addr->sin_addr.s_addr ^ netaddr->sin_addr.s_addr) &
71 netmask->sin_addr.s_addr) == 0)
79 const struct sockaddr_in6 *netaddr,
80 const struct sockaddr_in6 *netmask)
84 for (
i = 0;
i < 16;
i++)
86 if (((addr->sin6_addr.s6_addr[
i] ^ netaddr->sin6_addr.s6_addr[
i]) &
87 netmask->sin6_addr.s6_addr[
i]) != 0)
112 bits = (family == AF_INET) ? 32 : 128;
116 bits = strtol(numbits, &endptr, 10);
117 if (*numbits ==
'\0' || *endptr !=
'\0')
125 struct sockaddr_in mask4;
128 if (bits < 0 || bits > 32)
130 memset(&mask4, 0,
sizeof(mask4));
133 maskl = (0xffffffffUL << (32 - (int) bits))
137 mask4.sin_addr.s_addr =
pg_hton32(maskl);
138 memcpy(mask, &mask4,
sizeof(mask4));
144 struct sockaddr_in6 mask6;
147 if (bits < 0 || bits > 128)
149 memset(&mask6, 0,
sizeof(mask6));
150 for (
i = 0;
i < 16;
i++)
153 mask6.sin6_addr.s6_addr[
i] = 0;
155 mask6.sin6_addr.s6_addr[
i] = 0xff;
158 mask6.sin6_addr.s6_addr[
i] =
159 (0xff << (8 - (int) bits)) & 0xff;
163 memcpy(mask, &mask6,
sizeof(mask6));
171 mask->ss_family = family;
182 struct sockaddr *addr,
struct sockaddr *mask)
184 struct sockaddr_storage fullmask;
192 if (mask->sa_family != addr->sa_family)
196 else if (mask->sa_family == AF_INET)
198 if (((
struct sockaddr_in *) mask)->sin_addr.s_addr == INADDR_ANY)
201 else if (mask->sa_family == AF_INET6)
203 if (IN6_IS_ADDR_UNSPECIFIED(&((
struct sockaddr_in6 *) mask)->sin6_addr))
212 mask = (
struct sockaddr *) &fullmask;
215 (*callback) (addr, mask, cb_data);
220 #include <winsock2.h>
221 #include <ws2tcpip.h>
234 unsigned long length,
236 unsigned long n_ii = 0;
240 sock = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
241 if (sock == INVALID_SOCKET)
247 ptr =
realloc(ii,
sizeof(INTERFACE_INFO) * n_ii);
257 if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST, 0, 0,
258 ii, n_ii *
sizeof(INTERFACE_INFO),
259 &length, 0, 0) == SOCKET_ERROR)
261 error = WSAGetLastError();
262 if (
error == WSAEFAULT ||
error == WSAENOBUFS)
272 for (
i = 0;
i < length /
sizeof(INTERFACE_INFO); ++
i)
274 (
struct sockaddr *) &ii[
i].iiAddress,
275 (
struct sockaddr *) &ii[
i].iiNetmask);
281 #elif HAVE_GETIFADDRS
283 #ifdef HAVE_IFADDRS_H
300 if (getifaddrs(&ifa) < 0)
303 for (l = ifa; l; l = l->ifa_next)
305 l->ifa_addr, l->ifa_netmask);
312 #include <sys/ioctl.h>
315 #if defined(SIOCGIFCONF)
326 #ifndef _SIZEOF_ADDR_IFREQ
329 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
330 #define _SIZEOF_ADDR_IFREQ(ifr) \
331 ((ifr).ifr_addr.sa_len > sizeof(struct sockaddr) ? \
332 (sizeof(struct ifreq) - sizeof(struct sockaddr) + \
333 (ifr).ifr_addr.sa_len) : sizeof(struct ifreq))
337 #define _SIZEOF_ADDR_IFREQ(ifr) \
338 sizeof (struct ifreq)
358 size_t n_buffer = 1024;
361 sock =
socket(AF_INET, SOCK_DGRAM, 0);
365 while (n_buffer < 1024 * 100)
368 ptr =
realloc(buffer, n_buffer);
377 memset(&ifc, 0,
sizeof(ifc));
378 ifc.ifc_buf = buffer = ptr;
379 ifc.ifc_len = n_buffer;
381 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0)
395 if (ifc.ifc_len < n_buffer - 1024)
399 end = (
struct ifreq *) (buffer + ifc.ifc_len);
400 for (ifr = ifc.ifc_req; ifr < end;)
402 memcpy(&addr, ifr,
sizeof(addr));
403 memcpy(&mask, ifr,
sizeof(mask));
404 if (ioctl(sock, SIOCGIFADDR, &addr,
sizeof(addr)) == 0 &&
405 ioctl(sock, SIOCGIFNETMASK, &mask,
sizeof(mask)) == 0)
407 &addr.ifr_addr, &mask.ifr_addr);
408 ifr = (
struct ifreq *) ((
char *) ifr + _SIZEOF_ADDR_IFREQ(*ifr));
427 struct sockaddr_in addr;
428 struct sockaddr_storage mask;
429 struct sockaddr_in6 addr6;
432 memset(&addr, 0,
sizeof(addr));
433 addr.sin_family = AF_INET;
434 addr.sin_addr.s_addr =
pg_ntoh32(0x7f000001);
435 memset(&mask, 0,
sizeof(mask));
438 (
struct sockaddr *) &addr,
439 (
struct sockaddr *) &mask);
442 memset(&addr6, 0,
sizeof(addr6));
443 addr6.sin6_family = AF_INET6;
444 addr6.sin6_addr.s6_addr[15] = 1;
445 memset(&mask, 0,
sizeof(mask));
448 (
struct sockaddr *) &addr6,
449 (
struct sockaddr *) &mask);
static int range_sockaddr_AF_INET6(const struct sockaddr_in6 *addr, const struct sockaddr_in6 *netaddr, const struct sockaddr_in6 *netmask)
int pg_sockaddr_cidr_mask(struct sockaddr_storage *mask, char *numbits, int family)
int pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
static int range_sockaddr_AF_INET(const struct sockaddr_in *addr, const struct sockaddr_in *netaddr, const struct sockaddr_in *netmask)
static void run_ifaddr_callback(PgIfAddrCallback callback, void *cb_data, struct sockaddr *addr, struct sockaddr *mask)
int pg_range_sockaddr(const struct sockaddr_storage *addr, const struct sockaddr_storage *netaddr, const struct sockaddr_storage *netmask)
void(* PgIfAddrCallback)(struct sockaddr *addr, struct sockaddr *netmask, void *cb_data)
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
#define socket(af, type, protocol)