Back to index

glibc  2.9
Classes | Functions
if.h File Reference
#include <features.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  if_nameindex

Functions

__BEGIN_DECLS unsigned int if_nametoindex (__const char *__ifname) __THROW
char * if_indextoname (unsigned int __ifindex, char *__ifname) __THROW
struct if_nameindexif_nameindex (void) __THROW
void if_freenameindex (struct if_nameindex *__ptr) __THROW

Class Documentation

struct if_nameindex

Definition at line 182 of file if.h.

Class Members
unsigned int if_index
char * if_name

Function Documentation

void if_freenameindex ( struct if_nameindex __ptr)
char* if_indextoname ( unsigned int  __ifindex,
char *  __ifname 
)

Definition at line 157 of file if_index.c.

{
  struct ifreq ifr;
  int fd = __opensock ();

  if (fd < 0)
    return NULL;

  ifr.ifr_ifindex = ifindex;
  if (__ioctl (fd, SIOCGIFNAME, &ifr) < 0)
    {
      int saved_errno = errno;
      __close (fd);
      if (saved_errno == EINVAL || saved_errno == ENOTTY)
        __set_errno (ENOSYS);
      else if (saved_errno == ENODEV)
       __set_errno (ENXIO);
      return NULL;
    }
  __close (fd);
  return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
}
struct if_nameindex* if_nameindex ( void  ) [read]

Definition at line 72 of file if_index.c.

{
  error_t err = 0;
  char data[2048];
  file_t server;
  int fd = __opensock ();
  struct ifconf ifc;
  unsigned int nifs, i;
  struct if_nameindex *idx = NULL;

  ifc.ifc_buf = data;

  if (fd < 0)
    return NULL;

  server = _hurd_socket_server (PF_INET, 0);
  if (server == MACH_PORT_NULL)
    nifs = 0;
  else
    {
      size_t len = sizeof data;
      err = __pfinet_siocgifconf (server, -1, &ifc.ifc_buf, &len);
      if (err == MACH_SEND_INVALID_DEST || err == MIG_SERVER_DIED)
       {
         /* On the first use of the socket server during the operation,
            allow for the old server port dying.  */
         server = _hurd_socket_server (PF_INET, 1);
         if (server == MACH_PORT_NULL)
           goto out;
         err = __pfinet_siocgifconf (server, -1, &ifc.ifc_buf, &len);
       }
      if (err)
       goto out;

      ifc.ifc_len = len;
      nifs = len / sizeof (struct ifreq);
    }

  idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
  if (idx == NULL)
    {
      err = ENOBUFS;
      goto out;
    }

  for (i = 0; i < nifs; ++i)
    {
      struct ifreq *ifr = &ifc.ifc_req[i];
      idx[i].if_name = __strdup (ifr->ifr_name);
      if (idx[i].if_name == NULL
          || __ioctl (fd, SIOCGIFINDEX, ifr) < 0)
        {
          unsigned int j;
          err = errno;

          for (j =  0; j < i; ++j)
            free (idx[j].if_name);
          free (idx);
         idx = NULL;

          if (err == EINVAL)
            err = ENOSYS;
          else if (err == ENOMEM)
            err = ENOBUFS;
          goto out;
        }
      idx[i].if_index = ifr->ifr_ifindex;
    }

  idx[i].if_index = 0;
  idx[i].if_name = NULL;

 out:
  __close (fd);
  if (data != ifc.ifc_buf)
    __vm_deallocate (__mach_task_self (), (vm_address_t) ifc.ifc_buf,
                   ifc.ifc_len);
  __set_errno (err);
  return idx;
}
__BEGIN_DECLS unsigned int if_nametoindex ( __const char *  __ifname)