2 * Copyright (c) 1998-2012 by the citadel.org team
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.
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.
18 * Inspired by NodeJS.org; thanks for the MX-Parser ;-)
30 #include <sys/types.h>
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
38 # include <sys/time.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
51 #include <libcitadel.h>
54 #include "citserver.h"
57 #include "ctdl_module.h"
58 #include "event_client.h"
62 extern struct ev_loop *event_base;
64 void SockStateCb(void *data, int sock, int read, int write);
67 static void HostByAddrCb(void *data,
70 struct hostent *hostent)
74 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
76 EV_DNS_LOGT_STOP(DNS.timeout);
77 ev_timer_stop (event_base, &IO->DNS.timeout);
79 IO->DNS.Query->DNSStatus = status;
80 if (status != ARES_SUCCESS) {
81 StrBufPlain(IO->ErrMsg, ares_strerror(status), -1);
84 IO->DNS.Query->Data = hostent;
87 static void ParseAnswerA(AsyncIO *IO, unsigned char* abuf, int alen)
89 struct hostent* host = NULL;
91 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
93 if (IO->DNS.Query->VParsedDNSReply != NULL)
94 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
95 IO->DNS.Query->VParsedDNSReply = NULL;
97 IO->DNS.Query->DNSStatus = ares_parse_a_reply(abuf,
102 if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
104 ares_free_hostent(host);
105 StrBufPlain(IO->ErrMsg,
106 ares_strerror(IO->DNS.Query->DNSStatus), -1);
109 IO->DNS.Query->VParsedDNSReply = host;
110 IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
114 static void ParseAnswerAAAA(AsyncIO *IO, unsigned char* abuf, int alen)
116 struct hostent* host = NULL;
118 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
120 if (IO->DNS.Query->VParsedDNSReply != NULL)
121 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
122 IO->DNS.Query->VParsedDNSReply = NULL;
124 IO->DNS.Query->DNSStatus = ares_parse_aaaa_reply(abuf,
129 if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
131 ares_free_hostent(host);
132 StrBufPlain(IO->ErrMsg,
133 ares_strerror(IO->DNS.Query->DNSStatus), -1);
136 IO->DNS.Query->VParsedDNSReply = host;
137 IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
141 static void ParseAnswerCNAME(AsyncIO *IO, unsigned char* abuf, int alen)
143 struct hostent* host = NULL;
145 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
147 if (IO->DNS.Query->VParsedDNSReply != NULL)
148 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
149 IO->DNS.Query->VParsedDNSReply = NULL;
151 IO->DNS.Query->DNSStatus = ares_parse_a_reply(abuf,
156 if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
158 ares_free_hostent(host);
159 StrBufPlain(IO->ErrMsg,
160 ares_strerror(IO->DNS.Query->DNSStatus), -1);
164 // a CNAME lookup always returns a single record but
165 IO->DNS.Query->VParsedDNSReply = host;
166 IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
170 static void ParseAnswerMX(AsyncIO *IO, unsigned char* abuf, int alen)
172 struct ares_mx_reply *mx_out = NULL;
174 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
176 if (IO->DNS.Query->VParsedDNSReply != NULL)
177 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
178 IO->DNS.Query->VParsedDNSReply = NULL;
180 IO->DNS.Query->DNSStatus = ares_parse_mx_reply(abuf, alen, &mx_out);
181 if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
183 ares_free_data(mx_out);
184 StrBufPlain(IO->ErrMsg,
185 ares_strerror(IO->DNS.Query->DNSStatus), -1);
189 IO->DNS.Query->VParsedDNSReply = mx_out;
190 IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_data;
194 static void ParseAnswerNS(AsyncIO *IO, unsigned char* abuf, int alen)
196 struct hostent* host = NULL;
198 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
200 if (IO->DNS.Query->VParsedDNSReply != NULL)
201 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
202 IO->DNS.Query->VParsedDNSReply = NULL;
204 IO->DNS.Query->DNSStatus = ares_parse_ns_reply(abuf, alen, &host);
205 if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
207 ares_free_hostent(host);
208 StrBufPlain(IO->ErrMsg,
209 ares_strerror(IO->DNS.Query->DNSStatus), -1);
212 IO->DNS.Query->VParsedDNSReply = host;
213 IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
217 static void ParseAnswerSRV(AsyncIO *IO, unsigned char* abuf, int alen)
219 struct ares_srv_reply *srv_out = NULL;
221 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
223 if (IO->DNS.Query->VParsedDNSReply != NULL)
224 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
225 IO->DNS.Query->VParsedDNSReply = NULL;
227 IO->DNS.Query->DNSStatus = ares_parse_srv_reply(abuf, alen, &srv_out);
228 if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
230 ares_free_data(srv_out);
231 StrBufPlain(IO->ErrMsg,
232 ares_strerror(IO->DNS.Query->DNSStatus), -1);
236 IO->DNS.Query->VParsedDNSReply = srv_out;
237 IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_data;
241 static void ParseAnswerTXT(AsyncIO *IO, unsigned char* abuf, int alen)
243 struct ares_txt_reply *txt_out;
245 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
247 if (IO->DNS.Query->VParsedDNSReply != NULL)
248 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
249 IO->DNS.Query->VParsedDNSReply = NULL;
251 IO->DNS.Query->DNSStatus = ares_parse_txt_reply(abuf, alen, &txt_out);
252 if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
254 ares_free_data(txt_out);
255 StrBufPlain(IO->ErrMsg,
256 ares_strerror(IO->DNS.Query->DNSStatus), -1);
259 IO->DNS.Query->VParsedDNSReply = txt_out;
260 IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_data;
263 void QueryCb(void *arg,
271 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
273 EV_DNS_LOGT_STOP(DNS.timeout);
274 ev_timer_stop (event_base, &IO->DNS.timeout);
276 IO->DNS.Query->DNSStatus = status;
277 if (status == ARES_SUCCESS)
278 IO->DNS.Query->DNS_CB(arg, abuf, alen);
280 EV_DNS_syslog(LOG_DEBUG, "C-ARES: Failed by: %s error %s\n",
282 ares_strerror(status));
283 StrBufPlain(IO->ErrMsg, ares_strerror(status), -1);
284 IO->DNS.Query->DNSStatus = status;
287 ev_idle_init(&IO->unwind_stack,
288 IO_postdns_callback);
289 IO->unwind_stack.data = IO;
290 EV_DNS_LOGT_INIT(unwind_stack);
291 EV_DNS_LOGT_START(unwind_stack);
292 ev_idle_start(event_base, &IO->unwind_stack);
295 void QueryCbDone(AsyncIO *IO)
297 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
299 EV_DNS_LOGT_STOP(DNS.timeout);
300 ev_timer_stop (event_base, &IO->DNS.timeout);
302 EV_DNS_LOGT_STOP(unwind_stack);
303 ev_idle_stop(event_base, &IO->unwind_stack);
306 void DestructCAres(AsyncIO *IO)
308 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
310 EV_DNS_LOG_STOP(DNS.recv_event);
311 ev_io_stop(event_base, &IO->DNS.recv_event);
313 EV_DNS_LOG_STOP(DNS.send_event);
314 ev_io_stop(event_base, &IO->DNS.send_event);
316 EV_DNS_LOGT_STOP(DNS.timeout);
317 ev_timer_stop (event_base, &IO->DNS.timeout);
319 EV_DNS_LOGT_STOP(unwind_stack);
320 ev_idle_stop(event_base, &IO->unwind_stack);
321 ares_destroy_options(&IO->DNS.Options);
325 void InitC_ares_dns(AsyncIO *IO)
329 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s %p\n", __FUNCTION__, IO->DNS.Channel);
331 if (IO->DNS.Channel == NULL) {
332 optmask |= ARES_OPT_SOCK_STATE_CB;
333 IO->DNS.Options.sock_state_cb = SockStateCb;
334 IO->DNS.Options.sock_state_cb_data = IO;
335 ares_init_options(&IO->DNS.Channel, &IO->DNS.Options, optmask);
337 IO->DNS.Query->DNSStatus = 0;
341 DNStimeouttrigger_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
343 AsyncIO *IO = watcher->data;
344 struct timeval tv, MaxTV;
345 struct timeval *NextTV;
347 memset(&MaxTV, 0, sizeof(MaxTV));
348 memset(&tv, 0, sizeof(tv));
350 NextTV = ares_timeout(IO->DNS.Channel, &MaxTV, &tv);
352 if ((NextTV->tv_sec != MaxTV.tv_sec) ||
353 (NextTV->tv_usec != MaxTV.tv_usec))
355 fd_set readers, writers;
356 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s Timeout!\n", __FUNCTION__);
360 ares_fds(IO->DNS.Channel, &readers, &writers);
361 ares_process(IO->DNS.Channel, &readers, &writers);
365 void QueueGetHostByNameDone(void *Ctx,
368 struct hostent *hostent)
370 AsyncIO *IO = (AsyncIO *) Ctx;
372 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
375 IO->DNS.Query->DNSStatus = status;
376 IO->DNS.Query->VParsedDNSReply = hostent;
377 IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
379 EV_DNS_LOGT_STOP(DNS.timeout);
380 ev_timer_stop (event_base, &IO->DNS.timeout);
382 ev_idle_init(&IO->unwind_stack,
383 IO_postdns_callback);
384 IO->unwind_stack.data = IO;
385 EV_DNS_LOGT_INIT(unwind_stack);
386 EV_DNS_LOGT_START(unwind_stack);
387 ev_idle_start(event_base, &IO->unwind_stack);
391 void QueueGetHostByName(AsyncIO *IO,
392 const char *Hostname,
393 DNSQueryParts *QueryParts,
397 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
398 IO->DNS.SourcePort = 0;
400 IO->DNS.Query = QueryParts;
401 IO->DNS.Query->PostDNS = PostDNS;
405 ev_timer_init(&IO->DNS.timeout, DNStimeouttrigger_callback, 10, 1);
406 EV_DNS_LOGT_INIT(DNS.timeout);
407 IO->DNS.timeout.data = IO;
408 ares_gethostbyname(IO->DNS.Channel,
410 AF_INET6, /* it falls back to ipv4 in doubt... */
411 QueueGetHostByNameDone,
413 EV_DNS_LOGT_START(DNS.timeout);
414 ev_timer_start(event_base, &IO->DNS.timeout);
418 int QueueQuery(ns_type Type,
421 DNSQueryParts *QueryParts,
425 char address_b[sizeof(struct in6_addr)];
427 IO->DNS.SourcePort = 0;
429 IO->DNS.Query = QueryParts;
430 IO->DNS.Query->PostDNS = PostDNS;
431 IO->DNS.Start = IO->Now;
435 ev_timer_init(&IO->DNS.timeout, DNStimeouttrigger_callback, 10, 1);
436 IO->DNS.timeout.data = IO;
437 EV_DNS_LOGT_INIT(DNS.timeout);
441 IO->DNS.Query->DNS_CB = ParseAnswerA;
445 IO->DNS.Query->DNS_CB = ParseAnswerAAAA;
449 IO->DNS.Query->DNS_CB = ParseAnswerMX;
453 IO->DNS.Query->DNS_CB = ParseAnswerNS;
457 IO->DNS.Query->DNS_CB = ParseAnswerTXT;
461 IO->DNS.Query->DNS_CB = ParseAnswerSRV;
465 IO->DNS.Query->DNS_CB = ParseAnswerCNAME;
471 if (inet_pton(AF_INET, name, &address_b) == 1) {
472 length = sizeof(struct in_addr);
474 } else if (inet_pton(AF_INET6, name, &address_b) == 1) {
475 length = sizeof(struct in6_addr);
481 ares_gethostbyaddr(IO->DNS.Channel,
487 EV_DNS_LOGT_START(DNS.timeout);
488 ev_timer_start(event_base, &IO->DNS.timeout);
490 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s X1\n", __FUNCTION__);
496 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %sX2\n", __FUNCTION__);
499 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
501 ares_query(IO->DNS.Channel, name, ns_c_in, Type, QueryCb, IO);
502 EV_DNS_LOGT_START(DNS.timeout);
503 ev_timer_start(event_base, &IO->DNS.timeout);
511 /*****************************************************************************
512 * libev / c-ares integration *
513 *****************************************************************************/
514 static void DNS_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
516 AsyncIO *IO = watcher->data;
518 IO->Now = ev_now(event_base);
520 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
522 ares_process_fd(IO->DNS.Channel,
524 IO->DNS.send_event.fd);
526 static void DNS_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
528 AsyncIO *IO = watcher->data;
530 IO->Now = ev_now(event_base);
532 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
534 ares_process_fd(IO->DNS.Channel,
535 IO->DNS.recv_event.fd,
539 void SockStateCb(void *data, int sock, int read, int write)
542 /* already inside of the event queue. */
545 struct sockaddr_in sin = {};
548 if ((IO->DNS.SourcePort == 0) &&
549 (getsockname(sock, &sin, &slen) == 0))
551 IO->DNS.SourcePort = ntohs(sin.sin_port);
553 EV_DNS_syslog(LOG_DEBUG, "C-ARES: %s %d|%d Sock %d port %hu\n",
561 IO->Now = ev_now(event_base);
564 if ((IO->DNS.recv_event.fd != sock) &&
565 (IO->DNS.recv_event.fd != 0)) {
566 EV_DNS_LOG_STOP(DNS.recv_event);
567 ev_io_stop(event_base, &IO->DNS.recv_event);
569 IO->DNS.recv_event.fd = sock;
570 ev_io_init(&IO->DNS.recv_event,
572 IO->DNS.recv_event.fd,
574 EV_DNS_LOG_INIT(DNS.recv_event);
575 IO->DNS.recv_event.data = IO;
576 EV_DNS_LOG_START(DNS.recv_event);
577 ev_io_start(event_base, &IO->DNS.recv_event);
580 if ((IO->DNS.send_event.fd != sock) &&
581 (IO->DNS.send_event.fd != 0)) {
582 EV_DNS_LOG_STOP(DNS.send_event);
583 ev_io_stop(event_base, &IO->DNS.send_event);
585 IO->DNS.send_event.fd = sock;
586 ev_io_init(&IO->DNS.send_event,
588 IO->DNS.send_event.fd,
590 IO->DNS.send_event.data = IO;
591 EV_DNS_LOG_INIT(DNS.send_event);
592 EV_DNS_LOG_START(DNS.send_event);
593 ev_io_start(event_base, &IO->DNS.send_event);
595 if ((read == 0) && (write == 0)) {
596 EV_DNS_LOG_STOP(DNS.recv_event);
597 EV_DNS_LOG_STOP(DNS.send_event);
598 ev_io_stop(event_base, &IO->DNS.recv_event);
599 ev_io_stop(event_base, &IO->DNS.send_event);
602 void EnableDebugCAres(const int n)
607 CTDL_MODULE_INIT(c_ares_client)
611 CtdlRegisterDebugFlagHook(HKEY("cares"), EnableDebugCAres, &DebugCAres);
612 int r = ares_library_init(ARES_LIB_INIT_ALL);