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')
331 NW->IO.IOB.TotalSentAlready = 0;
332 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
333 EVN_syslog(LOG_DEBUG, "Expecting to transfer %d bytes\n", TotalSendSize);
334 if (TotalSendSize <= 0) {
335 NW->State = eNUOP - 1;
339 fd = open(ChrPtr(NW->tempFileName),
340 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
344 SetNWCState(IO, eNWCVSFail);
346 "cannot open %s: %s\n",
347 ChrPtr(NW->tempFileName),
350 NW->State = eQUIT - 1;
353 FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
359 SetNWCState(IO, eNWCVSFail);
364 eNextState NWC_SendREAD(AsyncNetworker *NW)
366 AsyncIO *IO = &NW->IO;
369 if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
372 * If shutting down we can exit here and unlink the temp file.
373 * this shouldn't loose us any messages.
375 if (server_shutting_down)
377 FDIOBufferDelete(&NW->IO.IOB);
378 unlink(ChrPtr(NW->tempFileName));
379 FDIOBufferDelete(&IO->IOB);
380 SetNWCState(IO, eNWCVSFail);
383 StrBufPrintf(NW->IO.SendBuf.Buf, "READ "LOFF_T_FMT"|%ld\n",
384 NW->IO.IOB.TotalSentAlready,
385 NW->IO.IOB.TotalSendSize);
387 ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
388 ? IGNET_PACKET_SIZE :
389 (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
398 rc = NWC_DispatchWriteDone(&NW->IO);
405 eNextState NWC_ReadREADState(AsyncNetworker *NW)
407 AsyncIO *IO = &NW->IO;
409 if (ChrPtr(NW->IO.IOBuf)[0] == '6')
411 NW->IO.IOB.ChunkSendRemain =
412 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
415 FDIOBufferDelete(&IO->IOB);
418 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
419 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
422 AsyncIO *IO = &NW->IO;
424 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
428 FDIOBufferDelete(&NW->IO.IOB);
430 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
431 EVN_syslog(LOG_ALERT,
432 "Could not link %s to %s: %s\n",
433 ChrPtr(NW->tempFileName),
434 ChrPtr(NW->SpoolFileName),
438 unlink(ChrPtr(NW->tempFileName));
439 rc = NWC_DispatchWriteDone(&NW->IO);
445 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
446 return eSendReply; //NWC_DispatchWriteDone(&NW->IO);
450 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
453 AsyncIO *IO = &NW->IO;
454 /* we don't have any data to debug print here. */
455 if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
459 FDIOBufferDelete(&NW->IO.IOB);
460 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
461 EVN_syslog(LOG_ALERT,
462 "Could not link %s to %s: %s\n",
463 ChrPtr(NW->tempFileName),
464 ChrPtr(NW->SpoolFileName),
468 unlink(ChrPtr(NW->tempFileName));
469 rc = NWC_DispatchWriteDone(&NW->IO);
475 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
476 return NWC_DispatchWriteDone(&NW->IO);
479 eNextState NWC_SendCLOS(AsyncNetworker *NW)
481 AsyncIO *IO = &NW->IO;
482 SetNWCState(IO, eNWCVSNDOPDone);
483 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
488 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
490 AsyncIO *IO = &NW->IO;
492 FDIOBufferDelete(&IO->IOB);
493 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
494 return eTerminateConnection;
499 eNextState NWC_SendNUOP(AsyncNetworker *NW)
501 AsyncIO *IO = &NW->IO;
507 SetNWCState(IO, eNWCVSNUOP);
508 StrBufPrintf(NW->SpoolFileName,
512 StrBufStripSlashes(NW->SpoolFileName, 1);
514 fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
516 if (errno != ENOENT) {
518 "cannot open %s: %s\n",
519 ChrPtr(NW->SpoolFileName),
523 rc = NWC_SendQUIT(NW);
528 if (fstat(fd, &statbuf) == -1) {
529 EVN_syslog(LOG_CRIT, "FSTAT FAILED %s [%s]--\n",
530 ChrPtr(NW->SpoolFileName),
532 if (fd > 0) close(fd);
535 TotalSendSize = statbuf.st_size;
536 if (TotalSendSize == 0) {
537 EVNM_syslog(LOG_DEBUG,
538 "Nothing to send.\n");
540 rc = NWC_SendQUIT(NW);
542 if (fd > 0) close(fd);
545 FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
547 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
552 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
554 AsyncIO *IO = &NW->IO;
556 if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
557 FDIOBufferDelete(&IO->IOB);
563 eNextState NWC_SendWRIT(AsyncNetworker *NW)
565 AsyncIO *IO = &NW->IO;
566 StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT "LOFF_T_FMT"\n",
567 NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
571 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
573 AsyncIO *IO = &NW->IO;
575 if (ChrPtr(NW->IO.IOBuf)[0] != '7')
577 FDIOBufferDelete(&IO->IOB);
581 NW->IO.IOB.ChunkSendRemain =
582 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
586 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
588 AsyncIO *IO = &NW->IO;
590 if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
594 FDIOBufferDelete(&IO->IOB);
595 rc = NWC_DispatchWriteDone(IO);
601 IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
602 rc = NWC_DispatchWriteDone(IO);
608 eNextState NWC_SendUCLS(AsyncNetworker *NW)
610 AsyncIO *IO = &NW->IO;
611 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
616 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
618 AsyncIO *IO = &NW->IO;
621 EVN_syslog(LOG_NOTICE, "Sent %ld octets to <%s>\n", NW->IO.IOB.ChunkSize, ChrPtr(NW->node));
622 if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
623 EVN_syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
624 unlink(ChrPtr(NW->SpoolFileName));
626 FDIOBufferDelete(&IO->IOB);
627 SetNWCState(IO, eNWCVSNUOPDone);
631 eNextState NWC_SendQUIT(AsyncNetworker *NW)
633 AsyncIO *IO = &NW->IO;
634 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
640 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
642 AsyncIO *IO = &NW->IO;
649 NWClientHandler NWC_ReadHandlers[] = {
662 long NWC_ConnTimeout = 100;
664 const long NWC_SendTimeouts[] = {
674 const ConstStr NWC[] = {
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 ")},
682 {HKEY("Connection broken during ")}
685 NWClientHandler NWC_SendHandlers[] = {
690 NWC_ReadREADBlobDone,
699 const long NWC_ReadTimeouts[] = {
715 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
717 AsyncNetworker *NW = IO->Data;
718 struct hostent *hostent;
722 hostent = NW->HostLookup.VParsedDNSReply;
723 if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) &&
724 (hostent != NULL) ) {
725 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
726 if (NW->IO.ConnectMe->IPv6) {
727 memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr,
728 &hostent->h_addr_list[0],
729 sizeof(struct in6_addr));
731 NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
732 NW->IO.ConnectMe->Addr.sin6_port = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
735 struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
736 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
737 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
738 memcpy(&addr->sin_addr.s_addr,
739 hostent->h_addr_list[0],
742 addr->sin_family = hostent->h_addrtype;
743 addr->sin_port = htons(504);/// default citadel port
746 return nwc_connect_ip(IO);
753 eNextState nwc_get_one_host_ip(AsyncIO *IO)
755 AsyncNetworker *NW = IO->Data;
757 * here we start with the lookup of one host.
760 EVN_syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
762 EVN_syslog(LOG_DEBUG,
763 "NWC client[%ld]: looking up %s-Record %s : %d ...\n",
765 (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
766 NW->IO.ConnectMe->Host,
767 NW->IO.ConnectMe->Port);
769 QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
770 NW->IO.ConnectMe->Host,
773 nwc_get_one_host_ip_done);
774 IO->NextState = eReadDNSReply;
775 return IO->NextState;
778 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
780 eReadState NWC_ReadServerStatus(AsyncIO *IO)
782 // AsyncNetworker *NW = IO->Data;
783 eReadState Finished = eBufferNotEmpty;
785 switch (IO->NextState) {
790 case eTerminateConnection:
792 Finished = eReadFail;
798 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
810 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
812 SetNWCState(IO, eNWCVSConnFail);
813 StopClientWatchers(IO, 1);
814 return QueueDBOperation(IO, SendFailureMessage);
817 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
819 AsyncIO *IO = &NW->IO;
820 double Timeout = 0.0;
822 EVN_syslog(LOG_DEBUG, "%s - %d\n", __FUNCTION__, NextTCPState);
824 switch (NextTCPState) {
830 Timeout = NWC_ReadTimeouts[NW->State];
841 case eTerminateConnection:
844 case eReadMore://// TODO
848 EVN_syslog(LOG_DEBUG,
853 SetNextTimeout(&NW->IO, Timeout*100);
858 eNextState NWC_DispatchReadDone(AsyncIO *IO)
860 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
861 AsyncNetworker *NW = IO->Data;
864 rc = NWC_ReadHandlers[NW->State](NW);
867 NWC_SetTimeout(rc, NW);
870 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
872 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
873 AsyncNetworker *NW = IO->Data;
876 rc = NWC_SendHandlers[NW->State](NW);
877 NWC_SetTimeout(rc, NW);
881 /*****************************************************************************/
882 /* Networker CLIENT ERROR CATCHERS */
883 /*****************************************************************************/
884 eNextState NWC_Terminate(AsyncIO *IO)
886 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
887 FinalizeNetworker(IO);
891 eNextState NWC_TerminateDB(AsyncIO *IO)
893 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
894 FinalizeNetworker(IO);
898 eNextState NWC_Timeout(AsyncIO *IO)
900 AsyncNetworker *NW = IO->Data;
901 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
903 if (NW->IO.ErrMsg == NULL)
904 NW->IO.ErrMsg = NewStrBuf();
905 StrBufPrintf(NW->IO.ErrMsg, "Timeout while talking to %s \r\n", ChrPtr(NW->host));
906 return NWC_FailNetworkConnection(IO);
908 eNextState NWC_ConnFail(AsyncIO *IO)
910 AsyncNetworker *NW = IO->Data;
912 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
913 if (NW->IO.ErrMsg == NULL)
914 NW->IO.ErrMsg = NewStrBuf();
915 StrBufPrintf(NW->IO.ErrMsg, "failed to connect %s \r\n", ChrPtr(NW->host));
917 return NWC_FailNetworkConnection(IO);
919 eNextState NWC_DNSFail(AsyncIO *IO)
921 AsyncNetworker *NW = IO->Data;
923 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
924 if (NW->IO.ErrMsg == NULL)
925 NW->IO.ErrMsg = NewStrBuf();
926 StrBufPrintf(NW->IO.ErrMsg, "failed to look up %s \r\n", ChrPtr(NW->host));
928 return NWC_FailNetworkConnection(IO);
930 eNextState NWC_Shutdown(AsyncIO *IO)
932 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
934 FinalizeNetworker(IO);
939 eNextState nwc_connect_ip(AsyncIO *IO)
941 AsyncNetworker *NW = IO->Data;
943 SetNWCState(&NW->IO, eNWCVSConnecting);
944 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
945 EVN_syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n",
950 return EvConnectSock(IO,
956 static int NetworkerCount = 0;
957 void RunNetworker(AsyncNetworker *NW)
959 NW->n = NetworkerCount++;
960 CtdlNetworkTalkingTo(SKEY(NW->node), NTT_ADD);
961 syslog(LOG_DEBUG, "NW[%s][%ld]: polling\n", ChrPtr(NW->node), NW->n);
962 ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
964 InitIOStruct(&NW->IO,
967 NWC_ReadServerStatus,
969 NWC_DispatchWriteDone,
970 NWC_DispatchReadDone,
977 safestrncpy(((CitContext *)NW->IO.CitContext)->cs_host,
979 sizeof(((CitContext *)NW->IO.CitContext)->cs_host));
981 if (NW->IO.ConnectMe->IsIP) {
982 SetNWCState(&NW->IO, eNWCVSLookup);
983 QueueEventContext(&NW->IO,
986 else { /* uneducated admin has chosen to add DNS to the equation... */
987 SetNWCState(&NW->IO, eNWCVSConnecting);
988 QueueEventContext(&NW->IO,
989 nwc_get_one_host_ip);
994 * Poll other Citadel nodes and transfer inbound/outbound network data.
995 * Set "full" to nonzero to force a poll of every node, or to zero to poll
996 * only nodes to which we have data to send.
998 void network_poll_other_citadel_nodes(int full_poll, HashList *ignetcfg)
1005 StrBuf *SpoolFileName;
1009 if (GetCount(ignetcfg) ==0) {
1010 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
1013 become_session(&networker_client_CC);
1015 SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
1017 Pos = GetNewHashPos(ignetcfg, 0);
1019 while (GetNextHashPos(ignetcfg, Pos, &len, &key, &vCfg))
1021 /* Use the string tokenizer to grab one line at a time */
1022 if(server_shutting_down)
1023 return;/* TODO free stuff*/
1024 CtdlNodeConf *pNode = (CtdlNodeConf*) vCfg;
1026 NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
1027 memset(NW, 0, sizeof(AsyncNetworker));
1029 NW->node = NewStrBufDup(pNode->NodeName);
1030 NW->host = NewStrBufDup(pNode->Host);
1031 NW->port = NewStrBufDup(pNode->Port);
1032 NW->secret = NewStrBufDup(pNode->Secret);
1034 if ( (StrLength(NW->node) != 0) &&
1035 (StrLength(NW->secret) != 0) &&
1036 (StrLength(NW->host) != 0) &&
1037 (StrLength(NW->port) != 0))
1042 StrBufAppendBufPlain(SpoolFileName, HKEY("/"), 0);
1043 StrBufAppendBuf(SpoolFileName, NW->node, 0);
1044 StrBufStripSlashes(SpoolFileName, 1);
1046 if (access(ChrPtr(SpoolFileName), R_OK) == 0) {
1052 (StrLength(NW->host) > 0) &&
1053 strcmp("0.0.0.0", ChrPtr(NW->host)))
1055 NW->Url = NewStrBuf();
1056 StrBufPrintf(NW->Url, "citadel://%s@%s:%s",
1060 if (!CtdlNetworkTalkingTo(SKEY(NW->node), NTT_CHECK))
1066 DeleteNetworker(NW);
1068 FreeStrBuf(&SpoolFileName);
1069 DeleteHashPos(&Pos);
1073 void network_do_clientqueue(void)
1075 HashList *working_ignetcfg;
1076 int full_processing = 1;
1077 static time_t last_run = 0L;
1080 * Run the full set of processing tasks no more frequently
1081 * than once every n seconds
1083 if ( (time(NULL) - last_run) < config.c_net_freq ) {
1084 full_processing = 0;
1085 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
1086 config.c_net_freq - (time(NULL)- last_run)
1090 working_ignetcfg = CtdlLoadIgNetCfg();
1092 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1093 * then we poll everyone. Otherwise we only poll nodes we have stuff
1096 network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
1097 DeleteHash(&working_ignetcfg);
1100 void LogDebugEnableNetworkClient(const int n)
1102 NetworkClientDebugEnabled = n;
1105 * Module entry point
1107 CTDL_MODULE_INIT(network_client)
1111 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
1113 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER, PRIO_SEND + 10);
1114 CtdlRegisterDebugFlagHook(HKEY("networkclient"), LogDebugEnableNetworkClient, &NetworkClientDebugEnabled);
1117 return "networkclient";