Back to index

glibc  2.9
Defines | Functions | Variables
ifreq.c File Reference
#include "ifreq.h"
#include <kernel-features.h>

Go to the source code of this file.

Defines

#define RQ_IFS   4

Functions

void __ifreq (struct ifreq **ifreqs, int *num_ifs, int sockfd)

Variables

static int old_siocgifconf

Define Documentation

#define RQ_IFS   4

Function Documentation

void __ifreq ( struct ifreq **  ifreqs,
int num_ifs,
int  sockfd 
)

Definition at line 32 of file ifreq.c.

{
  int fd = sockfd;
  struct ifconf ifc;
  int rq_len;
  int nifs;
# define RQ_IFS      4

  if (fd < 0)
    fd = __opensock ();
  if (fd < 0)
    {
      *num_ifs = 0;
      *ifreqs = NULL;
      return;
    }

  ifc.ifc_buf = NULL;

  /* We may be able to get the needed buffer size directly, rather than
     guessing.  */
  if (! old_siocgifconf)
    {
      ifc.ifc_buf = NULL;
      ifc.ifc_len = 0;
      if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0 || ifc.ifc_len == 0)
       {
# if __ASSUME_SIOCGIFNAME == 0
         old_siocgifconf = 1;
# endif
         rq_len = RQ_IFS * sizeof (struct ifreq);
       }
      else
       rq_len = ifc.ifc_len;
    }
  else
    rq_len = RQ_IFS * sizeof (struct ifreq);

  /* Read all the interfaces out of the kernel.  */
  while (1)
    {
      ifc.ifc_len = rq_len;
      void *newp = realloc (ifc.ifc_buf, ifc.ifc_len);
      if (newp == NULL
         || (ifc.ifc_buf = newp, __ioctl (fd, SIOCGIFCONF, &ifc)) < 0)
       {
         free (ifc.ifc_buf);

         if (fd != sockfd)
           __close (fd);

         *num_ifs = 0;
         *ifreqs = NULL;
         return;
       }

      if (!old_siocgifconf || ifc.ifc_len < rq_len)
       break;

      rq_len *= 2;
    }

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

  if (fd != sockfd)
    __close (fd);

  *num_ifs = nifs;
  *ifreqs = realloc (ifc.ifc_buf, nifs * sizeof (struct ifreq));
}

Here is the call graph for this function:


Variable Documentation

int old_siocgifconf [static]

Definition at line 25 of file ifreq.c.