473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 23430
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
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.