Date: Thu, 4 Jan 2001 18:48:28 +0100 From: ecureuil <ecureuil@easynet.fr> To: hackers@FreeBSD.org Subject: getifaddrs Message-ID: <20010104184828.A12168@foret>
next in thread | raw e-mail | index | archive | help
[-- Attachment #1 --]
Hi
I'm trying to get interfaces' list and infos with getifaddrs().
The ifa_data struct should contain all needed information, but
this is a NULL pointer for IPv4 interfaces. Why ?
a sample output for my ethernet card :
ed0 ( 6.3.6.0) : UP, AF_INET6 MTU=1500 (struct ifa_data)
ed0 ( 10.0.0.1) : UP MTU=1500 (struct ifa_data is a null pointer)
BTW, there is always 2 nodes for each interface, even if INET6 is not
enabled in the kernel. is it normal ?
thanks (see the source attached)
--
ecureuil <ecureuil@easynet.fr>
[-- Attachment #2 --]
/* ecureuil <ecureuil@easynet.fr>
get interface information
*/
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <unistd.h>
#include <ifaddrs.h>
int
main(int argc, char **argv)
{
struct ifaddrs *ifap, *ifa;
struct sockaddr_in *sa;
int fd;
struct ifreq ifr;
char ifstraddr[16];
sa_family_t fam;
if (getifaddrs(&ifap) < 0) {
perror("getifaddrs");
return -1;
}
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
printf("%6s ", ifa->ifa_name);
/* get if address */
sa = (struct sockaddr_in *) ifa->ifa_addr;
strncpy(ifstraddr, inet_ntoa(sa->sin_addr), 16);
printf("(%15s) : ",ifstraddr);
if (!(ifa->ifa_flags & IFF_UP)) printf("DOWN");
else printf("UP");
if (ifa->ifa_flags & IFF_PROMISC) printf(", PROMISCUOUS");
if (ifa->ifa_addr->sa_family & AF_INET6) printf(", AF_INET6");
/* get if mtu */
fam = ifa->ifa_addr->sa_family == AF_LINK ? AF_INET : ifa->ifa_addr->sa_family;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("[get_if_name] socket(AF_INET, SOCK_DGRAM, 0)");
return -1;
}
strncpy(ifr.ifr_name,ifa->ifa_name,IFNAMSIZ);
if (ioctl(fd, SIOCGIFMTU, &ifr) < 0 ) {
perror("ioctl");
return -1;
} else {
printf(" MTU=%d ",ifr.ifr_mtu);
}
close(fd);
if(ifa->ifa_data) printf("(struct ifa_data)\n");
else printf("(struct ifa_data is a null pointer)\n");
}
freeifaddrs(ifap);
return 0;
}
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010104184828.A12168>
