X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-core%2Fbrowse-service-type.c;h=252e3cb0f1d2fe389f4ba8f6dee36dff500e8e66;hb=b6fa96edc8d834c5b538953a7c0a80f532784172;hp=946702874738117d1904f23967ea8e19bd655670;hpb=8b5cd6ffd9137b14b7ed678f10a551e3911e4a40;p=catta diff --git a/avahi-core/browse-service-type.c b/avahi-core/browse-service-type.c index 9467028..252e3cb 100644 --- a/avahi-core/browse-service-type.c +++ b/avahi-core/browse-service-type.c @@ -27,6 +27,7 @@ #include #include +#include #include "browse.h" #include "log.h" @@ -34,7 +35,7 @@ struct AvahiSServiceTypeBrowser { AvahiServer *server; char *domain_name; - + AvahiSRecordBrowser *record_browser; AvahiSServiceTypeBrowserCallback callback; @@ -43,83 +44,104 @@ struct AvahiSServiceTypeBrowser { AVAHI_LLIST_FIELDS(AvahiSServiceTypeBrowser, browser); }; -static void record_browser_callback(AvahiSRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) { +static void record_browser_callback( + AvahiSRecordBrowser*rr, + AvahiIfIndex interface, + AvahiProtocol protocol, + AvahiBrowserEvent event, + AvahiRecord *record, + AvahiLookupResultFlags flags, + void* userdata) { + AvahiSServiceTypeBrowser *b = userdata; - char *n, *e, *c; assert(rr); - assert(record); assert(b); - assert(record->key->type == AVAHI_DNS_TYPE_PTR); - - n = avahi_normalize_name(record->data.ptr.name); - - if (*n != '_') - goto fail; - - for (c = e = n; *c == '_';) { - c += strcspn(c, "."); + /* Filter flags */ + flags &= AVAHI_LOOKUP_RESULT_CACHED | AVAHI_LOOKUP_RESULT_MULTICAST | AVAHI_LOOKUP_RESULT_WIDE_AREA; - if (*c == 0) - goto fail; + if (record) { + char type[AVAHI_DOMAIN_NAME_MAX], domain[AVAHI_DOMAIN_NAME_MAX]; + + assert(record->key->type == AVAHI_DNS_TYPE_PTR); - assert(*c == '.'); - e = c; - c++; - } + if (avahi_service_name_split(record->data.ptr.name, NULL, 0, type, sizeof(type), domain, sizeof(domain)) < 0) { + avahi_log_warn("Invalid service type '%s'", record->key->name); + return; + } - *e = 0; - - if (!avahi_domain_equal(c, b->domain_name)) - goto fail; - - b->callback(b, interface, protocol, event, n, c, b->userdata); - avahi_free(n); - - return; - -fail: - avahi_log_warn("Invalid service type '%s'", n); - avahi_free(n); + b->callback(b, interface, protocol, event, type, domain, flags, b->userdata); + } else + b->callback(b, interface, protocol, event, NULL, b->domain_name, flags, b->userdata); } -AvahiSServiceTypeBrowser *avahi_s_service_type_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiSServiceTypeBrowserCallback callback, void* userdata) { +AvahiSServiceTypeBrowser *avahi_s_service_type_browser_new( + AvahiServer *server, + AvahiIfIndex interface, + AvahiProtocol protocol, + const char *domain, + AvahiLookupFlags flags, + AvahiSServiceTypeBrowserCallback callback, + void* userdata) { + AvahiSServiceTypeBrowser *b; - AvahiKey *k; - char *n = NULL; + AvahiKey *k = NULL; + char n[AVAHI_DOMAIN_NAME_MAX]; + int r; assert(server); assert(callback); - if (domain && !avahi_is_valid_domain_name(domain)) { - avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME); + AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE); + AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL); + AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME); + AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS); + + if (!domain) + domain = server->domain_name; + + if ((r = avahi_service_name_join(n, sizeof(n), NULL, "_services._dns-sd._udp", domain)) < 0) { + avahi_server_set_errno(server, r); return NULL; } - + if (!(b = avahi_new(AvahiSServiceTypeBrowser, 1))) { avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY); return NULL; } b->server = server; - b->domain_name = avahi_normalize_name(domain ? domain : "local"); b->callback = callback; b->userdata = userdata; - + b->record_browser = NULL; + AVAHI_LLIST_PREPEND(AvahiSServiceTypeBrowser, browser, server->service_type_browsers, b); - n = avahi_strdup_printf("_services._dns-sd._udp.%s", b->domain_name); - k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR); - avahi_free(n); + if (!(b->domain_name = avahi_normalize_name_strdup(domain))) { + avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY); + goto fail; + } + + if (!(k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR))) { + avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY); + goto fail; + } - b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, b); + if (!(b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, b))) + goto fail; + avahi_key_unref(k); - if (!b->record_browser) - return NULL; - return b; + +fail: + if (k) + avahi_key_unref(k); + + avahi_s_service_type_browser_free(b); + + return NULL; } void avahi_s_service_type_browser_free(AvahiSServiceTypeBrowser *b) { @@ -129,7 +151,7 @@ void avahi_s_service_type_browser_free(AvahiSServiceTypeBrowser *b) { if (b->record_browser) avahi_s_record_browser_free(b->record_browser); - + avahi_free(b->domain_name); avahi_free(b); }