Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2549113
  • 博文数量: 351
  • 博客积分: 76
  • 博客等级: 上将
  • 技术积分: 3555
  • 用 户 组: 普通用户
  • 注册时间: 2004-11-13 21:27
文章分类

全部博文(351)

文章存档

2013年(1)

2012年(4)

2011年(7)

2010年(16)

2009年(34)

2008年(34)

2007年(34)

2006年(68)

2005年(82)

2004年(71)

分类: LINUX

2007-11-15 14:21:26

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

#include
#include
#include
#include
#include
#include
#include
#include

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

struct mii_data
{
        __u16 phy_id;
        __u16 reg_num;
        __u16 val_in;
        __u16 val_out;
};

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

        int skfd = 0;
        if( ( skfd = socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 )
        {
                perror( "socket" );
                return -1;
        }

        struct ifreq ifr;
        bzero( &ifr, sizeof( ifr ) );
        strncpy( ifr.ifr_name, argv[1], IFNAMSIZ - 1 );
        ifr.ifr_name[IFNAMSIZ - 1] = 0;
        if( ioctl( skfd, SIOCGMIIPHY, &ifr ) < 0 )
        {
                perror( "ioctl" );
                return -1;
        }

        struct mii_data* mii = NULL;
        mii = (struct mii_data*)&ifr.ifr_data;
        mii->reg_num = 0x01;
        if( ioctl( skfd, SIOCGMIIREG, &ifr ) < 0 )
        {
                perror( "ioctl2" );
                return -1;
        }

        if( mii->val_out & 0x0004 )
                printf( "Linkup\n" );
        else
                printf( "Linkdown\n" );

        close( skfd );
        return 0;
}
阅读(3922) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~