2 * This module handles shared rooms, inter-Citadel mail, and outbound
3 * mailing list processing.
5 * Copyright (c) 2000-2012 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 as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * ** NOTE ** A word on the S_NETCONFIGS semaphore:
22 * This is a fairly high-level type of critical section. It ensures that no
23 * two threads work on the netconfigs files at the same time. Since we do
24 * so many things inside these, here are the rules:
25 * 1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
26 * 2. Do *not* perform any I/O with the client during these sections.
41 #include <sys/types.h>
43 #if TIME_WITH_SYS_TIME
44 # include <sys/time.h>
48 # include <sys/time.h>
56 # if HAVE_SYS_SYSCALL_H
57 # include <sys/syscall.h>
64 #include <libcitadel.h>
67 #include "citserver.h"
73 #include "internet_addressing.h"
74 #include "serv_network.h"
75 #include "clientsocket.h"
77 #include "citadel_dirs.h"
86 #include "netconfig.h"
87 #include "ctdl_module.h"
89 struct CitContext networker_client_CC;
91 #define NODE ChrPtr(((AsyncNetworker*)IO->Data)->node)
92 #define N ((AsyncNetworker*)IO->Data)->n
94 int NetworkClientDebugEnabled = 0;
96 #define NCDBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (NetworkClientDebugEnabled != 0))
98 #define EVN_syslog(LEVEL, FORMAT, ...) \
99 NCDBGLOG(LEVEL) syslog(LEVEL, \
100 "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT, \
101 IO->ID, CCID, NODE, N, __VA_ARGS__)
103 #define EVNM_syslog(LEVEL, FORMAT) \
104 NCDBGLOG(LEVEL) syslog(LEVEL, \
105 "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT, \
106 IO->ID, CCID, NODE, N)
108 #define EVNCS_syslog(LEVEL, FORMAT, ...) \
109 NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT, \
110 IO->ID, NODE, N, __VA_ARGS__)
112 #define EVNCSM_syslog(LEVEL, FORMAT) \
113 NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT, \
117 typedef enum _eNWCState {
132 typedef struct _async_networker {
134 DNSQueryParts HostLookup;
137 StrBuf *SpoolFileName;
138 StrBuf *tempFileName;
146 typedef eNextState(*NWClientHandler)(AsyncNetworker* NW);
147 eNextState nwc_get_one_host_ip(AsyncIO *IO);
149 eNextState nwc_connect_ip(AsyncIO *IO);
151 eNextState NWC_SendQUIT(AsyncNetworker *NW);
152 eNextState NWC_DispatchWriteDone(AsyncIO *IO);
154 void DeleteNetworker(void *vptr)
156 AsyncNetworker *NW = (AsyncNetworker *)vptr;
157 FreeStrBuf(&NW->SpoolFileName);
158 FreeStrBuf(&NW->tempFileName);
159 FreeStrBuf(&NW->node);
160 FreeStrBuf(&NW->host);
161 FreeStrBuf(&NW->port);
162 FreeStrBuf(&NW->secret);
163 FreeStrBuf(&NW->Url);
164 FreeStrBuf(&NW->IO.ErrMsg);
165 FreeAsyncIOContents(&NW->IO);
166 if (NW->HostLookup.VParsedDNSReply != NULL) {
167 NW->HostLookup.DNSReplyFree(NW->HostLookup.VParsedDNSReply);
168 NW->HostLookup.VParsedDNSReply = NULL;
173 #define NWC_DBG_SEND() EVN_syslog(LOG_DEBUG, ": > %s", ChrPtr(NW->IO.SendBuf.Buf))
174 #define NWC_DBG_READ() EVN_syslog(LOG_DEBUG, ": < %s\n", ChrPtr(NW->IO.IOBuf))
175 #define NWC_OK (strncasecmp(ChrPtr(NW->IO.IOBuf), "+OK", 3) == 0)
177 eNextState SendFailureMessage(AsyncIO *IO)
179 AsyncNetworker *NW = IO->Data;
183 strs[0] = ChrPtr(NW->node);
184 lens[0] = StrLength(NW->node);
186 strs[1] = ChrPtr(NW->IO.ErrMsg);
187 lens[1] = StrLength(NW->IO.ErrMsg);
189 ChrPtr(NW->IO.ErrMsg),
191 2, strs, (long*) &lens);
196 eNextState FinalizeNetworker(AsyncIO *IO)
198 AsyncNetworker *NW = (AsyncNetworker *)IO->Data;
200 network_talking_to(SKEY(NW->node), NTT_REMOVE);
202 DeleteNetworker(IO->Data);
206 eNextState NWC_ReadGreeting(AsyncNetworker *NW)
208 char connected_to[SIZ];
209 AsyncIO *IO = &NW->IO;
211 /* Read the server greeting */
212 /* Check that the remote is who we think it is and warn the Aide if not */
213 extract_token (connected_to, ChrPtr(NW->IO.IOBuf), 1, ' ', sizeof connected_to);
214 if (strcmp(connected_to, ChrPtr(NW->node)) != 0)
216 if (NW->IO.ErrMsg == NULL)
217 NW->IO.ErrMsg = NewStrBuf();
218 StrBufPrintf(NW->IO.ErrMsg,
219 "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
220 connected_to, ChrPtr(NW->node));
221 EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
222 StopClientWatchers(IO, 1);
223 return QueueDBOperation(IO, SendFailureMessage);
228 eNextState NWC_SendAuth(AsyncNetworker *NW)
230 AsyncIO *IO = &NW->IO;
231 /* We're talking to the correct node. Now identify ourselves. */
232 StrBufPrintf(NW->IO.SendBuf.Buf, "NETP %s|%s\n",
239 eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
241 AsyncIO *IO = &NW->IO;
243 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
249 int Error = atol(ChrPtr(NW->IO.IOBuf));
250 if (NW->IO.ErrMsg == NULL)
251 NW->IO.ErrMsg = NewStrBuf();
252 StrBufPrintf(NW->IO.ErrMsg,
253 "Connected to node \"%s\" but my secret wasn't accurate.\nReason was:%s\n",
254 ChrPtr(NW->node), ChrPtr(NW->IO.IOBuf) + 4);
257 "Already talking to %s; skipping this time.\n",
262 EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
263 StopClientWatchers(IO, 1);
264 return QueueDBOperation(IO, SendFailureMessage);
270 eNextState NWC_SendNDOP(AsyncNetworker *NW)
272 AsyncIO *IO = &NW->IO;
273 NW->tempFileName = NewStrBuf();
274 NW->SpoolFileName = NewStrBuf();
275 StrBufPrintf(NW->SpoolFileName,
279 time(NULL),// TODO: get time from libev
281 StrBufStripSlashes(NW->SpoolFileName, 1);
282 StrBufPrintf(NW->tempFileName,
286 time(NULL),// TODO: get time from libev
288 StrBufStripSlashes(NW->tempFileName, 1);
289 /* We're talking to the correct node. Now identify ourselves. */
290 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NDOP\n"));
295 eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
297 AsyncIO *IO = &NW->IO;
300 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
303 NW->IO.IOB.TotalSentAlready = 0;
304 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
305 EVN_syslog(LOG_DEBUG, "Expecting to transfer %d bytes\n", TotalSendSize);
306 if (TotalSendSize <= 0) {
307 NW->State = eNUOP - 1;
311 fd = open(ChrPtr(NW->tempFileName),
312 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
317 "cannot open %s: %s\n",
318 ChrPtr(NW->tempFileName),
321 NW->State = eQUIT - 1;
324 FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
334 eNextState NWC_SendREAD(AsyncNetworker *NW)
336 AsyncIO *IO = &NW->IO;
339 if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
342 * If shutting down we can exit here and unlink the temp file.
343 * this shouldn't loose us any messages.
345 if (server_shutting_down)
347 FDIOBufferDelete(&NW->IO.IOB);
348 unlink(ChrPtr(NW->tempFileName));
349 FDIOBufferDelete(&IO->IOB);
352 StrBufPrintf(NW->IO.SendBuf.Buf, "READ %ld|%ld\n",
353 NW->IO.IOB.TotalSentAlready,
354 NW->IO.IOB.TotalSendSize);
356 ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
357 ? IGNET_PACKET_SIZE :
358 (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
367 rc = NWC_DispatchWriteDone(&NW->IO);
374 eNextState NWC_ReadREADState(AsyncNetworker *NW)
376 AsyncIO *IO = &NW->IO;
378 if (ChrPtr(NW->IO.IOBuf)[0] == '6')
380 NW->IO.IOB.ChunkSendRemain =
381 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
384 FDIOBufferDelete(&IO->IOB);
387 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
388 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
391 AsyncIO *IO = &NW->IO;
393 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
397 FDIOBufferDelete(&NW->IO.IOB);
399 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
400 EVN_syslog(LOG_ALERT,
401 "Could not link %s to %s: %s\n",
402 ChrPtr(NW->tempFileName),
403 ChrPtr(NW->SpoolFileName),
407 unlink(ChrPtr(NW->tempFileName));
408 rc = NWC_DispatchWriteDone(&NW->IO);
414 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
415 return eSendReply; //NWC_DispatchWriteDone(&NW->IO);
419 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
422 AsyncIO *IO = &NW->IO;
423 /* we don't have any data to debug print here. */
424 if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
428 FDIOBufferDelete(&NW->IO.IOB);
429 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
430 EVN_syslog(LOG_ALERT,
431 "Could not link %s to %s: %s\n",
432 ChrPtr(NW->tempFileName),
433 ChrPtr(NW->SpoolFileName),
437 unlink(ChrPtr(NW->tempFileName));
438 rc = NWC_DispatchWriteDone(&NW->IO);
444 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
445 return NWC_DispatchWriteDone(&NW->IO);
448 eNextState NWC_SendCLOS(AsyncNetworker *NW)
450 AsyncIO *IO = &NW->IO;
451 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
456 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
458 AsyncIO *IO = &NW->IO;
460 FDIOBufferDelete(&IO->IOB);
461 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
462 return eTerminateConnection;
467 eNextState NWC_SendNUOP(AsyncNetworker *NW)
469 AsyncIO *IO = &NW->IO;
475 StrBufPrintf(NW->SpoolFileName,
479 StrBufStripSlashes(NW->SpoolFileName, 1);
481 fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
483 if (errno != ENOENT) {
485 "cannot open %s: %s\n",
486 ChrPtr(NW->SpoolFileName),
490 rc = NWC_SendQUIT(NW);
495 if (fstat(fd, &statbuf) == -1) {
496 EVN_syslog(LOG_CRIT, "FSTAT FAILED %s [%s]--\n",
497 ChrPtr(NW->SpoolFileName),
499 if (fd > 0) close(fd);
502 TotalSendSize = statbuf.st_size;
503 if (TotalSendSize == 0) {
504 EVNM_syslog(LOG_DEBUG,
505 "Nothing to send.\n");
507 rc = NWC_SendQUIT(NW);
509 if (fd > 0) close(fd);
512 FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
514 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
519 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
521 AsyncIO *IO = &NW->IO;
523 if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
524 FDIOBufferDelete(&IO->IOB);
530 eNextState NWC_SendWRIT(AsyncNetworker *NW)
532 AsyncIO *IO = &NW->IO;
533 StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT %ld\n",
534 NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
538 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
540 AsyncIO *IO = &NW->IO;
542 if (ChrPtr(NW->IO.IOBuf)[0] != '7')
544 FDIOBufferDelete(&IO->IOB);
548 NW->IO.IOB.ChunkSendRemain =
549 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
553 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
555 AsyncIO *IO = &NW->IO;
557 if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
561 FDIOBufferDelete(&IO->IOB);
562 rc = NWC_DispatchWriteDone(IO);
568 IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
569 rc = NWC_DispatchWriteDone(IO);
575 eNextState NWC_SendUCLS(AsyncNetworker *NW)
577 AsyncIO *IO = &NW->IO;
578 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
583 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
585 AsyncIO *IO = &NW->IO;
588 EVN_syslog(LOG_NOTICE, "Sent %ld octets to <%s>\n", NW->IO.IOB.ChunkSize, ChrPtr(NW->node));
589 if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
590 EVN_syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
591 unlink(ChrPtr(NW->SpoolFileName));
593 FDIOBufferDelete(&IO->IOB);
597 eNextState NWC_SendQUIT(AsyncNetworker *NW)
599 AsyncIO *IO = &NW->IO;
600 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
606 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
608 AsyncIO *IO = &NW->IO;
615 NWClientHandler NWC_ReadHandlers[] = {
628 long NWC_ConnTimeout = 100;
630 const long NWC_SendTimeouts[] = {
640 const ConstStr NWC[] = {
641 {HKEY("Connection broken during ")},
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 ")}
651 NWClientHandler NWC_SendHandlers[] = {
656 NWC_ReadREADBlobDone,
665 const long NWC_ReadTimeouts[] = {
681 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
683 AsyncNetworker *NW = IO->Data;
684 struct hostent *hostent;
688 hostent = NW->HostLookup.VParsedDNSReply;
689 if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) &&
690 (hostent != NULL) ) {
691 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
692 if (NW->IO.ConnectMe->IPv6) {
693 memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr,
694 &hostent->h_addr_list[0],
695 sizeof(struct in6_addr));
697 NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
698 NW->IO.ConnectMe->Addr.sin6_port = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
701 struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
702 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
703 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
704 memcpy(&addr->sin_addr.s_addr,
705 hostent->h_addr_list[0],
708 addr->sin_family = hostent->h_addrtype;
709 addr->sin_port = htons(504);/// default citadel port
712 return nwc_connect_ip(IO);
719 eNextState nwc_get_one_host_ip(AsyncIO *IO)
721 AsyncNetworker *NW = IO->Data;
723 * here we start with the lookup of one host.
726 EVN_syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
728 EVN_syslog(LOG_DEBUG,
729 "NWC client[%ld]: looking up %s-Record %s : %d ...\n",
731 (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
732 NW->IO.ConnectMe->Host,
733 NW->IO.ConnectMe->Port);
735 QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
736 NW->IO.ConnectMe->Host,
739 nwc_get_one_host_ip_done);
740 IO->NextState = eReadDNSReply;
741 return IO->NextState;
744 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
746 eReadState NWC_ReadServerStatus(AsyncIO *IO)
748 // AsyncNetworker *NW = IO->Data;
749 eReadState Finished = eBufferNotEmpty;
751 switch (IO->NextState) {
756 case eTerminateConnection:
758 Finished = eReadFail;
764 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
776 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
778 StopClientWatchers(IO, 1);
779 return QueueDBOperation(IO, SendFailureMessage);
782 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
784 AsyncIO *IO = &NW->IO;
785 double Timeout = 0.0;
787 EVN_syslog(LOG_DEBUG, "%s - %d\n", __FUNCTION__, NextTCPState);
789 switch (NextTCPState) {
795 Timeout = NWC_ReadTimeouts[NW->State];
806 case eTerminateConnection:
809 case eReadMore://// TODO
813 EVN_syslog(LOG_DEBUG,
818 SetNextTimeout(&NW->IO, Timeout*100);
823 eNextState NWC_DispatchReadDone(AsyncIO *IO)
825 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
826 AsyncNetworker *NW = IO->Data;
829 rc = NWC_ReadHandlers[NW->State](NW);
832 NWC_SetTimeout(rc, NW);
835 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
837 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
838 AsyncNetworker *NW = IO->Data;
841 rc = NWC_SendHandlers[NW->State](NW);
842 NWC_SetTimeout(rc, NW);
846 /*****************************************************************************/
847 /* Networker CLIENT ERROR CATCHERS */
848 /*****************************************************************************/
849 eNextState NWC_Terminate(AsyncIO *IO)
851 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
852 FinalizeNetworker(IO);
856 eNextState NWC_TerminateDB(AsyncIO *IO)
858 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
859 FinalizeNetworker(IO);
863 eNextState NWC_Timeout(AsyncIO *IO)
865 AsyncNetworker *NW = IO->Data;
866 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
868 if (NW->IO.ErrMsg == NULL)
869 NW->IO.ErrMsg = NewStrBuf();
870 StrBufPrintf(NW->IO.ErrMsg, "Timeout while talking to %s \r\n", ChrPtr(NW->host));
871 return NWC_FailNetworkConnection(IO);
873 eNextState NWC_ConnFail(AsyncIO *IO)
875 AsyncNetworker *NW = IO->Data;
877 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
878 if (NW->IO.ErrMsg == NULL)
879 NW->IO.ErrMsg = NewStrBuf();
880 StrBufPrintf(NW->IO.ErrMsg, "failed to connect %s \r\n", ChrPtr(NW->host));
882 return NWC_FailNetworkConnection(IO);
884 eNextState NWC_DNSFail(AsyncIO *IO)
886 AsyncNetworker *NW = IO->Data;
888 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
889 if (NW->IO.ErrMsg == NULL)
890 NW->IO.ErrMsg = NewStrBuf();
891 StrBufPrintf(NW->IO.ErrMsg, "failed to look up %s \r\n", ChrPtr(NW->host));
893 return NWC_FailNetworkConnection(IO);
895 eNextState NWC_Shutdown(AsyncIO *IO)
897 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
899 FinalizeNetworker(IO);
904 eNextState nwc_connect_ip(AsyncIO *IO)
906 AsyncNetworker *NW = IO->Data;
908 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
909 EVN_syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n",
914 return EvConnectSock(IO,
920 static int NetworkerCount = 0;
921 void RunNetworker(AsyncNetworker *NW)
923 NW->n = NetworkerCount++;
924 network_talking_to(SKEY(NW->node), NTT_ADD);
925 syslog(LOG_DEBUG, "NW[%s][%ld]: polling\n", ChrPtr(NW->node), NW->n);
926 ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
928 InitIOStruct(&NW->IO,
931 NWC_ReadServerStatus,
933 NWC_DispatchWriteDone,
934 NWC_DispatchReadDone,
941 safestrncpy(((CitContext *)NW->IO.CitContext)->cs_host,
943 sizeof(((CitContext *)NW->IO.CitContext)->cs_host));
945 if (NW->IO.ConnectMe->IsIP) {
946 QueueEventContext(&NW->IO,
949 else { /* uneducated admin has chosen to add DNS to the equation... */
950 QueueEventContext(&NW->IO,
951 nwc_get_one_host_ip);
956 * Poll other Citadel nodes and transfer inbound/outbound network data.
957 * Set "full" to nonzero to force a poll of every node, or to zero to poll
958 * only nodes to which we have data to send.
960 void network_poll_other_citadel_nodes(int full_poll, HashList *ignetcfg)
967 StrBuf *SpoolFileName;
971 if (GetCount(ignetcfg) ==0) {
972 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
975 become_session(&networker_client_CC);
977 SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
979 Pos = GetNewHashPos(ignetcfg, 0);
981 while (GetNextHashPos(ignetcfg, Pos, &len, &key, &vCfg))
983 /* Use the string tokenizer to grab one line at a time */
984 if(server_shutting_down)
985 return;/* TODO free stuff*/
986 NodeConf *pNode = (NodeConf*) vCfg;
988 NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
989 memset(NW, 0, sizeof(AsyncNetworker));
991 NW->node = NewStrBufDup(pNode->NodeName);
992 NW->host = NewStrBufDup(pNode->Host);
993 NW->port = NewStrBufDup(pNode->Port);
994 NW->secret = NewStrBufDup(pNode->Secret);
996 if ( (StrLength(NW->node) != 0) &&
997 (StrLength(NW->secret) != 0) &&
998 (StrLength(NW->host) != 0) &&
999 (StrLength(NW->port) != 0))
1004 StrBufAppendBufPlain(SpoolFileName, HKEY("/"), 0);
1005 StrBufAppendBuf(SpoolFileName, NW->node, 0);
1006 StrBufStripSlashes(SpoolFileName, 1);
1008 if (access(ChrPtr(SpoolFileName), R_OK) == 0) {
1014 (StrLength(NW->host) > 0) &&
1015 strcmp("0.0.0.0", ChrPtr(NW->host)))
1017 NW->Url = NewStrBuf();
1018 StrBufPrintf(NW->Url, "citadel://%s@%s:%s",
1022 if (!network_talking_to(SKEY(NW->node), NTT_CHECK))
1028 DeleteNetworker(NW);
1030 FreeStrBuf(&SpoolFileName);
1031 DeleteHashPos(&Pos);
1035 void network_do_clientqueue(void)
1037 HashList *working_ignetcfg;
1038 int full_processing = 1;
1039 static time_t last_run = 0L;
1042 * Run the full set of processing tasks no more frequently
1043 * than once every n seconds
1045 if ( (time(NULL) - last_run) < config.c_net_freq ) {
1046 full_processing = 0;
1047 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
1048 config.c_net_freq - (time(NULL)- last_run)
1052 working_ignetcfg = load_ignetcfg();
1054 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1055 * then we poll everyone. Otherwise we only poll nodes we have stuff
1058 network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
1059 DeleteHash(&working_ignetcfg);
1062 void LogDebugEnableNetworkClient(const int n)
1064 NetworkClientDebugEnabled = n;
1067 * Module entry point
1069 CTDL_MODULE_INIT(network_client)
1073 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
1075 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER, PRIO_SEND + 10);
1076 CtdlRegisterDebugFlagHook(HKEY("networkclient"), LogDebugEnableNetworkClient, &NetworkClientDebugEnabled);
1079 return "networkclient";