C
chris
Here is my code for the call to ioctl:
for ( ; ; ) {
if ( (buf = malloc(len)) == NULL)
errQuit("malloc error while allocating ifconf buffer");
ifc.ifc_len = len;
ifc.ifc_buf = buf;
if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
if (errno != EINVAL || lastlen != 0)
errSys("ioctl error while getting ifconf structure");
} else {
if (ifc.ifc_len == lastlen)
break;
lastlen = ifc.ifc_len;
}
len += 10;
free(buf);
}
}
And here is the code to loop through the ifreq structures and pick the
first interface that is up:
for (ptr = buf; ptr < buf + ifc.ifc_len; ) {
ifr = (struct ifreq *) ptr;
if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr))
len = ifr->ifr_addr.sa_len;
else
len = sizeof(struct sockaddr);
ptr += sizeof(ifr->ifr_name) + len;
printf("\tinterface: %s\n", ifr->ifr_name);
/* Debug stuff */
struct sockaddr_in *sa;
sa = (struct sockaddr_in *) &ifr->ifr_addr;
#include <arpa/inet.h>
printf("address: %s\n", inet_ntoa(sa->sin_addr));
/* End debug stuff */
ifrcopy = *ifr;
if (ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy) < 0)
errSys("ioctl error while getting interface flags");
if ((ifrcopy.ifr_flags & IFF_UP) && ((ifrcopy.ifr_flags &
IFF_LOOPBACK) == 0)) {
strncpy(ifname, ifr->ifr_name, IFNAMELEN);
break;
}
}
Here is the output:
Aurora:/Users/chris/dev/network/packetsniffer root# ./psinterface: lo0
address: 24.3.0.0
interface: lo0
address: 0.0.0.0
interface: lo0
address: 0.0.0.0
interface: lo0
address: 127.0.0.1
interface: gif0
address: 55.4.0.0
interface: stf0
address: 57.4.0.0
interface: en0
address: 6.3.6.0
As you can see I'm getting some weird output (especially the addresses).
I've been messing around and I can't figure out what I'm doing wrong. I
find it odd because I seem to be getting the right interface names
(except lo0 shows up more than once).
for ( ; ; ) {
if ( (buf = malloc(len)) == NULL)
errQuit("malloc error while allocating ifconf buffer");
ifc.ifc_len = len;
ifc.ifc_buf = buf;
if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
if (errno != EINVAL || lastlen != 0)
errSys("ioctl error while getting ifconf structure");
} else {
if (ifc.ifc_len == lastlen)
break;
lastlen = ifc.ifc_len;
}
len += 10;
free(buf);
}
}
And here is the code to loop through the ifreq structures and pick the
first interface that is up:
for (ptr = buf; ptr < buf + ifc.ifc_len; ) {
ifr = (struct ifreq *) ptr;
if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr))
len = ifr->ifr_addr.sa_len;
else
len = sizeof(struct sockaddr);
ptr += sizeof(ifr->ifr_name) + len;
printf("\tinterface: %s\n", ifr->ifr_name);
/* Debug stuff */
struct sockaddr_in *sa;
sa = (struct sockaddr_in *) &ifr->ifr_addr;
#include <arpa/inet.h>
printf("address: %s\n", inet_ntoa(sa->sin_addr));
/* End debug stuff */
ifrcopy = *ifr;
if (ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy) < 0)
errSys("ioctl error while getting interface flags");
if ((ifrcopy.ifr_flags & IFF_UP) && ((ifrcopy.ifr_flags &
IFF_LOOPBACK) == 0)) {
strncpy(ifname, ifr->ifr_name, IFNAMELEN);
break;
}
}
Here is the output:
Aurora:/Users/chris/dev/network/packetsniffer root# ./psinterface: lo0
address: 24.3.0.0
interface: lo0
address: 0.0.0.0
interface: lo0
address: 0.0.0.0
interface: lo0
address: 127.0.0.1
interface: gif0
address: 55.4.0.0
interface: stf0
address: 57.4.0.0
interface: en0
address: 6.3.6.0
As you can see I'm getting some weird output (especially the addresses).
I've been messing around and I can't figure out what I'm doing wrong. I
find it odd because I seem to be getting the right interface names
(except lo0 shows up more than once).