免费注册 查看新帖 |

Chinaunix

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

检测网卡物理连接的方法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-06-14 14:34 |只看该作者 |倒序浏览
看到近期总有朋友提问在Linux下怎么检测网卡的物理连接,在这里把我用的方法贴出来,可能适用范围不广,仅以供参考,

  1. /* Check network adapter is up or down */
  2. // Note: one or two adapter drivers may be not support this method
  3. // exemple: based virtual machine

  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/socket.h>
  8. #include <sys/ioctl.h>
  9. #include <net/if.h>
  10. #include <linux/sockios.h>
  11. #include <linux/types.h>

  12. const char* VERSION = "0.6.0.48 --maintain by isnowran";

  13. struct mii_data
  14. {
  15.         __u16 phy_id;
  16.         __u16 reg_num;
  17.         __u16 val_in;
  18.         __u16 val_out;
  19. };

  20. int main( int argc, char* argv[] )
  21. {
  22.         if( argc != 2 )
  23.         {
  24.                 printf( "Version: %s\n", VERSION );
  25.                 printf( "Useage: argv[0] ethNO.\n" );
  26.                 return 0;
  27.         }

  28.         int skfd = 0;
  29.         if( ( skfd = socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 )
  30.         {
  31.                 perror( "socket" );
  32.                 return -1;
  33.         }

  34.         struct ifreq ifr;
  35.         bzero( &ifr, sizeof( ifr ) );
  36.         strncpy( ifr.ifr_name, argv[1], IFNAMSIZ - 1 );
  37.         ifr.ifr_name[IFNAMSIZ - 1] = 0;
  38.         if( ioctl( skfd, SIOCGMIIPHY, &ifr ) < 0 )
  39.         {
  40.                 perror( "ioctl" );
  41.                 return -1;
  42.         }

  43.         struct mii_data* mii = NULL;
  44.         mii = (struct mii_data*)&ifr.ifr_data;
  45.         mii->reg_num = 0x01;
  46.         if( ioctl( skfd, SIOCGMIIREG, &ifr ) < 0 )
  47.         {
  48.                 perror( "ioctl2" );
  49.                 return -1;
  50.         }

  51.         if( mii->val_out & 0x0004 )
  52.                 printf( "Linkup\n" );
  53.         else
  54.                 printf( "Linkdown\n" );

  55.         close( skfd );
  56.         return 0;
  57. }
复制代码

[ 本帖最后由 isnowran 于 2006-6-14 21:24 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-06-14 15:24 |只看该作者
谢谢,但在redhat9下出错,错误提示为
ioctl: Operation not supported

论坛徽章:
0
3 [报告]
发表于 2006-06-14 15:57 |只看该作者
你是真实环境还是虚拟机啊?

论坛徽章:
0
4 [报告]
发表于 2006-06-14 16:00 |只看该作者

回复 3楼 isnowran 的帖子

真实的环境

论坛徽章:
0
5 [报告]
发表于 2006-06-14 16:13 |只看该作者
我又测了一台redhat9机器和一台vpc虚拟机;
前者正常,虚拟机下为,ioctl: No such device,但是最开始的那台机器却是ioctl: Operation not supported

命令行为./a.out eth0

论坛徽章:
0
6 [报告]
发表于 2006-06-14 16:19 |只看该作者
可能跟网卡型号有关,我也在某些网卡上碰到ioctl失败,所以,代码开头注释

  1. /* Check network adapter is up or down */
  2. // Note: one or two adapter drivers may be not support this method
  3. // exemple: based virtual machine
复制代码

不过,我在常用的螃蟹卡、D-link、或者e100、e1000、intel的以及via的rhine上都没问题

论坛徽章:
0
7 [报告]
发表于 2006-06-14 16:39 |只看该作者
哦,明白,谢谢
不能正确使用的网卡:
divert: allocating divert_blk for eth0
eth0: Intel(R) PRO/1000 Network Connection
e1000: eth0 NIC Link is Up 100 Mbps Full Duplex
正确使用的网卡:
divert: allocating divert_blk for eth0
eth0: RealTek RTL8139 Fast Ethernet at 0xe08e6000, 00:07:95:12:e3:80, IRQ 10
eth0:  Identified 8139 chip type 'RTL-8139B'
eth0: Setting 100mbps full-duplex based on auto-negotiated partner ability 45e1.

论坛徽章:
0
8 [报告]
发表于 2006-06-14 17:01 |只看该作者
哦,对,我的 e1000 也不行,修正一下

论坛徽章:
0
9 [报告]
发表于 2006-06-14 17:33 |只看该作者
谢下先,检测一下~~~

论坛徽章:
0
10 [报告]
发表于 2006-06-14 23:37 |只看该作者
跟网卡的驱动有关,驱动中要是实现这样的功能,
就可以检测到。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP