Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

get the list of network interfaces

0 views
Skip to first unread message

Roman Mashak

unread,
Jun 26, 2005, 9:48:59 PM6/26/05
to
Hello, All!

I'd like to get the list of network interfaces available on the system
(linux, kernel 2.4.20) in my C-program.
Using 'ifreq' structure and appropriate 'ioctl' doesn't fit me, cause I
should specify interface name in 'ifr_name' field, that's why I need %subj%.

Thanks.

With best regards, Roman Mashak. E-mail: m...@tusur.ru


Kasper Dupont

unread,
Jun 27, 2005, 1:15:46 AM6/27/05
to
Roman Mashak wrote:
>
> Hello, All!
>
> I'd like to get the list of network interfaces available on the system
> (linux, kernel 2.4.20) in my C-program.
> Using 'ifreq' structure and appropriate 'ioctl' doesn't fit me, cause I
> should specify interface name in 'ifr_name' field, that's why I need %subj%.

http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#IP
(The server is down at the moment, but I expect it
to be online again in an hour or two).

--
Kasper Dupont -- der bruger for meget tid på usenet.
Note to self: Don't try to allocate 256000 pages
with GFP_KERNEL on x86.

Roman Mashak

unread,
Jun 27, 2005, 1:59:27 AM6/27/05
to
Hello, Kasper!
You wrote on Mon, 27 Jun 2005 07:15:46 +0200:

??>> Using 'ifreq' structure and appropriate 'ioctl' doesn't fit me, cause
??>> I should specify interface name in 'ifr_name' field, that's why I need
??>> %subj%.

KD> http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#IP
KD> (The server is down at the moment, but I expect it
KD> to be online again in an hour or two).
Thanks a lot, I'll read the FAQ. By now, after deep reading of man's and
some HOWTOs I wrote the following code, it does what I need, but I'm not
sure it's all correct and flawless:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <net/if.h>
#include <sys/ioctl.h>

int main(void)
{
int sd; /* socket descriptor */
char buf[BUFSIZ];
char *device;
struct ifconf ifc;

/* zero out structure */
memset(&ifc, 0, sizeof ifc);

ifc.ifc_len = sizeof buf;
ifc.ifc_buf = (char *)buf;

if ( (sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket() error");
exit(1);
}

if ( -1 == ioctl(sd, SIOCGIFCONF, &ifc) ) {
perror("ioctl() error");
exit(1);
}

int i;
for (i = 0; i < (ifc.ifc_len / sizeof(struct ifreq)); i++ ) {
device = (ifc.ifc_req++)->ifr_name;
printf("interfaces: %s\n", device);
}

close(sd);

return 0;

Kasper Dupont

unread,
Jun 27, 2005, 2:29:15 AM6/27/05
to
Roman Mashak wrote:
>
> it does what I need, but I'm not
> sure it's all correct and flawless:

I didn't notice any flaws when looking thorugh your
code. But I'm no expert on this part of network
programming.

0 new messages