免费注册 查看新帖 |

Chinaunix

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

请大家帮我看看我这个程序,看看那里有问题,是取MAC地址的 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-09-03 15:13 |只看该作者 |倒序浏览
#include <stdio.h>;
#include <sys/types.h>;
#include <sys/param.h>;

#include <sys/ioctl.h>;
#include <sys/socket.h>;
#include <net/if.h>;
#include <netinet/in.h>;
#include <net/if_arp.h>;

#ifdef SOLARIS
#include <sys/sockio.h>;
#endif

#define MAXINTERFACES   16

main (argc, argv)
register int argc;
register char *argv[];
{
   register 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);
                }
            else {
               char str[256];

               sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
               perror (str);
           }
/* 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)))
                {
                /* if(intrface==1){
                      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("";
                   }*/
                }


#else
#if 0
/*Get HW ADDRESS of the net card */
            if (!(ioctl (fd,  SIOCGENADDR, (char *) &buf[intrface])))
                {
                /* if(intrface==1){
                    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])))
                {
                 if(intrface==1){
                   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;
}

执行时候没有错误,取出来的MAC前为0?
为什么啊?

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2005-09-03 15:27 |只看该作者

请大家帮我看看我这个程序,看看那里有问题,是取MAC地址的

  1. #include <stdio.h>;
  2. #include <unistd.h>;
  3. #include <netdb.h>;
  4. #include <netinet/in.h>;
  5. #include <net/if.h>;
  6. #include <sys/ioctl.h>;
  7. #include <error.h>;
  8. #include <string.h>;
  9. #include <stdlib.h>;
  10. #include <arpa/inet.h>;

  11. int main(int argc,char *argv[])
  12. {
  13.     int sock;
  14.     struct ifreq ifreq;
  15.     struct sockaddr_in addr;
  16.     struct protoent *pro;
  17.     if(argc!=2)
  18.     {
  19.         printf("Usage: ethname\n");
  20.         exit(0);
  21.     }
  22.     pro=getprotobyname("tcp");
  23.     if((sock=socket(AF_INET,SOCK_STREAM,pro->;p_proto))<0)
  24.     {
  25.         perror("socket");
  26.         exit(1);
  27.     }
  28.     strcpy(ifreq.ifr_name,argv[1]);
  29.     if(ioctl(sock,SIOCGIFADDR,&ifreq))
  30.     {
  31.         perror("ioctl");
  32.         exit(2);
  33.     }
  34.     bcopy(&ifreq.ifr_addr,&addr,sizeof(ifreq.ifr_addr));
  35.     printf("%s :%s\n",argv[1],inet_ntoa(addr.sin_addr));
  36.     if(ioctl(sock,SIOCGIFHWADDR,&ifreq))
  37.     {
  38.         perror("ioctl");
  39.         exit(2);
  40.     }
  41.     printf("mac address :%02x:%02x:%02x:%02x:%02x:%02x\n",
  42.             (unsigned char)ifreq.ifr_hwaddr.sa_data[0],
  43.             (unsigned char)ifreq.ifr_hwaddr.sa_data[1],
  44.             (unsigned char)ifreq.ifr_hwaddr.sa_data[2],
  45.             (unsigned char)ifreq.ifr_hwaddr.sa_data[3],
  46.             (unsigned char)ifreq.ifr_hwaddr.sa_data[4],
  47.             (unsigned char)ifreq.ifr_hwaddr.sa_data[5]
  48.           );
  49.     exit(0);

  50. }
复制代码


这是我以前写的一个程序.你测试一下在你机器上.我今天心情不好.没心思给别人看程序.sorry.

论坛徽章:
0
3 [报告]
发表于 2005-09-03 15:50 |只看该作者

请大家帮我看看我这个程序,看看那里有问题,是取MAC地址的

没有关系,谢谢你

论坛徽章:
0
4 [报告]
发表于 2005-09-03 15:55 |只看该作者

请大家帮我看看我这个程序,看看那里有问题,是取MAC地址的

我昨天在网上找到份代码,取MAC的,但今天用的不是自己的机器。。。。。你可以到网上找一下。
那段代码比你的要小的多。

论坛徽章:
0
5 [报告]
发表于 2005-09-03 15:56 |只看该作者

请大家帮我看看我这个程序,看看那里有问题,是取MAC地址的

不行  我是新手。请多指教

论坛徽章:
0
6 [报告]
发表于 2005-09-03 15:59 |只看该作者

请大家帮我看看我这个程序,看看那里有问题,是取MAC地址的

找不到 555555555555

论坛徽章:
0
7 [报告]
发表于 2005-09-03 16:09 |只看该作者

请大家帮我看看我这个程序,看看那里有问题,是取MAC地址的

request mac 00:00:00:00:00:00,
request netmask 255.0.0.0
request IP 127.0.0.1
MAC地址怎么会是前零啊?

论坛徽章:
0
8 [报告]
发表于 2005-09-03 16:13 |只看该作者

请大家帮我看看我这个程序,看看那里有问题,是取MAC地址的

我是在uClinux里面运行的,和这个没有关系吧?

论坛徽章:
0
9 [报告]
发表于 2005-09-03 18:36 |只看该作者

请大家帮我看看我这个程序,看看那里有问题,是取MAC地址的

段错误是什么啊?

论坛徽章:
0
10 [报告]
发表于 2005-09-03 18:41 |只看该作者

请大家帮我看看我这个程序,看看那里有问题,是取MAC地址的

原帖由 "xiongpeng" 发表:
request mac 00:00:00:00:00:00,
request netmask 255.0.0.0
request IP 127.0.0.1
MAC地址怎么会是前零啊?


IP 127.0.0.1是回还接口,它的MAC当然测不出
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP