]> git.meshlink.io Git - catta/blob - src/browse-dns-server.c
add ioctl as a wrapper around ioctlsocket
[catta] / src / browse-dns-server.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 <string.h>
25
26 #include <catta/domain.h>
27 #include <catta/malloc.h>
28 #include <catta/error.h>
29
30 #include "browse.h"
31 #include <catta/log.h>
32 #include <catta/rr.h>
33
34 typedef struct CattaDNSServerInfo CattaDNSServerInfo;
35
36 struct CattaDNSServerInfo {
37     CattaSDNSServerBrowser *browser;
38
39     CattaIfIndex iface;
40     CattaProtocol protocol;
41     CattaRecord *srv_record;
42     CattaSHostNameResolver *host_name_resolver;
43     CattaAddress address;
44     CattaLookupResultFlags flags;
45
46     CATTA_LLIST_FIELDS(CattaDNSServerInfo, info);
47 };
48
49 struct CattaSDNSServerBrowser {
50     CattaServer *server;
51
52     CattaSRecordBrowser *record_browser;
53     CattaSDNSServerBrowserCallback callback;
54     void* userdata;
55     CattaProtocol aprotocol;
56     CattaLookupFlags user_flags;
57
58     unsigned n_info;
59
60     CATTA_LLIST_FIELDS(CattaSDNSServerBrowser, browser);
61     CATTA_LLIST_HEAD(CattaDNSServerInfo, info);
62 };
63
64 static CattaDNSServerInfo* get_server_info(CattaSDNSServerBrowser *b, CattaIfIndex iface, CattaProtocol protocol, CattaRecord *r) {
65     CattaDNSServerInfo *i;
66
67     assert(b);
68     assert(r);
69
70     for (i = b->info; i; i = i->info_next)
71         if (i->iface == iface &&
72             i->protocol == protocol &&
73             catta_record_equal_no_ttl(r, i->srv_record))
74             return i;
75
76     return NULL;
77 }
78
79 static void server_info_free(CattaSDNSServerBrowser *b, CattaDNSServerInfo *i) {
80     assert(b);
81     assert(i);
82
83     catta_record_unref(i->srv_record);
84     if (i->host_name_resolver)
85         catta_s_host_name_resolver_free(i->host_name_resolver);
86
87     CATTA_LLIST_REMOVE(CattaDNSServerInfo, info, b->info, i);
88
89     assert(b->n_info >= 1);
90     b->n_info--;
91
92     catta_free(i);
93 }
94
95 static void host_name_resolver_callback(
96     CattaSHostNameResolver *r,
97     CATTA_GCC_UNUSED CattaIfIndex iface,
98     CATTA_GCC_UNUSED CattaProtocol protocol,
99     CattaResolverEvent event,
100     const char *host_name,
101     const CattaAddress *a,
102     CattaLookupResultFlags flags,
103     void* userdata) {
104
105     CattaDNSServerInfo *i = userdata;
106
107     assert(r);
108     assert(host_name);
109     assert(i);
110
111     switch (event) {
112         case CATTA_RESOLVER_FOUND: {
113             i->address = *a;
114
115             i->browser->callback(
116                 i->browser,
117                 i->iface,
118                 i->protocol,
119                 CATTA_BROWSER_NEW,
120                 i->srv_record->data.srv.name,
121                 &i->address,
122                 i->srv_record->data.srv.port,
123                 i->flags | flags,
124                 i->browser->userdata);
125
126             break;
127         }
128
129         case CATTA_RESOLVER_FAILURE:
130             /* Ignore */
131             break;
132     }
133
134     catta_s_host_name_resolver_free(i->host_name_resolver);
135     i->host_name_resolver = NULL;
136 }
137
138 static void record_browser_callback(
139     CattaSRecordBrowser*rr,
140     CattaIfIndex iface,
141     CattaProtocol protocol,
142     CattaBrowserEvent event,
143     CattaRecord *record,
144     CattaLookupResultFlags flags,
145     void* userdata) {
146
147     CattaSDNSServerBrowser *b = userdata;
148
149     assert(rr);
150     assert(b);
151
152     /* Filter flags */
153     flags &= CATTA_LOOKUP_RESULT_CACHED | CATTA_LOOKUP_RESULT_MULTICAST | CATTA_LOOKUP_RESULT_WIDE_AREA;
154
155     switch (event) {
156         case CATTA_BROWSER_NEW: {
157             CattaDNSServerInfo *i;
158
159             assert(record);
160             assert(record->key->type == CATTA_DNS_TYPE_SRV);
161
162             if (get_server_info(b, iface, protocol, record))
163                 return;
164
165             if (b->n_info >= 10)
166                 return;
167
168             if (!(i = catta_new(CattaDNSServerInfo, 1)))
169                 return; /* OOM */
170
171             i->browser = b;
172             i->iface = iface;
173             i->protocol = protocol;
174             i->srv_record = catta_record_ref(record);
175             i->host_name_resolver = catta_s_host_name_resolver_new(
176                 b->server,
177                 iface, 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             CATTA_LLIST_PREPEND(CattaDNSServerInfo, info, b->info, i);
185
186             b->n_info++;
187             break;
188         }
189
190         case CATTA_BROWSER_REMOVE: {
191             CattaDNSServerInfo *i;
192
193             assert(record);
194             assert(record->key->type == CATTA_DNS_TYPE_SRV);
195
196             if (!(i = get_server_info(b, iface, protocol, record)))
197                 return;
198
199             if (!i->host_name_resolver)
200                 b->callback(
201                     b,
202                     iface,
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 CATTA_BROWSER_FAILURE:
216         case CATTA_BROWSER_ALL_FOR_NOW:
217         case CATTA_BROWSER_CACHE_EXHAUSTED:
218
219             b->callback(
220                 b,
221                 iface,
222                 protocol,
223                 event,
224                 NULL,
225                 NULL,
226                 0,
227                 flags,
228                 b->userdata);
229
230             break;
231     }
232 }
233
234 CattaSDNSServerBrowser *catta_s_dns_server_browser_new(
235     CattaServer *server,
236     CattaIfIndex iface,
237     CattaProtocol protocol,
238     const char *domain,
239     CattaDNSServerType type,
240     CattaProtocol aprotocol,
241     CattaLookupFlags flags,
242     CattaSDNSServerBrowserCallback callback,
243     void* userdata) {
244
245     static const char * const type_table[CATTA_DNS_SERVER_MAX] = {
246         "_domain._udp",
247         "_dns-update._udp"
248     };
249
250     CattaSDNSServerBrowser *b;
251     CattaKey *k = NULL;
252     char n[CATTA_DOMAIN_NAME_MAX];
253     int r;
254
255     assert(server);
256     assert(callback);
257
258     CATTA_CHECK_VALIDITY_RETURN_NULL(server, CATTA_IF_VALID(iface), CATTA_ERR_INVALID_INTERFACE);
259     CATTA_CHECK_VALIDITY_RETURN_NULL(server, CATTA_PROTO_VALID(protocol), CATTA_ERR_INVALID_PROTOCOL);
260     CATTA_CHECK_VALIDITY_RETURN_NULL(server, CATTA_PROTO_VALID(aprotocol), CATTA_ERR_INVALID_PROTOCOL);
261     CATTA_CHECK_VALIDITY_RETURN_NULL(server, !domain || catta_is_valid_domain_name(domain), CATTA_ERR_INVALID_DOMAIN_NAME);
262     CATTA_CHECK_VALIDITY_RETURN_NULL(server, CATTA_FLAGS_VALID(flags, CATTA_LOOKUP_USE_WIDE_AREA|CATTA_LOOKUP_USE_MULTICAST), CATTA_ERR_INVALID_FLAGS);
263     CATTA_CHECK_VALIDITY_RETURN_NULL(server, type < CATTA_DNS_SERVER_MAX, CATTA_ERR_INVALID_FLAGS);
264
265     if (!domain)
266         domain = server->domain_name;
267
268     if ((r = catta_service_name_join(n, sizeof(n), NULL, type_table[type], domain)) < 0) {
269         catta_server_set_errno(server, r);
270         return NULL;
271     }
272
273     if (!(b = catta_new(CattaSDNSServerBrowser, 1))) {
274         catta_server_set_errno(server, CATTA_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     CATTA_LLIST_HEAD_INIT(CattaDNSServerInfo, b->info);
286     CATTA_LLIST_PREPEND(CattaSDNSServerBrowser, browser, server->dns_server_browsers, b);
287
288     if (!(k = catta_key_new(n, CATTA_DNS_CLASS_IN, CATTA_DNS_TYPE_SRV))) {
289         catta_server_set_errno(server, CATTA_ERR_NO_MEMORY);
290         goto fail;
291     }
292
293     if (!(b->record_browser = catta_s_record_browser_new(server, iface, protocol, k, flags, record_browser_callback, b)))
294         goto fail;
295
296     catta_key_unref(k);
297
298     return b;
299
300 fail:
301
302     if (k)
303         catta_key_unref(k);
304
305     catta_s_dns_server_browser_free(b);
306     return NULL;
307 }
308
309 void catta_s_dns_server_browser_free(CattaSDNSServerBrowser *b) {
310     assert(b);
311
312     while (b->info)
313         server_info_free(b, b->info);
314
315     CATTA_LLIST_REMOVE(CattaSDNSServerBrowser, browser, b->server->dns_server_browsers, b);
316
317     if (b->record_browser)
318         catta_s_record_browser_free(b->record_browser);
319
320     catta_free(b);
321 }
322