2 * This module handles shared rooms, inter-Citadel mail, and outbound
3 * mailing list processing.
5 * Copyright (c) 2000-2017 by the citadel.org team
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 3.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * ** NOTE ** A word on the S_NETCONFIGS semaphore:
16 * This is a fairly high-level type of critical section. It ensures that no
17 * two threads work on the netconfigs files at the same time. Since we do
18 * so many things inside these, here are the rules:
19 * 1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
20 * 2. Do *not* perform any I/O with the client during these sections.
35 #include <sys/types.h>
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
42 # include <sys/time.h>
50 # if HAVE_SYS_SYSCALL_H
51 # include <sys/syscall.h>
58 #include <libcitadel.h>
61 #include "citserver.h"
67 #include "internet_addressing.h"
68 #include "clientsocket.h"
69 #include "citadel_dirs.h"
72 #include "ctdl_module.h"
74 struct CitContext networker_client_CC;
76 #define NODE ChrPtr(((AsyncNetworker*)IO->Data)->node)
77 #define N ((AsyncNetworker*)IO->Data)->n
79 typedef enum _eNWCState {
93 typedef enum _eNWCVState {
108 ConstStr NWCStateStr[] = {
109 {HKEY("Looking up Host")},
110 {HKEY("Connecting host")},
111 {HKEY("Failed to connect")},
112 {HKEY("Rread Greeting")},
113 {HKEY("Authenticating")},
114 {HKEY("Auth failed by NTT")},
115 {HKEY("Auth failed")},
116 {HKEY("Downloading")},
117 {HKEY("Downloading Success")},
118 {HKEY("Uploading Spoolfile")},
119 {HKEY("Uploading done")},
123 void SetNWCState(AsyncIO *IO, eNWCVState State)
125 CitContext* CCC = IO->CitContext;
126 memcpy(CCC->cs_clientname, NWCStateStr[State].Key, NWCStateStr[State].len + 1);
129 typedef struct _async_networker {
131 DNSQueryParts HostLookup;
134 StrBuf *SpoolFileName;
135 StrBuf *tempFileName;
143 typedef eNextState(*NWClientHandler)(AsyncNetworker* NW);
144 eNextState nwc_get_one_host_ip(AsyncIO *IO);
146 eNextState nwc_connect_ip(AsyncIO *IO);
148 eNextState NWC_SendQUIT(AsyncNetworker *NW);
149 eNextState NWC_DispatchWriteDone(AsyncIO *IO);
151 void DeleteNetworker(void *vptr)
153 AsyncNetworker *NW = (AsyncNetworker *)vptr;
154 FreeStrBuf(&NW->SpoolFileName);
155 FreeStrBuf(&NW->tempFileName);
156 FreeStrBuf(&NW->node);
157 FreeStrBuf(&NW->host);
158 FreeStrBuf(&NW->port);
159 FreeStrBuf(&NW->secret);
160 FreeStrBuf(&NW->Url);
161 FreeStrBuf(&NW->IO.ErrMsg);
162 FreeAsyncIOContents(&NW->IO);
163 if (NW->HostLookup.VParsedDNSReply != NULL) {
164 NW->HostLookup.DNSReplyFree(NW->HostLookup.VParsedDNSReply);
165 NW->HostLookup.VParsedDNSReply = NULL;
170 eNextState NWC_SendFailureMessage(AsyncIO *IO)
172 AsyncNetworker *NW = IO->Data;
174 syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
176 CtdlAideMessage(ChrPtr(NW->IO.ErrMsg), "Networker error");
180 eNextState FinalizeNetworker(AsyncIO *IO)
182 AsyncNetworker *NW = (AsyncNetworker *)IO->Data;
184 CtdlNetworkTalkingTo(SKEY(NW->node), NTT_REMOVE);
186 DeleteNetworker(IO->Data);
190 eNextState NWC_ReadGreeting(AsyncNetworker *NW)
192 char connected_to[SIZ];
193 AsyncIO *IO = &NW->IO;
194 SetNWCState(IO, eNWCVSGreating);
195 /* Read the server greeting */
196 /* Check that the remote is who we think it is and warn the Aide if not */
197 extract_token (connected_to, ChrPtr(NW->IO.IOBuf), 1, ' ', sizeof connected_to);
198 if (strcmp(connected_to, ChrPtr(NW->node)) != 0)
200 if (NW->IO.ErrMsg == NULL)
201 NW->IO.ErrMsg = NewStrBuf();
202 StrBufPrintf(NW->IO.ErrMsg,
203 "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
204 connected_to, ChrPtr(NW->node));
205 syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
207 return EventQueueDBOperation(IO, NWC_SendFailureMessage, 1);
212 eNextState NWC_SendAuth(AsyncNetworker *NW)
214 AsyncIO *IO = &NW->IO;
215 SetNWCState(IO, eNWCVSAuth);
216 /* We're talking to the correct node. Now identify ourselves. */
217 StrBufPrintf(NW->IO.SendBuf.Buf, "NETP %s|%s\n",
218 CtdlGetConfigStr("c_nodename"),
223 eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
225 AsyncIO *IO = &NW->IO;
226 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
232 int Error = atol(ChrPtr(NW->IO.IOBuf));
233 if (NW->IO.ErrMsg == NULL)
234 NW->IO.ErrMsg = NewStrBuf();
235 StrBufPrintf(NW->IO.ErrMsg,
236 "Connected to node \"%s\" but my secret wasn't accurate.\nReason was:%s\n",
237 ChrPtr(NW->node), ChrPtr(NW->IO.IOBuf) + 4);
239 SetNWCState(IO, eNWCVSAuthFailNTT);
241 "Already talking to %s; skipping this time.\n",
246 SetNWCState(IO, eNWCVSAuthFailNTT);
247 syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
248 return EventQueueDBOperation(IO, NWC_SendFailureMessage, 1);
254 eNextState NWC_SendNDOP(AsyncNetworker *NW)
256 AsyncIO *IO = &NW->IO;
257 SetNWCState(IO, eNWCVSNDOP);
258 NW->tempFileName = NewStrBuf();
259 NW->SpoolFileName = NewStrBuf();
260 StrBufPrintf(NW->SpoolFileName,
264 time(NULL),// TODO: get time from libev
266 StrBufStripSlashes(NW->SpoolFileName, 1);
267 StrBufPrintf(NW->tempFileName,
271 time(NULL),// TODO: get time from libev
273 StrBufStripSlashes(NW->tempFileName, 1);
274 /* We're talking to the correct node. Now identify ourselves. */
275 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NDOP\n"));
279 eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
281 AsyncIO *IO = &NW->IO;
283 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
285 int LogLevel = LOG_DEBUG;
287 NW->IO.IOB.TotalSentAlready = 0;
289 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
291 if (TotalSendSize > 0)
295 "Expecting to transfer %d bytes to %s\n",
297 ChrPtr(NW->tempFileName));
299 if (TotalSendSize <= 0) {
300 NW->State = eNUOP - 1;
304 fd = open(ChrPtr(NW->tempFileName),
305 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
309 SetNWCState(IO, eNWCVSFail);
311 "cannot open %s: %s\n",
312 ChrPtr(NW->tempFileName),
315 NW->State = eQUIT - 1;
318 FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
324 SetNWCState(IO, eNWCVSFail);
329 eNextState NWC_SendREAD(AsyncNetworker *NW)
331 AsyncIO *IO = &NW->IO;
334 if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
337 * If shutting down we can exit here and unlink the temp file.
338 * this shouldn't loose us any messages.
340 if (server_shutting_down)
342 FDIOBufferDelete(&NW->IO.IOB);
343 unlink(ChrPtr(NW->tempFileName));
344 FDIOBufferDelete(&IO->IOB);
345 SetNWCState(IO, eNWCVSFail);
348 StrBufPrintf(NW->IO.SendBuf.Buf, "READ "LOFF_T_FMT"|%ld\n",
349 NW->IO.IOB.TotalSentAlready,
350 NW->IO.IOB.TotalSendSize);
352 ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
353 ? IGNET_PACKET_SIZE :
354 (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
362 rc = NWC_DispatchWriteDone(&NW->IO);
368 eNextState NWC_ReadREADState(AsyncNetworker *NW)
370 AsyncIO *IO = &NW->IO;
371 if (ChrPtr(NW->IO.IOBuf)[0] == '6')
373 NW->IO.IOB.ChunkSendRemain =
374 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
377 FDIOBufferDelete(&IO->IOB);
380 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
381 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
384 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
388 FDIOBufferDelete(&NW->IO.IOB);
390 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
392 "Could not link %s to %s: %s\n",
393 ChrPtr(NW->tempFileName),
394 ChrPtr(NW->SpoolFileName),
400 ChrPtr(NW->tempFileName),
401 ChrPtr(NW->SpoolFileName));
404 unlink(ChrPtr(NW->tempFileName));
405 rc = NWC_DispatchWriteDone(&NW->IO);
411 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
412 return eSendReply; //NWC_DispatchWriteDone(&NW->IO);
416 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
419 /* we don't have any data to debug print here. */
420 if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
424 FDIOBufferDelete(&NW->IO.IOB);
425 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
427 "Could not link %s to %s: %s\n",
428 ChrPtr(NW->tempFileName),
429 ChrPtr(NW->SpoolFileName),
435 ChrPtr(NW->tempFileName),
436 ChrPtr(NW->SpoolFileName));
439 unlink(ChrPtr(NW->tempFileName));
440 rc = NWC_DispatchWriteDone(&NW->IO);
446 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
447 return NWC_DispatchWriteDone(&NW->IO);
450 eNextState NWC_SendCLOS(AsyncNetworker *NW)
452 AsyncIO *IO = &NW->IO;
453 SetNWCState(IO, eNWCVSNDOPDone);
454 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
458 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
460 AsyncIO *IO = &NW->IO;
461 FDIOBufferDelete(&IO->IOB);
462 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
463 return eTerminateConnection;
468 eNextState NWC_SendNUOP(AsyncNetworker *NW)
470 AsyncIO *IO = &NW->IO;
476 SetNWCState(IO, eNWCVSNUOP);
477 StrBufPrintf(NW->SpoolFileName,
481 StrBufStripSlashes(NW->SpoolFileName, 1);
483 fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
485 if (errno != ENOENT) {
487 "cannot open %s: %s\n",
488 ChrPtr(NW->SpoolFileName),
492 rc = NWC_SendQUIT(NW);
496 if (fstat(fd, &statbuf) == -1) {
497 syslog(LOG_CRIT, "FSTAT FAILED %s [%s]--\n",
498 ChrPtr(NW->SpoolFileName),
500 if (fd > 0) close(fd);
503 TotalSendSize = statbuf.st_size;
504 if (TotalSendSize == 0) {
506 "Nothing to send.\n");
508 rc = NWC_SendQUIT(NW);
509 if (fd > 0) close(fd);
515 "sending %s to %s\n",
516 ChrPtr(NW->SpoolFileName),
520 FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
522 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
526 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
528 AsyncIO *IO = &NW->IO;
529 if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
530 FDIOBufferDelete(&IO->IOB);
536 eNextState NWC_SendWRIT(AsyncNetworker *NW)
538 StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT "LOFF_T_FMT"\n",
539 NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
542 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
544 AsyncIO *IO = &NW->IO;
545 if (ChrPtr(NW->IO.IOBuf)[0] != '7')
547 FDIOBufferDelete(&IO->IOB);
551 NW->IO.IOB.ChunkSendRemain =
552 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
556 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
558 AsyncIO *IO = &NW->IO;
560 if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
564 FDIOBufferDelete(&IO->IOB);
565 rc = NWC_DispatchWriteDone(IO);
571 IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
572 rc = NWC_DispatchWriteDone(IO);
578 eNextState NWC_SendUCLS(AsyncNetworker *NW)
580 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
584 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
586 AsyncIO *IO = &NW->IO;
589 "Sent %s [%ld] octets to <%s>\n",
590 ChrPtr(NW->SpoolFileName),
591 NW->IO.IOB.ChunkSize,
594 if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
595 syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
596 unlink(ChrPtr(NW->SpoolFileName));
598 FDIOBufferDelete(&IO->IOB);
599 SetNWCState(IO, eNWCVSNUOPDone);
603 eNextState NWC_SendQUIT(AsyncNetworker *NW)
605 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
610 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
616 NWClientHandler NWC_ReadHandlers[] = {
629 long NWC_ConnTimeout = 100;
631 const long NWC_SendTimeouts[] = {
641 const ConstStr NWC[] = {
642 {HKEY("Connection broken during ")},
643 {HKEY("Connection broken during ")},
644 {HKEY("Connection broken during ")},
645 {HKEY("Connection broken during ")},
646 {HKEY("Connection broken during ")},
647 {HKEY("Connection broken during ")},
648 {HKEY("Connection broken during ")},
649 {HKEY("Connection broken during ")}
652 NWClientHandler NWC_SendHandlers[] = {
657 NWC_ReadREADBlobDone,
666 const long NWC_ReadTimeouts[] = {
680 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
682 AsyncNetworker *NW = IO->Data;
683 struct hostent *hostent;
687 hostent = NW->HostLookup.VParsedDNSReply;
688 if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) &&
689 (hostent != NULL) ) {
690 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
691 if (NW->IO.ConnectMe->IPv6) {
692 memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr,
693 &hostent->h_addr_list[0],
694 sizeof(struct in6_addr));
696 NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
697 NW->IO.ConnectMe->Addr.sin6_port = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
700 struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
701 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
702 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
703 memcpy(&addr->sin_addr.s_addr,
704 hostent->h_addr_list[0],
707 addr->sin_family = hostent->h_addrtype;
708 addr->sin_port = htons(504);/// default citadel port
711 return nwc_connect_ip(IO);
718 eNextState nwc_get_one_host_ip(AsyncIO *IO)
720 AsyncNetworker *NW = IO->Data;
722 * here we start with the lookup of one host.
725 syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
728 "NWC client[%ld]: looking up %s-Record %s : %d ...\n",
730 (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
731 NW->IO.ConnectMe->Host,
732 NW->IO.ConnectMe->Port);
734 QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
735 NW->IO.ConnectMe->Host,
738 nwc_get_one_host_ip_done);
739 IO->NextState = eReadDNSReply;
740 return IO->NextState;
743 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
745 eReadState NWC_ReadServerStatus(AsyncIO *IO)
747 // AsyncNetworker *NW = IO->Data;
748 eReadState Finished = eBufferNotEmpty;
750 switch (IO->NextState) {
755 case eTerminateConnection:
757 Finished = eReadFail;
763 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
775 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
777 SetNWCState(IO, eNWCVSConnFail);
778 return EventQueueDBOperation(IO, NWC_SendFailureMessage, 1);
781 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
783 double Timeout = 0.0;
785 //syslog(LOG_DEBUG, "%s - %d\n", __FUNCTION__, NextTCPState);
787 switch (NextTCPState) {
791 Timeout = NWC_ReadTimeouts[NW->State];
803 case eTerminateConnection:
813 SetNextTimeout(&NW->IO, Timeout*100);
818 eNextState NWC_DispatchReadDone(AsyncIO *IO)
820 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
821 AsyncNetworker *NW = IO->Data;
824 rc = NWC_ReadHandlers[NW->State](NW);
826 if ((rc != eReadMore) &&
832 NWC_SetTimeout(rc, NW);
836 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
838 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
839 AsyncNetworker *NW = IO->Data;
842 rc = NWC_SendHandlers[NW->State](NW);
843 NWC_SetTimeout(rc, NW);
847 /*****************************************************************************/
848 /* Networker CLIENT ERROR CATCHERS */
849 /*****************************************************************************/
850 eNextState NWC_Terminate(AsyncIO *IO)
852 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
853 FinalizeNetworker(IO);
857 eNextState NWC_TerminateDB(AsyncIO *IO)
859 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
860 FinalizeNetworker(IO);
864 eNextState NWC_Timeout(AsyncIO *IO)
866 AsyncNetworker *NW = IO->Data;
867 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
869 if (NW->IO.ErrMsg == NULL)
870 NW->IO.ErrMsg = NewStrBuf();
871 StrBufPrintf(NW->IO.ErrMsg, "Timeout while talking to %s \r\n", ChrPtr(NW->host));
872 return NWC_FailNetworkConnection(IO);
874 eNextState NWC_ConnFail(AsyncIO *IO)
876 AsyncNetworker *NW = IO->Data;
878 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
879 if (NW->IO.ErrMsg == NULL)
880 NW->IO.ErrMsg = NewStrBuf();
881 StrBufPrintf(NW->IO.ErrMsg, "failed to connect %s \r\n", ChrPtr(NW->host));
883 return NWC_FailNetworkConnection(IO);
885 eNextState NWC_DNSFail(AsyncIO *IO)
887 AsyncNetworker *NW = IO->Data;
889 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
890 if (NW->IO.ErrMsg == NULL)
891 NW->IO.ErrMsg = NewStrBuf();
892 StrBufPrintf(NW->IO.ErrMsg, "failed to look up %s \r\n", ChrPtr(NW->host));
894 return NWC_FailNetworkConnection(IO);
896 eNextState NWC_Shutdown(AsyncIO *IO)
898 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
900 FinalizeNetworker(IO);
905 eNextState nwc_connect_ip(AsyncIO *IO)
907 AsyncNetworker *NW = IO->Data;
909 SetNWCState(&NW->IO, eNWCVSConnecting);
910 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
911 syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n",
916 return EvConnectSock(IO,
922 static int NetworkerCount = 0;
923 void RunNetworker(AsyncNetworker *NW)
925 NW->n = NetworkerCount++;
926 CtdlNetworkTalkingTo(SKEY(NW->node), NTT_ADD);
927 syslog(LOG_DEBUG, "NW[%s][%ld]: polling\n", ChrPtr(NW->node), NW->n);
928 ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
930 InitIOStruct(&NW->IO,
933 NWC_ReadServerStatus,
935 NWC_DispatchWriteDone,
936 NWC_DispatchReadDone,
943 safestrncpy(((CitContext *)NW->IO.CitContext)->cs_host,
945 sizeof(((CitContext *)NW->IO.CitContext)->cs_host));
947 if (NW->IO.ConnectMe->IsIP) {
948 SetNWCState(&NW->IO, eNWCVSLookup);
949 QueueEventContext(&NW->IO,
952 else { /* uneducated admin has chosen to add DNS to the equation... */
953 SetNWCState(&NW->IO, eNWCVSConnecting);
954 QueueEventContext(&NW->IO,
955 nwc_get_one_host_ip);
960 * Poll other Citadel nodes and transfer inbound/outbound network data.
961 * Set "full" to nonzero to force a poll of every node, or to zero to poll
962 * only nodes to which we have data to send.
964 void network_poll_other_citadel_nodes(int full_poll, HashList *ignetcfg)
971 StrBuf *SpoolFileName;
975 if (GetCount(ignetcfg) ==0) {
976 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
979 become_session(&networker_client_CC);
981 SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
983 Pos = GetNewHashPos(ignetcfg, 0);
985 while (GetNextHashPos(ignetcfg, Pos, &len, &key, &vCfg))
987 /* Use the string tokenizer to grab one line at a time */
988 if(server_shutting_down)
989 return;/* TODO free stuff*/
990 CtdlNodeConf *pNode = (CtdlNodeConf*) vCfg;
992 NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
993 memset(NW, 0, sizeof(AsyncNetworker));
995 NW->node = NewStrBufDup(pNode->NodeName);
996 NW->host = NewStrBufDup(pNode->Host);
997 NW->port = NewStrBufDup(pNode->Port);
998 NW->secret = NewStrBufDup(pNode->Secret);
1000 if ( (StrLength(NW->node) != 0) &&
1001 (StrLength(NW->secret) != 0) &&
1002 (StrLength(NW->host) != 0) &&
1003 (StrLength(NW->port) != 0))
1008 StrBufAppendBufPlain(SpoolFileName, HKEY("/"), 0);
1009 StrBufAppendBuf(SpoolFileName, NW->node, 0);
1010 StrBufStripSlashes(SpoolFileName, 1);
1012 if (access(ChrPtr(SpoolFileName), R_OK) == 0) {
1018 (StrLength(NW->host) > 0) &&
1019 strcmp("0.0.0.0", ChrPtr(NW->host)))
1021 NW->Url = NewStrBuf();
1022 StrBufPrintf(NW->Url, "citadel://%s@%s:%s",
1026 if (!CtdlNetworkTalkingTo(SKEY(NW->node), NTT_CHECK))
1032 DeleteNetworker(NW);
1034 FreeStrBuf(&SpoolFileName);
1035 DeleteHashPos(&Pos);
1039 void network_do_clientqueue(void)
1041 HashList *working_ignetcfg;
1042 int full_processing = 1;
1043 static time_t last_run = 0L;
1046 * Run the full set of processing tasks no more frequently
1047 * than once every n seconds
1049 if ( (time(NULL) - last_run) < CtdlGetConfigLong("c_net_freq") )
1051 full_processing = 0;
1052 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
1053 CtdlGetConfigLong("c_net_freq") - (time(NULL)- last_run)
1057 working_ignetcfg = CtdlLoadIgNetCfg();
1059 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1060 * then we poll everyone. Otherwise we only poll nodes we have stuff
1063 network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
1064 DeleteHash(&working_ignetcfg);
1068 * Module entry point
1070 CTDL_MODULE_INIT(network_client)
1074 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
1075 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER, PRIO_SEND + 10);
1078 return "networkclient";