Networker: send fail message in DB queue; add flood protection.
[citadel.git] / citadel / modules / network / serv_networkclient.c
index 7f4ca89d11a3f3f873d5dade7724d1157784766d..996d4e042bc72ad92ca1772bbdec2f49bc541638 100644 (file)
@@ -2,7 +2,7 @@
  * This module handles shared rooms, inter-Citadel mail, and outbound
  * mailing list processing.
  *
- * Copyright (c) 2000-2011 by the citadel.org team
+ * Copyright (c) 2000-2012 by the citadel.org team
  *
  *  This program is open source software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 
 struct CitContext networker_client_CC;
 
+#define NODE ChrPtr(((AsyncNetworker*)IO->Data)->node)
+#define N ((AsyncNetworker*)IO->Data)->n
+
+int NetworkClientDebugEnabled = 0;
+
+#define NCDBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (NetworkClientDebugEnabled != 0))
+
+#define EVN_syslog(LEVEL, FORMAT, ...)                                 \
+       NCDBGLOG(LEVEL) syslog(LEVEL,                                   \
+                              "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT,       \
+                              IO->ID, CCID, NODE, N, __VA_ARGS__)
+
+#define EVNM_syslog(LEVEL, FORMAT)                                     \
+       NCDBGLOG(LEVEL) syslog(LEVEL,                                   \
+                              "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT,       \
+                              IO->ID, CCID, NODE, N)
+
+#define EVNCS_syslog(LEVEL, FORMAT, ...) \
+       NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT,      \
+                              IO->ID, NODE, N, __VA_ARGS__)
+
+#define EVNCSM_syslog(LEVEL, FORMAT) \
+       NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT,      \
+                              IO->ID, NODE, N)
+
+
 typedef enum _eNWCState {
        eeGreating,
        eAuth,
@@ -135,14 +161,38 @@ void DeleteNetworker(void *vptr)
        FreeStrBuf(&NW->port);
        FreeStrBuf(&NW->secret);
        FreeStrBuf(&NW->Url);
+       FreeStrBuf(&NW->IO.ErrMsg);
        FreeAsyncIOContents(&NW->IO);
+       if (NW->HostLookup.VParsedDNSReply != NULL) {
+               NW->HostLookup.DNSReplyFree(NW->HostLookup.VParsedDNSReply);
+               NW->HostLookup.VParsedDNSReply = NULL;
+       }
        free(NW);
 }
 
-#define NWC_DBG_SEND() syslog(LOG_DEBUG, "NW client[%ld]: > %s", NW->n, ChrPtr(NW->IO.SendBuf.Buf))
-#define NWC_DBG_READ() syslog(LOG_DEBUG, "NW client[%ld]: < %s\n", NW->n, ChrPtr(NW->IO.IOBuf))
+#define NWC_DBG_SEND() EVN_syslog(LOG_DEBUG, ": > %s", ChrPtr(NW->IO.SendBuf.Buf))
+#define NWC_DBG_READ() EVN_syslog(LOG_DEBUG, ": < %s\n", ChrPtr(NW->IO.IOBuf))
 #define NWC_OK (strncasecmp(ChrPtr(NW->IO.IOBuf), "+OK", 3) == 0)
 
+eNextState SendFailureMessage(AsyncIO *IO)
+{
+       AsyncNetworker *NW = IO->Data;
+       long lens[2];
+       const char *strs[2];
+
+       strs[0] = ChrPtr(NW->node);
+       lens[0] = StrLength(NW->node);
+       
+       strs[1] = ChrPtr(NW->IO.ErrMsg);
+       lens[1] = StrLength(NW->IO.ErrMsg);
+       CtdlAideFPMessage(
+               ChrPtr(NW->IO.ErrMsg),
+               "Networker error",
+               2, strs, (long*) &lens);
+       
+       return eAbort;
+}
+
 eNextState FinalizeNetworker(AsyncIO *IO)
 {
        AsyncNetworker *NW = (AsyncNetworker *)IO->Data;
@@ -156,24 +206,28 @@ eNextState FinalizeNetworker(AsyncIO *IO)
 eNextState NWC_ReadGreeting(AsyncNetworker *NW)
 {
        char connected_to[SIZ];
+       AsyncIO *IO = &NW->IO;
        NWC_DBG_READ();
        /* Read the server greeting */
        /* Check that the remote is who we think it is and warn the Aide if not */
        extract_token (connected_to, ChrPtr(NW->IO.IOBuf), 1, ' ', sizeof connected_to);
        if (strcmp(connected_to, ChrPtr(NW->node)) != 0)
        {
+               if (NW->IO.ErrMsg == NULL)
+                       NW->IO.ErrMsg = NewStrBuf();
                StrBufPrintf(NW->IO.ErrMsg,
                             "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
                             connected_to, ChrPtr(NW->node));
-               syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
-               CtdlAideMessage(ChrPtr(NW->IO.ErrMsg), "Network error");
-               return eAbort;/// todo: aide message in anderer queue speichern
+               EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
+               StopClientWatchers(IO, 1);
+               return QueueDBOperation(IO, SendFailureMessage);
        }
        return eSendReply;
 }
 
 eNextState NWC_SendAuth(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        /* We're talking to the correct node.  Now identify ourselves. */
        StrBufPrintf(NW->IO.SendBuf.Buf, "NETP %s|%s\n", 
                     config.c_nodename, 
@@ -184,6 +238,7 @@ eNextState NWC_SendAuth(AsyncNetworker *NW)
 
 eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        NWC_DBG_READ();
        if (ChrPtr(NW->IO.IOBuf)[0] == '2')
        {
@@ -191,33 +246,46 @@ eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
        }
        else
        {
+               int Error = atol(ChrPtr(NW->IO.IOBuf));
+               if (NW->IO.ErrMsg == NULL)
+                       NW->IO.ErrMsg = NewStrBuf();
                StrBufPrintf(NW->IO.ErrMsg,
-                            "Connected to node \"%s\" but my secret wasn't accurate.",
-                            ChrPtr(NW->node));
-               syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
-               CtdlAideMessage(ChrPtr(NW->IO.ErrMsg), "Network error");
-               
+                            "Connected to node \"%s\" but my secret wasn't accurate.\nReason was:%s\n",
+                            ChrPtr(NW->node), ChrPtr(NW->IO.IOBuf) + 4);
+               if (Error == 552) {
+                       EVN_syslog(LOG_INFO,
+                                  "Already talking to %s; skipping this time.\n",
+                                  ChrPtr(NW->node));
+                       
+               }
+               else {
+                       EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
+                       StopClientWatchers(IO, 1);
+                       return QueueDBOperation(IO, SendFailureMessage);
+               }
                return eAbort;
        }
 }
 
 eNextState NWC_SendNDOP(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        NW->tempFileName = NewStrBuf();
        NW->SpoolFileName = NewStrBuf();
-       StrBufPrintf(NW->tempFileName, 
+       StrBufPrintf(NW->SpoolFileName,
                     "%s/%s.%lx%x",
                     ctdl_netin_dir,
                     ChrPtr(NW->node),
                     time(NULL),// TODO: get time from libev
                     rand());
-       StrBufPrintf(NW->SpoolFileName, 
+       StrBufStripSlashes(NW->SpoolFileName, 1);
+       StrBufPrintf(NW->tempFileName, 
                     "%s/%s.%lx%x",
                     ctdl_nettmp_dir,
                     ChrPtr(NW->node),
                     time(NULL),// TODO: get time from libev
                     rand());
-
+       StrBufStripSlashes(NW->tempFileName, 1);
        /* We're talking to the correct node.  Now identify ourselves. */
        StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NDOP\n"));
        NWC_DBG_SEND();
@@ -226,6 +294,7 @@ eNextState NWC_SendNDOP(AsyncNetworker *NW)
 
 eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        int TotalSendSize;
        NWC_DBG_READ();
        if (ChrPtr(NW->IO.IOBuf)[0] == '2')
@@ -233,20 +302,20 @@ eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
 
                NW->IO.IOB.TotalSentAlready = 0;
                TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
-               syslog(LOG_DEBUG, "Expecting to transfer %ld bytes\n", NW->IO.IOB.TotalSendSize);
+               EVN_syslog(LOG_DEBUG, "Expecting to transfer %d bytes\n", TotalSendSize);
                if (TotalSendSize <= 0) {
                        NW->State = eNUOP - 1;
                }
                else {
                        int fd;
-                       fd = open(ChrPtr(NW->SpoolFileName), 
+                       fd = open(ChrPtr(NW->tempFileName), 
                                  O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY, 
                                  S_IRUSR|S_IWUSR);
                        if (fd < 0)
                        {
-                               syslog(LOG_CRIT,
+                               EVN_syslog(LOG_CRIT,
                                       "cannot open %s: %s\n", 
-                                      ChrPtr(NW->SpoolFileName), 
+                                      ChrPtr(NW->tempFileName), 
                                       strerror(errno));
 
                                NW->State = eQUIT - 1;
@@ -264,6 +333,7 @@ eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
 
 eNextState NWC_SendREAD(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        eNextState rc;
 
        if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
@@ -276,6 +346,7 @@ eNextState NWC_SendREAD(AsyncNetworker *NW)
                {
                        FDIOBufferDelete(&NW->IO.IOB);
                        unlink(ChrPtr(NW->tempFileName));
+                       FDIOBufferDelete(&IO->IOB);
                        return eAbort;
                }
                StrBufPrintf(NW->IO.SendBuf.Buf, "READ %ld|%ld\n",
@@ -302,6 +373,7 @@ eNextState NWC_SendREAD(AsyncNetworker *NW)
 
 eNextState NWC_ReadREADState(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        NWC_DBG_READ();
        if (ChrPtr(NW->IO.IOBuf)[0] == '6')
        {
@@ -309,20 +381,23 @@ eNextState NWC_ReadREADState(AsyncNetworker *NW)
                        NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
                return eReadFile;
        }
+       FDIOBufferDelete(&IO->IOB);
        return eAbort;
 }
 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
 {
+       eNextState rc;
+       AsyncIO *IO = &NW->IO;
        NWC_DBG_READ();
        if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
        {
                NW->State ++;
 
                FDIOBufferDelete(&NW->IO.IOB);
-               
-               if (link(ChrPtr(NW->SpoolFileName), ChrPtr(NW->tempFileName)) != 0) {
-                       syslog(LOG_ALERT, 
+
+               if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
+                       EVN_syslog(LOG_ALERT, 
                               "Could not link %s to %s: %s\n",
                               ChrPtr(NW->tempFileName), 
                               ChrPtr(NW->SpoolFileName), 
@@ -330,26 +405,29 @@ eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
                }
        
                unlink(ChrPtr(NW->tempFileName));
-               return NWC_DispatchWriteDone(&NW->IO);
+               rc = NWC_DispatchWriteDone(&NW->IO);
+               NW->State --;
+               return rc;
        }
        else {
                NW->State --;
                NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
-               return NWC_DispatchWriteDone(&NW->IO);
+               return eSendReply; //NWC_DispatchWriteDone(&NW->IO);
        }
 }
 
 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
 {
-       NWC_DBG_READ();
-       if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
+       eNextState rc;
+       AsyncIO *IO = &NW->IO;
+/* we don't have any data to debug print here. */
+       if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
        {
                NW->State ++;
 
                FDIOBufferDelete(&NW->IO.IOB);
-               
-               if (link(ChrPtr(NW->SpoolFileName), ChrPtr(NW->tempFileName)) != 0) {
-                       syslog(LOG_ALERT, 
+               if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
+                       EVN_syslog(LOG_ALERT, 
                               "Could not link %s to %s: %s\n",
                               ChrPtr(NW->tempFileName), 
                               ChrPtr(NW->SpoolFileName), 
@@ -357,7 +435,9 @@ eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
                }
        
                unlink(ChrPtr(NW->tempFileName));
-               return NWC_DispatchWriteDone(&NW->IO);
+               rc = NWC_DispatchWriteDone(&NW->IO);
+               NW->State --;
+               return rc;
        }
        else {
                NW->State --;
@@ -367,6 +447,7 @@ eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
 }
 eNextState NWC_SendCLOS(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
        NWC_DBG_SEND();
        return eSendReply;
@@ -374,7 +455,9 @@ eNextState NWC_SendCLOS(AsyncNetworker *NW)
 
 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        NWC_DBG_READ();
+       FDIOBufferDelete(&IO->IOB);
        if (ChrPtr(NW->IO.IOBuf)[0] != '2')
                return eTerminateConnection;
        return eSendReply;
@@ -383,42 +466,47 @@ eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
 
 eNextState NWC_SendNUOP(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        eNextState rc;
        long TotalSendSize;
        struct stat statbuf;
        int fd;
 
-       StrBufPrintf(NW->tempFileName,
+       StrBufPrintf(NW->SpoolFileName,
                     "%s/%s",
                     ctdl_netout_dir,
                     ChrPtr(NW->node));
-       fd = open(ChrPtr(NW->tempFileName), O_RDONLY);
+       StrBufStripSlashes(NW->SpoolFileName, 1);
+
+       fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
        if (fd < 0) {
                if (errno != ENOENT) {
-                       syslog(LOG_CRIT,
+                       EVN_syslog(LOG_CRIT,
                               "cannot open %s: %s\n", 
-                              ChrPtr(NW->tempFileName), 
+                              ChrPtr(NW->SpoolFileName), 
                               strerror(errno));
                }
                NW->State = eQUIT;
                rc = NWC_SendQUIT(NW);
                NWC_DBG_SEND();
+               return rc;
        }
 
        if (fstat(fd, &statbuf) == -1) {
-               syslog(9, "FSTAT FAILED %s [%s]--\n", 
-                      ChrPtr(NW->tempFileName), 
-                      strerror(errno));
+               EVN_syslog(LOG_CRIT, "FSTAT FAILED %s [%s]--\n", 
+                          ChrPtr(NW->SpoolFileName), 
+                          strerror(errno));
                if (fd > 0) close(fd);
                return eAbort;
        }
        TotalSendSize = statbuf.st_size;
        if (TotalSendSize == 0) {
-               syslog(LOG_DEBUG,
+               EVNM_syslog(LOG_DEBUG,
                       "Nothing to send.\n");
                NW->State = eQUIT;
                rc = NWC_SendQUIT(NW);
                NWC_DBG_SEND();
+               if (fd > 0) close(fd);
                return rc;
        }
        FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
@@ -430,14 +518,18 @@ eNextState NWC_SendNUOP(AsyncNetworker *NW)
 }
 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        NWC_DBG_READ();
-       if (ChrPtr(NW->IO.IOBuf)[0] != '2')
+       if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
+               FDIOBufferDelete(&IO->IOB);
                return eAbort;
+       }
        return eSendReply;
 }
 
 eNextState NWC_SendWRIT(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT %ld\n", 
                     NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
        NWC_DBG_SEND();
@@ -445,9 +537,11 @@ eNextState NWC_SendWRIT(AsyncNetworker *NW)
 }
 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        NWC_DBG_READ();
        if (ChrPtr(NW->IO.IOBuf)[0] != '7')
        {
+               FDIOBufferDelete(&IO->IOB);
                return eAbort;
        }
 
@@ -458,25 +552,29 @@ eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
 
 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        eNextState rc;
-       if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
+       if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
        {
                NW->State ++;
 
-               FDIOBufferDelete(&NW->IO.IOB);
-               rc =  NWC_DispatchWriteDone(&NW->IO);
+               FDIOBufferDelete(&IO->IOB);
+               rc =  NWC_DispatchWriteDone(IO);
                NW->State --;
                return rc;
        }
        else {
                NW->State --;
-               NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
-               return NWC_DispatchWriteDone(&NW->IO);
+               IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
+               rc = NWC_DispatchWriteDone(IO);
+               NW->State --;
+               return rc;
        }
 }
 
 eNextState NWC_SendUCLS(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
        NWC_DBG_SEND();
        return eSendReply;
@@ -484,18 +582,21 @@ eNextState NWC_SendUCLS(AsyncNetworker *NW)
 }
 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        NWC_DBG_READ();
 
-       syslog(LOG_NOTICE, "Sent %ld octets to <%s>\n", NW->IO.IOB.ChunkSize, ChrPtr(NW->node));
+       EVN_syslog(LOG_NOTICE, "Sent %ld octets to <%s>\n", NW->IO.IOB.ChunkSize, ChrPtr(NW->node));
        if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
-               syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->tempFileName));
-               unlink(ChrPtr(NW->tempFileName));
+               EVN_syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
+               unlink(ChrPtr(NW->SpoolFileName));
        }
+       FDIOBufferDelete(&IO->IOB);
        return eSendReply;
 }
 
 eNextState NWC_SendQUIT(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
 
        NWC_DBG_SEND();
@@ -504,6 +605,7 @@ eNextState NWC_SendQUIT(AsyncNetworker *NW)
 
 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        NWC_DBG_READ();
 
        return eAbort;
@@ -621,16 +723,14 @@ eNextState nwc_get_one_host_ip(AsyncIO *IO)
         * here we start with the lookup of one host.
         */ 
 
-       InitC_ares_dns(IO);
-
-       syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
+       EVN_syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
 
-       syslog(LOG_DEBUG, 
-                     "NWC client[%ld]: looking up %s-Record %s : %d ...\n", 
-                     NW->n, 
-                     (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
-                     NW->IO.ConnectMe->Host, 
-                     NW->IO.ConnectMe->Port);
+       EVN_syslog(LOG_DEBUG, 
+                  "NWC client[%ld]: looking up %s-Record %s : %d ...\n", 
+                  NW->n, 
+                  (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
+                  NW->IO.ConnectMe->Host, 
+                  NW->IO.ConnectMe->Port);
 
        QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a, 
                   NW->IO.ConnectMe->Host, 
@@ -675,14 +775,16 @@ eReadState NWC_ReadServerStatus(AsyncIO *IO)
 
 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
 {
-       return eAbort;
+       StopClientWatchers(IO, 1);
+       return QueueDBOperation(IO, SendFailureMessage);
 }
 
 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
 {
+       AsyncIO *IO = &NW->IO;
        double Timeout = 0.0;
 
-       syslog(LOG_DEBUG, "NWC3: %s\n", __FUNCTION__);
+       EVN_syslog(LOG_DEBUG, "%s - %d\n", __FUNCTION__, NextTCPState);
 
        switch (NextTCPState) {
        case eSendReply:
@@ -707,13 +809,20 @@ void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
        case eReadMore://// TODO
                return;
        }
-       SetNextTimeout(&NW->IO, Timeout);
+       if (Timeout > 0) {
+               EVN_syslog(LOG_DEBUG, 
+                          "%s - %d %f\n",
+                          __FUNCTION__,
+                          NextTCPState,
+                          Timeout);
+               SetNextTimeout(&NW->IO, Timeout*100);
+       }
 }
 
 
 eNextState NWC_DispatchReadDone(AsyncIO *IO)
 {
-       syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
+       EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
        AsyncNetworker *NW = IO->Data;
        eNextState rc;
 
@@ -725,7 +834,7 @@ eNextState NWC_DispatchReadDone(AsyncIO *IO)
 }
 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
 {
-       syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
+       EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
        AsyncNetworker *NW = IO->Data;
        eNextState rc;
 
@@ -739,37 +848,53 @@ eNextState NWC_DispatchWriteDone(AsyncIO *IO)
 /*****************************************************************************/
 eNextState NWC_Terminate(AsyncIO *IO)
 {
-       syslog(LOG_DEBUG, "Nw: %s\n", __FUNCTION__);
+       EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
+       FinalizeNetworker(IO);
+       return eAbort;
+}
+
+eNextState NWC_TerminateDB(AsyncIO *IO)
+{
+       EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
        FinalizeNetworker(IO);
        return eAbort;
 }
 
 eNextState NWC_Timeout(AsyncIO *IO)
 {
-       syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
+       AsyncNetworker *NW = IO->Data;
+       EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
 
+       if (NW->IO.ErrMsg == NULL)
+               NW->IO.ErrMsg = NewStrBuf();
+       StrBufPrintf(NW->IO.ErrMsg, "Timeout while talking to %s \r\n", ChrPtr(NW->host));
        return NWC_FailNetworkConnection(IO);
 }
 eNextState NWC_ConnFail(AsyncIO *IO)
 {
-///    AsyncNetworker *NW = IO->Data;
+       AsyncNetworker *NW = IO->Data;
+
+       EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
+       if (NW->IO.ErrMsg == NULL)
+               NW->IO.ErrMsg = NewStrBuf();
+       StrBufPrintf(NW->IO.ErrMsg, "failed to connect %s \r\n", ChrPtr(NW->host));
 
-       syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
-////   StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); todo
        return NWC_FailNetworkConnection(IO);
 }
 eNextState NWC_DNSFail(AsyncIO *IO)
 {
-///    AsyncNetworker *NW = IO->Data;
+       AsyncNetworker *NW = IO->Data;
+
+       EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
+       if (NW->IO.ErrMsg == NULL)
+               NW->IO.ErrMsg = NewStrBuf();
+       StrBufPrintf(NW->IO.ErrMsg, "failed to look up %s \r\n", ChrPtr(NW->host));
 
-       syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
-////   StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); todo
        return NWC_FailNetworkConnection(IO);
 }
 eNextState NWC_Shutdown(AsyncIO *IO)
 {
-       syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
-////   pop3aggr *pMsg = IO->Data;
+       EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
 
        FinalizeNetworker(IO);
        return eAbort;
@@ -780,12 +905,11 @@ eNextState nwc_connect_ip(AsyncIO *IO)
 {
        AsyncNetworker *NW = IO->Data;
 
-       syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
-       syslog(LOG_DEBUG, "network: polling <%s>\n", ChrPtr(NW->node));
-       syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n", 
-              ChrPtr(NW->node), 
-              ChrPtr(NW->host),
-              ChrPtr(NW->port));
+       EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
+       EVN_syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n", 
+                  ChrPtr(NW->node), 
+                  ChrPtr(NW->host),
+                  ChrPtr(NW->port));
        
        return EvConnectSock(IO,
                             NWC_ConnTimeout,
@@ -793,8 +917,12 @@ eNextState nwc_connect_ip(AsyncIO *IO)
                             1);
 }
 
+static int NetworkerCount = 0;
 void RunNetworker(AsyncNetworker *NW)
 {
+       NW->n = NetworkerCount++;
+       network_talking_to(SKEY(NW->node), NTT_ADD);
+       syslog(LOG_DEBUG, "NW[%s][%ld]: polling\n", ChrPtr(NW->node), NW->n);
        ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
 
        InitIOStruct(&NW->IO,
@@ -805,6 +933,7 @@ void RunNetworker(AsyncNetworker *NW)
                     NWC_DispatchWriteDone,
                     NWC_DispatchReadDone,
                     NWC_Terminate,
+                    NWC_TerminateDB,
                     NWC_ConnFail,
                     NWC_Timeout,
                     NWC_Shutdown);
@@ -828,91 +957,84 @@ void RunNetworker(AsyncNetworker *NW)
  * Set "full" to nonzero to force a poll of every node, or to zero to poll
  * only nodes to which we have data to send.
  */
-void network_poll_other_citadel_nodes(int full_poll, char *working_ignetcfg)
+void network_poll_other_citadel_nodes(int full_poll, HashList *ignetcfg)
 {
+       const char *key;
+       long len;
+       HashPos *Pos;
+       void *vCfg;
        AsyncNetworker *NW;
-       StrBuf *CfgData;
-       StrBuf *Line;
-       const char *lptr;
-       const char *CfgPtr;
-       int Done;
+       StrBuf *SpoolFileName;
        
        int poll = 0;
        
-       if ((working_ignetcfg == NULL) || (*working_ignetcfg == '\0')) {
+       if (GetCount(ignetcfg) ==0) {
                syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
                return;
        }
        become_session(&networker_client_CC);
 
-       CfgData = NewStrBufPlain(working_ignetcfg, -1);
-       Line = NewStrBufPlain(NULL, StrLength(CfgData));
-       Done = 0;
-       CfgPtr = NULL;
-       while (!Done)
+       SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
+
+       Pos = GetNewHashPos(ignetcfg, 0);
+
+       while (GetNextHashPos(ignetcfg, Pos, &len, &key, &vCfg))
        {
                /* Use the string tokenizer to grab one line at a time */
-               StrBufSipLine(Line, CfgData, &CfgPtr);
-               Done = CfgPtr == StrBufNOTNULL;
-               if (StrLength(Line) > 0)
+               if(server_shutting_down)
+                       return;/* TODO free stuff*/
+               NodeConf *pNode = (NodeConf*) vCfg;
+               poll = 0;
+               NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
+               memset(NW, 0, sizeof(AsyncNetworker));
+               
+               NW->node = NewStrBufDup(pNode->NodeName);
+               NW->host = NewStrBufDup(pNode->Host);
+               NW->port = NewStrBufDup(pNode->Port);
+               NW->secret = NewStrBufDup(pNode->Secret);
+               
+               if ( (StrLength(NW->node) != 0) && 
+                    (StrLength(NW->secret) != 0) &&
+                    (StrLength(NW->host) != 0) &&
+                    (StrLength(NW->port) != 0))
                {
-                       if(server_shutting_down)
-                               return;/* TODO free stuff*/
-                       lptr = NULL;
-                       poll = 0;
-                       NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
-                       memset(NW, 0, sizeof(AsyncNetworker));
-                       
-                       NW->node = NewStrBufPlain(NULL, StrLength(Line));
-                       NW->host = NewStrBufPlain(NULL, StrLength(Line));
-                       NW->port = NewStrBufPlain(NULL, StrLength(Line));
-                       NW->secret = NewStrBufPlain(NULL, StrLength(Line));
-                       
-                       StrBufExtract_NextToken(NW->node, Line, &lptr, '|');
-                       StrBufExtract_NextToken(NW->secret, Line, &lptr, '|');
-                       StrBufExtract_NextToken(NW->host, Line, &lptr, '|');
-                       StrBufExtract_NextToken(NW->port, Line, &lptr, '|');
-                       if ( (StrLength(NW->node) != 0) && 
-                            (StrLength(NW->secret) != 0) &&
-                            (StrLength(NW->host) != 0) &&
-                            (StrLength(NW->port) != 0))
+                       poll = full_poll;
+                       if (poll == 0)
                        {
-                               poll = full_poll;
-                               if (poll == 0)
-                               {
-                                       NW->SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
-                                       StrBufAppendBufPlain(NW->SpoolFileName, HKEY("/"), 0);
-                                       StrBufAppendBuf(NW->SpoolFileName, NW->node, 0);
-                                       if (access(ChrPtr(NW->SpoolFileName), R_OK) == 0) {
-                                               poll = 1;
-                                       }
+                               StrBufAppendBufPlain(SpoolFileName, HKEY("/"), 0);
+                               StrBufAppendBuf(SpoolFileName, NW->node, 0);
+                               StrBufStripSlashes(SpoolFileName, 1);
+                               
+                               if (access(ChrPtr(SpoolFileName), R_OK) == 0) {
+                                       poll = 1;
                                }
                        }
-                       if (poll) {
-                               NW->Url = NewStrBufPlain(NULL, StrLength(Line));
-                               StrBufPrintf(NW->Url, "citadel://:%s@%s:%s", 
-                                            ChrPtr(NW->secret),
-                                            ChrPtr(NW->host),
-                                            ChrPtr(NW->port));
-                               if (!network_talking_to(SKEY(NW->node), NTT_CHECK))
-                               {
-                                       network_talking_to(SKEY(NW->node), NTT_ADD);
-                                       RunNetworker(NW);
-                                       continue;
-                               }
+               }
+               if (poll && 
+                   (StrLength(NW->host) > 0) && 
+                   strcmp("0.0.0.0", ChrPtr(NW->host)))
+               {
+                       NW->Url = NewStrBuf();
+                       StrBufPrintf(NW->Url, "citadel://%s@%s:%s", 
+                                    ChrPtr(NW->secret),
+                                    ChrPtr(NW->host),
+                                    ChrPtr(NW->port));
+                       if (!network_talking_to(SKEY(NW->node), NTT_CHECK))
+                       {
+                               RunNetworker(NW);
+                               continue;
                        }
-                       DeleteNetworker(NW);
                }
+               DeleteNetworker(NW);
        }
-       FreeStrBuf(&CfgData);
-       FreeStrBuf(&Line);
-
+       FreeStrBuf(&SpoolFileName);
+       DeleteHashPos(&Pos);
 }
 
 
 void network_do_clientqueue(void)
 {
-       char *working_ignetcfg;
+       HashList *working_ignetcfg;
        int full_processing = 1;
        static time_t last_run = 0L;
 
@@ -927,19 +1049,20 @@ void network_do_clientqueue(void)
                );
        }
 
-       working_ignetcfg = load_working_ignetcfg();
+       working_ignetcfg = load_ignetcfg();
        /*
         * Poll other Citadel nodes.  Maybe.  If "full_processing" is set
         * then we poll everyone.  Otherwise we only poll nodes we have stuff
         * to send to.
         */
        network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
-       if (working_ignetcfg)
-               free(working_ignetcfg);
+       DeleteHash(&working_ignetcfg);
 }
 
-
-
+void LogDebugEnableNetworkClient(const int n)
+{
+       NetworkClientDebugEnabled = n;
+}
 /*
  * Module entry point
  */
@@ -949,7 +1072,9 @@ CTDL_MODULE_INIT(network_client)
        {
                CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
                
-               CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER);
+               CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER, PRIO_SEND + 10);
+               CtdlRegisterDebugFlagHook(HKEY("networkclient"), LogDebugEnableNetworkClient, &NetworkClientDebugEnabled);
+
        }
-       return "network_client";
+       return "networkclient";
 }