1/*-------------------------------------------------------------------------
4 * IP netmask calculations, and enumerating network interfaces.
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/libpq/ifaddr.c
13 * This file and the IPV6 implementation were initially provided by
14 * Nigel Kukard <nkukard@lbsd.net>, Linux Based Systems Design
15 * http://www.lbsd.net.
17 *-------------------------------------------------------------------------
34 const struct sockaddr_in *netaddr,
35 const struct sockaddr_in *netmask);
38 const struct sockaddr_in6 *netaddr,
39 const struct sockaddr_in6 *netmask);
43 * pg_range_sockaddr - is addr within the subnet specified by netaddr/netmask ?
45 * Note: caller must already have verified that all three addresses are
46 * in the same address family; and AF_UNIX addresses are not supported.
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)
95 * pg_sockaddr_cidr_mask - make a network mask of the appropriate family
96 * and required number of significant bits
98 * numbits can be null, in which case the mask is fully set.
100 * The resulting mask is placed in *mask, which had better be big enough.
102 * Return value is 0 if okay, -1 if not.
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));
131 /* avoid "x << 32", which is not portable */
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;
177 * Run the callback function for the addr/mask, after making sure the
178 * mask is sane for the addr.
182 struct sockaddr *addr,
struct sockaddr *mask)
184 struct sockaddr_storage fullmask;
189 /* Check that the mask is valid */
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))
208 /* If mask is invalid, generate our own fully-set mask */
212 mask = (
struct sockaddr *) &fullmask;
215 (*callback) (addr, mask, cb_data);
224 * Enumerate the system's network interface addresses and call the callback
225 * for each one. Returns 0 if successful, -1 if trouble.
227 * This version is for Win32. Uses the Winsock 2 functions (ie: ws2_32.dll)
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)
263 continue;
/* need to make the buffer bigger */
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 /* && !WIN32 */
288 * Enumerate the system's network interface addresses and call the callback
289 * for each one. Returns 0 if successful, -1 if trouble.
291 * This version uses the getifaddrs() interface, which is available on
292 * BSDs, macOS, Solaris, illumos and Linux.
300 if (getifaddrs(&ifa) < 0)
303 for (l = ifa; l; l = l->ifa_next)
305 l->ifa_addr, l->ifa_netmask);
310#else /* !HAVE_GETIFADDRS && !WIN32 */
312#include <sys/ioctl.h>
315#if defined(SIOCGIFCONF)
318 * Remaining Unixes use SIOCGIFCONF. Some only return IPv4 information
319 * here, so this is the least preferred method. Note that there is no
320 * standard way to iterate the struct ifreq returned in the array.
321 * On some OSs the structures are padded large enough for any address,
322 * on others you have to calculate the size of the struct ifreq.
325/* Some OSs have _SIZEOF_ADDR_IFREQ, so just use that */
326#ifndef _SIZEOF_ADDR_IFREQ
328/* Calculate based on sockaddr.sa_len */
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))
335/* Padded ifreq structure, simple */
337#define _SIZEOF_ADDR_IFREQ(ifr) \
338 sizeof (struct ifreq)
340#endif /* !_SIZEOF_ADDR_IFREQ */
343 * Enumerate the system's network interface addresses and call the callback
344 * for each one. Returns 0 if successful, -1 if trouble.
346 * This version uses ioctl(SIOCGIFCONF).
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)
391 * Some Unixes try to return as much data as possible, with no
392 * indication of whether enough space allocated. Don't believe we have
393 * it all unless there's lots of slop.
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));
415#else /* !defined(SIOCGIFCONF) */
418 * Enumerate the system's network interface addresses and call the callback
419 * for each one. Returns 0 if successful, -1 if trouble.
421 * This version is our fallback if there's no known way to get the
422 * interface addresses. Just return the standard loopback addresses.
427 struct sockaddr_in addr;
428 struct sockaddr_storage mask;
429 struct sockaddr_in6 addr6;
431 /* addr 127.0.0.1/8 */
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);
453#endif /* !defined(SIOCGIFCONF) */
455#endif /* !HAVE_GETIFADDRS */
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)