openwrt 获取网口MAC地址

static int get_mac(char* mac,char* brifc)
{
    struct ifreq tmp;
    int sock_mac;
    char mac_addr[32]={0};
    sock_mac = socket(AF_INET, SOCK_STREAM, 0);
    if( sock_mac == -1)
    {
        perror("create socket fail\n");
        return -1;
    }
    memset(&tmp,0,sizeof(tmp));
    strncpy(tmp.ifr_name,brifc,sizeof(tmp.ifr_name)-1 );
    if( (ioctl( sock_mac, SIOCGIFHWADDR, &tmp)) < 0 )
    {
        printf("mac ioctl error\n");
        return -1;
    }
    sprintf(mac_addr, "%02x:%02x:%02x:%02x:%02x:%02x",\
    (unsigned char)tmp.ifr_hwaddr.sa_data[0],
    (unsigned char)tmp.ifr_hwaddr.sa_data[1],
    (unsigned char)tmp.ifr_hwaddr.sa_data[2],
    (unsigned char)tmp.ifr_hwaddr.sa_data[3],
    (unsigned char)tmp.ifr_hwaddr.sa_data[4],
    (unsigned char)tmp.ifr_hwaddr.sa_data[5]
    );
    close(sock_mac);
    memcpy(mac,mac_addr,strlen(mac_addr));
    return 0;
}

参数char* brifc为接口名,如:br-lan、br0等等;

你可能感兴趣的:(openwrt,linux)