Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FreeBSD: network interface information

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?

like image 492
hari Avatar asked Jan 18 '23 13:01

hari


1 Answers

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          
like image 160
jackdoe Avatar answered Jan 28 '23 15:01

jackdoe