2 * interface monitor by Pontus Fuchs <pontus.fuchs@gmail.com>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include "iface_monitor.h"
38 #include <netlink/msg.h>
39 #include <netlink/attr.h>
40 #include <netlink/route/link.h>
42 /* libnl 1.x compatibility code */
44 #define nl_sock nl_handle
45 #define nl_socket_disable_seq_check nl_disable_sequence_check
47 static inline struct nl_handle *nl_socket_alloc(void)
49 return nl_handle_alloc();
52 static inline void nl_socket_free(struct nl_sock *h)
56 #endif /* HAVE_LIBNL1 */
58 static struct nl_sock *iface_mon_sock;
61 iface_mon_handler2(struct nl_object *obj, void *arg)
63 struct rtnl_link *filter;
64 struct rtnl_link *link_obj;
67 iface_mon_cb cb = arg;
69 filter = rtnl_link_alloc();
71 fprintf(stderr, "error allocating filter\n");
75 if (nl_object_match_filter (obj, OBJ_CAST (filter)) == 0) {
76 rtnl_link_put(filter);
80 link_obj = (struct rtnl_link *) obj;
81 flags = rtnl_link_get_flags (link_obj);
82 ifname = rtnl_link_get_name(link_obj);
84 up = (flags & IFF_UP) ? 1 : 0;
88 rtnl_link_put(filter);
94 iface_mon_handler(struct nl_msg *msg, void *arg)
96 nl_msg_parse (msg, &iface_mon_handler2, arg);
101 iface_mon_nl_init(void *arg)
105 iface_mon_sock = nl_socket_alloc();
106 if (!iface_mon_sock) {
107 fprintf(stderr, "Failed to allocate netlink socket.\n");
111 nl_socket_disable_seq_check(iface_mon_sock);
113 nl_socket_modify_cb(iface_mon_sock, NL_CB_VALID, NL_CB_CUSTOM, iface_mon_handler, arg);
115 if (nl_connect(iface_mon_sock, NETLINK_ROUTE)) {
116 fprintf(stderr, "Failed to connect to generic netlink.\n");
118 goto out_handle_destroy;
121 nl_socket_add_membership(iface_mon_sock, RTNLGRP_LINK);
126 nl_socket_free(iface_mon_sock);
131 iface_mon_event(void)
133 nl_recvmsgs_default(iface_mon_sock);
137 iface_mon_get_sock(void)
139 return nl_socket_get_fd(iface_mon_sock);
143 iface_mon_start(iface_mon_cb cb)
145 return iface_mon_nl_init(cb);
152 nl_socket_free(iface_mon_sock);
153 iface_mon_sock = NULL;
156 #else /* HAVE_LIBNL */
159 iface_mon_start(iface_mon_cb cb _U_)
170 iface_mon_get_sock(void)
176 iface_mon_event(void)
180 #endif /* HAVE_LIBNL */
182 #endif /* HAVE_LIBPCAP */