libev migration - copy valus we use for loop control before we hand it off into the...
[citadel.git] / citadel / modules / c-ares-dns / serv_c-ares-dns.c
1 /*
2  * Copyright (c) 1998-2009 by the citadel.org team
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Inspired by NodeJS.org; thanks for the MX-Parser ;-)
19  */
20
21 #include "sysdep.h"
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <termios.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <pwd.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <syslog.h>
32
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
35 # include <time.h>
36 #else
37 # if HAVE_SYS_TIME_H
38 #  include <sys/time.h>
39 # else
40 #  include <time.h>
41 # endif
42 #endif
43 #include <sys/wait.h>
44 #include <ctype.h>
45 #include <string.h>
46 #include <limits.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50
51 #include <libcitadel.h>
52 #include "citadel.h"
53 #include "server.h"
54 #include "citserver.h"
55 #include "support.h"
56
57 #include "ctdl_module.h"
58 #include "event_client.h"
59
60
61 extern struct ev_loop *event_base;
62
63 void SockStateCb(void *data, int sock, int read, int write);
64
65
66 static void HostByAddrCb(void *data,
67                          int status,
68                          int timeouts,
69                          struct hostent *hostent) 
70 {
71         AsyncIO *IO = data;
72         IO->DNSStatus = status;
73         if  (status != ARES_SUCCESS) {
74 //              ResolveError(*cb, status);
75                 return;
76         }
77         IO->Data = hostent;
78 /// TODO: howto free this??
79 }
80
81 static void ParseAnswerA(AsyncIO *IO, unsigned char* abuf, int alen) 
82 {
83         struct hostent* host;
84
85         if (IO->VParsedDNSReply != NULL)
86                 IO->DNSReplyFree(IO->VParsedDNSReply);
87         IO->VParsedDNSReply = NULL;
88
89         IO->DNSStatus = ares_parse_a_reply(abuf, alen, &host, NULL, NULL);
90         if (IO->DNSStatus != ARES_SUCCESS) {
91 //    ResolveError(arg->js_cb, status);
92                 return;
93         }
94         IO->VParsedDNSReply = host;
95         IO->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
96 }
97
98
99 static void ParseAnswerAAAA(AsyncIO *IO, unsigned char* abuf, int alen) 
100 {
101         struct hostent* host;
102
103         if (IO->VParsedDNSReply != NULL)
104                 IO->DNSReplyFree(IO->VParsedDNSReply);
105         IO->VParsedDNSReply = NULL;
106
107         IO->DNSStatus = ares_parse_aaaa_reply(abuf, alen, &host, NULL, NULL);
108         if (IO->DNSStatus != ARES_SUCCESS) {
109 //    ResolveError(arg->js_cb, status);
110                 return;
111         }
112         IO->VParsedDNSReply = host;
113         IO->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
114 }
115
116
117 static void ParseAnswerCNAME(AsyncIO *IO, unsigned char* abuf, int alen) 
118 {
119         struct hostent* host;
120
121         if (IO->VParsedDNSReply != NULL)
122                 IO->DNSReplyFree(IO->VParsedDNSReply);
123         IO->VParsedDNSReply = NULL;
124
125         IO->DNSStatus = ares_parse_a_reply(abuf, alen, &host, NULL, NULL);
126         if (IO->DNSStatus != ARES_SUCCESS) {
127 //    ResolveError(arg->js_cb, status);
128                 return;
129         }
130
131         // a CNAME lookup always returns a single record but
132         IO->VParsedDNSReply = host;
133         IO->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
134 }
135
136
137 static void ParseAnswerMX(AsyncIO *IO, unsigned char* abuf, int alen) 
138 {
139         struct ares_mx_reply *mx_out;
140
141         if (IO->VParsedDNSReply != NULL)
142                 IO->DNSReplyFree(IO->VParsedDNSReply);
143         IO->VParsedDNSReply = NULL;
144
145         IO->DNSStatus = ares_parse_mx_reply(abuf, alen, &mx_out);
146         if (IO->DNSStatus != ARES_SUCCESS) {
147 //    ResolveError(arg->js_cb, status);
148                 return;
149         }
150
151         IO->VParsedDNSReply = mx_out;
152         IO->DNSReplyFree = (FreeDNSReply) ares_free_data;
153 }
154
155
156 static void ParseAnswerNS(AsyncIO *IO, unsigned char* abuf, int alen) 
157 {
158         struct hostent* host;
159
160         if (IO->VParsedDNSReply != NULL)
161                 IO->DNSReplyFree(IO->VParsedDNSReply);
162         IO->VParsedDNSReply = NULL;
163
164         IO->DNSStatus = ares_parse_ns_reply(abuf, alen, &host);
165         if (IO->DNSStatus != ARES_SUCCESS) {
166 //    ResolveError(arg->js_cb, status);
167                 return;
168         }
169         IO->VParsedDNSReply = host;
170         IO->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
171 }
172
173
174 static void ParseAnswerSRV(AsyncIO *IO, unsigned char* abuf, int alen) 
175 {
176         struct ares_srv_reply *srv_out;
177
178         if (IO->VParsedDNSReply != NULL)
179                 IO->DNSReplyFree(IO->VParsedDNSReply);
180         IO->VParsedDNSReply = NULL;
181
182         IO->DNSStatus = ares_parse_srv_reply(abuf, alen, &srv_out);
183         if (IO->DNSStatus != ARES_SUCCESS) {
184 //    ResolveError(arg->js_cb, status);
185                 return;
186         }
187
188         IO->VParsedDNSReply = srv_out;
189         IO->DNSReplyFree = (FreeDNSReply) ares_free_data;
190 }
191
192
193 static void ParseAnswerTXT(AsyncIO *IO, unsigned char* abuf, int alen) 
194 {
195         struct ares_txt_reply *txt_out;
196
197         if (IO->VParsedDNSReply != NULL)
198                 IO->DNSReplyFree(IO->VParsedDNSReply);
199         IO->VParsedDNSReply = NULL;
200
201         IO->DNSStatus = ares_parse_txt_reply(abuf, alen, &txt_out);
202         if (IO->DNSStatus != ARES_SUCCESS) {
203 //    ResolveError(arg->js_cb, status);
204                 return;
205         }
206         IO->VParsedDNSReply = txt_out;
207         IO->DNSReplyFree = (FreeDNSReply) ares_free_data;
208 }
209
210 void QueryCb(void *arg,
211              int status,
212              int timeouts,
213              unsigned char* abuf,
214              int alen) 
215 {
216         AsyncIO *IO = arg;
217
218         IO->DNSStatus = status;
219         if (status == ARES_SUCCESS)
220                 IO->DNS_CB(arg, abuf, alen);
221         else
222                 IO->DNSStatus = status;
223 ///     ev_io_stop(event_base, &IO->dns_io_event);
224         
225         ev_timer_init(&IO->unwind_stack_timeout,
226                       IO_postdns_callback, 0.0, 0);
227         IO->unwind_stack_timeout.data = IO;
228         ev_timer_start(event_base, &IO->unwind_stack_timeout);
229         CtdlLogPrintf(CTDL_DEBUG, "C-ARES: %s\n", __FUNCTION__);
230 }
231
232 void QueryCbDone(AsyncIO *IO)
233 {
234         ev_timer_stop(event_base, &IO->unwind_stack_timeout);
235 }
236
237
238 void InitC_ares_dns(AsyncIO *IO)
239 {
240         int optmask = 0;
241         if (IO->DNSChannel == NULL) {
242                 optmask |= ARES_OPT_SOCK_STATE_CB;
243                 IO->DNSOptions.sock_state_cb = SockStateCb;
244                 IO->DNSOptions.sock_state_cb_data = IO;
245                 ares_init_options(&IO->DNSChannel, &IO->DNSOptions, optmask);
246         }
247 }
248 int QueueQuery(ns_type Type, char *name, AsyncIO *IO, IO_CallBack PostDNS)
249 {
250         int length, family;
251         char address_b[sizeof(struct in6_addr)];
252
253         InitC_ares_dns(IO);
254         IO->PostDNS = PostDNS;
255         switch(Type) {
256         case ns_t_a:
257                 IO->DNS_CB = ParseAnswerA;
258                 break;
259
260         case ns_t_aaaa:
261                 IO->DNS_CB = ParseAnswerAAAA;
262                 break;
263
264         case ns_t_mx:
265                 IO->DNS_CB = ParseAnswerMX;
266                 break;
267
268         case ns_t_ns:
269                 IO->DNS_CB = ParseAnswerNS;
270                 break;
271
272         case ns_t_txt:
273                 IO->DNS_CB = ParseAnswerTXT;
274                 break;
275
276         case ns_t_srv:
277                 IO->DNS_CB = ParseAnswerSRV;
278                 break;
279
280         case ns_t_cname:
281                 IO->DNS_CB = ParseAnswerCNAME;
282                 break;
283
284         case ns_t_ptr:
285
286
287                 if (inet_pton(AF_INET, name, &address_b) == 1) {
288                         length = sizeof(struct in_addr);
289                         family = AF_INET;
290                 } else if (inet_pton(AF_INET6, name, &address_b) == 1) {
291                         length = sizeof(struct in6_addr);
292                         family = AF_INET6;
293                 } else {
294                         return -1;
295                 }
296
297                 ares_gethostbyaddr(IO->DNSChannel, address_b, length, family, HostByAddrCb, IO);
298
299                 return 1;
300
301         default:
302                 return 0;
303         }
304         ares_query(IO->DNSChannel, name, ns_c_in, Type, QueryCb, IO);
305         return 1;
306 }
307
308 static void DNS_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
309 {
310         AsyncIO *IO = watcher->data;
311         
312         ares_process_fd(IO->DNSChannel, ARES_SOCKET_BAD, IO->dns_send_event.fd);
313 }
314 static void DNS_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
315 {
316         AsyncIO *IO = watcher->data;
317         
318         ares_process_fd(IO->DNSChannel, IO->dns_recv_event.fd, ARES_SOCKET_BAD);
319 }
320
321 void SockStateCb(void *data, int sock, int read, int write) 
322 {
323 /*
324         struct timeval tvbuf, maxtv, *ret;
325         
326         int64_t time = 10;
327 */
328         AsyncIO *IO = data;
329 /* already inside of the event queue. */        
330
331         if (read) {
332                 if ((IO->dns_recv_event.fd != sock) &&
333                     (IO->dns_recv_event.fd != 0) && 
334                     ((IO->active_dns_event & EV_READ) != 0)) {
335                         ev_io_stop(event_base, &IO->dns_recv_event);
336                 }
337                 IO->dns_recv_event.fd = sock;
338                 ev_io_init(&IO->dns_recv_event, DNS_recv_callback, IO->dns_recv_event.fd, EV_READ);
339                 IO->dns_recv_event.data = IO;
340                 ev_io_start(event_base, &IO->dns_recv_event);
341                 IO->active_dns_event = IO->active_dns_event | EV_READ;
342         } 
343         if (write) {
344                 if ((IO->dns_send_event.fd != sock) &&
345                     (IO->dns_send_event.fd != 0) && 
346                     ((IO->active_dns_event & EV_WRITE) != 0)) {
347                         ev_io_stop(event_base, &IO->dns_send_event);
348                 }
349                 IO->dns_send_event.fd = sock;
350                 ev_io_init(&IO->dns_send_event, DNS_send_callback, IO->dns_send_event.fd, EV_WRITE);
351                 IO->dns_send_event.data = IO;
352                 ev_io_start(event_base, &IO->dns_send_event);
353                 IO->active_dns_event = IO->active_dns_event | EV_WRITE;
354         }
355 /*
356
357                 ev_io_start(event_base, &IO->dns_io_event);
358         
359                 maxtv.tv_sec = time/1000;
360                 maxtv.tv_usec = (time % 1000) * 1000;
361                 
362                 ret = ares_timeout(IO->DNSChannel, &maxtv, &tvbuf);
363         }
364 */
365         if ((read == 0) && (write == 0)) {
366                 if ((IO->active_dns_event & EV_READ) != 0)
367                         ev_io_stop(event_base, &IO->dns_recv_event);
368                 if ((IO->active_dns_event & EV_WRITE) != 0)
369                         ev_io_stop(event_base, &IO->dns_send_event);
370                 IO->active_dns_event = 0;
371         }
372 }
373
374 CTDL_MODULE_INIT(c_ares_client)
375 {
376         if (!threading)
377         {
378                 int r = ares_library_init(ARES_LIB_INIT_ALL);
379                 if (0 != r) {
380                         // TODO
381                         // ThrowException(Exception::Error(String::New(ares_strerror(r))));
382 ////                    assert(r == 0);
383                 }
384         }
385         return "c-ares";
386 }