I am trying to programmingly find network interfaces info in FreeBSD. In linux, the interfaces are listed at /etc/network/interfaces file.
Is there any such file in FreeBSD? How can I extract that info?
you can always use getifaddrs(3)
here is an 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 fetch the link layer address of the interface you need to look for AF_PACKET sa_family, which is found in netpacket/packet.h on linux, *bsd its called AF_LINK and its 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With