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
source share