加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Linux下获取eth网卡MAC地址的代码

发布时间:2020-12-16 07:42:40 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 /* * getmac.c * * Created on: 2010-11-4 * Author: carl */ #include stdio.h #include fcntl.h #include stdlib.h #include string.h #include uni

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

/* 
 * getmac.c 
 * 
 *  Created on: 2010-11-4 
 *      Author: carl 
 */ 

#include <stdio.h> 
#include <fcntl.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 

#include <sys/ioctl.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <linux/if.h> 

#define IFNAMSIZ 16 

// data structs to store interface name list 
char ifname_buf[2048]; 
char *ifnames = ifname_buf; 
int count = 0; 

void add_interface_name(const char * name) 
{ 
    int i; 
    for (i=0;i<count;i++) 
    { 
        if (!strcmp(ifnames+i*IFNAMSIZ,name)) 
            return; 
    } 
    strncpy(ifnames+(count++)*IFNAMSIZ,name,IFNAMSIZ-1); 
} 

char * get_name(char *name,char *p) 
{ 
    while (isspace(*p)) 
    p++; 
    while (*p) { 
    if (isspace(*p)) 
        break; 
    if (*p == ':') {    /* could be an alias */ 
        char *dot = p,*dotname = name; 
        *name++ = *p++; 
        while (isdigit(*p)) 
        *name++ = *p++; 
        if (*p != ':') {    /* it wasn't,backup */ 
        p = dot; 
        name = dotname; 
        } 
        if (*p == '') 
        return NULL; 
        p++; 
        break; 
    } 
    *name++ = *p++; 
    } 
    *name++ = ''; 
    return p; 
} 

// get /proc/net/dev interface name list into buffer 
// return 0 if success 
int get_procnet_list() 
{ 
    FILE *fh; 
    char buf[512]; 
    fh = fopen("/proc/net/dev","r"); 
    if (!fh) 
        return -1; 

    fgets(buf,sizeof buf,fh); /* eat title lines */ 
    fgets(buf,fh); 
    while (fgets(buf,fh)) 
    { 
        char name[IFNAMSIZ]; 
        get_name(name,buf); 
        add_interface_name(name); 
    } 
    fclose(fh); 
    return 0; 
} 

long mac_addr_sys ( u_char *addr) 
{ 
/* implementation for Linux */ 
    struct ifreq ifr; 
    struct ifreq *IFR; 
    struct ifconf ifc; 
    char buf[1024]; 
    int s,i; 
    int ok = 0; 

    // clear buffer 
    memset(ifname_buf,sizeof(ifname_buf)); 

    s = socket(AF_INET,SOCK_DGRAM,0); 
    if (s==-1) { 
        return -1; 
    } 

    ifc.ifc_len = sizeof(buf); 
    ifc.ifc_buf = buf; 
    ioctl(s,SIOCGIFCONF,&ifc); 

    IFR = ifc.ifc_req; 
    // put the ioctl interface names in the list 
    for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; IFR++) { 
            add_interface_name(IFR->ifr_name); 
    } 
    // put the /proc/net/dev interface names in the list 
    if (get_procnet_list()) 
        return -1; 

    // get the first mac address of eth* device hardware address 
    for (i = 0; i < count; i++) { 
        strcpy(ifr.ifr_name,ifnames + i*IFNAMSIZ); 
        if (!strncmp(ifr.ifr_name,"eth",3)) 
            if (ioctl(s,SIOCGIFFLAGS,&ifr) == 0) { 
                if (! (ifr.ifr_flags & IFF_LOOPBACK)) { 
                    if (ioctl(s,SIOCGIFHWADDR,&ifr) == 0) { 
                        char *p = (char *)ifr.ifr_hwaddr.sa_data; 
                        if (!*((int *)p) && !*((int *)(p+2)) ) 
                            continue; 
                        // if not 00:00:00:00:00:00,yes,we get the real mac addr 
                        ok = 1; 
                        break; 
                    } 
                } 
            } 
    } 

    close(s); 
    if (ok) { 
        bcopy( ifr.ifr_hwaddr.sa_data,addr,6); 
    } 
    else { 
        return -1; 
    } 
    return 0; 
} 

/***********************************************************************/ 
/* 
 * Main (only for testing) 
 */ 
int main( int argc,char **argv) 
{ 
    long stat; 
    int i; 
    u_char addr[6]; 

    stat = mac_addr_sys( addr); 
    if (0 == stat) { 
        printf( "MAC address = "); 
        for (i=0; i<6; ++i) { 
            printf("%2.2x",addr[i]); 
            if (i<5) 
                printf(":"); 
        } 
        printf( "n"); 
    } 
    else { 
        fprintf( stderr,"can't get MAC addressn"); 
        exit( 1); 
    } 
    return 0; 
} 

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读