Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1999910
  • 博文数量: 369
  • 博客积分: 10093
  • 博客等级: 上将
  • 技术积分: 4271
  • 用 户 组: 普通用户
  • 注册时间: 2005-03-21 00:59
文章分类

全部博文(369)

文章存档

2013年(1)

2011年(2)

2010年(10)

2009年(16)

2008年(33)

2007年(146)

2006年(160)

2005年(1)

分类: LINUX

2010-04-02 13:36:26

在Linux下可以用rtnetlink监听网卡的链路通断,具体代码如下:


#include <sys/types.h>
#include <sys/socket.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if.h>
#include <string.h>

#define BUFLEN 32768

int main(int argc, char *argv[])
{
        int fd, retval;
        char *buf;
        struct sockaddr_nl addr;
        struct nlmsghdr *nh;
        int len = BUFLEN;
        struct ifinfomsg *ifinfo;
        struct rtattr *attr;

        buf = malloc(BUFLEN);
        fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
        setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len));
        memset(&addr, 0, sizeof(addr));
        addr.nl_family = AF_NETLINK;
        addr.nl_groups = RTNLGRP_LINK;
        bind(fd, (struct sockaddr*)&addr, sizeof(addr));
        while ((retval = read(fd, buf, BUFLEN)) > 0) {
                for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, retval);
                     nh = NLMSG_NEXT(nh, retval)) {
                        if (nh->nlmsg_type == NLMSG_DONE)
                                break;
                        else if (nh->nlmsg_type == NLMSG_ERROR)
                                return;
                        else if (nh->nlmsg_type != RTM_NEWLINK)
                                continue;
                        ifinfo = NLMSG_DATA(nh);
                        printf("%u: %s", ifinfo->ifi_index,
                                (ifinfo->ifi_flags & IFF_LOWER_UP) ?
                                "up" : "down" );
                        attr = (struct rtattr*)(((char*)nh) +
                                                NLMSG_SPACE(sizeof(*ifinfo)));
                        len = nh->nlmsg_len - NLMSG_SPACE(sizeof(*ifinfo));
                        for (; RTA_OK(attr, len); attr = RTA_NEXT(attr, len)) {
                                if (attr->rta_type == IFLA_IFNAME)
                                        printf(" %s", (char*)RTA_DATA(attr));
                                else if (attr->rta_type == IFLA_MASTER)
                                        printf(" [%u]",
                                               *(__u32*)RTA_DATA(attr));
                        }
                        printf("\n");
                }
        }

        return 0;
}


以上代码只是demo,没有错误检查,也可当做netlink的使用示例,仅此而已。

运行起来后,插拔网线,显示结果如下:

# ./a.out                                               
2: down eth0 [4]                                                          
2: down eth0                                                              
2: up eth0 [4]                                                            
2: up eth0

其中ifindex 2是eth0,ifindex 4是br0, eth0为br0的一个slave。
阅读(4159) | 评论(1) | 转发(0) |
0

上一篇:RPS进入Linux Kernel

下一篇:connection socket pool

给主人留下些什么吧!~~

chinaunix网友2010-04-02 22:30:32

哈哈,内核自己也会输出网卡up/down信息到syslog