diff options
Diffstat (limited to 'avahi-core')
-rw-r--r-- | avahi-core/Makefile.am | 3 | ||||
-rw-r--r-- | avahi-core/avahi-test.c | 22 | ||||
-rw-r--r-- | avahi-core/browse-dns-server.c | 194 | ||||
-rw-r--r-- | avahi-core/browse-domain.c | 11 | ||||
-rw-r--r-- | avahi-core/core.h | 44 | ||||
-rw-r--r-- | avahi-core/server.c | 104 | ||||
-rw-r--r-- | avahi-core/server.h | 1 |
7 files changed, 368 insertions, 11 deletions
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; |