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 AvahiSServiceTypeBrowser {
37 AvahiSRecordBrowser *record_browser;
39 AvahiSServiceTypeBrowserCallback callback;
42 AVAHI_LLIST_FIELDS(AvahiSServiceTypeBrowser, browser);
45 static void record_browser_callback(
46 AvahiSRecordBrowser*rr,
47 AvahiIfIndex interface,
48 AvahiProtocol protocol,
49 AvahiBrowserEvent event,
51 AvahiLookupResultFlags flags,
54 AvahiSServiceTypeBrowser *b = userdata;
60 flags &= AVAHI_LOOKUP_RESULT_CACHED | AVAHI_LOOKUP_RESULT_MULTICAST | AVAHI_LOOKUP_RESULT_WIDE_AREA;
63 char type[AVAHI_DOMAIN_NAME_MAX], domain[AVAHI_DOMAIN_NAME_MAX];
65 assert(record->key->type == AVAHI_DNS_TYPE_PTR);
67 if (avahi_service_name_split(record->data.ptr.name, NULL, 0, type, sizeof(type), domain, sizeof(domain)) < 0) {
68 avahi_log_warn("Invalid service type '%s'", record->key->name);
72 b->callback(b, interface, protocol, event, type, domain, flags, b->userdata);
74 b->callback(b, interface, protocol, event, NULL, b->domain_name, flags, b->userdata);
77 AvahiSServiceTypeBrowser *avahi_s_service_type_browser_new(
79 AvahiIfIndex interface,
80 AvahiProtocol protocol,
82 AvahiLookupFlags flags,
83 AvahiSServiceTypeBrowserCallback callback,
86 AvahiSServiceTypeBrowser *b;
88 char n[AVAHI_DOMAIN_NAME_MAX];
94 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
95 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
96 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
97 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
100 domain = server->domain_name;
102 if ((r = avahi_service_name_join(n, sizeof(n), NULL, "_services._dns-sd._udp", domain)) < 0) {
103 avahi_server_set_errno(server, r);
107 if (!(b = avahi_new(AvahiSServiceTypeBrowser, 1))) {
108 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
113 b->callback = callback;
114 b->userdata = userdata;
115 b->record_browser = NULL;
117 AVAHI_LLIST_PREPEND(AvahiSServiceTypeBrowser, browser, server->service_type_browsers, b);
119 if (!(b->domain_name = avahi_normalize_name_strdup(domain))) {
120 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
124 if (!(k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR))) {
125 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
129 if (!(b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, b)))
140 avahi_s_service_type_browser_free(b);
145 void avahi_s_service_type_browser_free(AvahiSServiceTypeBrowser *b) {
148 AVAHI_LLIST_REMOVE(AvahiSServiceTypeBrowser, browser, b->server->service_type_browsers, b);
150 if (b->record_browser)
151 avahi_s_record_browser_free(b->record_browser);
153 avahi_free(b->domain_name);