ioctl(sockfd, SIOCGIFCONF, &ifc)

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).
 
C

Case -

chris said:
> <snip>
struct sockaddr_in *sa;
sa = (struct sockaddr_in *) &ifr->ifr_addr;
#include <arpa/inet.h>

I wonder why you #include a (system) header file halfway
a function, in between statements, instead of at the top
of the module. It's legal C allright (to #include wherever
you want), but a style I did not see before. Although I
would not prefer this style either, I can image placing an
#include just before a function definition. Or perhaps
even at the top of a function body (together with (most)
variable definitions).
 
C

chris

Case said:
I wonder why you #include a (system) header file halfway
a function, in between statements, instead of at the top
of the module. It's legal C allright (to #include wherever
you want), but a style I did not see before. Although I
would not prefer this style either, I can image placing an
#include just before a function definition. Or perhaps
even at the top of a function body (together with (most)
variable definitions).

I was just there for debug perposes, hence the debug comments. When it's
all together it's easy to get rid of.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top