if (ioctl (sd, SIOCGIFNETMASK, if) < 0) continue; memcpy (&ifaddrs[num_ifs].u.ipv4.mask, ((struct sockaddr_in *)ifr->ifr_mask)->sin_addr, 4);
if (__ioctl (sd, SIOCGIFNETMASK, if) < 0) continue; memcpy (&ifaddrs[num_ifs].u.ipv4.mask, ((struct sockaddr_in *)ifr->ifr_mask)->sin_addr, 4);
_res_hconf_reorder_addrs (struct hostent * hp){#if defined (SIOCGIFCONF) && defined (SIOCGIFNETMASK)  static int num_ifs = -1;	/* number of interfaces */  static struct netaddr {    int addrtype;    union {      struct {	u_int32_t	addr;	u_int32_t	mask;      } ipv4    } u;  } * ifaddrs;  if (hp->h_addrtype != AF_INET)    return;	/* can't deal with anything but IPv4 for now... */  if (num_ifs <= 0)    {      struct ifconf ifs;      struct ifreq * ifr;      size_t size, num;      int sd;      /* initialize interface table: */      num_ifs = 0;      sd = socket (AF_INET, SOCK_DGRAM, 0);      if (sd < 0)	return;      /* Now get list of interfaces.  Since we don't know how many	 interfaces there are, we keep increasing the buffer size	 until we have at least sizeof(struct ifreq) too many bytes.	 That implies that the ioctl() return because it ran out of	 interfaces, not memory */      size = 0;      ifs.ifc_buf = 0;      do {	size += 4 * sizeof (struct ifreq);	ifs.ifc_buf = realloc (ifs.ifs_buf, size);	if (!ifs.ifc_buf)	  {	    close (sd);	    return;	  }	ifs.ifc_len = size;	if (ioctl (sd, SIOCGIFCONF, &ifs) < 0)	  goto cleanup;      } while (size - ifs.ifc_len < sizeof (struct ifreq));      num = ifs.ifc_len / sizeof (struct ifreq);      ifaddrs = malloc (num * sizeof (ifaddrs[0]));      if (!ifaddrs)	goto cleanup;      ifr = ifs.ifc_req;      for (i = 0; i < num; ++i) {	if (ifr->ifr_addr.sa_family != AF_INET)	  continue;	ifaddrs[num_ifs].addrtype = AF_INET;	memcpy (&ifaddrs[num_ifs].u.ipv4.addr,		&((struct sockaddr_in *)ifr->ifr_addr)->sin_addr, 4);	if (ioctl (sd, SIOCGIFNETMASK, if) < 0)	  continue;	memcpy (&ifaddrs[num_ifs].u.ipv4.mask,		((struct sockaddr_in *)ifr->ifr_mask)->sin_addr, 4);	++num_ifs;	/* now we're committed to this entry */      }      /* just keep enough memory to hold all the interfaces we want: */      ifaddrs = realloc (ifaddrs, num_ifs * sizeof (ifaddrs[0]));    cleanup:      close (sd);      free (ifs.ifc_buf);    }  if (num_ifs == 0)    return;  /* find an address for which we have a direct connection: */  for (i = 0; hp->h_addr_list[i]; ++i)    {      h_addr = (struct in_addr *) hp->h_addr_list[i];      for (j = 0; j < num_ifs; ++j)	{	  if_addr    = ifaddrs[j].u.ipv4.addr;	  if_netmask = ifaddrs[j].u.ipv4.mask;	  if (((h_addr->s_addr ^ if_addr) & if_netmask) == 0)	    {	      void * tmp;	      tmp		 = hp->h_addr_list[i];	      hp->h_addr_list[i] = hp->h_addr_list[0];	      hp->h_addr_list[0] = tmp;	      return;	    }	}    }#endif /* defined(SIOCGIFCONF) && ... */}