summaryrefslogtreecommitdiffstats
path: root/src/ifmonitor.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2003-08-09 16:36:18 +0000
committerLennart Poettering <lennart@poettering.net>2003-08-09 16:36:18 +0000
commit9a5162c33017bab6677c528189cff5a501f3822a (patch)
tree6da7de6655a0d667bf2693a92e9eacf20412c2c4 /src/ifmonitor.c
parent3f995b59303d7fb3b95126714b94a0354b2cdb91 (diff)
netlink monitoring aded
wireless querying improved git-svn-id: file:///home/lennart/svn/public/ifplugd/trunk@30 2bf48fe7-cfc1-0310-909f-d9042e1e0fef
Diffstat (limited to 'src/ifmonitor.c')
-rw-r--r--src/ifmonitor.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/ifmonitor.c b/src/ifmonitor.c
new file mode 100644
index 0000000..d49160b
--- /dev/null
+++ b/src/ifmonitor.c
@@ -0,0 +1,76 @@
+/* $Id: ifplugd.c 1.12 Sat, 01 Feb 2003 03:00:07 +0100 lennart $ */
+
+/*
+ * This file is part of ifplugd.
+ *
+ * ifplugd is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * ifplugd is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with ifplugd; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <stdio.h>
+#include <sys/socket.h>
+#include <linux/types.h>
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+#include <linux/if.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "nlapi.h"
+
+static int callback(struct nlmsghdr *n, void *u) {
+ int (*cb)(int b, int index, unsigned short type, const char *name) = u;
+
+ if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
+ struct rtattr *a;
+ struct ifinfomsg *i;
+ char ifname[IFNAMSIZ+1];
+ int la;
+
+ i = NLMSG_DATA(n);
+
+ if (n->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg))) {
+ fprintf(stderr, "NETLINK: Packet too small or truncated! (2)\n");
+ return -1;
+ }
+
+ memset(&ifname, 0, sizeof(ifname));
+
+ a = (void*) i + NLMSG_ALIGN(sizeof(struct ifinfomsg));
+ la = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
+
+ while (RTA_OK(a, la)) {
+
+ if(a->rta_type == IFLA_IFNAME) {
+ int l = RTA_PAYLOAD(a);
+ if (l > IFNAMSIZ)
+ l = IFNAMSIZ;
+ strncpy(ifname, RTA_DATA(a), l);
+ }
+
+ a = RTA_NEXT(a, la);
+ }
+
+ if (cb(n->nlmsg_type == RTM_NEWLINK, i->ifi_index, i->ifi_type, ifname[0] ? ifname : NULL) < 0)
+ return -1;
+ }
+
+
+ return 0;
+}
+
+int ifmonitor_init(int (*cb) (int b, int index, unsigned short type, const char *name)) {
+ return nlapi_register(callback, cb);
+}