LCOV - code coverage report
Current view: top level - src/backend/libpq - ifaddr.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 40 76 52.6 %
Date: 2024-11-21 08:14:44 Functions: 4 6 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * ifaddr.c
       4             :  *    IP netmask calculations, and enumerating network interfaces.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/libpq/ifaddr.c
      12             :  *
      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.
      16             :  *
      17             :  *-------------------------------------------------------------------------
      18             :  */
      19             : 
      20             : #include "postgres.h"
      21             : 
      22             : #include <unistd.h>
      23             : #include <sys/stat.h>
      24             : #include <sys/socket.h>
      25             : #include <netdb.h>
      26             : #include <netinet/in.h>
      27             : #include <netinet/tcp.h>
      28             : #include <sys/file.h>
      29             : 
      30             : #include "libpq/ifaddr.h"
      31             : #include "port/pg_bswap.h"
      32             : 
      33             : static int  range_sockaddr_AF_INET(const struct sockaddr_in *addr,
      34             :                                    const struct sockaddr_in *netaddr,
      35             :                                    const struct sockaddr_in *netmask);
      36             : 
      37             : static int  range_sockaddr_AF_INET6(const struct sockaddr_in6 *addr,
      38             :                                     const struct sockaddr_in6 *netaddr,
      39             :                                     const struct sockaddr_in6 *netmask);
      40             : 
      41             : 
      42             : /*
      43             :  * pg_range_sockaddr - is addr within the subnet specified by netaddr/netmask ?
      44             :  *
      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.
      47             :  */
      48             : int
      49         934 : pg_range_sockaddr(const struct sockaddr_storage *addr,
      50             :                   const struct sockaddr_storage *netaddr,
      51             :                   const struct sockaddr_storage *netmask)
      52             : {
      53         934 :     if (addr->ss_family == AF_INET)
      54         648 :         return range_sockaddr_AF_INET((const struct sockaddr_in *) addr,
      55             :                                       (const struct sockaddr_in *) netaddr,
      56             :                                       (const struct sockaddr_in *) netmask);
      57         286 :     else if (addr->ss_family == AF_INET6)
      58         286 :         return range_sockaddr_AF_INET6((const struct sockaddr_in6 *) addr,
      59             :                                        (const struct sockaddr_in6 *) netaddr,
      60             :                                        (const struct sockaddr_in6 *) netmask);
      61             :     else
      62           0 :         return 0;
      63             : }
      64             : 
      65             : static int
      66         648 : range_sockaddr_AF_INET(const struct sockaddr_in *addr,
      67             :                        const struct sockaddr_in *netaddr,
      68             :                        const struct sockaddr_in *netmask)
      69             : {
      70         648 :     if (((addr->sin_addr.s_addr ^ netaddr->sin_addr.s_addr) &
      71         648 :          netmask->sin_addr.s_addr) == 0)
      72         648 :         return 1;
      73             :     else
      74           0 :         return 0;
      75             : }
      76             : 
      77             : static int
      78         286 : range_sockaddr_AF_INET6(const struct sockaddr_in6 *addr,
      79             :                         const struct sockaddr_in6 *netaddr,
      80             :                         const struct sockaddr_in6 *netmask)
      81             : {
      82             :     int         i;
      83             : 
      84        4862 :     for (i = 0; i < 16; i++)
      85             :     {
      86        4576 :         if (((addr->sin6_addr.s6_addr[i] ^ netaddr->sin6_addr.s6_addr[i]) &
      87        4576 :              netmask->sin6_addr.s6_addr[i]) != 0)
      88           0 :             return 0;
      89             :     }
      90             : 
      91         286 :     return 1;
      92             : }
      93             : 
      94             : /*
      95             :  *  pg_sockaddr_cidr_mask - make a network mask of the appropriate family
      96             :  *    and required number of significant bits
      97             :  *
      98             :  * numbits can be null, in which case the mask is fully set.
      99             :  *
     100             :  * The resulting mask is placed in *mask, which had better be big enough.
     101             :  *
     102             :  * Return value is 0 if okay, -1 if not.
     103             :  */
     104             : int
     105        6826 : pg_sockaddr_cidr_mask(struct sockaddr_storage *mask, char *numbits, int family)
     106             : {
     107             :     long        bits;
     108             :     char       *endptr;
     109             : 
     110        6826 :     if (numbits == NULL)
     111             :     {
     112           0 :         bits = (family == AF_INET) ? 32 : 128;
     113             :     }
     114             :     else
     115             :     {
     116        6826 :         bits = strtol(numbits, &endptr, 10);
     117        6826 :         if (*numbits == '\0' || *endptr != '\0')
     118           0 :             return -1;
     119             :     }
     120             : 
     121        6826 :     switch (family)
     122             :     {
     123        3626 :         case AF_INET:
     124             :             {
     125             :                 struct sockaddr_in mask4;
     126             :                 long        maskl;
     127             : 
     128        3626 :                 if (bits < 0 || bits > 32)
     129           0 :                     return -1;
     130        3626 :                 memset(&mask4, 0, sizeof(mask4));
     131             :                 /* avoid "x << 32", which is not portable */
     132        3626 :                 if (bits > 0)
     133        3626 :                     maskl = (0xffffffffUL << (32 - (int) bits))
     134        3626 :                         & 0xffffffffUL;
     135             :                 else
     136           0 :                     maskl = 0;
     137        3626 :                 mask4.sin_addr.s_addr = pg_hton32(maskl);
     138        3626 :                 memcpy(mask, &mask4, sizeof(mask4));
     139        3626 :                 break;
     140             :             }
     141             : 
     142        3200 :         case AF_INET6:
     143             :             {
     144             :                 struct sockaddr_in6 mask6;
     145             :                 int         i;
     146             : 
     147        3200 :                 if (bits < 0 || bits > 128)
     148           0 :                     return -1;
     149        3200 :                 memset(&mask6, 0, sizeof(mask6));
     150       54400 :                 for (i = 0; i < 16; i++)
     151             :                 {
     152       51200 :                     if (bits <= 0)
     153           0 :                         mask6.sin6_addr.s6_addr[i] = 0;
     154       51200 :                     else if (bits >= 8)
     155       51200 :                         mask6.sin6_addr.s6_addr[i] = 0xff;
     156             :                     else
     157             :                     {
     158           0 :                         mask6.sin6_addr.s6_addr[i] =
     159           0 :                             (0xff << (8 - (int) bits)) & 0xff;
     160             :                     }
     161       51200 :                     bits -= 8;
     162             :                 }
     163        3200 :                 memcpy(mask, &mask6, sizeof(mask6));
     164        3200 :                 break;
     165             :             }
     166             : 
     167           0 :         default:
     168           0 :             return -1;
     169             :     }
     170             : 
     171        6826 :     mask->ss_family = family;
     172        6826 :     return 0;
     173             : }
     174             : 
     175             : 
     176             : /*
     177             :  * Run the callback function for the addr/mask, after making sure the
     178             :  * mask is sane for the addr.
     179             :  */
     180             : static void
     181           0 : run_ifaddr_callback(PgIfAddrCallback callback, void *cb_data,
     182             :                     struct sockaddr *addr, struct sockaddr *mask)
     183             : {
     184             :     struct sockaddr_storage fullmask;
     185             : 
     186           0 :     if (!addr)
     187           0 :         return;
     188             : 
     189             :     /* Check that the mask is valid */
     190           0 :     if (mask)
     191             :     {
     192           0 :         if (mask->sa_family != addr->sa_family)
     193             :         {
     194           0 :             mask = NULL;
     195             :         }
     196           0 :         else if (mask->sa_family == AF_INET)
     197             :         {
     198           0 :             if (((struct sockaddr_in *) mask)->sin_addr.s_addr == INADDR_ANY)
     199           0 :                 mask = NULL;
     200             :         }
     201           0 :         else if (mask->sa_family == AF_INET6)
     202             :         {
     203           0 :             if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *) mask)->sin6_addr))
     204           0 :                 mask = NULL;
     205             :         }
     206             :     }
     207             : 
     208             :     /* If mask is invalid, generate our own fully-set mask */
     209           0 :     if (!mask)
     210             :     {
     211           0 :         pg_sockaddr_cidr_mask(&fullmask, NULL, addr->sa_family);
     212           0 :         mask = (struct sockaddr *) &fullmask;
     213             :     }
     214             : 
     215           0 :     (*callback) (addr, mask, cb_data);
     216             : }
     217             : 
     218             : #ifdef WIN32
     219             : 
     220             : #include <winsock2.h>
     221             : #include <ws2tcpip.h>
     222             : 
     223             : /*
     224             :  * Enumerate the system's network interface addresses and call the callback
     225             :  * for each one.  Returns 0 if successful, -1 if trouble.
     226             :  *
     227             :  * This version is for Win32.  Uses the Winsock 2 functions (ie: ws2_32.dll)
     228             :  */
     229             : int
     230             : pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
     231             : {
     232             :     INTERFACE_INFO *ptr,
     233             :                *ii = NULL;
     234             :     unsigned long length,
     235             :                 i;
     236             :     unsigned long n_ii = 0;
     237             :     SOCKET      sock;
     238             :     int         error;
     239             : 
     240             :     sock = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
     241             :     if (sock == INVALID_SOCKET)
     242             :         return -1;
     243             : 
     244             :     while (n_ii < 1024)
     245             :     {
     246             :         n_ii += 64;
     247             :         ptr = realloc(ii, sizeof(INTERFACE_INFO) * n_ii);
     248             :         if (!ptr)
     249             :         {
     250             :             free(ii);
     251             :             closesocket(sock);
     252             :             errno = ENOMEM;
     253             :             return -1;
     254             :         }
     255             : 
     256             :         ii = ptr;
     257             :         if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST, 0, 0,
     258             :                      ii, n_ii * sizeof(INTERFACE_INFO),
     259             :                      &length, 0, 0) == SOCKET_ERROR)
     260             :         {
     261             :             error = WSAGetLastError();
     262             :             if (error == WSAEFAULT || error == WSAENOBUFS)
     263             :                 continue;       /* need to make the buffer bigger */
     264             :             closesocket(sock);
     265             :             free(ii);
     266             :             return -1;
     267             :         }
     268             : 
     269             :         break;
     270             :     }
     271             : 
     272             :     for (i = 0; i < length / sizeof(INTERFACE_INFO); ++i)
     273             :         run_ifaddr_callback(callback, cb_data,
     274             :                             (struct sockaddr *) &ii[i].iiAddress,
     275             :                             (struct sockaddr *) &ii[i].iiNetmask);
     276             : 
     277             :     closesocket(sock);
     278             :     free(ii);
     279             :     return 0;
     280             : }
     281             : #elif HAVE_GETIFADDRS           /* && !WIN32 */
     282             : 
     283             : #ifdef HAVE_IFADDRS_H
     284             : #include <ifaddrs.h>
     285             : #endif
     286             : 
     287             : /*
     288             :  * Enumerate the system's network interface addresses and call the callback
     289             :  * for each one.  Returns 0 if successful, -1 if trouble.
     290             :  *
     291             :  * This version uses the getifaddrs() interface, which is available on
     292             :  * BSDs, macOS, Solaris, illumos and Linux.
     293             :  */
     294             : int
     295           0 : pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
     296             : {
     297             :     struct ifaddrs *ifa,
     298             :                *l;
     299             : 
     300           0 :     if (getifaddrs(&ifa) < 0)
     301           0 :         return -1;
     302             : 
     303           0 :     for (l = ifa; l; l = l->ifa_next)
     304           0 :         run_ifaddr_callback(callback, cb_data,
     305             :                             l->ifa_addr, l->ifa_netmask);
     306             : 
     307           0 :     freeifaddrs(ifa);
     308           0 :     return 0;
     309             : }
     310             : #else                           /* !HAVE_GETIFADDRS && !WIN32 */
     311             : 
     312             : #include <sys/ioctl.h>
     313             : #include <net/if.h>
     314             : 
     315             : #if defined(SIOCGIFCONF)
     316             : 
     317             : /*
     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.
     323             :  */
     324             : 
     325             : /* Some OSs have _SIZEOF_ADDR_IFREQ, so just use that */
     326             : #ifndef _SIZEOF_ADDR_IFREQ
     327             : 
     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))
     334             : 
     335             : /* Padded ifreq structure, simple */
     336             : #else
     337             : #define _SIZEOF_ADDR_IFREQ(ifr) \
     338             :     sizeof (struct ifreq)
     339             : #endif
     340             : #endif                          /* !_SIZEOF_ADDR_IFREQ */
     341             : 
     342             : /*
     343             :  * Enumerate the system's network interface addresses and call the callback
     344             :  * for each one.  Returns 0 if successful, -1 if trouble.
     345             :  *
     346             :  * This version uses ioctl(SIOCGIFCONF).
     347             :  */
     348             : int
     349             : pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
     350             : {
     351             :     struct ifconf ifc;
     352             :     struct ifreq *ifr,
     353             :                *end,
     354             :                 addr,
     355             :                 mask;
     356             :     char       *ptr,
     357             :                *buffer = NULL;
     358             :     size_t      n_buffer = 1024;
     359             :     pgsocket    sock;
     360             : 
     361             :     sock = socket(AF_INET, SOCK_DGRAM, 0);
     362             :     if (sock == PGINVALID_SOCKET)
     363             :         return -1;
     364             : 
     365             :     while (n_buffer < 1024 * 100)
     366             :     {
     367             :         n_buffer += 1024;
     368             :         ptr = realloc(buffer, n_buffer);
     369             :         if (!ptr)
     370             :         {
     371             :             free(buffer);
     372             :             close(sock);
     373             :             errno = ENOMEM;
     374             :             return -1;
     375             :         }
     376             : 
     377             :         memset(&ifc, 0, sizeof(ifc));
     378             :         ifc.ifc_buf = buffer = ptr;
     379             :         ifc.ifc_len = n_buffer;
     380             : 
     381             :         if (ioctl(sock, SIOCGIFCONF, &ifc) < 0)
     382             :         {
     383             :             if (errno == EINVAL)
     384             :                 continue;
     385             :             free(buffer);
     386             :             close(sock);
     387             :             return -1;
     388             :         }
     389             : 
     390             :         /*
     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.
     394             :          */
     395             :         if (ifc.ifc_len < n_buffer - 1024)
     396             :             break;
     397             :     }
     398             : 
     399             :     end = (struct ifreq *) (buffer + ifc.ifc_len);
     400             :     for (ifr = ifc.ifc_req; ifr < end;)
     401             :     {
     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)
     406             :             run_ifaddr_callback(callback, cb_data,
     407             :                                 &addr.ifr_addr, &mask.ifr_addr);
     408             :         ifr = (struct ifreq *) ((char *) ifr + _SIZEOF_ADDR_IFREQ(*ifr));
     409             :     }
     410             : 
     411             :     free(buffer);
     412             :     close(sock);
     413             :     return 0;
     414             : }
     415             : #else                           /* !defined(SIOCGIFCONF) */
     416             : 
     417             : /*
     418             :  * Enumerate the system's network interface addresses and call the callback
     419             :  * for each one.  Returns 0 if successful, -1 if trouble.
     420             :  *
     421             :  * This version is our fallback if there's no known way to get the
     422             :  * interface addresses.  Just return the standard loopback addresses.
     423             :  */
     424             : int
     425             : pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
     426             : {
     427             :     struct sockaddr_in addr;
     428             :     struct sockaddr_storage mask;
     429             :     struct sockaddr_in6 addr6;
     430             : 
     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));
     436             :     pg_sockaddr_cidr_mask(&mask, "8", AF_INET);
     437             :     run_ifaddr_callback(callback, cb_data,
     438             :                         (struct sockaddr *) &addr,
     439             :                         (struct sockaddr *) &mask);
     440             : 
     441             :     /* addr ::1/128 */
     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));
     446             :     pg_sockaddr_cidr_mask(&mask, "128", AF_INET6);
     447             :     run_ifaddr_callback(callback, cb_data,
     448             :                         (struct sockaddr *) &addr6,
     449             :                         (struct sockaddr *) &mask);
     450             : 
     451             :     return 0;
     452             : }
     453             : #endif                          /* !defined(SIOCGIFCONF) */
     454             : 
     455             : #endif                          /* !HAVE_GETIFADDRS */

Generated by: LCOV version 1.14