#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"
-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_address_is_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->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 free_address(flxInterfaceMonitor *m, flxInterfaceAddress *a) {
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);
}
while (i->addresses)
free_address(m, i->addresses);
+ if (i->ipv4_cache)
+ flx_cache_free(i->ipv4_cache);
+ if (i->ipv6_cache)
+ flx_cache_free(i->ipv6_cache);
+
g_assert(i->n_ipv6_addrs == 0);
g_assert(i->n_ipv4_addrs == 0);
- if (i->prev)
- i->prev->next = i->next;
- else
- m->interfaces = i->next;
-
- if (i->next)
- i->next->prev = i->prev;
-
+ FLX_LLIST_REMOVE(flxInterface, interface, m->interfaces, i);
g_hash_table_remove(m->hash_table, &i->index);
+
+ flx_cache_free(i->ipv4_cache);
+ flx_cache_free(i->ipv6_cache);
g_free(i->name);
g_free(i);
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;
}
changed = 1;
else {
i = g_new(flxInterface, 1);
+ i->monitor = m;
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;
+ FLX_LLIST_PREPEND(flxInterface, interface, m->interfaces, i);
g_hash_table_insert(m->hash_table, &i->index, i);
+ i->ipv4_cache = flx_cache_new(m->server, i);
+ i->ipv6_cache = flx_cache_new(m->server, i);
+
changed = 0;
}
a = RTA_NEXT(a, l);
}
- run_interface_callbacks(m, changed ? FLX_INTERFACE_CHANGE : FLX_INTERFACE_NEW, i);
-
+ update_interface_rr(m, i, 0);
} else if (n->nlmsg_type == RTM_DELLINK) {
struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
flxInterface *i;
- flxInterfaceAddress *a;
if (ifinfomsg->ifi_family != AF_UNSPEC)
return;
if (!(i = (flxInterface*) flx_interface_monitor_get_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);
-
+ update_interface_rr(m, i, 1);
free_interface(m, i);
} else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
else {
addr = g_new(flxInterfaceAddress, 1);
addr->address = raddr;
+ addr->interface = i;
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;
+
+ addr->rr_id = -1;
+
+ FLX_LLIST_PREPEND(flxInterfaceAddress, address, i->addresses, addr);
changed = 0;
}
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, 0);
} else {
flxInterfaceAddress *addr;
if (!(addr = get_address(m, i, &raddr)))
return;
- run_address_callbacks(m, FLX_INTERFACE_REMOVE, addr);
+ update_address_rr(m, addr, 1);
free_address(m, addr);
}
}
}
-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;
if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
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) {
g_assert(m);
g_assert(index > 0);
return g_hash_table_lookup(m->hash_table, &index);
}
-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;
-
+flxInterface* flx_interface_monitor_get_first(flxInterfaceMonitor *m) {
g_assert(m);
- g_assert(callback);
-
- info = g_new(interface_callback_info, 1);
- info->callback = callback;
- info->userdata = userdata;
- info->next = m->interface_callbacks;
- m->interface_callbacks = info;
+ return m->interfaces;
}
-void flx_interface_monitor_remove_interface_callback(
- flxInterfaceMonitor *m,
- void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterface *i, gpointer userdata),
- gpointer userdata) {
+int flx_interface_is_relevant(flxInterface *i) {
+ g_assert(i);
- interface_callback_info *info, *prev;
+ return
+ (i->flags & IFF_UP) &&
+ (i->flags & IFF_RUNNING) &&
+ !(i->flags & IFF_LOOPBACK);
+}
- g_assert(m);
- g_assert(callback);
+int flx_address_is_relevant(flxInterfaceAddress *a) {
+ g_assert(a);
- 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;
- }
- }
+ return
+ a->scope == RT_SCOPE_UNIVERSE &&
+ flx_interface_is_relevant(a->interface);
}
-void flx_interface_monitor_add_address_callback(
- flxInterfaceMonitor *m,
- void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterfaceAddress *a, gpointer userdata),
- gpointer userdata) {
-
- address_callback_info *info;
+void flx_interface_send_packet(flxInterface *i, guchar protocol, flxDnsPacket *p) {
+ g_assert(i);
+ g_assert(p);
- g_assert(m);
- g_assert(callback);
+ if ((protocol == AF_INET || protocol == AF_UNSPEC) && i->n_ipv4_addrs > 0 && flx_interface_is_relevant(i)) {
+ g_message("sending on '%s':IPv4", i->name);
+ flx_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->index, p);
+ }
- info = g_new(address_callback_info, 1);
- info->callback = callback;
- info->userdata = userdata;
- info->next = m->address_callbacks;
- m->address_callbacks = info;
+ if ((protocol == AF_INET6 || protocol == AF_UNSPEC) && i->n_ipv6_addrs > 0 && flx_interface_is_relevant(i)) {
+ g_message("sending on '%s':IPv6", i->name);
+ flx_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->index, p);
+ }
}
+void flx_interface_send_query(flxInterface *i, guchar protocol, flxKey *k) {
+ flxDnsPacket *p;
+
+ g_assert(i);
+ g_assert(k);
+
+ p = flx_dns_packet_new();
+ flx_dns_packet_set_field(p, DNS_FIELD_FLAGS, DNS_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
+ flx_dns_packet_append_key(p, k);
+ flx_dns_packet_set_field(p, DNS_FIELD_QDCOUNT, 1);
+ flx_interface_send_packet(i, protocol, p);
+ flx_dns_packet_free(p);
+}
-void flx_interface_monitor_remove_address_callback(
- flxInterfaceMonitor *m,
- void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterfaceAddress *a, gpointer userdata),
- gpointer userdata) {
+void flx_interface_send_response(flxInterface *i, guchar protocol, flxRecord *rr) {
+ flxDnsPacket *p;
+
+ g_assert(i);
+ g_assert(rr);
+
+ p = flx_dns_packet_new();
+ flx_dns_packet_set_field(p, DNS_FIELD_FLAGS, DNS_FLAGS(1, 0, 0, 0, 0, 0, 0, 0, 0, 0));
+ flx_dns_packet_append_record(p, rr, FALSE);
+ flx_dns_packet_set_field(p, DNS_FIELD_ANCOUNT, 1);
+ flx_interface_send_packet(i, protocol, p);
+ flx_dns_packet_free(p);
+}
- address_callback_info *info, *prev;
+void flx_dump_caches(flxServer *s, FILE *f) {
+ flxInterface *i;
+ g_assert(s);
- g_assert(m);
- g_assert(callback);
+ for (i = flx_interface_monitor_get_first(s->monitor); i; i = i->interface_next) {
+ if (!flx_interface_is_relevant(i))
+ continue;
+
+ if (i->n_ipv4_addrs > 0) {
+ fprintf(f, ";;; INTERFACE %s; IPv4 ;;;\n", i->name);
+ flx_cache_dump(i->ipv4_cache, f);
+ }
- info = m->address_callbacks;
- prev = NULL;
-
- while (info) {
- if (info->callback == callback && info->userdata == userdata) {
- address_callback_info *c = info;
-
- if (prev)
- prev->next = c->next;
- else
- m->address_callbacks = c->next;
-
- info = c->next;
- g_free(c);
- } else {
- prev = info;
- info = info->next;
+ if (i->n_ipv6_addrs > 0) {
+ fprintf(f, ";;; INTERFACE %s; IPv6 ;;;\n", i->name);
+ flx_cache_dump(i->ipv6_cache, f);
}
}
-
-}
-
-const flxInterface* flx_interface_monitor_get_first(flxInterfaceMonitor *m) {
- g_assert(m);
- return m->interfaces;
}