Chinaunix首页 | 论坛 | 博客
  • 博客访问: 637452
  • 博文数量: 79
  • 博客积分: 2616
  • 博客等级: 少校
  • 技术积分: 1036
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-28 17:41
个人简介

苏北下邳附近人氏, 跟项羽、刘邦老乡,吕布很不幸,死在俺家门口那块小麦田上。 爱好家乡的小麦煎饼、盐豆子! 新浪微博:@dodolovely

文章分类

全部博文(79)

文章存档

2013年(2)

2012年(67)

2011年(1)

2010年(9)

分类: C/C++

2012-08-01 11:20:02


  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/param.h>
  4. #include <sys/ioctl.h>
  5. #include <sys/socket.h>
  6. #include <net/if.h>
  7. #include <netinet/in.h>
  8. #include <net/if_arp.h>

  9. #define MAXINTERFACES 16

  10. int main(int argc, char **argv)
  11. {
  12.     register int fd, interface, retn = 0;
  13.     struct ifreq buf[MAXINTERFACES];
  14.     struct arpreq arp;
  15.     struct ifconf ifc;
  16.     char mac[32] = "";
  17.  
  18.     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
  19.         ifc.ifc_len = sizeof buf;
  20.         ifc.ifc_buf = (caddr_t) buf;
  21.         if (!ioctl(fd, SIOCGIFCONF, (char *) &ifc)) {
  22.             interface = ifc.ifc_len / sizeof(struct ifreq);
  23.             printf("interface num is interface=%d\n\n", interface);
  24.             while (interface-- > 0) {
  25.                 printf("net device %s\n", buf[interface].ifr_name);
  26.  
  27.                 /*Jugde whether the net card status is promisc */
  28.                 if (!(ioctl(fd, SIOCGIFFLAGS, (char *) &buf[interface]))) {
  29.                     if (buf[interface].ifr_flags & IFF_PROMISC) {
  30.                         printf("the interface is PROMISC");
  31.                         retn ;
  32.                     }
  33.                 } else {
  34.                     char str[256] = "";
  35.  
  36.                     sprintf(str, "cpm: ioctl device %s",
  37.                             buf[interface].ifr_name);
  38.                     perror(str);
  39.                 }
  40.  
  41.                 /*Jugde whether the net card status is up */
  42.                 if (buf[interface].ifr_flags & IFF_UP) {
  43.                     printf("the interface status is UP\n");
  44.                 } else {
  45.                     printf("the interface status is DOWN\n");
  46.                 }
  47.  
  48.                 /*Get IP of the net card */
  49.                 if (!(ioctl(fd, SIOCGIFADDR, (char *) &buf[interface]))) {
  50.                     printf("IP address is:");
  51.                     printf("%s\n",
  52.                            inet_ntoa(((struct sockaddr_in
  53.                                        *) (&buf[interface].ifr_addr))->
  54.                                      sin_addr));
  55.                 } else {
  56.                     char str[256] = "";
  57.  
  58.                     sprintf(str, "cpm: ioctl device %s",
  59.                             buf[interface].ifr_name);
  60.                     perror(str);
  61.                 }
  62.  
  63.                 /*Get HW ADDRESS of the net card */
  64.                 if (!(ioctl(fd, SIOCGIFHWADDR, (char *) &buf[interface]))) {
  65.                     printf("HW address is:");
  66.  
  67.                     sprintf(mac, "%02x%02x%02x%02x%02x%02x",
  68.                             (unsigned char) buf[interface].ifr_hwaddr.
  69.                             sa_data[0],
  70.                             (unsigned char) buf[interface].ifr_hwaddr.
  71.                             sa_data[1],
  72.                             (unsigned char) buf[interface].ifr_hwaddr.
  73.                             sa_data[2],
  74.                             (unsigned char) buf[interface].ifr_hwaddr.
  75.                             sa_data[3],
  76.                             (unsigned char) buf[interface].ifr_hwaddr.
  77.                             sa_data[4],
  78.                             (unsigned char) buf[interface].ifr_hwaddr.
  79.                             sa_data[5]); // 利用sprintf转换成char *
  80.                     printf("%s\n", mac);
  81.  
  82.                     printf("\n");
  83.                 }
  84.  
  85.                 else {
  86.                     char str[256];
  87.  
  88.                     sprintf(str, "cpm: ioctl device %s",
  89.                             buf[interface].ifr_name);
  90.                     perror(str);
  91.                 }
  92.             } //end of while
  93.         } else
  94.             perror("cpm: ioctl");
  95.  
  96.     } else
  97.         perror("cpm: socket");
  98.  
  99.     close(fd);
  100.     return retn;
  101. }
编译 运行即可
阅读(1787) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~