2 #include <sys/socket.h>
4 #include <linux/netlink.h>
5 #include <linux/rtnetlink.h>
11 typedef struct _interface_callback_info {
12 void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterface *i, gpointer userdata);
14 struct _interface_callback_info *next;
15 } interface_callback_info;
17 typedef struct _address_callback_info {
18 void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterfaceAddress *i, gpointer userdata);
20 struct _address_callback_info *next;
21 } address_callback_info;
23 struct _flxInterfaceMonitor {
25 GHashTable *hash_table;
26 interface_callback_info *interface_callbacks;
27 address_callback_info *address_callbacks;
28 flxInterface *interfaces;
29 guint query_addr_seq, query_link_seq;
30 enum { LIST_IFACE, LIST_ADDR, LIST_DONE } list;
33 static void run_interface_callbacks(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterface *i) {
34 interface_callback_info *c;
38 for (c = m->interface_callbacks; c; c = c->next) {
39 g_assert(c->callback);
40 c->callback(m, change, i, c->userdata);
44 static void run_address_callbacks(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterfaceAddress *a) {
45 address_callback_info *c;
49 for (c = m->address_callbacks; c; c = c->next) {
50 g_assert(c->callback);
51 c->callback(m, change, a, c->userdata);
55 static void free_address(flxInterfaceMonitor *m, flxInterfaceAddress *a) {
58 g_assert(a->interface);
61 a->prev->next = a->next;
63 a->interface->addresses = a->next;
66 a->next->prev = a->prev;
71 static void free_interface(flxInterfaceMonitor *m, flxInterface *i) {
76 free_address(m, i->addresses);
79 i->prev->next = i->next;
81 m->interfaces = i->next;
84 i->next->prev = i->prev;
86 g_hash_table_remove(m->hash_table, &i->index);
92 static flxInterfaceAddress* get_address(flxInterfaceMonitor *m, flxInterface *i, const flxAddress *raddr) {
93 flxInterfaceAddress *ia;
99 for (ia = i->addresses; ia; ia = ia->next) {
100 if (flx_address_cmp(&ia->address, raddr) == 0)
107 static int netlink_list_items(flxNetlink *nl, guint16 type, guint *ret_seq) {
109 struct rtgenmsg *gen;
112 memset(&req, 0, sizeof(req));
113 n = (struct nlmsghdr*) req;
114 n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
115 n->nlmsg_type = type;
116 n->nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
120 memset(gen, 0, sizeof(struct rtgenmsg));
121 gen->rtgen_family = AF_UNSPEC;
123 return flx_netlink_send(nl, n, ret_seq);
126 static void callback(flxNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
127 flxInterfaceMonitor *m = userdata;
131 g_assert(m->netlink == nl);
133 if (n->nlmsg_type == RTM_NEWLINK) {
134 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
136 struct rtattr *a = NULL;
140 if (ifinfomsg->ifi_family != AF_UNSPEC)
143 if ((i = (flxInterface*) flx_interface_monitor_get_interface(m, ifinfomsg->ifi_index)))
146 i = g_new0(flxInterface, 1);
147 i->index = ifinfomsg->ifi_index;
149 if ((i->next = m->interfaces))
153 g_hash_table_insert(m->hash_table, &i->index, i);
157 i->flags = ifinfomsg->ifi_flags;
159 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
160 a = IFLA_RTA(ifinfomsg);
162 while (RTA_OK(a, l)) {
163 switch(a->rta_type) {
166 i->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
176 run_interface_callbacks(m, changed ? FLX_INTERFACE_CHANGE : FLX_INTERFACE_NEW, i);
178 } else if (n->nlmsg_type == RTM_DELLINK) {
179 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
181 flxInterfaceAddress *a;
183 if (ifinfomsg->ifi_family != AF_UNSPEC)
186 if (!(i = (flxInterface*) flx_interface_monitor_get_interface(m, ifinfomsg->ifi_index)))
189 for (a = i->addresses; a; a = a->next)
190 run_address_callbacks(m, FLX_INTERFACE_REMOVE, a);
192 run_interface_callbacks(m, FLX_INTERFACE_REMOVE, i);
194 free_interface(m, i);
196 } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
198 struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
200 struct rtattr *a = NULL;
206 if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6)
209 if (!(i = (flxInterface*) flx_interface_monitor_get_interface(m, ifaddrmsg->ifa_index)))
212 raddr.family = ifaddrmsg->ifa_family;
214 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
215 a = IFA_RTA(ifaddrmsg);
217 while (RTA_OK(a, l)) {
218 switch(a->rta_type) {
220 if ((raddr.family == AF_INET6 && RTA_PAYLOAD(a) != 16) ||
221 (raddr.family == AF_INET && RTA_PAYLOAD(a) != 4))
224 memcpy(raddr.data, RTA_DATA(a), RTA_PAYLOAD(a));
238 if (n->nlmsg_type == RTM_NEWADDR) {
239 flxInterfaceAddress *addr;
241 if ((addr = get_address(m, i, &raddr)))
244 addr = g_new0(flxInterfaceAddress, 1);
245 addr->address = raddr;
247 if ((addr->next = i->addresses))
248 addr->next->prev = addr;
255 addr->flags = ifaddrmsg->ifa_flags;
256 addr->scope = ifaddrmsg->ifa_scope;
258 run_address_callbacks(m, changed ? FLX_INTERFACE_CHANGE : FLX_INTERFACE_NEW, addr);
260 flxInterfaceAddress *addr;
262 if (!(addr = get_address(m, i, &raddr)))
265 run_address_callbacks(m, FLX_INTERFACE_REMOVE, addr);
266 free_address(m, addr);
269 } else if (n->nlmsg_type == NLMSG_DONE) {
271 if (m->list == LIST_IFACE) {
274 if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0) {
275 g_warning("NETLINK: Failed to list addrs: %s", strerror(errno));
281 } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) {
282 struct nlmsgerr *e = NLMSG_DATA (n);
285 g_warning("NETLINK: Failed to browse: %s", strerror(-e->error));
289 flxInterfaceMonitor *flx_interface_monitor_new(GMainContext *c) {
290 flxInterfaceMonitor *m = NULL;
292 m = g_new0(flxInterfaceMonitor, 1);
293 if (!(m->netlink = flx_netlink_new(c, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
296 m->hash_table = g_hash_table_new(g_int_hash, g_int_equal);
297 m->interface_callbacks = NULL;
298 m->address_callbacks = NULL;
299 m->interfaces = NULL;
301 if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
304 m->list = LIST_IFACE;
309 flx_interface_monitor_free(m);
313 void flx_interface_monitor_free(flxInterfaceMonitor *m) {
317 flx_netlink_free(m->netlink);
320 g_hash_table_destroy(m->hash_table);
322 while (m->interface_callbacks) {
323 interface_callback_info *c = m->interface_callbacks;
324 m->interface_callbacks = c->next;
328 while (m->address_callbacks) {
329 address_callback_info *c = m->address_callbacks;
330 m->address_callbacks = c->next;
338 const flxInterface* flx_interface_monitor_get_interface(flxInterfaceMonitor *m, gint index) {
342 return g_hash_table_lookup(m->hash_table, &index);
345 void flx_interface_monitor_add_interface_callback(
346 flxInterfaceMonitor *m,
347 void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterface *i, gpointer userdata),
350 interface_callback_info *info;
355 info = g_new(interface_callback_info, 1);
356 info->callback = callback;
357 info->userdata = userdata;
358 info->next = m->interface_callbacks;
359 m->interface_callbacks = info;
362 void flx_interface_monitor_remove_interface_callback(
363 flxInterfaceMonitor *m,
364 void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterface *i, gpointer userdata),
367 interface_callback_info *info, *prev;
372 info = m->interface_callbacks;
376 if (info->callback == callback && info->userdata == userdata) {
377 interface_callback_info *c = info;
380 prev->next = c->next;
382 m->interface_callbacks = c->next;
393 void flx_interface_monitor_add_address_callback(
394 flxInterfaceMonitor *m,
395 void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterfaceAddress *a, gpointer userdata),
398 address_callback_info *info;
403 info = g_new(address_callback_info, 1);
404 info->callback = callback;
405 info->userdata = userdata;
406 info->next = m->address_callbacks;
407 m->address_callbacks = info;
411 void flx_interface_monitor_remove_address_callback(
412 flxInterfaceMonitor *m,
413 void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterfaceAddress *a, gpointer userdata),
416 address_callback_info *info, *prev;
421 info = m->address_callbacks;
425 if (info->callback == callback && info->userdata == userdata) {
426 address_callback_info *c = info;
429 prev->next = c->next;
431 m->address_callbacks = c->next;
443 const flxInterface* flx_interface_monitor_get_first(flxInterfaceMonitor *m) {
445 return m->interfaces;