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
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.
??>> 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;
I didn't notice any flaws when looking thorugh your
code. But I'm no expert on this part of network
programming.