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 "serv_network.h"
69 #include "clientsocket.h"
71 #include "citadel_dirs.h"
74 #include "ctdl_module.h"
76 struct CitContext networker_client_CC;
78 #define NODE ChrPtr(((AsyncNetworker*)IO->Data)->node)
79 #define N ((AsyncNetworker*)IO->Data)->n
81 int NetworkClientDebugEnabled = 0;
83 #define NCDBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (NetworkClientDebugEnabled != 0))
85 #define EVN_syslog(LEVEL, FORMAT, ...) \
86 NCDBGLOG(LEVEL) syslog(LEVEL, \
87 "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT, \
88 IO->ID, CCID, NODE, N, __VA_ARGS__)
90 #define EVNM_syslog(LEVEL, FORMAT) \
91 NCDBGLOG(LEVEL) syslog(LEVEL, \
92 "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT, \
93 IO->ID, CCID, NODE, N)
95 #define EVNCS_syslog(LEVEL, FORMAT, ...) \
96 NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT, \
97 IO->ID, NODE, N, __VA_ARGS__)
99 #define EVNCSM_syslog(LEVEL, FORMAT) \
100 NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT, \
104 typedef enum _eNWCState {
118 typedef enum _eNWCVState {
133 ConstStr NWCStateStr[] = {
134 {HKEY("Looking up Host")},
135 {HKEY("Connecting host")},
136 {HKEY("Failed to connect")},
137 {HKEY("Rread Greeting")},
138 {HKEY("Authenticating")},
139 {HKEY("Auth failed by NTT")},
140 {HKEY("Auth failed")},
141 {HKEY("Downloading")},
142 {HKEY("Downloading Success")},
143 {HKEY("Uploading Spoolfile")},
144 {HKEY("Uploading done")},
148 void SetNWCState(AsyncIO *IO, eNWCVState State)
150 CitContext* CCC = IO->CitContext;
151 memcpy(CCC->cs_clientname, NWCStateStr[State].Key, NWCStateStr[State].len + 1);
154 typedef struct _async_networker {
156 DNSQueryParts HostLookup;
159 StrBuf *SpoolFileName;
160 StrBuf *tempFileName;
168 typedef eNextState(*NWClientHandler)(AsyncNetworker* NW);
169 eNextState nwc_get_one_host_ip(AsyncIO *IO);
171 eNextState nwc_connect_ip(AsyncIO *IO);
173 eNextState NWC_SendQUIT(AsyncNetworker *NW);
174 eNextState NWC_DispatchWriteDone(AsyncIO *IO);
176 void DeleteNetworker(void *vptr)
178 AsyncNetworker *NW = (AsyncNetworker *)vptr;
179 FreeStrBuf(&NW->SpoolFileName);
180 FreeStrBuf(&NW->tempFileName);
181 FreeStrBuf(&NW->node);
182 FreeStrBuf(&NW->host);
183 FreeStrBuf(&NW->port);
184 FreeStrBuf(&NW->secret);
185 FreeStrBuf(&NW->Url);
186 FreeStrBuf(&NW->IO.ErrMsg);
187 FreeAsyncIOContents(&NW->IO);
188 if (NW->HostLookup.VParsedDNSReply != NULL) {
189 NW->HostLookup.DNSReplyFree(NW->HostLookup.VParsedDNSReply);
190 NW->HostLookup.VParsedDNSReply = NULL;
195 #define NWC_DBG_SEND() EVN_syslog(LOG_DEBUG, ": > %s", ChrPtr(NW->IO.SendBuf.Buf))
196 #define NWC_DBG_READ() EVN_syslog(LOG_DEBUG, ": < %s\n", ChrPtr(NW->IO.IOBuf))
197 #define NWC_OK (strncasecmp(ChrPtr(NW->IO.IOBuf), "+OK", 3) == 0)
199 eNextState SendFailureMessage(AsyncIO *IO)
201 AsyncNetworker *NW = IO->Data;
205 strs[0] = ChrPtr(NW->node);
206 lens[0] = StrLength(NW->node);
208 strs[1] = ChrPtr(NW->IO.ErrMsg);
209 lens[1] = StrLength(NW->IO.ErrMsg);
211 ChrPtr(NW->IO.ErrMsg),
213 2, strs, (long*) &lens);
218 eNextState FinalizeNetworker(AsyncIO *IO)
220 AsyncNetworker *NW = (AsyncNetworker *)IO->Data;
222 CtdlNetworkTalkingTo(SKEY(NW->node), NTT_REMOVE);
224 DeleteNetworker(IO->Data);
228 eNextState NWC_ReadGreeting(AsyncNetworker *NW)
230 char connected_to[SIZ];
231 AsyncIO *IO = &NW->IO;
232 SetNWCState(IO, eNWCVSGreating);
234 /* Read the server greeting */
235 /* Check that the remote is who we think it is and warn the Aide if not */
236 extract_token (connected_to, ChrPtr(NW->IO.IOBuf), 1, ' ', sizeof connected_to);
237 if (strcmp(connected_to, ChrPtr(NW->node)) != 0)
239 if (NW->IO.ErrMsg == NULL)
240 NW->IO.ErrMsg = NewStrBuf();
241 StrBufPrintf(NW->IO.ErrMsg,
242 "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
243 connected_to, ChrPtr(NW->node));
244 EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
245 StopClientWatchers(IO, 1);
246 return QueueDBOperation(IO, SendFailureMessage);
251 eNextState NWC_SendAuth(AsyncNetworker *NW)
253 AsyncIO *IO = &NW->IO;
254 SetNWCState(IO, eNWCVSAuth);
255 /* We're talking to the correct node. Now identify ourselves. */
256 StrBufPrintf(NW->IO.SendBuf.Buf, "NETP %s|%s\n",
263 eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
265 AsyncIO *IO = &NW->IO;
267 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
273 int Error = atol(ChrPtr(NW->IO.IOBuf));
274 if (NW->IO.ErrMsg == NULL)
275 NW->IO.ErrMsg = NewStrBuf();
276 StrBufPrintf(NW->IO.ErrMsg,
277 "Connected to node \"%s\" but my secret wasn't accurate.\nReason was:%s\n",
278 ChrPtr(NW->node), ChrPtr(NW->IO.IOBuf) + 4);
280 SetNWCState(IO, eNWCVSAuthFailNTT);
282 "Already talking to %s; skipping this time.\n",
287 SetNWCState(IO, eNWCVSAuthFailNTT);
288 EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
289 StopClientWatchers(IO, 1);
290 return QueueDBOperation(IO, SendFailureMessage);
296 eNextState NWC_SendNDOP(AsyncNetworker *NW)
298 AsyncIO *IO = &NW->IO;
299 SetNWCState(IO, eNWCVSNDOP);
300 NW->tempFileName = NewStrBuf();
301 NW->SpoolFileName = NewStrBuf();
302 StrBufPrintf(NW->SpoolFileName,
306 time(NULL),// TODO: get time from libev
308 StrBufStripSlashes(NW->SpoolFileName, 1);
309 StrBufPrintf(NW->tempFileName,
313 time(NULL),// TODO: get time from libev
315 StrBufStripSlashes(NW->tempFileName, 1);
316 /* We're talking to the correct node. Now identify ourselves. */
317 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NDOP\n"));
322 eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
324 AsyncIO *IO = &NW->IO;
327 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
330 NW->IO.IOB.TotalSentAlready = 0;
331 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
332 EVN_syslog(LOG_DEBUG, "Expecting to transfer %d bytes\n", TotalSendSize);
333 if (TotalSendSize <= 0) {
334 NW->State = eNUOP - 1;
338 fd = open(ChrPtr(NW->tempFileName),
339 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
343 SetNWCState(IO, eNWCVSFail);
345 "cannot open %s: %s\n",
346 ChrPtr(NW->tempFileName),
349 NW->State = eQUIT - 1;
352 FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
358 SetNWCState(IO, eNWCVSFail);
363 eNextState NWC_SendREAD(AsyncNetworker *NW)
365 AsyncIO *IO = &NW->IO;
368 if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
371 * If shutting down we can exit here and unlink the temp file.
372 * this shouldn't loose us any messages.
374 if (server_shutting_down)
376 FDIOBufferDelete(&NW->IO.IOB);
377 unlink(ChrPtr(NW->tempFileName));
378 FDIOBufferDelete(&IO->IOB);
379 SetNWCState(IO, eNWCVSFail);
382 StrBufPrintf(NW->IO.SendBuf.Buf, "READ %ld|%ld\n",
383 NW->IO.IOB.TotalSentAlready,
384 NW->IO.IOB.TotalSendSize);
386 ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
387 ? IGNET_PACKET_SIZE :
388 (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
397 rc = NWC_DispatchWriteDone(&NW->IO);
404 eNextState NWC_ReadREADState(AsyncNetworker *NW)
406 AsyncIO *IO = &NW->IO;
408 if (ChrPtr(NW->IO.IOBuf)[0] == '6')
410 NW->IO.IOB.ChunkSendRemain =
411 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
414 FDIOBufferDelete(&IO->IOB);
417 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
418 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
421 AsyncIO *IO = &NW->IO;
423 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
427 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 eSendReply; //NWC_DispatchWriteDone(&NW->IO);
449 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
452 AsyncIO *IO = &NW->IO;
453 /* we don't have any data to debug print here. */
454 if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
458 FDIOBufferDelete(&NW->IO.IOB);
459 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
460 EVN_syslog(LOG_ALERT,
461 "Could not link %s to %s: %s\n",
462 ChrPtr(NW->tempFileName),
463 ChrPtr(NW->SpoolFileName),
467 unlink(ChrPtr(NW->tempFileName));
468 rc = NWC_DispatchWriteDone(&NW->IO);
474 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
475 return NWC_DispatchWriteDone(&NW->IO);
478 eNextState NWC_SendCLOS(AsyncNetworker *NW)
480 AsyncIO *IO = &NW->IO;
481 SetNWCState(IO, eNWCVSNDOPDone);
482 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
487 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
489 AsyncIO *IO = &NW->IO;
491 FDIOBufferDelete(&IO->IOB);
492 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
493 return eTerminateConnection;
498 eNextState NWC_SendNUOP(AsyncNetworker *NW)
500 AsyncIO *IO = &NW->IO;
506 SetNWCState(IO, eNWCVSNUOP);
507 StrBufPrintf(NW->SpoolFileName,
511 StrBufStripSlashes(NW->SpoolFileName, 1);
513 fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
515 if (errno != ENOENT) {
517 "cannot open %s: %s\n",
518 ChrPtr(NW->SpoolFileName),
522 rc = NWC_SendQUIT(NW);
527 if (fstat(fd, &statbuf) == -1) {
528 EVN_syslog(LOG_CRIT, "FSTAT FAILED %s [%s]--\n",
529 ChrPtr(NW->SpoolFileName),
531 if (fd > 0) close(fd);
534 TotalSendSize = statbuf.st_size;
535 if (TotalSendSize == 0) {
536 EVNM_syslog(LOG_DEBUG,
537 "Nothing to send.\n");
539 rc = NWC_SendQUIT(NW);
541 if (fd > 0) close(fd);
544 FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
546 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
551 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
553 AsyncIO *IO = &NW->IO;
555 if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
556 FDIOBufferDelete(&IO->IOB);
562 eNextState NWC_SendWRIT(AsyncNetworker *NW)
564 AsyncIO *IO = &NW->IO;
565 StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT %ld\n",
566 NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
570 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
572 AsyncIO *IO = &NW->IO;
574 if (ChrPtr(NW->IO.IOBuf)[0] != '7')
576 FDIOBufferDelete(&IO->IOB);
580 NW->IO.IOB.ChunkSendRemain =
581 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
585 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
587 AsyncIO *IO = &NW->IO;
589 if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
593 FDIOBufferDelete(&IO->IOB);
594 rc = NWC_DispatchWriteDone(IO);
600 IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
601 rc = NWC_DispatchWriteDone(IO);
607 eNextState NWC_SendUCLS(AsyncNetworker *NW)
609 AsyncIO *IO = &NW->IO;
610 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
615 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
617 AsyncIO *IO = &NW->IO;
620 EVN_syslog(LOG_NOTICE, "Sent %ld octets to <%s>\n", NW->IO.IOB.ChunkSize, ChrPtr(NW->node));
621 if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
622 EVN_syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
623 unlink(ChrPtr(NW->SpoolFileName));
625 FDIOBufferDelete(&IO->IOB);
626 SetNWCState(IO, eNWCVSNUOPDone);
630 eNextState NWC_SendQUIT(AsyncNetworker *NW)
632 AsyncIO *IO = &NW->IO;
633 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
639 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
641 AsyncIO *IO = &NW->IO;
648 NWClientHandler NWC_ReadHandlers[] = {
661 long NWC_ConnTimeout = 100;
663 const long NWC_SendTimeouts[] = {
673 const ConstStr NWC[] = {
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 ")},
681 {HKEY("Connection broken during ")}
684 NWClientHandler NWC_SendHandlers[] = {
689 NWC_ReadREADBlobDone,
698 const long NWC_ReadTimeouts[] = {
714 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
716 AsyncNetworker *NW = IO->Data;
717 struct hostent *hostent;
721 hostent = NW->HostLookup.VParsedDNSReply;
722 if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) &&
723 (hostent != NULL) ) {
724 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
725 if (NW->IO.ConnectMe->IPv6) {
726 memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr,
727 &hostent->h_addr_list[0],
728 sizeof(struct in6_addr));
730 NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
731 NW->IO.ConnectMe->Addr.sin6_port = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
734 struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
735 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
736 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
737 memcpy(&addr->sin_addr.s_addr,
738 hostent->h_addr_list[0],
741 addr->sin_family = hostent->h_addrtype;
742 addr->sin_port = htons(504);/// default citadel port
745 return nwc_connect_ip(IO);
752 eNextState nwc_get_one_host_ip(AsyncIO *IO)
754 AsyncNetworker *NW = IO->Data;
756 * here we start with the lookup of one host.
759 EVN_syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
761 EVN_syslog(LOG_DEBUG,
762 "NWC client[%ld]: looking up %s-Record %s : %d ...\n",
764 (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
765 NW->IO.ConnectMe->Host,
766 NW->IO.ConnectMe->Port);
768 QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
769 NW->IO.ConnectMe->Host,
772 nwc_get_one_host_ip_done);
773 IO->NextState = eReadDNSReply;
774 return IO->NextState;
777 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
779 eReadState NWC_ReadServerStatus(AsyncIO *IO)
781 // AsyncNetworker *NW = IO->Data;
782 eReadState Finished = eBufferNotEmpty;
784 switch (IO->NextState) {
789 case eTerminateConnection:
791 Finished = eReadFail;
797 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
809 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
811 SetNWCState(IO, eNWCVSConnFail);
812 StopClientWatchers(IO, 1);
813 return QueueDBOperation(IO, SendFailureMessage);
816 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
818 AsyncIO *IO = &NW->IO;
819 double Timeout = 0.0;
821 EVN_syslog(LOG_DEBUG, "%s - %d\n", __FUNCTION__, NextTCPState);
823 switch (NextTCPState) {
829 Timeout = NWC_ReadTimeouts[NW->State];
840 case eTerminateConnection:
843 case eReadMore://// TODO
847 EVN_syslog(LOG_DEBUG,
852 SetNextTimeout(&NW->IO, Timeout*100);
857 eNextState NWC_DispatchReadDone(AsyncIO *IO)
859 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
860 AsyncNetworker *NW = IO->Data;
863 rc = NWC_ReadHandlers[NW->State](NW);
866 NWC_SetTimeout(rc, NW);
869 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
871 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
872 AsyncNetworker *NW = IO->Data;
875 rc = NWC_SendHandlers[NW->State](NW);
876 NWC_SetTimeout(rc, NW);
880 /*****************************************************************************/
881 /* Networker CLIENT ERROR CATCHERS */
882 /*****************************************************************************/
883 eNextState NWC_Terminate(AsyncIO *IO)
885 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
886 FinalizeNetworker(IO);
890 eNextState NWC_TerminateDB(AsyncIO *IO)
892 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
893 FinalizeNetworker(IO);
897 eNextState NWC_Timeout(AsyncIO *IO)
899 AsyncNetworker *NW = IO->Data;
900 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
902 if (NW->IO.ErrMsg == NULL)
903 NW->IO.ErrMsg = NewStrBuf();
904 StrBufPrintf(NW->IO.ErrMsg, "Timeout while talking to %s \r\n", ChrPtr(NW->host));
905 return NWC_FailNetworkConnection(IO);
907 eNextState NWC_ConnFail(AsyncIO *IO)
909 AsyncNetworker *NW = IO->Data;
911 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
912 if (NW->IO.ErrMsg == NULL)
913 NW->IO.ErrMsg = NewStrBuf();
914 StrBufPrintf(NW->IO.ErrMsg, "failed to connect %s \r\n", ChrPtr(NW->host));
916 return NWC_FailNetworkConnection(IO);
918 eNextState NWC_DNSFail(AsyncIO *IO)
920 AsyncNetworker *NW = IO->Data;
922 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
923 if (NW->IO.ErrMsg == NULL)
924 NW->IO.ErrMsg = NewStrBuf();
925 StrBufPrintf(NW->IO.ErrMsg, "failed to look up %s \r\n", ChrPtr(NW->host));
927 return NWC_FailNetworkConnection(IO);
929 eNextState NWC_Shutdown(AsyncIO *IO)
931 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
933 FinalizeNetworker(IO);
938 eNextState nwc_connect_ip(AsyncIO *IO)
940 AsyncNetworker *NW = IO->Data;
942 SetNWCState(&NW->IO, eNWCVSConnecting);
943 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
944 EVN_syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n",
949 return EvConnectSock(IO,
955 static int NetworkerCount = 0;
956 void RunNetworker(AsyncNetworker *NW)
958 NW->n = NetworkerCount++;
959 CtdlNetworkTalkingTo(SKEY(NW->node), NTT_ADD);
960 syslog(LOG_DEBUG, "NW[%s][%ld]: polling\n", ChrPtr(NW->node), NW->n);
961 ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
963 InitIOStruct(&NW->IO,
966 NWC_ReadServerStatus,
968 NWC_DispatchWriteDone,
969 NWC_DispatchReadDone,
976 safestrncpy(((CitContext *)NW->IO.CitContext)->cs_host,
978 sizeof(((CitContext *)NW->IO.CitContext)->cs_host));
980 if (NW->IO.ConnectMe->IsIP) {
981 SetNWCState(&NW->IO, eNWCVSLookup);
982 QueueEventContext(&NW->IO,
985 else { /* uneducated admin has chosen to add DNS to the equation... */
986 SetNWCState(&NW->IO, eNWCVSConnecting);
987 QueueEventContext(&NW->IO,
988 nwc_get_one_host_ip);
993 * Poll other Citadel nodes and transfer inbound/outbound network data.
994 * Set "full" to nonzero to force a poll of every node, or to zero to poll
995 * only nodes to which we have data to send.
997 void network_poll_other_citadel_nodes(int full_poll, HashList *ignetcfg)
1004 StrBuf *SpoolFileName;
1008 if (GetCount(ignetcfg) ==0) {
1009 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
1012 become_session(&networker_client_CC);
1014 SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
1016 Pos = GetNewHashPos(ignetcfg, 0);
1018 while (GetNextHashPos(ignetcfg, Pos, &len, &key, &vCfg))
1020 /* Use the string tokenizer to grab one line at a time */
1021 if(server_shutting_down)
1022 return;/* TODO free stuff*/
1023 CtdlNodeConf *pNode = (CtdlNodeConf*) vCfg;
1025 NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
1026 memset(NW, 0, sizeof(AsyncNetworker));
1028 NW->node = NewStrBufDup(pNode->NodeName);
1029 NW->host = NewStrBufDup(pNode->Host);
1030 NW->port = NewStrBufDup(pNode->Port);
1031 NW->secret = NewStrBufDup(pNode->Secret);
1033 if ( (StrLength(NW->node) != 0) &&
1034 (StrLength(NW->secret) != 0) &&
1035 (StrLength(NW->host) != 0) &&
1036 (StrLength(NW->port) != 0))
1041 StrBufAppendBufPlain(SpoolFileName, HKEY("/"), 0);
1042 StrBufAppendBuf(SpoolFileName, NW->node, 0);
1043 StrBufStripSlashes(SpoolFileName, 1);
1045 if (access(ChrPtr(SpoolFileName), R_OK) == 0) {
1051 (StrLength(NW->host) > 0) &&
1052 strcmp("0.0.0.0", ChrPtr(NW->host)))
1054 NW->Url = NewStrBuf();
1055 StrBufPrintf(NW->Url, "citadel://%s@%s:%s",
1059 if (!CtdlNetworkTalkingTo(SKEY(NW->node), NTT_CHECK))
1065 DeleteNetworker(NW);
1067 FreeStrBuf(&SpoolFileName);
1068 DeleteHashPos(&Pos);
1072 void network_do_clientqueue(void)
1074 HashList *working_ignetcfg;
1075 int full_processing = 1;
1076 static time_t last_run = 0L;
1079 * Run the full set of processing tasks no more frequently
1080 * than once every n seconds
1082 if ( (time(NULL) - last_run) < config.c_net_freq ) {
1083 full_processing = 0;
1084 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
1085 config.c_net_freq - (time(NULL)- last_run)
1089 working_ignetcfg = CtdlLoadIgNetCfg();
1091 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1092 * then we poll everyone. Otherwise we only poll nodes we have stuff
1095 network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
1096 DeleteHash(&working_ignetcfg);
1099 void LogDebugEnableNetworkClient(const int n)
1101 NetworkClientDebugEnabled = n;
1104 * Module entry point
1106 CTDL_MODULE_INIT(network_client)
1110 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
1112 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER, PRIO_SEND + 10);
1113 CtdlRegisterDebugFlagHook(HKEY("networkclient"), LogDebugEnableNetworkClient, &NetworkClientDebugEnabled);
1116 return "networkclient";