Libev/libc-ares migration: cleanup
authorWilfried Goesgens <dothebart@citadel.org>
Sat, 8 Jan 2011 17:32:56 +0000 (18:32 +0100)
committerWilfried Goesgens <dothebart@citadel.org>
Sat, 8 Jan 2011 17:32:56 +0000 (18:32 +0100)
citadel/event_client.c
citadel/event_client.h
citadel/modules/c-ares-dns/serv_c-ares-dns.c
citadel/modules/eventclient/serv_eventclient.c
citadel/modules/smtp/serv_smtpeventclient.c

index 2ffc1c6e5e1970759d84326eab57b3ce2558b759..1d1ca925c722663a33d19566cfcf0ea08a511394 100644 (file)
@@ -121,6 +121,8 @@ void FreeAsyncIOContents(AsyncIO *IO)
        FreeStrBuf(&IO->IOBuf);
        FreeStrBuf(&IO->SendBuf.Buf);
        FreeStrBuf(&IO->RecvBuf.Buf);
+       FreeStrBuf(&IO->ErrMsg);
+       ares_destroy(IO->DNSChannel);
 
 }
 
@@ -355,11 +357,11 @@ set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
 
 int event_connect_socket(AsyncIO *IO)
 {
+       struct sockaddr_in  saddr;
        int fdflags; 
        int rc = -1;
 
-       IO->SendBuf.fd = 
-               IO->RecvBuf.fd = 
+       IO->SendBuf.fd = IO->RecvBuf.fd = 
                IO->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
 /*
 IO->curr_ai->ai_family, 
@@ -397,8 +399,6 @@ IO->curr_ai->ai_family,
        ev_io_init(&IO->send_event, IO_send_callback, IO->sock, EV_WRITE);
        IO->send_event.data = IO;
        
-       unsigned short dport = atoi("25"); ///todo
-       struct sockaddr_in  saddr;
        memset( (struct sockaddr_in *)&saddr, '\0', sizeof( saddr ) );
 
        memcpy(&saddr.sin_addr, 
@@ -407,7 +407,7 @@ IO->curr_ai->ai_family,
 
 //     saddr.sin_addr.s_addr = inet_addr("85.88.5.80");
        saddr.sin_family = AF_INET;
-       saddr.sin_port = htons(dport);/// TODO
+       saddr.sin_port = htons(IO->dport);
        rc = connect(IO->sock, 
                     (struct sockaddr *) &saddr,
 /// TODO: ipv6??                    (IO->HEnt->h_addrtype == AF_INET6)?
@@ -453,7 +453,7 @@ void InitEventIO(AsyncIO *IO,
        else {
                IO->NextState = eSendReply;
        }
-//     IO->IP6 = IO->HEnt->h_addrtype == AF_INET6;
+       IO->IP6 = IO->HEnt->h_addrtype == AF_INET6;
 //     IO->res = HEnt->h_addr_list[0];
        event_connect_socket(IO);
 }
index 6032a3c17167e0ce11b35eb3fe520d562bf250a4..cfb8d031beb42c1d36ac6759c75000ce9a84c6de 100644 (file)
@@ -33,6 +33,7 @@ struct AsyncIO {
        int IP6;
        struct hostent *HEnt;
        int sock;
+       unsigned short dport;
        int active_event;
                eNextState NextState;
        ev_io recv_event, 
index ec55ed9e0c1cc4150e5a32fb138312534475547b..6d410efa1064cb0696f7e21ae6369caa47d23db2 100644 (file)
@@ -59,8 +59,6 @@
 
 
 extern struct ev_loop *event_base;
-struct ares_options options;
-ares_channel Channel;
 
 void SockStateCb(void *data, int sock, int read, int write);
 
@@ -330,22 +328,12 @@ CTDL_MODULE_INIT(c_ares_client)
 {
        if (!threading)
        {
-               int optmask = 0;
-               
-
                int r = ares_library_init(ARES_LIB_INIT_ALL);
                if (0 != r) {
                        // TODO
                        // ThrowException(Exception::Error(String::New(ares_strerror(r))));
 ////                   assert(r == 0);
                }
-
-               optmask |= ARES_OPT_SOCK_STATE_CB;
-               memset(&options, 0, sizeof(struct ares_options));
-               options.sock_state_cb = SockStateCb;
-               
-               ares_init_options(&Channel, &options, optmask);
-
        }
        return "c-ares";
 }
index d135dccf6c4547f668aef00778ab0e1fa25a9077..a200754dc8a10f5821e8419bc30c5b425538a6b1 100644 (file)
@@ -176,7 +176,14 @@ void *client_event_thread(void *arg)
        ev_loop (event_base, 0);
        CtdlClearSystemContext();
        ev_default_destroy ();
+       
+       DeleteHash(&QueueEvents);
+       InboundEventQueue = NULL;
+       DeleteHash(&InboundEventQueues[0]);
+       DeleteHash(&InboundEventQueues[1]);
        citthread_mutex_destroy(&EventQueueMutex);
+
+
        return(NULL);
 }
 
index 16b45cb774b1449408df1188f3d05a9cca9696e9..b89f875f024e97216f3f70114b61ffcb46d4a539 100644 (file)
@@ -198,7 +198,6 @@ typedef struct _stmp_out_msg {
 
        struct hostent *OneMX;
 
-
        char mx_user[1024];
        char mx_pass[1024];
        StrBuf *msgtext;
@@ -211,6 +210,8 @@ typedef struct _stmp_out_msg {
 void DeleteSmtpOutMsg(void *v)
 {
        SmtpOutMsg *Msg = v;
+
+       ares_free_data(Msg->AllMX);
        FreeStrBuf(&Msg->msgtext);
        FreeAsyncIOContents(&Msg->IO);
        free(Msg);
@@ -569,64 +570,6 @@ int smtp_resolve_recipients(SmtpOutMsg *SendMsg)
 #define SMTP_DBG_SEND() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: > %s\n", SendMsg->n, ChrPtr(SendMsg->IO.IOBuf))
 #define SMTP_DBG_READ() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: < %s\n", SendMsg->n, ChrPtr(SendMsg->IO.IOBuf))
 
-/*
-void connect_one_smtpsrv_xamine_result(void *Ctx, 
-                                      int status,
-                                      int timeouts,
-                                      struct hostent *hostent)
-{
-       SmtpOutMsg *SendMsg = Ctx;
-
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: connecting [%s:%s]!\n", 
-                     SendMsg->n, SendMsg->mx_host, SendMsg->mx_port);
-
-       SendMsg->IO.SendBuf.Buf = NewStrBufPlain(NULL, 1024);
-       SendMsg->IO.RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
-       SendMsg->IO.IOBuf = NewStrBuf();
-       SendMsg->IO.ErrMsg = SendMsg->MyQEntry->StatusMessage;
-
-
-       SendMsg->IO.SendBuf.fd = 
-       SendMsg->IO.RecvBuf.fd = 
-       SendMsg->IO.sock = sock_connect(SendMsg->mx_host, SendMsg->mx_port);
-
-       StrBufPrintf(SendMsg->MyQEntry->StatusMessage, 
-                    "Could not connect: %s", strerror(errno));
-
-
-       if (SendMsg->IO.sock < 0) {
-               if (errno > 0) {
-                       StrBufPlain(SendMsg->MyQEntry->StatusMessage, 
-                                   strerror(errno), -1);
-               }
-               else {
-                       StrBufPrintf(SendMsg->MyQEntry->StatusMessage, 
-                                    "Unable to connect to %s : %s\n", 
-                                    SendMsg->mx_host, SendMsg->mx_port);
-               }
-       }
-       /// hier: naechsten mx ausprobieren.
-       if (SendMsg->IO.sock < 0) {
-               SendMsg->MyQEntry->Status = 4;  /* dsn is already filled in * /
-               //// hier: abbrechen & bounce.
-               return;
-       }
-/*
-
-       InitEventIO(&SendMsg->IO, SendMsg, 
-                   SMTP_C_DispatchReadDone, 
-                   SMTP_C_DispatchWriteDone, 
-                   SMTP_C_Terminate,
-                   SMTP_C_Timeout,
-                   SMTP_C_ConnFail,
-                   SMTP_C_MXLookup,
-                   SMTP_C_ReadServerStatus,
-                   1);
-* /
-       return;
-}
-*/
-
 void get_one_mx_host_name_done(void *Ctx, 
                               int status,
                               int timeouts,
@@ -636,26 +579,26 @@ void get_one_mx_host_name_done(void *Ctx,
        SmtpOutMsg *SendMsg = IO->Data;
        if ((status == ARES_SUCCESS) && (hostent != NULL) ) {
 
-                       SendMsg->IO.HEnt = hostent;
-                       InitEventIO(IO, SendMsg, 
-                                   SMTP_C_DispatchReadDone, 
-                                   SMTP_C_DispatchWriteDone, 
-                                   SMTP_C_Terminate,
-                                   SMTP_C_Timeout,
-                                   SMTP_C_ConnFail,
-                                   SMTP_C_ReadServerStatus,
-                                   1);
+               SendMsg->IO.HEnt = hostent;
+               InitEventIO(IO, SendMsg, 
+                           SMTP_C_DispatchReadDone, 
+                           SMTP_C_DispatchWriteDone, 
+                           SMTP_C_Terminate,
+                           SMTP_C_Timeout,
+                           SMTP_C_ConnFail,
+                           SMTP_C_ReadServerStatus,
+                           1);
 
        }
 }
 
-const char *DefaultMXPort = "25";
+const unsigned short DefaultMXPort = 25;
 void connect_one_smtpsrv(SmtpOutMsg *SendMsg)
 {
        //char *endpart;
        //char buf[SIZ];
 
-       SendMsg->mx_port = DefaultMXPort;
+       SendMsg->IO.dport = DefaultMXPort;
 
 
 /* TODO: Relay!
@@ -684,10 +627,10 @@ void connect_one_smtpsrv(SmtpOutMsg *SendMsg)
        SendMsg->CurrMX = SendMsg->CurrMX->next;
 
        CtdlLogPrintf(CTDL_DEBUG, 
-                     "SMTP client[%ld]: connecting to %s : %s ...\n", 
+                     "SMTP client[%ld]: connecting to %s : %d ...\n", 
                      SendMsg->n, 
                      SendMsg->mx_host, 
-                     SendMsg->mx_port);
+                     SendMsg->IO.dport);
 
        ares_gethostbyname(SendMsg->IO.DNSChannel,
                           SendMsg->mx_host,   
@@ -976,22 +919,7 @@ eNextState smtp_resolve_mx_done(void *data)
 int resolve_mx_records(void *Ctx)
 {
        SmtpOutMsg * SendMsg = Ctx;
-/*//TMP
-       SendMsg->IO.SendBuf.Buf = NewStrBufPlain(NULL, 1024);
-       SendMsg->IO.RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
-       SendMsg->IO.IOBuf = NewStrBuf();
-       SendMsg->IO.ErrMsg = SendMsg->MyQEntry->StatusMessage;
 
-       InitEventIO(&SendMsg->IO, SendMsg, 
-                                   SMTP_C_DispatchReadDone, 
-                                   SMTP_C_DispatchWriteDone, 
-                                   SMTP_C_Terminate,
-                                   SMTP_C_Timeout,
-                                   SMTP_C_ConnFail,
-                                   SMTP_C_ReadServerStatus,
-                                   1);
-                                   return 0;
-/// END TMP */
        if (!QueueQuery(ns_t_mx, 
                        SendMsg->node, 
                        &SendMsg->IO, 
@@ -1029,8 +957,6 @@ void smtp_try(OneQueItem *MyQItem,
        QueueEventContext(SendMsg, 
                          &SendMsg->IO,
                          resolve_mx_records);
-
-
 }
 
 
@@ -1399,6 +1325,12 @@ eNextState SMTP_C_DispatchWriteDone(void *Data)
        
 }
 
+void smtp_evc_cleanup(void)
+{
+       DeleteHash(&QItemHandlers);
+       DeleteHash(&ActiveQItems);
+}
+
 #endif
 CTDL_MODULE_INIT(smtp_eventclient)
 {
@@ -1420,6 +1352,8 @@ CTDL_MODULE_INIT(smtp_eventclient)
 
 
                smtp_init_spoolout();
+
+               CtdlRegisterCleanupHook(smtp_evc_cleanup);
                CtdlThreadCreate("SMTPEvent Send", CTDLTHREAD_BIGSTACK, smtp_queue_thread, NULL);
 
                CtdlRegisterProtoHook(cmd_smtp, "SMTP", "SMTP utility commands");