all: flexmdns prioq-test
-flexmdns: main.o iface.o netlink.o server.o address.o util.o local.o prioq.o
+flexmdns: main.o iface.o netlink.o server.o address.o util.o prioq.o
$(CC) -o $@ $^ $(LIBS)
#test-llist: test-llist.o
#include <stdio.h>
#include <glib.h>
-#include "address.h"
-
struct _flxServer;
typedef struct _flxServer flxServer;
+#include "address.h"
+
typedef struct {
gchar *name;
guint16 type;
void flx_server_add_rr(flxServer *s, gint id, gint interface, guchar protocol, const flxRecord *rr);
void flx_server_add(flxServer *s, gint id, gint interface, guchar protocol, const gchar *name, guint16 type, gconstpointer data, guint size);
void flx_server_add_address(flxServer *s, gint id, gint interface, guchar protocol, const gchar *name, flxAddress *a);
+void flx_server_add_text(flxServer *s, gint id, gint interface, guchar protocol, const gchar *name, const gchar *text);
void flx_server_remove(flxServer *s, gint id);
struct _flxLocalAddrSource;
typedef struct _flxLocalAddrSource flxLocalAddrSource;
-flxLocalAddrSource *flx_local_addr_source_new(flxServer *s);
-void flx_local_addr_source_free(flxLocalAddrSource *l);
-
enum {
FLX_DNS_TYPE_A = 0x01,
FLX_DNS_TYPE_NS = 0x02,
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <errno.h>
+#include <net/if.h>
#include "iface.h"
#include "netlink.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, 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->next)
+ update_address_rr(m, a, remove);
}
static void free_address(flxInterfaceMonitor *m, flxInterfaceAddress *a) {
g_assert(i);
g_assert(raddr);
- for (ia = i->addresses; ia; ia = ia->next) {
+ for (ia = i->addresses; ia; ia = ia->next)
if (flx_address_cmp(&ia->address, raddr) == 0)
return ia;
- }
return NULL;
}
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) {
addr->next->prev = addr;
i->addresses = addr;
addr->prev = NULL;
+ addr->rr_id = -1;
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);
}
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;
-
- 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;
-}
-
-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;
-
+const flxInterface* flx_interface_monitor_get_first(flxInterfaceMonitor *m) {
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;
- }
- }
+ return m->interfaces;
}
-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;
-
- g_assert(m);
- g_assert(callback);
+int flx_interface_is_relevant(flxInterface *i) {
+ g_assert(i);
- info = g_new(address_callback_info, 1);
- info->callback = callback;
- info->userdata = userdata;
- info->next = m->address_callbacks;
- m->address_callbacks = info;
+ return
+ (i->flags & IFF_UP) &&
+ (i->flags & IFF_RUNNING) &&
+ !(i->flags & IFF_LOOPBACK);
}
+int flx_address_is_relevant(flxInterfaceAddress *a) {
+ g_assert(a);
-void flx_interface_monitor_remove_address_callback(
- flxInterfaceMonitor *m,
- void (*callback)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterfaceAddress *a, gpointer userdata),
- gpointer userdata) {
-
- address_callback_info *info, *prev;
-
- 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 (prev)
- prev->next = c->next;
- else
- m->address_callbacks = c->next;
-
- info = c->next;
- g_free(c);
- } else {
- prev = info;
- info = info->next;
- }
- }
-
-}
-
-const flxInterface* flx_interface_monitor_get_first(flxInterfaceMonitor *m) {
- g_assert(m);
- return m->interfaces;
+ return
+ a->scope == RT_SCOPE_UNIVERSE &&
+ flx_interface_is_relevant(a->interface);
}
#include <glib.h>
-#include "address.h"
-
struct _flxInterfaceMonitor;
typedef struct _flxInterfaceMonitor flxInterfaceMonitor;
struct _flxInterface;
typedef struct _flxInterface flxInterface;
+#include "address.h"
+#include "server.h"
+#include "netlink.h"
+
+struct _flxInterfaceMonitor {
+ flxServer *server;
+ flxNetlink *netlink;
+ GHashTable *hash_table;
+
+ flxInterface *interfaces;
+
+ guint query_addr_seq, query_link_seq;
+
+ enum { LIST_IFACE, LIST_ADDR, LIST_DONE } list;
+};
+
struct _flxInterface {
gchar *name;
gint index;
flxInterface *interface;
flxInterfaceAddress *next, *prev;
-};
-typedef enum { FLX_INTERFACE_NEW, FLX_INTERFACE_REMOVE, FLX_INTERFACE_CHANGE } flxInterfaceChange;
+ gint rr_id;
+};
-flxInterfaceMonitor *flx_interface_monitor_new(GMainContext *c);
+flxInterfaceMonitor *flx_interface_monitor_new(flxServer *server);
void flx_interface_monitor_free(flxInterfaceMonitor *m);
const flxInterface* flx_interface_monitor_get_interface(flxInterfaceMonitor *m, gint index);
const flxInterface* flx_interface_monitor_get_first(flxInterfaceMonitor *m);
-void flx_interface_monitor_add_interface_callback(
- flxInterfaceMonitor *m,
- void (*cb)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterface *i, gpointer userdata),
- gpointer userdata);
-
-void flx_interface_monitor_remove_interface_callback(
- flxInterfaceMonitor *m,
- void (*cb)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterface *i, gpointer userdata),
- gpointer userdata);
-
-void flx_interface_monitor_add_address_callback(
- flxInterfaceMonitor *m,
- void (*cb)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterfaceAddress *a, gpointer userdata),
- gpointer userdata);
-
-void flx_interface_monitor_remove_address_callback(
- flxInterfaceMonitor *m,
- void (*cb)(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterfaceAddress *a, gpointer userdata),
- gpointer userdata);
-
+int flx_interface_is_relevant(flxInterface *i);
+int flx_address_is_relevant(flxInterfaceAddress *a);
+
#endif
+++ /dev/null
-#include <sys/socket.h>
-#include <asm/types.h>
-#include <linux/netlink.h>
-#include <linux/rtnetlink.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <sys/utsname.h>
-#include <net/if.h>
-
-#include "flx.h"
-#include "server.h"
-#include "util.h"
-#include "iface.h"
-
-typedef struct {
- flxAddress address;
- flxServer *server;
- gint id;
-} addr_info;
-
-struct _flxLocalAddrSource {
- flxServer *server;
- GHashTable *hash_table;
- gint hinfo_id;
- gchar *hostname;
-};
-
-static gboolean addr_equal(gconstpointer a, gconstpointer b) {
- return flx_address_cmp(a, b) == 0;
-}
-
-static guint hash(gconstpointer v, guint l) {
- const guint8 *c;
- guint hash = 0;
-
- for (c = v; l > 0; c++, l--)
- hash = 31 * hash + *c;
-
- return hash;
-}
-
-static guint addr_hash(gconstpointer v) {
- const flxAddress *a = v;
-
- return hash(a->data, flx_address_get_size(a));
-}
-
-static void remove_addr(flxLocalAddrSource *l, const flxInterfaceAddress *a) {
- flxAddress foo;
- g_assert(l);
- g_assert(a);
-
- memset(&foo, 0, sizeof(foo));
- foo.family = AF_INET;
-
- g_hash_table_remove(l->hash_table, &foo);
-}
-
-static void add_addr(flxLocalAddrSource *l, const flxInterfaceAddress *a) {
- addr_info *ai;
- g_assert(l);
- g_assert(a);
-
- if (g_hash_table_lookup(l->hash_table, &a->address))
- return; /* Entry already existant */
-
- ai = g_new(addr_info, 1);
- ai->server = l->server;
- ai->address = a->address;
-
- ai->id = flx_server_get_next_id(l->server);
-
- flx_server_add_address(l->server, ai->id, a->interface->index, AF_UNSPEC, l->hostname, &ai->address);
-
- g_hash_table_replace(l->hash_table, &ai->address, ai);
-}
-
-static void handle_addr(flxLocalAddrSource *l, const flxInterfaceAddress *a) {
- g_assert(l);
- g_assert(a);
-
- if (!(a->interface->flags & IFF_UP) ||
- !(a->interface->flags & IFF_RUNNING) ||
- (a->interface->flags & IFF_LOOPBACK) ||
- a->scope != RT_SCOPE_UNIVERSE)
-
- remove_addr(l, a);
- else
- add_addr(l, a);
-}
-
-/* Called whenever a new address becomes available, is changed or removed on the local machine */
-static void addr_callback(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterfaceAddress *a, gpointer userdata) {
- flxLocalAddrSource *l = userdata;
- g_assert(m);
- g_assert(a);
- g_assert(l);
-
- if (change == FLX_INTERFACE_REMOVE)
- remove_addr(l, a);
- else
- handle_addr(l, a);
-}
-
-/* Called whenever a new interface becomes available, is changed or removed on the local machine */
-static void interface_callback(flxInterfaceMonitor *m, flxInterfaceChange change, const flxInterface *i, gpointer userdata) {
- flxLocalAddrSource *l = userdata;
- g_assert(m);
- g_assert(i);
- g_assert(l);
-
- if (change == FLX_INTERFACE_CHANGE) {
- flxInterfaceAddress *a;
-
- for (a = i->addresses; a; a = a->next)
- handle_addr(l, a);
- }
-}
-
-static void destroy(gpointer data) {
- addr_info *ai = data;
- flx_server_remove(ai->server, ai->id);
- g_free(ai);
-}
-
-flxLocalAddrSource *flx_local_addr_source_new(flxServer *s) {
- flxLocalAddrSource *l;
- const flxInterface *i;
- struct utsname utsname;
- gint length;
- gchar *e, *hn, *c;
-
- l = g_new(flxLocalAddrSource, 1);
- l->server = s;
- l->hash_table = g_hash_table_new_full(addr_hash, addr_equal, NULL, destroy);
-
- hn = flx_get_host_name();
- if ((e = strchr(hn, '.')))
- *e = 0;
-
- l->hostname = g_strdup_printf("%s.local.", hn);
- g_free(hn);
-
- flx_interface_monitor_add_address_callback(s->monitor, addr_callback, l);
- flx_interface_monitor_add_interface_callback(s->monitor, interface_callback, l);
-
- for (i = flx_interface_monitor_get_first(s->monitor); i; i = i->next) {
- flxInterfaceAddress *a;
-
- for (a = i->addresses; a; a = a->next)
- add_addr(l, a);
- }
-
- l->hinfo_id = flx_server_get_next_id(l->server);
-
- uname(&utsname);
- c = g_strdup_printf("%s%c%s%n", g_strup(utsname.machine), 0, g_strup(utsname.sysname), &length);
-
- flx_server_add(l->server, l->hinfo_id, 0, AF_UNSPEC,
- l->hostname, FLX_DNS_TYPE_HINFO, c, length+1);
- g_free(c);
-
- return l;
-}
-
-void flx_local_addr_source_free(flxLocalAddrSource *l) {
- g_assert(l);
-
- flx_interface_monitor_remove_address_callback(l->server->monitor, addr_callback, l);
- g_hash_table_destroy(l->hash_table);
- flx_server_remove(l->server, l->hinfo_id);
- g_free(l->hostname);
- g_free(l);
-}
int main(int argc, char *argv[]) {
flxServer *flx;
- flxLocalAddrSource *l;
- flxAddress a;
gchar *r;
flxQuery q;
flx = flx_server_new(NULL);
- l = flx_local_addr_source_new(flx);
-
- flx_address_parse("127.0.0.1", AF_INET, &a);
- flx_server_add_address(flx, 0, 0, AF_UNSPEC, "localhost", &a);
-
- flx_address_parse("::1", AF_INET6, &a);
- flx_server_add_address(flx, 0, 0, AF_UNSPEC, "ip6-localhost", &a);
-
+ flx_server_add_text(flx, 0, 0, AF_UNSPEC, NULL, "hallo");
+
q.name = "campari.local.";
q.class = FLX_DNS_CLASS_IN;
q.type = FLX_DNS_TYPE_A;
flx_server_dump(flx, stdout);
- flx_local_addr_source_free(l);
flx_server_free(flx);
return 0;
}
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
+#include <sys/utsname.h>
#include "server.h"
#include "util.h"
}
flxServer *flx_server_new(GMainContext *c) {
+ gchar *hn, *e, *hinfo;
+ struct utsname utsname;
+ gint length;
+ flxAddress a;
flxServer *s = g_new(flxServer, 1);
if (c) {
s->query_job_queue = flx_prio_queue_new(query_job_instance_compare);
s->response_job_queue = flx_prio_queue_new(response_job_instance_compare);
- s->monitor = flx_interface_monitor_new(s->context);
+ s->monitor = flx_interface_monitor_new(s);
+
+ /* Get host name */
+ hn = flx_get_host_name();
+ if ((e = strchr(hn, '.')))
+ *e = 0;
+
+ s->hostname = g_strdup_printf("%s.local.", hn);
+ g_free(hn);
+
+ /* Fill in HINFO rr */
+ s->hinfo_rr_id = flx_server_get_next_id(s);
+
+ uname(&utsname);
+ hinfo = g_strdup_printf("%s%c%s%n", g_strup(utsname.machine), 0, g_strup(utsname.sysname), &length);
+ flx_server_add(s, s->hinfo_rr_id, 0, AF_UNSPEC,
+ s->hostname, FLX_DNS_TYPE_HINFO, hinfo, length+1);
+
+
+ /* Add localhost entries */
+ flx_address_parse("127.0.0.1", AF_INET, &a);
+ flx_server_add_address(s, 0, 0, AF_UNSPEC, "localhost", &a);
+
+ flx_address_parse("::1", AF_INET6, &a);
+ flx_server_add_address(s, 0, 0, AF_UNSPEC, "ip6-localhost", &a);
+
return s;
}
flx_prio_queue_free(s->query_job_queue);
flx_prio_queue_free(s->response_job_queue);
-
- flx_interface_monitor_free(s->monitor);
+ flx_interface_monitor_free(s->monitor);
+
flx_server_remove(s, 0);
g_hash_table_destroy(s->rrset_by_id);
g_hash_table_destroy(s->rrset_by_name);
g_main_context_unref(s->context);
+
+ g_free(s->hostname);
g_free(s);
}
snprintf(t, sizeof(t), "'%s' '%s'", (char*) e->rr.data, s2);
}
+ } else if (e->rr.type == FLX_DNS_TYPE_TXT) {
+ size_t l;
+
+ l = e->rr.size;
+ if (l > sizeof(t)-1)
+ l = sizeof(t)-1;
+
+ memcpy(t, e->rr.data, l);
+ t[l] = 0;
}
}
void flx_server_add_address(flxServer *s, gint id, gint interface, guchar protocol, const gchar *name, flxAddress *a) {
gchar *n;
g_assert(s);
- g_assert(name);
g_assert(a);
- n = flx_normalize_name(name);
+ n = flx_normalize_name(name ? name : s->hostname);
if (a->family == AF_INET) {
gchar *r;
g_free(n);
}
+void flx_server_add_text(flxServer *s, gint id, gint interface, guchar protocol, const gchar *name, const gchar *text) {
+ gchar *n;
+ g_assert(s);
+ g_assert(text);
+
+ n = flx_normalize_name(name ? name : s->hostname);
+ flx_server_add(s, id, interface, protocol, n, FLX_DNS_TYPE_TXT, text, strlen(text));
+ g_free(n);
+}
+
+
flxQueryJob* flx_query_job_new(void) {
flxQueryJob *job = g_new(flxQueryJob, 1);
job->query.name = NULL;
#ifndef fooflxserverhfoo
#define fooflxserverhfoo
+struct _flxEntry;
+typedef struct _flxEntry flxEntry;
+
#include "flx.h"
#include "iface.h"
#include "prioq.h"
-struct _flxEntry;
-typedef struct _flxEntry flxEntry;
struct _flxEntry {
flxRecord rr;
gint id;
flxPrioQueue *query_job_queue;
flxPrioQueue *response_job_queue;
+
+ gint hinfo_rr_id;
+
+ gchar *hostname;
};
flxQueryJob* flx_query_job_new(void);