]> git.meshlink.io Git - catta/blob - avahi-core/browse-service-type.c
* replace guchar and gint by AvahiProtocol, AvahiIfIndex at many places where 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     b = g_new(AvahiServiceTypeBrowser, 1);
94     b->server = server;
95     b->domain_name = avahi_normalize_name(domain ? domain : "local");
96     b->callback = callback;
97     b->userdata = userdata;
98
99
100     n = g_strdup_printf("_services._dns-sd._udp.%s", b->domain_name);
101     k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
102     g_free(n);
103     
104     b->record_browser = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, b);
105     avahi_key_unref(k);
106
107     AVAHI_LLIST_PREPEND(AvahiServiceTypeBrowser, browser, server->service_type_browsers, b);
108     
109     return b;
110 }
111
112 void avahi_service_type_browser_free(AvahiServiceTypeBrowser *b) {
113     g_assert(b);
114
115     AVAHI_LLIST_REMOVE(AvahiServiceTypeBrowser, browser, b->server->service_type_browsers, b);
116
117     avahi_record_browser_free(b->record_browser);
118     g_free(b->domain_name);
119     g_free(b);
120 }
121
122