]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/c-ares-dns/serv_c-ares-dns.c
Memleak: in case of DNS errors take care whether we need to free the reply or not.
[citadel.git] / citadel / modules / c-ares-dns / serv_c-ares-dns.c
index e945ccf9f785da5f30de87acbc23942e8a31cd49..8d433268dc0e6c56cc4292e9a263ac40e6259d02 100644 (file)
@@ -1,19 +1,19 @@
 /*
- * Copyright (c) 1998-2009 by the citadel.org team
+ * Copyright (c) 1998-2012 by the citadel.org team
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 3 of the License, or
- *  (at your option) any later version.
+ *  This program is open source software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 3.
+ *  
+ *  
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  
+ *  
+ *  
  *
  *  Inspired by NodeJS.org; thanks for the MX-Parser ;-)
  */
@@ -85,7 +85,7 @@ static void HostByAddrCb(void *data,
 
 static void ParseAnswerA(AsyncIO *IO, unsigned char* abuf, int alen)
 {
-       struct hostent* host;
+       struct hostent* host = NULL;
 #ifdef DEBUG_CARES
        EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
 #endif
@@ -100,6 +100,8 @@ static void ParseAnswerA(AsyncIO *IO, unsigned char* abuf, int alen)
                                                      NULL,
                                                      NULL);
        if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
+               if (host != NULL)
+                       ares_free_hostent(host);
                StrBufPlain(IO->ErrMsg,
                            ares_strerror(IO->DNS.Query->DNSStatus), -1);
                return;
@@ -111,7 +113,7 @@ static void ParseAnswerA(AsyncIO *IO, unsigned char* abuf, int alen)
 
 static void ParseAnswerAAAA(AsyncIO *IO, unsigned char* abuf, int alen)
 {
-       struct hostent* host;
+       struct hostent* host = NULL;
 #ifdef DEBUG_CARES
        EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
 #endif
@@ -126,6 +128,8 @@ static void ParseAnswerAAAA(AsyncIO *IO, unsigned char* abuf, int alen)
                                                         NULL,
                                                         NULL);
        if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
+               if (host != NULL)
+                       ares_free_hostent(host);
                StrBufPlain(IO->ErrMsg,
                            ares_strerror(IO->DNS.Query->DNSStatus), -1);
                return;
@@ -137,7 +141,7 @@ static void ParseAnswerAAAA(AsyncIO *IO, unsigned char* abuf, int alen)
 
 static void ParseAnswerCNAME(AsyncIO *IO, unsigned char* abuf, int alen)
 {
-       struct hostent* host;
+       struct hostent* host = NULL;
 
 #ifdef DEBUG_CARES
        EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
@@ -153,6 +157,8 @@ static void ParseAnswerCNAME(AsyncIO *IO, unsigned char* abuf, int alen)
                                                      NULL,
                                                      NULL);
        if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
+               if (host != NULL)
+                       ares_free_hostent(host);
                StrBufPlain(IO->ErrMsg,
                            ares_strerror(IO->DNS.Query->DNSStatus), -1);
                return;
@@ -166,7 +172,7 @@ static void ParseAnswerCNAME(AsyncIO *IO, unsigned char* abuf, int alen)
 
 static void ParseAnswerMX(AsyncIO *IO, unsigned char* abuf, int alen)
 {
-       struct ares_mx_reply *mx_out;
+       struct ares_mx_reply *mx_out = NULL;
 #ifdef DEBUG_CARES
        EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
 #endif
@@ -177,6 +183,8 @@ static void ParseAnswerMX(AsyncIO *IO, unsigned char* abuf, int alen)
 
        IO->DNS.Query->DNSStatus = ares_parse_mx_reply(abuf, alen, &mx_out);
        if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
+               if (mx_out != NULL)
+                       ares_free_data(mx_out);
                StrBufPlain(IO->ErrMsg,
                            ares_strerror(IO->DNS.Query->DNSStatus), -1);
                return;
@@ -189,7 +197,7 @@ static void ParseAnswerMX(AsyncIO *IO, unsigned char* abuf, int alen)
 
 static void ParseAnswerNS(AsyncIO *IO, unsigned char* abuf, int alen)
 {
-       struct hostent* host;
+       struct hostent* host = NULL;
 #ifdef DEBUG_CARES
        EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
 #endif
@@ -200,6 +208,8 @@ static void ParseAnswerNS(AsyncIO *IO, unsigned char* abuf, int alen)
 
        IO->DNS.Query->DNSStatus = ares_parse_ns_reply(abuf, alen, &host);
        if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
+               if (host != NULL)
+                       ares_free_hostent(host);
                StrBufPlain(IO->ErrMsg,
                            ares_strerror(IO->DNS.Query->DNSStatus), -1);
                return;
@@ -211,7 +221,7 @@ static void ParseAnswerNS(AsyncIO *IO, unsigned char* abuf, int alen)
 
 static void ParseAnswerSRV(AsyncIO *IO, unsigned char* abuf, int alen)
 {
-       struct ares_srv_reply *srv_out;
+       struct ares_srv_reply *srv_out = NULL;
 #ifdef DEBUG_CARES
        EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
 #endif
@@ -222,6 +232,8 @@ static void ParseAnswerSRV(AsyncIO *IO, unsigned char* abuf, int alen)
 
        IO->DNS.Query->DNSStatus = ares_parse_srv_reply(abuf, alen, &srv_out);
        if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
+               if (srv_out != NULL)
+                       ares_free_data(srv_out);
                StrBufPlain(IO->ErrMsg,
                            ares_strerror(IO->DNS.Query->DNSStatus), -1);
                return;
@@ -245,6 +257,8 @@ static void ParseAnswerTXT(AsyncIO *IO, unsigned char* abuf, int alen)
 
        IO->DNS.Query->DNSStatus = ares_parse_txt_reply(abuf, alen, &txt_out);
        if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
+               if (txt_out != NULL)
+                       ares_free_data(txt_out);
                StrBufPlain(IO->ErrMsg,
                            ares_strerror(IO->DNS.Query->DNSStatus), -1);
                return;
@@ -297,6 +311,16 @@ void QueryCbDone(AsyncIO *IO)
 
 void DestructCAres(AsyncIO *IO)
 {
+#ifdef DEBUG_CARES
+       EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
+       EV_DNS_LOGT_STOP(DNS.timeout);
+#endif
+       EV_DNS_LOG_STOP(DNS.recv_event);
+       ev_io_stop(event_base, &IO->DNS.recv_event);
+       EV_DNS_LOG_STOP(DNS.send_event);
+       ev_io_stop(event_base, &IO->DNS.send_event);
+       ev_timer_stop (event_base, &IO->DNS.timeout);
+       ev_idle_stop(event_base, &IO->unwind_stack);
        ares_destroy_options(&IO->DNS.Options);
 }
 
@@ -363,6 +387,7 @@ void QueueGetHostByNameDone(void *Ctx,
        EV_DNS_LOGT_INIT(unwind_stack);
        EV_DNS_LOGT_START(unwind_stack);
        ev_idle_start(event_base, &IO->unwind_stack);
+       ev_timer_stop (event_base, &IO->DNS.timeout);
 }
 
 void QueueGetHostByName(AsyncIO *IO,