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