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 as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * ** NOTE ** A word on the S_NETCONFIGS semaphore:
22 * This is a fairly high-level type of critical section. It ensures that no
23 * two threads work on the netconfigs files at the same time. Since we do
24 * so many things inside these, here are the rules:
25 * 1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
26 * 2. Do *not* perform any I/O with the client during these sections.
41 #include <sys/types.h>
43 #if TIME_WITH_SYS_TIME
44 # include <sys/time.h>
48 # include <sys/time.h>
56 # if HAVE_SYS_SYSCALL_H
57 # include <sys/syscall.h>
64 #include <libcitadel.h>
67 #include "citserver.h"
73 #include "internet_addressing.h"
74 #include "serv_network.h"
75 #include "clientsocket.h"
77 #include "citadel_dirs.h"
86 #include "netconfig.h"
87 #include "ctdl_module.h"
89 struct CitContext networker_client_CC;
91 #define NODE ChrPtr(((AsyncNetworker*)IO->Data)->node)
92 #define N ((AsyncNetworker*)IO->Data)->n
94 int NetworkClientDebugEnabled = 0;
96 #define NCDBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (NetworkClientDebugEnabled != 0))
98 #define EVN_syslog(LEVEL, FORMAT, ...) \
99 NCDBGLOG(LEVEL) syslog(LEVEL, \
100 "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT, \
101 IO->ID, CCID, NODE, N, __VA_ARGS__)
103 #define EVNM_syslog(LEVEL, FORMAT) \
104 NCDBGLOG(LEVEL) syslog(LEVEL, \
105 "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT, \
106 IO->ID, CCID, NODE, N)
108 #define EVNCS_syslog(LEVEL, FORMAT, ...) \
109 NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT, \
110 IO->ID, NODE, N, __VA_ARGS__)
112 #define EVNCSM_syslog(LEVEL, FORMAT) \
113 NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT, \
117 typedef enum _eNWCState {
131 typedef enum _eNWCVState {
146 ConstStr NWCStateStr[] = {
147 {HKEY("Looking up Host")},
148 {HKEY("Connecting host")},
149 {HKEY("Failed to connect")},
150 {HKEY("Rread Greeting")},
151 {HKEY("Authenticating")},
152 {HKEY("Auth failed by NTT")},
153 {HKEY("Auth failed")},
154 {HKEY("Downloading")},
155 {HKEY("Downloading Success")},
156 {HKEY("Uploading Spoolfile")},
157 {HKEY("Uploading done")},
161 void SetNWCState(AsyncIO *IO, eNWCVState State)
163 CitContext* CCC = IO->CitContext;
164 memcpy(CCC->cs_clientname, NWCStateStr[State].Key, NWCStateStr[State].len + 1);
167 typedef struct _async_networker {
169 DNSQueryParts HostLookup;
172 StrBuf *SpoolFileName;
173 StrBuf *tempFileName;
181 typedef eNextState(*NWClientHandler)(AsyncNetworker* NW);
182 eNextState nwc_get_one_host_ip(AsyncIO *IO);
184 eNextState nwc_connect_ip(AsyncIO *IO);
186 eNextState NWC_SendQUIT(AsyncNetworker *NW);
187 eNextState NWC_DispatchWriteDone(AsyncIO *IO);
189 void DeleteNetworker(void *vptr)
191 AsyncNetworker *NW = (AsyncNetworker *)vptr;
192 FreeStrBuf(&NW->SpoolFileName);
193 FreeStrBuf(&NW->tempFileName);
194 FreeStrBuf(&NW->node);
195 FreeStrBuf(&NW->host);
196 FreeStrBuf(&NW->port);
197 FreeStrBuf(&NW->secret);
198 FreeStrBuf(&NW->Url);
199 FreeStrBuf(&NW->IO.ErrMsg);
200 FreeAsyncIOContents(&NW->IO);
201 if (NW->HostLookup.VParsedDNSReply != NULL) {
202 NW->HostLookup.DNSReplyFree(NW->HostLookup.VParsedDNSReply);
203 NW->HostLookup.VParsedDNSReply = NULL;
208 #define NWC_DBG_SEND() EVN_syslog(LOG_DEBUG, ": > %s", ChrPtr(NW->IO.SendBuf.Buf))
209 #define NWC_DBG_READ() EVN_syslog(LOG_DEBUG, ": < %s\n", ChrPtr(NW->IO.IOBuf))
210 #define NWC_OK (strncasecmp(ChrPtr(NW->IO.IOBuf), "+OK", 3) == 0)
212 eNextState SendFailureMessage(AsyncIO *IO)
214 AsyncNetworker *NW = IO->Data;
218 strs[0] = ChrPtr(NW->node);
219 lens[0] = StrLength(NW->node);
221 strs[1] = ChrPtr(NW->IO.ErrMsg);
222 lens[1] = StrLength(NW->IO.ErrMsg);
224 ChrPtr(NW->IO.ErrMsg),
226 2, strs, (long*) &lens);
231 eNextState FinalizeNetworker(AsyncIO *IO)
233 AsyncNetworker *NW = (AsyncNetworker *)IO->Data;
235 network_talking_to(SKEY(NW->node), NTT_REMOVE);
237 DeleteNetworker(IO->Data);
241 eNextState NWC_ReadGreeting(AsyncNetworker *NW)
243 char connected_to[SIZ];
244 AsyncIO *IO = &NW->IO;
245 SetNWCState(IO, eNWCVSGreating);
247 /* Read the server greeting */
248 /* Check that the remote is who we think it is and warn the Aide if not */
249 extract_token (connected_to, ChrPtr(NW->IO.IOBuf), 1, ' ', sizeof connected_to);
250 if (strcmp(connected_to, ChrPtr(NW->node)) != 0)
252 if (NW->IO.ErrMsg == NULL)
253 NW->IO.ErrMsg = NewStrBuf();
254 StrBufPrintf(NW->IO.ErrMsg,
255 "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
256 connected_to, ChrPtr(NW->node));
257 EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
258 StopClientWatchers(IO, 1);
259 return QueueDBOperation(IO, SendFailureMessage);
264 eNextState NWC_SendAuth(AsyncNetworker *NW)
266 AsyncIO *IO = &NW->IO;
267 SetNWCState(IO, eNWCVSAuth);
268 /* We're talking to the correct node. Now identify ourselves. */
269 StrBufPrintf(NW->IO.SendBuf.Buf, "NETP %s|%s\n",
276 eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
278 AsyncIO *IO = &NW->IO;
280 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
286 int Error = atol(ChrPtr(NW->IO.IOBuf));
287 if (NW->IO.ErrMsg == NULL)
288 NW->IO.ErrMsg = NewStrBuf();
289 StrBufPrintf(NW->IO.ErrMsg,
290 "Connected to node \"%s\" but my secret wasn't accurate.\nReason was:%s\n",
291 ChrPtr(NW->node), ChrPtr(NW->IO.IOBuf) + 4);
293 SetNWCState(IO, eNWCVSAuthFailNTT);
295 "Already talking to %s; skipping this time.\n",
300 SetNWCState(IO, eNWCVSAuthFailNTT);
301 EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
302 StopClientWatchers(IO, 1);
303 return QueueDBOperation(IO, SendFailureMessage);
309 eNextState NWC_SendNDOP(AsyncNetworker *NW)
311 AsyncIO *IO = &NW->IO;
312 SetNWCState(IO, eNWCVSNDOP);
313 NW->tempFileName = NewStrBuf();
314 NW->SpoolFileName = NewStrBuf();
315 StrBufPrintf(NW->SpoolFileName,
319 time(NULL),// TODO: get time from libev
321 StrBufStripSlashes(NW->SpoolFileName, 1);
322 StrBufPrintf(NW->tempFileName,
326 time(NULL),// TODO: get time from libev
328 StrBufStripSlashes(NW->tempFileName, 1);
329 /* We're talking to the correct node. Now identify ourselves. */
330 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NDOP\n"));
335 eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
337 AsyncIO *IO = &NW->IO;
340 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
343 NW->IO.IOB.TotalSentAlready = 0;
344 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
345 EVN_syslog(LOG_DEBUG, "Expecting to transfer %d bytes\n", TotalSendSize);
346 if (TotalSendSize <= 0) {
347 NW->State = eNUOP - 1;
351 fd = open(ChrPtr(NW->tempFileName),
352 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
356 SetNWCState(IO, eNWCVSFail);
358 "cannot open %s: %s\n",
359 ChrPtr(NW->tempFileName),
362 NW->State = eQUIT - 1;
365 FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
371 SetNWCState(IO, eNWCVSFail);
376 eNextState NWC_SendREAD(AsyncNetworker *NW)
378 AsyncIO *IO = &NW->IO;
381 if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
384 * If shutting down we can exit here and unlink the temp file.
385 * this shouldn't loose us any messages.
387 if (server_shutting_down)
389 FDIOBufferDelete(&NW->IO.IOB);
390 unlink(ChrPtr(NW->tempFileName));
391 FDIOBufferDelete(&IO->IOB);
392 SetNWCState(IO, eNWCVSFail);
395 StrBufPrintf(NW->IO.SendBuf.Buf, "READ %ld|%ld\n",
396 NW->IO.IOB.TotalSentAlready,
397 NW->IO.IOB.TotalSendSize);
399 ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
400 ? IGNET_PACKET_SIZE :
401 (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
410 rc = NWC_DispatchWriteDone(&NW->IO);
417 eNextState NWC_ReadREADState(AsyncNetworker *NW)
419 AsyncIO *IO = &NW->IO;
421 if (ChrPtr(NW->IO.IOBuf)[0] == '6')
423 NW->IO.IOB.ChunkSendRemain =
424 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
427 FDIOBufferDelete(&IO->IOB);
430 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
431 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
434 AsyncIO *IO = &NW->IO;
436 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
440 FDIOBufferDelete(&NW->IO.IOB);
442 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
443 EVN_syslog(LOG_ALERT,
444 "Could not link %s to %s: %s\n",
445 ChrPtr(NW->tempFileName),
446 ChrPtr(NW->SpoolFileName),
450 unlink(ChrPtr(NW->tempFileName));
451 rc = NWC_DispatchWriteDone(&NW->IO);
457 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
458 return eSendReply; //NWC_DispatchWriteDone(&NW->IO);
462 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
465 AsyncIO *IO = &NW->IO;
466 /* we don't have any data to debug print here. */
467 if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
471 FDIOBufferDelete(&NW->IO.IOB);
472 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
473 EVN_syslog(LOG_ALERT,
474 "Could not link %s to %s: %s\n",
475 ChrPtr(NW->tempFileName),
476 ChrPtr(NW->SpoolFileName),
480 unlink(ChrPtr(NW->tempFileName));
481 rc = NWC_DispatchWriteDone(&NW->IO);
487 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
488 return NWC_DispatchWriteDone(&NW->IO);
491 eNextState NWC_SendCLOS(AsyncNetworker *NW)
493 AsyncIO *IO = &NW->IO;
494 SetNWCState(IO, eNWCVSNDOPDone);
495 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
500 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
502 AsyncIO *IO = &NW->IO;
504 FDIOBufferDelete(&IO->IOB);
505 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
506 return eTerminateConnection;
511 eNextState NWC_SendNUOP(AsyncNetworker *NW)
513 AsyncIO *IO = &NW->IO;
519 SetNWCState(IO, eNWCVSNUOP);
520 StrBufPrintf(NW->SpoolFileName,
524 StrBufStripSlashes(NW->SpoolFileName, 1);
526 fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
528 if (errno != ENOENT) {
530 "cannot open %s: %s\n",
531 ChrPtr(NW->SpoolFileName),
535 rc = NWC_SendQUIT(NW);
540 if (fstat(fd, &statbuf) == -1) {
541 EVN_syslog(LOG_CRIT, "FSTAT FAILED %s [%s]--\n",
542 ChrPtr(NW->SpoolFileName),
544 if (fd > 0) close(fd);
547 TotalSendSize = statbuf.st_size;
548 if (TotalSendSize == 0) {
549 EVNM_syslog(LOG_DEBUG,
550 "Nothing to send.\n");
552 rc = NWC_SendQUIT(NW);
554 if (fd > 0) close(fd);
557 FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
559 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
564 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
566 AsyncIO *IO = &NW->IO;
568 if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
569 FDIOBufferDelete(&IO->IOB);
575 eNextState NWC_SendWRIT(AsyncNetworker *NW)
577 AsyncIO *IO = &NW->IO;
578 StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT %ld\n",
579 NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
583 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
585 AsyncIO *IO = &NW->IO;
587 if (ChrPtr(NW->IO.IOBuf)[0] != '7')
589 FDIOBufferDelete(&IO->IOB);
593 NW->IO.IOB.ChunkSendRemain =
594 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
598 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
600 AsyncIO *IO = &NW->IO;
602 if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
606 FDIOBufferDelete(&IO->IOB);
607 rc = NWC_DispatchWriteDone(IO);
613 IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
614 rc = NWC_DispatchWriteDone(IO);
620 eNextState NWC_SendUCLS(AsyncNetworker *NW)
622 AsyncIO *IO = &NW->IO;
623 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
628 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
630 AsyncIO *IO = &NW->IO;
633 EVN_syslog(LOG_NOTICE, "Sent %ld octets to <%s>\n", NW->IO.IOB.ChunkSize, ChrPtr(NW->node));
634 if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
635 EVN_syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
636 unlink(ChrPtr(NW->SpoolFileName));
638 FDIOBufferDelete(&IO->IOB);
639 SetNWCState(IO, eNWCVSNUOPDone);
643 eNextState NWC_SendQUIT(AsyncNetworker *NW)
645 AsyncIO *IO = &NW->IO;
646 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
652 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
654 AsyncIO *IO = &NW->IO;
661 NWClientHandler NWC_ReadHandlers[] = {
674 long NWC_ConnTimeout = 100;
676 const long NWC_SendTimeouts[] = {
686 const ConstStr NWC[] = {
687 {HKEY("Connection broken during ")},
688 {HKEY("Connection broken during ")},
689 {HKEY("Connection broken during ")},
690 {HKEY("Connection broken during ")},
691 {HKEY("Connection broken during ")},
692 {HKEY("Connection broken during ")},
693 {HKEY("Connection broken during ")},
694 {HKEY("Connection broken during ")}
697 NWClientHandler NWC_SendHandlers[] = {
702 NWC_ReadREADBlobDone,
711 const long NWC_ReadTimeouts[] = {
727 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
729 AsyncNetworker *NW = IO->Data;
730 struct hostent *hostent;
734 hostent = NW->HostLookup.VParsedDNSReply;
735 if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) &&
736 (hostent != NULL) ) {
737 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
738 if (NW->IO.ConnectMe->IPv6) {
739 memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr,
740 &hostent->h_addr_list[0],
741 sizeof(struct in6_addr));
743 NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
744 NW->IO.ConnectMe->Addr.sin6_port = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
747 struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
748 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
749 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
750 memcpy(&addr->sin_addr.s_addr,
751 hostent->h_addr_list[0],
754 addr->sin_family = hostent->h_addrtype;
755 addr->sin_port = htons(504);/// default citadel port
758 return nwc_connect_ip(IO);
765 eNextState nwc_get_one_host_ip(AsyncIO *IO)
767 AsyncNetworker *NW = IO->Data;
769 * here we start with the lookup of one host.
772 EVN_syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
774 EVN_syslog(LOG_DEBUG,
775 "NWC client[%ld]: looking up %s-Record %s : %d ...\n",
777 (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
778 NW->IO.ConnectMe->Host,
779 NW->IO.ConnectMe->Port);
781 QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
782 NW->IO.ConnectMe->Host,
785 nwc_get_one_host_ip_done);
786 IO->NextState = eReadDNSReply;
787 return IO->NextState;
790 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
792 eReadState NWC_ReadServerStatus(AsyncIO *IO)
794 // AsyncNetworker *NW = IO->Data;
795 eReadState Finished = eBufferNotEmpty;
797 switch (IO->NextState) {
802 case eTerminateConnection:
804 Finished = eReadFail;
810 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
822 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
824 SetNWCState(IO, eNWCVSConnFail);
825 StopClientWatchers(IO, 1);
826 return QueueDBOperation(IO, SendFailureMessage);
829 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
831 AsyncIO *IO = &NW->IO;
832 double Timeout = 0.0;
834 EVN_syslog(LOG_DEBUG, "%s - %d\n", __FUNCTION__, NextTCPState);
836 switch (NextTCPState) {
842 Timeout = NWC_ReadTimeouts[NW->State];
853 case eTerminateConnection:
856 case eReadMore://// TODO
860 EVN_syslog(LOG_DEBUG,
865 SetNextTimeout(&NW->IO, Timeout*100);
870 eNextState NWC_DispatchReadDone(AsyncIO *IO)
872 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
873 AsyncNetworker *NW = IO->Data;
876 rc = NWC_ReadHandlers[NW->State](NW);
879 NWC_SetTimeout(rc, NW);
882 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
884 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
885 AsyncNetworker *NW = IO->Data;
888 rc = NWC_SendHandlers[NW->State](NW);
889 NWC_SetTimeout(rc, NW);
893 /*****************************************************************************/
894 /* Networker CLIENT ERROR CATCHERS */
895 /*****************************************************************************/
896 eNextState NWC_Terminate(AsyncIO *IO)
898 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
899 FinalizeNetworker(IO);
903 eNextState NWC_TerminateDB(AsyncIO *IO)
905 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
906 FinalizeNetworker(IO);
910 eNextState NWC_Timeout(AsyncIO *IO)
912 AsyncNetworker *NW = IO->Data;
913 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
915 if (NW->IO.ErrMsg == NULL)
916 NW->IO.ErrMsg = NewStrBuf();
917 StrBufPrintf(NW->IO.ErrMsg, "Timeout while talking to %s \r\n", ChrPtr(NW->host));
918 return NWC_FailNetworkConnection(IO);
920 eNextState NWC_ConnFail(AsyncIO *IO)
922 AsyncNetworker *NW = IO->Data;
924 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
925 if (NW->IO.ErrMsg == NULL)
926 NW->IO.ErrMsg = NewStrBuf();
927 StrBufPrintf(NW->IO.ErrMsg, "failed to connect %s \r\n", ChrPtr(NW->host));
929 return NWC_FailNetworkConnection(IO);
931 eNextState NWC_DNSFail(AsyncIO *IO)
933 AsyncNetworker *NW = IO->Data;
935 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
936 if (NW->IO.ErrMsg == NULL)
937 NW->IO.ErrMsg = NewStrBuf();
938 StrBufPrintf(NW->IO.ErrMsg, "failed to look up %s \r\n", ChrPtr(NW->host));
940 return NWC_FailNetworkConnection(IO);
942 eNextState NWC_Shutdown(AsyncIO *IO)
944 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
946 FinalizeNetworker(IO);
951 eNextState nwc_connect_ip(AsyncIO *IO)
953 AsyncNetworker *NW = IO->Data;
955 SetNWCState(&NW->IO, eNWCVSConnecting);
956 EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
957 EVN_syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n",
962 return EvConnectSock(IO,
968 static int NetworkerCount = 0;
969 void RunNetworker(AsyncNetworker *NW)
971 NW->n = NetworkerCount++;
972 network_talking_to(SKEY(NW->node), NTT_ADD);
973 syslog(LOG_DEBUG, "NW[%s][%ld]: polling\n", ChrPtr(NW->node), NW->n);
974 ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
976 InitIOStruct(&NW->IO,
979 NWC_ReadServerStatus,
981 NWC_DispatchWriteDone,
982 NWC_DispatchReadDone,
989 safestrncpy(((CitContext *)NW->IO.CitContext)->cs_host,
991 sizeof(((CitContext *)NW->IO.CitContext)->cs_host));
993 if (NW->IO.ConnectMe->IsIP) {
994 SetNWCState(&NW->IO, eNWCVSLookup);
995 QueueEventContext(&NW->IO,
998 else { /* uneducated admin has chosen to add DNS to the equation... */
999 SetNWCState(&NW->IO, eNWCVSConnecting);
1000 QueueEventContext(&NW->IO,
1001 nwc_get_one_host_ip);
1006 * Poll other Citadel nodes and transfer inbound/outbound network data.
1007 * Set "full" to nonzero to force a poll of every node, or to zero to poll
1008 * only nodes to which we have data to send.
1010 void network_poll_other_citadel_nodes(int full_poll, HashList *ignetcfg)
1017 StrBuf *SpoolFileName;
1021 if (GetCount(ignetcfg) ==0) {
1022 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
1025 become_session(&networker_client_CC);
1027 SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
1029 Pos = GetNewHashPos(ignetcfg, 0);
1031 while (GetNextHashPos(ignetcfg, Pos, &len, &key, &vCfg))
1033 /* Use the string tokenizer to grab one line at a time */
1034 if(server_shutting_down)
1035 return;/* TODO free stuff*/
1036 NodeConf *pNode = (NodeConf*) vCfg;
1038 NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
1039 memset(NW, 0, sizeof(AsyncNetworker));
1041 NW->node = NewStrBufDup(pNode->NodeName);
1042 NW->host = NewStrBufDup(pNode->Host);
1043 NW->port = NewStrBufDup(pNode->Port);
1044 NW->secret = NewStrBufDup(pNode->Secret);
1046 if ( (StrLength(NW->node) != 0) &&
1047 (StrLength(NW->secret) != 0) &&
1048 (StrLength(NW->host) != 0) &&
1049 (StrLength(NW->port) != 0))
1054 StrBufAppendBufPlain(SpoolFileName, HKEY("/"), 0);
1055 StrBufAppendBuf(SpoolFileName, NW->node, 0);
1056 StrBufStripSlashes(SpoolFileName, 1);
1058 if (access(ChrPtr(SpoolFileName), R_OK) == 0) {
1064 (StrLength(NW->host) > 0) &&
1065 strcmp("0.0.0.0", ChrPtr(NW->host)))
1067 NW->Url = NewStrBuf();
1068 StrBufPrintf(NW->Url, "citadel://%s@%s:%s",
1072 if (!network_talking_to(SKEY(NW->node), NTT_CHECK))
1078 DeleteNetworker(NW);
1080 FreeStrBuf(&SpoolFileName);
1081 DeleteHashPos(&Pos);
1085 void network_do_clientqueue(void)
1087 HashList *working_ignetcfg;
1088 int full_processing = 1;
1089 static time_t last_run = 0L;
1092 * Run the full set of processing tasks no more frequently
1093 * than once every n seconds
1095 if ( (time(NULL) - last_run) < config.c_net_freq ) {
1096 full_processing = 0;
1097 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
1098 config.c_net_freq - (time(NULL)- last_run)
1102 working_ignetcfg = load_ignetcfg();
1104 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1105 * then we poll everyone. Otherwise we only poll nodes we have stuff
1108 network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
1109 DeleteHash(&working_ignetcfg);
1112 void LogDebugEnableNetworkClient(const int n)
1114 NetworkClientDebugEnabled = n;
1117 * Module entry point
1119 CTDL_MODULE_INIT(network_client)
1123 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
1125 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER, PRIO_SEND + 10);
1126 CtdlRegisterDebugFlagHook(HKEY("networkclient"), LogDebugEnableNetworkClient, &NetworkClientDebugEnabled);
1129 return "networkclient";