FreeBSD: Network Interface Information

I am trying to program network interface information in FreeBSD. In linux, interfaces are listed in the / etc / network / interfaces file.

Is there such a file in FreeBSD? How can I extract this information?

+4
source share
1 answer

you can always use getifaddrs(3) here: exmaple:

 #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <ifaddrs.h> int main(void) { struct ifaddrs *ifap,*ifa; getifaddrs(&ifap); for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { printf("%s\n",ifa->ifa_name); } freeifaddrs(ifap); } 

EDIT: on linux, if you need to get the interface link level address, you need to look for AF_PACKET sa_family, which is in netpacket / packet.h on linux, * bsd is called AF_LINK, and in net / if_dl.h

 #ifdef AF_LINK # include <net/if_dl.h> #endif #ifdef AF_PACKET # include <netpacket/packet.h> #endif #ifdef AF_LINK #define SDL ((struct sockaddr_dl *)ifa->ifa_addr) if (SDL->sdl_family == AF_LINK) { bcopy(SDL->sdl_data + SDL->sdl_nlen,....,SDL->sdl_alen } #undef SDL #endif #ifdef AF_PACKET if (ifa->ifa_addr->sa_family == AF_PACKET) { struct sockaddr_ll *sl = (struct sockaddr_ll*) ifa->ifa_addr; bcopy(sl->sll_addr,....,sl->sll_halen } #endif 
+9
source

Source: https://habr.com/ru/post/1394842/


All Articles