]> git.meshlink.io Git - catta/commitdiff
fold local.c into iface.c
authorLennart Poettering <lennart@poettering.net>
Tue, 18 Jan 2005 00:17:40 +0000 (00:17 +0000)
committerLennart Poettering <lennart@poettering.net>
Tue, 18 Jan 2005 00:17:40 +0000 (00:17 +0000)
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@7 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

Makefile
flx.h
iface.c
iface.h
local-addr.h [deleted file]
local.c [deleted file]
main.c
server.c
server.h

index 1be90ce5678c3dd8bbc95444b267f6b126c796e7..7aaa056d3a55169460ebc7c6af8e8ba145a9b6f4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ LIBS=$(shell pkg-config --libs glib-2.0)
 
 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
diff --git a/flx.h b/flx.h
index 419ab86efe5af22c3ccf81d45983388a354be7b2..dad707e7bf1ea27d147ff893610f06f7d8b32e4b 100644 (file)
--- a/flx.h
+++ b/flx.h
@@ -4,11 +4,11 @@
 #include <stdio.h>
 #include <glib.h>
 
-#include "address.h"
-
 struct _flxServer;
 typedef struct _flxServer flxServer;
 
+#include "address.h"
+
 typedef struct  {
     gchar *name;
     guint16 type;
@@ -32,6 +32,7 @@ gint flx_server_get_next_id(flxServer *s);
 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);
 
@@ -44,9 +45,6 @@ void flx_server_dump(flxServer *s, FILE *f);
 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,
diff --git a/iface.c b/iface.c
index 61d7b51f401bc0877cf0706f5006a0756a6b3aa8..4bcd2d9de3fb228138116d36c16cd3114ea855d1 100644 (file)
--- a/iface.c
+++ b/iface.c
@@ -4,52 +4,35 @@
 #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) {
@@ -104,10 +87,9 @@ static flxInterfaceAddress* get_address(flxInterfaceMonitor *m, flxInterface *i,
     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;
 }
@@ -183,12 +165,10 @@ static void callback(flxNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
             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;
@@ -196,11 +176,7 @@ static void callback(flxNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
         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) {
@@ -266,21 +242,22 @@ static void callback(flxNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
                     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);
         }
                 
@@ -304,16 +281,15 @@ static void callback(flxNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
     }
 }
 
-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)
@@ -337,18 +313,6 @@ void flx_interface_monitor_free(flxInterfaceMonitor *m) {
     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);
 }
 
@@ -360,105 +324,24 @@ const flxInterface* flx_interface_monitor_get_interface(flxInterfaceMonitor *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);
 }
diff --git a/iface.h b/iface.h
index 49bcf200995f2b9241c0b49f485087caba62b4d7..90bfc05857623adf02012ac5d54ad5de46786914 100644 (file)
--- a/iface.h
+++ b/iface.h
@@ -3,8 +3,6 @@
 
 #include <glib.h>
 
-#include "address.h"
-
 struct _flxInterfaceMonitor;
 typedef struct _flxInterfaceMonitor flxInterfaceMonitor;
 
@@ -14,6 +12,22 @@ typedef struct _flxInterfaceAddress flxInterfaceAddress;
 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;
@@ -32,34 +46,17 @@ struct _flxInterfaceAddress {
     
     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
diff --git a/local-addr.h b/local-addr.h
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/local.c b/local.c
deleted file mode 100644 (file)
index 6c61fac..0000000
--- a/local.c
+++ /dev/null
@@ -1,174 +0,0 @@
-#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);
-}
diff --git a/main.c b/main.c
index 17655401d3be756e36e534736d786a8a51fc1946..d7a0f2f080cf8e0ac731463c827da083bc97e947 100644 (file)
--- a/main.c
+++ b/main.c
@@ -14,21 +14,13 @@ static gboolean timeout(gpointer data) {
 
 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;
@@ -42,7 +34,6 @@ int main(int argc, char *argv[]) {
 
     flx_server_dump(flx, stdout);
 
-    flx_local_addr_source_free(l);
     flx_server_free(flx);
     return 0;
 }
index 1f8bb6fa14caf39b1a43493100e1deb248289c0b..c31ff8097439279a8101d39890d225b0121c2d76 100644 (file)
--- a/server.c
+++ b/server.c
@@ -1,6 +1,7 @@
 #include <sys/socket.h>
 #include <arpa/inet.h>
 #include <string.h>
+#include <sys/utsname.h>
 
 #include "server.h"
 #include "util.h"
@@ -42,6 +43,10 @@ static gint response_job_instance_compare(gpointer a, gpointer b) {
 }
 
 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) {
@@ -58,8 +63,33 @@ flxServer *flx_server_new(GMainContext *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;
 }
 
@@ -71,14 +101,16 @@ void flx_server_free(flxServer* 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);
 }
 
@@ -268,6 +300,15 @@ void flx_server_dump(flxServer *s, FILE *f) {
                         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;
             }
         }
             
@@ -278,10 +319,9 @@ void flx_server_dump(flxServer *s, FILE *f) {
 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;
@@ -312,6 +352,17 @@ void flx_server_add_address(flxServer *s, gint id, gint interface, guchar protoc
     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;
index 4134638dd6fd88d5dae95d787f9bf1142d4c076c..daaa815ebd967cc129f63b2f8a81494762ebb3b0 100644 (file)
--- a/server.h
+++ b/server.h
@@ -1,12 +1,13 @@
 #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;
@@ -59,6 +60,10 @@ struct _flxServer {
 
     flxPrioQueue *query_job_queue;
     flxPrioQueue *response_job_queue;
+
+    gint hinfo_rr_id;
+
+    gchar *hostname;
 };
 
 flxQueryJob* flx_query_job_new(void);