]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/c-ares-dns/serv_c-ares-dns.c
Implement Ares Timeouts & error messages
[citadel.git] / citadel / modules / c-ares-dns / serv_c-ares-dns.c
index 6d410efa1064cb0696f7e21ae6369caa47d23db2..8a18b3259e1cd0ed619b18c0fd5c7ac79ee23791 100644 (file)
@@ -69,48 +69,59 @@ static void HostByAddrCb(void *data,
                          struct hostent *hostent) 
 {
        AsyncIO *IO = data;
-       IO->DNSStatus = status;
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+#endif
+       ev_timer_stop (event_base, &IO->dns_timeout);
+
+       IO->DNSQuery->DNSStatus = status;
        if  (status != ARES_SUCCESS) {
-//             ResolveError(*cb, status);
+               StrBufPlain(IO->ErrMsg, ares_strerror(status), -1);
                return;
        }
-       IO->Data = hostent;
+       IO->DNSQuery->Data = hostent;
 /// TODO: howto free this??
 }
 
 static void ParseAnswerA(AsyncIO *IO, unsigned char* abuf, int alen) 
 {
        struct hostent* host;
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+#endif
 
-       if (IO->VParsedDNSReply != NULL)
-               IO->DNSReplyFree(IO->VParsedDNSReply);
-       IO->VParsedDNSReply = NULL;
+       if (IO->DNSQuery->VParsedDNSReply != NULL)
+               IO->DNSQuery->DNSReplyFree(IO->DNSQuery->VParsedDNSReply);
+       IO->DNSQuery->VParsedDNSReply = NULL;
 
-       IO->DNSStatus = ares_parse_a_reply(abuf, alen, &host, NULL, NULL);
-       if (IO->DNSStatus != ARES_SUCCESS) {
-//    ResolveError(arg->js_cb, status);
+       IO->DNSQuery->DNSStatus = ares_parse_a_reply(abuf, alen, &host, NULL, NULL);
+       if (IO->DNSQuery->DNSStatus != ARES_SUCCESS) {
+               StrBufPlain(IO->ErrMsg, ares_strerror(IO->DNSQuery->DNSStatus), -1);
                return;
        }
-       IO->VParsedDNSReply = host;
-       IO->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
+       IO->DNSQuery->VParsedDNSReply = host;
+       IO->DNSQuery->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
 }
 
 
 static void ParseAnswerAAAA(AsyncIO *IO, unsigned char* abuf, int alen) 
 {
        struct hostent* host;
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+#endif
 
-       if (IO->VParsedDNSReply != NULL)
-               IO->DNSReplyFree(IO->VParsedDNSReply);
-       IO->VParsedDNSReply = NULL;
+       if (IO->DNSQuery->VParsedDNSReply != NULL)
+               IO->DNSQuery->DNSReplyFree(IO->DNSQuery->VParsedDNSReply);
+       IO->DNSQuery->VParsedDNSReply = NULL;
 
-       IO->DNSStatus = ares_parse_aaaa_reply(abuf, alen, &host, NULL, NULL);
-       if (IO->DNSStatus != ARES_SUCCESS) {
-//    ResolveError(arg->js_cb, status);
+       IO->DNSQuery->DNSStatus = ares_parse_aaaa_reply(abuf, alen, &host, NULL, NULL);
+       if (IO->DNSQuery->DNSStatus != ARES_SUCCESS) {
+               StrBufPlain(IO->ErrMsg, ares_strerror(IO->DNSQuery->DNSStatus), -1);
                return;
        }
-       IO->VParsedDNSReply = host;
-       IO->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
+       IO->DNSQuery->VParsedDNSReply = host;
+       IO->DNSQuery->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
 }
 
 
@@ -118,93 +129,109 @@ static void ParseAnswerCNAME(AsyncIO *IO, unsigned char* abuf, int alen)
 {
        struct hostent* host;
 
-       if (IO->VParsedDNSReply != NULL)
-               IO->DNSReplyFree(IO->VParsedDNSReply);
-       IO->VParsedDNSReply = NULL;
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+#endif
+
+       if (IO->DNSQuery->VParsedDNSReply != NULL)
+               IO->DNSQuery->DNSReplyFree(IO->DNSQuery->VParsedDNSReply);
+       IO->DNSQuery->VParsedDNSReply = NULL;
 
-       IO->DNSStatus = ares_parse_a_reply(abuf, alen, &host, NULL, NULL);
-       if (IO->DNSStatus != ARES_SUCCESS) {
-//    ResolveError(arg->js_cb, status);
+       IO->DNSQuery->DNSStatus = ares_parse_a_reply(abuf, alen, &host, NULL, NULL);
+       if (IO->DNSQuery->DNSStatus != ARES_SUCCESS) {
+               StrBufPlain(IO->ErrMsg, ares_strerror(IO->DNSQuery->DNSStatus), -1);
                return;
        }
 
        // a CNAME lookup always returns a single record but
-       IO->VParsedDNSReply = host;
-       IO->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
+       IO->DNSQuery->VParsedDNSReply = host;
+       IO->DNSQuery->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
 }
 
 
 static void ParseAnswerMX(AsyncIO *IO, unsigned char* abuf, int alen) 
 {
        struct ares_mx_reply *mx_out;
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+#endif
 
-       if (IO->VParsedDNSReply != NULL)
-               IO->DNSReplyFree(IO->VParsedDNSReply);
-       IO->VParsedDNSReply = NULL;
+       if (IO->DNSQuery->VParsedDNSReply != NULL)
+               IO->DNSQuery->DNSReplyFree(IO->DNSQuery->VParsedDNSReply);
+       IO->DNSQuery->VParsedDNSReply = NULL;
 
-       IO->DNSStatus = ares_parse_mx_reply(abuf, alen, &mx_out);
-       if (IO->DNSStatus != ARES_SUCCESS) {
-//    ResolveError(arg->js_cb, status);
+       IO->DNSQuery->DNSStatus = ares_parse_mx_reply(abuf, alen, &mx_out);
+       if (IO->DNSQuery->DNSStatus != ARES_SUCCESS) {
+               StrBufPlain(IO->ErrMsg, ares_strerror(IO->DNSQuery->DNSStatus), -1);
                return;
        }
 
-       IO->VParsedDNSReply = mx_out;
-       IO->DNSReplyFree = (FreeDNSReply) ares_free_data;
+       IO->DNSQuery->VParsedDNSReply = mx_out;
+       IO->DNSQuery->DNSReplyFree = (FreeDNSReply) ares_free_data;
 }
 
 
 static void ParseAnswerNS(AsyncIO *IO, unsigned char* abuf, int alen) 
 {
        struct hostent* host;
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+#endif
 
-       if (IO->VParsedDNSReply != NULL)
-               IO->DNSReplyFree(IO->VParsedDNSReply);
-       IO->VParsedDNSReply = NULL;
+       if (IO->DNSQuery->VParsedDNSReply != NULL)
+               IO->DNSQuery->DNSReplyFree(IO->DNSQuery->VParsedDNSReply);
+       IO->DNSQuery->VParsedDNSReply = NULL;
 
-       IO->DNSStatus = ares_parse_ns_reply(abuf, alen, &host);
-       if (IO->DNSStatus != ARES_SUCCESS) {
-//    ResolveError(arg->js_cb, status);
+       IO->DNSQuery->DNSStatus = ares_parse_ns_reply(abuf, alen, &host);
+       if (IO->DNSQuery->DNSStatus != ARES_SUCCESS) {
+               StrBufPlain(IO->ErrMsg, ares_strerror(IO->DNSQuery->DNSStatus), -1);
                return;
        }
-       IO->VParsedDNSReply = host;
-       IO->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
+       IO->DNSQuery->VParsedDNSReply = host;
+       IO->DNSQuery->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
 }
 
 
 static void ParseAnswerSRV(AsyncIO *IO, unsigned char* abuf, int alen) 
 {
        struct ares_srv_reply *srv_out;
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+#endif
 
-       if (IO->VParsedDNSReply != NULL)
-               IO->DNSReplyFree(IO->VParsedDNSReply);
-       IO->VParsedDNSReply = NULL;
+       if (IO->DNSQuery->VParsedDNSReply != NULL)
+               IO->DNSQuery->DNSReplyFree(IO->DNSQuery->VParsedDNSReply);
+       IO->DNSQuery->VParsedDNSReply = NULL;
 
-       IO->DNSStatus = ares_parse_srv_reply(abuf, alen, &srv_out);
-       if (IO->DNSStatus != ARES_SUCCESS) {
-//    ResolveError(arg->js_cb, status);
+       IO->DNSQuery->DNSStatus = ares_parse_srv_reply(abuf, alen, &srv_out);
+       if (IO->DNSQuery->DNSStatus != ARES_SUCCESS) {
+               StrBufPlain(IO->ErrMsg, ares_strerror(IO->DNSQuery->DNSStatus), -1);
                return;
        }
 
-       IO->VParsedDNSReply = srv_out;
-       IO->DNSReplyFree = (FreeDNSReply) ares_free_data;
+       IO->DNSQuery->VParsedDNSReply = srv_out;
+       IO->DNSQuery->DNSReplyFree = (FreeDNSReply) ares_free_data;
 }
 
 
 static void ParseAnswerTXT(AsyncIO *IO, unsigned char* abuf, int alen) 
 {
        struct ares_txt_reply *txt_out;
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+#endif
 
-       if (IO->VParsedDNSReply != NULL)
-               IO->DNSReplyFree(IO->VParsedDNSReply);
-       IO->VParsedDNSReply = NULL;
+       if (IO->DNSQuery->VParsedDNSReply != NULL)
+               IO->DNSQuery->DNSReplyFree(IO->DNSQuery->VParsedDNSReply);
+       IO->DNSQuery->VParsedDNSReply = NULL;
 
-       IO->DNSStatus = ares_parse_txt_reply(abuf, alen, &txt_out);
-       if (IO->DNSStatus != ARES_SUCCESS) {
-//    ResolveError(arg->js_cb, status);
+       IO->DNSQuery->DNSStatus = ares_parse_txt_reply(abuf, alen, &txt_out);
+       if (IO->DNSQuery->DNSStatus != ARES_SUCCESS) {
+               StrBufPlain(IO->ErrMsg, ares_strerror(IO->DNSQuery->DNSStatus), -1);
                return;
        }
-       IO->VParsedDNSReply = txt_out;
-       IO->DNSReplyFree = (FreeDNSReply) ares_free_data;
+       IO->DNSQuery->VParsedDNSReply = txt_out;
+       IO->DNSQuery->DNSReplyFree = (FreeDNSReply) ares_free_data;
 }
 
 void QueryCb(void *arg,
@@ -214,20 +241,44 @@ void QueryCb(void *arg,
             int alen) 
 {
        AsyncIO *IO = arg;
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+#endif
+       ev_timer_stop (event_base, &IO->dns_timeout);
 
-       IO->DNSStatus = status;
+       IO->DNSQuery->DNSStatus = status;
        if (status == ARES_SUCCESS)
-               IO->DNS_CB(arg, abuf, alen);
-///    ev_io_stop(event_base, &IO->dns_io_event);
-               
-       IO->PostDNS(IO);
+               IO->DNSQuery->DNS_CB(arg, abuf, alen);
+       else {
+               EV_syslog(LOG_DEBUG, "C-ARES: Failed by: %s error %s\n",
+                         __FUNCTION__,
+                         ares_strerror(status));
+               StrBufPlain(IO->ErrMsg, ares_strerror(status), -1);
+               IO->DNSQuery->DNSStatus = status;
+       }
+       
+       ev_idle_init(&IO->unwind_stack,
+                    IO_postdns_callback);
+       IO->unwind_stack.data = IO;
+       ev_idle_start(event_base, &IO->unwind_stack);
 }
 
-int QueueQuery(ns_type Type, char *name, AsyncIO *IO, IO_CallBack PostDNS)
+void QueryCbDone(AsyncIO *IO)
+{
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+#endif
+
+       ev_idle_stop(event_base, &IO->unwind_stack);
+}
+
+
+void InitC_ares_dns(AsyncIO *IO)
 {
-       int length, family;
-       char address_b[sizeof(struct in6_addr)];
        int optmask = 0;
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s %p\n", __FUNCTION__, IO->DNSChannel);
+#endif
 
        if (IO->DNSChannel == NULL) {
                optmask |= ARES_OPT_SOCK_STATE_CB;
@@ -235,35 +286,121 @@ int QueueQuery(ns_type Type, char *name, AsyncIO *IO, IO_CallBack PostDNS)
                IO->DNSOptions.sock_state_cb_data = IO;
                ares_init_options(&IO->DNSChannel, &IO->DNSOptions, optmask);
        }
+       IO->DNSQuery->DNSStatus = 0;
+}
+
+static void
+DNS_timeouttrigger_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
+{
+       AsyncIO *IO = watcher->data;
+       struct timeval tv, MaxTV;
+       struct timeval *NextTV;
+
+       memset(&MaxTV, 0, sizeof(MaxTV));
+       memset(&tv, 0, sizeof(tv));
+       MaxTV.tv_sec = 30;
+       NextTV = ares_timeout(IO->DNSChannel, &MaxTV, &tv);
+
+       if ((NextTV->tv_sec != MaxTV.tv_sec) ||
+           (NextTV->tv_usec != MaxTV.tv_usec))
+       {
+               fd_set readers, writers;
+#ifdef DEBUG_CARES
+               EV_syslog(LOG_DEBUG, "C-ARES: %s Timeout!\n", __FUNCTION__);
+#endif
+               FD_ZERO(&readers);
+               FD_ZERO(&writers);
+               ares_fds(IO->DNSChannel, &readers, &writers);
+               ares_process(IO->DNSChannel, &readers, &writers);
+       }
+}
+
+void QueueGetHostByNameDone(void *Ctx, 
+                           int status,
+                           int timeouts,
+                           struct hostent *hostent)
+{
+       AsyncIO *IO = (AsyncIO *) Ctx;
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+#endif
+
+       IO->DNSQuery->DNSStatus = status;
+       IO->DNSQuery->VParsedDNSReply = hostent;
+       IO->DNSQuery->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
+
+       ev_idle_init(&IO->unwind_stack,
+                    IO_postdns_callback);
+       IO->unwind_stack.data = IO;
+       ev_idle_start(event_base, &IO->unwind_stack);
+}
+
+void QueueGetHostByName(AsyncIO *IO, const char *Hostname, DNSQueryParts *QueryParts, IO_CallBack PostDNS)
+{
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+       IO->DnsSourcePort = 0;
+#endif
+
+       IO->DNSQuery = QueryParts;
+       IO->DNSQuery->PostDNS = PostDNS;
+
+       InitC_ares_dns(IO);
+
+       ev_timer_init(&IO->dns_timeout, DNS_timeouttrigger_callback, 10, 1);
+       IO->dns_timeout.data = IO;
+       ares_gethostbyname(IO->DNSChannel,
+                          Hostname,   
+                          AF_INET6, /* it falls back to ipv4 in doubt... */
+                          QueueGetHostByNameDone,
+                          IO);
+       ev_timer_start(event_base, &IO->dns_timeout);
+
+}
+int QueueQuery(ns_type Type, const char *name, AsyncIO *IO, DNSQueryParts *QueryParts, IO_CallBack PostDNS)
+{
+       int length, family;
+       char address_b[sizeof(struct in6_addr)];
+
+#ifdef DEBUG_CARES
+       IO->DnsSourcePort = 0;
+#endif
+
+       IO->DNSQuery = QueryParts;
+       IO->DNSQuery->PostDNS = PostDNS;
+
+       InitC_ares_dns(IO);
+
+       ev_timer_init(&IO->dns_timeout, DNS_timeouttrigger_callback, 10, 1);
+       IO->dns_timeout.data = IO;
 
-       IO->PostDNS = PostDNS;
        switch(Type) {
        case ns_t_a:
-               IO->DNS_CB = ParseAnswerA;
+               IO->DNSQuery->DNS_CB = ParseAnswerA;
                break;
 
        case ns_t_aaaa:
-               IO->DNS_CB = ParseAnswerAAAA;
+               IO->DNSQuery->DNS_CB = ParseAnswerAAAA;
                break;
 
        case ns_t_mx:
-               IO->DNS_CB = ParseAnswerMX;
+               IO->DNSQuery->DNS_CB = ParseAnswerMX;
                break;
 
        case ns_t_ns:
-               IO->DNS_CB = ParseAnswerNS;
+               IO->DNSQuery->DNS_CB = ParseAnswerNS;
                break;
 
        case ns_t_txt:
-               IO->DNS_CB = ParseAnswerTXT;
+               IO->DNSQuery->DNS_CB = ParseAnswerTXT;
                break;
 
        case ns_t_srv:
-               IO->DNS_CB = ParseAnswerSRV;
+               IO->DNSQuery->DNS_CB = ParseAnswerSRV;
                break;
 
        case ns_t_cname:
-               IO->DNS_CB = ParseAnswerCNAME;
+               IO->DNSQuery->DNS_CB = ParseAnswerCNAME;
                break;
 
        case ns_t_ptr:
@@ -280,47 +417,100 @@ int QueueQuery(ns_type Type, char *name, AsyncIO *IO, IO_CallBack PostDNS)
                }
 
                ares_gethostbyaddr(IO->DNSChannel, address_b, length, family, HostByAddrCb, IO);
-
+               ev_timer_start(event_base, &IO->dns_timeout);
+#ifdef DEBUG_CARES
+               EV_syslog(LOG_DEBUG, "C-ARES: %s X1\n", __FUNCTION__);
+#endif
                return 1;
 
        default:
+#ifdef DEBUG_CARES
+               EV_syslog(LOG_DEBUG, "C-ARES: %sX2\n", __FUNCTION__);
+#endif
                return 0;
        }
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+#endif
        ares_query(IO->DNSChannel, name, ns_c_in, Type, QueryCb, IO);
+       ev_timer_start(event_base, &IO->dns_timeout);
        return 1;
 }
 
-static void DNS_io_callback(struct ev_loop *loop, ev_io *watcher, int revents)
+
+
+
+
+/*****************************************************************************
+ *                      libev / c-ares integration                           *
+ *****************************************************************************/
+static void DNS_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
 {
        AsyncIO *IO = watcher->data;
        
-       ares_process_fd(IO->DNSChannel, IO->dns_io_event.fd, 0);
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+#endif
+
+       ares_process_fd(IO->DNSChannel, ARES_SOCKET_BAD, IO->dns_send_event.fd);
+}
+static void DNS_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
+{
+       AsyncIO *IO = watcher->data;
+       
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+#endif
+
+       ares_process_fd(IO->DNSChannel, IO->dns_recv_event.fd, ARES_SOCKET_BAD);
 }
 
 void SockStateCb(void *data, int sock, int read, int write) 
 {
-       struct timeval tvbuf, maxtv, *ret;
-       
-       int64_t time = 10;
        AsyncIO *IO = data;
-/* already inside of the event queue. */       
+/* already inside of the event queue. */
+#ifdef DEBUG_CARES
+{
+       struct sockaddr_in sin = {};
+       socklen_t slen;
+       slen = sizeof(sin);     
+       if ((IO->DnsSourcePort == 0) && 
+           (getsockname(sock, &sin, &slen) == 0))
+       {
+               IO->DnsSourcePort = ntohs(sin.sin_port);
+       }
+       EV_syslog(LOG_DEBUG, "C-ARES: %s %d|%d Sock %d port %hu\n",
+                 __FUNCTION__,
+                 read,
+                 write,
+                 sock,
+                 IO->DnsSourcePort);
+}
+#endif
 
-       if ((read == 0) && (write == 0)) {
-//             ev_io_stop(event_base, &IO->dns_io_event);
-       } else if (IO->dns_io_event.fd != sock) {
-               if (IO->dns_io_event.fd != 0) {
-                       ev_io_stop(event_base, &IO->dns_io_event);
+       if (read) {
+               if ((IO->dns_recv_event.fd != sock) &&
+                   (IO->dns_recv_event.fd != 0)) {
+                       ev_io_stop(event_base, &IO->dns_recv_event);
                }
-               IO->dns_io_event.fd = sock;
-               ev_io_init(&IO->dns_io_event, DNS_io_callback, IO->dns_io_event.fd, EV_READ|EV_WRITE);
-               IO->dns_io_event.data = IO;
-
-               ev_io_start(event_base, &IO->dns_io_event);
-       
-               maxtv.tv_sec = time/1000;
-               maxtv.tv_usec = (time % 1000) * 1000;
-               
-               ret = ares_timeout(IO->DNSChannel, &maxtv, &tvbuf);
+               IO->dns_recv_event.fd = sock;
+               ev_io_init(&IO->dns_recv_event, DNS_recv_callback, IO->dns_recv_event.fd, EV_READ);
+               IO->dns_recv_event.data = IO;
+               ev_io_start(event_base, &IO->dns_recv_event);
+       } 
+       if (write) {
+               if ((IO->dns_send_event.fd != sock) &&
+                   (IO->dns_send_event.fd != 0)) {
+                       ev_io_stop(event_base, &IO->dns_send_event);
+               }
+               IO->dns_send_event.fd = sock;
+               ev_io_init(&IO->dns_send_event, DNS_send_callback, IO->dns_send_event.fd, EV_WRITE);
+               IO->dns_send_event.data = IO;
+               ev_io_start(event_base, &IO->dns_send_event);
+       }
+       if ((read == 0) && (write == 0)) {
+               ev_io_stop(event_base, &IO->dns_recv_event);
+               ev_io_stop(event_base, &IO->dns_send_event);
        }
 }