473,489 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ioctl(sockfd, SIOCGIFCONF, &ifc)

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
3 23441
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5519
by: svirdi | last post by:
I have been trying to get all ip addresses from all interfaces, with the code listed below. However, all I get is the 1st IP only. I have configured 2 IPs on a single interface, I want to get both...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7352
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.