]> git.meshlink.io Git - catta/blob - avahi-core/browse-dns-server.c
* drop AVAHI_RESOLVER_TIMEOUT, AVAHI_RESOLVER_NOT_FOUND and AVAHI_BROWSER_NOT_FOUND...
[catta] / avahi-core / browse-dns-server.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 #include "rr.h"
35
36 typedef struct AvahiDNSServerInfo AvahiDNSServerInfo;
37
38 struct AvahiDNSServerInfo {
39     AvahiSDNSServerBrowser *browser;
40
41     AvahiIfIndex interface;
42     AvahiProtocol protocol;
43     AvahiRecord *srv_record;
44     AvahiSHostNameResolver *host_name_resolver;
45     AvahiAddress address;
46     AvahiLookupResultFlags flags;
47     
48     AVAHI_LLIST_FIELDS(AvahiDNSServerInfo, info);
49 };
50
51 struct AvahiSDNSServerBrowser {
52     AvahiServer *server;
53     
54     AvahiSRecordBrowser *record_browser;
55     AvahiSDNSServerBrowserCallback callback;
56     void* userdata;
57     AvahiProtocol aprotocol;
58     AvahiLookupFlags user_flags;
59
60     unsigned n_info;
61     
62     AVAHI_LLIST_FIELDS(AvahiSDNSServerBrowser, browser);
63     AVAHI_LLIST_HEAD(AvahiDNSServerInfo, info);
64 };
65
66 static AvahiDNSServerInfo* get_server_info(AvahiSDNSServerBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r) {
67     AvahiDNSServerInfo *i;
68     
69     assert(b);
70     assert(r);
71
72     for (i = b->info; i; i = i->info_next)
73         if (i->interface == interface &&
74             i->protocol == protocol &&
75             avahi_record_equal_no_ttl(r, i->srv_record))
76             return i;
77
78     return NULL;
79 }
80
81 static void server_info_free(AvahiSDNSServerBrowser *b, AvahiDNSServerInfo *i) {
82     assert(b);
83     assert(i);
84
85     avahi_record_unref(i->srv_record);
86     if (i->host_name_resolver)
87         avahi_s_host_name_resolver_free(i->host_name_resolver);
88     
89     AVAHI_LLIST_REMOVE(AvahiDNSServerInfo, info, b->info, i);
90
91     assert(b->n_info >= 1);
92     b->n_info--;
93     
94     avahi_free(i);
95 }
96
97 static void host_name_resolver_callback(
98     AvahiSHostNameResolver *r,
99     AvahiIfIndex interface,
100     AvahiProtocol protocol,
101     AvahiResolverEvent event,
102     const char *host_name,
103     const AvahiAddress *a,
104     AvahiLookupResultFlags flags,
105     void* userdata) {
106     
107     AvahiDNSServerInfo *i = userdata;
108     
109     assert(r);
110     assert(host_name);
111     assert(i);
112
113     switch (event) {
114         case AVAHI_RESOLVER_FOUND: {
115             i->address = *a;
116             
117             i->browser->callback(
118                 i->browser,
119                 i->interface,
120                 i->protocol,
121                 AVAHI_BROWSER_NEW,
122                 i->srv_record->data.srv.name,
123                 &i->address,
124                 i->srv_record->data.srv.port,
125                 i->flags | flags,
126                 i->browser->userdata);
127
128             break;
129         }
130
131         case AVAHI_RESOLVER_FAILURE:
132             /* Ignore */
133             break;
134     }
135
136     avahi_s_host_name_resolver_free(i->host_name_resolver);
137     i->host_name_resolver = NULL;
138 }
139
140 static void record_browser_callback(
141     AvahiSRecordBrowser*rr,
142     AvahiIfIndex interface,
143     AvahiProtocol protocol,
144     AvahiBrowserEvent event,
145     AvahiRecord *record,
146     AvahiLookupResultFlags flags,
147     void* userdata) {
148     
149     AvahiSDNSServerBrowser *b = userdata;
150
151     assert(rr);
152     assert(b);
153
154
155     switch (event) {
156         case AVAHI_BROWSER_NEW: {
157             AvahiDNSServerInfo *i;
158             
159             assert(record);
160             assert(record->key->type == AVAHI_DNS_TYPE_SRV);
161
162             if (get_server_info(b, interface, protocol, record))
163                 return;
164             
165             if (b->n_info >= 10)
166                 return;
167             
168             if (!(i = avahi_new(AvahiDNSServerInfo, 1)))
169                 return; /* OOM */
170             
171             i->browser = b;
172             i->interface = interface;
173             i->protocol = protocol;
174             i->srv_record = avahi_record_ref(record);
175             i->host_name_resolver = avahi_s_host_name_resolver_new(
176                 b->server,
177                 interface, protocol,
178                 record->data.srv.name,
179                 b->aprotocol,
180                 b->user_flags,
181                 host_name_resolver_callback, i);
182             i->flags = flags;
183             
184             AVAHI_LLIST_PREPEND(AvahiDNSServerInfo, info, b->info, i);
185             
186             b->n_info++;
187             break;
188         }
189
190         case AVAHI_BROWSER_REMOVE: {
191             AvahiDNSServerInfo *i;
192             
193             assert(record);
194             assert(record->key->type == AVAHI_DNS_TYPE_SRV);
195
196             if (!(i = get_server_info(b, interface, protocol, record)))
197                 return;
198             
199             if (!i->host_name_resolver)
200                 b->callback(
201                     b,
202                     interface,
203                     protocol,
204                     event,
205                     i->srv_record->data.srv.name,
206                     &i->address,
207                     i->srv_record->data.srv.port,
208                     i->flags | flags,
209                     b->userdata);
210             
211             server_info_free(b, i);
212             break;
213         }
214
215         case AVAHI_BROWSER_FAILURE:
216         case AVAHI_BROWSER_ALL_FOR_NOW:
217         case AVAHI_BROWSER_CACHE_EXHAUSTED:
218
219             b->callback(
220                 b,
221                 interface,
222                 protocol,
223                 event,
224                 NULL,
225                 NULL,
226                 0,
227                 flags,
228                 b->userdata);
229             
230             break;
231     }
232 }
233
234 AvahiSDNSServerBrowser *avahi_s_dns_server_browser_new(
235     AvahiServer *server,
236     AvahiIfIndex interface,
237     AvahiProtocol protocol,
238     const char *domain,
239     AvahiDNSServerType type,
240     AvahiProtocol aprotocol,
241     AvahiLookupFlags flags,
242     AvahiSDNSServerBrowserCallback callback,
243     void* userdata) {
244
245     static const char * const type_table[AVAHI_DNS_SERVER_MAX] = {
246         "_domain._udp",
247         "_dns-update._udp"
248     };
249     
250     AvahiSDNSServerBrowser *b;
251     AvahiKey *k = NULL;
252     char n[AVAHI_DOMAIN_NAME_MAX];
253     int r;
254     
255     assert(server);
256     assert(callback);
257
258     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
259     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
260     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(aprotocol), AVAHI_ERR_INVALID_PROTOCOL);
261     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
262     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
263     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, type < AVAHI_DNS_SERVER_MAX, AVAHI_ERR_INVALID_FLAGS);
264
265     if (!domain)
266         domain = server->domain_name;
267
268     if ((r = avahi_service_name_join(n, sizeof(n), NULL, type_table[type], domain)) < 0) {
269         avahi_server_set_errno(server, r);
270         return NULL;
271     }
272     
273     if (!(b = avahi_new(AvahiSDNSServerBrowser, 1))) {
274         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
275         return NULL;
276     }
277     
278     b->server = server;
279     b->callback = callback;
280     b->userdata = userdata;
281     b->aprotocol = aprotocol;
282     b->n_info = 0;
283     b->user_flags = flags;
284
285     AVAHI_LLIST_HEAD_INIT(AvahiDNSServerInfo, b->info);
286     AVAHI_LLIST_PREPEND(AvahiSDNSServerBrowser, browser, server->dns_server_browsers, b);
287     
288     if (!(k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV))) {
289         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
290         goto fail;
291     }
292     
293     if (!(b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, b)))
294         goto fail;
295         
296     avahi_key_unref(k);
297
298     return b;
299
300 fail:
301
302     if (k)
303         avahi_key_unref(k);
304     
305     avahi_s_dns_server_browser_free(b);
306     return NULL;
307 }
308
309 void avahi_s_dns_server_browser_free(AvahiSDNSServerBrowser *b) {
310     assert(b);
311
312     while (b->info)
313         server_info_free(b, b->info);
314     
315     AVAHI_LLIST_REMOVE(AvahiSDNSServerBrowser, browser, b->server->dns_server_browsers, b);
316
317     if (b->record_browser)
318         avahi_s_record_browser_free(b->record_browser);
319
320     avahi_free(b);
321 }
322