]> git.meshlink.io Git - catta/blob - avahi-core/browse-service-type.c
* Add avahi-client examples to doxygen
[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 <avahi-common/domain.h>
29 #include <avahi-common/malloc.h>
30 #include <avahi-common/error.h>
31
32 #include "browse.h"
33 #include "log.h"
34
35 struct AvahiSServiceTypeBrowser {
36     AvahiServer *server;
37     char *domain_name;
38     
39     AvahiSRecordBrowser *record_browser;
40
41     AvahiSServiceTypeBrowserCallback callback;
42     void* userdata;
43
44     AVAHI_LLIST_FIELDS(AvahiSServiceTypeBrowser, browser);
45 };
46
47 static void record_browser_callback(AvahiSRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
48     AvahiSServiceTypeBrowser *b = userdata;
49     char *n, *e, *c;
50
51     assert(rr);
52     assert(record);
53     assert(b);
54
55     assert(record->key->type == AVAHI_DNS_TYPE_PTR);
56
57     n = avahi_normalize_name(record->data.ptr.name);
58
59     if (*n != '_')
60         goto fail;
61     
62     for (c = e = n; *c == '_';) {
63         c += strcspn(c, ".");
64
65         if (*c == 0)
66             goto fail;
67
68         assert(*c == '.');
69         e = c;
70         c++;
71     }
72
73     *e = 0;
74
75     if (!avahi_domain_equal(c, b->domain_name))
76         goto fail;
77     
78     b->callback(b, interface, protocol, event, n, c, b->userdata);
79     avahi_free(n);
80
81     return;
82
83 fail:
84     avahi_log_warn("Invalid service type '%s'", n);
85     avahi_free(n);
86 }
87
88 AvahiSServiceTypeBrowser *avahi_s_service_type_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiSServiceTypeBrowserCallback callback, void* userdata) {
89     AvahiSServiceTypeBrowser *b;
90     AvahiKey *k;
91     char *n = NULL;
92     
93     assert(server);
94     assert(callback);
95
96     if (domain && !avahi_is_valid_domain_name(domain)) {
97         avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME);
98         return NULL;
99     }
100
101     if (!(b = avahi_new(AvahiSServiceTypeBrowser, 1))) {
102         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
103         return NULL;
104     }
105     
106     b->server = server;
107     b->domain_name = avahi_normalize_name(domain ? domain : "local");
108     b->callback = callback;
109     b->userdata = userdata;
110
111     AVAHI_LLIST_PREPEND(AvahiSServiceTypeBrowser, browser, server->service_type_browsers, b);
112
113     n = avahi_strdup_printf("_services._dns-sd._udp.%s", b->domain_name);
114     k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
115     avahi_free(n);
116     
117     b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, b);
118     avahi_key_unref(k);
119
120     if (!b->record_browser)
121         return NULL;
122     
123     return b;
124 }
125
126 void avahi_s_service_type_browser_free(AvahiSServiceTypeBrowser *b) {
127     assert(b);
128
129     AVAHI_LLIST_REMOVE(AvahiSServiceTypeBrowser, browser, b->server->service_type_browsers, b);
130
131     if (b->record_browser)
132         avahi_s_record_browser_free(b->record_browser);
133     
134     avahi_free(b->domain_name);
135     avahi_free(b);
136 }
137
138