Chinaunix首页 | 论坛 | 博客
  • 博客访问: 795863
  • 博文数量: 106
  • 博客积分: 1250
  • 博客等级: 少尉
  • 技术积分: 1349
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-09 09:38
文章分类

全部博文(106)

文章存档

2014年(1)

2013年(13)

2012年(92)

分类:

2012-04-27 08:38:48


点击(此处)折叠或打开

  1. ******************************************************/
  2. /******************************************************
  3. *auth : slz
  4. *
  5. *information : the damoes display the interface speed and duplex
  6. ******************************************************/

  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <sys/ioctl.h>
  13. //#include <sys/stat.h>
  14. #include <net/if.h>
  15. #include <errno.h>
  16. #include <linux/sockios.h>

  17. #ifndef SIOCETHTOOL
  18. #define SIOCETHTOOL 0x8946
  19. #endif
  20. /* The forced speed, 10Mb, 100Mb, gigabit, 2.5Gb, 10GbE. */
  21. #define SPEED_10 10
  22. #define SPEED_100 100
  23. #define SPEED_1000 1000
  24. #define SPEED_2500 2500
  25. #define SPEED_10000 10000

  26. /* Duplex, half or full. */
  27. #define DUPLEX_HALF 0x00
  28. #define DUPLEX_FULL 0x01

  29. /* CMDs currently supported */
  30. #define ETHTOOL_GSET 0x00000001 /* Get settings. */
  31. #define ETHTOOL_SSET 0x00000002 /* Set settings. */

  32. /* hack, so we may include kernel's ethtool.h */
  33. //typedef unsigned long long __u64;
  34. typedef __uint32_t __u32; /* ditto */
  35. typedef __uint16_t __u16; /* ditto */
  36. typedef __uint8_t __u8; /* ditto */

  37. /* This should work for both 32 and 64 bit userland. */
  38. struct ethtool_cmd
  39. {
  40. __u32 cmd;
  41. __u32 supported; /* Features this interface supports */
  42. __u32 advertising; /* Features this interface advertises */
  43. __u16 speed; /* The forced speed, 10Mb, 100Mb, gigabit */
  44. __u8 duplex; /* Duplex, half or full */
  45. __u8 port; /* Which connector port */
  46. __u8 phy_address;
  47. __u8 transceiver; /* Which transceiver to use */
  48. __u8 autoneg; /* Enable or disable autonegotiation */
  49. __u32 maxtxpkt; /* Tx pkts before generating tx int */
  50. __u32 maxrxpkt; /* Rx pkts before generating rx int */
  51. __u32 reserved[4];
  52. };

  53. static int dump_ecmd (struct ethtool_cmd *ep);
  54. static int do_gset (int fd, struct ifreq *ifr);

  55. static int
  56. dump_ecmd (struct ethtool_cmd *ep)
  57. {
  58. fprintf (stdout, " Speed: ");
  59. switch (ep->speed)
  60.     {
  61.     case SPEED_10:
  62.       fprintf (stdout, "10Mb/s\n");
  63.       break;
  64.     case SPEED_100:
  65.       fprintf (stdout, "100Mb/s\n");
  66.       break;
  67.     case SPEED_1000:
  68.       fprintf (stdout, "1000Mb/s\n");
  69.       break;
  70.     case SPEED_2500:
  71.       fprintf (stdout, "2500Mb/s\n");
  72.       break;
  73.     case SPEED_10000:
  74.       fprintf (stdout, "10000Mb/s\n");
  75.       break;
  76.     default:
  77.       fprintf (stdout, "Unknown! (%i)\n", ep->speed);
  78.       break;
  79.     };
  80. fprintf (stdout, " Duplex: ");
  81. switch (ep->duplex)
  82.     {
  83.     case DUPLEX_HALF:
  84.       fprintf (stdout, "Half\n");
  85.       break;
  86.     case DUPLEX_FULL:
  87.       fprintf (stdout, "Full\n");
  88.       break;
  89.     default:
  90.       fprintf (stdout, "Unknown! (%i)\n", ep->duplex);
  91.       break;
  92.     };
  93. fprintf (stdout, " autoneg : %s\n", (ep->autoneg==1)?"on":"off");
  94. return 0;
  95. }

  96. static int
  97. do_gset (int fd, struct ifreq *ifr)
  98. {
  99. int err;
  100. struct ethtool_cmd ecmd;
  101. int allfail = 1;

  102. fprintf (stdout, "Settings for %s:\n", ifr->ifr_name);
  103. ecmd.cmd = ETHTOOL_GSET;
  104. ifr->ifr_ifru.ifru_data = (caddr_t) & ecmd;
  105. err = ioctl (fd, SIOCETHTOOL, ifr);
  106. if (err == 0)
  107.     {
  108.       err = dump_ecmd (&ecmd);
  109.       if (err)
  110.         return err;
  111.       allfail = 0;
  112.     }
  113. else if (errno != EOPNOTSUPP)
  114.     {
  115.       perror ("Cannot get device settings");
  116.     }
  117. if (allfail)
  118.     {
  119.       fprintf (stdout, "No data available\n");
  120.       return 75;
  121.     }
  122. close(fd);
  123. return 0;
  124. }

  125. int
  126. main (int argc, char *argv[])
  127. {
  128. char *device;
  129. int fd;
  130. struct ifreq ifr;

  131. if (argc > 2)
  132.     return 1;

  133. if (argc < 2)
  134.     {
  135.         printf ("input interface name \n");
  136.         return 1;
  137.     }

  138. if (argc == 0)
  139.     return 1;
  140. //while(1)
  141. //{
  142. device = argv[1];

  143. strcpy (ifr.ifr_name, device);

  144. /* Open control socket. */
  145. fd = socket (AF_INET, SOCK_DGRAM, 0);
  146. if (fd < 0)
  147.     {
  148.       perror ("Cannot get control socket");
  149.       return 70;
  150.     }

  151. do_gset (fd, &ifr);
  152. //}
  153.     return 0;
  154. }

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