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;
176 syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
178 strs[0] = ChrPtr(NW->node);
179 lens[0] = StrLength(NW->node);
181 strs[1] = ChrPtr(NW->IO.ErrMsg);
182 lens[1] = StrLength(NW->IO.ErrMsg);
184 ChrPtr(NW->IO.ErrMsg),
186 2, strs, (long*) &lens,
193 eNextState FinalizeNetworker(AsyncIO *IO)
195 AsyncNetworker *NW = (AsyncNetworker *)IO->Data;
197 CtdlNetworkTalkingTo(SKEY(NW->node), NTT_REMOVE);
199 DeleteNetworker(IO->Data);
203 eNextState NWC_ReadGreeting(AsyncNetworker *NW)
205 char connected_to[SIZ];
206 AsyncIO *IO = &NW->IO;
207 SetNWCState(IO, eNWCVSGreating);
208 /* Read the server greeting */
209 /* Check that the remote is who we think it is and warn the Aide if not */
210 extract_token (connected_to, ChrPtr(NW->IO.IOBuf), 1, ' ', sizeof connected_to);
211 if (strcmp(connected_to, ChrPtr(NW->node)) != 0)
213 if (NW->IO.ErrMsg == NULL)
214 NW->IO.ErrMsg = NewStrBuf();
215 StrBufPrintf(NW->IO.ErrMsg,
216 "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
217 connected_to, ChrPtr(NW->node));
218 syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
220 return EventQueueDBOperation(IO, NWC_SendFailureMessage, 1);
225 eNextState NWC_SendAuth(AsyncNetworker *NW)
227 AsyncIO *IO = &NW->IO;
228 SetNWCState(IO, eNWCVSAuth);
229 /* We're talking to the correct node. Now identify ourselves. */
230 StrBufPrintf(NW->IO.SendBuf.Buf, "NETP %s|%s\n",
231 CtdlGetConfigStr("c_nodename"),
236 eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
238 AsyncIO *IO = &NW->IO;
239 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
245 int Error = atol(ChrPtr(NW->IO.IOBuf));
246 if (NW->IO.ErrMsg == NULL)
247 NW->IO.ErrMsg = NewStrBuf();
248 StrBufPrintf(NW->IO.ErrMsg,
249 "Connected to node \"%s\" but my secret wasn't accurate.\nReason was:%s\n",
250 ChrPtr(NW->node), ChrPtr(NW->IO.IOBuf) + 4);
252 SetNWCState(IO, eNWCVSAuthFailNTT);
254 "Already talking to %s; skipping this time.\n",
259 SetNWCState(IO, eNWCVSAuthFailNTT);
260 syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
261 return EventQueueDBOperation(IO, NWC_SendFailureMessage, 1);
267 eNextState NWC_SendNDOP(AsyncNetworker *NW)
269 AsyncIO *IO = &NW->IO;
270 SetNWCState(IO, eNWCVSNDOP);
271 NW->tempFileName = NewStrBuf();
272 NW->SpoolFileName = NewStrBuf();
273 StrBufPrintf(NW->SpoolFileName,
277 time(NULL),// TODO: get time from libev
279 StrBufStripSlashes(NW->SpoolFileName, 1);
280 StrBufPrintf(NW->tempFileName,
284 time(NULL),// TODO: get time from libev
286 StrBufStripSlashes(NW->tempFileName, 1);
287 /* We're talking to the correct node. Now identify ourselves. */
288 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NDOP\n"));
292 eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
294 AsyncIO *IO = &NW->IO;
296 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
298 int LogLevel = LOG_DEBUG;
300 NW->IO.IOB.TotalSentAlready = 0;
302 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
304 if (TotalSendSize > 0)
308 "Expecting to transfer %d bytes to %s\n",
310 ChrPtr(NW->tempFileName));
312 if (TotalSendSize <= 0) {
313 NW->State = eNUOP - 1;
317 fd = open(ChrPtr(NW->tempFileName),
318 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
322 SetNWCState(IO, eNWCVSFail);
324 "cannot open %s: %s\n",
325 ChrPtr(NW->tempFileName),
328 NW->State = eQUIT - 1;
331 FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
337 SetNWCState(IO, eNWCVSFail);
342 eNextState NWC_SendREAD(AsyncNetworker *NW)
344 AsyncIO *IO = &NW->IO;
347 if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
350 * If shutting down we can exit here and unlink the temp file.
351 * this shouldn't loose us any messages.
353 if (server_shutting_down)
355 FDIOBufferDelete(&NW->IO.IOB);
356 unlink(ChrPtr(NW->tempFileName));
357 FDIOBufferDelete(&IO->IOB);
358 SetNWCState(IO, eNWCVSFail);
361 StrBufPrintf(NW->IO.SendBuf.Buf, "READ "LOFF_T_FMT"|%ld\n",
362 NW->IO.IOB.TotalSentAlready,
363 NW->IO.IOB.TotalSendSize);
365 ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
366 ? IGNET_PACKET_SIZE :
367 (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
375 rc = NWC_DispatchWriteDone(&NW->IO);
381 eNextState NWC_ReadREADState(AsyncNetworker *NW)
383 AsyncIO *IO = &NW->IO;
384 if (ChrPtr(NW->IO.IOBuf)[0] == '6')
386 NW->IO.IOB.ChunkSendRemain =
387 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
390 FDIOBufferDelete(&IO->IOB);
393 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
394 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
397 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
401 FDIOBufferDelete(&NW->IO.IOB);
403 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
405 "Could not link %s to %s: %s\n",
406 ChrPtr(NW->tempFileName),
407 ChrPtr(NW->SpoolFileName),
413 ChrPtr(NW->tempFileName),
414 ChrPtr(NW->SpoolFileName));
417 unlink(ChrPtr(NW->tempFileName));
418 rc = NWC_DispatchWriteDone(&NW->IO);
424 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
425 return eSendReply; //NWC_DispatchWriteDone(&NW->IO);
429 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
432 /* we don't have any data to debug print here. */
433 if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
437 FDIOBufferDelete(&NW->IO.IOB);
438 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
440 "Could not link %s to %s: %s\n",
441 ChrPtr(NW->tempFileName),
442 ChrPtr(NW->SpoolFileName),
448 ChrPtr(NW->tempFileName),
449 ChrPtr(NW->SpoolFileName));
452 unlink(ChrPtr(NW->tempFileName));
453 rc = NWC_DispatchWriteDone(&NW->IO);
459 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
460 return NWC_DispatchWriteDone(&NW->IO);
463 eNextState NWC_SendCLOS(AsyncNetworker *NW)
465 AsyncIO *IO = &NW->IO;
466 SetNWCState(IO, eNWCVSNDOPDone);
467 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
471 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
473 AsyncIO *IO = &NW->IO;
474 FDIOBufferDelete(&IO->IOB);
475 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
476 return eTerminateConnection;
481 eNextState NWC_SendNUOP(AsyncNetworker *NW)
483 AsyncIO *IO = &NW->IO;
489 SetNWCState(IO, eNWCVSNUOP);
490 StrBufPrintf(NW->SpoolFileName,
494 StrBufStripSlashes(NW->SpoolFileName, 1);
496 fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
498 if (errno != ENOENT) {
500 "cannot open %s: %s\n",
501 ChrPtr(NW->SpoolFileName),
505 rc = NWC_SendQUIT(NW);
509 if (fstat(fd, &statbuf) == -1) {
510 syslog(LOG_CRIT, "FSTAT FAILED %s [%s]--\n",
511 ChrPtr(NW->SpoolFileName),
513 if (fd > 0) close(fd);
516 TotalSendSize = statbuf.st_size;
517 if (TotalSendSize == 0) {
519 "Nothing to send.\n");
521 rc = NWC_SendQUIT(NW);
522 if (fd > 0) close(fd);
528 "sending %s to %s\n",
529 ChrPtr(NW->SpoolFileName),
533 FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
535 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
539 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
541 AsyncIO *IO = &NW->IO;
542 if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
543 FDIOBufferDelete(&IO->IOB);
549 eNextState NWC_SendWRIT(AsyncNetworker *NW)
551 StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT "LOFF_T_FMT"\n",
552 NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
555 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
557 AsyncIO *IO = &NW->IO;
558 if (ChrPtr(NW->IO.IOBuf)[0] != '7')
560 FDIOBufferDelete(&IO->IOB);
564 NW->IO.IOB.ChunkSendRemain =
565 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
569 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
571 AsyncIO *IO = &NW->IO;
573 if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
577 FDIOBufferDelete(&IO->IOB);
578 rc = NWC_DispatchWriteDone(IO);
584 IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
585 rc = NWC_DispatchWriteDone(IO);
591 eNextState NWC_SendUCLS(AsyncNetworker *NW)
593 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
597 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
599 AsyncIO *IO = &NW->IO;
602 "Sent %s [%ld] octets to <%s>\n",
603 ChrPtr(NW->SpoolFileName),
604 NW->IO.IOB.ChunkSize,
607 if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
608 syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
609 unlink(ChrPtr(NW->SpoolFileName));
611 FDIOBufferDelete(&IO->IOB);
612 SetNWCState(IO, eNWCVSNUOPDone);
616 eNextState NWC_SendQUIT(AsyncNetworker *NW)
618 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
623 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
629 NWClientHandler NWC_ReadHandlers[] = {
642 long NWC_ConnTimeout = 100;
644 const long NWC_SendTimeouts[] = {
654 const ConstStr NWC[] = {
655 {HKEY("Connection broken during ")},
656 {HKEY("Connection broken during ")},
657 {HKEY("Connection broken during ")},
658 {HKEY("Connection broken during ")},
659 {HKEY("Connection broken during ")},
660 {HKEY("Connection broken during ")},
661 {HKEY("Connection broken during ")},
662 {HKEY("Connection broken during ")}
665 NWClientHandler NWC_SendHandlers[] = {
670 NWC_ReadREADBlobDone,
679 const long NWC_ReadTimeouts[] = {
693 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
695 AsyncNetworker *NW = IO->Data;
696 struct hostent *hostent;
700 hostent = NW->HostLookup.VParsedDNSReply;
701 if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) &&
702 (hostent != NULL) ) {
703 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
704 if (NW->IO.ConnectMe->IPv6) {
705 memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr,
706 &hostent->h_addr_list[0],
707 sizeof(struct in6_addr));
709 NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
710 NW->IO.ConnectMe->Addr.sin6_port = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
713 struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
714 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
715 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
716 memcpy(&addr->sin_addr.s_addr,
717 hostent->h_addr_list[0],
720 addr->sin_family = hostent->h_addrtype;
721 addr->sin_port = htons(504);/// default citadel port
724 return nwc_connect_ip(IO);
731 eNextState nwc_get_one_host_ip(AsyncIO *IO)
733 AsyncNetworker *NW = IO->Data;
735 * here we start with the lookup of one host.
738 syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
741 "NWC client[%ld]: looking up %s-Record %s : %d ...\n",
743 (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
744 NW->IO.ConnectMe->Host,
745 NW->IO.ConnectMe->Port);
747 QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
748 NW->IO.ConnectMe->Host,
751 nwc_get_one_host_ip_done);
752 IO->NextState = eReadDNSReply;
753 return IO->NextState;
756 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
758 eReadState NWC_ReadServerStatus(AsyncIO *IO)
760 // AsyncNetworker *NW = IO->Data;
761 eReadState Finished = eBufferNotEmpty;
763 switch (IO->NextState) {
768 case eTerminateConnection:
770 Finished = eReadFail;
776 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
788 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
790 SetNWCState(IO, eNWCVSConnFail);
791 return EventQueueDBOperation(IO, NWC_SendFailureMessage, 1);
794 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
796 double Timeout = 0.0;
798 //syslog(LOG_DEBUG, "%s - %d\n", __FUNCTION__, NextTCPState);
800 switch (NextTCPState) {
804 Timeout = NWC_ReadTimeouts[NW->State];
816 case eTerminateConnection:
826 SetNextTimeout(&NW->IO, Timeout*100);
831 eNextState NWC_DispatchReadDone(AsyncIO *IO)
833 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
834 AsyncNetworker *NW = IO->Data;
837 rc = NWC_ReadHandlers[NW->State](NW);
839 if ((rc != eReadMore) &&
845 NWC_SetTimeout(rc, NW);
849 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
851 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
852 AsyncNetworker *NW = IO->Data;
855 rc = NWC_SendHandlers[NW->State](NW);
856 NWC_SetTimeout(rc, NW);
860 /*****************************************************************************/
861 /* Networker CLIENT ERROR CATCHERS */
862 /*****************************************************************************/
863 eNextState NWC_Terminate(AsyncIO *IO)
865 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
866 FinalizeNetworker(IO);
870 eNextState NWC_TerminateDB(AsyncIO *IO)
872 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
873 FinalizeNetworker(IO);
877 eNextState NWC_Timeout(AsyncIO *IO)
879 AsyncNetworker *NW = IO->Data;
880 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
882 if (NW->IO.ErrMsg == NULL)
883 NW->IO.ErrMsg = NewStrBuf();
884 StrBufPrintf(NW->IO.ErrMsg, "Timeout while talking to %s \r\n", ChrPtr(NW->host));
885 return NWC_FailNetworkConnection(IO);
887 eNextState NWC_ConnFail(AsyncIO *IO)
889 AsyncNetworker *NW = IO->Data;
891 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
892 if (NW->IO.ErrMsg == NULL)
893 NW->IO.ErrMsg = NewStrBuf();
894 StrBufPrintf(NW->IO.ErrMsg, "failed to connect %s \r\n", ChrPtr(NW->host));
896 return NWC_FailNetworkConnection(IO);
898 eNextState NWC_DNSFail(AsyncIO *IO)
900 AsyncNetworker *NW = IO->Data;
902 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
903 if (NW->IO.ErrMsg == NULL)
904 NW->IO.ErrMsg = NewStrBuf();
905 StrBufPrintf(NW->IO.ErrMsg, "failed to look up %s \r\n", ChrPtr(NW->host));
907 return NWC_FailNetworkConnection(IO);
909 eNextState NWC_Shutdown(AsyncIO *IO)
911 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
913 FinalizeNetworker(IO);
918 eNextState nwc_connect_ip(AsyncIO *IO)
920 AsyncNetworker *NW = IO->Data;
922 SetNWCState(&NW->IO, eNWCVSConnecting);
923 syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
924 syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n",
929 return EvConnectSock(IO,
935 static int NetworkerCount = 0;
936 void RunNetworker(AsyncNetworker *NW)
938 NW->n = NetworkerCount++;
939 CtdlNetworkTalkingTo(SKEY(NW->node), NTT_ADD);
940 syslog(LOG_DEBUG, "NW[%s][%ld]: polling\n", ChrPtr(NW->node), NW->n);
941 ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
943 InitIOStruct(&NW->IO,
946 NWC_ReadServerStatus,
948 NWC_DispatchWriteDone,
949 NWC_DispatchReadDone,
956 safestrncpy(((CitContext *)NW->IO.CitContext)->cs_host,
958 sizeof(((CitContext *)NW->IO.CitContext)->cs_host));
960 if (NW->IO.ConnectMe->IsIP) {
961 SetNWCState(&NW->IO, eNWCVSLookup);
962 QueueEventContext(&NW->IO,
965 else { /* uneducated admin has chosen to add DNS to the equation... */
966 SetNWCState(&NW->IO, eNWCVSConnecting);
967 QueueEventContext(&NW->IO,
968 nwc_get_one_host_ip);
973 * Poll other Citadel nodes and transfer inbound/outbound network data.
974 * Set "full" to nonzero to force a poll of every node, or to zero to poll
975 * only nodes to which we have data to send.
977 void network_poll_other_citadel_nodes(int full_poll, HashList *ignetcfg)
984 StrBuf *SpoolFileName;
988 if (GetCount(ignetcfg) ==0) {
989 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
992 become_session(&networker_client_CC);
994 SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
996 Pos = GetNewHashPos(ignetcfg, 0);
998 while (GetNextHashPos(ignetcfg, Pos, &len, &key, &vCfg))
1000 /* Use the string tokenizer to grab one line at a time */
1001 if(server_shutting_down)
1002 return;/* TODO free stuff*/
1003 CtdlNodeConf *pNode = (CtdlNodeConf*) vCfg;
1005 NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
1006 memset(NW, 0, sizeof(AsyncNetworker));
1008 NW->node = NewStrBufDup(pNode->NodeName);
1009 NW->host = NewStrBufDup(pNode->Host);
1010 NW->port = NewStrBufDup(pNode->Port);
1011 NW->secret = NewStrBufDup(pNode->Secret);
1013 if ( (StrLength(NW->node) != 0) &&
1014 (StrLength(NW->secret) != 0) &&
1015 (StrLength(NW->host) != 0) &&
1016 (StrLength(NW->port) != 0))
1021 StrBufAppendBufPlain(SpoolFileName, HKEY("/"), 0);
1022 StrBufAppendBuf(SpoolFileName, NW->node, 0);
1023 StrBufStripSlashes(SpoolFileName, 1);
1025 if (access(ChrPtr(SpoolFileName), R_OK) == 0) {
1031 (StrLength(NW->host) > 0) &&
1032 strcmp("0.0.0.0", ChrPtr(NW->host)))
1034 NW->Url = NewStrBuf();
1035 StrBufPrintf(NW->Url, "citadel://%s@%s:%s",
1039 if (!CtdlNetworkTalkingTo(SKEY(NW->node), NTT_CHECK))
1045 DeleteNetworker(NW);
1047 FreeStrBuf(&SpoolFileName);
1048 DeleteHashPos(&Pos);
1052 void network_do_clientqueue(void)
1054 HashList *working_ignetcfg;
1055 int full_processing = 1;
1056 static time_t last_run = 0L;
1059 * Run the full set of processing tasks no more frequently
1060 * than once every n seconds
1062 if ( (time(NULL) - last_run) < CtdlGetConfigLong("c_net_freq") )
1064 full_processing = 0;
1065 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
1066 CtdlGetConfigLong("c_net_freq") - (time(NULL)- last_run)
1070 working_ignetcfg = CtdlLoadIgNetCfg();
1072 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1073 * then we poll everyone. Otherwise we only poll nodes we have stuff
1076 network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
1077 DeleteHash(&working_ignetcfg);
1081 * Module entry point
1083 CTDL_MODULE_INIT(network_client)
1087 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
1088 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER, PRIO_SEND + 10);
1091 return "networkclient";