7

我编写了一个程序,如果网络接口发生变化,特别是新的出现或存在的已经消失,需要得到通知。我的研究带来了netlink及其RTMGRP_LINK信号。这个手册页给出了一个我还不清楚的例子。

它有这个代码:

memset(&sa, 0, sizeof(sa));
snl.nl_family = AF_NETLINK;
snl.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;

fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
bind(fd, (struct sockaddr*)&sa, sizeof(sa));

我的理解是这是启用某些信号的初始化部分。我想一定有一个处理事件的处理程序子程序。

我的问题是:

  • 这是启用事件的正确代码片段吗?

  • 如何处理事件:我需要一些处理程序吗?

  • 包含相关信息(关于网络接口及其更改)的数据结构是什么?

谢谢你的帮助。

4

2 回答 2

8

以下代码取自此处,我添加了获取相关接口名称的逻辑。

    #include <asm/types.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #include <errno.h>
    #include <stdio.h>
    #include <string.h>
    #include <net/if.h>
    #include <netinet/in.h>

    #include <linux/netlink.h>
    #include <linux/rtnetlink.h>

    #define MYPROTO NETLINK_ROUTE
    #define MYMGRP RTMGRP_IPV4_ROUTE

    int open_netlink()
    {
        int sock = socket(AF_NETLINK,SOCK_RAW,MYPROTO);
        struct sockaddr_nl addr;

        memset((void *)&addr, 0, sizeof(addr));

        if (sock<0)
            return sock;
        addr.nl_family = AF_NETLINK;
        addr.nl_pid = getpid();
        addr.nl_groups = RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR;
        if (bind(sock,(struct sockaddr *)&addr,sizeof(addr))<0)
            return -1;
        return sock;
    }


    int read_event(int sockint, int (*msg_handler)(struct sockaddr_nl *,
                                                   struct nlmsghdr *))
    {
        int status;
        int ret = 0;
        char buf[4096];
        struct iovec iov = { buf, sizeof buf };
        struct sockaddr_nl snl;
        struct msghdr msg = { (void*)&snl, sizeof snl, &iov, 1, NULL, 0, 0};
        struct nlmsghdr *h;

        status = recvmsg(sockint, &msg, 0);

        if(status < 0)
        {
            /* Socket non-blocking so bail out once we have read everything */
            if (errno == EWOULDBLOCK || errno == EAGAIN)
                return ret;

            /* Anything else is an error */
            printf("read_netlink: Error recvmsg: %d\n", status);
            perror("read_netlink: Error: ");
            return status;
        }

        if(status == 0)
        {
            printf("read_netlink: EOF\n");
        }

        /* We need to handle more than one message per 'recvmsg' */
        for(h = (struct nlmsghdr *) buf; NLMSG_OK (h, (unsigned int)status); 
        h = NLMSG_NEXT (h, status))
        {
            /* Finish reading */
            if (h->nlmsg_type == NLMSG_DONE)
                return ret;

            /* Message is some kind of error */
            if (h->nlmsg_type == NLMSG_ERROR)
            {
                printf("read_netlink: Message is an error - decode TBD\n");
                return -1; // Error
            }

            /* Call message handler */
            if(msg_handler)
            {
                ret = (*msg_handler)(&snl, h);
                if(ret < 0)
                {
                    printf("read_netlink: Message hander error %d\n", ret);
                    return ret;
                }
            }
            else
            {
                printf("read_netlink: Error NULL message handler\n");
                return -1;
            }
        }



        return ret;
    }

    static int netlink_link_state(struct sockaddr_nl *nl, struct nlmsghdr *msg)
    {
        int len;
        struct ifinfomsg *ifi;

        nl = nl;

        ifi = NLMSG_DATA(msg);
        char ifname[1024];if_indextoname(ifi->ifi_index,ifname);

        printf("netlink_link_state: Link %s %s\n", 
                /*(ifi->ifi_flags & IFF_RUNNING)?"Up":"Down");*/
            ifname,(ifi->ifi_flags & IFF_UP)?"Up":"Down");
        return 0;
    }

    static int msg_handler(struct sockaddr_nl *nl, struct nlmsghdr *msg)
    {
        struct ifinfomsg *ifi=NLMSG_DATA(msg); /* for RTM_{NEW,DEL}ADDR */
        struct ifaddrmsg *ifa=NLMSG_DATA(msg); /* for RTM_{NEW,DEL}LINK */
        char ifname[1024];
        switch (msg->nlmsg_type)
        {
            case RTM_NEWADDR:
                if_indextoname(ifa->ifa_index,ifname);
                printf("msg_handler: RTM_NEWADDR : %s\n",ifname);
                break;
            case RTM_DELADDR:
                if_indextoname(ifa->ifa_index,ifname);
                printf("msg_handler: RTM_DELADDR : %s\n",ifname);
                break;
            case RTM_NEWLINK:
                if_indextoname(ifi->ifi_index,ifname);
                printf("msg_handler: RTM_NEWLINK\n");
                netlink_link_state(nl, msg);
                break;
            case RTM_DELLINK:
                if_indextoname(ifi->ifi_index,ifname);
                printf("msg_handler: RTM_DELLINK : %s\n",ifname);
                break;
            default:
                printf("msg_handler: Unknown netlink nlmsg_type %d\n",
                        msg->nlmsg_type);
                break;
        }
        return 0;
    }
    int main(int argc, char *argv[])
    {
        int nls = open_netlink();
        printf("Started watching:\n");
        if (nls<0) {
            printf("Open Error!");
        }
        while (1)
            read_event(nls, msg_handler);
        return 0;
    }
于 2014-11-27T11:08:28.677 回答
2
#include<stdio.h>
#include <asm/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <netinet/in.h>

#include <linux/netlink.h>
#include <linux/rtnetlink.h>
static void network_loop(void *data)
{
    struct sockaddr_nl addr;
    int nls, len, rtl;
    char buffer[4096];
    struct nlmsghdr *nlh;
    struct ifaddrmsg *ifa;
    struct rtattr *rth;

    if ((nls = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
        printf("MY_PROG: socket failure\n");

    memset (&addr,0,sizeof(addr));
    addr.nl_family = AF_NETLINK;
    addr.nl_groups = RTMGRP_IPV4_IFADDR;

    if (bind(nls, (struct sockaddr *)&addr, sizeof(addr)) == -1)
        printf("MY_PROG:network_loop bind failure\n");

    nlh = (struct nlmsghdr *)buffer;
    while ((len = recv(nls,nlh,4096,0)) > 0)
    {
        for (; (NLMSG_OK (nlh, len)) && (nlh->nlmsg_type != NLMSG_DONE); nlh = NLMSG_NEXT(nlh, len))
        {
            if (nlh->nlmsg_type == RTM_NEWADDR)
                printf("MY_PROG:network_loop RTM_NEWADDR\n");
            else if (nlh->nlmsg_type == RTM_DELADDR)
                printf("MY_PROG:network_loop RTM_DELADDR\n");
            else
                printf("MY_PROG:network_loop nlmsg_type=%d\n", nlh->nlmsg_type);

            if ((nlh->nlmsg_type != RTM_NEWADDR) && (nlh->nlmsg_type != RTM_DELADDR)) continue; /* some other kind of announcement */

            ifa = (struct ifaddrmsg *) NLMSG_DATA (nlh);

            rth = IFA_RTA (ifa);
            rtl = IFA_PAYLOAD (nlh);
            for (; rtl && RTA_OK (rth, rtl); rth = RTA_NEXT (rth,rtl))
            {
                char name[IFNAMSIZ];
                uint32_t ipaddr;

                if (rth->rta_type != IFA_LOCAL) continue;

                ipaddr = * ((uint32_t *)RTA_DATA(rth));
                ipaddr = htonl(ipaddr);

                printf("MY_PROG:network_loop %s is now %X\n",if_indextoname(ifa->ifa_index,name),ipaddr);
            }
        }
    }

}

main()
{
    network_loop("MYPROG");
    printf("EXIT main()\n");
}

了解Linux系统中网络变化通知并经过测试的简单程序。

于 2017-08-22T09:27:49.983 回答