From 6b391bb81f0dce0193a722254016b26c12a17643 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 18 Oct 2005 19:52:03 +0000 Subject: * remove AVAHI_PUBLISH_IS_PROXY, it was a bad idea * drop avahi_service_is_service_local(), avahi_client_is_service_local() * add new lookup result flags AVAHI_LOOKUP_RESULT_OUR_OWN and AVAHI_LOOKUP_RESULT_LOCAL * remove avahi_address_resolver_new() and replace it by avahi_address_resolver_new_a() * avahi-client: save query data in browse/resolve objects so that we can return it on failure * other cleanups git-svn-id: file:///home/lennart/svn/public/avahi/trunk@811 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-core/browse-dns-server.c | 2 + avahi-core/browse-domain.c | 4 +- avahi-core/browse-service-type.c | 3 ++ avahi-core/browse-service.c | 6 +++ avahi-core/browse.c | 3 ++ avahi-core/core.h | 3 -- avahi-core/entry.c | 17 ++++---- avahi-core/internal.h | 5 +++ avahi-core/publish.h | 3 ++ avahi-core/resolve-address.c | 1 - avahi-core/server.c | 84 +++++++++++++++++++++++++++++++++------- 11 files changed, 103 insertions(+), 28 deletions(-) (limited to 'avahi-core') diff --git a/avahi-core/browse-dns-server.c b/avahi-core/browse-dns-server.c index 2c907c3..ba0aeaa 100644 --- a/avahi-core/browse-dns-server.c +++ b/avahi-core/browse-dns-server.c @@ -151,6 +151,8 @@ static void record_browser_callback( assert(rr); assert(b); + /* Filter flags */ + flags &= AVAHI_LOOKUP_RESULT_CACHED | AVAHI_LOOKUP_RESULT_MULTICAST | AVAHI_LOOKUP_RESULT_WIDE_AREA; switch (event) { case AVAHI_BROWSER_NEW: { diff --git a/avahi-core/browse-domain.c b/avahi-core/browse-domain.c index e2eab44..5074469 100644 --- a/avahi-core/browse-domain.c +++ b/avahi-core/browse-domain.c @@ -54,6 +54,9 @@ static void record_browser_callback( assert(rr); assert(b); + + /* Filter flags */ + flags &= AVAHI_LOOKUP_RESULT_CACHED | AVAHI_LOOKUP_RESULT_MULTICAST | AVAHI_LOOKUP_RESULT_WIDE_AREA; if (record) { assert(record->key->type == AVAHI_DNS_TYPE_PTR); @@ -63,7 +66,6 @@ static void record_browser_callback( b->callback(b, interface, protocol, event, n, flags, b->userdata); } - AvahiSDomainBrowser *avahi_s_domain_browser_new( AvahiServer *server, AvahiIfIndex interface, diff --git a/avahi-core/browse-service-type.c b/avahi-core/browse-service-type.c index 39bf471..252e3cb 100644 --- a/avahi-core/browse-service-type.c +++ b/avahi-core/browse-service-type.c @@ -58,6 +58,9 @@ static void record_browser_callback( assert(rr); assert(b); + /* Filter flags */ + flags &= AVAHI_LOOKUP_RESULT_CACHED | AVAHI_LOOKUP_RESULT_MULTICAST | AVAHI_LOOKUP_RESULT_WIDE_AREA; + if (record) { char type[AVAHI_DOMAIN_NAME_MAX], domain[AVAHI_DOMAIN_NAME_MAX]; diff --git a/avahi-core/browse-service.c b/avahi-core/browse-service.c index 170a64f..43778dd 100644 --- a/avahi-core/browse-service.c +++ b/avahi-core/browse-service.c @@ -59,11 +59,17 @@ static void record_browser_callback( assert(rr); assert(b); + /* Filter flags */ + flags &= AVAHI_LOOKUP_RESULT_CACHED | AVAHI_LOOKUP_RESULT_MULTICAST | AVAHI_LOOKUP_RESULT_WIDE_AREA; + if (record) { char service[AVAHI_LABEL_MAX], type[AVAHI_DOMAIN_NAME_MAX], domain[AVAHI_DOMAIN_NAME_MAX]; assert(record->key->type == AVAHI_DNS_TYPE_PTR); + if (event == AVAHI_BROWSER_NEW && avahi_server_is_service_local(b->server, interface, protocol, record->data.ptr.name)) + flags |= AVAHI_LOOKUP_RESULT_LOCAL; + if (avahi_service_name_split(record->data.ptr.name, service, sizeof(service), type, sizeof(type), domain, sizeof(domain)) < 0) { avahi_log_warn("Failed to split '%s'", record->key->name); return; diff --git a/avahi-core/browse.c b/avahi-core/browse.c index 09bc0fa..e7e01d1 100644 --- a/avahi-core/browse.c +++ b/avahi-core/browse.c @@ -280,6 +280,9 @@ static void lookup_multicast_callback( /* It's a normal record, so let's call the user callback */ assert(avahi_key_equal(b->key, l->key)); + if (avahi_server_is_record_local(b->server, interface, protocol, r)) + flags |= AVAHI_LOOKUP_RESULT_LOCAL; + b->callback(b, interface, protocol, event, r, flags, b->userdata); } break; diff --git a/avahi-core/core.h b/avahi-core/core.h index e3bf8a3..4223bf7 100644 --- a/avahi-core/core.h +++ b/avahi-core/core.h @@ -154,9 +154,6 @@ int avahi_server_errno(AvahiServer *s); /** Return the local service cookie */ uint32_t avahi_server_get_local_service_cookie(AvahiServer *s); -/** Return 1 if there is a local service with the specified credentials registeresd. Return 0 if not, negative on failure */ -int avahi_server_is_service_local(AvahiServer *s, AvahiIfIndex, AvahiProtocol protocol, const char *name, const char *type, const char*domain); - /** Set the wide area DNS servers */ int avahi_server_set_wide_area_servers(AvahiServer *s, const AvahiAddress *a, unsigned n); diff --git a/avahi-core/entry.c b/avahi-core/entry.c index fe7702e..3b73ff3 100644 --- a/avahi-core/entry.c +++ b/avahi-core/entry.c @@ -172,7 +172,6 @@ int avahi_server_add( AVAHI_PUBLISH_NO_PROBE| AVAHI_PUBLISH_UNIQUE| AVAHI_PUBLISH_ALLOW_MULTIPLE| - AVAHI_PUBLISH_IS_PROXY| AVAHI_PUBLISH_UPDATE)) return avahi_server_set_errno(s, AVAHI_ERR_INVALID_FLAGS); @@ -523,7 +522,7 @@ static int server_add_service_strlst_nocopy( AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE); AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL); - AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_COOKIE|AVAHI_PUBLISH_IS_PROXY|AVAHI_PUBLISH_UPDATE), AVAHI_ERR_INVALID_FLAGS); + AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_COOKIE|AVAHI_PUBLISH_UPDATE), AVAHI_ERR_INVALID_FLAGS); AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME); AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE); AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME); @@ -549,7 +548,7 @@ static int server_add_service_strlst_nocopy( /* Add service enumeration PTR record */ - if ((ret = avahi_server_add_ptr(s, g, interface, protocol, flags & AVAHI_PUBLISH_IS_PROXY, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0) + if ((ret = avahi_server_add_ptr(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0) goto fail; /* Add SRV record */ @@ -564,7 +563,7 @@ static int server_add_service_strlst_nocopy( r->data.srv.port = port; r->data.srv.name = h; h = NULL; - ret = avahi_server_add(s, g, interface, protocol, (flags & AVAHI_PUBLISH_IS_PROXY) | AVAHI_PUBLISH_UNIQUE, r); + ret = avahi_server_add(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE, r); avahi_record_unref(r); if (ret < 0) @@ -575,7 +574,7 @@ static int server_add_service_strlst_nocopy( if (!(flags & AVAHI_PUBLISH_NO_COOKIE)) strlst = add_magic_cookie(s, strlst); - ret = server_add_txt_strlst_nocopy(s, g, interface, protocol, (flags & AVAHI_PUBLISH_IS_PROXY) | AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL, svc_name, strlst); + ret = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL, svc_name, strlst); strlst = NULL; if (ret < 0) @@ -583,7 +582,7 @@ static int server_add_service_strlst_nocopy( /* Add service type enumeration record */ - ret = avahi_server_add_ptr(s, g, interface, protocol, (flags & AVAHI_PUBLISH_IS_PROXY), AVAHI_DEFAULT_TTL, enum_ptr, ptr_name); + ret = avahi_server_add_ptr(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, enum_ptr, ptr_name); fail: @@ -676,7 +675,7 @@ static int server_update_service_txt_strlst_nocopy( AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE); AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL); - AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_IS_PROXY|AVAHI_PUBLISH_NO_COOKIE), AVAHI_ERR_INVALID_FLAGS); + AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_COOKIE), AVAHI_ERR_INVALID_FLAGS); AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME); AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE); AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME); @@ -693,7 +692,7 @@ static int server_update_service_txt_strlst_nocopy( if (!(flags & AVAHI_PUBLISH_NO_COOKIE)) strlst = add_magic_cookie(s, strlst); - ret = server_add_txt_strlst_nocopy(s, g, interface, protocol, (flags & AVAHI_PUBLISH_IS_PROXY) | AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_UPDATE, AVAHI_DEFAULT_TTL, svc_name, strlst); + ret = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_UPDATE, AVAHI_DEFAULT_TTL, svc_name, strlst); strlst = NULL; fail: @@ -789,7 +788,7 @@ int avahi_server_add_service_subtype( goto fail; } - if ((ret = avahi_server_add_ptr(s, g, interface, protocol, flags & AVAHI_PUBLISH_IS_PROXY, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0) + if ((ret = avahi_server_add_ptr(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0) goto fail; fail: diff --git a/avahi-core/internal.h b/avahi-core/internal.h index 47a9c84..27edde8 100644 --- a/avahi-core/internal.h +++ b/avahi-core/internal.h @@ -180,6 +180,9 @@ void avahi_server_decrease_host_rr_pending(AvahiServer *s); int avahi_server_set_errno(AvahiServer *s, int error); +int avahi_server_is_service_local(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, const char *name); +int avahi_server_is_record_local(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *record); + int avahi_server_add_ptr( AvahiServer *s, AvahiSEntryGroup *g, @@ -191,6 +194,8 @@ int avahi_server_add_ptr( const char *dest); + + #define AVAHI_CHECK_VALIDITY_RETURN_NULL(server, expression, error) { \ if (!(expression)) { \ avahi_server_set_errno((server), (error)); \ diff --git a/avahi-core/publish.h b/avahi-core/publish.h index a059a4b..38e5116 100644 --- a/avahi-core/publish.h +++ b/avahi-core/publish.h @@ -197,6 +197,9 @@ int avahi_server_update_service_txt( const char *domain, ...) AVAHI_GCC_SENTINEL; +/** Check if there is a service locally defined and return the entry group it is attached to. Returns NULL if the service isn't local*/ +int avahi_server_get_group_of_service(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain, AvahiSEntryGroup** ret_group); + #ifndef DOXYGEN_SHOULD_SKIP_THIS AVAHI_C_DECL_END #endif diff --git a/avahi-core/resolve-address.c b/avahi-core/resolve-address.c index 661982d..94e07f5 100644 --- a/avahi-core/resolve-address.c +++ b/avahi-core/resolve-address.c @@ -108,7 +108,6 @@ static void record_browser_callback( assert(rr); assert(r); - switch (event) { case AVAHI_BROWSER_NEW: assert(record); diff --git a/avahi-core/server.c b/avahi-core/server.c index 5651bc1..a1486f8 100644 --- a/avahi-core/server.c +++ b/avahi-core/server.c @@ -1566,17 +1566,40 @@ uint32_t avahi_server_get_local_service_cookie(AvahiServer *s) { return s->local_service_cookie; } -int avahi_server_is_service_local(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char*domain) { +static AvahiEntry *find_entry(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key) { + AvahiEntry *e; + + assert(s); + assert(key); + + for (e = avahi_hashmap_lookup(s->entries_by_key, key); e; e = e->by_key_next) + + if ((e->interface == interface || e->interface <= 0 || interface <= 0) && + (e->protocol == protocol || e->protocol == AVAHI_PROTO_UNSPEC || protocol == AVAHI_PROTO_UNSPEC) && + (!e->group || e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED || e->group->state == AVAHI_ENTRY_GROUP_REGISTERING)) + + return e; + + return NULL; +} + +int avahi_server_get_group_of_service(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain, AvahiSEntryGroup** ret_group) { AvahiKey *key = NULL; - char n[256]; - int ret; AvahiEntry *e; + int ret; + char n[AVAHI_DOMAIN_NAME_MAX]; assert(s); assert(name); assert(type); - assert(domain); + assert(ret_group); + + if (!AVAHI_IF_VALID(interface)) + return avahi_server_set_errno(s, AVAHI_ERR_INVALID_INTERFACE); + if (!AVAHI_IF_VALID(protocol)) + return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PROTOCOL); + if (!avahi_is_valid_service_name(name)) return avahi_server_set_errno(s, AVAHI_ERR_INVALID_SERVICE_NAME); @@ -1592,21 +1615,54 @@ int avahi_server_is_service_local(AvahiServer *s, AvahiIfIndex interface, AvahiP if (!(key = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV))) return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); - ret = 0; - - for (e = avahi_hashmap_lookup(s->entries_by_key, key); e; e = e->by_key_next) { + e = find_entry(s, interface, protocol, key); + avahi_key_unref(key); - if ((e->interface == interface || e->interface <= 0 || interface <= 0) && - (e->protocol == protocol || e->protocol == AVAHI_PROTO_UNSPEC || protocol == AVAHI_PROTO_UNSPEC) && - !(e->flags & AVAHI_PUBLISH_IS_PROXY)) { - ret = 1; - break; - } + if (e) { + *ret_group = e->group; + return AVAHI_OK; } + + return avahi_server_set_errno(s, AVAHI_ERR_NOT_FOUND); +} + +int avahi_server_is_service_local(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, const char *name) { + AvahiKey *key = NULL; + AvahiEntry *e; + + assert(s); + assert(name); + + if (!s->host_name_fqdn) + return 0; + if (!(key = avahi_key_new(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV))) + return 0; + + e = find_entry(s, interface, protocol, key); avahi_key_unref(key); + + if (!e) + return 0; - return ret; + return avahi_domain_equal(s->host_name_fqdn, e->record->data.srv.name); +} + +int avahi_server_is_record_local(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *record) { + AvahiEntry *e; + + assert(s); + assert(record); + + for (e = avahi_hashmap_lookup(s->entries_by_key, record->key); e; e = e->by_key_next) + + if ((e->interface == interface || e->interface <= 0 || interface <= 0) && + (e->protocol == protocol || e->protocol == AVAHI_PROTO_UNSPEC || protocol == AVAHI_PROTO_UNSPEC) && + (!e->group || e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED || e->group->state == AVAHI_ENTRY_GROUP_REGISTERING) && + avahi_record_equal_no_ttl(record, e->record)) + return 1; + + return 0; } /** Set the wide area DNS servers */ -- cgit