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