By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,382 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,382 IT Pros & Developers. It's quick & easy.

ioctl(sockfd, SIOCGIFCONF, &ifc)

P: n/a
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).
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
chris <ch***@misery.net> scribbled the following:
Here is my code for the call to ioctl:


ioctl is not a standard C function. Please ask in comp.unix.programmer.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The question of copying music from the Internet is like a two-barreled sword."
- Finnish rap artist Ezkimo
Nov 14 '05 #2

P: n/a
chris wrote:
<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).
printf("address: %s\n", inet_ntoa(sa->sin_addr));
<snip>


Nov 14 '05 #3

P: n/a
Case - wrote:
chris wrote:
> <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).
printf("address: %s\n", inet_ntoa(sa->sin_addr));

> <snip>


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

This discussion thread is closed

Replies have been disabled for this discussion.