From: Lennart Poettering Date: Sun, 22 May 2005 15:04:39 +0000 (+0000) Subject: * implement AvahiServiceBrowser X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=618af3a6a37099c7bc1e07704d48355353b3ab98;p=catta * implement AvahiServiceBrowser git-svn-id: file:///home/lennart/svn/public/avahi/trunk@87 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-core/Makefile.am b/avahi-core/Makefile.am index 39d7844..6c30809 100644 --- a/avahi-core/Makefile.am +++ b/avahi-core/Makefile.am @@ -61,7 +61,8 @@ libavahi_core_la_SOURCES = \ resolve-host-name.c \ resolve-address.c \ browse-domain.c \ - browse-service-type.c + browse-service-type.c \ + browse-service.c prioq_test_SOURCES = \ prioq-test.c \ diff --git a/avahi-core/avahi-test.c b/avahi-core/avahi-test.c index 5603481..e4ed4ab 100644 --- a/avahi-core/avahi-test.c +++ b/avahi-core/avahi-test.c @@ -128,7 +128,7 @@ static void hnr_callback(AvahiHostNameResolver *r, gint iface, guchar protocol, if (a) avahi_address_snprint(t, sizeof(t), a); - g_message("HNR: (%i.%i) %s -> %s [%s]", iface, protocol, hostname, a ? t : "n/a", event == AVAHI_BROWSER_NEW ? "found" : "timeout"); + g_message("HNR: (%i.%i) <%s> -> %s [%s]", iface, protocol, hostname, a ? t : "n/a", event == AVAHI_BROWSER_NEW ? "found" : "timeout"); } static void ar_callback(AvahiAddressResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const AvahiAddress *a, const gchar *hostname, gpointer userdata) { @@ -136,17 +136,21 @@ static void ar_callback(AvahiAddressResolver *r, gint iface, guchar protocol, Av avahi_address_snprint(t, sizeof(t), a); - g_message("AR: (%i.%i) %s -> %s [%s]", iface, protocol, t, hostname ? hostname : "n/a", event == AVAHI_BROWSER_NEW ? "found" : "timeout"); + g_message("AR: (%i.%i) %s -> <%s> [%s]", iface, protocol, t, hostname ? hostname : "n/a", event == AVAHI_BROWSER_NEW ? "found" : "timeout"); } static void db_callback(AvahiDomainBrowser *b, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar *domain, gpointer userdata) { - g_message("DB: (%i.%i) %s [%s]", iface, protocol, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove"); + g_message("DB: (%i.%i) <%s> [%s]", iface, protocol, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove"); } static void stb_callback(AvahiServiceTypeBrowser *b, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar *service_type, const gchar *domain, gpointer userdata) { - g_message("STB: (%i.%i) %s in %s [%s]", iface, protocol, service_type, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove"); + g_message("STB: (%i.%i) %s in <%s> [%s]", iface, protocol, service_type, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove"); +} + +static void sb_callback(AvahiServiceBrowser *b, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar *name, const gchar *service_type, const gchar *domain, gpointer userdata) { + g_message("SB: (%i.%i) <%s> as %s in <%s> [%s]", iface, protocol, name, service_type, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove"); } int main(int argc, char *argv[]) { @@ -159,6 +163,7 @@ int main(int argc, char *argv[]) { AvahiAddress a; AvahiDomainBrowser *db; AvahiServiceTypeBrowser *stb; + AvahiServiceBrowser *sb; avahi_server_config_init(&config); /* config.host_name = g_strdup("test"); */ @@ -176,6 +181,8 @@ int main(int argc, char *argv[]) { db = avahi_domain_browser_new(server, -1, AF_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, db_callback, NULL); stb = avahi_service_type_browser_new(server, -1, AF_UNSPEC, NULL, stb_callback, NULL); + + sb = avahi_service_browser_new(server, -1, AF_UNSPEC, "_http._tcp", NULL, sb_callback, NULL); loop = g_main_loop_new(NULL, FALSE); @@ -189,6 +196,7 @@ int main(int argc, char *argv[]) { avahi_host_name_resolver_free(hnr); avahi_address_resolver_free(ar); avahi_service_type_browser_free(stb); + avahi_service_browser_free(sb); if (group) avahi_entry_group_free(group); diff --git a/avahi-core/browse-service-type.c b/avahi-core/browse-service-type.c index d981692..dec7eef 100644 --- a/avahi-core/browse-service-type.c +++ b/avahi-core/browse-service-type.c @@ -71,7 +71,7 @@ static void record_browser_callback(AvahiRecordBrowser*rr, gint interface, gucha if (!avahi_domain_equal(c, b->domain_name)) goto fail; - b->callback(b, interface, protocol, event, n, b->domain_name, b->userdata); + b->callback(b, interface, protocol, event, n, c, b->userdata); g_free(n); return; diff --git a/avahi-core/browse-service.c b/avahi-core/browse-service.c new file mode 100644 index 0000000..10b3f33 --- /dev/null +++ b/avahi-core/browse-service.c @@ -0,0 +1,123 @@ +/* $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 +#endif + +#include + +#include "browse.h" +#include "util.h" + +struct AvahiServiceBrowser { + AvahiServer *server; + gchar *domain_name; + gchar *service_type; + + AvahiRecordBrowser *record_browser; + + AvahiServiceBrowserCallback callback; + gpointer userdata; + + AVAHI_LLIST_FIELDS(AvahiServiceBrowser, browser); +}; + +static void record_browser_callback(AvahiRecordBrowser*rr, gint interface, guchar protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) { + AvahiServiceBrowser *b = userdata; + gchar *n, *e, *c, *s; + gchar service[128]; + + g_assert(rr); + g_assert(record); + g_assert(b); + + g_assert(record->key->type == AVAHI_DNS_TYPE_PTR); + + c = n = avahi_normalize_name(record->data.ptr.name); + + if (!(avahi_unescape_label((const gchar**) &c, service, sizeof(service)))) + goto fail; + + for (s = e = c; *c == '_';) { + c += strcspn(c, "."); + + if (*c == 0) + goto fail; + + g_assert(*c == '.'); + e = c; + c++; + } + + *e = 0; + + if (!avahi_domain_equal(c, b->domain_name)) + goto fail; + + b->callback(b, interface, protocol, event, service, s, c, b->userdata); + g_free(n); + + return; + +fail: + g_warning("Invalid service '%s'", n); + g_free(n); +} + +AvahiServiceBrowser *avahi_service_browser_new(AvahiServer *server, gint interface, guchar protocol, const gchar *service_type, const gchar *domain, AvahiServiceBrowserCallback callback, gpointer userdata) { + AvahiServiceBrowser *b; + AvahiKey *k; + gchar *n = NULL; + + g_assert(server); + g_assert(callback); + g_assert(service_type); + + b = g_new(AvahiServiceBrowser, 1); + b->server = server; + b->domain_name = avahi_normalize_name(domain ? domain : "local."); + b->service_type = avahi_normalize_name(service_type); + b->callback = callback; + b->userdata = userdata; + + n = g_strdup_printf("%s%s", b->service_type, b->domain_name); + k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR); + 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(AvahiServiceBrowser, browser, server->service_browsers, b); + + return b; +} + +void avahi_service_browser_free(AvahiServiceBrowser *b) { + g_assert(b); + + AVAHI_LLIST_REMOVE(AvahiServiceBrowser, browser, b->server->service_browsers, b); + + avahi_record_browser_free(b->record_browser); + g_free(b->domain_name); + g_free(b->service_type); + g_free(b); +} diff --git a/avahi-core/core.h b/avahi-core/core.h index 2c3dafd..4577584 100644 --- a/avahi-core/core.h +++ b/avahi-core/core.h @@ -249,10 +249,13 @@ typedef void (*AvahiServiceTypeBrowserCallback)(AvahiServiceTypeBrowser *b, gint AvahiServiceTypeBrowser *avahi_service_type_browser_new(AvahiServer *server, gint interface, guchar protocol, const gchar *domain, AvahiServiceTypeBrowserCallback callback, gpointer userdata); void avahi_service_type_browser_free(AvahiServiceTypeBrowser *b); +typedef struct AvahiServiceBrowser AvahiServiceBrowser; +typedef void (*AvahiServiceBrowserCallback)(AvahiServiceBrowser *b, gint interface, guchar protocol, AvahiBrowserEvent event, const gchar *name, const gchar *type, const gchar *domain, gpointer userdata); +AvahiServiceBrowser *avahi_service_browser_new(AvahiServer *server, gint interface, guchar protocol, const gchar *service_type, const gchar *domain, AvahiServiceBrowserCallback callback, gpointer userdata); +void avahi_service_browser_free(AvahiServiceBrowser *b); /* Not yet implemented */ -typedef struct AvahiServiceBrowser AvahiServiceBrowser; typedef struct AvahiServiceResolver AvahiServiceResolver; #endif diff --git a/avahi-core/server.c b/avahi-core/server.c index 83bec15..fd17078 100644 --- a/avahi-core/server.c +++ b/avahi-core/server.c @@ -966,6 +966,7 @@ AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, Avah AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, s->address_resolvers); AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, s->domain_browsers); AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, s->service_type_browsers); + AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, s->service_browsers); /* Get host name */ s->host_name = s->config.host_name ? avahi_normalize_name(s->config.host_name) : avahi_get_host_name(); @@ -1012,6 +1013,8 @@ void avahi_server_free(AvahiServer* s) { avahi_domain_browser_free(s->domain_browsers); while (s->service_type_browsers) avahi_service_type_browser_free(s->service_type_browsers); + while (s->service_browsers) + avahi_service_browser_free(s->service_browsers); while (s->record_browsers) avahi_record_browser_destroy(s->record_browsers); g_hash_table_destroy(s->record_browser_hashtable); diff --git a/avahi-core/server.h b/avahi-core/server.h index 60737ce..0e6f2e7 100644 --- a/avahi-core/server.h +++ b/avahi-core/server.h @@ -81,6 +81,7 @@ struct AvahiServer { AVAHI_LLIST_HEAD(AvahiAddressResolver, address_resolvers); AVAHI_LLIST_HEAD(AvahiDomainBrowser, domain_browsers); AVAHI_LLIST_HEAD(AvahiServiceTypeBrowser, service_type_browsers); + AVAHI_LLIST_HEAD(AvahiServiceBrowser, service_browsers); gboolean need_entry_cleanup, need_group_cleanup, need_browser_cleanup;