Blame sysdeps/gnu/ifaddrs.c

Packit 6c4009
/* getifaddrs -- get names and addresses of all network interfaces
Packit 6c4009
   Copyright (C) 2002-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <ifaddrs.h>
Packit 6c4009
#include <net/if.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
#include <sys/ioctl.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <netinet/in.h>
Packit 6c4009
Packit 6c4009
#include "ifreq.h"
Packit 6c4009
Packit 6c4009
/* Create a linked list of `struct ifaddrs' structures, one for each
Packit 6c4009
   network interface on the host machine.  If successful, store the
Packit 6c4009
   list in *IFAP and return 0.  On errors, return -1 and set `errno'.  */
Packit 6c4009
int
Packit 6c4009
__getifaddrs (struct ifaddrs **ifap)
Packit 6c4009
{
Packit 6c4009
  /* This implementation handles only IPv4 interfaces.
Packit 6c4009
     The various ioctls below will only work on an AF_INET socket.
Packit 6c4009
     Some different mechanism entirely must be used for IPv6.  */
Packit 6c4009
  int fd = __socket (AF_INET, SOCK_DGRAM, 0);
Packit 6c4009
  struct ifreq *ifreqs;
Packit 6c4009
  int nifs;
Packit 6c4009
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  __ifreq (&ifreqs, &nifs, fd);
Packit 6c4009
  if (ifreqs == NULL)		/* XXX doesn't distinguish error vs none */
Packit 6c4009
    {
Packit 6c4009
      __close (fd);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now we have the list of interfaces and each one's address.
Packit 6c4009
     Put it into the expected format and fill in the remaining details.  */
Packit 6c4009
  if (nifs == 0)
Packit 6c4009
    *ifap = NULL;
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      struct
Packit 6c4009
      {
Packit 6c4009
	struct ifaddrs ia;
Packit 6c4009
	struct sockaddr addr, netmask, broadaddr;
Packit 6c4009
	char name[IF_NAMESIZE];
Packit 6c4009
      } *storage;
Packit 6c4009
      struct ifreq *ifr;
Packit 6c4009
      int i;
Packit 6c4009
Packit 6c4009
      storage = malloc (nifs * sizeof storage[0]);
Packit 6c4009
      if (storage == NULL)
Packit 6c4009
	{
Packit 6c4009
	  __close (fd);
Packit 6c4009
	  __if_freereq (ifreqs, nifs);
Packit 6c4009
	  return -1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      i = 0;
Packit 6c4009
      ifr = ifreqs;
Packit 6c4009
      do
Packit 6c4009
	{
Packit 6c4009
	  /* Fill in pointers to the storage we've already allocated.  */
Packit 6c4009
	  storage[i].ia.ifa_next = &storage[i + 1].ia;
Packit 6c4009
	  storage[i].ia.ifa_addr = &storage[i].addr;
Packit 6c4009
Packit 6c4009
	  /* Now copy the information we already have from SIOCGIFCONF.  */
Packit 6c4009
	  storage[i].ia.ifa_name = strncpy (storage[i].name, ifr->ifr_name,
Packit 6c4009
					    sizeof storage[i].name);
Packit 6c4009
	  storage[i].addr = ifr->ifr_addr;
Packit 6c4009
Packit 6c4009
	  /* The SIOCGIFCONF call filled in only the name and address.
Packit 6c4009
	     Now we must also ask for the other information we need.  */
Packit 6c4009
Packit 6c4009
	  if (__ioctl (fd, SIOCGIFFLAGS, ifr) < 0)
Packit 6c4009
	    break;
Packit 6c4009
	  storage[i].ia.ifa_flags = ifr->ifr_flags;
Packit 6c4009
Packit 6c4009
	  ifr->ifr_addr = storage[i].addr;
Packit 6c4009
Packit 6c4009
	  if (__ioctl (fd, SIOCGIFNETMASK, ifr) < 0)
Packit 6c4009
	    storage[i].ia.ifa_netmask = NULL;
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      storage[i].ia.ifa_netmask = &storage[i].netmask;
Packit 6c4009
	      storage[i].netmask = ifr->ifr_netmask;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if (ifr->ifr_flags & IFF_BROADCAST)
Packit 6c4009
	    {
Packit 6c4009
	      ifr->ifr_addr = storage[i].addr;
Packit 6c4009
	      if (__ioctl (fd, SIOCGIFBRDADDR, ifr) < 0)
Packit 6c4009
		storage[i].ia.ifa_broadaddr = NULL;
Packit 6c4009
	      {
Packit 6c4009
		storage[i].ia.ifa_broadaddr = &storage[i].broadaddr;
Packit 6c4009
		storage[i].broadaddr = ifr->ifr_broadaddr;
Packit 6c4009
	      }
Packit 6c4009
	    }
Packit 6c4009
	  else if (ifr->ifr_flags & IFF_POINTOPOINT)
Packit 6c4009
	    {
Packit 6c4009
	      ifr->ifr_addr = storage[i].addr;
Packit 6c4009
	      if (__ioctl (fd, SIOCGIFDSTADDR, ifr) < 0)
Packit 6c4009
		storage[i].ia.ifa_broadaddr = NULL;
Packit 6c4009
	      else
Packit 6c4009
		{
Packit 6c4009
		  storage[i].ia.ifa_broadaddr = &storage[i].broadaddr;
Packit 6c4009
		  storage[i].broadaddr = ifr->ifr_dstaddr;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    storage[i].ia.ifa_broadaddr = NULL;
Packit 6c4009
Packit 6c4009
	  storage[i].ia.ifa_data = NULL; /* Nothing here for now.  */
Packit 6c4009
Packit 6c4009
	  ifr = __if_nextreq (ifr);
Packit 6c4009
	} while (++i < nifs);
Packit 6c4009
      if (i < nifs)		/* Broke out early on error.  */
Packit 6c4009
	{
Packit 6c4009
	  __close (fd);
Packit 6c4009
	  free (storage);
Packit 6c4009
	  __if_freereq (ifreqs, nifs);
Packit 6c4009
	  return -1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      storage[i - 1].ia.ifa_next = NULL;
Packit 6c4009
Packit 6c4009
      *ifap = &storage[0].ia;
Packit 6c4009
Packit 6c4009
      __close (fd);
Packit 6c4009
      __if_freereq (ifreqs, nifs);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
weak_alias (__getifaddrs, getifaddrs)
Packit 6c4009
libc_hidden_def (__getifaddrs)
Packit 6c4009
#ifndef getifaddrs
Packit 6c4009
libc_hidden_weak (getifaddrs)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
__freeifaddrs (struct ifaddrs *ifa)
Packit 6c4009
{
Packit 6c4009
  free (ifa);
Packit 6c4009
}
Packit 6c4009
weak_alias (__freeifaddrs, freeifaddrs)
Packit 6c4009
libc_hidden_def (__freeifaddrs)
Packit 6c4009
libc_hidden_weak (freeifaddrs)