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"
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 int NetworkClientDebugEnabled = 0;
81 #define NCDBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (NetworkClientDebugEnabled != 0))
83 #define EVN_syslog(LEVEL, FORMAT, ...) \
84 NCDBGLOG(LEVEL) syslog(LEVEL, \
85 "%s[%ld]CC[%d]NW[%s][%ld]" FORMAT, \
86 IOSTR, IO->ID, CCID, NODE, N, __VA_ARGS__)
88 #define EVNM_syslog(LEVEL, FORMAT) \
89 NCDBGLOG(LEVEL) syslog(LEVEL, \
90 "%s[%ld]CC[%d]NW[%s][%ld]" FORMAT, \
91 IOSTR, IO->ID, CCID, NODE, N)
93 #define EVNCS_syslog(LEVEL, FORMAT, ...) \
94 NCDBGLOG(LEVEL) syslog(LEVEL, "%s[%ld]NW[%s][%ld]" FORMAT, \
95 IOSTR, IO->ID, NODE, N, __VA_ARGS__)
97 #define EVNCSM_syslog(LEVEL, FORMAT) \
98 NCDBGLOG(LEVEL) syslog(LEVEL, "%s[%ld]NW[%s][%ld]" FORMAT, \
99 IOSTR, IO->ID, NODE, N)
102 typedef enum _eNWCState {
116 typedef enum _eNWCVState {
131 ConstStr NWCStateStr[] = {
132 {HKEY("Looking up Host")},
133 {HKEY("Connecting host")},
134 {HKEY("Failed to connect")},
135 {HKEY("Rread Greeting")},
136 {HKEY("Authenticating")},
137 {HKEY("Auth failed by NTT")},
138 {HKEY("Auth failed")},
139 {HKEY("Downloading")},
140 {HKEY("Downloading Success")},
141 {HKEY("Uploading Spoolfile")},
142 {HKEY("Uploading done")},
146 void SetNWCState(AsyncIO *IO, eNWCVState State)
148 CitContext* CCC = IO->CitContext;
149 memcpy(CCC->cs_clientname, NWCStateStr[State].Key, NWCStateStr[State].len + 1);
152 typedef struct _async_networker {
154 DNSQueryParts HostLookup;
157 StrBuf *SpoolFileName;
158 StrBuf *tempFileName;
166 typedef eNextState(*NWClientHandler)(AsyncNetworker* NW);
167 eNextState nwc_get_one_host_ip(AsyncIO *IO);
169 eNextState nwc_connect_ip(AsyncIO *IO);
171 eNextState NWC_SendQUIT(AsyncNetworker *NW);
172 eNextState NWC_DispatchWriteDone(AsyncIO *IO);
174 void DeleteNetworker(void *vptr)
176 AsyncNetworker *NW = (AsyncNetworker *)vptr;
177 FreeStrBuf(&NW->SpoolFileName);
178 FreeStrBuf(&NW->tempFileName);
179 FreeStrBuf(&NW->node);
180 FreeStrBuf(&NW->host);
181 FreeStrBuf(&NW->port);
182 FreeStrBuf(&NW->secret);
183 FreeStrBuf(&NW->Url);
184 FreeStrBuf(&NW->IO.ErrMsg);
185 FreeAsyncIOContents(&NW->IO);
186 if (NW->HostLookup.VParsedDNSReply != NULL) {
187 NW->HostLookup.DNSReplyFree(NW->HostLookup.VParsedDNSReply);
188 NW->HostLookup.VParsedDNSReply = NULL;
193 #define NWC_DBG_SEND() EVN_syslog(LOG_DEBUG, ": > %s", ChrPtr(NW->IO.SendBuf.Buf))
194 #define NWC_DBG_READ() EVN_syslog(LOG_DEBUG, ": < %s\n", ChrPtr(NW->IO.IOBuf))
195 #define NWC_OK (strncasecmp(ChrPtr(NW->IO.IOBuf), "+OK", 3) == 0)
197 eNextState NWC_SendFailureMessage(AsyncIO *IO)
199 AsyncNetworker *NW = IO->Data;
203 strs[0] = ChrPtr(NW->node);
204 lens[0] = StrLength(NW->node);
206 strs[1] = ChrPtr(NW->IO.ErrMsg);
207 lens[1] = StrLength(NW->IO.ErrMsg);
209 ChrPtr(NW->IO.ErrMsg),
211 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));
246 return EventQueueDBOperation(IO, NWC_SendFailureMessage, 1);
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 return EventQueueDBOperation(IO, NWC_SendFailureMessage, 1);
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')
328 int LogLevel = LOG_DEBUG;
330 NW->IO.IOB.TotalSentAlready = 0;
332 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
334 if (TotalSendSize > 0)
338 "Expecting to transfer %d bytes to %s\n",
340 ChrPtr(NW->tempFileName));
342 if (TotalSendSize <= 0) {
343 NW->State = eNUOP - 1;
347 fd = open(ChrPtr(NW->tempFileName),
348 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
352 SetNWCState(IO, eNWCVSFail);
354 "cannot open %s: %s\n",
355 ChrPtr(NW->tempFileName),
358 NW->State = eQUIT - 1;
361 FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
367 SetNWCState(IO, eNWCVSFail);
372 eNextState NWC_SendREAD(AsyncNetworker *NW)
374 AsyncIO *IO = &NW->IO;
377 if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
380 * If shutting down we can exit here and unlink the temp file.
381 * this shouldn't loose us any messages.
383 if (server_shutting_down)
385 FDIOBufferDelete(&NW->IO.IOB);
386 unlink(ChrPtr(NW->tempFileName));
387 FDIOBufferDelete(&IO->IOB);
388 SetNWCState(IO, eNWCVSFail);
391 StrBufPrintf(NW->IO.SendBuf.Buf, "READ "LOFF_T_FMT"|%ld\n",
392 NW->IO.IOB.TotalSentAlready,
393 NW->IO.IOB.TotalSendSize);
395 ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
396 ? IGNET_PACKET_SIZE :
397 (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
406 rc = NWC_DispatchWriteDone(&NW->IO);
413 eNextState NWC_ReadREADState(AsyncNetworker *NW)
415 AsyncIO *IO = &NW->IO;
417 if (ChrPtr(NW->IO.IOBuf)[0] == '6')
419 NW->IO.IOB.ChunkSendRemain =
420 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
423 FDIOBufferDelete(&IO->IOB);
426 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
427 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
430 AsyncIO *IO = &NW->IO;
432 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
436 FDIOBufferDelete(&NW->IO.IOB);
438 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
439 EVN_syslog(LOG_ALERT,
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 eSendReply; //NWC_DispatchWriteDone(&NW->IO);
464 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
467 AsyncIO *IO = &NW->IO;
468 /* we don't have any data to debug print here. */
469 if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
473 FDIOBufferDelete(&NW->IO.IOB);
474 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
475 EVN_syslog(LOG_ALERT,
476 "Could not link %s to %s: %s\n",
477 ChrPtr(NW->tempFileName),
478 ChrPtr(NW->SpoolFileName),
484 ChrPtr(NW->tempFileName),
485 ChrPtr(NW->SpoolFileName));
488 unlink(ChrPtr(NW->tempFileName));
489 rc = NWC_DispatchWriteDone(&NW->IO);
495 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
496 return NWC_DispatchWriteDone(&NW->IO);
499 eNextState NWC_SendCLOS(AsyncNetworker *NW)
501 AsyncIO *IO = &NW->IO;
502 SetNWCState(IO, eNWCVSNDOPDone);
503 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
508 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
510 AsyncIO *IO = &NW->IO;
512 FDIOBufferDelete(&IO->IOB);
513 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
514 return eTerminateConnection;
519 eNextState NWC_SendNUOP(AsyncNetworker *NW)
521 AsyncIO *IO = &NW->IO;
527 SetNWCState(IO, eNWCVSNUOP);
528 StrBufPrintf(NW->SpoolFileName,
532 StrBufStripSlashes(NW->SpoolFileName, 1);
534 fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
536 if (errno != ENOENT) {
538 "cannot open %s: %s\n",
539 ChrPtr(NW->SpoolFileName),
543 rc = NWC_SendQUIT(NW);
548 if (fstat(fd, &statbuf) == -1) {
549 EVN_syslog(LOG_CRIT, "FSTAT FAILED %s [%s]--\n",
550 ChrPtr(NW->SpoolFileName),
552 if (fd > 0) close(fd);
555 TotalSendSize = statbuf.st_size;
556 if (TotalSendSize == 0) {
557 EVNM_syslog(LOG_DEBUG,
558 "Nothing to send.\n");
560 rc = NWC_SendQUIT(NW);
562 if (fd > 0) close(fd);
568 "sending %s to %s\n",
569 ChrPtr(NW->SpoolFileName),
573 FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
575 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
580 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
582 AsyncIO *IO = &NW->IO;
584 if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
585 FDIOBufferDelete(&IO->IOB);
591 eNextState NWC_SendWRIT(AsyncNetworker *NW)
593 AsyncIO *IO = &NW->IO;
594 StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT "LOFF_T_FMT"\n",
595 NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
599 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
601 AsyncIO *IO = &NW->IO;
603 if (ChrPtr(NW->IO.IOBuf)[0] != '7')
605 FDIOBufferDelete(&IO->IOB);
609 NW->IO.IOB.ChunkSendRemain =
610 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
614 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
616 AsyncIO *IO = &NW->IO;
618 if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
622 FDIOBufferDelete(&IO->IOB);
623 rc = NWC_DispatchWriteDone(IO);
629 IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
630 rc = NWC_DispatchWriteDone(IO);
636 eNextState NWC_SendUCLS(AsyncNetworker *NW)
638 AsyncIO *IO = &NW->IO;
639 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
644 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
646 AsyncIO *IO = &NW->IO;
649 EVN_syslog(LOG_NOTICE,
650 "Sent %s [%ld] octets to <%s>\n",
651 ChrPtr(NW->SpoolFileName),
652 NW->IO.IOB.ChunkSize,
655 if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
656 EVN_syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
657 unlink(ChrPtr(NW->SpoolFileName));
659 FDIOBufferDelete(&IO->IOB);
660 SetNWCState(IO, eNWCVSNUOPDone);
664 eNextState NWC_SendQUIT(AsyncNetworker *NW)
666 AsyncIO *IO = &NW->IO;
667 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
673 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
675 AsyncIO *IO = &NW->IO;
682 NWClientHandler NWC_ReadHandlers[] = {
695 long NWC_ConnTimeout = 100;
697 const long NWC_SendTimeouts[] = {
707 const ConstStr NWC[] = {
708 {HKEY("Connection broken during ")},
709 {HKEY("Connection broken during ")},
710 {HKEY("Connection broken during ")},
711 {HKEY("Connection broken during ")},
712 {HKEY("Connection broken during ")},
713 {HKEY("Connection broken during ")},
714 {HKEY("Connection broken during ")},
715 {HKEY("Connection broken during ")}
718 NWClientHandler NWC_SendHandlers[] = {
723 NWC_ReadREADBlobDone,
732 const long NWC_ReadTimeouts[] = {
748 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
750 AsyncNetworker *NW = IO->Data;
751 struct hostent *hostent;
755 hostent = NW->HostLookup.VParsedDNSReply;
756 if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) &&
757 (hostent != NULL) ) {
758 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
759 if (NW->IO.ConnectMe->IPv6) {
760 memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr,
761 &hostent->h_addr_list[0],
762 sizeof(struct in6_addr));
764 NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
765 NW->IO.ConnectMe->Addr.sin6_port = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
768 struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
769 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
770 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
771 memcpy(&addr->sin_addr.s_addr,
772 hostent->h_addr_list[0],
775 addr->sin_family = hostent->h_addrtype;
776 addr->sin_port = htons(504);/// default citadel port
779 return nwc_connect_ip(IO);
786 eNextState nwc_get_one_host_ip(AsyncIO *IO)
788 AsyncNetworker *NW = IO->Data;
790 * here we start with the lookup of one host.
793 EVN_syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
795 EVN_syslog(LOG_DEBUG,
796 "NWC client[%ld]: looking up %s-Record %s : %d ...\n",
798 (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
799 NW->IO.ConnectMe->Host,
800 NW->IO.ConnectMe->Port);
802 QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
803 NW->IO.ConnectMe->Host,
806 nwc_get_one_host_ip_done);
807 IO->NextState = eReadDNSReply;
808 return IO->NextState;
811 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
813 eReadState NWC_ReadServerStatus(AsyncIO *IO)
815 // AsyncNetworker *NW = IO->Data;
816 eReadState Finished = eBufferNotEmpty;
818 switch (IO->NextState) {
823 case eTerminateConnection:
825 Finished = eReadFail;
831 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
843 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
845 SetNWCState(IO, eNWCVSConnFail);
846 return EventQueueDBOperation(IO, NWC_SendFailureMessage, 1);
849 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
851 AsyncIO *IO = &NW->IO;
852 double Timeout = 0.0;
854 EVN_syslog(LOG_DEBUG, "%s - %d\n", __FUNCTION__, NextTCPState);
856 switch (NextTCPState) {
860 Timeout = NWC_ReadTimeouts[NW->State];
872 case eTerminateConnection:
877 EVN_syslog(LOG_DEBUG,
882 SetNextTimeout(&NW->IO, Timeout*100);
887 eNextState NWC_DispatchReadDone(AsyncIO *IO)
889 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
890 AsyncNetworker *NW = IO->Data;
893 rc = NWC_ReadHandlers[NW->State](NW);
898 NWC_SetTimeout(rc, NW);
902 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
904 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
905 AsyncNetworker *NW = IO->Data;
908 rc = NWC_SendHandlers[NW->State](NW);
909 NWC_SetTimeout(rc, NW);
913 /*****************************************************************************/
914 /* Networker CLIENT ERROR CATCHERS */
915 /*****************************************************************************/
916 eNextState NWC_Terminate(AsyncIO *IO)
918 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
919 FinalizeNetworker(IO);
923 eNextState NWC_TerminateDB(AsyncIO *IO)
925 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
926 FinalizeNetworker(IO);
930 eNextState NWC_Timeout(AsyncIO *IO)
932 AsyncNetworker *NW = IO->Data;
933 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
935 if (NW->IO.ErrMsg == NULL)
936 NW->IO.ErrMsg = NewStrBuf();
937 StrBufPrintf(NW->IO.ErrMsg, "Timeout while talking to %s \r\n", ChrPtr(NW->host));
938 return NWC_FailNetworkConnection(IO);
940 eNextState NWC_ConnFail(AsyncIO *IO)
942 AsyncNetworker *NW = IO->Data;
944 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
945 if (NW->IO.ErrMsg == NULL)
946 NW->IO.ErrMsg = NewStrBuf();
947 StrBufPrintf(NW->IO.ErrMsg, "failed to connect %s \r\n", ChrPtr(NW->host));
949 return NWC_FailNetworkConnection(IO);
951 eNextState NWC_DNSFail(AsyncIO *IO)
953 AsyncNetworker *NW = IO->Data;
955 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
956 if (NW->IO.ErrMsg == NULL)
957 NW->IO.ErrMsg = NewStrBuf();
958 StrBufPrintf(NW->IO.ErrMsg, "failed to look up %s \r\n", ChrPtr(NW->host));
960 return NWC_FailNetworkConnection(IO);
962 eNextState NWC_Shutdown(AsyncIO *IO)
964 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
966 FinalizeNetworker(IO);
971 eNextState nwc_connect_ip(AsyncIO *IO)
973 AsyncNetworker *NW = IO->Data;
975 SetNWCState(&NW->IO, eNWCVSConnecting);
976 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
977 EVN_syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n",
982 return EvConnectSock(IO,
988 static int NetworkerCount = 0;
989 void RunNetworker(AsyncNetworker *NW)
991 NW->n = NetworkerCount++;
992 CtdlNetworkTalkingTo(SKEY(NW->node), NTT_ADD);
993 syslog(LOG_DEBUG, "NW[%s][%ld]: polling\n", ChrPtr(NW->node), NW->n);
994 ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
996 InitIOStruct(&NW->IO,
999 NWC_ReadServerStatus,
1001 NWC_DispatchWriteDone,
1002 NWC_DispatchReadDone,
1009 safestrncpy(((CitContext *)NW->IO.CitContext)->cs_host,
1011 sizeof(((CitContext *)NW->IO.CitContext)->cs_host));
1013 if (NW->IO.ConnectMe->IsIP) {
1014 SetNWCState(&NW->IO, eNWCVSLookup);
1015 QueueEventContext(&NW->IO,
1018 else { /* uneducated admin has chosen to add DNS to the equation... */
1019 SetNWCState(&NW->IO, eNWCVSConnecting);
1020 QueueEventContext(&NW->IO,
1021 nwc_get_one_host_ip);
1026 * Poll other Citadel nodes and transfer inbound/outbound network data.
1027 * Set "full" to nonzero to force a poll of every node, or to zero to poll
1028 * only nodes to which we have data to send.
1030 void network_poll_other_citadel_nodes(int full_poll, HashList *ignetcfg)
1037 StrBuf *SpoolFileName;
1041 if (GetCount(ignetcfg) ==0) {
1042 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
1045 become_session(&networker_client_CC);
1047 SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
1049 Pos = GetNewHashPos(ignetcfg, 0);
1051 while (GetNextHashPos(ignetcfg, Pos, &len, &key, &vCfg))
1053 /* Use the string tokenizer to grab one line at a time */
1054 if(server_shutting_down)
1055 return;/* TODO free stuff*/
1056 CtdlNodeConf *pNode = (CtdlNodeConf*) vCfg;
1058 NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
1059 memset(NW, 0, sizeof(AsyncNetworker));
1061 NW->node = NewStrBufDup(pNode->NodeName);
1062 NW->host = NewStrBufDup(pNode->Host);
1063 NW->port = NewStrBufDup(pNode->Port);
1064 NW->secret = NewStrBufDup(pNode->Secret);
1066 if ( (StrLength(NW->node) != 0) &&
1067 (StrLength(NW->secret) != 0) &&
1068 (StrLength(NW->host) != 0) &&
1069 (StrLength(NW->port) != 0))
1074 StrBufAppendBufPlain(SpoolFileName, HKEY("/"), 0);
1075 StrBufAppendBuf(SpoolFileName, NW->node, 0);
1076 StrBufStripSlashes(SpoolFileName, 1);
1078 if (access(ChrPtr(SpoolFileName), R_OK) == 0) {
1084 (StrLength(NW->host) > 0) &&
1085 strcmp("0.0.0.0", ChrPtr(NW->host)))
1087 NW->Url = NewStrBuf();
1088 StrBufPrintf(NW->Url, "citadel://%s@%s:%s",
1092 if (!CtdlNetworkTalkingTo(SKEY(NW->node), NTT_CHECK))
1098 DeleteNetworker(NW);
1100 FreeStrBuf(&SpoolFileName);
1101 DeleteHashPos(&Pos);
1105 void network_do_clientqueue(void)
1107 HashList *working_ignetcfg;
1108 int full_processing = 1;
1109 static time_t last_run = 0L;
1112 * Run the full set of processing tasks no more frequently
1113 * than once every n seconds
1115 if ( (time(NULL) - last_run) < config.c_net_freq ) {
1116 full_processing = 0;
1117 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
1118 config.c_net_freq - (time(NULL)- last_run)
1122 working_ignetcfg = CtdlLoadIgNetCfg();
1124 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1125 * then we poll everyone. Otherwise we only poll nodes we have stuff
1128 network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
1129 DeleteHash(&working_ignetcfg);
1132 void LogDebugEnableNetworkClient(const int n)
1134 NetworkClientDebugEnabled = n;
1137 * Module entry point
1139 CTDL_MODULE_INIT(network_client)
1143 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
1145 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER, PRIO_SEND + 10);
1146 CtdlRegisterDebugFlagHook(HKEY("networkclient"), LogDebugEnableNetworkClient, &NetworkClientDebugEnabled);
1149 return "networkclient";