LINUX检测网线热插拔事件

在嵌入式产品上,如何让用户选择有线还是无线上网呢?如何让他们判断使用了哪种方式上网呢?我们是这样处理的:优先使用网线上网,稳定。即程序自动判断,如果插了网线,则使用网线上网,如果没有网线连接,则无线上网。

所以,问题的重点是如何判断用户是否插了网线!

一、读取/proc/net/dev

$ cat /proc/net/dev

Inter-| Receive | Transmit

face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carriercompressed

lo: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

eth0: 3439 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0

在开发板上/proc/net目录下,还有很多关于网络的信息的文件,如果启动开发板后,eth0中bytes、packets 不为0,那它一定插了网线,但此种方法只适合开机启动时判断,之后的话,就很麻烦了。

获取/proc/net/dev的信息,需要借助getline()函数和sscanf()函数。可链接:getline()函数

二、通过ifconfig的RUNNING判断

1、插网线:

$ ifconfig eth0

eth0 Link encap:Ethernet HWaddr 06:ED:B5:C3:AA:23

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:159 errors:0 dropped:0overruns:0 frame:0

TX packets:0 errors:0 dropped:0overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:23822 (23.2 KiB) TX bytes:0 (0.0 B)

Interrupt:14

2、拔网线:

/mnt/wifi$ ifconfig eth0

eth0 Link encap:Ethernet HWaddr 06:ED:B5:C3:AA:23

UP BROADCAST MULTICAST MTU:1500 Metric:1

RX packets:160 errors:0 dropped:0overruns:0 frame:0

TX packets:0 errors:0 dropped:0overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:24055 (23.4 KiB) TX bytes:0 (0.0 B)

Interrupt:14

通过红字的RUNNING来区分是否插拔网线。

三、通过Netlink实现网卡上下线监控

#include <sys/types.h>  
#include <sys/socket.h>  
#include <asm/types.h>  
#include <linux/netlink.h>  
#include <linux/rtnetlink.h>  
#include <unistd.h>
#include <stdlib.h>  
#include <stdio.h>  
#include <sys/ioctl.h>  
#include <linux/if.h>  
#include <string.h>  
  
#define BUFLEN 20480  
  
int main(int argc, char *argv[])  
{  
    int fd, retval;  
    char buf[BUFLEN] = {0};  
    int len = BUFLEN;  
    struct sockaddr_nl addr;  
    struct nlmsghdr *nh;  
    struct ifinfomsg *ifinfo;  
    struct rtattr *attr;  
  
    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 -1;  
            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));  
                    break;  
                }  
            }  
            printf("
");  
        }  
    }  
  
    return 0;  
}
经验分享 程序员 微信小程序 职场和发展