]> git.meshlink.io Git - catta/blob - src/resolve-address.c
rename everything avahi to catta
[catta] / src / resolve-address.c
1 /***
2   This file is part of catta.
3
4   catta is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8
9   catta is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with catta; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25
26 #include <catta/timeval.h>
27 #include <catta/malloc.h>
28 #include <catta/error.h>
29 #include <catta/domain.h>
30
31 #include "browse.h"
32
33 #define TIMEOUT_MSEC 5000
34
35 struct CattaSAddressResolver {
36     CattaServer *server;
37     CattaAddress address;
38
39     CattaSRecordBrowser *record_browser;
40
41     CattaSAddressResolverCallback callback;
42     void* userdata;
43
44     CattaRecord *ptr_record;
45     CattaIfIndex interface;
46     CattaProtocol protocol;
47     CattaLookupResultFlags flags;
48
49     int retry_with_multicast;
50     CattaKey *key;
51
52     CattaTimeEvent *time_event;
53
54     CATTA_LLIST_FIELDS(CattaSAddressResolver, resolver);
55 };
56
57 static void finish(CattaSAddressResolver *r, CattaResolverEvent event) {
58     assert(r);
59
60     if (r->time_event) {
61         catta_time_event_free(r->time_event);
62         r->time_event = NULL;
63     }
64
65     switch (event) {
66         case CATTA_RESOLVER_FAILURE:
67             r->callback(r, r->interface, r->protocol, event, &r->address, NULL, r->flags, r->userdata);
68             break;
69
70         case CATTA_RESOLVER_FOUND:
71             assert(r->ptr_record);
72             r->callback(r, r->interface, r->protocol, event, &r->address, r->ptr_record->data.ptr.name, r->flags, r->userdata);
73             break;
74     }
75 }
76
77 static void time_event_callback(CattaTimeEvent *e, void *userdata) {
78     CattaSAddressResolver *r = userdata;
79
80     assert(e);
81     assert(r);
82
83     catta_server_set_errno(r->server, CATTA_ERR_TIMEOUT);
84     finish(r, CATTA_RESOLVER_FAILURE);
85 }
86
87 static void start_timeout(CattaSAddressResolver *r) {
88     struct timeval tv;
89     assert(r);
90
91     if (r->time_event)
92         return;
93
94     catta_elapse_time(&tv, TIMEOUT_MSEC, 0);
95     r->time_event = catta_time_event_new(r->server->time_event_queue, &tv, time_event_callback, r);
96 }
97
98 static void record_browser_callback(
99     CattaSRecordBrowser*rr,
100     CattaIfIndex interface,
101     CattaProtocol protocol,
102     CattaBrowserEvent event,
103     CattaRecord *record,
104     CattaLookupResultFlags flags,
105     void* userdata) {
106
107     CattaSAddressResolver *r = userdata;
108
109     assert(rr);
110     assert(r);
111
112     switch (event) {
113         case CATTA_BROWSER_NEW:
114             assert(record);
115             assert(record->key->type == CATTA_DNS_TYPE_PTR);
116
117             if (r->interface > 0 && interface != r->interface)
118                 return;
119
120             if (r->protocol != CATTA_PROTO_UNSPEC && protocol != r->protocol)
121                 return;
122
123             if (r->interface <= 0)
124                 r->interface = interface;
125
126             if (r->protocol == CATTA_PROTO_UNSPEC)
127                 r->protocol = protocol;
128
129             if (!r->ptr_record) {
130                 r->ptr_record = catta_record_ref(record);
131                 r->flags = flags;
132
133                 finish(r, CATTA_RESOLVER_FOUND);
134             }
135             break;
136
137         case CATTA_BROWSER_REMOVE:
138             assert(record);
139             assert(record->key->type == CATTA_DNS_TYPE_PTR);
140
141             if (r->ptr_record && catta_record_equal_no_ttl(record, r->ptr_record)) {
142                 catta_record_unref(r->ptr_record);
143                 r->ptr_record = NULL;
144                 r->flags = flags;
145
146                 /** Look for a replacement */
147                 catta_s_record_browser_restart(r->record_browser);
148                 start_timeout(r);
149             }
150
151             break;
152
153         case CATTA_BROWSER_CACHE_EXHAUSTED:
154         case CATTA_BROWSER_ALL_FOR_NOW:
155             break;
156
157         case CATTA_BROWSER_FAILURE:
158
159             if (r->retry_with_multicast) {
160                 r->retry_with_multicast = 0;
161
162                 catta_s_record_browser_free(r->record_browser);
163                 r->record_browser = catta_s_record_browser_new(r->server, r->interface, r->protocol, r->key, CATTA_LOOKUP_USE_MULTICAST, record_browser_callback, r);
164
165                 if (r->record_browser) {
166                     start_timeout(r);
167                     break;
168                 }
169             }
170
171             r->flags = flags;
172             finish(r, CATTA_RESOLVER_FAILURE);
173             break;
174     }
175 }
176
177 CattaSAddressResolver *catta_s_address_resolver_new(
178     CattaServer *server,
179     CattaIfIndex interface,
180     CattaProtocol protocol,
181     const CattaAddress *address,
182     CattaLookupFlags flags,
183     CattaSAddressResolverCallback callback,
184     void* userdata) {
185
186     CattaSAddressResolver *r;
187     CattaKey *k;
188     char n[CATTA_DOMAIN_NAME_MAX];
189
190     assert(server);
191     assert(address);
192     assert(callback);
193
194     CATTA_CHECK_VALIDITY_RETURN_NULL(server, CATTA_IF_VALID(interface), CATTA_ERR_INVALID_INTERFACE);
195     CATTA_CHECK_VALIDITY_RETURN_NULL(server, CATTA_PROTO_VALID(protocol), CATTA_ERR_INVALID_PROTOCOL);
196     CATTA_CHECK_VALIDITY_RETURN_NULL(server, address->proto == CATTA_PROTO_INET || address->proto == CATTA_PROTO_INET6, CATTA_ERR_INVALID_PROTOCOL);
197     CATTA_CHECK_VALIDITY_RETURN_NULL(server, CATTA_FLAGS_VALID(flags, CATTA_LOOKUP_USE_WIDE_AREA|CATTA_LOOKUP_USE_MULTICAST), CATTA_ERR_INVALID_FLAGS);
198
199     catta_reverse_lookup_name(address, n, sizeof(n));
200
201     if (!(k = catta_key_new(n, CATTA_DNS_CLASS_IN, CATTA_DNS_TYPE_PTR))) {
202         catta_server_set_errno(server, CATTA_ERR_NO_MEMORY);
203         return NULL;
204     }
205
206     if (!(r = catta_new(CattaSAddressResolver, 1))) {
207         catta_server_set_errno(server, CATTA_ERR_NO_MEMORY);
208         catta_key_unref(k);
209         return NULL;
210     }
211
212     r->server = server;
213     r->address = *address;
214     r->callback = callback;
215     r->userdata = userdata;
216     r->ptr_record = NULL;
217     r->interface = interface;
218     r->protocol = protocol;
219     r->flags = 0;
220     r->retry_with_multicast = 0;
221     r->key = k;
222
223     r->record_browser = NULL;
224     CATTA_LLIST_PREPEND(CattaSAddressResolver, resolver, server->address_resolvers, r);
225
226     r->time_event = NULL;
227
228     if (!(flags & (CATTA_LOOKUP_USE_MULTICAST|CATTA_LOOKUP_USE_WIDE_AREA))) {
229
230         if (!server->wide_area_lookup_engine || !catta_wide_area_has_servers(server->wide_area_lookup_engine))
231             flags |= CATTA_LOOKUP_USE_MULTICAST;
232         else {
233             flags |= CATTA_LOOKUP_USE_WIDE_AREA;
234             r->retry_with_multicast = 1;
235         }
236     }
237
238     r->record_browser = catta_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, r);
239
240     if (!r->record_browser) {
241         catta_s_address_resolver_free(r);
242         return NULL;
243     }
244
245     start_timeout(r);
246
247     return r;
248 }
249
250 void catta_s_address_resolver_free(CattaSAddressResolver *r) {
251     assert(r);
252
253     CATTA_LLIST_REMOVE(CattaSAddressResolver, resolver, r->server->address_resolvers, r);
254
255     if (r->record_browser)
256         catta_s_record_browser_free(r->record_browser);
257
258     if (r->time_event)
259         catta_time_event_free(r->time_event);
260
261     if (r->ptr_record)
262         catta_record_unref(r->ptr_record);
263
264     if (r->key)
265         catta_key_unref(r->key);
266
267     catta_free(r);
268 }