免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1629 | 回复: 0
打印 上一主题 下一主题

获得网卡的mac地址 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-23 13:18 |只看该作者 |倒序浏览

               
                /*Time-stamp:
*filename:getmac.c
*author  :yechao
*description:get ip and mac address of the machines
*compile
*      solaris:gcc getip.c -DSOLARIS -lsocket -lnsl
*      linux  :gcc getip.c
*/
#include
#include
#include
#include
#include
#include
#include
#include
#ifdef SOLARIS
#include
#endif
#define MAXINTERFACES   16
int  main (int argc, char **argv)
{
  int fd, intrface, retn = 0;
   struct ifreq buf[MAXINTERFACES];
   struct arpreq arp;
   struct ifconf ifc;
   if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
     {
       ifc.ifc_len = sizeof buf;
       ifc.ifc_buf = (caddr_t) buf;
       if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc))
         {
           intrface = ifc.ifc_len / sizeof (struct ifreq);
           printf("interface num is intrface=%d\n\n\n",intrface);
           while (intrface-- > 0)
             {
               printf ("net device %s\n", buf[intrface].ifr_name);
               /*Jugde whether the net card status is promisc  */
               if (!(ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface])))
                 {
                   if (buf[intrface].ifr_flags & IFF_PROMISC)
                     {
                       puts ("the interface is PROMISC");
                       retn++;
                     }
                 } else
                   {
                     char str[256];
                     sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
                     perror (str);
                   }
               /*Jugde whether the net card status is up       */
               if (buf[intrface].ifr_flags & IFF_UP)
                 {
                   puts("the interface status is UP");
                 }
               else
                 {
                   puts("the interface status is DOWN");
                 }
               /*Get IP of the net card */
               if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
                 {
                   puts ("IP address is:");
                   puts(inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));
                   puts("");
                   //puts (buf[intrface].ifr_addr.sa_data);
                 } /* End of if (!(ioctl(fd,SIOCGIFADDR,(char*) &buf[intrface]))) */
               else/*else of if (!(ioctl(fd,SIOCGIFADDR,(char*) &buf[intrface]))) */  
                 {
                   char str[256];
                   sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
                   perror (str);
                 }//End of if(!(ioctl(fd,SIOCGIFADDR,(char *) &buf[intrface])))
               /* this section can't get Hardware Address,I don't know whether the reason is module driver*/
       //          ((struct sockaddr_in*)&arp.arp_pa)->;sin_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->;sin_addr;
#ifdef SOLARIS
               arp.arp_pa.sa_family = AF_INET;
               arp.arp_ha.sa_family = AF_INET;
       ((struct sockaddr_in*)&arp.arp_pa)->sin_addr.s_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr.s_addr;
               if (!(ioctl (fd, SIOCGARP, (char *) &arp)))
                 {
                   puts ("HW address is:");
                   printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
                        (unsigned char)arp.arp_ha.sa_data[0],
                        (unsigned char)arp.arp_ha.sa_data[1],
                        (unsigned char)arp.arp_ha.sa_data[2],
                        (unsigned char)arp.arp_ha.sa_data[3],
                        (unsigned char)arp.arp_ha.sa_data[4],
                        (unsigned char)arp.arp_ha.sa_data[5]);
                 puts("");
                 puts("");
                 }//End of if(!(ioctl(fd,SIOCGARP,(char*) &arp)))
#else
#if 0
/*Get HW ADDRESS of the net card */
            if (!(ioctl (fd,  SIOCGENADDR, (char *) &buf[intrface])))
              {
                 puts ("HW address is:");
                 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
                                (unsigned char)buf[intrface].ifr_enaddr[0],
                                (unsigned char)buf[intrface].ifr_enaddr[1],
                                (unsigned char)buf[intrface].ifr_enaddr[2],
                                (unsigned char)buf[intrface].ifr_enaddr[3],
                                (unsigned char)buf[intrface].ifr_enaddr[4],
                                (unsigned char)buf[intrface].ifr_enaddr[5]);
                 puts("");
                 puts("");
              }
#endif
            if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface])))
                {
                 puts ("HW address is:");
                 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
                                (unsigned char)buf[intrface].ifr_hwaddr.sa_data[0],
                                (unsigned char)buf[intrface].ifr_hwaddr.sa_data[1],
                                (unsigned char)buf[intrface].ifr_hwaddr.sa_data[2],
                                (unsigned char)buf[intrface].ifr_hwaddr.sa_data[3],
                                (unsigned char)buf[intrface].ifr_hwaddr.sa_data[4],
                                (unsigned char)buf[intrface].ifr_hwaddr.sa_data[5]);
                 puts("");
                 puts("");
                }
#endif
            else {
               char str[256];
               sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
               perror (str);
           }
        }
      } else
        perror ("cpm: ioctl");
   } else
     perror ("cpm: socket");
   close (fd);
    return retn;
} /* End of function main(int argc,char **argv) */
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/19582/showart_116802.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP