#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <errno.h>
+#include <net/if.h>
#include "iface.h"
#include "netlink.h"
+#include "dns.h"
+#include "socket.h"
+#include "announce.h"
-typedef struct _interface_callback_info {
- void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterface *i, gpointer userdata);
- gpointer userdata;
- struct _interface_callback_info *next;
-} interface_callback_info;
-
-typedef struct _address_callback_info {
- void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterfaceAddress *i, gpointer userdata);
- gpointer userdata;
- struct _address_callback_info *next;
-} address_callback_info;
-
-struct _flxInterfaceMonitor {
- flxNetlink *netlink;
- GHashTable *hash_table;
- interface_callback_info *interface_callbacks;
- address_callback_info *address_callbacks;
- flxInterface *interfaces;
- guint query_addr_seq, query_link_seq;
- enum { LIST_IFACE, LIST_ADDR, LIST_DONE } list;
-};
-
-static void run_interface_callbacks(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterface *i) {
- interface_callback_info *c;
+static void update_address_rr(flxInterfaceMonitor *m, flxInterfaceAddress *a, int remove) {
g_assert(m);
- g_assert(i);
+ g_assert(a);
- for (c = m->interface_callbacks; c; c = c->next) {
- g_assert(c->callback);
- c->callback(m, change, i, c->userdata);
+ if (!flx_interface_address_relevant(a) || remove) {
+ if (a->rr_id >= 0) {
+ flx_server_remove(m->server, a->rr_id);
+ a->rr_id = -1;
+ }
+ } else {
+ if (a->rr_id < 0) {
+ a->rr_id = flx_server_get_next_id(m->server);
+ flx_server_add_address(m->server, a->rr_id, a->interface->hardware->index, AF_UNSPEC, FALSE, m->server->hostname, &a->address);
+ }
}
}
-static void run_address_callbacks(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterfaceAddress *a) {
- address_callback_info *c;
+static void update_interface_rr(flxInterfaceMonitor *m, flxInterface *i, int remove) {
+ flxInterfaceAddress *a;
g_assert(m);
- g_assert(a);
+ g_assert(i);
- for (c = m->address_callbacks; c; c = c->next) {
- g_assert(c->callback);
- c->callback(m, change, a, c->userdata);
- }
+ for (a = i->addresses; a; a = a->address_next)
+ update_address_rr(m, a, remove);
+}
+
+static void update_hw_interface_rr(flxInterfaceMonitor *m, flxHwInterface *hw, int remove) {
+ flxInterface *i;
+
+ g_assert(m);
+ g_assert(hw);
+
+ for (i = hw->interfaces; i; i = i->by_hardware_next)
+ update_interface_rr(m, i, remove);
}
static void free_address(flxInterfaceMonitor *m, flxInterfaceAddress *a) {
g_assert(a);
g_assert(a->interface);
- if (a->address.family == AF_INET)
- a->interface->n_ipv4_addrs --;
- else if (a->address.family == AF_INET6)
- a->interface->n_ipv6_addrs --;
-
- if (a->prev)
- a->prev->next = a->next;
- else
- a->interface->addresses = a->next;
-
- if (a->next)
- a->next->prev = a->prev;
-
+ FLX_LLIST_REMOVE(flxInterfaceAddress, address, a->interface->addresses, a);
+ flx_server_remove(m->server, a->rr_id);
+
g_free(a);
}
g_assert(m);
g_assert(i);
+ flx_goodbye_interface(m->server, i, FALSE);
+ g_assert(!i->announcements);
+
while (i->addresses)
free_address(m, i->addresses);
- g_assert(i->n_ipv6_addrs == 0);
- g_assert(i->n_ipv4_addrs == 0);
+ flx_packet_scheduler_free(i->scheduler);
+ flx_cache_free(i->cache);
+
+ FLX_LLIST_REMOVE(flxInterface, interface, m->interfaces, i);
+ FLX_LLIST_REMOVE(flxInterface, by_hardware, i->hardware->interfaces, i);
+
+ g_free(i);
+}
+
+static void free_hw_interface(flxInterfaceMonitor *m, flxHwInterface *hw) {
+ g_assert(m);
+ g_assert(hw);
- if (i->prev)
- i->prev->next = i->next;
- else
- m->interfaces = i->next;
+ while (hw->interfaces)
+ free_interface(m, hw->interfaces);
- if (i->next)
- i->next->prev = i->prev;
+ FLX_LLIST_REMOVE(flxHwInterface, hardware, m->hw_interfaces, hw);
+ g_hash_table_remove(m->hash_table, &hw->index);
- g_hash_table_remove(m->hash_table, &i->index);
-
- g_free(i->name);
- g_free(i);
+ g_free(hw->name);
+ g_free(hw);
}
static flxInterfaceAddress* get_address(flxInterfaceMonitor *m, flxInterface *i, const flxAddress *raddr) {
g_assert(i);
g_assert(raddr);
- for (ia = i->addresses; ia; ia = ia->next) {
+ for (ia = i->addresses; ia; ia = ia->address_next)
if (flx_address_cmp(&ia->address, raddr) == 0)
return ia;
- }
return NULL;
}
return flx_netlink_send(nl, n, ret_seq);
}
+static void new_interface(flxInterfaceMonitor *m, flxHwInterface *hw, guchar protocol) {
+ flxInterface *i;
+
+ g_assert(m);
+ g_assert(hw);
+ g_assert(protocol != AF_UNSPEC);
+
+ i = g_new(flxInterface, 1);
+ i->monitor = m;
+ i->hardware = hw;
+ i->protocol = protocol;
+ i->relevant = FALSE;
+
+ FLX_LLIST_HEAD_INIT(flxInterfaceAddress, i->addresses);
+ FLX_LLIST_HEAD_INIT(flxAnnouncement, i->announcements);
+
+ i->cache = flx_cache_new(m->server, i);
+ i->scheduler = flx_packet_scheduler_new(m->server, i);
+
+ FLX_LLIST_PREPEND(flxInterface, by_hardware, hw->interfaces, i);
+ FLX_LLIST_PREPEND(flxInterface, interface, m->interfaces, i);
+}
+
+static void check_interface_relevant(flxInterfaceMonitor *m, flxInterface *i) {
+ gboolean b;
+ g_assert(m);
+ g_assert(i);
+
+ b = flx_interface_relevant(i);
+
+ if (b && !i->relevant) {
+ g_message("New relevant interface %s.%i", i->hardware->name, i->protocol);
+
+ flx_announce_interface(m->server, i);
+ } else if (!b && i->relevant) {
+ g_message("Interface %s.%i no longer relevant", i->hardware->name, i->protocol);
+
+ flx_goodbye_interface(m->server, i, FALSE);
+ }
+
+ i->relevant = b;
+}
+
+static void check_hw_interface_relevant(flxInterfaceMonitor *m, flxHwInterface *hw) {
+ flxInterface *i;
+
+ g_assert(m);
+ g_assert(hw);
+
+ for (i = hw->interfaces; i; i = i->by_hardware_next)
+ check_interface_relevant(m, i);
+}
+
static void callback(flxNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
flxInterfaceMonitor *m = userdata;
if (n->nlmsg_type == RTM_NEWLINK) {
struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
- flxInterface *i;
+ flxHwInterface *hw;
struct rtattr *a = NULL;
size_t l;
- int changed;
if (ifinfomsg->ifi_family != AF_UNSPEC)
return;
- if ((i = (flxInterface*) flx_interface_monitor_get_interface(m, ifinfomsg->ifi_index)))
- changed = 1;
- else {
- i = g_new(flxInterface, 1);
- i->name = NULL;
- i->index = ifinfomsg->ifi_index;
- i->addresses = NULL;
- i->n_ipv4_addrs = i->n_ipv6_addrs = 0;
- if ((i->next = m->interfaces))
- i->next->prev = i;
- m->interfaces = i;
- i->prev = NULL;
- g_hash_table_insert(m->hash_table, &i->index, i);
- changed = 0;
+ if (!(hw = g_hash_table_lookup(m->hash_table, &ifinfomsg->ifi_index))) {
+ hw = g_new(flxHwInterface, 1);
+ hw->monitor = m;
+ hw->name = NULL;
+ hw->flags = 0;
+ hw->mtu = 1500;
+ hw->index = ifinfomsg->ifi_index;
+
+ FLX_LLIST_HEAD_INIT(flxInterface, hw->interfaces);
+ FLX_LLIST_PREPEND(flxHwInterface, hardware, m->hw_interfaces, hw);
+
+ g_hash_table_insert(m->hash_table, &hw->index, hw);
+
+ if (m->server->fd_ipv4 >= 0)
+ new_interface(m, hw, AF_INET);
+ if (m->server->fd_ipv6 >= 0)
+ new_interface(m, hw, AF_INET6);
}
- i->flags = ifinfomsg->ifi_flags;
+ hw->flags = ifinfomsg->ifi_flags;
l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
a = IFLA_RTA(ifinfomsg);
while (RTA_OK(a, l)) {
switch(a->rta_type) {
case IFLA_IFNAME:
- g_free(i->name);
- i->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
+ g_free(hw->name);
+ hw->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
+ break;
+
+ case IFLA_MTU:
+ g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
+ hw->mtu = *((unsigned int*) RTA_DATA(a));
break;
default:
a = RTA_NEXT(a, l);
}
- run_interface_callbacks(m, changed ? FLX_INTERFACE_CHANGE : FLX_INTERFACE_NEW, i);
+ update_hw_interface_rr(m, hw, FALSE);
+ check_hw_interface_relevant(m, hw);
} else if (n->nlmsg_type == RTM_DELLINK) {
struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
+ flxHwInterface *hw;
flxInterface *i;
- flxInterfaceAddress *a;
if (ifinfomsg->ifi_family != AF_UNSPEC)
return;
- if (!(i = (flxInterface*) flx_interface_monitor_get_interface(m, ifinfomsg->ifi_index)))
+ if (!(hw = flx_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
return;
- for (a = i->addresses; a; a = a->next)
- run_address_callbacks(m, FLX_INTERFACE_REMOVE, a);
-
- run_interface_callbacks(m, FLX_INTERFACE_REMOVE, i);
-
- free_interface(m, i);
+ update_hw_interface_rr(m, hw, TRUE);
+ free_hw_interface(m, hw);
} else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
flxInterface *i;
struct rtattr *a = NULL;
size_t l;
- int changed;
flxAddress raddr;
int raddr_valid = 0;
if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6)
return;
- if (!(i = (flxInterface*) flx_interface_monitor_get_interface(m, ifaddrmsg->ifa_index)))
+ if (!(i = (flxInterface*) flx_interface_monitor_get_interface(m, ifaddrmsg->ifa_index, ifaddrmsg->ifa_family)))
return;
raddr.family = ifaddrmsg->ifa_family;
(raddr.family == AF_INET && RTA_PAYLOAD(a) != 4))
return;
- memcpy(raddr.data, RTA_DATA(a), RTA_PAYLOAD(a));
+ memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
raddr_valid = 1;
break;
default:
;
}
-
+
a = RTA_NEXT(a, l);
}
-
+
if (!raddr_valid)
return;
if (n->nlmsg_type == RTM_NEWADDR) {
flxInterfaceAddress *addr;
- if ((addr = get_address(m, i, &raddr)))
- changed = 1;
- else {
+ if (!(addr = get_address(m, i, &raddr))) {
addr = g_new(flxInterfaceAddress, 1);
+ addr->monitor = m;
addr->address = raddr;
-
- if (raddr.family == AF_INET)
- i->n_ipv4_addrs++;
- else if (raddr.family == AF_INET6)
- i->n_ipv6_addrs++;
-
addr->interface = i;
- if ((addr->next = i->addresses))
- addr->next->prev = addr;
- i->addresses = addr;
- addr->prev = NULL;
-
- changed = 0;
+ addr->rr_id = -1;
+
+ FLX_LLIST_PREPEND(flxInterfaceAddress, address, i->addresses, addr);
}
addr->flags = ifaddrmsg->ifa_flags;
addr->scope = ifaddrmsg->ifa_scope;
-
- run_address_callbacks(m, changed ? FLX_INTERFACE_CHANGE : FLX_INTERFACE_NEW, addr);
+
+ update_address_rr(m, addr, FALSE);
+ check_interface_relevant(m, i);
} else {
flxInterfaceAddress *addr;
if (!(addr = get_address(m, i, &raddr)))
return;
- run_address_callbacks(m, FLX_INTERFACE_REMOVE, addr);
+ update_address_rr(m, addr, TRUE);
free_address(m, addr);
+
+ check_interface_relevant(m, i);
}
} else if (n->nlmsg_type == NLMSG_DONE) {
-
+
if (m->list == LIST_IFACE) {
m->list = LIST_DONE;
- if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0) {
+ if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0)
g_warning("NETLINK: Failed to list addrs: %s", strerror(errno));
- } else
+ else
m->list = LIST_ADDR;
} else
m->list = LIST_DONE;
}
}
-flxInterfaceMonitor *flx_interface_monitor_new(GMainContext *c) {
+flxInterfaceMonitor *flx_interface_monitor_new(flxServer *s) {
flxInterfaceMonitor *m = NULL;
m = g_new0(flxInterfaceMonitor, 1);
- if (!(m->netlink = flx_netlink_new(c, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
+ m->server = s;
+ if (!(m->netlink = flx_netlink_new(s->context, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
goto fail;
m->hash_table = g_hash_table_new(g_int_hash, g_int_equal);
- m->interface_callbacks = NULL;
- m->address_callbacks = NULL;
- m->interfaces = NULL;
+
+ FLX_LLIST_HEAD_INIT(flxInterface, m->interfaces);
+ FLX_LLIST_HEAD_INIT(flxHwInterface, m->hw_interfaces);
if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
goto fail;
if (m->netlink)
flx_netlink_free(m->netlink);
+ while (m->hw_interfaces)
+ free_hw_interface(m, m->hw_interfaces);
+
+ g_assert(!m->interfaces);
+
if (m->hash_table)
g_hash_table_destroy(m->hash_table);
- while (m->interface_callbacks) {
- interface_callback_info *c = m->interface_callbacks;
- m->interface_callbacks = c->next;
- g_free(c);
- }
-
- while (m->address_callbacks) {
- address_callback_info *c = m->address_callbacks;
- m->address_callbacks = c->next;
- g_free(c);
- }
-
g_free(m);
}
-const flxInterface* flx_interface_monitor_get_interface(flxInterfaceMonitor *m, gint index) {
+flxInterface* flx_interface_monitor_get_interface(flxInterfaceMonitor *m, gint index, guchar protocol) {
+ flxHwInterface *hw;
+ flxInterface *i;
+
g_assert(m);
g_assert(index > 0);
+ g_assert(protocol != AF_UNSPEC);
- return g_hash_table_lookup(m->hash_table, &index);
+ if (!(hw = flx_interface_monitor_get_hw_interface(m, index)))
+ return NULL;
+
+ for (i = hw->interfaces; i; i = i->by_hardware_next)
+ if (i->protocol == protocol)
+ return i;
+
+ return NULL;
}
-void flx_interface_monitor_add_interface_callback(
- flxInterfaceMonitor *m,
- void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterface *i, gpointer userdata),
- gpointer userdata) {
-
- interface_callback_info *info;
-
+flxHwInterface* flx_interface_monitor_get_hw_interface(flxInterfaceMonitor *m, gint index) {
g_assert(m);
- g_assert(callback);
+ g_assert(index > 0);
+
+ return g_hash_table_lookup(m->hash_table, &index);
+}
+
+
+void flx_interface_send_packet(flxInterface *i, flxDnsPacket *p) {
+ g_assert(i);
+ g_assert(p);
+
+ if (i->relevant) {
+ g_message("sending on '%s.%i'", i->hardware->name, i->protocol);
+
+ if (i->protocol == AF_INET && i->monitor->server->fd_ipv4 >= 0)
+ flx_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p);
+ else if (i->protocol == AF_INET6 && i->monitor->server->fd_ipv6 >= 0)
+ flx_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p);
+ }
+}
+
+void flx_interface_post_query(flxInterface *i, flxKey *key) {
+ g_assert(i);
+ g_assert(key);
- info = g_new(interface_callback_info, 1);
- info->callback = callback;
- info->userdata = userdata;
- info->next = m->interface_callbacks;
- m->interface_callbacks = info;
+ if (i->relevant)
+ flx_packet_scheduler_post_query(i->scheduler, key);
}
-void flx_interface_monitor_remove_interface_callback(
- flxInterfaceMonitor *m,
- void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterface *i, gpointer userdata),
- gpointer userdata) {
- interface_callback_info *info, *prev;
+void flx_interface_post_response(flxInterface *i, flxRecord *record) {
+ g_assert(i);
+ g_assert(record);
+
+ if (i->relevant)
+ flx_packet_scheduler_post_response(i->scheduler, record);
+}
+void flx_dump_caches(flxInterfaceMonitor *m, FILE *f) {
+ flxInterface *i;
g_assert(m);
- g_assert(callback);
- info = m->interface_callbacks;
- prev = NULL;
-
- while (info) {
- if (info->callback == callback && info->userdata == userdata) {
- interface_callback_info *c = info;
-
- if (prev)
- prev->next = c->next;
- else
- m->interface_callbacks = c->next;
-
- info = c->next;
- g_free(c);
- } else {
- prev = info;
- info = info->next;
+ for (i = m->interfaces; i; i = i->interface_next) {
+ if (i->relevant) {
+ fprintf(f, ";;; INTERFACE %s.%i ;;;\n", i->hardware->name, i->protocol);
+ flx_cache_dump(i->cache, f);
}
}
}
-void flx_interface_monitor_add_address_callback(
- flxInterfaceMonitor *m,
- void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterfaceAddress *a, gpointer userdata),
- gpointer userdata) {
+gboolean flx_interface_relevant(flxInterface *i) {
+ g_assert(i);
- address_callback_info *info;
-
- g_assert(m);
- g_assert(callback);
+ return
+ (i->hardware->flags & IFF_UP) &&
+ (i->hardware->flags & IFF_RUNNING) &&
+ !(i->hardware->flags & IFF_LOOPBACK) &&
+ (i->hardware->flags & IFF_MULTICAST) &&
+ i->addresses;
+}
+
+gboolean flx_interface_address_relevant(flxInterfaceAddress *a) {
+ g_assert(a);
- info = g_new(address_callback_info, 1);
- info->callback = callback;
- info->userdata = userdata;
- info->next = m->address_callbacks;
- m->address_callbacks = info;
+ return a->scope == RT_SCOPE_UNIVERSE;
}
-void flx_interface_monitor_remove_address_callback(
- flxInterfaceMonitor *m,
- void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterfaceAddress *a, gpointer userdata),
- gpointer userdata) {
+gboolean flx_interface_match(flxInterface *i, gint index, guchar protocol) {
+ g_assert(i);
+
+ if (index > 0 && index != i->hardware->index)
+ return FALSE;
+
+ if (protocol != AF_UNSPEC && protocol != i->protocol)
+ return FALSE;
+
+ return TRUE;
+}
- address_callback_info *info, *prev;
+void flx_interface_monitor_walk(flxInterfaceMonitor *m, gint interface, guchar protocol, flxInterfaceMonitorWalkCallback callback, gpointer userdata) {
g_assert(m);
g_assert(callback);
-
- info = m->address_callbacks;
- prev = NULL;
- while (info) {
- if (info->callback == callback && info->userdata == userdata) {
- address_callback_info *c = info;
+ if (interface > 0) {
+ if (protocol != AF_UNSPEC) {
+ flxInterface *i;
- if (prev)
- prev->next = c->next;
- else
- m->address_callbacks = c->next;
+ if ((i = flx_interface_monitor_get_interface(m, interface, protocol)))
+ callback(m, i, userdata);
- info = c->next;
- g_free(c);
} else {
- prev = info;
- info = info->next;
+ flxHwInterface *hw;
+ flxInterface *i;
+
+ if ((hw = flx_interface_monitor_get_hw_interface(m, interface)))
+ for (i = hw->interfaces; i; i = i->by_hardware_next)
+ if (flx_interface_match(i, interface, protocol))
+ callback(m, i, userdata);
}
+
+ } else {
+ flxInterface *i;
+
+ for (i = m->interfaces; i; i = i->interface_next)
+ if (flx_interface_match(i, interface, protocol))
+ callback(m, i, userdata);
}
-
-}
-
-const flxInterface* flx_interface_monitor_get_first(flxInterfaceMonitor *m) {
- g_assert(m);
- return m->interfaces;
}