4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <avahi-common/domain.h>
32 struct AvahiServiceBrowser {
37 AvahiRecordBrowser *record_browser;
39 AvahiServiceBrowserCallback callback;
42 AVAHI_LLIST_FIELDS(AvahiServiceBrowser, browser);
45 static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
46 AvahiServiceBrowser *b = userdata;
54 g_assert(record->key->type == AVAHI_DNS_TYPE_PTR);
56 c = n = avahi_normalize_name(record->data.ptr.name);
58 if (!(avahi_unescape_label((const gchar**) &c, service, sizeof(service))))
61 for (s = e = c; *c == '_';) {
74 if (!avahi_domain_equal(c, b->domain_name))
77 b->callback(b, interface, protocol, event, service, s, c, b->userdata);
83 avahi_log_warn("Invalid service '%s'", n);
87 AvahiServiceBrowser *avahi_service_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const gchar *service_type, const gchar *domain, AvahiServiceBrowserCallback callback, gpointer userdata) {
88 AvahiServiceBrowser *b;
94 g_assert(service_type);
96 if (!avahi_valid_service_type(service_type)) {
97 avahi_server_set_errno(server, AVAHI_ERR_INVALID_SERVICE_TYPE);
101 if (domain && !avahi_valid_domain_name(domain)) {
102 avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME);
106 b = g_new(AvahiServiceBrowser, 1);
108 b->domain_name = avahi_normalize_name(domain ? domain : "local");
109 b->service_type = avahi_normalize_name(service_type);
110 b->callback = callback;
111 b->userdata = userdata;
112 AVAHI_LLIST_PREPEND(AvahiServiceBrowser, browser, server->service_browsers, b);
114 n = g_strdup_printf("%s.%s", b->service_type, b->domain_name);
115 k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
118 b->record_browser = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, b);
122 if (!b->record_browser) {
123 avahi_service_browser_free(b);
130 void avahi_service_browser_free(AvahiServiceBrowser *b) {
133 AVAHI_LLIST_REMOVE(AvahiServiceBrowser, browser, b->server->service_browsers, b);
135 if (b->record_browser)
136 avahi_record_browser_free(b->record_browser);
138 g_free(b->domain_name);
139 g_free(b->service_type);