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,
219 eNextState FinalizeNetworker(AsyncIO *IO)
221 AsyncNetworker *NW = (AsyncNetworker *)IO->Data;
223 CtdlNetworkTalkingTo(SKEY(NW->node), NTT_REMOVE);
225 DeleteNetworker(IO->Data);
229 eNextState NWC_ReadGreeting(AsyncNetworker *NW)
231 char connected_to[SIZ];
232 AsyncIO *IO = &NW->IO;
233 SetNWCState(IO, eNWCVSGreating);
235 /* Read the server greeting */
236 /* Check that the remote is who we think it is and warn the Aide if not */
237 extract_token (connected_to, ChrPtr(NW->IO.IOBuf), 1, ' ', sizeof connected_to);
238 if (strcmp(connected_to, ChrPtr(NW->node)) != 0)
240 if (NW->IO.ErrMsg == NULL)
241 NW->IO.ErrMsg = NewStrBuf();
242 StrBufPrintf(NW->IO.ErrMsg,
243 "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
244 connected_to, ChrPtr(NW->node));
245 EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
246 StopClientWatchers(IO, 1);
247 return QueueDBOperation(IO, SendFailureMessage);
252 eNextState NWC_SendAuth(AsyncNetworker *NW)
254 AsyncIO *IO = &NW->IO;
255 SetNWCState(IO, eNWCVSAuth);
256 /* We're talking to the correct node. Now identify ourselves. */
257 StrBufPrintf(NW->IO.SendBuf.Buf, "NETP %s|%s\n",
264 eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
266 AsyncIO *IO = &NW->IO;
268 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
274 int Error = atol(ChrPtr(NW->IO.IOBuf));
275 if (NW->IO.ErrMsg == NULL)
276 NW->IO.ErrMsg = NewStrBuf();
277 StrBufPrintf(NW->IO.ErrMsg,
278 "Connected to node \"%s\" but my secret wasn't accurate.\nReason was:%s\n",
279 ChrPtr(NW->node), ChrPtr(NW->IO.IOBuf) + 4);
281 SetNWCState(IO, eNWCVSAuthFailNTT);
283 "Already talking to %s; skipping this time.\n",
288 SetNWCState(IO, eNWCVSAuthFailNTT);
289 EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
290 StopClientWatchers(IO, 1);
291 return QueueDBOperation(IO, SendFailureMessage);
297 eNextState NWC_SendNDOP(AsyncNetworker *NW)
299 AsyncIO *IO = &NW->IO;
300 SetNWCState(IO, eNWCVSNDOP);
301 NW->tempFileName = NewStrBuf();
302 NW->SpoolFileName = NewStrBuf();
303 StrBufPrintf(NW->SpoolFileName,
307 time(NULL),// TODO: get time from libev
309 StrBufStripSlashes(NW->SpoolFileName, 1);
310 StrBufPrintf(NW->tempFileName,
314 time(NULL),// TODO: get time from libev
316 StrBufStripSlashes(NW->tempFileName, 1);
317 /* We're talking to the correct node. Now identify ourselves. */
318 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NDOP\n"));
323 eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
325 AsyncIO *IO = &NW->IO;
328 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
330 int LogLevel = LOG_DEBUG;
332 NW->IO.IOB.TotalSentAlready = 0;
334 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
336 if (TotalSendSize > 0)
340 "Expecting to transfer %d bytes to %s\n",
342 ChrPtr(NW->tempFileName));
344 if (TotalSendSize <= 0) {
345 NW->State = eNUOP - 1;
349 fd = open(ChrPtr(NW->tempFileName),
350 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
354 SetNWCState(IO, eNWCVSFail);
356 "cannot open %s: %s\n",
357 ChrPtr(NW->tempFileName),
360 NW->State = eQUIT - 1;
363 FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
369 SetNWCState(IO, eNWCVSFail);
374 eNextState NWC_SendREAD(AsyncNetworker *NW)
376 AsyncIO *IO = &NW->IO;
379 if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
382 * If shutting down we can exit here and unlink the temp file.
383 * this shouldn't loose us any messages.
385 if (server_shutting_down)
387 FDIOBufferDelete(&NW->IO.IOB);
388 unlink(ChrPtr(NW->tempFileName));
389 FDIOBufferDelete(&IO->IOB);
390 SetNWCState(IO, eNWCVSFail);
393 StrBufPrintf(NW->IO.SendBuf.Buf, "READ "LOFF_T_FMT"|%ld\n",
394 NW->IO.IOB.TotalSentAlready,
395 NW->IO.IOB.TotalSendSize);
397 ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
398 ? IGNET_PACKET_SIZE :
399 (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
408 rc = NWC_DispatchWriteDone(&NW->IO);
415 eNextState NWC_ReadREADState(AsyncNetworker *NW)
417 AsyncIO *IO = &NW->IO;
419 if (ChrPtr(NW->IO.IOBuf)[0] == '6')
421 NW->IO.IOB.ChunkSendRemain =
422 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
425 FDIOBufferDelete(&IO->IOB);
428 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
429 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
432 AsyncIO *IO = &NW->IO;
434 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
438 FDIOBufferDelete(&NW->IO.IOB);
440 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
441 EVN_syslog(LOG_ALERT,
442 "Could not link %s to %s: %s\n",
443 ChrPtr(NW->tempFileName),
444 ChrPtr(NW->SpoolFileName),
450 ChrPtr(NW->tempFileName),
451 ChrPtr(NW->SpoolFileName));
454 unlink(ChrPtr(NW->tempFileName));
455 rc = NWC_DispatchWriteDone(&NW->IO);
461 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
462 return eSendReply; //NWC_DispatchWriteDone(&NW->IO);
466 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
469 AsyncIO *IO = &NW->IO;
470 /* we don't have any data to debug print here. */
471 if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
475 FDIOBufferDelete(&NW->IO.IOB);
476 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
477 EVN_syslog(LOG_ALERT,
478 "Could not link %s to %s: %s\n",
479 ChrPtr(NW->tempFileName),
480 ChrPtr(NW->SpoolFileName),
486 ChrPtr(NW->tempFileName),
487 ChrPtr(NW->SpoolFileName));
490 unlink(ChrPtr(NW->tempFileName));
491 rc = NWC_DispatchWriteDone(&NW->IO);
497 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
498 return NWC_DispatchWriteDone(&NW->IO);
501 eNextState NWC_SendCLOS(AsyncNetworker *NW)
503 AsyncIO *IO = &NW->IO;
504 SetNWCState(IO, eNWCVSNDOPDone);
505 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
510 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
512 AsyncIO *IO = &NW->IO;
514 FDIOBufferDelete(&IO->IOB);
515 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
516 return eTerminateConnection;
521 eNextState NWC_SendNUOP(AsyncNetworker *NW)
523 AsyncIO *IO = &NW->IO;
529 SetNWCState(IO, eNWCVSNUOP);
530 StrBufPrintf(NW->SpoolFileName,
534 StrBufStripSlashes(NW->SpoolFileName, 1);
536 fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
538 if (errno != ENOENT) {
540 "cannot open %s: %s\n",
541 ChrPtr(NW->SpoolFileName),
545 rc = NWC_SendQUIT(NW);
550 if (fstat(fd, &statbuf) == -1) {
551 EVN_syslog(LOG_CRIT, "FSTAT FAILED %s [%s]--\n",
552 ChrPtr(NW->SpoolFileName),
554 if (fd > 0) close(fd);
557 TotalSendSize = statbuf.st_size;
558 if (TotalSendSize == 0) {
559 EVNM_syslog(LOG_DEBUG,
560 "Nothing to send.\n");
562 rc = NWC_SendQUIT(NW);
564 if (fd > 0) close(fd);
570 "sending %s to %s\n",
571 ChrPtr(NW->SpoolFileName),
575 FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
577 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
582 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
584 AsyncIO *IO = &NW->IO;
586 if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
587 FDIOBufferDelete(&IO->IOB);
593 eNextState NWC_SendWRIT(AsyncNetworker *NW)
595 AsyncIO *IO = &NW->IO;
596 StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT "LOFF_T_FMT"\n",
597 NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
601 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
603 AsyncIO *IO = &NW->IO;
605 if (ChrPtr(NW->IO.IOBuf)[0] != '7')
607 FDIOBufferDelete(&IO->IOB);
611 NW->IO.IOB.ChunkSendRemain =
612 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
616 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
618 AsyncIO *IO = &NW->IO;
620 if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
624 FDIOBufferDelete(&IO->IOB);
625 rc = NWC_DispatchWriteDone(IO);
631 IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
632 rc = NWC_DispatchWriteDone(IO);
638 eNextState NWC_SendUCLS(AsyncNetworker *NW)
640 AsyncIO *IO = &NW->IO;
641 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
646 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
648 AsyncIO *IO = &NW->IO;
651 EVN_syslog(LOG_NOTICE,
652 "Sent %s [%ld] octets to <%s>\n",
653 ChrPtr(NW->SpoolFileName),
654 NW->IO.IOB.ChunkSize,
657 if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
658 EVN_syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
659 unlink(ChrPtr(NW->SpoolFileName));
661 FDIOBufferDelete(&IO->IOB);
662 SetNWCState(IO, eNWCVSNUOPDone);
666 eNextState NWC_SendQUIT(AsyncNetworker *NW)
668 AsyncIO *IO = &NW->IO;
669 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
675 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
677 AsyncIO *IO = &NW->IO;
684 NWClientHandler NWC_ReadHandlers[] = {
697 long NWC_ConnTimeout = 100;
699 const long NWC_SendTimeouts[] = {
709 const ConstStr NWC[] = {
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 ")},
716 {HKEY("Connection broken during ")},
717 {HKEY("Connection broken during ")}
720 NWClientHandler NWC_SendHandlers[] = {
725 NWC_ReadREADBlobDone,
734 const long NWC_ReadTimeouts[] = {
750 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
752 AsyncNetworker *NW = IO->Data;
753 struct hostent *hostent;
757 hostent = NW->HostLookup.VParsedDNSReply;
758 if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) &&
759 (hostent != NULL) ) {
760 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
761 if (NW->IO.ConnectMe->IPv6) {
762 memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr,
763 &hostent->h_addr_list[0],
764 sizeof(struct in6_addr));
766 NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
767 NW->IO.ConnectMe->Addr.sin6_port = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
770 struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
771 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
772 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
773 memcpy(&addr->sin_addr.s_addr,
774 hostent->h_addr_list[0],
777 addr->sin_family = hostent->h_addrtype;
778 addr->sin_port = htons(504);/// default citadel port
781 return nwc_connect_ip(IO);
788 eNextState nwc_get_one_host_ip(AsyncIO *IO)
790 AsyncNetworker *NW = IO->Data;
792 * here we start with the lookup of one host.
795 EVN_syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
797 EVN_syslog(LOG_DEBUG,
798 "NWC client[%ld]: looking up %s-Record %s : %d ...\n",
800 (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
801 NW->IO.ConnectMe->Host,
802 NW->IO.ConnectMe->Port);
804 QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
805 NW->IO.ConnectMe->Host,
808 nwc_get_one_host_ip_done);
809 IO->NextState = eReadDNSReply;
810 return IO->NextState;
813 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
815 eReadState NWC_ReadServerStatus(AsyncIO *IO)
817 // AsyncNetworker *NW = IO->Data;
818 eReadState Finished = eBufferNotEmpty;
820 switch (IO->NextState) {
825 case eTerminateConnection:
827 Finished = eReadFail;
833 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
845 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
847 SetNWCState(IO, eNWCVSConnFail);
848 StopClientWatchers(IO, 1);
849 return QueueDBOperation(IO, SendFailureMessage);
852 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
854 AsyncIO *IO = &NW->IO;
855 double Timeout = 0.0;
857 EVN_syslog(LOG_DEBUG, "%s - %d\n", __FUNCTION__, NextTCPState);
859 switch (NextTCPState) {
865 Timeout = NWC_ReadTimeouts[NW->State];
876 case eTerminateConnection:
879 case eReadMore://// TODO
883 EVN_syslog(LOG_DEBUG,
888 SetNextTimeout(&NW->IO, Timeout*100);
893 eNextState NWC_DispatchReadDone(AsyncIO *IO)
895 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
896 AsyncNetworker *NW = IO->Data;
899 rc = NWC_ReadHandlers[NW->State](NW);
902 NWC_SetTimeout(rc, NW);
905 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
907 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
908 AsyncNetworker *NW = IO->Data;
911 rc = NWC_SendHandlers[NW->State](NW);
912 NWC_SetTimeout(rc, NW);
916 /*****************************************************************************/
917 /* Networker CLIENT ERROR CATCHERS */
918 /*****************************************************************************/
919 eNextState NWC_Terminate(AsyncIO *IO)
921 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
922 FinalizeNetworker(IO);
926 eNextState NWC_TerminateDB(AsyncIO *IO)
928 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
929 FinalizeNetworker(IO);
933 eNextState NWC_Timeout(AsyncIO *IO)
935 AsyncNetworker *NW = IO->Data;
936 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
938 if (NW->IO.ErrMsg == NULL)
939 NW->IO.ErrMsg = NewStrBuf();
940 StrBufPrintf(NW->IO.ErrMsg, "Timeout while talking to %s \r\n", ChrPtr(NW->host));
941 return NWC_FailNetworkConnection(IO);
943 eNextState NWC_ConnFail(AsyncIO *IO)
945 AsyncNetworker *NW = IO->Data;
947 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
948 if (NW->IO.ErrMsg == NULL)
949 NW->IO.ErrMsg = NewStrBuf();
950 StrBufPrintf(NW->IO.ErrMsg, "failed to connect %s \r\n", ChrPtr(NW->host));
952 return NWC_FailNetworkConnection(IO);
954 eNextState NWC_DNSFail(AsyncIO *IO)
956 AsyncNetworker *NW = IO->Data;
958 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
959 if (NW->IO.ErrMsg == NULL)
960 NW->IO.ErrMsg = NewStrBuf();
961 StrBufPrintf(NW->IO.ErrMsg, "failed to look up %s \r\n", ChrPtr(NW->host));
963 return NWC_FailNetworkConnection(IO);
965 eNextState NWC_Shutdown(AsyncIO *IO)
967 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
969 FinalizeNetworker(IO);
974 eNextState nwc_connect_ip(AsyncIO *IO)
976 AsyncNetworker *NW = IO->Data;
978 SetNWCState(&NW->IO, eNWCVSConnecting);
979 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
980 EVN_syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n",
985 return EvConnectSock(IO,
991 static int NetworkerCount = 0;
992 void RunNetworker(AsyncNetworker *NW)
994 NW->n = NetworkerCount++;
995 CtdlNetworkTalkingTo(SKEY(NW->node), NTT_ADD);
996 syslog(LOG_DEBUG, "NW[%s][%ld]: polling\n", ChrPtr(NW->node), NW->n);
997 ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
999 InitIOStruct(&NW->IO,
1002 NWC_ReadServerStatus,
1004 NWC_DispatchWriteDone,
1005 NWC_DispatchReadDone,
1012 safestrncpy(((CitContext *)NW->IO.CitContext)->cs_host,
1014 sizeof(((CitContext *)NW->IO.CitContext)->cs_host));
1016 if (NW->IO.ConnectMe->IsIP) {
1017 SetNWCState(&NW->IO, eNWCVSLookup);
1018 QueueEventContext(&NW->IO,
1021 else { /* uneducated admin has chosen to add DNS to the equation... */
1022 SetNWCState(&NW->IO, eNWCVSConnecting);
1023 QueueEventContext(&NW->IO,
1024 nwc_get_one_host_ip);
1029 * Poll other Citadel nodes and transfer inbound/outbound network data.
1030 * Set "full" to nonzero to force a poll of every node, or to zero to poll
1031 * only nodes to which we have data to send.
1033 void network_poll_other_citadel_nodes(int full_poll, HashList *ignetcfg)
1040 StrBuf *SpoolFileName;
1044 if (GetCount(ignetcfg) ==0) {
1045 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
1048 become_session(&networker_client_CC);
1050 SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
1052 Pos = GetNewHashPos(ignetcfg, 0);
1054 while (GetNextHashPos(ignetcfg, Pos, &len, &key, &vCfg))
1056 /* Use the string tokenizer to grab one line at a time */
1057 if(server_shutting_down)
1058 return;/* TODO free stuff*/
1059 CtdlNodeConf *pNode = (CtdlNodeConf*) vCfg;
1061 NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
1062 memset(NW, 0, sizeof(AsyncNetworker));
1064 NW->node = NewStrBufDup(pNode->NodeName);
1065 NW->host = NewStrBufDup(pNode->Host);
1066 NW->port = NewStrBufDup(pNode->Port);
1067 NW->secret = NewStrBufDup(pNode->Secret);
1069 if ( (StrLength(NW->node) != 0) &&
1070 (StrLength(NW->secret) != 0) &&
1071 (StrLength(NW->host) != 0) &&
1072 (StrLength(NW->port) != 0))
1077 StrBufAppendBufPlain(SpoolFileName, HKEY("/"), 0);
1078 StrBufAppendBuf(SpoolFileName, NW->node, 0);
1079 StrBufStripSlashes(SpoolFileName, 1);
1081 if (access(ChrPtr(SpoolFileName), R_OK) == 0) {
1087 (StrLength(NW->host) > 0) &&
1088 strcmp("0.0.0.0", ChrPtr(NW->host)))
1090 NW->Url = NewStrBuf();
1091 StrBufPrintf(NW->Url, "citadel://%s@%s:%s",
1095 if (!CtdlNetworkTalkingTo(SKEY(NW->node), NTT_CHECK))
1101 DeleteNetworker(NW);
1103 FreeStrBuf(&SpoolFileName);
1104 DeleteHashPos(&Pos);
1108 void network_do_clientqueue(void)
1110 HashList *working_ignetcfg;
1111 int full_processing = 1;
1112 static time_t last_run = 0L;
1115 * Run the full set of processing tasks no more frequently
1116 * than once every n seconds
1118 if ( (time(NULL) - last_run) < config.c_net_freq ) {
1119 full_processing = 0;
1120 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
1121 config.c_net_freq - (time(NULL)- last_run)
1125 working_ignetcfg = CtdlLoadIgNetCfg();
1127 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1128 * then we poll everyone. Otherwise we only poll nodes we have stuff
1131 network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
1132 DeleteHash(&working_ignetcfg);
1135 void LogDebugEnableNetworkClient(const int n)
1137 NetworkClientDebugEnabled = n;
1140 * Module entry point
1142 CTDL_MODULE_INIT(network_client)
1146 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
1148 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER, PRIO_SEND + 10);
1149 CtdlRegisterDebugFlagHook(HKEY("networkclient"), LogDebugEnableNetworkClient, &NetworkClientDebugEnabled);
1152 return "networkclient";