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, "netpoll: %s", __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, "netpoll: %s", 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);
240 syslog(LOG_INFO, "netpoll: already talking to %s; skipping this time.", ChrPtr(NW->node));
244 SetNWCState(IO, eNWCVSAuthFailNTT);
245 syslog(LOG_ERR, "netpoll: %s", ChrPtr(NW->IO.ErrMsg));
246 return EventQueueDBOperation(IO, NWC_SendFailureMessage, 1);
252 eNextState NWC_SendNDOP(AsyncNetworker *NW)
254 AsyncIO *IO = &NW->IO;
255 SetNWCState(IO, eNWCVSNDOP);
256 NW->tempFileName = NewStrBuf();
257 NW->SpoolFileName = NewStrBuf();
258 StrBufPrintf(NW->SpoolFileName,
262 time(NULL),// TODO: get time from libev
264 StrBufStripSlashes(NW->SpoolFileName, 1);
265 StrBufPrintf(NW->tempFileName,
269 time(NULL),// TODO: get time from libev
271 StrBufStripSlashes(NW->tempFileName, 1);
272 /* We're talking to the correct node. Now identify ourselves. */
273 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NDOP\n"));
277 eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
279 AsyncIO *IO = &NW->IO;
281 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
283 int LogLevel = LOG_DEBUG;
285 NW->IO.IOB.TotalSentAlready = 0;
287 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
289 if (TotalSendSize > 0)
292 syslog(LogLevel, "netpoll: expecting to transfer %d bytes to %s", TotalSendSize, ChrPtr(NW->tempFileName));
294 if (TotalSendSize <= 0) {
295 NW->State = eNUOP - 1;
299 fd = open(ChrPtr(NW->tempFileName),
300 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
304 SetNWCState(IO, eNWCVSFail);
305 syslog(LOG_ERR, "%s: %s", ChrPtr(NW->tempFileName), strerror(errno));
307 NW->State = eQUIT - 1;
310 FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
316 SetNWCState(IO, eNWCVSFail);
321 eNextState NWC_SendREAD(AsyncNetworker *NW)
323 AsyncIO *IO = &NW->IO;
326 if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
329 * If shutting down we can exit here and unlink the temp file.
330 * this shouldn't loose us any messages.
332 if (server_shutting_down)
334 FDIOBufferDelete(&NW->IO.IOB);
335 unlink(ChrPtr(NW->tempFileName));
336 FDIOBufferDelete(&IO->IOB);
337 SetNWCState(IO, eNWCVSFail);
340 StrBufPrintf(NW->IO.SendBuf.Buf, "READ "LOFF_T_FMT"|%ld\n",
341 NW->IO.IOB.TotalSentAlready,
342 NW->IO.IOB.TotalSendSize);
348 rc = NWC_DispatchWriteDone(&NW->IO);
354 eNextState NWC_ReadREADState(AsyncNetworker *NW)
356 AsyncIO *IO = &NW->IO;
357 if (ChrPtr(NW->IO.IOBuf)[0] == '6')
359 NW->IO.IOB.ChunkSendRemain =
360 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
363 FDIOBufferDelete(&IO->IOB);
366 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
367 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
370 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
374 FDIOBufferDelete(&NW->IO.IOB);
376 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
377 syslog(LOG_ERR, "netpoll: could not link %s to %s: %s", ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName), strerror(errno));
380 syslog(LOG_INFO, "netpoll: moved %s to %s", ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName));
383 unlink(ChrPtr(NW->tempFileName));
384 rc = NWC_DispatchWriteDone(&NW->IO);
390 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
391 return eSendReply; //NWC_DispatchWriteDone(&NW->IO);
395 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
398 /* we don't have any data to debug print here. */
399 if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
403 FDIOBufferDelete(&NW->IO.IOB);
404 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
405 syslog(LOG_ERR, "netpoll: could not link %s to %s: %s", ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName), strerror(errno));
408 syslog(LOG_INFO, "netpoll: moved %s to %s", ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName));
411 unlink(ChrPtr(NW->tempFileName));
412 rc = NWC_DispatchWriteDone(&NW->IO);
418 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
419 return NWC_DispatchWriteDone(&NW->IO);
422 eNextState NWC_SendCLOS(AsyncNetworker *NW)
424 AsyncIO *IO = &NW->IO;
425 SetNWCState(IO, eNWCVSNDOPDone);
426 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
430 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
432 AsyncIO *IO = &NW->IO;
433 FDIOBufferDelete(&IO->IOB);
434 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
435 return eTerminateConnection;
440 eNextState NWC_SendNUOP(AsyncNetworker *NW)
442 AsyncIO *IO = &NW->IO;
448 SetNWCState(IO, eNWCVSNUOP);
449 StrBufPrintf(NW->SpoolFileName,
453 StrBufStripSlashes(NW->SpoolFileName, 1);
455 fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
457 if (errno != ENOENT) {
458 syslog(LOG_ERR, "%s: %s", ChrPtr(NW->SpoolFileName), strerror(errno));
461 rc = NWC_SendQUIT(NW);
465 if (fstat(fd, &statbuf) == -1) {
466 syslog(LOG_ERR, "%s: %s", ChrPtr(NW->SpoolFileName), strerror(errno));
467 if (fd > 0) close(fd);
470 TotalSendSize = statbuf.st_size;
471 if (TotalSendSize == 0) {
472 syslog(LOG_DEBUG, "netpoll: nothing to send.");
474 rc = NWC_SendQUIT(NW);
475 if (fd > 0) close(fd);
480 syslog(LOG_INFO, "netpoll: sending %s to %s", ChrPtr(NW->SpoolFileName), ChrPtr(NW->node));
483 FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
485 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
489 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
491 AsyncIO *IO = &NW->IO;
492 if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
493 FDIOBufferDelete(&IO->IOB);
499 eNextState NWC_SendWRIT(AsyncNetworker *NW)
501 StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT "LOFF_T_FMT"\n",
502 NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
505 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
507 AsyncIO *IO = &NW->IO;
508 if (ChrPtr(NW->IO.IOBuf)[0] != '7')
510 FDIOBufferDelete(&IO->IOB);
514 NW->IO.IOB.ChunkSendRemain =
515 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
519 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
521 AsyncIO *IO = &NW->IO;
523 if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
527 FDIOBufferDelete(&IO->IOB);
528 rc = NWC_DispatchWriteDone(IO);
534 IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
535 rc = NWC_DispatchWriteDone(IO);
541 eNextState NWC_SendUCLS(AsyncNetworker *NW)
543 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
547 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
549 AsyncIO *IO = &NW->IO;
551 syslog(LOG_INFO, "netpoll: sent %s (%ld octets) to <%s>", ChrPtr(NW->SpoolFileName), NW->IO.IOB.ChunkSize, ChrPtr(NW->node));
553 if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
554 syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
555 unlink(ChrPtr(NW->SpoolFileName));
557 FDIOBufferDelete(&IO->IOB);
558 SetNWCState(IO, eNWCVSNUOPDone);
562 eNextState NWC_SendQUIT(AsyncNetworker *NW)
564 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
569 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
575 NWClientHandler NWC_ReadHandlers[] = {
588 long NWC_ConnTimeout = 100;
590 const long NWC_SendTimeouts[] = {
600 const ConstStr NWC[] = {
601 {HKEY("Connection broken during ")},
602 {HKEY("Connection broken during ")},
603 {HKEY("Connection broken during ")},
604 {HKEY("Connection broken during ")},
605 {HKEY("Connection broken during ")},
606 {HKEY("Connection broken during ")},
607 {HKEY("Connection broken during ")},
608 {HKEY("Connection broken during ")}
611 NWClientHandler NWC_SendHandlers[] = {
616 NWC_ReadREADBlobDone,
625 const long NWC_ReadTimeouts[] = {
639 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
641 AsyncNetworker *NW = IO->Data;
642 struct hostent *hostent;
646 hostent = NW->HostLookup.VParsedDNSReply;
647 if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) &&
648 (hostent != NULL) ) {
649 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
650 if (NW->IO.ConnectMe->IPv6) {
651 memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr,
652 &hostent->h_addr_list[0],
653 sizeof(struct in6_addr));
655 NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
656 NW->IO.ConnectMe->Addr.sin6_port = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
659 struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
660 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
661 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
662 memcpy(&addr->sin_addr.s_addr,
663 hostent->h_addr_list[0],
666 addr->sin_family = hostent->h_addrtype;
667 addr->sin_port = htons(504);/// default citadel port
670 return nwc_connect_ip(IO);
677 eNextState nwc_get_one_host_ip(AsyncIO *IO)
679 AsyncNetworker *NW = IO->Data;
681 * here we start with the lookup of one host.
684 syslog(LOG_DEBUG, "netpoll: %s", __FUNCTION__);
687 "netpoll: [%ld]: looking up %s-Record %s : %d ...",
689 (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
690 NW->IO.ConnectMe->Host,
691 NW->IO.ConnectMe->Port);
693 QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
694 NW->IO.ConnectMe->Host,
697 nwc_get_one_host_ip_done);
698 IO->NextState = eReadDNSReply;
699 return IO->NextState;
702 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
704 eReadState NWC_ReadServerStatus(AsyncIO *IO)
706 // AsyncNetworker *NW = IO->Data;
707 eReadState Finished = eBufferNotEmpty;
709 switch (IO->NextState) {
714 case eTerminateConnection:
716 Finished = eReadFail;
722 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
734 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
736 SetNWCState(IO, eNWCVSConnFail);
737 return EventQueueDBOperation(IO, NWC_SendFailureMessage, 1);
740 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
742 double Timeout = 0.0;
744 switch (NextTCPState) {
748 Timeout = NWC_ReadTimeouts[NW->State];
760 case eTerminateConnection:
765 syslog(LOG_DEBUG, "netpoll: %s - %d %f", __FUNCTION__, NextTCPState, Timeout);
766 SetNextTimeout(&NW->IO, Timeout*100);
771 eNextState NWC_DispatchReadDone(AsyncIO *IO)
773 syslog(LOG_DEBUG, "netpoll: %s", __FUNCTION__);
774 AsyncNetworker *NW = IO->Data;
777 rc = NWC_ReadHandlers[NW->State](NW);
779 if ((rc != eReadMore) &&
785 NWC_SetTimeout(rc, NW);
789 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
791 syslog(LOG_DEBUG, "netpoll: %s", __FUNCTION__);
792 AsyncNetworker *NW = IO->Data;
795 rc = NWC_SendHandlers[NW->State](NW);
796 NWC_SetTimeout(rc, NW);
800 /*****************************************************************************/
801 /* Networker CLIENT ERROR CATCHERS */
802 /*****************************************************************************/
803 eNextState NWC_Terminate(AsyncIO *IO)
805 syslog(LOG_DEBUG, "netpoll: %s", __FUNCTION__);
806 FinalizeNetworker(IO);
810 eNextState NWC_TerminateDB(AsyncIO *IO)
812 syslog(LOG_DEBUG, "netpoll: %s", __FUNCTION__);
813 FinalizeNetworker(IO);
817 eNextState NWC_Timeout(AsyncIO *IO)
819 AsyncNetworker *NW = IO->Data;
820 syslog(LOG_DEBUG, "netpoll: %s", __FUNCTION__);
822 if (NW->IO.ErrMsg == NULL)
823 NW->IO.ErrMsg = NewStrBuf();
824 StrBufPrintf(NW->IO.ErrMsg, "Timeout while talking to %s \r\n", ChrPtr(NW->host));
825 return NWC_FailNetworkConnection(IO);
827 eNextState NWC_ConnFail(AsyncIO *IO)
829 AsyncNetworker *NW = IO->Data;
831 syslog(LOG_DEBUG, "netpoll: %s", __FUNCTION__);
832 if (NW->IO.ErrMsg == NULL)
833 NW->IO.ErrMsg = NewStrBuf();
834 StrBufPrintf(NW->IO.ErrMsg, "failed to connect %s \r\n", ChrPtr(NW->host));
836 return NWC_FailNetworkConnection(IO);
838 eNextState NWC_DNSFail(AsyncIO *IO)
840 AsyncNetworker *NW = IO->Data;
842 syslog(LOG_DEBUG, "netpoll: %s", __FUNCTION__);
843 if (NW->IO.ErrMsg == NULL)
844 NW->IO.ErrMsg = NewStrBuf();
845 StrBufPrintf(NW->IO.ErrMsg, "failed to look up %s \r\n", ChrPtr(NW->host));
847 return NWC_FailNetworkConnection(IO);
849 eNextState NWC_Shutdown(AsyncIO *IO)
851 syslog(LOG_DEBUG, "netpoll: %s", __FUNCTION__);
853 FinalizeNetworker(IO);
858 eNextState nwc_connect_ip(AsyncIO *IO)
860 AsyncNetworker *NW = IO->Data;
862 SetNWCState(&NW->IO, eNWCVSConnecting);
863 syslog(LOG_DEBUG, "netpoll: %s", __FUNCTION__);
864 syslog(LOG_INFO, "netpoll: onnecting to <%s> at %s:%s", ChrPtr(NW->node), ChrPtr(NW->host), ChrPtr(NW->port));
866 return EvConnectSock(IO,
872 static int NetworkerCount = 0;
873 void RunNetworker(AsyncNetworker *NW)
875 NW->n = NetworkerCount++;
876 CtdlNetworkTalkingTo(SKEY(NW->node), NTT_ADD);
877 syslog(LOG_DEBUG, "netpoll: NW[%s][%ld]: polling", ChrPtr(NW->node), NW->n);
878 ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
880 InitIOStruct(&NW->IO,
883 NWC_ReadServerStatus,
885 NWC_DispatchWriteDone,
886 NWC_DispatchReadDone,
893 safestrncpy(((CitContext *)NW->IO.CitContext)->cs_host,
895 sizeof(((CitContext *)NW->IO.CitContext)->cs_host));
897 if (NW->IO.ConnectMe->IsIP) {
898 SetNWCState(&NW->IO, eNWCVSLookup);
899 QueueEventContext(&NW->IO,
902 else { /* uneducated admin has chosen to add DNS to the equation... */
903 SetNWCState(&NW->IO, eNWCVSConnecting);
904 QueueEventContext(&NW->IO,
905 nwc_get_one_host_ip);
911 * Poll other Citadel nodes and transfer inbound/outbound network data.
912 * Set "full" to nonzero to force a poll of every node, or to zero to poll
913 * only nodes to which we have data to send.
915 void network_poll_other_citadel_nodes(int full_poll, HashList *ignetcfg)
922 StrBuf *SpoolFileName;
926 if (GetCount(ignetcfg) ==0) {
927 syslog(LOG_DEBUG, "netpoll: no neighbor nodes are configured - not polling");
930 become_session(&networker_client_CC);
932 SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
934 Pos = GetNewHashPos(ignetcfg, 0);
936 while (GetNextHashPos(ignetcfg, Pos, &len, &key, &vCfg))
938 /* Use the string tokenizer to grab one line at a time */
939 if(server_shutting_down)
940 return;/* TODO free stuff*/
941 CtdlNodeConf *pNode = (CtdlNodeConf*) vCfg;
943 NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
944 memset(NW, 0, sizeof(AsyncNetworker));
946 NW->node = NewStrBufDup(pNode->NodeName);
947 NW->host = NewStrBufDup(pNode->Host);
948 NW->port = NewStrBufDup(pNode->Port);
949 NW->secret = NewStrBufDup(pNode->Secret);
951 if ( (StrLength(NW->node) != 0) &&
952 (StrLength(NW->secret) != 0) &&
953 (StrLength(NW->host) != 0) &&
954 (StrLength(NW->port) != 0))
959 StrBufAppendBufPlain(SpoolFileName, HKEY("/"), 0);
960 StrBufAppendBuf(SpoolFileName, NW->node, 0);
961 StrBufStripSlashes(SpoolFileName, 1);
963 if (access(ChrPtr(SpoolFileName), R_OK) == 0) {
968 if (poll && (StrLength(NW->host) > 0) && strcmp("0.0.0.0", ChrPtr(NW->host)))
970 NW->Url = NewStrBuf();
971 StrBufPrintf(NW->Url, "citadel://%s@%s:%s", ChrPtr(NW->secret), ChrPtr(NW->host), ChrPtr(NW->port));
972 if (!CtdlNetworkTalkingTo(SKEY(NW->node), NTT_CHECK))
980 FreeStrBuf(&SpoolFileName);
985 void network_do_clientqueue(void)
987 HashList *working_ignetcfg;
988 int full_processing = 1;
989 static time_t last_run = 0L;
992 * Run the full set of processing tasks no more frequently
993 * than once every n seconds
995 if ( (time(NULL) - last_run) < CtdlGetConfigLong("c_net_freq") )
998 syslog(LOG_DEBUG, "netpoll: full processing in %ld seconds.",
999 CtdlGetConfigLong("c_net_freq") - (time(NULL)- last_run)
1003 working_ignetcfg = CtdlLoadIgNetCfg();
1005 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1006 * then we poll everyone. Otherwise we only poll nodes we have stuff
1009 network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
1010 DeleteHash(&working_ignetcfg);
1014 * Module entry point
1016 CTDL_MODULE_INIT(network_client)
1020 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
1021 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER, PRIO_SEND + 10);
1024 return "networkclient";