Chinaunix首页 | 论坛 | 博客
  • 博客访问: 55105
  • 博文数量: 21
  • 博客积分: 1440
  • 博客等级: 上尉
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-25 14:47
文章分类

全部博文(21)

文章存档

2010年(9)

2009年(12)

我的朋友

分类: C/C++

2009-10-12 22:41:35

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

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

int main(int argc, char *argv[])
{
        struct mii_data *mii = NULL;
        int sockfd;
        struct ifreq ifr;

        if(argc != 2)
        {
                printf( "Useage: argv[0] ethX.\n" );
                return 0;
        }

        if((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
        {
                perror("socket");
                return -1;
        }

        memset(&ifr, '\0', sizeof(ifr));
        strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
        if(ioctl(sockfd, SIOCGMIIPHY, &ifr) == -1)
        {
                perror("ioct SIOCGMIIPHY");
                return -1;
        }

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

        if(mii->val_out & 0x0004)
                printf("link up\n");
        else
                printf("link down\n");

        close(sockfd);

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