]> git.meshlink.io Git - catta/blob - avahi-core/resolve-host-name.c
* make all flags parameters UINT32 when marshalling for DBUS
[catta] / avahi-core / resolve-host-name.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 <stdlib.h>
27
28 #include <avahi-common/domain.h>
29 #include <avahi-common/timeval.h>
30 #include <avahi-common/malloc.h>
31 #include <avahi-common/error.h>
32
33 #include "browse.h"
34 #include "log.h"
35
36 #define TIMEOUT_MSEC 5000
37
38 struct AvahiSHostNameResolver {
39     AvahiServer *server;
40     char *host_name;
41     
42     AvahiSRecordBrowser *record_browser_a;
43     AvahiSRecordBrowser *record_browser_aaaa;
44
45     AvahiSHostNameResolverCallback callback;
46     void* userdata;
47
48     AvahiRecord *address_record;
49     AvahiIfIndex interface;
50     AvahiProtocol protocol;
51     AvahiLookupResultFlags flags;
52
53     AvahiTimeEvent *time_event;
54
55     AVAHI_LLIST_FIELDS(AvahiSHostNameResolver, resolver);
56 };
57
58 static void finish(AvahiSHostNameResolver *r, AvahiResolverEvent event) {
59     assert(r);
60
61     if (r->time_event) {
62         avahi_time_event_free(r->time_event);
63         r->time_event = NULL;
64     }
65
66     switch (event) {
67         case AVAHI_RESOLVER_FOUND: {
68             AvahiAddress a;
69             
70             assert(r->address_record);
71             
72             switch (r->address_record->key->type) {
73                 case AVAHI_DNS_TYPE_A:
74                     a.proto = AVAHI_PROTO_INET;
75                     a.data.ipv4 = r->address_record->data.a.address;
76                     break;
77                     
78                 case AVAHI_DNS_TYPE_AAAA:
79                     a.proto = AVAHI_PROTO_INET6;
80                     a.data.ipv6 = r->address_record->data.aaaa.address;
81                     break;
82                     
83                 default:
84                     abort();
85             }
86
87             r->callback(r, r->interface, r->protocol, AVAHI_RESOLVER_FOUND, r->address_record->key->name, &a, r->flags, r->userdata);
88             break;
89
90         }
91             
92         case AVAHI_RESOLVER_TIMEOUT:
93         case AVAHI_RESOLVER_NOT_FOUND:
94         case AVAHI_RESOLVER_FAILURE:
95             
96             r->callback(r, r->interface, r->protocol, event, r->host_name, NULL, r->flags, r->userdata);
97             break;
98     }
99 }
100
101 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
102     AvahiSHostNameResolver *r = userdata;
103     
104     assert(e);
105     assert(r);
106
107     finish(r, AVAHI_RESOLVER_TIMEOUT);
108 }
109
110 static void start_timeout(AvahiSHostNameResolver *r) {
111     struct timeval tv;
112     assert(r);
113
114     if (r->time_event)
115         return;
116
117     avahi_elapse_time(&tv, TIMEOUT_MSEC, 0);
118
119     r->time_event = avahi_time_event_new(r->server->time_event_queue, &tv, time_event_callback, r);
120 }
121
122 static void record_browser_callback(
123     AvahiSRecordBrowser*rr,
124     AvahiIfIndex interface,
125     AvahiProtocol protocol,
126     AvahiBrowserEvent event,
127     AvahiRecord *record,
128     AvahiLookupResultFlags flags,
129     void* userdata) {
130     
131     AvahiSHostNameResolver *r = userdata;
132     
133     assert(rr);
134     assert(r);
135
136
137     switch (event) {
138         case AVAHI_BROWSER_NEW:
139             assert(record);
140             assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
141
142             if (r->interface > 0 && interface != r->interface)
143                 return;
144             
145             if (r->protocol != AVAHI_PROTO_UNSPEC && protocol != r->protocol)
146                 return;
147             
148             if (r->interface <= 0)
149                 r->interface = interface;
150             
151             if (r->protocol == AVAHI_PROTO_UNSPEC)
152                 r->protocol = protocol;
153             
154             if (!r->address_record) {
155                 r->address_record = avahi_record_ref(record);
156                 r->flags = flags;
157                 
158                 finish(r, AVAHI_RESOLVER_FOUND);
159             }
160
161             break;
162
163         case AVAHI_BROWSER_REMOVE:
164             assert(record);
165             assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
166
167             if (r->address_record && avahi_record_equal_no_ttl(record, r->address_record)) {
168                 avahi_record_unref(r->address_record);
169                 r->address_record = NULL;
170
171                 r->flags = flags;
172
173                 
174                 /** Look for a replacement */
175                 if (r->record_browser_aaaa)
176                     avahi_s_record_browser_restart(r->record_browser_aaaa);
177                 if (r->record_browser_a)
178                     avahi_s_record_browser_restart(r->record_browser_a);
179                 
180                 start_timeout(r);
181             }
182
183             break;
184
185         case AVAHI_BROWSER_CACHE_EXHAUSTED:
186         case AVAHI_BROWSER_ALL_FOR_NOW:
187             /* Ignore */
188             break;
189
190         case AVAHI_BROWSER_FAILURE:
191         case AVAHI_BROWSER_NOT_FOUND:
192
193             /* Stop browsers */
194             
195             if (r->record_browser_aaaa)
196                 avahi_s_record_browser_free(r->record_browser_aaaa);
197             if (r->record_browser_a)
198                 avahi_s_record_browser_free(r->record_browser_a);
199
200             r->record_browser_a = r->record_browser_aaaa = NULL;
201             r->flags = flags;
202             
203             finish(r, event == AVAHI_BROWSER_FAILURE ? AVAHI_RESOLVER_FAILURE : AVAHI_RESOLVER_NOT_FOUND);
204             break;
205     }
206 }
207
208 AvahiSHostNameResolver *avahi_s_host_name_resolver_new(
209     AvahiServer *server,
210     AvahiIfIndex interface,
211     AvahiProtocol protocol,
212     const char *host_name,
213     AvahiProtocol aprotocol,
214     AvahiLookupFlags flags,
215     AvahiSHostNameResolverCallback callback,
216     void* userdata) {
217     
218     AvahiSHostNameResolver *r;
219     AvahiKey *k;
220    
221     assert(server);
222     assert(host_name);
223     assert(callback);
224
225     assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
226
227     if (!AVAHI_IF_VALID(interface)) {
228         avahi_server_set_errno(server, AVAHI_ERR_INVALID_INTERFACE);
229         return NULL;
230     }
231
232     if (!avahi_is_valid_domain_name(host_name)) {
233         avahi_server_set_errno(server, AVAHI_ERR_INVALID_HOST_NAME);
234         return NULL;
235     }
236
237     if (!AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST)) {
238         avahi_server_set_errno(server, AVAHI_ERR_INVALID_FLAGS);
239         return NULL;
240     }
241
242     if (!(r = avahi_new(AvahiSHostNameResolver, 1))) {
243         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
244         return NULL;
245     }
246     
247     r->server = server;
248     r->host_name = avahi_normalize_name(host_name);
249     r->callback = callback;
250     r->userdata = userdata;
251     r->address_record = NULL;
252     r->interface = interface;
253     r->protocol = protocol;
254     r->flags = 0;
255
256     r->record_browser_a = r->record_browser_aaaa = NULL;
257
258     r->time_event = NULL;
259
260     AVAHI_LLIST_PREPEND(AvahiSHostNameResolver, resolver, server->host_name_resolvers, r);
261
262     r->record_browser_aaaa = r->record_browser_a = NULL;
263
264
265     if (aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_UNSPEC) {
266         k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
267         r->record_browser_a = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, r);
268         avahi_key_unref(k);
269
270         if (!r->record_browser_a)
271             goto fail;
272     } 
273
274     if (aprotocol == AVAHI_PROTO_INET6 || aprotocol == AVAHI_PROTO_UNSPEC) {
275         k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
276         r->record_browser_aaaa = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, r);
277         avahi_key_unref(k);
278
279         if (!r->record_browser_aaaa)
280             goto fail;
281     }
282
283     assert(r->record_browser_aaaa || r->record_browser_a);
284
285     start_timeout(r);
286     
287     return r;
288
289 fail:
290     avahi_s_host_name_resolver_free(r);
291     return NULL;
292 }
293
294 void avahi_s_host_name_resolver_free(AvahiSHostNameResolver *r) {
295     assert(r);
296
297     AVAHI_LLIST_REMOVE(AvahiSHostNameResolver, resolver, r->server->host_name_resolvers, r);
298
299     if (r->record_browser_a)
300         avahi_s_record_browser_free(r->record_browser_a);
301
302     if (r->record_browser_aaaa)
303         avahi_s_record_browser_free(r->record_browser_aaaa);
304
305     if (r->time_event)
306         avahi_time_event_free(r->time_event);
307
308     if (r->address_record)
309         avahi_record_unref(r->address_record);
310     
311     avahi_free(r->host_name);
312     avahi_free(r);
313 }