C-Ares: stop timer for timeouts in all cases.
[citadel.git] / citadel / modules / c-ares-dns / serv_c-ares-dns.c
1 /*
2  * Copyright (c) 1998-2012 by the citadel.org team
3  *
4  *  This program is open source software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License version 3.
6  *  
7  *  
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  *  
15  *  
16  *  
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 #ifdef DEBUG_CARES
73         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
74         EV_DNS_LOGT_STOP(DNS.timeout);
75 #endif
76         ev_timer_stop (event_base, &IO->DNS.timeout);
77
78         IO->DNS.Query->DNSStatus = status;
79         if  (status != ARES_SUCCESS) {
80                 StrBufPlain(IO->ErrMsg, ares_strerror(status), -1);
81                 return;
82         }
83         IO->DNS.Query->Data = hostent;
84 }
85
86 static void ParseAnswerA(AsyncIO *IO, unsigned char* abuf, int alen)
87 {
88         struct hostent* host;
89 #ifdef DEBUG_CARES
90         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
91 #endif
92
93         if (IO->DNS.Query->VParsedDNSReply != NULL)
94                 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
95         IO->DNS.Query->VParsedDNSReply = NULL;
96
97         IO->DNS.Query->DNSStatus = ares_parse_a_reply(abuf,
98                                                       alen,
99                                                       &host,
100                                                       NULL,
101                                                       NULL);
102         if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
103                 StrBufPlain(IO->ErrMsg,
104                             ares_strerror(IO->DNS.Query->DNSStatus), -1);
105                 return;
106         }
107         IO->DNS.Query->VParsedDNSReply = host;
108         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
109 }
110
111
112 static void ParseAnswerAAAA(AsyncIO *IO, unsigned char* abuf, int alen)
113 {
114         struct hostent* host;
115 #ifdef DEBUG_CARES
116         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
117 #endif
118
119         if (IO->DNS.Query->VParsedDNSReply != NULL)
120                 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
121         IO->DNS.Query->VParsedDNSReply = NULL;
122
123         IO->DNS.Query->DNSStatus = ares_parse_aaaa_reply(abuf,
124                                                          alen,
125                                                          &host,
126                                                          NULL,
127                                                          NULL);
128         if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
129                 StrBufPlain(IO->ErrMsg,
130                             ares_strerror(IO->DNS.Query->DNSStatus), -1);
131                 return;
132         }
133         IO->DNS.Query->VParsedDNSReply = host;
134         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
135 }
136
137
138 static void ParseAnswerCNAME(AsyncIO *IO, unsigned char* abuf, int alen)
139 {
140         struct hostent* host;
141
142 #ifdef DEBUG_CARES
143         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
144 #endif
145
146         if (IO->DNS.Query->VParsedDNSReply != NULL)
147                 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
148         IO->DNS.Query->VParsedDNSReply = NULL;
149
150         IO->DNS.Query->DNSStatus = ares_parse_a_reply(abuf,
151                                                       alen,
152                                                       &host,
153                                                       NULL,
154                                                       NULL);
155         if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
156                 StrBufPlain(IO->ErrMsg,
157                             ares_strerror(IO->DNS.Query->DNSStatus), -1);
158                 return;
159         }
160
161         // a CNAME lookup always returns a single record but
162         IO->DNS.Query->VParsedDNSReply = host;
163         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
164 }
165
166
167 static void ParseAnswerMX(AsyncIO *IO, unsigned char* abuf, int alen)
168 {
169         struct ares_mx_reply *mx_out;
170 #ifdef DEBUG_CARES
171         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
172 #endif
173
174         if (IO->DNS.Query->VParsedDNSReply != NULL)
175                 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
176         IO->DNS.Query->VParsedDNSReply = NULL;
177
178         IO->DNS.Query->DNSStatus = ares_parse_mx_reply(abuf, alen, &mx_out);
179         if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
180                 StrBufPlain(IO->ErrMsg,
181                             ares_strerror(IO->DNS.Query->DNSStatus), -1);
182                 return;
183         }
184
185         IO->DNS.Query->VParsedDNSReply = mx_out;
186         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_data;
187 }
188
189
190 static void ParseAnswerNS(AsyncIO *IO, unsigned char* abuf, int alen)
191 {
192         struct hostent* host;
193 #ifdef DEBUG_CARES
194         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
195 #endif
196
197         if (IO->DNS.Query->VParsedDNSReply != NULL)
198                 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
199         IO->DNS.Query->VParsedDNSReply = NULL;
200
201         IO->DNS.Query->DNSStatus = ares_parse_ns_reply(abuf, alen, &host);
202         if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
203                 StrBufPlain(IO->ErrMsg,
204                             ares_strerror(IO->DNS.Query->DNSStatus), -1);
205                 return;
206         }
207         IO->DNS.Query->VParsedDNSReply = host;
208         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
209 }
210
211
212 static void ParseAnswerSRV(AsyncIO *IO, unsigned char* abuf, int alen)
213 {
214         struct ares_srv_reply *srv_out;
215 #ifdef DEBUG_CARES
216         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
217 #endif
218
219         if (IO->DNS.Query->VParsedDNSReply != NULL)
220                 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
221         IO->DNS.Query->VParsedDNSReply = NULL;
222
223         IO->DNS.Query->DNSStatus = ares_parse_srv_reply(abuf, alen, &srv_out);
224         if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
225                 StrBufPlain(IO->ErrMsg,
226                             ares_strerror(IO->DNS.Query->DNSStatus), -1);
227                 return;
228         }
229
230         IO->DNS.Query->VParsedDNSReply = srv_out;
231         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_data;
232 }
233
234
235 static void ParseAnswerTXT(AsyncIO *IO, unsigned char* abuf, int alen)
236 {
237         struct ares_txt_reply *txt_out;
238 #ifdef DEBUG_CARES
239         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
240 #endif
241
242         if (IO->DNS.Query->VParsedDNSReply != NULL)
243                 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
244         IO->DNS.Query->VParsedDNSReply = NULL;
245
246         IO->DNS.Query->DNSStatus = ares_parse_txt_reply(abuf, alen, &txt_out);
247         if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
248                 StrBufPlain(IO->ErrMsg,
249                             ares_strerror(IO->DNS.Query->DNSStatus), -1);
250                 return;
251         }
252         IO->DNS.Query->VParsedDNSReply = txt_out;
253         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_data;
254 }
255
256 void QueryCb(void *arg,
257              int status,
258              int timeouts,
259              unsigned char* abuf,
260              int alen)
261 {
262         AsyncIO *IO = arg;
263 #ifdef DEBUG_CARES
264         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
265         EV_DNS_LOGT_STOP(DNS.timeout);
266 #endif
267         ev_timer_stop (event_base, &IO->DNS.timeout);
268
269         IO->DNS.Query->DNSStatus = status;
270         if (status == ARES_SUCCESS)
271                 IO->DNS.Query->DNS_CB(arg, abuf, alen);
272         else {
273                 EV_syslog(LOG_DEBUG, "C-ARES: Failed by: %s error %s\n",
274                           __FUNCTION__,
275                           ares_strerror(status));
276                 StrBufPlain(IO->ErrMsg, ares_strerror(status), -1);
277                 IO->DNS.Query->DNSStatus = status;
278         }
279
280         ev_idle_init(&IO->unwind_stack,
281                      IO_postdns_callback);
282         IO->unwind_stack.data = IO;
283         EV_DNS_LOGT_INIT(unwind_stack);
284         EV_DNS_LOGT_START(unwind_stack);
285         ev_idle_start(event_base, &IO->unwind_stack);
286 }
287
288 void QueryCbDone(AsyncIO *IO)
289 {
290 #ifdef DEBUG_CARES
291         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
292         EV_DNS_LOGT_STOP(DNS.timeout);
293 #endif
294
295         ev_idle_stop(event_base, &IO->unwind_stack);
296 }
297
298 void DestructCAres(AsyncIO *IO)
299 {
300         ev_timer_stop (event_base, &IO->DNS.timeout);
301         ares_destroy_options(&IO->DNS.Options);
302 }
303
304
305 void InitC_ares_dns(AsyncIO *IO)
306 {
307         int optmask = 0;
308 #ifdef DEBUG_CARES
309         EV_syslog(LOG_DEBUG, "C-ARES: %s %p\n", __FUNCTION__, IO->DNS.Channel);
310 #endif
311
312         if (IO->DNS.Channel == NULL) {
313                 optmask |= ARES_OPT_SOCK_STATE_CB;
314                 IO->DNS.Options.sock_state_cb = SockStateCb;
315                 IO->DNS.Options.sock_state_cb_data = IO;
316                 ares_init_options(&IO->DNS.Channel, &IO->DNS.Options, optmask);
317         }
318         IO->DNS.Query->DNSStatus = 0;
319 }
320
321 static void
322 DNStimeouttrigger_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
323 {
324         AsyncIO *IO = watcher->data;
325         struct timeval tv, MaxTV;
326         struct timeval *NextTV;
327
328         memset(&MaxTV, 0, sizeof(MaxTV));
329         memset(&tv, 0, sizeof(tv));
330         MaxTV.tv_sec = 30;
331         NextTV = ares_timeout(IO->DNS.Channel, &MaxTV, &tv);
332
333         if ((NextTV->tv_sec != MaxTV.tv_sec) ||
334             (NextTV->tv_usec != MaxTV.tv_usec))
335         {
336                 fd_set readers, writers;
337 #ifdef DEBUG_CARES
338                 EV_syslog(LOG_DEBUG, "C-ARES: %s Timeout!\n", __FUNCTION__);
339 #endif
340                 FD_ZERO(&readers);
341                 FD_ZERO(&writers);
342                 ares_fds(IO->DNS.Channel, &readers, &writers);
343                 ares_process(IO->DNS.Channel, &readers, &writers);
344         }
345 }
346
347 void QueueGetHostByNameDone(void *Ctx,
348                             int status,
349                             int timeouts,
350                             struct hostent *hostent)
351 {
352         AsyncIO *IO = (AsyncIO *) Ctx;
353 #ifdef DEBUG_CARES
354         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
355 #endif
356
357         IO->DNS.Query->DNSStatus = status;
358         IO->DNS.Query->VParsedDNSReply = hostent;
359         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
360
361         ev_idle_init(&IO->unwind_stack,
362                      IO_postdns_callback);
363         IO->unwind_stack.data = IO;
364         EV_DNS_LOGT_INIT(unwind_stack);
365         EV_DNS_LOGT_START(unwind_stack);
366         ev_idle_start(event_base, &IO->unwind_stack);
367         ev_timer_stop (event_base, &IO->DNS.timeout);
368 }
369
370 void QueueGetHostByName(AsyncIO *IO,
371                         const char *Hostname,
372                         DNSQueryParts *QueryParts,
373                         IO_CallBack PostDNS)
374 {
375 #ifdef DEBUG_CARES
376         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
377         IO->DNS.SourcePort = 0;
378 #endif
379
380         IO->DNS.Query = QueryParts;
381         IO->DNS.Query->PostDNS = PostDNS;
382
383         InitC_ares_dns(IO);
384
385         ev_timer_init(&IO->DNS.timeout, DNStimeouttrigger_callback, 10, 1);
386         EV_DNS_LOGT_INIT(DNS.timeout);
387         IO->DNS.timeout.data = IO;
388         ares_gethostbyname(IO->DNS.Channel,
389                            Hostname,
390                            AF_INET6, /* it falls back to ipv4 in doubt... */
391                            QueueGetHostByNameDone,
392                            IO);
393         EV_DNS_LOGT_START(DNS.timeout);
394         ev_timer_start(event_base, &IO->DNS.timeout);
395
396 }
397
398 int QueueQuery(ns_type Type,
399                const char *name,
400                AsyncIO *IO,
401                DNSQueryParts *QueryParts,
402                IO_CallBack PostDNS)
403 {
404         int length, family;
405         char address_b[sizeof(struct in6_addr)];
406
407 #ifdef DEBUG_CARES
408         IO->DNS.SourcePort = 0;
409 #endif
410
411         IO->DNS.Query = QueryParts;
412         IO->DNS.Query->PostDNS = PostDNS;
413
414         InitC_ares_dns(IO);
415
416         ev_timer_init(&IO->DNS.timeout, DNStimeouttrigger_callback, 10, 1);
417         IO->DNS.timeout.data = IO;
418         EV_DNS_LOGT_INIT(DNS.timeout);
419
420         switch(Type) {
421         case ns_t_a:
422                 IO->DNS.Query->DNS_CB = ParseAnswerA;
423                 break;
424
425         case ns_t_aaaa:
426                 IO->DNS.Query->DNS_CB = ParseAnswerAAAA;
427                 break;
428
429         case ns_t_mx:
430                 IO->DNS.Query->DNS_CB = ParseAnswerMX;
431                 break;
432
433         case ns_t_ns:
434                 IO->DNS.Query->DNS_CB = ParseAnswerNS;
435                 break;
436
437         case ns_t_txt:
438                 IO->DNS.Query->DNS_CB = ParseAnswerTXT;
439                 break;
440
441         case ns_t_srv:
442                 IO->DNS.Query->DNS_CB = ParseAnswerSRV;
443                 break;
444
445         case ns_t_cname:
446                 IO->DNS.Query->DNS_CB = ParseAnswerCNAME;
447                 break;
448
449         case ns_t_ptr:
450
451
452                 if (inet_pton(AF_INET, name, &address_b) == 1) {
453                         length = sizeof(struct in_addr);
454                         family = AF_INET;
455                 } else if (inet_pton(AF_INET6, name, &address_b) == 1) {
456                         length = sizeof(struct in6_addr);
457                         family = AF_INET6;
458                 } else {
459                         return -1;
460                 }
461
462                 ares_gethostbyaddr(IO->DNS.Channel,
463                                    address_b,
464                                    length,
465                                    family,
466                                    HostByAddrCb,
467                                    IO);
468                 EV_DNS_LOGT_START(DNS.timeout);
469                 ev_timer_start(event_base, &IO->DNS.timeout);
470 #ifdef DEBUG_CARES
471                 EV_syslog(LOG_DEBUG, "C-ARES: %s X1\n", __FUNCTION__);
472 #endif
473                 return 1;
474
475         default:
476 #ifdef DEBUG_CARES
477                 EV_syslog(LOG_DEBUG, "C-ARES: %sX2\n", __FUNCTION__);
478 #endif
479                 return 0;
480         }
481 #ifdef DEBUG_CARES
482         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
483 #endif
484         ares_query(IO->DNS.Channel, name, ns_c_in, Type, QueryCb, IO);
485         EV_DNS_LOGT_START(DNS.timeout);
486         ev_timer_start(event_base, &IO->DNS.timeout);
487         return 1;
488 }
489
490
491
492
493
494 /*****************************************************************************
495  *                      libev / c-ares integration                           *
496  *****************************************************************************/
497 static void DNS_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
498 {
499         AsyncIO *IO = watcher->data;
500
501 #ifdef DEBUG_CARES
502         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
503 #endif
504
505         ares_process_fd(IO->DNS.Channel,
506                         ARES_SOCKET_BAD,
507                         IO->DNS.send_event.fd);
508 }
509 static void DNS_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
510 {
511         AsyncIO *IO = watcher->data;
512
513 #ifdef DEBUG_CARES
514         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
515 #endif
516
517         ares_process_fd(IO->DNS.Channel,
518                         IO->DNS.recv_event.fd,
519                         ARES_SOCKET_BAD);
520 }
521
522 void SockStateCb(void *data, int sock, int read, int write)
523 {
524         AsyncIO *IO = data;
525 /* already inside of the event queue. */
526 #ifdef DEBUG_CARES
527 {
528         struct sockaddr_in sin = {};
529         socklen_t slen;
530         slen = sizeof(sin);
531         if ((IO->DNS.SourcePort == 0) &&
532             (getsockname(sock, &sin, &slen) == 0))
533         {
534                 IO->DNS.SourcePort = ntohs(sin.sin_port);
535         }
536         EV_syslog(LOG_DEBUG, "C-ARES: %s %d|%d Sock %d port %hu\n",
537                   __FUNCTION__,
538                   read,
539                   write,
540                   sock,
541                   IO->DNS.SourcePort);
542 }
543 #endif
544
545         if (read) {
546                 if ((IO->DNS.recv_event.fd != sock) &&
547                     (IO->DNS.recv_event.fd != 0)) {
548                         EV_DNS_LOG_STOP(DNS.recv_event);
549                         ev_io_stop(event_base, &IO->DNS.recv_event);
550                 }
551                 IO->DNS.recv_event.fd = sock;
552                 ev_io_init(&IO->DNS.recv_event,
553                            DNS_recv_callback,
554                            IO->DNS.recv_event.fd,
555                            EV_READ);
556                 EV_DNS_LOG_INIT(DNS.recv_event);
557                 IO->DNS.recv_event.data = IO;
558                 EV_DNS_LOG_START(DNS.recv_event);
559                 ev_io_start(event_base, &IO->DNS.recv_event);
560         }
561         if (write) {
562                 if ((IO->DNS.send_event.fd != sock) &&
563                     (IO->DNS.send_event.fd != 0)) {
564                         EV_DNS_LOG_STOP(DNS.send_event);
565                         ev_io_stop(event_base, &IO->DNS.send_event);
566                 }
567                 IO->DNS.send_event.fd = sock;
568                 ev_io_init(&IO->DNS.send_event,
569                            DNS_send_callback,
570                            IO->DNS.send_event.fd,
571                            EV_WRITE);
572                 IO->DNS.send_event.data = IO;
573                 EV_DNS_LOG_INIT(DNS.send_event);
574                 EV_DNS_LOG_START(DNS.send_event);
575                 ev_io_start(event_base, &IO->DNS.send_event);
576         }
577         if ((read == 0) && (write == 0)) {
578                 EV_DNS_LOG_STOP(DNS.recv_event);
579                 EV_DNS_LOG_STOP(DNS.send_event);
580                 ev_io_stop(event_base, &IO->DNS.recv_event);
581                 ev_io_stop(event_base, &IO->DNS.send_event);
582         }
583 }
584
585 CTDL_MODULE_INIT(c_ares_client)
586 {
587         if (!threading)
588         {
589                 int r = ares_library_init(ARES_LIB_INIT_ALL);
590                 if (0 != r) {
591                         
592                 }
593         }
594         return "c-ares";
595 }