summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2005-06-24 01:00:13 +0000
committerLennart Poettering <lennart@poettering.net>2005-06-24 01:00:13 +0000
commit9f9f4f6ea2405edc642d322c19f6f13e31920046 (patch)
tree2a4a3a8ecbe0f942a140a98b4f1621d183575297
parent697d33e34648286fd42ff6086087e0111726f749 (diff)
* update todo list
* add avvahi-dnsconfd * implement AvahiDNSServerBrowser * Update special browse/register RRs to match current RFC * add support to avahi-daemon to publish name server information * add support to avahi-daemon to publish /etc/resolv.conf DNS server information * add new simple protocol command: "BROWSE-DNS-SERVERS" * cleanup final configure message git-svn-id: file:///home/lennart/svn/public/avahi/trunk@143 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe
-rw-r--r--Makefile.am2
-rw-r--r--avahi-core/Makefile.am3
-rw-r--r--avahi-core/avahi-test.c22
-rw-r--r--avahi-core/browse-dns-server.c194
-rw-r--r--avahi-core/browse-domain.c11
-rw-r--r--avahi-core/core.h44
-rw-r--r--avahi-core/server.c104
-rw-r--r--avahi-core/server.h1
-rw-r--r--avahi-daemon/Makefile.am2
-rw-r--r--avahi-daemon/avahi.conf2
-rw-r--r--avahi-daemon/main.c162
-rw-r--r--avahi-daemon/simple-protocol.c50
-rw-r--r--avahi-dnsconfd/Makefile.am51
-rwxr-xr-xavahi-dnsconfd/dnsconf.action62
-rw-r--r--avahi-dnsconfd/main.c608
-rw-r--r--configure.ac29
-rw-r--r--todo4
17 files changed, 1305 insertions, 46 deletions
diff --git a/Makefile.am b/Makefile.am
index 70757bb..f59f886 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -18,7 +18,7 @@
# USA.
EXTRA_DIST = bootstrap.sh LICENSE
-SUBDIRS = avahi-common avahi-core avahi-discover avahi-client avahi-daemon doxygen initscript
+SUBDIRS = avahi-common avahi-core avahi-discover avahi-client avahi-daemon doxygen initscript avahi-dnsconfd
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = avahi-core.pc
diff --git a/avahi-core/Makefile.am b/avahi-core/Makefile.am
index 0746622..e4dd771 100644
--- a/avahi-core/Makefile.am
+++ b/avahi-core/Makefile.am
@@ -69,7 +69,8 @@ libavahi_core_la_SOURCES = \
dns.c dns.h \
core.h \
llist.h \
- log.c log.h
+ log.c log.h \
+ browse-dns-server.c browse-dns-server.h
libavahi_core_la_CFLAGS = $(AM_CFLAGS)
libavahi_core_la_LIBADD = $(AM_LDADD) $(COMMON_LDADD)
diff --git a/avahi-core/avahi-test.c b/avahi-core/avahi-test.c
index 0d47941..908212c 100644
--- a/avahi-core/avahi-test.c
+++ b/avahi-core/avahi-test.c
@@ -102,6 +102,7 @@ static void remove_entries(void) {
}
static void create_entries(gboolean new_name) {
+ AvahiAddress a;
remove_entries();
group = avahi_entry_group_new(server, entry_group_callback, NULL);
@@ -128,7 +129,12 @@ static void create_entries(gboolean new_name) {
avahi_log_error("Failed to add WEBDAV service");
goto fail;
}
-
+
+ if (avahi_server_add_dns_server_address(server, group, 0, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, avahi_address_parse("192.168.50.1", AF_UNSPEC, &a), 53) < 0) {
+ avahi_log_error("Failed to add new DNS Server address");
+ goto fail;
+ }
+
avahi_entry_group_commit(group);
return;
@@ -186,6 +192,12 @@ static void sr_callback(AvahiServiceResolver *r, gint iface, guchar protocol, Av
}
}
+static void dsb_callback(AvahiDNSServerBrowser *b, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar*hostname, const AvahiAddress *a, guint16 port, gpointer userdata) {
+ gchar t[64];
+ avahi_address_snprint(t, sizeof(t), a);
+ avahi_log_debug("DSB: (%i.%i): %s/%s:%i [%s]", iface, protocol, hostname, t, port, event == AVAHI_BROWSER_NEW ? "new" : "remove");
+}
+
int main(int argc, char *argv[]) {
GMainLoop *loop = NULL;
AvahiRecordBrowser *r;
@@ -198,7 +210,8 @@ int main(int argc, char *argv[]) {
AvahiServiceTypeBrowser *stb;
AvahiServiceBrowser *sb;
AvahiServiceResolver *sr;
-
+ AvahiDNSServerBrowser *dsb;
+
avahi_server_config_init(&config);
/* config.host_name = g_strdup("test"); */
server = avahi_server_new(NULL, &config, server_callback, NULL);
@@ -219,7 +232,9 @@ int main(int argc, char *argv[]) {
sb = avahi_service_browser_new(server, -1, AF_UNSPEC, "_http._tcp", NULL, sb_callback, NULL);
sr = avahi_service_resolver_new(server, -1, AF_UNSPEC, "Ecstasy HTTP", "_http._tcp", "local", AF_UNSPEC, sr_callback, NULL);
-
+
+ dsb = avahi_dns_server_browser_new(server, -1, AF_UNSPEC, "local", AVAHI_DNS_SERVER_RESOLVE, AF_UNSPEC, dsb_callback, NULL);
+
loop = g_main_loop_new(NULL, FALSE);
g_timeout_add(1000*5, dump_timeout, server);
@@ -234,6 +249,7 @@ int main(int argc, char *argv[]) {
avahi_service_type_browser_free(stb);
avahi_service_browser_free(sb);
avahi_service_resolver_free(sr);
+ avahi_dns_server_browser_free(dsb);
if (group)
avahi_entry_group_free(group);
diff --git a/avahi-core/browse-dns-server.c b/avahi-core/browse-dns-server.c
new file mode 100644
index 0000000..9dd4798
--- /dev/null
+++ b/avahi-core/browse-dns-server.c
@@ -0,0 +1,194 @@
+/* $Id$ */
+
+/***
+ This file is part of avahi.
+
+ avahi is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ avahi is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+ Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with avahi; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <string.h>
+
+#include "browse.h"
+#include "util.h"
+#include "log.h"
+#include "rr.h"
+
+typedef struct AvahiDNSServerInfo AvahiDNSServerInfo;
+
+struct AvahiDNSServerInfo {
+ AvahiDNSServerBrowser *browser;
+
+ gint interface;
+ guchar protocol;
+ AvahiRecord *srv_record;
+ AvahiHostNameResolver *host_name_resolver;
+ AvahiAddress address;
+
+ AVAHI_LLIST_FIELDS(AvahiDNSServerInfo, info);
+};
+
+struct AvahiDNSServerBrowser {
+ AvahiServer *server;
+ gchar *domain_name;
+
+ AvahiRecordBrowser *record_browser;
+ AvahiDNSServerBrowserCallback callback;
+ gpointer userdata;
+ guchar aprotocol;
+
+ guint n_info;
+
+ AVAHI_LLIST_FIELDS(AvahiDNSServerBrowser, browser);
+ AVAHI_LLIST_HEAD(AvahiDNSServerInfo, info);
+};
+
+static AvahiDNSServerInfo* get_server_info(AvahiDNSServerBrowser *b, gint interface, guchar protocol, AvahiRecord *r) {
+ AvahiDNSServerInfo *i;
+
+ g_assert(b);
+ g_assert(r);
+
+ for (i = b->info; i; i = i->info_next)
+ if (i->interface == interface &&
+ i->protocol == protocol &&
+ avahi_record_equal_no_ttl(r, i->srv_record))
+ return i;
+
+ return NULL;
+}
+
+static void server_info_free(AvahiDNSServerBrowser *b, AvahiDNSServerInfo *i) {
+ g_assert(b);
+ g_assert(i);
+
+ avahi_record_unref(i->srv_record);
+ if (i->host_name_resolver)
+ avahi_host_name_resolver_free(i->host_name_resolver);
+
+ AVAHI_LLIST_REMOVE(AvahiDNSServerInfo, info, b->info, i);
+
+ g_assert(b->n_info >= 1);
+ b->n_info--;
+
+ g_free(i);
+}
+
+static void host_name_resolver_callback(AvahiHostNameResolver *r, gint interface, guchar protocol, AvahiResolverEvent event, const gchar *host_name, const AvahiAddress *a, gpointer userdata) {
+ AvahiDNSServerInfo *i = userdata;
+
+ g_assert(r);
+ g_assert(host_name);
+ g_assert(i);
+
+ if (event == AVAHI_RESOLVER_FOUND) {
+ i->address = *a;
+
+ i->browser->callback(i->browser, i->interface, i->protocol, AVAHI_BROWSER_NEW, i->srv_record->data.srv.name, &i->address, i->srv_record->data.srv.port, i->browser->userdata);
+ }
+
+ avahi_host_name_resolver_free(i->host_name_resolver);
+ i->host_name_resolver = NULL;
+}
+
+static void record_browser_callback(AvahiRecordBrowser*rr, gint interface, guchar protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
+ AvahiDNSServerBrowser *b = userdata;
+
+ g_assert(rr);
+ g_assert(record);
+ g_assert(b);
+
+ g_assert(record->key->type == AVAHI_DNS_TYPE_SRV);
+
+ if (event == AVAHI_BROWSER_NEW) {
+ AvahiDNSServerInfo *i;
+
+ if (get_server_info(b, interface, protocol, record))
+ return;
+
+ if (b->n_info >= 10)
+ return;
+
+ i = g_new(AvahiDNSServerInfo, 1);
+ i->browser = b;
+ i->interface = interface;
+ i->protocol = protocol;
+ i->srv_record = avahi_record_ref(record);
+ i->host_name_resolver = avahi_host_name_resolver_new(b->server, interface, protocol, record->data.srv.name, b->aprotocol, host_name_resolver_callback, i);
+
+ AVAHI_LLIST_PREPEND(AvahiDNSServerInfo, info, b->info, i);
+
+ b->n_info++;
+ } else if (event == AVAHI_BROWSER_REMOVE) {
+ AvahiDNSServerInfo *i;
+
+ if (!(i = get_server_info(b, interface, protocol, record)))
+ return;
+
+ if (!i->host_name_resolver)
+ b->callback(b, interface, protocol, event, i->srv_record->data.srv.name, &i->address, i->srv_record->data.srv.port, b->userdata);
+
+ server_info_free(b, i);
+ }
+}
+
+AvahiDNSServerBrowser *avahi_dns_server_browser_new(AvahiServer *server, gint interface, guchar protocol, const gchar *domain, AvahiDNSServerType type, guchar aprotocol, AvahiDNSServerBrowserCallback callback, gpointer userdata) {
+ AvahiDNSServerBrowser *b;
+ AvahiKey *k;
+ gchar *n = NULL;
+
+ g_assert(server);
+ g_assert(callback);
+ g_assert(type == AVAHI_DNS_SERVER_RESOLVE || type == AVAHI_DNS_SERVER_UPDATE);
+
+ b = g_new(AvahiDNSServerBrowser, 1);
+ b->server = server;
+ b->domain_name = avahi_normalize_name(domain ? domain : "local.");
+ b->callback = callback;
+ b->userdata = userdata;
+ b->aprotocol = aprotocol;
+ b->n_info = 0;
+
+ AVAHI_LLIST_HEAD_INIT(AvahiDNSServerInfo, b->info);
+
+ n = g_strdup_printf("%s.%s",type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", b->domain_name);
+ k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
+ g_free(n);
+
+ b->record_browser = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, b);
+ avahi_key_unref(k);
+
+ AVAHI_LLIST_PREPEND(AvahiDNSServerBrowser, browser, server->dns_server_browsers, b);
+
+ return b;
+}
+
+void avahi_dns_server_browser_free(AvahiDNSServerBrowser *b) {
+ g_assert(b);
+
+ while (b->info)
+ server_info_free(b, b->info);
+
+ AVAHI_LLIST_REMOVE(AvahiDNSServerBrowser, browser, b->server->dns_server_browsers, b);
+
+ avahi_record_browser_free(b->record_browser);
+ g_free(b->domain_name);
+ g_free(b);
+}
+
diff --git a/avahi-core/browse-domain.c b/avahi-core/browse-domain.c
index 61807a5..06ad64a 100644
--- a/avahi-core/browse-domain.c
+++ b/avahi-core/browse-domain.c
@@ -69,16 +69,19 @@ AvahiDomainBrowser *avahi_domain_browser_new(AvahiServer *server, gint interface
switch (type) {
case AVAHI_DOMAIN_BROWSER_BROWSE:
- n = g_strdup_printf("_browse._dns-sd._udp.%s", b->domain_name);
+ n = g_strdup_printf("b._dns-sd._udp.%s", b->domain_name);
break;
case AVAHI_DOMAIN_BROWSER_BROWSE_DEFAULT:
- n = g_strdup_printf("_default._browse._dns-sd._udp.%s", b->domain_name);
+ n = g_strdup_printf("db._dns-sd._udp.%s", b->domain_name);
break;
case AVAHI_DOMAIN_BROWSER_REGISTER:
- n = g_strdup_printf("_register._dns-sd._udp.%s", b->domain_name);
+ n = g_strdup_printf("r._dns-sd._udp.%s", b->domain_name);
break;
case AVAHI_DOMAIN_BROWSER_REGISTER_DEFAULT:
- n = g_strdup_printf("_default._register._dns-sd._udp.%s", b->domain_name);
+ n = g_strdup_printf("dr._dns-sd._udp.%s", b->domain_name);
+ break;
+ case AVAHI_DOMAIN_BROWSER_BROWSE_LEGACY:
+ n = g_strdup_printf("lb._dns-sd._udp.%s", b->domain_name);
break;
}
diff --git a/avahi-core/core.h b/avahi-core/core.h
index 0565d23..74f5cf4 100644
--- a/avahi-core/core.h
+++ b/avahi-core/core.h
@@ -275,6 +275,38 @@ gint avahi_server_add_service_strlst(
guint16 port,
AvahiStringList *strlst);
+/** The type of DNS server */
+typedef enum {
+ AVAHI_DNS_SERVER_RESOLVE, /**< Unicast DNS servers for normal resolves (_domain._udp)*/
+ AVAHI_DNS_SERVER_UPDATE /**< Unicast DNS servers for updates (_dns-update._udp)*/
+} AvahiDNSServerType;
+
+/** Publish the specified unicast DNS server address via mDNS. You may
+ * browse for records create this way wit
+ * avahi_dns_server_browser_new(). */
+gint avahi_server_add_dns_server_address(
+ AvahiServer *s,
+ AvahiEntryGroup *g,
+ gint interface,
+ guchar protocol,
+ const gchar *domain,
+ AvahiDNSServerType type,
+ const AvahiAddress *address,
+ guint16 port /** should be 53 */);
+
+/** Similar to avahi_server_add_dns_server_address(), but specify a
+host name instead of an address. The specified host name should be
+resolvable via mDNS */
+gint avahi_server_add_dns_server_name(
+ AvahiServer *s,
+ AvahiEntryGroup *g,
+ gint interface,
+ guchar protocol,
+ const gchar *domain,
+ AvahiDNSServerType type,
+ const gchar *name,
+ guint16 port /** should be 53 */);
+
typedef enum {
AVAHI_BROWSER_NEW = 0,
AVAHI_BROWSER_REMOVE = -1
@@ -306,7 +338,8 @@ typedef enum {
AVAHI_DOMAIN_BROWSER_REGISTER, /**< Browse for a list of available registering domains */
AVAHI_DOMAIN_BROWSER_REGISTER_DEFAULT, /**< Browse for the default registering domain */
AVAHI_DOMAIN_BROWSER_BROWSE, /**< Browse for a list of available browsing domains */
- AVAHI_DOMAIN_BROWSER_BROWSE_DEFAULT /**< Browse for the default browsing domain */
+ AVAHI_DOMAIN_BROWSER_BROWSE_DEFAULT, /**< Browse for the default browsing domain */
+ AVAHI_DOMAIN_BROWSER_BROWSE_LEGACY /**< Legacy browse domain - see DNS-SD spec for more information */
} AvahiDomainBrowserType;
typedef struct AvahiDomainBrowser AvahiDomainBrowser;
@@ -329,4 +362,13 @@ typedef void (*AvahiServiceResolverCallback)(AvahiServiceResolver *r, gint inter
AvahiServiceResolver *avahi_service_resolver_new(AvahiServer *server, gint interface, guchar protocol, const gchar *name, const gchar *type, const gchar *domain, guchar aprotocol, AvahiServiceResolverCallback calback, gpointer userdata);
void avahi_service_resolver_free(AvahiServiceResolver *r);
+
+/** A domain service browser object. Use this to browse for
+ * conventional unicast DNS servers which may be used to resolve
+ * conventional domain names */
+typedef struct AvahiDNSServerBrowser AvahiDNSServerBrowser;
+typedef void (*AvahiDNSServerBrowserCallback)(AvahiDNSServerBrowser *b, gint interface, guchar protocol, AvahiBrowserEvent event, const gchar *host_name, const AvahiAddress *a, guint16 port, gpointer userdata);
+AvahiDNSServerBrowser *avahi_dns_server_browser_new(AvahiServer *server, gint interface, guchar protocol, const gchar *domain, AvahiDNSServerType type, guchar aprotocol, AvahiDNSServerBrowserCallback callback, gpointer userdata);
+void avahi_dns_server_browser_free(AvahiDNSServerBrowser *b);
+
#endif
diff --git a/avahi-core/server.c b/avahi-core/server.c
index 356a917..0e92e27 100644
--- a/avahi-core/server.c
+++ b/avahi-core/server.c
@@ -1151,7 +1151,7 @@ static void register_browse_domain(AvahiServer *s) {
return;
s->browse_domain_entry_group = avahi_entry_group_new(s, NULL, NULL);
- avahi_server_add_ptr(s, s->browse_domain_entry_group, 0, AF_UNSPEC, 0, "_browse._dns-sd._udp.local", s->domain_name);
+ avahi_server_add_ptr(s, s->browse_domain_entry_group, 0, AF_UNSPEC, 0, "b._dns-sd._udp.local", s->domain_name);
avahi_entry_group_commit(s->browse_domain_entry_group);
}
@@ -1318,6 +1318,7 @@ AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, Avah
AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, s->service_type_browsers);
AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, s->service_browsers);
AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, s->service_resolvers);
+ AVAHI_LLIST_HEAD_INIT(AvahiDNSServerBrowser, s->dns_server_browsers);
s->legacy_unicast_reflect_slots = NULL;
s->legacy_unicast_reflect_id = 0;
@@ -1360,7 +1361,9 @@ void avahi_server_free(AvahiServer* s) {
free_group(s, s->groups);
free_slots(s);
-
+
+ while (s->dns_server_browsers)
+ avahi_dns_server_browser_free(s->dns_server_browsers);
while (s->host_name_resolvers)
avahi_host_name_resolver_free(s->host_name_resolvers);
while (s->address_resolvers)
@@ -1767,6 +1770,103 @@ gint avahi_server_add_service(
return ret;
}
+static void hexstring(gchar *s, size_t sl, const void *p, size_t pl) {
+ static const gchar hex[] = "0123456789abcdef";
+ gboolean b = FALSE;
+ const guint8 *k = p;
+
+ while (sl > 1 && pl > 0) {
+ *(s++) = hex[(b ? *k : *k >> 4) & 0xF];
+
+ if (b) {
+ k++;
+ pl--;
+ }
+
+ b = !b;
+
+ sl--;
+ }
+
+ if (sl > 0)
+ *s = 0;
+}
+
+gint avahi_server_add_dns_server_address(
+ AvahiServer *s,
+ AvahiEntryGroup *g,
+ gint interface,
+ guchar protocol,
+ const gchar *domain,
+ AvahiDNSServerType type,
+ const AvahiAddress *address,
+ guint16 port /** should be 53 */) {
+
+ AvahiRecord *r;
+ gint ret;
+ gchar n[64] = "ip";
+
+ g_assert(s);
+ g_assert(address);
+ g_assert(type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE);
+ g_assert(address->family == AF_INET || address->family == AF_INET6);
+
+ if (address->family == AF_INET) {
+ hexstring(n+2, sizeof(n)-2, &address->data, 4);
+ r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
+ r->data.a.address = address->data.ipv4;
+ } else {
+ hexstring(n+2, sizeof(n)-2, &address->data, 6);
+ r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
+ r->data.aaaa.address = address->data.ipv6;
+ }
+
+ ret = avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r);
+ avahi_record_unref(r);
+
+ ret |= avahi_server_add_dns_server_name(s, g, interface, protocol, domain, type, n, port);
+
+ return ret;
+}
+
+gint avahi_server_add_dns_server_name(
+ AvahiServer *s,
+ AvahiEntryGroup *g,
+ gint interface,
+ guchar protocol,
+ const gchar *domain,
+ AvahiDNSServerType type,
+ const gchar *name,
+ guint16 port /** should be 53 */) {
+
+ gint ret = -1;
+ gchar t[256];
+ AvahiRecord *r;
+
+ g_assert(s);
+ g_assert(name);
+ g_assert(type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE);
+
+ if (domain) {
+ while (domain[0] == '.')
+ domain++;
+ } else
+ domain = s->domain_name;
+
+ snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", domain);
+
+ r = avahi_record_new_full(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
+ r->data.srv.priority = 0;
+ r->data.srv.weight = 0;
+ r->data.srv.port = port;
+ r->data.srv.name = avahi_normalize_name(name);
+ ret = avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_NULL, r);
+ avahi_record_unref(r);
+
+ return ret;
+}
+
+
static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
AvahiKey *k = userdata;
diff --git a/avahi-core/server.h b/avahi-core/server.h
index 0da7c34..ad8576d 100644
--- a/avahi-core/server.h
+++ b/avahi-core/server.h
@@ -98,6 +98,7 @@ struct AvahiServer {
AVAHI_LLIST_HEAD(AvahiServiceTypeBrowser, service_type_browsers);
AVAHI_LLIST_HEAD(AvahiServiceBrowser, service_browsers);
AVAHI_LLIST_HEAD(AvahiServiceResolver, service_resolvers);
+ AVAHI_LLIST_HEAD(AvahiDNSServerBrowser, dns_server_browsers);
gboolean need_entry_cleanup, need_group_cleanup, need_browser_cleanup;
diff --git a/avahi-daemon/Makefile.am b/avahi-daemon/Makefile.am
index 3b90852..4c943e4 100644
--- a/avahi-daemon/Makefile.am
+++ b/avahi-daemon/Makefile.am
@@ -27,6 +27,7 @@ AM_CFLAGS= \
-DAVAHI_SERVICE_DIRECTORY=\"$(servicedir)\" \
-DAVAHI_CONFIG_FILE=\"$(pkgsysconfdir)/avahi.conf\" \
-DAVAHI_RUNTIME_DIR=\"$(localstatedir)/run/avahi\"
+
AM_LDADD=-lexpat
# GLIB 2.0
@@ -37,7 +38,6 @@ AM_LDADD+=$(GLIB20_LIBS)
AM_CFLAGS+=$(LIBDAEMON_CFLAGS)
AM_LDADD+=$(LIBDAEMON_LIBS)
-
if ENABLE_DBUS
# DBUS
AM_CFLAGS+=$(DBUS_CFLAGS)
diff --git a/avahi-daemon/avahi.conf b/avahi-daemon/avahi.conf
index 900d04b..82f5326 100644
--- a/avahi-daemon/avahi.conf
+++ b/avahi-daemon/avahi.conf
@@ -12,6 +12,8 @@ publish-addresses=yes
publish-hinfo=yes
publish-workstation=yes
publish-domain=yes
+#publish-dns-servers=192.168.50.1,192.168.50.2
+#publish-resolv-conf-dns-servers=yes
[reflector]
enable-reflector=no
diff --git a/avahi-daemon/main.c b/avahi-daemon/main.c
index ce3400a..40e140c 100644
--- a/avahi-daemon/main.c
+++ b/avahi-daemon/main.c
@@ -63,19 +63,136 @@ typedef struct {
gchar *config_file;
gboolean enable_dbus;
gboolean drop_root;
+ gboolean publish_resolv_conf;
+ gchar ** publish_dns_servers;
} DaemonConfig;
+#define RESOLV_CONF "/etc/resolv.conf"
+
+static AvahiEntryGroup *dns_servers_entry_group = NULL;
+static AvahiEntryGroup *resolv_conf_entry_group = NULL;
+
+static gchar **resolv_conf = NULL;
+
+static DaemonConfig config;
+
+#define MAX_NAME_SERVERS 10
+
+static gint load_resolv_conf(const DaemonConfig *config) {
+ gint ret = -1;
+ FILE *f;
+ gint i = 0;
+
+ g_strfreev(resolv_conf);
+ resolv_conf = NULL;
+
+ if (!config->publish_resolv_conf)
+ return 0;
+
+ if (!(f = fopen(RESOLV_CONF, "r"))) {
+ avahi_log_warn("Failed to open "RESOLV_CONF".");
+ goto finish;
+ }
+
+ resolv_conf = g_new0(gchar*, MAX_NAME_SERVERS+1);
+
+ while (!feof(f) && i < MAX_NAME_SERVERS) {
+ char ln[128];
+ gchar *p;
+
+ if (!(fgets(ln, sizeof(ln), f)))
+ break;
+
+ ln[strcspn(ln, "\r\n#")] = 0;
+ p = ln + strspn(ln, "\t ");
+
+ if (g_str_has_prefix(p, "nameserver")) {
+ p += 10;
+ p += strspn(p, "\t ");
+ p[strcspn(p, "\t ")] = 0;
+ resolv_conf[i++] = strdup(p);
+ }
+ }
+
+ ret = 0;
+
+finish:
+
+ if (ret != 0) {
+ g_strfreev(resolv_conf);
+ resolv_conf = NULL;
+ }
+
+ if (f)
+ fclose(f);
+
+ return ret;
+}
+
+static AvahiEntryGroup* add_dns_servers(AvahiServer *s, gchar **l) {
+ gchar **p;
+ AvahiEntryGroup *g;
+
+ g_assert(s);
+ g_assert(l);
+
+ g = avahi_entry_group_new(s, NULL, NULL);
+
+ for (p = l; *p; p++) {
+ AvahiAddress a;
+
+ if (!avahi_address_parse(*p, AF_UNSPEC, &a))
+ avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
+ else
+ if (avahi_server_add_dns_server_address(s, g, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
+ avahi_entry_group_free(g);
+ return NULL;
+ }
+ }
+
+ avahi_entry_group_commit(g);
+
+ return g;
+
+}
+
+static void remove_dns_server_entry_groups(void) {
+ if (resolv_conf_entry_group) {
+ avahi_entry_group_free(resolv_conf_entry_group);
+ resolv_conf_entry_group = NULL;
+ }
+
+ if (dns_servers_entry_group) {
+ avahi_entry_group_free(dns_servers_entry_group);
+ dns_servers_entry_group = NULL;
+ }
+}
+
static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
+ DaemonConfig *config = userdata;
+
g_assert(s);
+ g_assert(config);
if (state == AVAHI_SERVER_RUNNING) {
avahi_log_info("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
static_service_add_to_server();
+
+ remove_dns_server_entry_groups();
+
+ if (resolv_conf && resolv_conf[0])
+ resolv_conf_entry_group = add_dns_servers(s, resolv_conf);
+
+ if (config->publish_dns_servers && config->publish_dns_servers[0])
+ dns_servers_entry_group = add_dns_servers(s, config->publish_dns_servers);
+
} else if (state == AVAHI_SERVER_COLLISION) {
gchar *n;
static_service_remove_from_server();
-
+
+ remove_dns_server_entry_groups();
+
n = avahi_alternative_host_name(avahi_server_get_host_name(s));
avahi_log_warn("Host name conflict, retrying with <%s>", n);
avahi_server_set_host_name(s, n);
@@ -162,6 +279,7 @@ static gint load_config_file(DaemonConfig *config) {
g_assert(config);
f = g_key_file_new();
+ g_key_file_set_list_separator(f, ',');
if (!g_key_file_load_from_file(f, config->config_file ? config->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
fprintf(stderr, "Unable to read config file: %s\n", err->message);
@@ -229,7 +347,12 @@ static gint load_config_file(DaemonConfig *config) {
config->server_config.publish_workstation = is_yes(v);
else if (g_strcasecmp(*k, "publish-domain") == 0)
config->server_config.publish_domain = is_yes(v);
- else {
+ else if (g_strcasecmp(*k, "publish-resolv-conf-dns-servers") == 0)
+ config->publish_resolv_conf = is_yes(v);
+ else if (g_strcasecmp(*k, "publish-dns-servers") == 0) {
+ g_strfreev(config->publish_dns_servers);
+ config->publish_dns_servers = g_key_file_get_string_list(f, *g, *k, NULL, NULL);
+ } else {
fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
goto finish;
}
@@ -307,7 +430,10 @@ static void log_function(AvahiLogLevel level, const gchar *txt) {
static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
gint sig;
+ GMainLoop *loop = data;
+
g_assert(source);
+ g_assert(loop);
if ((sig = daemon_signal_next()) <= 0) {
avahi_log_error("daemon_signal_next() failed");
@@ -322,13 +448,24 @@ static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpoi
"Got %s, quitting.",
sig == SIGINT ? "SIGINT" :
(sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
- g_main_loop_quit((GMainLoop*) data);
+ g_main_loop_quit(loop);
break;
case SIGHUP:
avahi_log_info("Got SIGHUP, reloading.");
static_service_load();
static_service_add_to_server();
+
+ if (resolv_conf_entry_group) {
+ avahi_entry_group_free(resolv_conf_entry_group);
+ resolv_conf_entry_group = NULL;
+ }
+
+ load_resolv_conf(&config);
+
+ if (resolv_conf && resolv_conf[0])
+ resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf);
+
break;
default:
@@ -371,8 +508,10 @@ static gint run_server(DaemonConfig *config) {
goto finish;
#endif
- if (!(avahi_server = avahi_server_new(NULL, &config->server_config, server_callback, NULL)))
+ if (!(avahi_server = avahi_server_new(NULL, &config->server_config, server_callback, config)))
goto finish;
+
+ load_resolv_conf(config);
static_service_load();
@@ -387,6 +526,7 @@ finish:
static_service_remove_from_server();
static_service_free_all();
+ remove_dns_server_entry_groups();
simple_protocol_shutdown();
@@ -395,6 +535,7 @@ finish:
dbus_protocol_shutdown();
#endif
+
if (avahi_server)
avahi_server_free(avahi_server);
@@ -476,7 +617,7 @@ static gint drop_root(void) {
}
static const char* pid_file_proc(void) {
- return AVAHI_RUNTIME_DIR"/pid";
+ return AVAHI_RUNTIME_DIR"/avahi-daemon.pid";
}
static gint make_runtime_dir(void) {
@@ -525,12 +666,9 @@ fail:
return r;
}
-
-
int main(int argc, char *argv[]) {
gint r = 255;
- DaemonConfig config;
const gchar *argv0;
gboolean wrote_pid_file = FALSE;
@@ -542,6 +680,8 @@ int main(int argc, char *argv[]) {
config.config_file = NULL;
config.enable_dbus = TRUE;
config.drop_root = TRUE;
+ config.publish_dns_servers = NULL;
+ config.publish_resolv_conf = FALSE;
if ((argv0 = strrchr(argv[0], '/')))
argv0++;
@@ -549,7 +689,6 @@ int main(int argc, char *argv[]) {
argv0 = argv[0];
daemon_pid_file_ident = daemon_log_ident = (char *) argv0;
-
daemon_pid_file_proc = pid_file_proc;
if (parse_command_line(&config, argc, argv) < 0)
@@ -560,7 +699,7 @@ int main(int argc, char *argv[]) {
r = 0;
} else if (config.command == DAEMON_VERSION) {
printf("%s "PACKAGE_VERSION"\n", argv0);
-
+ r = 0;
} else if (config.command == DAEMON_KILL) {
if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
@@ -594,7 +733,6 @@ int main(int argc, char *argv[]) {
goto finish;
if (config.daemonize) {
-
daemon_retval_init();
if ((pid = daemon_fork()) < 0)
@@ -645,6 +783,8 @@ finish:
avahi_server_config_free(&config.server_config);
g_free(config.config_file);
+ g_strfreev(config.publish_dns_servers);
+ g_strfreev(resolv_conf);
if (wrote_pid_file)
daemon_pid_file_remove();
diff --git a/avahi-daemon/simple-protocol.c b/avahi-daemon/simple-protocol.c
index c41e07c..39580dd 100644
--- a/avahi-daemon/simple-protocol.c
+++ b/avahi-daemon/simple-protocol.c
@@ -39,7 +39,7 @@
#include "simple-protocol.h"
#include "main.h"
-#define BUFFER_SIZE (10*1024)
+#define BUFFER_SIZE (20*1024)
#define UNIX_SOCKET AVAHI_RUNTIME_DIR "/socket"
@@ -52,6 +52,7 @@ typedef enum {
CLIENT_IDLE,
CLIENT_RESOLVE_HOSTNAME,
CLIENT_RESOLVE_ADDRESS,
+ CLIENT_BROWSE_DNS_SERVERS,
CLIENT_DEAD
} ClientState;
@@ -68,6 +69,7 @@ struct Client {
AvahiHostNameResolver *host_name_resolver;
AvahiAddressResolver *address_resolver;
+ AvahiDNSServerBrowser *dns_server_browser;
AVAHI_LLIST_FIELDS(Client, clients);
};
@@ -96,6 +98,9 @@ static void client_free(Client *c) {
if (c->address_resolver)
avahi_address_resolver_free(c->address_resolver);
+
+ if (c->dns_server_browser)
+ avahi_dns_server_browser_free(c->dns_server_browser);
g_source_remove_poll(&c->server->source, &c->poll_fd);
close(c->fd);
@@ -117,6 +122,7 @@ static void client_new(Server *s, int fd) {
c->host_name_resolver = NULL;
c->address_resolver = NULL;
+ c->dns_server_browser = NULL;
memset(&c->poll_fd, 0, sizeof(GPollFD));
c->poll_fd.fd = fd;
@@ -146,14 +152,15 @@ static void client_output(Client *c, const guint8*data, guint size) {
}
static void client_output_printf(Client *c, gchar *format, ...) {
- gchar txt[256];
+ gchar *t;
va_list ap;
va_start(ap, format);
- vsnprintf(txt, sizeof(txt), format, ap);
+ t = g_strdup_vprintf(format, ap);
va_end(ap);
- client_output(c, (guint8*) txt, strlen(txt));
+ client_output(c, (guint8*) t, strlen(t));
+ g_free(t);
}
static void host_name_resolver_callback(AvahiHostNameResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar *hostname, const AvahiAddress *a, gpointer userdata) {
@@ -167,7 +174,7 @@ static void host_name_resolver_callback(AvahiHostNameResolver *r, gint iface, gu
else {
gchar t[64];
avahi_address_snprint(t, sizeof(t), a);
- client_output_printf(c, "+ %s\n", t);
+ client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
}
c->state = CLIENT_DEAD;
@@ -176,16 +183,30 @@ static void host_name_resolver_callback(AvahiHostNameResolver *r, gint iface, gu
static void address_resolver_callback(AvahiAddressResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const AvahiAddress *a, const gchar *hostname, gpointer userdata) {
Client *c = userdata;
+
g_assert(c);
if (event == AVAHI_RESOLVER_TIMEOUT)
client_output_printf(c, "- Query timed out\n");
else
- client_output_printf(c, "+ %s\n", hostname);
+ client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
c->state = CLIENT_DEAD;
}
+static void dns_server_browser_callback(AvahiDNSServerBrowser *b, gint interface, guchar protocol, AvahiBrowserEvent event, const gchar *host_name, const AvahiAddress *a, guint16 port, gpointer userdata) {
+ Client *c = userdata;
+ gchar t[64];
+
+ g_assert(c);
+
+ if (!a)
+ return;
+
+ avahi_address_snprint(t, sizeof(t), a);
+ client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<', interface, protocol, t, port);
+}
+
static void handle_line(Client *c, const gchar *s) {
gchar cmd[64], arg[64];
gint n_args;
@@ -208,7 +229,10 @@ static void handle_line(Client *c, const gchar *s) {
"+ RESOLVE-HOSTNAME <hostname>\n"
"+ RESOLVE-HOSTNAME-IPV6 <hostname>\n"
"+ RESOLVE-HOSTNAME-IPV4 <hostname>\n"
- "+ RESOLVE-ADDRESS <address>\n");
+ "+ RESOLVE-ADDRESS <address>\n"
+ "+ BROWSE-DNS-SERVERS\n"
+ "+ BROWSE-DNS-SERVERS-IPV4\n"
+ "+ BROWSE-DNS-SERVERS-IPV6\n");
c->state = CLIENT_DEAD; }
else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
client_output_printf(c, "FUCK: Go fuck yourself!\n");
@@ -232,6 +256,18 @@ static void handle_line(Client *c, const gchar *s) {
c->state = CLIENT_RESOLVE_ADDRESS;
c->address_resolver = avahi_address_resolver_new(avahi_server, -1, AF_UNSPEC, &addr, address_resolver_callback, c);
}
+ } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
+ c->state = CLIENT_BROWSE_DNS_SERVERS;
+ c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, AF_INET, dns_server_browser_callback, c);
+ client_output_printf(c, "+ Browsing ...\n");
+ } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
+ c->state = CLIENT_BROWSE_DNS_SERVERS;
+ c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, AF_INET6, dns_server_browser_callback, c);
+ client_output_printf(c, "+ Browsing ...\n");
+ } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
+ c->state = CLIENT_BROWSE_DNS_SERVERS;
+ c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, AF_UNSPEC, dns_server_browser_callback, c);
+ client_output_printf(c, "+ Browsing ...\n");
} else {
client_output_printf(c, "- Invalid command \"%s\", try \"HELP\".\n", cmd);
c->state = CLIENT_DEAD;
diff --git a/avahi-dnsconfd/Makefile.am b/avahi-dnsconfd/Makefile.am
new file mode 100644
index 0000000..8a0787a
--- /dev/null
+++ b/avahi-dnsconfd/Makefile.am
@@ -0,0 +1,51 @@
+# $Id$
+#
+# This file is part of avahi.
+#
+# avahi is free software; you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
+#
+# avahi is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+# License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with avahi; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+# USA.
+
+pkgsysconfdir=$(sysconfdir)/avahi
+
+AM_CFLAGS= \
+ -I$(top_srcdir) \
+ -D_GNU_SOURCE \
+ -DAVAHI_DNSCONF_SCRIPT=\"$(pkgsysconfdir)/dnsconf.action\" \
+ -DAVAHI_SOCKET=\"$(localstatedir)/run/avahi/socket\" \
+ -DAVAHI_RUNTIME_DIR=\"$(localstatedir)/run\"
+
+# GLIB 2.0
+AM_CFLAGS+=$(GLIB20_CFLAGS)
+AM_LDADD=$(GLIB20_LIBS)
+
+# libdaemon
+AM_CFLAGS+=$(LIBDAEMON_CFLAGS)
+AM_LDADD+=$(LIBDAEMON_LIBS)
+
+# This cool debug trap works on i386/gcc only
+AM_CFLAGS+='-DDEBUG_TRAP=__asm__("int $$3")'
+
+bin_PROGRAMS = \
+ avahi-dnsconfd
+
+avahi_dnsconfd_SOURCES = \
+ main.c main.h
+
+avahi_dnsconfd_CFLAGS = $(AM_CFLAGS)
+avahi_dnsconfd_LDADD = $(AM_LDADD) ../avahi-common/libavahi-common.la
+
+pkgsysconf_DATA=dnsconf.action
+
+EXTRA_DIST=dnsconf.action
diff --git a/avahi-dnsconfd/dnsconf.action b/avahi-dnsconfd/dnsconf.action
new file mode 100755
index 0000000..9337a6d
--- /dev/null
+++ b/avahi-dnsconfd/dnsconf.action
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+# $Id$
+#
+# This file is part of avahi.
+#
+# avahi is free software; you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
+#
+# avahi is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+# License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with avahi; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+# USA.
+
+set -e
+
+test "x$AVAHI_INTERFACE" != "x"
+
+# Command line arguments:
+# $1 "+" if a new DNS server was found, "-" if one was removed
+# $2 DNS Server address
+# $3 interface index where this server was found on
+# $4 protocol number where this server was found on
+
+# Available environment variables:
+#
+# $AVAHI_INTERFACE The interface name where this DNS server was found on
+# $AVAHI_INTERFACE_DNS_SERVERS A whitespace seperated list of DNS servers on $AVAHI_INTERFACE
+# $AVAHI_DNS_SERVERS The complete list of all DNS servers found on all interfaces
+
+if [ -x /sbin/resolvconf ] ; then
+
+ # We have Debian's resolvconf tool
+
+ if [ "x$AVAHI_INTERFACE_DNS_SERVERS" = "x" ] ; then
+ /sbin/resolvconf -d "$AVAHI_INTERFACE.avahi"
+ else
+ for n in $AVAHI_INTERFACE_DNS_SERVERS ; do
+ echo "nameserver $n"
+ done | /sbin/resolvconf -a "$AVAHI_INTERFACE.avahi"
+ fi
+else
+
+ # No resolvconf tool available
+
+ if [ "x$AVAHI_DNS_SERVERS" = "x" ] ; then
+ test -f /etc/resolv.conf.avahi && mv /etc/resolv.conf.avahi /etc/resolv.conf
+ else
+ test -f /etc/resolv.conf.avahi || mv /etc/resolv.conf /etc/resolv.conf.avahi
+
+ for n in $AVAHI_DNS_SERVERS ; do
+ echo "nameserver $n"
+ done > /etc/resolv.conf
+ fi
+fi
diff --git a/avahi-dnsconfd/main.c b/avahi-dnsconfd/main.c
new file mode 100644
index 0000000..918beeb
--- /dev/null
+++ b/avahi-dnsconfd/main.c
@@ -0,0 +1,608 @@
+/* $Id$ */
+
+/***
+ This file is part of avahi.
+
+ avahi is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ avahi is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+ Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with avahi; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <sys/un.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
+#include <signal.h>
+#include <sys/wait.h>
+#include <getopt.h>
+
+#include <glib.h>
+
+#include <avahi-common/util.h>
+#include <avahi-core/llist.h>
+
+#include <libdaemon/dfork.h>
+#include <libdaemon/dsignal.h>
+#include <libdaemon/dlog.h>
+#include <libdaemon/dpid.h>
+#include <libdaemon/dexec.h>
+
+#define BROWSE_DNS_SERVERS "BROWSE-DNS-SERVERS\n"
+
+static enum {
+ ACKWAIT,
+ BROWSING
+} state = ACKWAIT;
+
+static gboolean quit = FALSE;
+
+static enum {
+ DAEMON_RUN,
+ DAEMON_KILL,
+ DAEMON_RELOAD,
+ DAEMON_VERSION,
+ DAEMON_HELP
+} command = DAEMON_RUN;
+
+static gboolean daemonize = FALSE;
+
+typedef struct DNSServerInfo DNSServerInfo;
+
+struct DNSServerInfo {
+ gint interface;
+ guchar protocol;
+ gchar *address;
+ AVAHI_LLIST_FIELDS(DNSServerInfo, servers);
+};
+
+static AVAHI_LLIST_HEAD(DNSServerInfo, servers);
+
+static void server_info_free(DNSServerInfo *i) {
+ g_assert(i);
+
+ g_free(i->address);
+
+ AVAHI_LLIST_REMOVE(DNSServerInfo, servers, servers, i);
+ g_free(i);
+}
+
+static DNSServerInfo* get_server_info(gint interface, guchar protocol, const gchar *address) {
+ DNSServerInfo *i;
+ g_assert(address);
+
+ for (i = servers; i; i = i->servers_next)
+ if (i->interface == interface &&
+ i->protocol == protocol &&
+ strcmp(i->address, address) == 0)
+ return i;
+
+ return NULL;
+}
+
+static DNSServerInfo* new_server_info(gint interface, guchar protocol, const gchar *address) {
+ DNSServerInfo *i;
+
+ g_assert(address);
+
+ i = g_new(DNSServerInfo, 1);
+ i->interface = interface;
+ i->protocol = protocol;
+ i->address = g_strdup(address);
+
+ AVAHI_LLIST_PREPEND(DNSServerInfo, servers, servers, i);
+
+ return i;
+}
+
+static int open_socket(void) {
+ int fd = -1;
+ struct sockaddr_un sa;
+
+ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+ daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
+ goto fail;
+ }
+
+ if (avahi_set_cloexec(fd) < 0) {
+ daemon_log(LOG_ERR, "fcntl(): %s", strerror(errno));
+ goto fail;
+ }
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sun_family = AF_UNIX;
+ strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
+ sa.sun_path[sizeof(sa.sun_path)-1] = 0;
+
+ if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
+ daemon_log(LOG_ERR, "connect(): %s", strerror(errno));
+ goto fail;
+ }
+
+ return fd;
+
+fail:
+ if (fd >= 0)
+ close(fd);
+
+ return -1;
+}
+
+static ssize_t loop_write(int fd, const void*data, size_t size) {
+
+ ssize_t ret = 0;
+ g_assert(fd >= 0 && data && size);
+
+ while (size > 0) {
+ ssize_t r;
+
+ if ((r = write(fd, data, size)) < 0)
+ return r;
+
+ if (r == 0)
+ break;
+
+ ret += r;
+ data = (guint8*) data + r;
+ size -= r;
+ }
+
+ return ret;
+}
+
+static gchar *concat_dns_servers(gint interface) {
+ DNSServerInfo *i;
+ gchar *r = NULL;
+
+ for (i = servers; i; i = i->servers_next)
+ if (i->interface == interface || interface <= 0) {
+ gchar *t;
+
+ if (!r)
+ t = g_strdup(i->address);
+ else
+ t = g_strdup_printf("%s %s", r, i->address);
+
+ g_free(r);
+ r = t;
+ }
+
+ return r;
+}
+
+static gchar *getifname(gint interface, gchar *name, guint len) {
+ int fd = -1;
+ gchar *ret = NULL;
+ struct ifreq ifr;
+
+ g_assert(interface >= 0);
+
+ if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
+ daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
+ goto finish;
+ }
+
+ memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_ifindex = interface;
+
+ if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
+ daemon_log(LOG_ERR, "SIOCGIFNAME: %s\n", strerror(errno));
+ goto finish;
+ }
+
+ strncpy(name, ifr.ifr_name, len-1);
+ name[len-1] = 0;
+ ret = name;
+
+finish:
+ if (fd >= 0)
+ close(fd);
+
+ return ret;
+ }
+
+static void run_script(gboolean new, gint interface, guchar protocol, const gchar *address) {
+ gchar *p;
+ g_assert(interface > 0);
+ gint ret;
+ gchar ia[16], pa[16];
+ gchar name[IFNAMSIZ+1];
+
+ if (!getifname(interface, name, sizeof(name)))
+ return;
+
+ p = concat_dns_servers(interface);
+ g_setenv("AVAHI_INTERFACE_DNS_SERVERS", p ? p : "", TRUE);
+ g_free(p);
+
+ p = concat_dns_servers(-1);
+ g_setenv("AVAHI_DNS_SERVERS", p ? p : "", TRUE);
+ g_free(p);
+
+ g_setenv("AVAHI_INTERFACE", name, TRUE);
+
+ snprintf(ia, sizeof(ia), "%i", interface);
+ snprintf(pa, sizeof(pa), "%u", protocol);
+
+ if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, NULL) < 0)
+ daemon_log(LOG_WARNING, "Failed to run script");
+ else if (ret != 0)
+ daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
+}
+
+static gint new_line(const gchar *l) {
+ g_assert(l);
+
+ if (state == ACKWAIT) {
+ if (*l != '+') {
+ daemon_log(LOG_ERR, "Avahi command failed: %s", l);
+ return -1;
+ }
+
+ daemon_log(LOG_INFO, "Successfully connected to Avahi daemon.");
+ state = BROWSING;
+ } else {
+ gint interface;
+ guint protocol;
+ guint port;
+ gchar a[64];
+
+ g_assert(state == BROWSING);
+
+ if (*l != '<' && *l != '>') {
+ daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
+ return -1;
+ }
+
+ if (sscanf(l+1, "%i %u %64s %u", &interface, &protocol, a, &port) != 4) {
+ daemon_log(LOG_ERR, "Failed to parse browsing line: %s", l);
+ return -1;
+ }
+
+ if (*l == '>') {
+ if (port != 53)
+ daemon_log(LOG_WARNING, "DNS server with port address != 53 found, ignoring");
+ else {
+ daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%u)", a, interface, protocol);
+ new_server_info(interface, (guchar) protocol, a);
+ run_script(TRUE, interface, (guchar) protocol, a);
+ }
+ } else {
+ DNSServerInfo *i;
+
+ if (port == 53)
+ if ((i = get_server_info(interface, (guchar) protocol, a))) {
+ daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%u)", a, interface, protocol);
+ server_info_free(i);
+ run_script(FALSE, interface, (guchar) protocol, a);
+ }
+ }
+
+ }
+
+ return 0;
+}
+
+static gint do_connect(void) {
+ gint fd = -1;
+
+ if ((fd = open_socket()) < 0)
+ goto fail;
+
+ if (loop_write(fd, BROWSE_DNS_SERVERS, sizeof(BROWSE_DNS_SERVERS)-1) < 0) {
+ daemon_log(LOG_ERR, "write(): %s", strerror(errno));
+ goto fail;
+ }
+
+ state = ACKWAIT;
+ return fd;
+
+fail:
+ if (fd >= 0)
+ close(fd);
+
+ return -1;
+}
+
+static void free_dns_server_info_list(void) {
+ while (servers) {
+ gint interface = servers->interface;
+ guchar protocol = servers->protocol;
+ gchar *address = g_strdup(servers->address);
+ server_info_free(servers);
+
+ run_script(FALSE, interface, protocol, address);
+ g_free(address);
+ }
+}
+
+static void help(FILE *f, const gchar *argv0) {
+ fprintf(f,
+ "%s [options]\n"
+ " -h --help Show this help\n"
+ " -D --daemonize Daemonize after startup\n"
+ " -k --kill Kill a running daemon\n"
+ " -r --reload Request a running daemon to reload static services\n"
+ " -V --version Show version\n",
+ argv0);
+}
+
+static gint parse_command_line(int argc, char *argv[]) {
+ gint c;
+
+ static const struct option const long_options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "daemonize", no_argument, NULL, 'D' },
+ { "kill", no_argument, NULL, 'k' },
+ { "version", no_argument, NULL, 'V' },
+ { "reload", no_argument, NULL, 'r' },
+ };
+
+ opterr = 0;
+ while ((c = getopt_long(argc, argv, "hDkVr", long_options, NULL)) >= 0) {
+
+ switch(c) {
+ case 'h':
+ command = DAEMON_HELP;
+ break;
+ case 'D':
+ daemonize = TRUE;
+ break;
+ case 'k':
+ command = DAEMON_KILL;
+ break;
+ case 'V':
+ command = DAEMON_VERSION;
+ break;
+ case 'r':
+ command = DAEMON_RELOAD;
+ break;
+ default:
+ fprintf(stderr, "Invalid command line argument: %c\n", c);
+ return -1;
+ }
+ }
+
+ if (optind < argc) {
+ fprintf(stderr, "Too many arguments\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int run_daemon(void) {
+ gint fd = -1, ret = -1;
+ gchar buf[1024];
+ size_t buflen = 0;
+
+ AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
+
+ daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
+
+ if ((fd = do_connect()) < 0)
+ goto finish;
+
+ if (daemonize)
+ daemon_retval_send(0);
+
+ while (!quit) {
+ fd_set rfds, wfds;
+
+ FD_ZERO(&rfds);
+ FD_ZERO(&wfds);
+
+ FD_SET(fd, &rfds);
+ FD_SET(daemon_signal_fd(), &rfds);
+
+ for (;;) {
+ if (select(fd+1, &rfds, NULL, NULL, NULL) < 0) {
+ if (errno == EINTR)
+ continue;
+
+ daemon_log(LOG_ERR, "select(): %s", strerror(errno));
+ goto finish;
+ }
+
+ break;
+ }
+
+ if (FD_ISSET(daemon_signal_fd(), &rfds)) {
+
+ int sig;
+
+ if ((sig = daemon_signal_next()) <= 0) {
+ daemon_log(LOG_ERR, "daemon_signal_next() failed");
+ goto finish;
+ }
+
+ switch(sig) {
+ case SIGINT:
+ case SIGTERM:
+ daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
+ ret = 0;
+ goto finish;
+
+ case SIGCHLD:
+ waitpid(-1, NULL, WNOHANG);
+ break;
+
+ case SIGHUP:
+ daemon_log(LOG_INFO, "Rrefreshing DNS Server list");
+
+ close(fd);
+ free_dns_server_info_list();
+
+ if ((fd = do_connect()) < 0)
+ goto finish;
+
+ break;
+ }
+
+ } else if (FD_ISSET(fd, &rfds)) {
+ ssize_t r;
+ gchar *n;
+
+ if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
+ daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
+ goto finish;
+ }
+
+ buflen += r;
+ g_assert(buflen <= sizeof(buf)-1);
+
+ while ((n = memchr(buf, '\n', buflen))) {
+ *(n++) = 0;
+
+ if (new_line(buf) < 0)
+ goto finish;
+
+ buflen -= (n - buf);
+ memmove(buf, n, buflen);
+ }
+
+ if (buflen >= sizeof(buf)-1) {
+ /* The incoming line is horribly long */
+ buf[sizeof(buf)-1] = 0;
+
+ if (new_line(buf) < 0)
+ goto finish;
+
+ buflen = 0;
+ }
+ }
+ }
+
+ ret = 0;
+
+finish:
+
+ free_dns_server_info_list();
+
+ if (fd >= 0)
+ close(fd);
+
+ daemon_signal_done();
+
+ return ret;
+}
+
+static const char* pid_file_proc(void) {
+ return AVAHI_RUNTIME_DIR"/avahi-dnsconfd.pid";
+}
+
+gint main(gint argc, gchar *argv[]) {
+ gchar *argv0;
+ gint r = 1;
+ gboolean wrote_pid_file = FALSE;
+
+ if ((argv0 = strrchr(argv[0], '/')))
+ argv0++;
+ else
+ argv0 = argv[0];
+
+ daemon_pid_file_ident = daemon_log_ident = argv0;
+ daemon_pid_file_proc = pid_file_proc;
+
+ if (parse_command_line(argc, argv) < 0)
+ goto finish;
+
+ if (command == DAEMON_RUN) {
+ pid_t pid;
+
+ if (getuid() != 0) {
+ daemon_log(LOG_ERR, "This program is intended to be run as root.");
+ goto finish;
+ }
+
+ if ((pid = daemon_pid_file_is_running()) >= 0) {
+ daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
+ goto finish;
+ }
+
+ if (daemonize) {
+ daemon_retval_init();
+
+ if ((pid = daemon_fork()) < 0)
+ goto finish;
+ else if (pid != 0) {
+ int ret;
+ /** Parent **/
+
+ if ((ret = daemon_retval_wait(20)) < 0) {
+ daemon_log(LOG_ERR, "Could not recieve return value from daemon process.");
+ goto finish;
+ }
+
+ r = ret;
+ goto finish;
+ }
+
+ /* Child */
+ }
+
+ chdir("/");
+
+ if (daemon_pid_file_create() < 0) {
+ daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
+
+ if (daemonize)
+ daemon_retval_send(1);
+ goto finish;
+ } else
+ wrote_pid_file = TRUE;
+
+ if (run_daemon() < 0)
+ goto finish;
+
+ } else if (command == DAEMON_HELP) {
+ help(stdout, argv0);
+
+ } else if (command == DAEMON_VERSION) {
+ printf("%s "PACKAGE_VERSION"\n", argv0);
+
+ } else if (command == DAEMON_KILL) {
+ if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
+ daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
+ goto finish;
+ }
+
+ } else if (command == DAEMON_RELOAD) {
+ if (daemon_pid_file_kill(SIGHUP) < 0) {
+ daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
+ goto finish;
+ }
+ }
+
+ r = 0;
+
+finish:
+
+ if (daemonize)
+ daemon_retval_done();
+
+ if (wrote_pid_file)
+ daemon_pid_file_remove();
+
+ return r;
+}
diff --git a/configure.ac b/configure.ac
index 97a57e5..2bbea03 100644
--- a/configure.ac
+++ b/configure.ac
@@ -234,24 +234,23 @@ avahi-client/Makefile
initscript/Makefile
initscript/Debian/Makefile
initscript/Debian/75avahi
+avahi-dnsconfd/Makefile
])
AC_OUTPUT
dnl ==========================================================================
echo "
- $PACKAGE_NAME $VERSION
- ============
-
- prefix: ${prefix}
- sysconfdir: ${sysconfdir}
- dbus-1 system.d dir: ${DBUS_SYS_DIR}
- dbus-1 version: `pkg-config dbus-1 --modversion`
- compiler: ${CC}
- cflags: ${CFLAGS}
- Linux Distro: ${with_distro}
- User for Avahi: ${AVAHI_USER}
- Group for Avahi: ${AVAHI_GROUP}
+ ---{ $PACKAGE_NAME $VERSION }---
+
+ prefix: ${prefix}
+ sysconfdir: ${sysconfdir}
+ dbus-1 system.d dir: ${DBUS_SYS_DIR}
+ dbus-1 version: `pkg-config dbus-1 --modversion`
+ compiler: ${CC}
+ cflags: ${CFLAGS}
+ Linux Distro: ${with_distro}
+ User for Avahi: ${AVAHI_USER}
+ Group for Avahi: ${AVAHI_GROUP}
+
+ NOTE: Remember to create user ${AVAHI_USER} and group ${AVAHI_GROUP} before make install
"
-
-echo "NOTE: Remember to create user ${AVAHI_USER} and group ${AVAHI_GROUP} before make install"
-echo
diff --git a/todo b/todo
index 9f19798..0be5a5e 100644
--- a/todo
+++ b/todo
@@ -1,5 +1,9 @@
todo:
* release!
+* support for special domain PTR records based on local IP subnet address
+* Introduce AvahiProtocol
+* drop trailing dot on avahi_normalize_name()
+* c++ support
done:
* Probing/Conflict resolution