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