PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ifaddr.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * ifaddr.c
4  * IP netmask calculations, and enumerating network interfaces.
5  *
6  * Portions Copyright (c) 1996-2017, 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/types.h>
24 #include <sys/stat.h>
25 #include <sys/socket.h>
26 #include <netdb.h>
27 #include <netinet/in.h>
28 #ifdef HAVE_NETINET_TCP_H
29 #include <netinet/tcp.h>
30 #endif
31 #include <arpa/inet.h>
32 #include <sys/file.h>
33 
34 #include "libpq/ifaddr.h"
35 
36 static int range_sockaddr_AF_INET(const struct sockaddr_in * addr,
37  const struct sockaddr_in * netaddr,
38  const struct sockaddr_in * netmask);
39 
40 #ifdef HAVE_IPV6
41 static int range_sockaddr_AF_INET6(const struct sockaddr_in6 * addr,
42  const struct sockaddr_in6 * netaddr,
43  const struct sockaddr_in6 * netmask);
44 #endif
45 
46 
47 /*
48  * pg_range_sockaddr - is addr within the subnet specified by netaddr/netmask ?
49  *
50  * Note: caller must already have verified that all three addresses are
51  * in the same address family; and AF_UNIX addresses are not supported.
52  */
53 int
54 pg_range_sockaddr(const struct sockaddr_storage * addr,
55  const struct sockaddr_storage * netaddr,
56  const struct sockaddr_storage * netmask)
57 {
58  if (addr->ss_family == AF_INET)
59  return range_sockaddr_AF_INET((const struct sockaddr_in *) addr,
60  (const struct sockaddr_in *) netaddr,
61  (const struct sockaddr_in *) netmask);
62 #ifdef HAVE_IPV6
63  else if (addr->ss_family == AF_INET6)
64  return range_sockaddr_AF_INET6((const struct sockaddr_in6 *) addr,
65  (const struct sockaddr_in6 *) netaddr,
66  (const struct sockaddr_in6 *) netmask);
67 #endif
68  else
69  return 0;
70 }
71 
72 static int
73 range_sockaddr_AF_INET(const struct sockaddr_in * addr,
74  const struct sockaddr_in * netaddr,
75  const struct sockaddr_in * netmask)
76 {
77  if (((addr->sin_addr.s_addr ^ netaddr->sin_addr.s_addr) &
78  netmask->sin_addr.s_addr) == 0)
79  return 1;
80  else
81  return 0;
82 }
83 
84 
85 #ifdef HAVE_IPV6
86 
87 static int
88 range_sockaddr_AF_INET6(const struct sockaddr_in6 * addr,
89  const struct sockaddr_in6 * netaddr,
90  const struct sockaddr_in6 * netmask)
91 {
92  int i;
93 
94  for (i = 0; i < 16; i++)
95  {
96  if (((addr->sin6_addr.s6_addr[i] ^ netaddr->sin6_addr.s6_addr[i]) &
97  netmask->sin6_addr.s6_addr[i]) != 0)
98  return 0;
99  }
100 
101  return 1;
102 }
103 #endif /* HAVE_IPV6 */
104 
105 /*
106  * pg_sockaddr_cidr_mask - make a network mask of the appropriate family
107  * and required number of significant bits
108  *
109  * numbits can be null, in which case the mask is fully set.
110  *
111  * The resulting mask is placed in *mask, which had better be big enough.
112  *
113  * Return value is 0 if okay, -1 if not.
114  */
115 int
116 pg_sockaddr_cidr_mask(struct sockaddr_storage * mask, char *numbits, int family)
117 {
118  long bits;
119  char *endptr;
120 
121  if (numbits == NULL)
122  {
123  bits = (family == AF_INET) ? 32 : 128;
124  }
125  else
126  {
127  bits = strtol(numbits, &endptr, 10);
128  if (*numbits == '\0' || *endptr != '\0')
129  return -1;
130  }
131 
132  switch (family)
133  {
134  case AF_INET:
135  {
136  struct sockaddr_in mask4;
137  long maskl;
138 
139  if (bits < 0 || bits > 32)
140  return -1;
141  memset(&mask4, 0, sizeof(mask4));
142  /* avoid "x << 32", which is not portable */
143  if (bits > 0)
144  maskl = (0xffffffffUL << (32 - (int) bits))
145  & 0xffffffffUL;
146  else
147  maskl = 0;
148  mask4.sin_addr.s_addr = htonl(maskl);
149  memcpy(mask, &mask4, sizeof(mask4));
150  break;
151  }
152 
153 #ifdef HAVE_IPV6
154  case AF_INET6:
155  {
156  struct sockaddr_in6 mask6;
157  int i;
158 
159  if (bits < 0 || bits > 128)
160  return -1;
161  memset(&mask6, 0, sizeof(mask6));
162  for (i = 0; i < 16; i++)
163  {
164  if (bits <= 0)
165  mask6.sin6_addr.s6_addr[i] = 0;
166  else if (bits >= 8)
167  mask6.sin6_addr.s6_addr[i] = 0xff;
168  else
169  {
170  mask6.sin6_addr.s6_addr[i] =
171  (0xff << (8 - (int) bits)) & 0xff;
172  }
173  bits -= 8;
174  }
175  memcpy(mask, &mask6, sizeof(mask6));
176  break;
177  }
178 #endif
179  default:
180  return -1;
181  }
182 
183  mask->ss_family = family;
184  return 0;
185 }
186 
187 
188 /*
189  * Run the callback function for the addr/mask, after making sure the
190  * mask is sane for the addr.
191  */
192 static void
194  struct sockaddr * addr, struct sockaddr * mask)
195 {
196  struct sockaddr_storage fullmask;
197 
198  if (!addr)
199  return;
200 
201  /* Check that the mask is valid */
202  if (mask)
203  {
204  if (mask->sa_family != addr->sa_family)
205  {
206  mask = NULL;
207  }
208  else if (mask->sa_family == AF_INET)
209  {
210  if (((struct sockaddr_in *) mask)->sin_addr.s_addr == INADDR_ANY)
211  mask = NULL;
212  }
213 #ifdef HAVE_IPV6
214  else if (mask->sa_family == AF_INET6)
215  {
216  if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *) mask)->sin6_addr))
217  mask = NULL;
218  }
219 #endif
220  }
221 
222  /* If mask is invalid, generate our own fully-set mask */
223  if (!mask)
224  {
225  pg_sockaddr_cidr_mask(&fullmask, NULL, addr->sa_family);
226  mask = (struct sockaddr *) & fullmask;
227  }
228 
229  (*callback) (addr, mask, cb_data);
230 }
231 
232 #ifdef WIN32
233 
234 #include <winsock2.h>
235 #include <ws2tcpip.h>
236 
237 /*
238  * Enumerate the system's network interface addresses and call the callback
239  * for each one. Returns 0 if successful, -1 if trouble.
240  *
241  * This version is for Win32. Uses the Winsock 2 functions (ie: ws2_32.dll)
242  */
243 int
245 {
246  INTERFACE_INFO *ptr,
247  *ii = NULL;
248  unsigned long length,
249  i;
250  unsigned long n_ii = 0;
251  SOCKET sock;
252  int error;
253 
254  sock = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
255  if (sock == INVALID_SOCKET)
256  return -1;
257 
258  while (n_ii < 1024)
259  {
260  n_ii += 64;
261  ptr = realloc(ii, sizeof(INTERFACE_INFO) * n_ii);
262  if (!ptr)
263  {
264  free(ii);
265  closesocket(sock);
266  errno = ENOMEM;
267  return -1;
268  }
269 
270  ii = ptr;
271  if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST, 0, 0,
272  ii, n_ii * sizeof(INTERFACE_INFO),
273  &length, 0, 0) == SOCKET_ERROR)
274  {
275  error = WSAGetLastError();
276  if (error == WSAEFAULT || error == WSAENOBUFS)
277  continue; /* need to make the buffer bigger */
278  closesocket(sock);
279  free(ii);
280  return -1;
281  }
282 
283  break;
284  }
285 
286  for (i = 0; i < length / sizeof(INTERFACE_INFO); ++i)
287  run_ifaddr_callback(callback, cb_data,
288  (struct sockaddr *) & ii[i].iiAddress,
289  (struct sockaddr *) & ii[i].iiNetmask);
290 
291  closesocket(sock);
292  free(ii);
293  return 0;
294 }
295 #elif HAVE_GETIFADDRS /* && !WIN32 */
296 
297 #ifdef HAVE_IFADDRS_H
298 #include <ifaddrs.h>
299 #endif
300 
301 /*
302  * Enumerate the system's network interface addresses and call the callback
303  * for each one. Returns 0 if successful, -1 if trouble.
304  *
305  * This version uses the getifaddrs() interface, which is available on
306  * BSDs, AIX, and modern Linux.
307  */
308 int
309 pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
310 {
311  struct ifaddrs *ifa,
312  *l;
313 
314  if (getifaddrs(&ifa) < 0)
315  return -1;
316 
317  for (l = ifa; l; l = l->ifa_next)
318  run_ifaddr_callback(callback, cb_data,
319  l->ifa_addr, l->ifa_netmask);
320 
321  freeifaddrs(ifa);
322  return 0;
323 }
324 #else /* !HAVE_GETIFADDRS && !WIN32 */
325 
326 #ifdef HAVE_SYS_IOCTL_H
327 #include <sys/ioctl.h>
328 #endif
329 
330 #ifdef HAVE_NET_IF_H
331 #include <net/if.h>
332 #endif
333 
334 #ifdef HAVE_SYS_SOCKIO_H
335 #include <sys/sockio.h>
336 #endif
337 
338 /*
339  * SIOCGIFCONF does not return IPv6 addresses on Solaris
340  * and HP/UX. So we prefer SIOCGLIFCONF if it's available.
341  *
342  * On HP/UX, however, it *only* returns IPv6 addresses,
343  * and the structs are named slightly differently too.
344  * We'd have to do another call with SIOCGIFCONF to get the
345  * IPv4 addresses as well. We don't currently bother, just
346  * fall back to SIOCGIFCONF on HP/UX.
347  */
348 
349 #if defined(SIOCGLIFCONF) && !defined(__hpux)
350 
351 /*
352  * Enumerate the system's network interface addresses and call the callback
353  * for each one. Returns 0 if successful, -1 if trouble.
354  *
355  * This version uses ioctl(SIOCGLIFCONF).
356  */
357 int
358 pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
359 {
360  struct lifconf lifc;
361  struct lifreq *lifr,
362  lmask;
363  struct sockaddr *addr,
364  *mask;
365  char *ptr,
366  *buffer = NULL;
367  size_t n_buffer = 1024;
368  pgsocket sock,
369  fd;
370 
371 #ifdef HAVE_IPV6
372  pgsocket sock6;
373 #endif
374  int i,
375  total;
376 
377  sock = socket(AF_INET, SOCK_DGRAM, 0);
378  if (sock == PGINVALID_SOCKET)
379  return -1;
380 
381  while (n_buffer < 1024 * 100)
382  {
383  n_buffer += 1024;
384  ptr = realloc(buffer, n_buffer);
385  if (!ptr)
386  {
387  free(buffer);
388  close(sock);
389  errno = ENOMEM;
390  return -1;
391  }
392 
393  memset(&lifc, 0, sizeof(lifc));
394  lifc.lifc_family = AF_UNSPEC;
395  lifc.lifc_buf = buffer = ptr;
396  lifc.lifc_len = n_buffer;
397 
398  if (ioctl(sock, SIOCGLIFCONF, &lifc) < 0)
399  {
400  if (errno == EINVAL)
401  continue;
402  free(buffer);
403  close(sock);
404  return -1;
405  }
406 
407  /*
408  * Some Unixes try to return as much data as possible, with no
409  * indication of whether enough space allocated. Don't believe we have
410  * it all unless there's lots of slop.
411  */
412  if (lifc.lifc_len < n_buffer - 1024)
413  break;
414  }
415 
416 #ifdef HAVE_IPV6
417  /* We'll need an IPv6 socket too for the SIOCGLIFNETMASK ioctls */
418  sock6 = socket(AF_INET6, SOCK_DGRAM, 0);
419  if (sock6 == PGINVALID_SOCKET)
420  {
421  free(buffer);
422  close(sock);
423  return -1;
424  }
425 #endif
426 
427  total = lifc.lifc_len / sizeof(struct lifreq);
428  lifr = lifc.lifc_req;
429  for (i = 0; i < total; ++i)
430  {
431  addr = (struct sockaddr *) & lifr[i].lifr_addr;
432  memcpy(&lmask, &lifr[i], sizeof(struct lifreq));
433 #ifdef HAVE_IPV6
434  fd = (addr->sa_family == AF_INET6) ? sock6 : sock;
435 #else
436  fd = sock;
437 #endif
438  if (ioctl(fd, SIOCGLIFNETMASK, &lmask) < 0)
439  mask = NULL;
440  else
441  mask = (struct sockaddr *) & lmask.lifr_addr;
442  run_ifaddr_callback(callback, cb_data, addr, mask);
443  }
444 
445  free(buffer);
446  close(sock);
447 #ifdef HAVE_IPV6
448  close(sock6);
449 #endif
450  return 0;
451 }
452 #elif defined(SIOCGIFCONF)
453 
454 /*
455  * Remaining Unixes use SIOCGIFCONF. Some only return IPv4 information
456  * here, so this is the least preferred method. Note that there is no
457  * standard way to iterate the struct ifreq returned in the array.
458  * On some OSs the structures are padded large enough for any address,
459  * on others you have to calculate the size of the struct ifreq.
460  */
461 
462 /* Some OSs have _SIZEOF_ADDR_IFREQ, so just use that */
463 #ifndef _SIZEOF_ADDR_IFREQ
464 
465 /* Calculate based on sockaddr.sa_len */
466 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
467 #define _SIZEOF_ADDR_IFREQ(ifr) \
468  ((ifr).ifr_addr.sa_len > sizeof(struct sockaddr) ? \
469  (sizeof(struct ifreq) - sizeof(struct sockaddr) + \
470  (ifr).ifr_addr.sa_len) : sizeof(struct ifreq))
471 
472 /* Padded ifreq structure, simple */
473 #else
474 #define _SIZEOF_ADDR_IFREQ(ifr) \
475  sizeof (struct ifreq)
476 #endif
477 #endif /* !_SIZEOF_ADDR_IFREQ */
478 
479 /*
480  * Enumerate the system's network interface addresses and call the callback
481  * for each one. Returns 0 if successful, -1 if trouble.
482  *
483  * This version uses ioctl(SIOCGIFCONF).
484  */
485 int
486 pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
487 {
488  struct ifconf ifc;
489  struct ifreq *ifr,
490  *end,
491  addr,
492  mask;
493  char *ptr,
494  *buffer = NULL;
495  size_t n_buffer = 1024;
496  pgsocket sock;
497 
498  sock = socket(AF_INET, SOCK_DGRAM, 0);
499  if (sock == PGINVALID_SOCKET)
500  return -1;
501 
502  while (n_buffer < 1024 * 100)
503  {
504  n_buffer += 1024;
505  ptr = realloc(buffer, n_buffer);
506  if (!ptr)
507  {
508  free(buffer);
509  close(sock);
510  errno = ENOMEM;
511  return -1;
512  }
513 
514  memset(&ifc, 0, sizeof(ifc));
515  ifc.ifc_buf = buffer = ptr;
516  ifc.ifc_len = n_buffer;
517 
518  if (ioctl(sock, SIOCGIFCONF, &ifc) < 0)
519  {
520  if (errno == EINVAL)
521  continue;
522  free(buffer);
523  close(sock);
524  return -1;
525  }
526 
527  /*
528  * Some Unixes try to return as much data as possible, with no
529  * indication of whether enough space allocated. Don't believe we have
530  * it all unless there's lots of slop.
531  */
532  if (ifc.ifc_len < n_buffer - 1024)
533  break;
534  }
535 
536  end = (struct ifreq *) (buffer + ifc.ifc_len);
537  for (ifr = ifc.ifc_req; ifr < end;)
538  {
539  memcpy(&addr, ifr, sizeof(addr));
540  memcpy(&mask, ifr, sizeof(mask));
541  if (ioctl(sock, SIOCGIFADDR, &addr, sizeof(addr)) == 0 &&
542  ioctl(sock, SIOCGIFNETMASK, &mask, sizeof(mask)) == 0)
543  run_ifaddr_callback(callback, cb_data,
544  &addr.ifr_addr, &mask.ifr_addr);
545  ifr = (struct ifreq *) ((char *) ifr + _SIZEOF_ADDR_IFREQ(*ifr));
546  }
547 
548  free(buffer);
549  close(sock);
550  return 0;
551 }
552 #else /* !defined(SIOCGIFCONF) */
553 
554 /*
555  * Enumerate the system's network interface addresses and call the callback
556  * for each one. Returns 0 if successful, -1 if trouble.
557  *
558  * This version is our fallback if there's no known way to get the
559  * interface addresses. Just return the standard loopback addresses.
560  */
561 int
562 pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
563 {
564  struct sockaddr_in addr;
565  struct sockaddr_storage mask;
566 
567 #ifdef HAVE_IPV6
568  struct sockaddr_in6 addr6;
569 #endif
570 
571  /* addr 127.0.0.1/8 */
572  memset(&addr, 0, sizeof(addr));
573  addr.sin_family = AF_INET;
574  addr.sin_addr.s_addr = ntohl(0x7f000001);
575  memset(&mask, 0, sizeof(mask));
576  pg_sockaddr_cidr_mask(&mask, "8", AF_INET);
577  run_ifaddr_callback(callback, cb_data,
578  (struct sockaddr *) & addr,
579  (struct sockaddr *) & mask);
580 
581 #ifdef HAVE_IPV6
582  /* addr ::1/128 */
583  memset(&addr6, 0, sizeof(addr6));
584  addr6.sin6_family = AF_INET6;
585  addr6.sin6_addr.s6_addr[15] = 1;
586  memset(&mask, 0, sizeof(mask));
587  pg_sockaddr_cidr_mask(&mask, "128", AF_INET6);
588  run_ifaddr_callback(callback, cb_data,
589  (struct sockaddr *) & addr6,
590  (struct sockaddr *) & mask);
591 #endif
592 
593  return 0;
594 }
595 #endif /* !defined(SIOCGIFCONF) */
596 
597 #endif /* !HAVE_GETIFADDRS */
int length(const List *list)
Definition: list.c:1271
static void error(void)
Definition: sql-dyntest.c:147
static int range_sockaddr_AF_INET(const struct sockaddr_in *addr, const struct sockaddr_in *netaddr, const struct sockaddr_in *netmask)
Definition: ifaddr.c:73
static void run_ifaddr_callback(PgIfAddrCallback callback, void *cb_data, struct sockaddr *addr, struct sockaddr *mask)
Definition: ifaddr.c:193
#define closesocket
Definition: port.h:328
#define socket(af, type, protocol)
Definition: win32.h:379
static int fd(const char *x, int i)
Definition: preproc-init.c:105
int pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
Definition: ifaddr.c:562
void(* PgIfAddrCallback)(struct sockaddr *addr, struct sockaddr *netmask, void *cb_data)
Definition: ifaddr.h:17
int pg_range_sockaddr(const struct sockaddr_storage *addr, const struct sockaddr_storage *netaddr, const struct sockaddr_storage *netmask)
Definition: ifaddr.c:54
int pg_sockaddr_cidr_mask(struct sockaddr_storage *mask, char *numbits, int family)
Definition: ifaddr.c:116
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:49
int pgsocket
Definition: port.h:22
#define PGINVALID_SOCKET
Definition: port.h:24
#define free(a)
Definition: header.h:60
#define NULL
Definition: c.h:226
#define realloc(a, b)
Definition: header.h:55
int i
#define close(a)
Definition: win32.h:17