source: trunk/third/ifplugd/src/ifmonitor.c @ 20388

Revision 20388, 2.3 KB checked in by amb, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r20387, which included commits to RCS files with non-trunk default branches.
Line 
1/* $Id: ifmonitor.c,v 1.1.1.1 2004-04-09 20:08:58 amb Exp $ */
2
3/*
4 * This file is part of ifplugd.
5 *
6 * ifplugd is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * ifplugd is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with ifplugd; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21#include <stdio.h>
22#include <sys/socket.h>
23#include <linux/types.h>
24#include <linux/netlink.h>
25#include <linux/rtnetlink.h>
26#include <linux/if.h>
27#include <string.h>
28#include <unistd.h>
29#include <errno.h>
30
31#include <libdaemon/dlog.h>
32
33#include "nlapi.h"
34
35static int callback(struct nlmsghdr *n, void *u) {
36    int (*cb)(int b, int index, unsigned short type, const char *name) = u;
37   
38    if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
39        struct rtattr *a;
40        struct ifinfomsg *i;
41        char ifname[IFNAMSIZ+1];
42        int la;
43       
44        i = NLMSG_DATA(n);
45       
46        if (n->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg))) {
47            daemon_log(LOG_ERR, "NETLINK: Packet too small or truncated! (2)\n");
48            return -1;
49        }
50
51        memset(&ifname, 0, sizeof(ifname));
52       
53        a = (void*) i + NLMSG_ALIGN(sizeof(struct ifinfomsg));
54        la = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
55               
56        while (RTA_OK(a, la)) {
57           
58            if(a->rta_type == IFLA_IFNAME) {
59                int l = RTA_PAYLOAD(a);
60                if (l > IFNAMSIZ)
61                    l = IFNAMSIZ;
62                strncpy(ifname, RTA_DATA(a), l);
63            }
64                       
65            a = RTA_NEXT(a, la);
66        }
67
68        if (cb(n->nlmsg_type == RTM_NEWLINK, i->ifi_index, i->ifi_type, ifname[0] ? ifname : NULL) < 0)
69            return -1;
70    }
71
72   
73    return 0;
74}
75
76int ifmonitor_init(int (*cb) (int b, int index, unsigned short type, const char *name)) {
77    return nlapi_register(callback, cb);
78}
Note: See TracBrowser for help on using the repository browser.