1 /*-------------------------------------------------------------------------
4 * IPv6-aware network access.
6 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/libpq/ip.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 *-------------------------------------------------------------------------
20 /* This is intended to be used in both frontend and backend, so use c.h */
24 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #ifdef HAVE_NETINET_TCP_H
30 #include <netinet/tcp.h>
32 #include <arpa/inet.h>
38 static int range_sockaddr_AF_INET(const struct sockaddr_in * addr,
39 const struct sockaddr_in * netaddr,
40 const struct sockaddr_in * netmask);
43 static int range_sockaddr_AF_INET6(const struct sockaddr_in6 * addr,
44 const struct sockaddr_in6 * netaddr,
45 const struct sockaddr_in6 * netmask);
48 #ifdef HAVE_UNIX_SOCKETS
49 static int getaddrinfo_unix(const char *path,
50 const struct addrinfo * hintsp,
51 struct addrinfo ** result);
53 static int getnameinfo_unix(const struct sockaddr_un * sa, int salen,
54 char *node, int nodelen,
55 char *service, int servicelen,
61 * pg_getaddrinfo_all - get address info for Unix, IPv4 and IPv6 sockets
64 pg_getaddrinfo_all(const char *hostname, const char *servname,
65 const struct addrinfo * hintp, struct addrinfo ** result)
69 /* not all versions of getaddrinfo() zero *result on failure */
72 #ifdef HAVE_UNIX_SOCKETS
73 if (hintp->ai_family == AF_UNIX)
74 return getaddrinfo_unix(servname, hintp, result);
77 /* NULL has special meaning to getaddrinfo(). */
78 rc = getaddrinfo((!hostname || hostname[0] == '\0') ? NULL : hostname,
79 servname, hintp, result);
86 * pg_freeaddrinfo_all - free addrinfo structures for IPv4, IPv6, or Unix
88 * Note: the ai_family field of the original hint structure must be passed
89 * so that we can tell whether the addrinfo struct was built by the system's
90 * getaddrinfo() routine or our own getaddrinfo_unix() routine. Some versions
91 * of getaddrinfo() might be willing to return AF_UNIX addresses, so it's
92 * not safe to look at ai_family in the addrinfo itself.
95 pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo * ai)
97 #ifdef HAVE_UNIX_SOCKETS
98 if (hint_ai_family == AF_UNIX)
100 /* struct was built by getaddrinfo_unix (see pg_getaddrinfo_all) */
103 struct addrinfo *p = ai;
111 #endif /* HAVE_UNIX_SOCKETS */
113 /* struct was built by getaddrinfo() */
121 * pg_getnameinfo_all - get name info for Unix, IPv4 and IPv6 sockets
123 * The API of this routine differs from the standard getnameinfo() definition
124 * in two ways: first, the addr parameter is declared as sockaddr_storage
125 * rather than struct sockaddr, and second, the node and service fields are
126 * guaranteed to be filled with something even on failure return.
129 pg_getnameinfo_all(const struct sockaddr_storage * addr, int salen,
130 char *node, int nodelen,
131 char *service, int servicelen,
136 #ifdef HAVE_UNIX_SOCKETS
137 if (addr && addr->ss_family == AF_UNIX)
138 rc = getnameinfo_unix((const struct sockaddr_un *) addr, salen,
144 rc = getnameinfo((const struct sockaddr *) addr, salen,
152 strlcpy(node, "???", nodelen);
154 strlcpy(service, "???", servicelen);
161 #if defined(HAVE_UNIX_SOCKETS)
164 * getaddrinfo_unix - get unix socket info using IPv6-compatible API
166 * Bugs: only one addrinfo is set even though hintsp is NULL or
168 * AI_CANONNAME is not supported.
172 getaddrinfo_unix(const char *path, const struct addrinfo * hintsp,
173 struct addrinfo ** result)
175 struct addrinfo hints;
176 struct addrinfo *aip;
177 struct sockaddr_un *unp;
181 MemSet(&hints, 0, sizeof(hints));
183 if (strlen(path) >= sizeof(unp->sun_path))
188 hints.ai_family = AF_UNIX;
189 hints.ai_socktype = SOCK_STREAM;
192 memcpy(&hints, hintsp, sizeof(hints));
194 if (hints.ai_socktype == 0)
195 hints.ai_socktype = SOCK_STREAM;
197 if (hints.ai_family != AF_UNIX)
199 /* shouldn't have been called */
203 aip = calloc(1, sizeof(struct addrinfo));
207 unp = calloc(1, sizeof(struct sockaddr_un));
214 aip->ai_family = AF_UNIX;
215 aip->ai_socktype = hints.ai_socktype;
216 aip->ai_protocol = hints.ai_protocol;
218 aip->ai_canonname = NULL;
221 unp->sun_family = AF_UNIX;
222 aip->ai_addr = (struct sockaddr *) unp;
223 aip->ai_addrlen = sizeof(struct sockaddr_un);
225 strcpy(unp->sun_path, path);
227 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN
228 unp->sun_len = sizeof(struct sockaddr_un);
235 * Convert an address to a hostname.
238 getnameinfo_unix(const struct sockaddr_un * sa, int salen,
239 char *node, int nodelen,
240 char *service, int servicelen,
245 /* Invalid arguments. */
246 if (sa == NULL || sa->sun_family != AF_UNIX ||
247 (node == NULL && service == NULL))
250 /* We don't support those. */
251 if ((node && !(flags & NI_NUMERICHOST))
252 || (service && !(flags & NI_NUMERICSERV)))
257 ret = snprintf(node, nodelen, "%s", "[local]");
258 if (ret == -1 || ret > nodelen)
264 ret = snprintf(service, servicelen, "%s", sa->sun_path);
265 if (ret == -1 || ret > servicelen)
271 #endif /* HAVE_UNIX_SOCKETS */
275 * pg_range_sockaddr - is addr within the subnet specified by netaddr/netmask ?
277 * Note: caller must already have verified that all three addresses are
278 * in the same address family; and AF_UNIX addresses are not supported.
281 pg_range_sockaddr(const struct sockaddr_storage * addr,
282 const struct sockaddr_storage * netaddr,
283 const struct sockaddr_storage * netmask)
285 if (addr->ss_family == AF_INET)
286 return range_sockaddr_AF_INET((const struct sockaddr_in *) addr,
287 (const struct sockaddr_in *) netaddr,
288 (const struct sockaddr_in *) netmask);
290 else if (addr->ss_family == AF_INET6)
291 return range_sockaddr_AF_INET6((const struct sockaddr_in6 *) addr,
292 (const struct sockaddr_in6 *) netaddr,
293 (const struct sockaddr_in6 *) netmask);
300 range_sockaddr_AF_INET(const struct sockaddr_in * addr,
301 const struct sockaddr_in * netaddr,
302 const struct sockaddr_in * netmask)
304 if (((addr->sin_addr.s_addr ^ netaddr->sin_addr.s_addr) &
305 netmask->sin_addr.s_addr) == 0)
315 range_sockaddr_AF_INET6(const struct sockaddr_in6 * addr,
316 const struct sockaddr_in6 * netaddr,
317 const struct sockaddr_in6 * netmask)
321 for (i = 0; i < 16; i++)
323 if (((addr->sin6_addr.s6_addr[i] ^ netaddr->sin6_addr.s6_addr[i]) &
324 netmask->sin6_addr.s6_addr[i]) != 0)
330 #endif /* HAVE_IPV6 */
333 * pg_sockaddr_cidr_mask - make a network mask of the appropriate family
334 * and required number of significant bits
336 * numbits can be null, in which case the mask is fully set.
338 * The resulting mask is placed in *mask, which had better be big enough.
340 * Return value is 0 if okay, -1 if not.
343 pg_sockaddr_cidr_mask(struct sockaddr_storage * mask, char *numbits, int family)
350 bits = (family == AF_INET) ? 32 : 128;
354 bits = strtol(numbits, &endptr, 10);
355 if (*numbits == '\0' || *endptr != '\0')
363 struct sockaddr_in mask4;
366 if (bits < 0 || bits > 32)
368 memset(&mask4, 0, sizeof(mask4));
369 /* avoid "x << 32", which is not portable */
371 maskl = (0xffffffffUL << (32 - (int) bits))
375 mask4.sin_addr.s_addr = htonl(maskl);
376 memcpy(mask, &mask4, sizeof(mask4));
383 struct sockaddr_in6 mask6;
386 if (bits < 0 || bits > 128)
388 memset(&mask6, 0, sizeof(mask6));
389 for (i = 0; i < 16; i++)
392 mask6.sin6_addr.s6_addr[i] = 0;
394 mask6.sin6_addr.s6_addr[i] = 0xff;
397 mask6.sin6_addr.s6_addr[i] =
398 (0xff << (8 - (int) bits)) & 0xff;
402 memcpy(mask, &mask6, sizeof(mask6));
410 mask->ss_family = family;
418 * pg_promote_v4_to_v6_addr --- convert an AF_INET addr to AF_INET6, using
419 * the standard convention for IPv4 addresses mapped into IPv6 world
421 * The passed addr is modified in place; be sure it is large enough to
422 * hold the result! Note that we only worry about setting the fields
423 * that pg_range_sockaddr will look at.
426 pg_promote_v4_to_v6_addr(struct sockaddr_storage * addr)
428 struct sockaddr_in addr4;
429 struct sockaddr_in6 addr6;
432 memcpy(&addr4, addr, sizeof(addr4));
433 ip4addr = ntohl(addr4.sin_addr.s_addr);
435 memset(&addr6, 0, sizeof(addr6));
437 addr6.sin6_family = AF_INET6;
439 addr6.sin6_addr.s6_addr[10] = 0xff;
440 addr6.sin6_addr.s6_addr[11] = 0xff;
441 addr6.sin6_addr.s6_addr[12] = (ip4addr >> 24) & 0xFF;
442 addr6.sin6_addr.s6_addr[13] = (ip4addr >> 16) & 0xFF;
443 addr6.sin6_addr.s6_addr[14] = (ip4addr >> 8) & 0xFF;
444 addr6.sin6_addr.s6_addr[15] = (ip4addr) & 0xFF;
446 memcpy(addr, &addr6, sizeof(addr6));
450 * pg_promote_v4_to_v6_mask --- convert an AF_INET netmask to AF_INET6, using
451 * the standard convention for IPv4 addresses mapped into IPv6 world
453 * This must be different from pg_promote_v4_to_v6_addr because we want to
454 * set the high-order bits to 1's not 0's.
456 * The passed addr is modified in place; be sure it is large enough to
457 * hold the result! Note that we only worry about setting the fields
458 * that pg_range_sockaddr will look at.
461 pg_promote_v4_to_v6_mask(struct sockaddr_storage * addr)
463 struct sockaddr_in addr4;
464 struct sockaddr_in6 addr6;
468 memcpy(&addr4, addr, sizeof(addr4));
469 ip4addr = ntohl(addr4.sin_addr.s_addr);
471 memset(&addr6, 0, sizeof(addr6));
473 addr6.sin6_family = AF_INET6;
475 for (i = 0; i < 12; i++)
476 addr6.sin6_addr.s6_addr[i] = 0xff;
478 addr6.sin6_addr.s6_addr[12] = (ip4addr >> 24) & 0xFF;
479 addr6.sin6_addr.s6_addr[13] = (ip4addr >> 16) & 0xFF;
480 addr6.sin6_addr.s6_addr[14] = (ip4addr >> 8) & 0xFF;
481 addr6.sin6_addr.s6_addr[15] = (ip4addr) & 0xFF;
483 memcpy(addr, &addr6, sizeof(addr6));
485 #endif /* HAVE_IPV6 */
489 * Run the callback function for the addr/mask, after making sure the
490 * mask is sane for the addr.
493 run_ifaddr_callback(PgIfAddrCallback callback, void *cb_data,
494 struct sockaddr * addr, struct sockaddr * mask)
496 struct sockaddr_storage fullmask;
501 /* Check that the mask is valid */
504 if (mask->sa_family != addr->sa_family)
508 else if (mask->sa_family == AF_INET)
510 if (((struct sockaddr_in *) mask)->sin_addr.s_addr == INADDR_ANY)
514 else if (mask->sa_family == AF_INET6)
516 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *) mask)->sin6_addr))
522 /* If mask is invalid, generate our own fully-set mask */
525 pg_sockaddr_cidr_mask(&fullmask, NULL, addr->sa_family);
526 mask = (struct sockaddr *) & fullmask;
529 (*callback) (addr, mask, cb_data);
534 #include <winsock2.h>
535 #include <ws2tcpip.h>
538 * Enumerate the system's network interface addresses and call the callback
539 * for each one. Returns 0 if successful, -1 if trouble.
541 * This version is for Win32. Uses the Winsock 2 functions (ie: ws2_32.dll)
544 pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
548 unsigned long length,
550 unsigned long n_ii = 0;
554 sock = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
555 if (sock == SOCKET_ERROR)
561 ptr = realloc(ii, sizeof(INTERFACE_INFO) * n_ii);
571 if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST, 0, 0,
572 ii, n_ii * sizeof(INTERFACE_INFO),
573 &length, 0, 0) == SOCKET_ERROR)
575 error = WSAGetLastError();
576 if (error == WSAEFAULT || error == WSAENOBUFS)
577 continue; /* need to make the buffer bigger */
586 for (i = 0; i < length / sizeof(INTERFACE_INFO); ++i)
587 run_ifaddr_callback(callback, cb_data,
588 (struct sockaddr *) & ii[i].iiAddress,
589 (struct sockaddr *) & ii[i].iiNetmask);
595 #elif HAVE_GETIFADDRS /* && !WIN32 */
597 #ifdef HAVE_IFADDRS_H
602 * Enumerate the system's network interface addresses and call the callback
603 * for each one. Returns 0 if successful, -1 if trouble.
605 * This version uses the getifaddrs() interface, which is available on
606 * BSDs, AIX, and modern Linux.
609 pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
614 if (getifaddrs(&ifa) < 0)
617 for (l = ifa; l; l = l->ifa_next)
618 run_ifaddr_callback(callback, cb_data,
619 l->ifa_addr, l->ifa_netmask);
624 #else /* !HAVE_GETIFADDRS && !WIN32 */
626 #ifdef HAVE_SYS_IOCTL_H
627 #include <sys/ioctl.h>
634 #ifdef HAVE_SYS_SOCKIO_H
635 #include <sys/sockio.h>
639 * SIOCGIFCONF does not return IPv6 addresses on Solaris
640 * and HP/UX. So we prefer SIOCGLIFCONF if it's available.
642 * On HP/UX, however, it *only* returns IPv6 addresses,
643 * and the structs are named slightly differently too.
644 * We'd have to do another call with SIOCGIFCONF to get the
645 * IPv4 addresses as well. We don't currently bother, just
646 * fall back to SIOCGIFCONF on HP/UX.
649 #if defined(SIOCGLIFCONF) && !defined(__hpux)
652 * Enumerate the system's network interface addresses and call the callback
653 * for each one. Returns 0 if successful, -1 if trouble.
655 * This version uses ioctl(SIOCGLIFCONF).
658 pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
663 struct sockaddr *addr,
667 size_t n_buffer = 1024;
677 sock = socket(AF_INET, SOCK_DGRAM, 0);
681 while (n_buffer < 1024 * 100)
684 ptr = realloc(buffer, n_buffer);
693 memset(&lifc, 0, sizeof(lifc));
694 lifc.lifc_family = AF_UNSPEC;
695 lifc.lifc_buf = buffer = ptr;
696 lifc.lifc_len = n_buffer;
698 if (ioctl(sock, SIOCGLIFCONF, &lifc) < 0)
708 * Some Unixes try to return as much data as possible, with no
709 * indication of whether enough space allocated. Don't believe we have
710 * it all unless there's lots of slop.
712 if (lifc.lifc_len < n_buffer - 1024)
717 /* We'll need an IPv6 socket too for the SIOCGLIFNETMASK ioctls */
718 sock6 = socket(AF_INET6, SOCK_DGRAM, 0);
727 total = lifc.lifc_len / sizeof(struct lifreq);
728 lifr = lifc.lifc_req;
729 for (i = 0; i < total; ++i)
731 addr = (struct sockaddr *) & lifr[i].lifr_addr;
732 memcpy(&lmask, &lifr[i], sizeof(struct lifreq));
734 fd = (addr->sa_family == AF_INET6) ? sock6 : sock;
738 if (ioctl(fd, SIOCGLIFNETMASK, &lmask) < 0)
741 mask = (struct sockaddr *) & lmask.lifr_addr;
742 run_ifaddr_callback(callback, cb_data, addr, mask);
752 #elif defined(SIOCGIFCONF)
755 * Remaining Unixes use SIOCGIFCONF. Some only return IPv4 information
756 * here, so this is the least preferred method. Note that there is no
757 * standard way to iterate the struct ifreq returned in the array.
758 * On some OSs the structures are padded large enough for any address,
759 * on others you have to calculate the size of the struct ifreq.
762 /* Some OSs have _SIZEOF_ADDR_IFREQ, so just use that */
763 #ifndef _SIZEOF_ADDR_IFREQ
765 /* Calculate based on sockaddr.sa_len */
766 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
767 #define _SIZEOF_ADDR_IFREQ(ifr) \
768 ((ifr).ifr_addr.sa_len > sizeof(struct sockaddr) ? \
769 (sizeof(struct ifreq) - sizeof(struct sockaddr) + \
770 (ifr).ifr_addr.sa_len) : sizeof(struct ifreq))
772 /* Padded ifreq structure, simple */
774 #define _SIZEOF_ADDR_IFREQ(ifr) \
775 sizeof (struct ifreq)
777 #endif /* !_SIZEOF_ADDR_IFREQ */
780 * Enumerate the system's network interface addresses and call the callback
781 * for each one. Returns 0 if successful, -1 if trouble.
783 * This version uses ioctl(SIOCGIFCONF).
786 pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
795 size_t n_buffer = 1024;
798 sock = socket(AF_INET, SOCK_DGRAM, 0);
802 while (n_buffer < 1024 * 100)
805 ptr = realloc(buffer, n_buffer);
814 memset(&ifc, 0, sizeof(ifc));
815 ifc.ifc_buf = buffer = ptr;
816 ifc.ifc_len = n_buffer;
818 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0)
828 * Some Unixes try to return as much data as possible, with no
829 * indication of whether enough space allocated. Don't believe we have
830 * it all unless there's lots of slop.
832 if (ifc.ifc_len < n_buffer - 1024)
836 end = (struct ifreq *) (buffer + ifc.ifc_len);
837 for (ifr = ifc.ifc_req; ifr < end;)
839 memcpy(&addr, ifr, sizeof(addr));
840 memcpy(&mask, ifr, sizeof(mask));
841 if (ioctl(sock, SIOCGIFADDR, &addr, sizeof(addr)) == 0 &&
842 ioctl(sock, SIOCGIFNETMASK, &mask, sizeof(mask)) == 0)
843 run_ifaddr_callback(callback, cb_data,
844 &addr.ifr_addr, &mask.ifr_addr);
845 ifr = (struct ifreq *) ((char *) ifr + _SIZEOF_ADDR_IFREQ(*ifr));
852 #else /* !defined(SIOCGIFCONF) */
855 * Enumerate the system's network interface addresses and call the callback
856 * for each one. Returns 0 if successful, -1 if trouble.
858 * This version is our fallback if there's no known way to get the
859 * interface addresses. Just return the standard loopback addresses.
862 pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
864 struct sockaddr_in addr;
865 struct sockaddr_storage mask;
868 struct sockaddr_in6 addr6;
871 /* addr 127.0.0.1/8 */
872 memset(&addr, 0, sizeof(addr));
873 addr.sin_family = AF_INET;
874 addr.sin_addr.s_addr = ntohl(0x7f000001);
875 memset(&mask, 0, sizeof(mask));
876 pg_sockaddr_cidr_mask(&mask, "8", AF_INET);
877 run_ifaddr_callback(callback, cb_data,
878 (struct sockaddr *) & addr,
879 (struct sockaddr *) & mask);
883 memset(&addr6, 0, sizeof(addr6));
884 addr6.sin6_family = AF_INET6;
885 addr6.sin6_addr.s6_addr[15] = 1;
886 memset(&mask, 0, sizeof(mask));
887 pg_sockaddr_cidr_mask(&mask, "128", AF_INET6);
888 run_ifaddr_callback(callback, cb_data,
889 (struct sockaddr *) & addr6,
890 (struct sockaddr *) & mask);
895 #endif /* !defined(SIOCGIFCONF) */
897 #endif /* !HAVE_GETIFADDRS */