2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <avahi-common/domain.h>
27 #include <avahi-common/malloc.h>
28 #include <avahi-common/error.h>
33 struct AvahiSServiceBrowser {
38 AvahiSRecordBrowser *record_browser;
40 AvahiSServiceBrowserCallback callback;
43 AVAHI_LLIST_FIELDS(AvahiSServiceBrowser, browser);
46 static void record_browser_callback(
47 AvahiSRecordBrowser*rr,
48 AvahiIfIndex interface,
49 AvahiProtocol protocol,
50 AvahiBrowserEvent event,
52 AvahiLookupResultFlags flags,
55 AvahiSServiceBrowser *b = userdata;
61 flags &= AVAHI_LOOKUP_RESULT_CACHED | AVAHI_LOOKUP_RESULT_MULTICAST | AVAHI_LOOKUP_RESULT_WIDE_AREA;
64 char service[AVAHI_LABEL_MAX], type[AVAHI_DOMAIN_NAME_MAX], domain[AVAHI_DOMAIN_NAME_MAX];
66 assert(record->key->type == AVAHI_DNS_TYPE_PTR);
68 if (event == AVAHI_BROWSER_NEW && avahi_server_is_service_local(b->server, interface, protocol, record->data.ptr.name))
69 flags |= AVAHI_LOOKUP_RESULT_LOCAL;
71 if (avahi_service_name_split(record->data.ptr.name, service, sizeof(service), type, sizeof(type), domain, sizeof(domain)) < 0) {
72 avahi_log_warn("Failed to split '%s'", record->key->name);
76 b->callback(b, interface, protocol, event, service, type, domain, flags, b->userdata);
79 b->callback(b, interface, protocol, event, NULL, b->service_type, b->domain_name, flags, b->userdata);
83 AvahiSServiceBrowser *avahi_s_service_browser_new(
85 AvahiIfIndex interface,
86 AvahiProtocol protocol,
87 const char *service_type,
89 AvahiLookupFlags flags,
90 AvahiSServiceBrowserCallback callback,
93 AvahiSServiceBrowser *b;
95 char n[AVAHI_DOMAIN_NAME_MAX];
100 assert(service_type);
102 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
103 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
104 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
105 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
106 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, avahi_is_valid_service_type_generic(service_type), AVAHI_ERR_INVALID_SERVICE_TYPE);
109 domain = server->domain_name;
111 if ((r = avahi_service_name_join(n, sizeof(n), NULL, service_type, domain)) < 0) {
112 avahi_server_set_errno(server, r);
116 if (!(b = avahi_new(AvahiSServiceBrowser, 1))) {
117 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
122 b->domain_name = b->service_type = NULL;
123 b->callback = callback;
124 b->userdata = userdata;
125 b->record_browser = NULL;
127 AVAHI_LLIST_PREPEND(AvahiSServiceBrowser, browser, server->service_browsers, b);
129 if (!(b->domain_name = avahi_normalize_name_strdup(domain)) ||
130 !(b->service_type = avahi_normalize_name_strdup(service_type))) {
131 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
135 if (!(k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR))) {
136 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
140 if (!(b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, b)))
152 avahi_s_service_browser_free(b);
156 void avahi_s_service_browser_free(AvahiSServiceBrowser *b) {
159 AVAHI_LLIST_REMOVE(AvahiSServiceBrowser, browser, b->server->service_browsers, b);
161 if (b->record_browser)
162 avahi_s_record_browser_free(b->record_browser);
164 avahi_free(b->domain_name);
165 avahi_free(b->service_type);