]> git.meshlink.io Git - catta/blob - avahi-core/browse-service-type.c
* add proper error codes and patch everything to make use of it
[catta] / avahi-core / browse-service-type.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27
28 #include "browse.h"
29 #include "util.h"
30 #include "log.h"
31
32 struct AvahiServiceTypeBrowser {
33     AvahiServer *server;
34     gchar *domain_name;
35     
36     AvahiRecordBrowser *record_browser;
37
38     AvahiServiceTypeBrowserCallback callback;
39     gpointer userdata;
40
41     AVAHI_LLIST_FIELDS(AvahiServiceTypeBrowser, browser);
42 };
43
44 static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
45     AvahiServiceTypeBrowser *b = userdata;
46     gchar *n, *e, *c;
47
48     g_assert(rr);
49     g_assert(record);
50     g_assert(b);
51
52     g_assert(record->key->type == AVAHI_DNS_TYPE_PTR);
53
54     n = avahi_normalize_name(record->data.ptr.name);
55
56     if (*n != '_')
57         goto fail;
58     
59     for (c = e = n; *c == '_';) {
60         c += strcspn(c, ".");
61
62         if (*c == 0)
63             goto fail;
64
65         g_assert(*c == '.');
66         e = c;
67         c++;
68     }
69
70     *e = 0;
71
72     if (!avahi_domain_equal(c, b->domain_name))
73         goto fail;
74     
75     b->callback(b, interface, protocol, event, n, c, b->userdata);
76     g_free(n);
77
78     return;
79
80 fail:
81     avahi_log_warn("Invalid service type '%s'", n);
82     g_free(n);
83 }
84
85 AvahiServiceTypeBrowser *avahi_service_type_browser_new(AvahiServer *server, gint interface, AvahiProtocol protocol, const gchar *domain, AvahiServiceTypeBrowserCallback callback, gpointer userdata) {
86     AvahiServiceTypeBrowser *b;
87     AvahiKey *k;
88     gchar *n = NULL;
89     
90     g_assert(server);
91     g_assert(callback);
92
93     if (domain && !avahi_valid_domain_name(domain)) {
94         avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME);
95         return NULL;
96     }
97
98     b = g_new(AvahiServiceTypeBrowser, 1);
99     b->server = server;
100     b->domain_name = avahi_normalize_name(domain ? domain : "local");
101     b->callback = callback;
102     b->userdata = userdata;
103
104     AVAHI_LLIST_PREPEND(AvahiServiceTypeBrowser, browser, server->service_type_browsers, b);
105
106     n = g_strdup_printf("_services._dns-sd._udp.%s", b->domain_name);
107     k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
108     g_free(n);
109     
110     b->record_browser = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, b);
111     avahi_key_unref(k);
112
113     if (!b->record_browser)
114         return NULL;
115     
116     return b;
117 }
118
119 void avahi_service_type_browser_free(AvahiServiceTypeBrowser *b) {
120     g_assert(b);
121
122     AVAHI_LLIST_REMOVE(AvahiServiceTypeBrowser, browser, b->server->service_type_browsers, b);
123
124     if (b->record_browser)
125         avahi_record_browser_free(b->record_browser);
126     
127     g_free(b->domain_name);
128     g_free(b);
129 }
130
131