]> git.meshlink.io Git - catta/blob - avahi-core/browse-service.c
* replace guchar and gint by AvahiProtocol, AvahiIfIndex at many places where it...
[catta] / avahi-core / browse-service.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 AvahiServiceBrowser {
33     AvahiServer *server;
34     gchar *domain_name;
35     gchar *service_type;
36     
37     AvahiRecordBrowser *record_browser;
38
39     AvahiServiceBrowserCallback callback;
40     gpointer userdata;
41
42     AVAHI_LLIST_FIELDS(AvahiServiceBrowser, browser);
43 };
44
45 static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
46     AvahiServiceBrowser *b = userdata;
47     gchar *n, *e, *c, *s;
48     gchar service[128];
49
50     g_assert(rr);
51     g_assert(record);
52     g_assert(b);
53
54     g_assert(record->key->type == AVAHI_DNS_TYPE_PTR);
55
56     c = n = avahi_normalize_name(record->data.ptr.name);
57
58     if (!(avahi_unescape_label((const gchar**) &c, service, sizeof(service))))
59         goto fail;
60
61     for (s = e = c; *c == '_';) {
62         c += strcspn(c, ".");
63
64         if (*c == 0)
65             goto fail;
66
67         g_assert(*c == '.');
68         e = c;
69         c++;
70     }
71
72     *e = 0;
73
74     if (!avahi_domain_equal(c, b->domain_name))
75         goto fail;
76     
77     b->callback(b, interface, protocol, event, service, s, c, b->userdata);
78     g_free(n);
79
80     return;
81
82 fail:
83     avahi_log_warn("Invalid service '%s'", n);
84     g_free(n);
85 }
86
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;
89     AvahiKey *k;
90     gchar *n = NULL;
91     
92     g_assert(server);
93     g_assert(callback);
94     g_assert(service_type);
95
96     b = g_new(AvahiServiceBrowser, 1);
97     b->server = server;
98     b->domain_name = avahi_normalize_name(domain ? domain : "local");
99     b->service_type = avahi_normalize_name(service_type);
100     b->callback = callback;
101     b->userdata = userdata;
102
103     n = g_strdup_printf("%s.%s", b->service_type, b->domain_name);
104     k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
105     g_free(n);
106     
107     b->record_browser = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, b);
108     avahi_key_unref(k);
109
110     AVAHI_LLIST_PREPEND(AvahiServiceBrowser, browser, server->service_browsers, b);
111     
112     return b;
113 }
114
115 void avahi_service_browser_free(AvahiServiceBrowser *b) {
116     g_assert(b);
117
118     AVAHI_LLIST_REMOVE(AvahiServiceBrowser, browser, b->server->service_browsers, b);
119
120     avahi_record_browser_free(b->record_browser);
121     g_free(b->domain_name);
122     g_free(b->service_type);
123     g_free(b);
124 }