An ugly way to get MAC Address in FreeBSD

char ethaddr[6];
    struct ifaddrs *list;
    if(getifaddrs(&list) < 0)
    {
        perror("getifaddrs");
    }
        
    struct ifaddrs *cur;        
    for(cur = list; cur != NULL; cur = cur->ifa_next)
    {
        if(cur->ifa_addr->sa_family != AF_LINK)
            continue;
        if(cur->ifa_name!=if_name)
            continue;
        int i;
        for (i=9;i<15;i++)
        ethaddr[i-9]=cur->ifa_addr->sa_data[i];
    }
        
    freeifaddrs(list);

I’ll refer to some books to see the meaning of ifa_addr->sa_data when ifa_addr is an ethernet address…
Currently I konw:
sa_data:
0 byte 1
1 byte 0
2 byte 6 // addr len?
3 byte 3 // name len?
4 byte 6 // addr len?
5 byte 0
6,7,8 name[3]
9-14 addr[6]
… and something more…
Old Blog Link: http://computer.mblogger.cn/henryhu/posts/61231.aspx

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.