From: Lennart Poettering Date: Sat, 15 Oct 2005 19:16:47 +0000 (+0000) Subject: implement sw_discovery_browse_domains() X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=97613137fbe867f1d4905e60712f635b0cfd7b87;p=catta implement sw_discovery_browse_domains() git-svn-id: file:///home/lennart/svn/public/avahi/trunk@782 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-compat-howl/Makefile.am b/avahi-compat-howl/Makefile.am index 8202703..5c12782 100644 --- a/avahi-compat-howl/Makefile.am +++ b/avahi-compat-howl/Makefile.am @@ -70,7 +70,7 @@ HOWLHEADERS = \ lib_LTLIBRARIES = libavahi-compat-howl.la -noinst_PROGRAMS = address-test text-test +noinst_PROGRAMS = address-test text-test browse-domain-test libavahi_compat_howl_la_SOURCES = \ $(HOWLHEADERS) \ @@ -98,4 +98,10 @@ text_test_SOURCES = \ text_test_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) -I $(srcdir)/include text_test_LDADD = $(AM_LDADD) ../avahi-common/libavahi-common.la +browse_domain_test_SOURCES = \ + $(HOWLHEADERS) \ + browse-domain-test.c +browse_domain_test_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) -I $(srcdir)/include +browse_domain_test_LDADD = $(AM_LDADD) libavahi-compat-howl.la + endif diff --git a/avahi-compat-howl/address-test.c b/avahi-compat-howl/address-test.c index b9c8b13..73c356d 100644 --- a/avahi-compat-howl/address-test.c +++ b/avahi-compat-howl/address-test.c @@ -26,7 +26,7 @@ #include #include -#include +#include "howl.h" #define ASSERT_SW_OKAY(t) { sw_result r; r = (t); assert(r == SW_OKAY); } #define ASSERT_NOT_NULL(t) { const void* r; r = (t); assert(r); } diff --git a/avahi-compat-howl/browse-domain-test.c b/avahi-compat-howl/browse-domain-test.c new file mode 100644 index 0000000..2eedd48 --- /dev/null +++ b/avahi-compat-howl/browse-domain-test.c @@ -0,0 +1,75 @@ +/* $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 + +#include "howl.h" + +#define ASSERT_SW_OKAY(t) { sw_result _r; _r = (t); assert(_r == SW_OKAY); } +#define ASSERT_NOT_NULL(t) { const void* _r; r = (t); assert(_r); } + +static sw_result reply( + sw_discovery discovery, + sw_discovery_oid oid, + sw_discovery_browse_status status, + sw_uint32 interface_index, + sw_const_string name, + sw_const_string type, + sw_const_string domain, + sw_opaque extra) { + + switch (status) { + case SW_DISCOVERY_BROWSE_ADD_DOMAIN: + fprintf(stderr, "new domain %s\n", domain); + break; + + case SW_DISCOVERY_BROWSE_REMOVE_DOMAIN: + fprintf(stderr, "removed domain %s\n", domain); + break; + + case SW_DISCOVERY_BROWSE_INVALID: + fprintf(stderr, "some kind of failure happened: %s\n", domain); + break; + + default: + abort(); + } + + return SW_OKAY; +} + +int main(int argc, char *argv[]) { + sw_discovery discovery; + sw_discovery_oid oid; + + ASSERT_SW_OKAY(sw_discovery_init(&discovery)); + + ASSERT_SW_OKAY(sw_discovery_browse_domains(discovery, 0, reply, NULL, &oid)); + + ASSERT_SW_OKAY(sw_discovery_run(discovery)); + + return 0; +} diff --git a/avahi-compat-howl/compat.c b/avahi-compat-howl/compat.c index f5415bf..d72b882 100644 --- a/avahi-compat-howl/compat.c +++ b/avahi-compat-howl/compat.c @@ -524,14 +524,90 @@ sw_result sw_discovery_publish( return SW_E_NO_IMPL; } +static void domain_browser_callback( + AvahiDomainBrowser *b, + AvahiIfIndex interface, + AvahiProtocol protocol, + AvahiBrowserEvent event, + const char *domain, + AvahiLookupResultFlags flags, + void *userdata) { + + oid_data* data = userdata; + sw_discovery_browse_reply reply; + static char domain_fixed[AVAHI_DOMAIN_NAME_MAX]; + + assert(b); + assert(data); + + reply = (sw_discovery_browse_reply) data->reply; + + domain = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed)); + + switch (event) { + case AVAHI_BROWSER_NEW: + reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_ADD_DOMAIN, interface, NULL, NULL, domain, data->extra); + break; + + case AVAHI_BROWSER_REMOVE: + reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_REMOVE_DOMAIN, interface, NULL, NULL, domain, data->extra); + break; + + case AVAHI_BROWSER_FAILURE: + reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_INVALID, interface, NULL, NULL, domain, data->extra); + break; + + case AVAHI_BROWSER_CACHE_EXHAUSTED: + case AVAHI_BROWSER_ALL_FOR_NOW: + break; + } +} + sw_result sw_discovery_browse_domains( sw_discovery self, sw_uint32 interface_index, sw_discovery_browse_reply reply, sw_opaque extra, sw_discovery_oid * oid) { - AVAHI_WARN_UNSUPPORTED; - return SW_E_NO_IMPL; + + oid_data *data; + AvahiIfIndex ifindex; + sw_result result = SW_E_UNKNOWN; + + assert(self); + assert(reply); + assert(oid); + + AVAHI_WARN_LINKAGE; + + if ((*oid = oid_alloc(self, OID_DOMAIN_BROWSER)) == (sw_discovery_oid) -1) + return SW_E_UNKNOWN; + + data = oid_get(self, *oid); + assert(data); + data->reply = (sw_result (*)(void)) reply; + data->extra = extra; + + ifindex = interface_index == 0 ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface_index; + + ASSERT_SUCCESS(pthread_mutex_lock(&self->mutex)); + + if (!(data->object = avahi_domain_browser_new(self->client, ifindex, AVAHI_PROTO_INET, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, 0, domain_browser_callback, data))) { + result = map_error(avahi_client_errno(self->client)); + goto finish; + } + + result = SW_OKAY; + +finish: + + ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex)); + + if (result != SW_OKAY) + if (*oid != (sw_discovery_oid) -1) + oid_release(self, *oid); + + return result; } static void service_resolver_callback( @@ -753,6 +829,14 @@ sw_result sw_discovery_cancel(sw_discovery self, sw_discovery_oid oid) { avahi_service_resolver_free(data->object); break; + case OID_DOMAIN_BROWSER: + avahi_domain_browser_free(data->object); + break; + + case OID_ENTRY_GROUP: + avahi_entry_group_free(data->object); + break; + case OID_UNUSED: ; } diff --git a/avahi-compat-howl/text-test.c b/avahi-compat-howl/text-test.c index a32e113..995a7f5 100644 --- a/avahi-compat-howl/text-test.c +++ b/avahi-compat-howl/text-test.c @@ -26,7 +26,7 @@ #include #include -#include +#include "howl.h" #define ASSERT_SW_OKAY(t) { sw_result _r; _r = (t); assert(_r == SW_OKAY); } #define ASSERT_NOT_NULL(t) { const void* _r; r = (t); assert(_r); }