Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1260290
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类: 嵌入式

2012-04-13 10:23: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. #ifdef SOLARIS
  10. #include <sys/sockio.h>
  11. #endif
  12. #define MAXINTERFACES 16
  13. main (argc, argv)
  14. register int argc;
  15. register char *argv[];
  16. {
  17. register int fd, intrface, retn = 0;
  18. struct ifreq buf[MAXINTERFACES];
  19. struct arpreq arp;
  20. struct ifconf ifc;
  21. if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
  22. {
  23. ifc.ifc_len = sizeof buf;
  24. ifc.ifc_buf = (caddr_t) buf;
  25. if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc))
  26. {
  27. //获取接口信息
  28. intrface = ifc.ifc_len / sizeof (struct ifreq);
  29. printf("interface num is intrface=%d\n\n\n",intrface);
  30. //根据借口信息循环获取设备IP和MAC地址
  31. while (intrface-- > 0)
  32. {
  33. //获取设备名称
  34. printf ("net device %s\n", buf[intrface].ifr_name);

  35. //判断网卡类型
  36. if (!(ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface])))
  37. {
  38. if (buf[intrface].ifr_flags & IFF_PROMISC)
  39. {
  40. puts ("the interface is PROMISC");
  41. retn++;
  42. }
  43. }
  44. else
  45. {
  46. char str[256];
  47. sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
  48. perror (str);
  49. }
  50. //判断网卡状态
  51. if (buf[intrface].ifr_flags & IFF_UP)
  52. {
  53. puts("the interface status is UP");
  54. }
  55. else
  56. {
  57. puts("the interface status is DOWN");
  58. }
  59. //获取当前网卡的IP地址
  60. if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
  61. {
  62. puts ("IP address is:");
  63. puts(inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));
  64. puts("");
  65. //puts (buf[intrface].ifr_addr.sa_data);
  66. }
  67. else
  68. {
  69. char str[256];
  70. sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
  71. perror (str);
  72. }
  73. /* this section can't get Hardware Address,I don't know whether the reason is module driver*/
  74. #ifdef SOLARIS
  75. //获取MAC地址
  76. arp.arp_pa.sa_family = AF_INET;
  77. arp.arp_ha.sa_family = AF_INET;
  78. ((struct sockaddr_in*)&arp.arp_pa)->sin_addr.s_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr.s_addr;
  79. if (!(ioctl (fd, SIOCGARP, (char *) &arp)))
  80. {
  81. puts ("HW address is:");
  82. //以十六进制显示MAC地址
  83. printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
  84. (unsigned char)arp.arp_ha.sa_data[0],
  85. (unsigned char)arp.arp_ha.sa_data[1],
  86. (unsigned char)arp.arp_ha.sa_data[2],
  87. (unsigned char)arp.arp_ha.sa_data[3],
  88. (unsigned char)arp.arp_ha.sa_data[4],
  89. (unsigned char)arp.arp_ha.sa_data[5]);
  90. puts("");
  91. puts("");
  92. }

  93. #else
  94. #if 0
  95. /*Get HW ADDRESS of the net card */
  96. if (!(ioctl (fd, SIOCGENADDR, (char *) &buf[intrface])))
  97. {
  98. puts ("HW address is:");
  99. printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
  100. (unsigned char)buf[intrface].ifr_enaddr[0],
  101. (unsigned char)buf[intrface].ifr_enaddr[1],
  102. (unsigned char)buf[intrface].ifr_enaddr[2],
  103. (unsigned char)buf[intrface].ifr_enaddr[3],
  104. (unsigned char)buf[intrface].ifr_enaddr[4],
  105. (unsigned char)buf[intrface].ifr_enaddr[5]);
  106. puts("");
  107. puts("");
  108. }
  109. #endif
  110. if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface])))
  111. {
  112. puts ("HW address is:");
  113. printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
  114. (unsigned char)buf[intrface].ifr_hwaddr.sa_data[0],
  115. (unsigned char)buf[intrface].ifr_hwaddr.sa_data[1],
  116. (unsigned char)buf[intrface].ifr_hwaddr.sa_data[2],
  117. (unsigned char)buf[intrface].ifr_hwaddr.sa_data[3],
  118. (unsigned char)buf[intrface].ifr_hwaddr.sa_data[4],
  119. (unsigned char)buf[intrface].ifr_hwaddr.sa_data[5]);
  120. puts("");
  121. puts("");
  122. }
  123. #endif
  124. else
  125. {
  126. char str[256];
  127. sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
  128. perror (str);
  129. }
  130. } //while
  131. } else
  132. perror ("cpm: ioctl");
  133. } else
  134. perror ("cpm: socket");
  135. close (fd);
  136. return retn;
  137. }

阅读(470) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~