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 "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT, \
86 IO->ID, CCID, NODE, N, __VA_ARGS__)
88 #define EVNM_syslog(LEVEL, FORMAT) \
89 NCDBGLOG(LEVEL) syslog(LEVEL, \
90 "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT, \
91 IO->ID, CCID, NODE, N)
93 #define EVNCS_syslog(LEVEL, FORMAT, ...) \
94 NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT, \
95 IO->ID, NODE, N, __VA_ARGS__)
97 #define EVNCSM_syslog(LEVEL, FORMAT) \
98 NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT, \
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));
245 StopClientWatchers(IO, 1);
246 return QueueDBOperation(IO, NWC_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, NWC_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')
329 int LogLevel = LOG_DEBUG;
331 NW->IO.IOB.TotalSentAlready = 0;
333 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
335 if (TotalSendSize > 0)
339 "Expecting to transfer %d bytes to %s\n",
341 ChrPtr(NW->tempFileName));
343 if (TotalSendSize <= 0) {
344 NW->State = eNUOP - 1;
348 fd = open(ChrPtr(NW->tempFileName),
349 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
353 SetNWCState(IO, eNWCVSFail);
355 "cannot open %s: %s\n",
356 ChrPtr(NW->tempFileName),
359 NW->State = eQUIT - 1;
362 FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
368 SetNWCState(IO, eNWCVSFail);
373 eNextState NWC_SendREAD(AsyncNetworker *NW)
375 AsyncIO *IO = &NW->IO;
378 if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
381 * If shutting down we can exit here and unlink the temp file.
382 * this shouldn't loose us any messages.
384 if (server_shutting_down)
386 FDIOBufferDelete(&NW->IO.IOB);
387 unlink(ChrPtr(NW->tempFileName));
388 FDIOBufferDelete(&IO->IOB);
389 SetNWCState(IO, eNWCVSFail);
392 StrBufPrintf(NW->IO.SendBuf.Buf, "READ "LOFF_T_FMT"|%ld\n",
393 NW->IO.IOB.TotalSentAlready,
394 NW->IO.IOB.TotalSendSize);
396 ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
397 ? IGNET_PACKET_SIZE :
398 (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
407 rc = NWC_DispatchWriteDone(&NW->IO);
414 eNextState NWC_ReadREADState(AsyncNetworker *NW)
416 AsyncIO *IO = &NW->IO;
418 if (ChrPtr(NW->IO.IOBuf)[0] == '6')
420 NW->IO.IOB.ChunkSendRemain =
421 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
424 FDIOBufferDelete(&IO->IOB);
427 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
428 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
431 AsyncIO *IO = &NW->IO;
433 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
437 FDIOBufferDelete(&NW->IO.IOB);
439 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
440 EVN_syslog(LOG_ALERT,
441 "Could not link %s to %s: %s\n",
442 ChrPtr(NW->tempFileName),
443 ChrPtr(NW->SpoolFileName),
449 ChrPtr(NW->tempFileName),
450 ChrPtr(NW->SpoolFileName));
453 unlink(ChrPtr(NW->tempFileName));
454 rc = NWC_DispatchWriteDone(&NW->IO);
460 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
461 return eSendReply; //NWC_DispatchWriteDone(&NW->IO);
465 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
468 AsyncIO *IO = &NW->IO;
469 /* we don't have any data to debug print here. */
470 if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
474 FDIOBufferDelete(&NW->IO.IOB);
475 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
476 EVN_syslog(LOG_ALERT,
477 "Could not link %s to %s: %s\n",
478 ChrPtr(NW->tempFileName),
479 ChrPtr(NW->SpoolFileName),
485 ChrPtr(NW->tempFileName),
486 ChrPtr(NW->SpoolFileName));
489 unlink(ChrPtr(NW->tempFileName));
490 rc = NWC_DispatchWriteDone(&NW->IO);
496 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
497 return NWC_DispatchWriteDone(&NW->IO);
500 eNextState NWC_SendCLOS(AsyncNetworker *NW)
502 AsyncIO *IO = &NW->IO;
503 SetNWCState(IO, eNWCVSNDOPDone);
504 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
509 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
511 AsyncIO *IO = &NW->IO;
513 FDIOBufferDelete(&IO->IOB);
514 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
515 return eTerminateConnection;
520 eNextState NWC_SendNUOP(AsyncNetworker *NW)
522 AsyncIO *IO = &NW->IO;
528 SetNWCState(IO, eNWCVSNUOP);
529 StrBufPrintf(NW->SpoolFileName,
533 StrBufStripSlashes(NW->SpoolFileName, 1);
535 fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
537 if (errno != ENOENT) {
539 "cannot open %s: %s\n",
540 ChrPtr(NW->SpoolFileName),
544 rc = NWC_SendQUIT(NW);
549 if (fstat(fd, &statbuf) == -1) {
550 EVN_syslog(LOG_CRIT, "FSTAT FAILED %s [%s]--\n",
551 ChrPtr(NW->SpoolFileName),
553 if (fd > 0) close(fd);
556 TotalSendSize = statbuf.st_size;
557 if (TotalSendSize == 0) {
558 EVNM_syslog(LOG_DEBUG,
559 "Nothing to send.\n");
561 rc = NWC_SendQUIT(NW);
563 if (fd > 0) close(fd);
569 "sending %s to %s\n",
570 ChrPtr(NW->SpoolFileName),
574 FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
576 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
581 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
583 AsyncIO *IO = &NW->IO;
585 if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
586 FDIOBufferDelete(&IO->IOB);
592 eNextState NWC_SendWRIT(AsyncNetworker *NW)
594 AsyncIO *IO = &NW->IO;
595 StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT "LOFF_T_FMT"\n",
596 NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
600 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
602 AsyncIO *IO = &NW->IO;
604 if (ChrPtr(NW->IO.IOBuf)[0] != '7')
606 FDIOBufferDelete(&IO->IOB);
610 NW->IO.IOB.ChunkSendRemain =
611 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
615 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
617 AsyncIO *IO = &NW->IO;
619 if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
623 FDIOBufferDelete(&IO->IOB);
624 rc = NWC_DispatchWriteDone(IO);
630 IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
631 rc = NWC_DispatchWriteDone(IO);
637 eNextState NWC_SendUCLS(AsyncNetworker *NW)
639 AsyncIO *IO = &NW->IO;
640 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
645 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
647 AsyncIO *IO = &NW->IO;
650 EVN_syslog(LOG_NOTICE,
651 "Sent %s [%ld] octets to <%s>\n",
652 ChrPtr(NW->SpoolFileName),
653 NW->IO.IOB.ChunkSize,
656 if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
657 EVN_syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
658 unlink(ChrPtr(NW->SpoolFileName));
660 FDIOBufferDelete(&IO->IOB);
661 SetNWCState(IO, eNWCVSNUOPDone);
665 eNextState NWC_SendQUIT(AsyncNetworker *NW)
667 AsyncIO *IO = &NW->IO;
668 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
674 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
676 AsyncIO *IO = &NW->IO;
683 NWClientHandler NWC_ReadHandlers[] = {
696 long NWC_ConnTimeout = 100;
698 const long NWC_SendTimeouts[] = {
708 const ConstStr NWC[] = {
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 ")},
716 {HKEY("Connection broken during ")}
719 NWClientHandler NWC_SendHandlers[] = {
724 NWC_ReadREADBlobDone,
733 const long NWC_ReadTimeouts[] = {
749 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
751 AsyncNetworker *NW = IO->Data;
752 struct hostent *hostent;
756 hostent = NW->HostLookup.VParsedDNSReply;
757 if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) &&
758 (hostent != NULL) ) {
759 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
760 if (NW->IO.ConnectMe->IPv6) {
761 memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr,
762 &hostent->h_addr_list[0],
763 sizeof(struct in6_addr));
765 NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
766 NW->IO.ConnectMe->Addr.sin6_port = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
769 struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
770 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
771 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
772 memcpy(&addr->sin_addr.s_addr,
773 hostent->h_addr_list[0],
776 addr->sin_family = hostent->h_addrtype;
777 addr->sin_port = htons(504);/// default citadel port
780 return nwc_connect_ip(IO);
787 eNextState nwc_get_one_host_ip(AsyncIO *IO)
789 AsyncNetworker *NW = IO->Data;
791 * here we start with the lookup of one host.
794 EVN_syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
796 EVN_syslog(LOG_DEBUG,
797 "NWC client[%ld]: looking up %s-Record %s : %d ...\n",
799 (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
800 NW->IO.ConnectMe->Host,
801 NW->IO.ConnectMe->Port);
803 QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
804 NW->IO.ConnectMe->Host,
807 nwc_get_one_host_ip_done);
808 IO->NextState = eReadDNSReply;
809 return IO->NextState;
812 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
814 eReadState NWC_ReadServerStatus(AsyncIO *IO)
816 // AsyncNetworker *NW = IO->Data;
817 eReadState Finished = eBufferNotEmpty;
819 switch (IO->NextState) {
824 case eTerminateConnection:
826 Finished = eReadFail;
832 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
844 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
846 SetNWCState(IO, eNWCVSConnFail);
847 StopClientWatchers(IO, 1);
848 return QueueDBOperation(IO, NWC_SendFailureMessage);
851 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
853 AsyncIO *IO = &NW->IO;
854 double Timeout = 0.0;
856 EVN_syslog(LOG_DEBUG, "%s - %d\n", __FUNCTION__, NextTCPState);
858 switch (NextTCPState) {
862 Timeout = NWC_ReadTimeouts[NW->State];
874 case eTerminateConnection:
879 EVN_syslog(LOG_DEBUG,
884 SetNextTimeout(&NW->IO, Timeout*100);
889 eNextState NWC_DispatchReadDone(AsyncIO *IO)
891 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
892 AsyncNetworker *NW = IO->Data;
895 rc = NWC_ReadHandlers[NW->State](NW);
900 NWC_SetTimeout(rc, NW);
904 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
906 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
907 AsyncNetworker *NW = IO->Data;
910 rc = NWC_SendHandlers[NW->State](NW);
911 NWC_SetTimeout(rc, NW);
915 /*****************************************************************************/
916 /* Networker CLIENT ERROR CATCHERS */
917 /*****************************************************************************/
918 eNextState NWC_Terminate(AsyncIO *IO)
920 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
921 FinalizeNetworker(IO);
925 eNextState NWC_TerminateDB(AsyncIO *IO)
927 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
928 FinalizeNetworker(IO);
932 eNextState NWC_Timeout(AsyncIO *IO)
934 AsyncNetworker *NW = IO->Data;
935 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
937 if (NW->IO.ErrMsg == NULL)
938 NW->IO.ErrMsg = NewStrBuf();
939 StrBufPrintf(NW->IO.ErrMsg, "Timeout while talking to %s \r\n", ChrPtr(NW->host));
940 return NWC_FailNetworkConnection(IO);
942 eNextState NWC_ConnFail(AsyncIO *IO)
944 AsyncNetworker *NW = IO->Data;
946 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
947 if (NW->IO.ErrMsg == NULL)
948 NW->IO.ErrMsg = NewStrBuf();
949 StrBufPrintf(NW->IO.ErrMsg, "failed to connect %s \r\n", ChrPtr(NW->host));
951 return NWC_FailNetworkConnection(IO);
953 eNextState NWC_DNSFail(AsyncIO *IO)
955 AsyncNetworker *NW = IO->Data;
957 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
958 if (NW->IO.ErrMsg == NULL)
959 NW->IO.ErrMsg = NewStrBuf();
960 StrBufPrintf(NW->IO.ErrMsg, "failed to look up %s \r\n", ChrPtr(NW->host));
962 return NWC_FailNetworkConnection(IO);
964 eNextState NWC_Shutdown(AsyncIO *IO)
966 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
968 FinalizeNetworker(IO);
973 eNextState nwc_connect_ip(AsyncIO *IO)
975 AsyncNetworker *NW = IO->Data;
977 SetNWCState(&NW->IO, eNWCVSConnecting);
978 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
979 EVN_syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n",
984 return EvConnectSock(IO,
990 static int NetworkerCount = 0;
991 void RunNetworker(AsyncNetworker *NW)
993 NW->n = NetworkerCount++;
994 CtdlNetworkTalkingTo(SKEY(NW->node), NTT_ADD);
995 syslog(LOG_DEBUG, "NW[%s][%ld]: polling\n", ChrPtr(NW->node), NW->n);
996 ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
998 InitIOStruct(&NW->IO,
1001 NWC_ReadServerStatus,
1003 NWC_DispatchWriteDone,
1004 NWC_DispatchReadDone,
1011 safestrncpy(((CitContext *)NW->IO.CitContext)->cs_host,
1013 sizeof(((CitContext *)NW->IO.CitContext)->cs_host));
1015 if (NW->IO.ConnectMe->IsIP) {
1016 SetNWCState(&NW->IO, eNWCVSLookup);
1017 QueueEventContext(&NW->IO,
1020 else { /* uneducated admin has chosen to add DNS to the equation... */
1021 SetNWCState(&NW->IO, eNWCVSConnecting);
1022 QueueEventContext(&NW->IO,
1023 nwc_get_one_host_ip);
1028 * Poll other Citadel nodes and transfer inbound/outbound network data.
1029 * Set "full" to nonzero to force a poll of every node, or to zero to poll
1030 * only nodes to which we have data to send.
1032 void network_poll_other_citadel_nodes(int full_poll, HashList *ignetcfg)
1039 StrBuf *SpoolFileName;
1043 if (GetCount(ignetcfg) ==0) {
1044 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
1047 become_session(&networker_client_CC);
1049 SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
1051 Pos = GetNewHashPos(ignetcfg, 0);
1053 while (GetNextHashPos(ignetcfg, Pos, &len, &key, &vCfg))
1055 /* Use the string tokenizer to grab one line at a time */
1056 if(server_shutting_down)
1057 return;/* TODO free stuff*/
1058 CtdlNodeConf *pNode = (CtdlNodeConf*) vCfg;
1060 NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
1061 memset(NW, 0, sizeof(AsyncNetworker));
1063 NW->node = NewStrBufDup(pNode->NodeName);
1064 NW->host = NewStrBufDup(pNode->Host);
1065 NW->port = NewStrBufDup(pNode->Port);
1066 NW->secret = NewStrBufDup(pNode->Secret);
1068 if ( (StrLength(NW->node) != 0) &&
1069 (StrLength(NW->secret) != 0) &&
1070 (StrLength(NW->host) != 0) &&
1071 (StrLength(NW->port) != 0))
1076 StrBufAppendBufPlain(SpoolFileName, HKEY("/"), 0);
1077 StrBufAppendBuf(SpoolFileName, NW->node, 0);
1078 StrBufStripSlashes(SpoolFileName, 1);
1080 if (access(ChrPtr(SpoolFileName), R_OK) == 0) {
1086 (StrLength(NW->host) > 0) &&
1087 strcmp("0.0.0.0", ChrPtr(NW->host)))
1089 NW->Url = NewStrBuf();
1090 StrBufPrintf(NW->Url, "citadel://%s@%s:%s",
1094 if (!CtdlNetworkTalkingTo(SKEY(NW->node), NTT_CHECK))
1100 DeleteNetworker(NW);
1102 FreeStrBuf(&SpoolFileName);
1103 DeleteHashPos(&Pos);
1107 void network_do_clientqueue(void)
1109 HashList *working_ignetcfg;
1110 int full_processing = 1;
1111 static time_t last_run = 0L;
1114 * Run the full set of processing tasks no more frequently
1115 * than once every n seconds
1117 if ( (time(NULL) - last_run) < config.c_net_freq ) {
1118 full_processing = 0;
1119 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
1120 config.c_net_freq - (time(NULL)- last_run)
1124 working_ignetcfg = CtdlLoadIgNetCfg();
1126 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1127 * then we poll everyone. Otherwise we only poll nodes we have stuff
1130 network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
1131 DeleteHash(&working_ignetcfg);
1134 void LogDebugEnableNetworkClient(const int n)
1136 NetworkClientDebugEnabled = n;
1139 * Module entry point
1141 CTDL_MODULE_INIT(network_client)
1145 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
1147 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER, PRIO_SEND + 10);
1148 CtdlRegisterDebugFlagHook(HKEY("networkclient"), LogDebugEnableNetworkClient, &NetworkClientDebugEnabled);
1151 return "networkclient";