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, 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"
70 #include "citadel_dirs.h"
73 #include "ctdl_module.h"
75 struct CitContext networker_client_CC;
77 #define NODE ChrPtr(((AsyncNetworker*)IO->Data)->node)
78 #define N ((AsyncNetworker*)IO->Data)->n
80 int NetworkClientDebugEnabled = 0;
82 #define NCDBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (NetworkClientDebugEnabled != 0))
84 #define EVN_syslog(LEVEL, FORMAT, ...) \
85 NCDBGLOG(LEVEL) syslog(LEVEL, \
86 "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT, \
87 IO->ID, CCID, NODE, N, __VA_ARGS__)
89 #define EVNM_syslog(LEVEL, FORMAT) \
90 NCDBGLOG(LEVEL) syslog(LEVEL, \
91 "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT, \
92 IO->ID, CCID, NODE, N)
94 #define EVNCS_syslog(LEVEL, FORMAT, ...) \
95 NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT, \
96 IO->ID, NODE, N, __VA_ARGS__)
98 #define EVNCSM_syslog(LEVEL, FORMAT) \
99 NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT, \
103 typedef enum _eNWCState {
117 typedef enum _eNWCVState {
132 ConstStr NWCStateStr[] = {
133 {HKEY("Looking up Host")},
134 {HKEY("Connecting host")},
135 {HKEY("Failed to connect")},
136 {HKEY("Rread Greeting")},
137 {HKEY("Authenticating")},
138 {HKEY("Auth failed by NTT")},
139 {HKEY("Auth failed")},
140 {HKEY("Downloading")},
141 {HKEY("Downloading Success")},
142 {HKEY("Uploading Spoolfile")},
143 {HKEY("Uploading done")},
147 void SetNWCState(AsyncIO *IO, eNWCVState State)
149 CitContext* CCC = IO->CitContext;
150 memcpy(CCC->cs_clientname, NWCStateStr[State].Key, NWCStateStr[State].len + 1);
153 typedef struct _async_networker {
155 DNSQueryParts HostLookup;
158 StrBuf *SpoolFileName;
159 StrBuf *tempFileName;
167 typedef eNextState(*NWClientHandler)(AsyncNetworker* NW);
168 eNextState nwc_get_one_host_ip(AsyncIO *IO);
170 eNextState nwc_connect_ip(AsyncIO *IO);
172 eNextState NWC_SendQUIT(AsyncNetworker *NW);
173 eNextState NWC_DispatchWriteDone(AsyncIO *IO);
175 void DeleteNetworker(void *vptr)
177 AsyncNetworker *NW = (AsyncNetworker *)vptr;
178 FreeStrBuf(&NW->SpoolFileName);
179 FreeStrBuf(&NW->tempFileName);
180 FreeStrBuf(&NW->node);
181 FreeStrBuf(&NW->host);
182 FreeStrBuf(&NW->port);
183 FreeStrBuf(&NW->secret);
184 FreeStrBuf(&NW->Url);
185 FreeStrBuf(&NW->IO.ErrMsg);
186 FreeAsyncIOContents(&NW->IO);
187 if (NW->HostLookup.VParsedDNSReply != NULL) {
188 NW->HostLookup.DNSReplyFree(NW->HostLookup.VParsedDNSReply);
189 NW->HostLookup.VParsedDNSReply = NULL;
194 #define NWC_DBG_SEND() EVN_syslog(LOG_DEBUG, ": > %s", ChrPtr(NW->IO.SendBuf.Buf))
195 #define NWC_DBG_READ() EVN_syslog(LOG_DEBUG, ": < %s\n", ChrPtr(NW->IO.IOBuf))
196 #define NWC_OK (strncasecmp(ChrPtr(NW->IO.IOBuf), "+OK", 3) == 0)
198 eNextState SendFailureMessage(AsyncIO *IO)
200 AsyncNetworker *NW = IO->Data;
204 strs[0] = ChrPtr(NW->node);
205 lens[0] = StrLength(NW->node);
207 strs[1] = ChrPtr(NW->IO.ErrMsg);
208 lens[1] = StrLength(NW->IO.ErrMsg);
210 ChrPtr(NW->IO.ErrMsg),
212 2, strs, (long*) &lens);
217 eNextState FinalizeNetworker(AsyncIO *IO)
219 AsyncNetworker *NW = (AsyncNetworker *)IO->Data;
221 CtdlNetworkTalkingTo(SKEY(NW->node), NTT_REMOVE);
223 DeleteNetworker(IO->Data);
227 eNextState NWC_ReadGreeting(AsyncNetworker *NW)
229 char connected_to[SIZ];
230 AsyncIO *IO = &NW->IO;
231 SetNWCState(IO, eNWCVSGreating);
233 /* Read the server greeting */
234 /* Check that the remote is who we think it is and warn the Aide if not */
235 extract_token (connected_to, ChrPtr(NW->IO.IOBuf), 1, ' ', sizeof connected_to);
236 if (strcmp(connected_to, ChrPtr(NW->node)) != 0)
238 if (NW->IO.ErrMsg == NULL)
239 NW->IO.ErrMsg = NewStrBuf();
240 StrBufPrintf(NW->IO.ErrMsg,
241 "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
242 connected_to, ChrPtr(NW->node));
243 EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
244 StopClientWatchers(IO, 1);
245 return QueueDBOperation(IO, SendFailureMessage);
250 eNextState NWC_SendAuth(AsyncNetworker *NW)
252 AsyncIO *IO = &NW->IO;
253 SetNWCState(IO, eNWCVSAuth);
254 /* We're talking to the correct node. Now identify ourselves. */
255 StrBufPrintf(NW->IO.SendBuf.Buf, "NETP %s|%s\n",
262 eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
264 AsyncIO *IO = &NW->IO;
266 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
272 int Error = atol(ChrPtr(NW->IO.IOBuf));
273 if (NW->IO.ErrMsg == NULL)
274 NW->IO.ErrMsg = NewStrBuf();
275 StrBufPrintf(NW->IO.ErrMsg,
276 "Connected to node \"%s\" but my secret wasn't accurate.\nReason was:%s\n",
277 ChrPtr(NW->node), ChrPtr(NW->IO.IOBuf) + 4);
279 SetNWCState(IO, eNWCVSAuthFailNTT);
281 "Already talking to %s; skipping this time.\n",
286 SetNWCState(IO, eNWCVSAuthFailNTT);
287 EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
288 StopClientWatchers(IO, 1);
289 return QueueDBOperation(IO, SendFailureMessage);
295 eNextState NWC_SendNDOP(AsyncNetworker *NW)
297 AsyncIO *IO = &NW->IO;
298 SetNWCState(IO, eNWCVSNDOP);
299 NW->tempFileName = NewStrBuf();
300 NW->SpoolFileName = NewStrBuf();
301 StrBufPrintf(NW->SpoolFileName,
305 time(NULL),// TODO: get time from libev
307 StrBufStripSlashes(NW->SpoolFileName, 1);
308 StrBufPrintf(NW->tempFileName,
312 time(NULL),// TODO: get time from libev
314 StrBufStripSlashes(NW->tempFileName, 1);
315 /* We're talking to the correct node. Now identify ourselves. */
316 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NDOP\n"));
321 eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
323 AsyncIO *IO = &NW->IO;
326 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
329 NW->IO.IOB.TotalSentAlready = 0;
330 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
331 EVN_syslog(LOG_DEBUG, "Expecting to transfer %d bytes\n", TotalSendSize);
332 if (TotalSendSize <= 0) {
333 NW->State = eNUOP - 1;
337 fd = open(ChrPtr(NW->tempFileName),
338 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
342 SetNWCState(IO, eNWCVSFail);
344 "cannot open %s: %s\n",
345 ChrPtr(NW->tempFileName),
348 NW->State = eQUIT - 1;
351 FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
357 SetNWCState(IO, eNWCVSFail);
362 eNextState NWC_SendREAD(AsyncNetworker *NW)
364 AsyncIO *IO = &NW->IO;
367 if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
370 * If shutting down we can exit here and unlink the temp file.
371 * this shouldn't loose us any messages.
373 if (server_shutting_down)
375 FDIOBufferDelete(&NW->IO.IOB);
376 unlink(ChrPtr(NW->tempFileName));
377 FDIOBufferDelete(&IO->IOB);
378 SetNWCState(IO, eNWCVSFail);
381 StrBufPrintf(NW->IO.SendBuf.Buf, "READ %ld|%ld\n",
382 NW->IO.IOB.TotalSentAlready,
383 NW->IO.IOB.TotalSendSize);
385 ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
386 ? IGNET_PACKET_SIZE :
387 (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
396 rc = NWC_DispatchWriteDone(&NW->IO);
403 eNextState NWC_ReadREADState(AsyncNetworker *NW)
405 AsyncIO *IO = &NW->IO;
407 if (ChrPtr(NW->IO.IOBuf)[0] == '6')
409 NW->IO.IOB.ChunkSendRemain =
410 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
413 FDIOBufferDelete(&IO->IOB);
416 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
417 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
420 AsyncIO *IO = &NW->IO;
422 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
426 FDIOBufferDelete(&NW->IO.IOB);
428 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
429 EVN_syslog(LOG_ALERT,
430 "Could not link %s to %s: %s\n",
431 ChrPtr(NW->tempFileName),
432 ChrPtr(NW->SpoolFileName),
436 unlink(ChrPtr(NW->tempFileName));
437 rc = NWC_DispatchWriteDone(&NW->IO);
443 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
444 return eSendReply; //NWC_DispatchWriteDone(&NW->IO);
448 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
451 AsyncIO *IO = &NW->IO;
452 /* we don't have any data to debug print here. */
453 if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
457 FDIOBufferDelete(&NW->IO.IOB);
458 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
459 EVN_syslog(LOG_ALERT,
460 "Could not link %s to %s: %s\n",
461 ChrPtr(NW->tempFileName),
462 ChrPtr(NW->SpoolFileName),
466 unlink(ChrPtr(NW->tempFileName));
467 rc = NWC_DispatchWriteDone(&NW->IO);
473 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
474 return NWC_DispatchWriteDone(&NW->IO);
477 eNextState NWC_SendCLOS(AsyncNetworker *NW)
479 AsyncIO *IO = &NW->IO;
480 SetNWCState(IO, eNWCVSNDOPDone);
481 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
486 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
488 AsyncIO *IO = &NW->IO;
490 FDIOBufferDelete(&IO->IOB);
491 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
492 return eTerminateConnection;
497 eNextState NWC_SendNUOP(AsyncNetworker *NW)
499 AsyncIO *IO = &NW->IO;
505 SetNWCState(IO, eNWCVSNUOP);
506 StrBufPrintf(NW->SpoolFileName,
510 StrBufStripSlashes(NW->SpoolFileName, 1);
512 fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
514 if (errno != ENOENT) {
516 "cannot open %s: %s\n",
517 ChrPtr(NW->SpoolFileName),
521 rc = NWC_SendQUIT(NW);
526 if (fstat(fd, &statbuf) == -1) {
527 EVN_syslog(LOG_CRIT, "FSTAT FAILED %s [%s]--\n",
528 ChrPtr(NW->SpoolFileName),
530 if (fd > 0) close(fd);
533 TotalSendSize = statbuf.st_size;
534 if (TotalSendSize == 0) {
535 EVNM_syslog(LOG_DEBUG,
536 "Nothing to send.\n");
538 rc = NWC_SendQUIT(NW);
540 if (fd > 0) close(fd);
543 FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
545 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
550 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
552 AsyncIO *IO = &NW->IO;
554 if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
555 FDIOBufferDelete(&IO->IOB);
561 eNextState NWC_SendWRIT(AsyncNetworker *NW)
563 AsyncIO *IO = &NW->IO;
564 StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT %ld\n",
565 NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
569 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
571 AsyncIO *IO = &NW->IO;
573 if (ChrPtr(NW->IO.IOBuf)[0] != '7')
575 FDIOBufferDelete(&IO->IOB);
579 NW->IO.IOB.ChunkSendRemain =
580 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
584 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
586 AsyncIO *IO = &NW->IO;
588 if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
592 FDIOBufferDelete(&IO->IOB);
593 rc = NWC_DispatchWriteDone(IO);
599 IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
600 rc = NWC_DispatchWriteDone(IO);
606 eNextState NWC_SendUCLS(AsyncNetworker *NW)
608 AsyncIO *IO = &NW->IO;
609 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
614 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
616 AsyncIO *IO = &NW->IO;
619 EVN_syslog(LOG_NOTICE, "Sent %ld octets to <%s>\n", NW->IO.IOB.ChunkSize, ChrPtr(NW->node));
620 if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
621 EVN_syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
622 unlink(ChrPtr(NW->SpoolFileName));
624 FDIOBufferDelete(&IO->IOB);
625 SetNWCState(IO, eNWCVSNUOPDone);
629 eNextState NWC_SendQUIT(AsyncNetworker *NW)
631 AsyncIO *IO = &NW->IO;
632 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
638 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
640 AsyncIO *IO = &NW->IO;
647 NWClientHandler NWC_ReadHandlers[] = {
660 long NWC_ConnTimeout = 100;
662 const long NWC_SendTimeouts[] = {
672 const ConstStr NWC[] = {
673 {HKEY("Connection broken during ")},
674 {HKEY("Connection broken during ")},
675 {HKEY("Connection broken during ")},
676 {HKEY("Connection broken during ")},
677 {HKEY("Connection broken during ")},
678 {HKEY("Connection broken during ")},
679 {HKEY("Connection broken during ")},
680 {HKEY("Connection broken during ")}
683 NWClientHandler NWC_SendHandlers[] = {
688 NWC_ReadREADBlobDone,
697 const long NWC_ReadTimeouts[] = {
713 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
715 AsyncNetworker *NW = IO->Data;
716 struct hostent *hostent;
720 hostent = NW->HostLookup.VParsedDNSReply;
721 if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) &&
722 (hostent != NULL) ) {
723 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
724 if (NW->IO.ConnectMe->IPv6) {
725 memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr,
726 &hostent->h_addr_list[0],
727 sizeof(struct in6_addr));
729 NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
730 NW->IO.ConnectMe->Addr.sin6_port = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
733 struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
734 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
735 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
736 memcpy(&addr->sin_addr.s_addr,
737 hostent->h_addr_list[0],
740 addr->sin_family = hostent->h_addrtype;
741 addr->sin_port = htons(504);/// default citadel port
744 return nwc_connect_ip(IO);
751 eNextState nwc_get_one_host_ip(AsyncIO *IO)
753 AsyncNetworker *NW = IO->Data;
755 * here we start with the lookup of one host.
758 EVN_syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
760 EVN_syslog(LOG_DEBUG,
761 "NWC client[%ld]: looking up %s-Record %s : %d ...\n",
763 (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
764 NW->IO.ConnectMe->Host,
765 NW->IO.ConnectMe->Port);
767 QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
768 NW->IO.ConnectMe->Host,
771 nwc_get_one_host_ip_done);
772 IO->NextState = eReadDNSReply;
773 return IO->NextState;
776 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
778 eReadState NWC_ReadServerStatus(AsyncIO *IO)
780 // AsyncNetworker *NW = IO->Data;
781 eReadState Finished = eBufferNotEmpty;
783 switch (IO->NextState) {
788 case eTerminateConnection:
790 Finished = eReadFail;
796 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
808 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
810 SetNWCState(IO, eNWCVSConnFail);
811 StopClientWatchers(IO, 1);
812 return QueueDBOperation(IO, SendFailureMessage);
815 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
817 AsyncIO *IO = &NW->IO;
818 double Timeout = 0.0;
820 EVN_syslog(LOG_DEBUG, "%s - %d\n", __FUNCTION__, NextTCPState);
822 switch (NextTCPState) {
828 Timeout = NWC_ReadTimeouts[NW->State];
839 case eTerminateConnection:
842 case eReadMore://// TODO
846 EVN_syslog(LOG_DEBUG,
851 SetNextTimeout(&NW->IO, Timeout*100);
856 eNextState NWC_DispatchReadDone(AsyncIO *IO)
858 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
859 AsyncNetworker *NW = IO->Data;
862 rc = NWC_ReadHandlers[NW->State](NW);
865 NWC_SetTimeout(rc, NW);
868 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
870 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
871 AsyncNetworker *NW = IO->Data;
874 rc = NWC_SendHandlers[NW->State](NW);
875 NWC_SetTimeout(rc, NW);
879 /*****************************************************************************/
880 /* Networker CLIENT ERROR CATCHERS */
881 /*****************************************************************************/
882 eNextState NWC_Terminate(AsyncIO *IO)
884 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
885 FinalizeNetworker(IO);
889 eNextState NWC_TerminateDB(AsyncIO *IO)
891 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
892 FinalizeNetworker(IO);
896 eNextState NWC_Timeout(AsyncIO *IO)
898 AsyncNetworker *NW = IO->Data;
899 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
901 if (NW->IO.ErrMsg == NULL)
902 NW->IO.ErrMsg = NewStrBuf();
903 StrBufPrintf(NW->IO.ErrMsg, "Timeout while talking to %s \r\n", ChrPtr(NW->host));
904 return NWC_FailNetworkConnection(IO);
906 eNextState NWC_ConnFail(AsyncIO *IO)
908 AsyncNetworker *NW = IO->Data;
910 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
911 if (NW->IO.ErrMsg == NULL)
912 NW->IO.ErrMsg = NewStrBuf();
913 StrBufPrintf(NW->IO.ErrMsg, "failed to connect %s \r\n", ChrPtr(NW->host));
915 return NWC_FailNetworkConnection(IO);
917 eNextState NWC_DNSFail(AsyncIO *IO)
919 AsyncNetworker *NW = IO->Data;
921 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
922 if (NW->IO.ErrMsg == NULL)
923 NW->IO.ErrMsg = NewStrBuf();
924 StrBufPrintf(NW->IO.ErrMsg, "failed to look up %s \r\n", ChrPtr(NW->host));
926 return NWC_FailNetworkConnection(IO);
928 eNextState NWC_Shutdown(AsyncIO *IO)
930 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
932 FinalizeNetworker(IO);
937 eNextState nwc_connect_ip(AsyncIO *IO)
939 AsyncNetworker *NW = IO->Data;
941 SetNWCState(&NW->IO, eNWCVSConnecting);
942 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
943 EVN_syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n",
948 return EvConnectSock(IO,
954 static int NetworkerCount = 0;
955 void RunNetworker(AsyncNetworker *NW)
957 NW->n = NetworkerCount++;
958 CtdlNetworkTalkingTo(SKEY(NW->node), NTT_ADD);
959 syslog(LOG_DEBUG, "NW[%s][%ld]: polling\n", ChrPtr(NW->node), NW->n);
960 ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
962 InitIOStruct(&NW->IO,
965 NWC_ReadServerStatus,
967 NWC_DispatchWriteDone,
968 NWC_DispatchReadDone,
975 safestrncpy(((CitContext *)NW->IO.CitContext)->cs_host,
977 sizeof(((CitContext *)NW->IO.CitContext)->cs_host));
979 if (NW->IO.ConnectMe->IsIP) {
980 SetNWCState(&NW->IO, eNWCVSLookup);
981 QueueEventContext(&NW->IO,
984 else { /* uneducated admin has chosen to add DNS to the equation... */
985 SetNWCState(&NW->IO, eNWCVSConnecting);
986 QueueEventContext(&NW->IO,
987 nwc_get_one_host_ip);
992 * Poll other Citadel nodes and transfer inbound/outbound network data.
993 * Set "full" to nonzero to force a poll of every node, or to zero to poll
994 * only nodes to which we have data to send.
996 void network_poll_other_citadel_nodes(int full_poll, HashList *ignetcfg)
1003 StrBuf *SpoolFileName;
1007 if (GetCount(ignetcfg) ==0) {
1008 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
1011 become_session(&networker_client_CC);
1013 SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
1015 Pos = GetNewHashPos(ignetcfg, 0);
1017 while (GetNextHashPos(ignetcfg, Pos, &len, &key, &vCfg))
1019 /* Use the string tokenizer to grab one line at a time */
1020 if(server_shutting_down)
1021 return;/* TODO free stuff*/
1022 CtdlNodeConf *pNode = (CtdlNodeConf*) vCfg;
1024 NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
1025 memset(NW, 0, sizeof(AsyncNetworker));
1027 NW->node = NewStrBufDup(pNode->NodeName);
1028 NW->host = NewStrBufDup(pNode->Host);
1029 NW->port = NewStrBufDup(pNode->Port);
1030 NW->secret = NewStrBufDup(pNode->Secret);
1032 if ( (StrLength(NW->node) != 0) &&
1033 (StrLength(NW->secret) != 0) &&
1034 (StrLength(NW->host) != 0) &&
1035 (StrLength(NW->port) != 0))
1040 StrBufAppendBufPlain(SpoolFileName, HKEY("/"), 0);
1041 StrBufAppendBuf(SpoolFileName, NW->node, 0);
1042 StrBufStripSlashes(SpoolFileName, 1);
1044 if (access(ChrPtr(SpoolFileName), R_OK) == 0) {
1050 (StrLength(NW->host) > 0) &&
1051 strcmp("0.0.0.0", ChrPtr(NW->host)))
1053 NW->Url = NewStrBuf();
1054 StrBufPrintf(NW->Url, "citadel://%s@%s:%s",
1058 if (!CtdlNetworkTalkingTo(SKEY(NW->node), NTT_CHECK))
1064 DeleteNetworker(NW);
1066 FreeStrBuf(&SpoolFileName);
1067 DeleteHashPos(&Pos);
1071 void network_do_clientqueue(void)
1073 HashList *working_ignetcfg;
1074 int full_processing = 1;
1075 static time_t last_run = 0L;
1078 * Run the full set of processing tasks no more frequently
1079 * than once every n seconds
1081 if ( (time(NULL) - last_run) < config.c_net_freq ) {
1082 full_processing = 0;
1083 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
1084 config.c_net_freq - (time(NULL)- last_run)
1088 working_ignetcfg = CtdlLoadIgNetCfg();
1090 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1091 * then we poll everyone. Otherwise we only poll nodes we have stuff
1094 network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
1095 DeleteHash(&working_ignetcfg);
1098 void LogDebugEnableNetworkClient(const int n)
1100 NetworkClientDebugEnabled = n;
1103 * Module entry point
1105 CTDL_MODULE_INIT(network_client)
1109 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
1111 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER, PRIO_SEND + 10);
1112 CtdlRegisterDebugFlagHook(HKEY("networkclient"), LogDebugEnableNetworkClient, &NetworkClientDebugEnabled);
1115 return "networkclient";