This source file includes following definitions.
- rep_freeifaddrs
- sockaddr_dup
- rep_getifaddrs
- rep_getifaddrs
- rep_getifaddrs
- rep_getifaddrs
#define SOCKET_WRAPPER_NOT_REPLACE
#include "replace.h"
#include "system/network.h"
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifndef SIOCGIFCONF
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif
#endif
#ifdef HAVE_IFACE_GETIFADDRS
#define _FOUND_IFACE_ANY
#else
void rep_freeifaddrs(struct ifaddrs *ifp)
{
if (ifp != NULL) {
free(ifp->ifa_name);
free(ifp->ifa_addr);
free(ifp->ifa_netmask);
free(ifp->ifa_dstaddr);
freeifaddrs(ifp->ifa_next);
free(ifp);
}
}
static struct sockaddr *sockaddr_dup(struct sockaddr *sa)
{
struct sockaddr *ret;
socklen_t socklen;
#ifdef HAVE_SOCKADDR_SA_LEN
socklen = sa->sa_len;
#else
socklen = sizeof(struct sockaddr_storage);
#endif
ret = calloc(1, socklen);
if (ret == NULL)
return NULL;
memcpy(ret, sa, socklen);
return ret;
}
#endif
#if HAVE_IFACE_IFCONF
int rep_getifaddrs(struct ifaddrs **ifap)
{
struct ifconf ifc;
char buff[8192];
int fd, i, n;
struct ifreq *ifr=NULL;
struct ifaddrs *curif;
struct ifaddrs *lastif = NULL;
*ifap = NULL;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
return -1;
}
ifc.ifc_len = sizeof(buff);
ifc.ifc_buf = buff;
if (ioctl(fd, SIOCGIFCONF, &ifc) != 0) {
close(fd);
return -1;
}
ifr = ifc.ifc_req;
n = ifc.ifc_len / sizeof(struct ifreq);
for (i=n-1; i>=0; i--) {
if (ioctl(fd, SIOCGIFFLAGS, &ifr[i]) == -1) {
freeifaddrs(*ifap);
return -1;
}
curif = calloc(1, sizeof(struct ifaddrs));
curif->ifa_name = strdup(ifr[i].ifr_name);
curif->ifa_flags = ifr[i].ifr_flags;
curif->ifa_dstaddr = NULL;
curif->ifa_data = NULL;
curif->ifa_next = NULL;
curif->ifa_addr = NULL;
if (ioctl(fd, SIOCGIFADDR, &ifr[i]) != -1) {
curif->ifa_addr = sockaddr_dup(&ifr[i].ifr_addr);
}
curif->ifa_netmask = NULL;
if (ioctl(fd, SIOCGIFNETMASK, &ifr[i]) != -1) {
curif->ifa_netmask = sockaddr_dup(&ifr[i].ifr_addr);
}
if (lastif == NULL) {
*ifap = curif;
} else {
lastif->ifa_next = curif;
}
lastif = curif;
}
close(fd);
return 0;
}
#define _FOUND_IFACE_ANY
#endif
#ifdef HAVE_IFACE_IFREQ
#ifndef I_STR
#include <sys/stropts.h>
#endif
int rep_getifaddrs(struct ifaddrs **ifap)
{
struct ifreq ifreq;
struct strioctl strioctl;
char buff[8192];
int fd, i, n;
struct ifreq *ifr=NULL;
struct ifaddrs *curif;
struct ifaddrs *lastif = NULL;
*ifap = NULL;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
return -1;
}
strioctl.ic_cmd = SIOCGIFCONF;
strioctl.ic_dp = buff;
strioctl.ic_len = sizeof(buff);
if (ioctl(fd, I_STR, &strioctl) < 0) {
close(fd);
return -1;
}
n = strioctl.ic_len / sizeof(struct ifreq);
if (n*sizeof(struct ifreq) + sizeof(int) == strioctl.ic_len) {
ifr = (struct ifreq *)(buff + sizeof(int));
} else {
ifr = (struct ifreq *)buff;
}
for (i = 0; i<n; i++) {
ifreq = ifr[i];
curif = calloc(1, sizeof(struct ifaddrs));
if (lastif == NULL) {
*ifap = curif;
} else {
lastif->ifa_next = curif;
}
strioctl.ic_cmd = SIOCGIFFLAGS;
strioctl.ic_dp = (char *)&ifreq;
strioctl.ic_len = sizeof(struct ifreq);
if (ioctl(fd, I_STR, &strioctl) != 0) {
freeifaddrs(*ifap);
return -1;
}
curif->ifa_flags = ifreq.ifr_flags;
strioctl.ic_cmd = SIOCGIFADDR;
strioctl.ic_dp = (char *)&ifreq;
strioctl.ic_len = sizeof(struct ifreq);
if (ioctl(fd, I_STR, &strioctl) != 0) {
freeifaddrs(*ifap);
return -1;
}
curif->ifa_name = strdup(ifreq.ifr_name);
curif->ifa_addr = sockaddr_dup(&ifreq.ifr_addr);
curif->ifa_dstaddr = NULL;
curif->ifa_data = NULL;
curif->ifa_next = NULL;
curif->ifa_netmask = NULL;
strioctl.ic_cmd = SIOCGIFNETMASK;
strioctl.ic_dp = (char *)&ifreq;
strioctl.ic_len = sizeof(struct ifreq);
if (ioctl(fd, I_STR, &strioctl) != 0) {
freeifaddrs(*ifap);
return -1;
}
curif->ifa_netmask = sockaddr_dup(&ifreq.ifr_addr);
lastif = curif;
}
close(fd);
return 0;
}
#define _FOUND_IFACE_ANY
#endif
#ifdef HAVE_IFACE_AIX
int rep_getifaddrs(struct ifaddrs **ifap)
{
char buff[8192];
int fd, i;
struct ifconf ifc;
struct ifreq *ifr=NULL;
struct ifaddrs *curif;
struct ifaddrs *lastif = NULL;
*ifap = NULL;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
return -1;
}
ifc.ifc_len = sizeof(buff);
ifc.ifc_buf = buff;
if (ioctl(fd, SIOCGIFCONF, &ifc) != 0) {
close(fd);
return -1;
}
ifr = ifc.ifc_req;
i = ifc.ifc_len;
while (i > 0) {
uint_t inc;
inc = ifr->ifr_addr.sa_len;
if (ioctl(fd, SIOCGIFADDR, ifr) != 0) {
freeaddrinfo(*ifap);
return -1;
}
curif = calloc(1, sizeof(struct ifaddrs));
if (lastif == NULL) {
*ifap = curif;
} else {
lastif->ifa_next = curif;
}
curif->ifa_name = strdup(ifr->ifr_name);
curif->ifa_addr = sockaddr_dup(&ifr->ifr_addr);
curif->ifa_dstaddr = NULL;
curif->ifa_data = NULL;
curif->ifa_netmask = NULL;
curif->ifa_next = NULL;
if (ioctl(fd, SIOCGIFFLAGS, ifr) != 0) {
freeaddrinfo(*ifap);
return -1;
}
curif->ifa_flags = ifr->ifr_flags;
if (ioctl(fd, SIOCGIFNETMASK, ifr) != 0) {
freeaddrinfo(*ifap);
return -1;
}
curif->ifa_netmask = sockaddr_dup(&ifr->ifr_addr);
lastif = curif;
next:
if (inc < sizeof(ifr->ifr_addr))
inc = sizeof(ifr->ifr_addr);
inc += IFNAMSIZ;
ifr = (struct ifreq*) (((char*) ifr) + inc);
i -= inc;
}
close(fd);
return 0;
}
#define _FOUND_IFACE_ANY
#endif
#ifndef _FOUND_IFACE_ANY
int rep_getifaddrs(struct ifaddrs **ifap)
{
errno = ENOSYS;
return -1;
}
#endif