NetworkConfig: move to RoomNetworkConfig; move NTT-List into its own file.
[citadel.git] / citadel / modules / network / serv_networkclient.c
index 0023d83631904b0dfa1eb5dbe2d359e3c2fa84a8..c3cd1e700bb31a3934e29958231469da6aba743b 100644 (file)
@@ -2,22 +2,16 @@
  * 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
- *  the Free Software Foundation; either version 3 of the License, or
- *  (at your option) any later version.
+ *  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
- *
  * ** NOTE **   A word on the S_NETCONFIGS semaphore:
  * This is a fairly high-level type of critical section.  It ensures that no
  * two threads work on the netconfigs files at the same time.  Since we do
 #include "file_ops.h"
 #include "citadel_dirs.h"
 #include "threads.h"
-
-#ifndef HAVE_SNPRINTF
-#include "snprintf.h"
-#endif
-
 #include "context.h"
-
-#include "netconfig.h"
 #include "ctdl_module.h"
 
 struct CitContext networker_client_CC;
@@ -91,27 +78,31 @@ struct CitContext networker_client_CC;
 #define NODE ChrPtr(((AsyncNetworker*)IO->Data)->node)
 #define N ((AsyncNetworker*)IO->Data)->n
 
-#define EVN_syslog(LEVEL, FORMAT, ...) \
-       syslog(LEVEL, \
-              "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT, \
-              IO->ID, CCID, NODE, N, __VA_ARGS__)
+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) \
-       syslog(LEVEL, \
-              "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT, \
-              IO->ID, CCID, NODE, N)
+#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, ...) \
-       syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT, \
-              IO->ID, NODE, N, __VA_ARGS__)
+       NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT,      \
+                              IO->ID, NODE, N, __VA_ARGS__)
 
 #define EVNCSM_syslog(LEVEL, FORMAT) \
-       syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT, \
-              IO->ID, NODE, N)
+       NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT,      \
+                              IO->ID, NODE, N)
 
 
 typedef enum _eNWCState {
-       eeGreating,
+       eGreating,
        eAuth,
        eNDOP,
        eREAD,
@@ -124,6 +115,41 @@ typedef enum _eNWCState {
        eQUIT
 }eNWCState;
 
+typedef enum _eNWCVState {
+       eNWCVSLookup,
+       eNWCVSConnecting,
+       eNWCVSConnFail,
+       eNWCVSGreating,
+       eNWCVSAuth,
+       eNWCVSAuthFailNTT,
+       eNWCVSAuthFail,
+       eNWCVSNDOP,
+       eNWCVSNDOPDone,
+       eNWCVSNUOP,
+       eNWCVSNUOPDone,
+       eNWCVSFail
+}eNWCVState;
+
+ConstStr NWCStateStr[] = {
+       {HKEY("Looking up Host")},
+       {HKEY("Connecting host")},
+       {HKEY("Failed to connect")},
+       {HKEY("Rread Greeting")},
+       {HKEY("Authenticating")},
+       {HKEY("Auth failed by NTT")},
+       {HKEY("Auth failed")},
+       {HKEY("Downloading")},
+       {HKEY("Downloading Success")},
+       {HKEY("Uploading Spoolfile")},
+       {HKEY("Uploading done")},
+       {HKEY("failed")}
+};
+
+void SetNWCState(AsyncIO *IO, eNWCVState State)
+{
+       CitContext* CCC = IO->CitContext;
+       memcpy(CCC->cs_clientname, NWCStateStr[State].Key, NWCStateStr[State].len + 1);
+}
 
 typedef struct _async_networker {
         AsyncIO IO;
@@ -157,7 +183,12 @@ 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);
 }
 
@@ -165,11 +196,30 @@ void DeleteNetworker(void *vptr)
 #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;
 
-       network_talking_to(SKEY(NW->node), NTT_REMOVE);
+       CtdlNetworkTalkingTo(SKEY(NW->node), NTT_REMOVE);
 
        DeleteNetworker(IO->Data);
        return eAbort;
@@ -179,6 +229,7 @@ eNextState NWC_ReadGreeting(AsyncNetworker *NW)
 {
        char connected_to[SIZ];
        AsyncIO *IO = &NW->IO;
+       SetNWCState(IO, eNWCVSGreating);
        NWC_DBG_READ();
        /* Read the server greeting */
        /* Check that the remote is who we think it is and warn the Aide if not */
@@ -191,8 +242,8 @@ eNextState NWC_ReadGreeting(AsyncNetworker *NW)
                             "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
                             connected_to, ChrPtr(NW->node));
                EVN_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
+               StopClientWatchers(IO, 1);
+               return QueueDBOperation(IO, SendFailureMessage);
        }
        return eSendReply;
 }
@@ -200,6 +251,7 @@ eNextState NWC_ReadGreeting(AsyncNetworker *NW)
 eNextState NWC_SendAuth(AsyncNetworker *NW)
 {
        AsyncIO *IO = &NW->IO;
+       SetNWCState(IO, eNWCVSAuth);
        /* We're talking to the correct node.  Now identify ourselves. */
        StrBufPrintf(NW->IO.SendBuf.Buf, "NETP %s|%s\n", 
                     config.c_nodename, 
@@ -218,14 +270,25 @@ 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));
-               EVN_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) {
+                       SetNWCState(IO, eNWCVSAuthFailNTT);
+                       EVN_syslog(LOG_INFO,
+                                  "Already talking to %s; skipping this time.\n",
+                                  ChrPtr(NW->node));
+                       
+               }
+               else {
+                       SetNWCState(IO, eNWCVSAuthFailNTT);
+                       EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
+                       StopClientWatchers(IO, 1);
+                       return QueueDBOperation(IO, SendFailureMessage);
+               }
                return eAbort;
        }
 }
@@ -233,6 +296,7 @@ eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
 eNextState NWC_SendNDOP(AsyncNetworker *NW)
 {
        AsyncIO *IO = &NW->IO;
+       SetNWCState(IO, eNWCVSNDOP);
        NW->tempFileName = NewStrBuf();
        NW->SpoolFileName = NewStrBuf();
        StrBufPrintf(NW->SpoolFileName,
@@ -265,7 +329,7 @@ eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
 
                NW->IO.IOB.TotalSentAlready = 0;
                TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
-               EVN_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;
                }
@@ -276,6 +340,7 @@ eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
                                  S_IRUSR|S_IWUSR);
                        if (fd < 0)
                        {
+                               SetNWCState(IO, eNWCVSFail);
                                EVN_syslog(LOG_CRIT,
                                       "cannot open %s: %s\n", 
                                       ChrPtr(NW->tempFileName), 
@@ -290,6 +355,7 @@ eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
        }
        else
        {
+               SetNWCState(IO, eNWCVSFail);
                return eAbort;
        }
 }
@@ -309,6 +375,8 @@ eNextState NWC_SendREAD(AsyncNetworker *NW)
                {
                        FDIOBufferDelete(&NW->IO.IOB);
                        unlink(ChrPtr(NW->tempFileName));
+                       FDIOBufferDelete(&IO->IOB);
+                       SetNWCState(IO, eNWCVSFail);
                        return eAbort;
                }
                StrBufPrintf(NW->IO.SendBuf.Buf, "READ %ld|%ld\n",
@@ -343,6 +411,7 @@ 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);
@@ -382,12 +451,11 @@ eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
        eNextState rc;
        AsyncIO *IO = &NW->IO;
 /* we don't have any data to debug print here. */
-       if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
+       if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
        {
                NW->State ++;
 
                FDIOBufferDelete(&NW->IO.IOB);
-
                if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
                        EVN_syslog(LOG_ALERT, 
                               "Could not link %s to %s: %s\n",
@@ -410,6 +478,7 @@ eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
 eNextState NWC_SendCLOS(AsyncNetworker *NW)
 {
        AsyncIO *IO = &NW->IO;
+       SetNWCState(IO, eNWCVSNDOPDone);
        StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
        NWC_DBG_SEND();
        return eSendReply;
@@ -419,6 +488,7 @@ 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;
@@ -433,13 +503,14 @@ eNextState NWC_SendNUOP(AsyncNetworker *NW)
        struct stat statbuf;
        int fd;
 
+       SetNWCState(IO, eNWCVSNUOP);
        StrBufPrintf(NW->SpoolFileName,
                     "%s/%s",
                     ctdl_netout_dir,
                     ChrPtr(NW->node));
        StrBufStripSlashes(NW->SpoolFileName, 1);
 
-       fd = open(ChrPtr(NW->SpoolFileName), O_RDONLY);
+       fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
        if (fd < 0) {
                if (errno != ENOENT) {
                        EVN_syslog(LOG_CRIT,
@@ -467,6 +538,7 @@ eNextState NWC_SendNUOP(AsyncNetworker *NW)
                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);
@@ -480,8 +552,10 @@ 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;
 }
 
@@ -499,6 +573,7 @@ eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
        NWC_DBG_READ();
        if (ChrPtr(NW->IO.IOBuf)[0] != '7')
        {
+               FDIOBufferDelete(&IO->IOB);
                return eAbort;
        }
 
@@ -511,7 +586,7 @@ eNextState NWC_SendBlobDone(AsyncNetworker *NW)
 {
        AsyncIO *IO = &NW->IO;
        eNextState rc;
-       if (IO->IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
+       if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
        {
                NW->State ++;
 
@@ -547,6 +622,8 @@ eNextState NWC_ReadUCLS(AsyncNetworker *NW)
                EVN_syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
                unlink(ChrPtr(NW->SpoolFileName));
        }
+       FDIOBufferDelete(&IO->IOB);
+       SetNWCState(IO, eNWCVSNUOPDone);
        return eSendReply;
 }
 
@@ -731,7 +808,9 @@ eReadState NWC_ReadServerStatus(AsyncIO *IO)
 
 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
 {
-       return eAbort;
+       SetNWCState(IO, eNWCVSConnFail);
+       StopClientWatchers(IO, 1);
+       return QueueDBOperation(IO, SendFailureMessage);
 }
 
 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
@@ -808,6 +887,13 @@ eNextState NWC_Terminate(AsyncIO *IO)
        return eAbort;
 }
 
+eNextState NWC_TerminateDB(AsyncIO *IO)
+{
+       EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
+       FinalizeNetworker(IO);
+       return eAbort;
+}
+
 eNextState NWC_Timeout(AsyncIO *IO)
 {
        AsyncNetworker *NW = IO->Data;
@@ -853,6 +939,7 @@ eNextState nwc_connect_ip(AsyncIO *IO)
 {
        AsyncNetworker *NW = IO->Data;
 
+       SetNWCState(&NW->IO, eNWCVSConnecting);
        EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
        EVN_syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n", 
                   ChrPtr(NW->node), 
@@ -869,7 +956,7 @@ static int NetworkerCount = 0;
 void RunNetworker(AsyncNetworker *NW)
 {
        NW->n = NetworkerCount++;
-       network_talking_to(SKEY(NW->node), NTT_ADD);
+       CtdlNetworkTalkingTo(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);
 
@@ -881,6 +968,7 @@ void RunNetworker(AsyncNetworker *NW)
                     NWC_DispatchWriteDone,
                     NWC_DispatchReadDone,
                     NWC_Terminate,
+                    NWC_TerminateDB,
                     NWC_ConnFail,
                     NWC_Timeout,
                     NWC_Shutdown);
@@ -890,10 +978,12 @@ void RunNetworker(AsyncNetworker *NW)
                    sizeof(((CitContext *)NW->IO.CitContext)->cs_host)); 
 
        if (NW->IO.ConnectMe->IsIP) {
+               SetNWCState(&NW->IO, eNWCVSLookup);
                QueueEventContext(&NW->IO,
                                  nwc_connect_ip);
        }
        else { /* uneducated admin has chosen to add DNS to the equation... */
+               SetNWCState(&NW->IO, eNWCVSConnecting);
                QueueEventContext(&NW->IO,
                                  nwc_get_one_host_ip);
        }
@@ -904,97 +994,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;
        StrBuf *SpoolFileName;
-       const char *lptr;
-       const char *CfgPtr;
-       int Done;
        
        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);
        SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
-       Line = NewStrBufPlain(NULL, StrLength(CfgData));
-       Done = 0;
-       CfgPtr = NULL;
-       while (!Done)
+
+       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*/
+               CtdlNodeConf *pNode = (CtdlNodeConf*) 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)
-                               {
-                                       StrBufAppendBufPlain(SpoolFileName, HKEY("/"), 0);
-                                       StrBufAppendBuf(SpoolFileName, NW->node, 0);
-                                       StrBufStripSlashes(SpoolFileName, 1);
-
-                                       if (access(ChrPtr(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 && 
-                           (StrLength(NW->host) > 0) && 
-                           strcmp("0.0.0.0", ChrPtr(NW->host)))
+               }
+               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 (!CtdlNetworkTalkingTo(SKEY(NW->node), NTT_CHECK))
                        {
-                               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))
-                               {
-                                       RunNetworker(NW);
-                                       continue;
-                               }
+                               RunNetworker(NW);
+                               continue;
                        }
-                       DeleteNetworker(NW);
                }
+               DeleteNetworker(NW);
        }
        FreeStrBuf(&SpoolFileName);
-       FreeStrBuf(&CfgData);
-       FreeStrBuf(&Line);
-
+       DeleteHashPos(&Pos);
 }
 
 
 void network_do_clientqueue(void)
 {
-       char *working_ignetcfg;
+       HashList *working_ignetcfg;
        int full_processing = 1;
        static time_t last_run = 0L;
 
@@ -1009,19 +1086,20 @@ void network_do_clientqueue(void)
                );
        }
 
-       working_ignetcfg = load_working_ignetcfg();
+       working_ignetcfg = CtdlLoadIgNetCfg();
        /*
         * 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
  */
@@ -1031,7 +1109,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";
 }