本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:邢红瑞的blog
日志总数:523
评论数量:1142
留言数量:0
访问次数:9690099
建立时间:2004年12月20日




[c++]Detecting MAC Address using C application
原创空间,  文章收藏,  软件技术,  电脑与网络

邢红瑞 发表于 2010/7/9 21:09:49

#include <stdio.h>#include <fcntl.h>#include <stdlib.h>#include <string.h>#include <unistd.h> #ifdef Linux#include <sys/ioctl.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <linux/if.h>#endif #ifdef HPUX#include <netio.h>#endif #ifdef AIX#include <sys/ndd_var.h>#include <sys/kinfo.h>#endif long mac_addr_sys ( u_char *addr){/* implementation for Linux */#ifdef Linux    struct ifreq ifr;    struct ifreq *IFR;    struct ifconf ifc;    char buf[1024];    int s, i;    int ok = 0;     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;    for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; IFR++) {         strcpy(ifr.ifr_name, IFR->ifr_name);        if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0) {            if (! (ifr.ifr_flags & IFF_LOOPBACK)) {                if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {                    ok = 1;                    break;                }            }        }    }     close(s);    if (ok) {        bcopy( ifr.ifr_hwaddr.sa_data, addr, 6);    }    else {        return -1;    }    return 0;#endif /* implementation for HP-UX */#ifdef HPUX #define LAN_DEV0 "/dev/lan0"     intfd;    struct fisiocnt_block;    inti;    charnet_buf[sizeof(LAN_DEV0)+1];    char*p;     (void)sprintf(net_buf, "%s", LAN_DEV0);    p = net_buf + strlen(net_buf) - 1;     /*     * Get 802.3 address from card by opening the driver and interrogating it.     */    for (i = 0; i < 10; i++, (*p)++) {        if ((fd = open (net_buf, O_RDONLY)) != -1) {iocnt_block.reqtype = LOCAL_ADDRESS;ioctl (fd, NETSTAT, &iocnt_block);close (fd);             if (iocnt_block.vtype == 6)                break;        }    }     if (fd == -1 || iocnt_block.vtype != 6) {        return -1;    } bcopy( &iocnt_block.value.s[0], addr, 6);return 0; #endif /* HPUX */ /* implementation for AIX */#ifdef AIX     int size;    struct kinfo_ndd *nddp;     size = getkerninfo(KINFO_NDD, 0, 0, 0);    if (size <= 0) {        return -1;    }    nddp = (struct kinfo_ndd *)malloc(size);     if (!nddp) {        return -1;    }    if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) {        free(nddp);        return -1;    }    bcopy(nddp->ndd_addr, addr, 6);    free(nddp);    return 0;#endif /* Not implemented platforms */return -1;} /***********************************************************************//* * Main (only for testing) */#ifdef MAINint 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]);        }        printf( "\n");    }    else {        fprintf( stderr, "can't get MAC address\n");        exit( 1);    }    return 0;}#endif E.g you want to use on Linux, save it as file.c simply compile like this:   gcc -O2 -DMAIN -DLinux file.c -o file And run it./fileMAC address = 0008c7e9e386 Compare with /sbin/ifconfig result:/sbin/ifconfig eth0


阅读全文(3597) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144773119 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号