Blame sysdeps/mach/hurd/if_index.c

Packit 6c4009
/* Find network interface names and index numbers.  Hurd version.
Packit 6c4009
   Copyright (C) 2000-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 <error.h>
Packit 6c4009
#include <net/if.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/ioctl.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/ioctl.h>
Packit 6c4009
#include <hurd/pfinet.h>
Packit 6c4009
Packit 6c4009
/* Return the interface index corresponding to interface IFNAME.
Packit 6c4009
   On error, return 0.  */
Packit 6c4009
unsigned int
Packit 6c4009
__if_nametoindex (const char *ifname)
Packit 6c4009
{
Packit 6c4009
  struct ifreq ifr;
Packit 6c4009
  int fd = __opensock ();
Packit 6c4009
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  if (strlen (ifname) >= IFNAMSIZ)
Packit 6c4009
    {
Packit 6c4009
      __set_errno (ENODEV);
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  strncpy (ifr.ifr_name, ifname, IFNAMSIZ);
Packit 6c4009
  if (__ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
Packit 6c4009
    {
Packit 6c4009
      int saved_errno = errno;
Packit 6c4009
      __close (fd);
Packit 6c4009
      if (saved_errno == EINVAL || saved_errno == ENOTTY)
Packit 6c4009
        __set_errno (ENOSYS);
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
  __close (fd);
Packit 6c4009
  return ifr.ifr_ifindex;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__if_nametoindex)
Packit 6c4009
weak_alias (__if_nametoindex, if_nametoindex)
Packit 6c4009
libc_hidden_weak (if_nametoindex)
Packit 6c4009
Packit 6c4009
/* Free the structure IFN returned by if_nameindex.  */
Packit 6c4009
void
Packit 6c4009
__if_freenameindex (struct if_nameindex *ifn)
Packit 6c4009
{
Packit 6c4009
  struct if_nameindex *ptr = ifn;
Packit 6c4009
  while (ptr->if_name || ptr->if_index)
Packit 6c4009
    {
Packit 6c4009
      free (ptr->if_name);
Packit 6c4009
      ++ptr;
Packit 6c4009
    }
Packit 6c4009
  free (ifn);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__if_freenameindex)
Packit 6c4009
weak_alias (__if_freenameindex, if_freenameindex)
Packit 6c4009
libc_hidden_weak (if_freenameindex)
Packit 6c4009
Packit 6c4009
/* Return an array of if_nameindex structures, one for each network
Packit 6c4009
   interface present, plus one indicating the end of the array.  On
Packit 6c4009
   error, return NULL.  */
Packit 6c4009
struct if_nameindex *
Packit 6c4009
__if_nameindex (void)
Packit 6c4009
{
Packit 6c4009
  error_t err = 0;
Packit 6c4009
  char data[2048];
Packit 6c4009
  file_t server;
Packit 6c4009
  int fd = __opensock ();
Packit 6c4009
  struct ifconf ifc;
Packit 6c4009
  unsigned int nifs, i;
Packit 6c4009
  struct if_nameindex *idx = NULL;
Packit 6c4009
Packit 6c4009
  ifc.ifc_buf = data;
Packit 6c4009
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  server = _hurd_socket_server (PF_INET, 0);
Packit 6c4009
  if (server == MACH_PORT_NULL)
Packit 6c4009
    nifs = 0;
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      size_t len = sizeof data;
Packit 6c4009
      err = __pfinet_siocgifconf (server, -1, &ifc.ifc_buf, &len;;
Packit 6c4009
      if (err == MACH_SEND_INVALID_DEST || err == MIG_SERVER_DIED)
Packit 6c4009
	{
Packit 6c4009
	  /* On the first use of the socket server during the operation,
Packit 6c4009
	     allow for the old server port dying.  */
Packit 6c4009
	  server = _hurd_socket_server (PF_INET, 1);
Packit 6c4009
	  if (server == MACH_PORT_NULL)
Packit 6c4009
	    goto out;
Packit 6c4009
	  err = __pfinet_siocgifconf (server, -1, &ifc.ifc_buf, &len;;
Packit 6c4009
	}
Packit 6c4009
      if (err)
Packit 6c4009
	goto out;
Packit 6c4009
Packit 6c4009
      ifc.ifc_len = len;
Packit 6c4009
      nifs = len / sizeof (struct ifreq);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
Packit 6c4009
  if (idx == NULL)
Packit 6c4009
    {
Packit 6c4009
      err = ENOBUFS;
Packit 6c4009
      goto out;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (i = 0; i < nifs; ++i)
Packit 6c4009
    {
Packit 6c4009
      struct ifreq *ifr = &ifc.ifc_req[i];
Packit 6c4009
      idx[i].if_name = __strdup (ifr->ifr_name);
Packit 6c4009
      if (idx[i].if_name == NULL
Packit 6c4009
          || __ioctl (fd, SIOCGIFINDEX, ifr) < 0)
Packit 6c4009
        {
Packit 6c4009
          unsigned int j;
Packit 6c4009
          err = errno;
Packit 6c4009
Packit 6c4009
          for (j =  0; j < i; ++j)
Packit 6c4009
            free (idx[j].if_name);
Packit 6c4009
          free (idx);
Packit 6c4009
	  idx = NULL;
Packit 6c4009
Packit 6c4009
          if (err == EINVAL)
Packit 6c4009
            err = ENOSYS;
Packit 6c4009
          else if (err == ENOMEM)
Packit 6c4009
            err = ENOBUFS;
Packit 6c4009
          goto out;
Packit 6c4009
        }
Packit 6c4009
      idx[i].if_index = ifr->ifr_ifindex;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  idx[i].if_index = 0;
Packit 6c4009
  idx[i].if_name = NULL;
Packit 6c4009
Packit 6c4009
 out:
Packit 6c4009
  __close (fd);
Packit 6c4009
  if (data != ifc.ifc_buf)
Packit 6c4009
    __vm_deallocate (__mach_task_self (), (vm_address_t) ifc.ifc_buf,
Packit 6c4009
		     ifc.ifc_len);
Packit 6c4009
  __set_errno (err);
Packit 6c4009
  return idx;
Packit 6c4009
}
Packit 6c4009
weak_alias (__if_nameindex, if_nameindex)
Packit 6c4009
libc_hidden_weak (if_nameindex)
Packit 6c4009
Packit 6c4009
/* Store the name of the interface corresponding to index IFINDEX in
Packit 6c4009
   IFNAME (which has space for at least IFNAMSIZ characters).  Return
Packit 6c4009
   IFNAME, or NULL on error.  */
Packit 6c4009
char *
Packit 6c4009
__if_indextoname (unsigned int ifindex, char *ifname)
Packit 6c4009
{
Packit 6c4009
  struct ifreq ifr;
Packit 6c4009
  int fd = __opensock ();
Packit 6c4009
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  ifr.ifr_ifindex = ifindex;
Packit 6c4009
  if (__ioctl (fd, SIOCGIFNAME, &ifr) < 0)
Packit 6c4009
    {
Packit 6c4009
      int saved_errno = errno;
Packit 6c4009
      __close (fd);
Packit 6c4009
      if (saved_errno == EINVAL || saved_errno == ENOTTY)
Packit 6c4009
        __set_errno (ENOSYS);
Packit 6c4009
      else if (saved_errno == ENODEV)
Packit 6c4009
	__set_errno (ENXIO);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
  __close (fd);
Packit 6c4009
  return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
Packit 6c4009
}
Packit 6c4009
weak_alias (__if_indextoname, if_indextoname)
Packit 6c4009
libc_hidden_weak (if_indextoname)
Packit 6c4009
Packit 6c4009
#if 0
Packit 6c4009
void
Packit 6c4009
__protocol_available (int *have_inet, int *have_inet6)
Packit 6c4009
{
Packit 6c4009
  *have_inet = _hurd_socket_server (PF_INET, 0) != MACH_PORT_NULL;
Packit 6c4009
  *have_inet6 = _hurd_socket_server (PF_INET6, 0) != MACH_PORT_NULL;
Packit 6c4009
}
Packit 6c4009
#endif