2 * This module handles shared rooms, inter-Citadel mail, and outbound
3 * mailing list processing.
5 * Copyright (c) 2000-2011 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 typedef enum _eNWCState {
106 typedef struct _async_networker {
108 DNSQueryParts HostLookup;
111 StrBuf *SpoolFileName;
112 StrBuf *tempFileName;
120 typedef eNextState(*NWClientHandler)(AsyncNetworker* NW);
121 eNextState nwc_get_one_host_ip(AsyncIO *IO);
123 eNextState nwc_connect_ip(AsyncIO *IO);
125 eNextState NWC_SendQUIT(AsyncNetworker *NW);
126 eNextState NWC_DispatchWriteDone(AsyncIO *IO);
128 void DeleteNetworker(void *vptr)
130 AsyncNetworker *NW = (AsyncNetworker *)vptr;
131 FreeStrBuf(&NW->SpoolFileName);
132 FreeStrBuf(&NW->tempFileName);
133 FreeStrBuf(&NW->node);
134 FreeStrBuf(&NW->host);
135 FreeStrBuf(&NW->port);
136 FreeStrBuf(&NW->secret);
137 FreeStrBuf(&NW->Url);
138 FreeAsyncIOContents(&NW->IO);
142 #define NWC_DBG_SEND() syslog(LOG_DEBUG, "NW client[%ld]: > %s", NW->n, ChrPtr(NW->IO.SendBuf.Buf))
143 #define NWC_DBG_READ() syslog(LOG_DEBUG, "NW client[%ld]: < %s\n", NW->n, ChrPtr(NW->IO.IOBuf))
144 #define NWC_OK (strncasecmp(ChrPtr(NW->IO.IOBuf), "+OK", 3) == 0)
146 eNextState FinalizeNetworker(AsyncIO *IO)
148 AsyncNetworker *NW = (AsyncNetworker *)IO->Data;
150 network_talking_to(SKEY(NW->node), NTT_REMOVE);
152 DeleteNetworker(IO->Data);
156 eNextState NWC_ReadGreeting(AsyncNetworker *NW)
158 char connected_to[SIZ];
160 /* Read the server greeting */
161 /* Check that the remote is who we think it is and warn the Aide if not */
162 extract_token (connected_to, ChrPtr(NW->IO.IOBuf), 1, ' ', sizeof connected_to);
163 if (strcmp(connected_to, ChrPtr(NW->node)) != 0)
165 StrBufPrintf(NW->IO.ErrMsg,
166 "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
167 connected_to, ChrPtr(NW->node));
168 syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
169 CtdlAideMessage(ChrPtr(NW->IO.ErrMsg), "Network error");
170 return eAbort;/// todo: aide message in anderer queue speichern
175 eNextState NWC_SendAuth(AsyncNetworker *NW)
177 /* We're talking to the correct node. Now identify ourselves. */
178 StrBufPrintf(NW->IO.SendBuf.Buf, "NETP %s|%s\n",
185 eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
188 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
194 StrBufPrintf(NW->IO.ErrMsg,
195 "Connected to node \"%s\" but my secret wasn't accurate.",
197 syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
198 CtdlAideMessage(ChrPtr(NW->IO.ErrMsg), "Network error");
204 eNextState NWC_SendNDOP(AsyncNetworker *NW)
206 NW->tempFileName = NewStrBuf();
207 NW->SpoolFileName = NewStrBuf();
208 StrBufPrintf(NW->tempFileName,
212 time(NULL),// TODO: get time from libev
214 StrBufPrintf(NW->SpoolFileName,
218 time(NULL),// TODO: get time from libev
221 /* We're talking to the correct node. Now identify ourselves. */
222 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NDOP\n"));
227 eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
231 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
234 NW->IO.IOB.TotalSentAlready = 0;
235 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
236 syslog(LOG_DEBUG, "Expecting to transfer %ld bytes\n", NW->IO.IOB.TotalSendSize);
237 if (TotalSendSize <= 0) {
238 NW->State = eNUOP - 1;
242 fd = open(ChrPtr(NW->SpoolFileName),
243 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
248 "cannot open %s: %s\n",
249 ChrPtr(NW->SpoolFileName),
252 NW->State = eQUIT - 1;
255 FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
265 eNextState NWC_SendREAD(AsyncNetworker *NW)
269 if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
272 * If shutting down we can exit here and unlink the temp file.
273 * this shouldn't loose us any messages.
275 if (server_shutting_down)
277 FDIOBufferDelete(&NW->IO.IOB);
278 unlink(ChrPtr(NW->tempFileName));
281 StrBufPrintf(NW->IO.SendBuf.Buf, "READ %ld|%ld\n",
282 NW->IO.IOB.TotalSentAlready,
283 NW->IO.IOB.TotalSendSize);
285 ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
286 ? IGNET_PACKET_SIZE :
287 (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
296 rc = NWC_DispatchWriteDone(&NW->IO);
303 eNextState NWC_ReadREADState(AsyncNetworker *NW)
306 if (ChrPtr(NW->IO.IOBuf)[0] == '6')
308 NW->IO.IOB.ChunkSendRemain =
309 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
314 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
315 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
318 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
322 FDIOBufferDelete(&NW->IO.IOB);
324 if (link(ChrPtr(NW->SpoolFileName), ChrPtr(NW->tempFileName)) != 0) {
326 "Could not link %s to %s: %s\n",
327 ChrPtr(NW->tempFileName),
328 ChrPtr(NW->SpoolFileName),
332 unlink(ChrPtr(NW->tempFileName));
333 return NWC_DispatchWriteDone(&NW->IO);
337 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
338 return NWC_DispatchWriteDone(&NW->IO);
342 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
345 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
349 FDIOBufferDelete(&NW->IO.IOB);
351 if (link(ChrPtr(NW->SpoolFileName), ChrPtr(NW->tempFileName)) != 0) {
353 "Could not link %s to %s: %s\n",
354 ChrPtr(NW->tempFileName),
355 ChrPtr(NW->SpoolFileName),
359 unlink(ChrPtr(NW->tempFileName));
360 return NWC_DispatchWriteDone(&NW->IO);
364 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
365 return NWC_DispatchWriteDone(&NW->IO);
368 eNextState NWC_SendCLOS(AsyncNetworker *NW)
370 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
375 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
378 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
379 return eTerminateConnection;
384 eNextState NWC_SendNUOP(AsyncNetworker *NW)
391 StrBufPrintf(NW->tempFileName,
395 fd = open(ChrPtr(NW->tempFileName), O_RDONLY);
397 if (errno != ENOENT) {
399 "cannot open %s: %s\n",
400 ChrPtr(NW->tempFileName),
404 rc = NWC_SendQUIT(NW);
408 if (fstat(fd, &statbuf) == -1) {
409 syslog(9, "FSTAT FAILED %s [%s]--\n",
410 ChrPtr(NW->tempFileName),
412 if (fd > 0) close(fd);
415 TotalSendSize = statbuf.st_size;
416 if (TotalSendSize == 0) {
418 "Nothing to send.\n");
420 rc = NWC_SendQUIT(NW);
424 FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
426 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
431 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
434 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
439 eNextState NWC_SendWRIT(AsyncNetworker *NW)
441 StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT %ld\n",
442 NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
446 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
449 if (ChrPtr(NW->IO.IOBuf)[0] != '7')
454 NW->IO.IOB.ChunkSendRemain =
455 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
459 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
462 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
466 FDIOBufferDelete(&NW->IO.IOB);
467 rc = NWC_DispatchWriteDone(&NW->IO);
473 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
474 return NWC_DispatchWriteDone(&NW->IO);
478 eNextState NWC_SendUCLS(AsyncNetworker *NW)
480 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
485 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
489 syslog(LOG_NOTICE, "Sent %ld octets to <%s>\n", NW->IO.IOB.ChunkSize, ChrPtr(NW->node));
490 if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
491 syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->tempFileName));
492 unlink(ChrPtr(NW->tempFileName));
497 eNextState NWC_SendQUIT(AsyncNetworker *NW)
499 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
505 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
513 NWClientHandler NWC_ReadHandlers[] = {
526 long NWC_ConnTimeout = 100;
528 const long NWC_SendTimeouts[] = {
538 const ConstStr NWC[] = {
539 {HKEY("Connection broken during ")},
540 {HKEY("Connection broken during ")},
541 {HKEY("Connection broken during ")},
542 {HKEY("Connection broken during ")},
543 {HKEY("Connection broken during ")},
544 {HKEY("Connection broken during ")},
545 {HKEY("Connection broken during ")},
546 {HKEY("Connection broken during ")}
549 NWClientHandler NWC_SendHandlers[] = {
554 NWC_ReadREADBlobDone,
563 const long NWC_ReadTimeouts[] = {
579 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
581 AsyncNetworker *NW = IO->Data;
582 struct hostent *hostent;
586 hostent = NW->HostLookup.VParsedDNSReply;
587 if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) &&
588 (hostent != NULL) ) {
589 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
590 if (NW->IO.ConnectMe->IPv6) {
591 memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr,
592 &hostent->h_addr_list[0],
593 sizeof(struct in6_addr));
595 NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
596 NW->IO.ConnectMe->Addr.sin6_port = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
599 struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
600 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
601 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
602 memcpy(&addr->sin_addr.s_addr,
603 hostent->h_addr_list[0],
606 addr->sin_family = hostent->h_addrtype;
607 addr->sin_port = htons(504);/// default citadel port
610 return nwc_connect_ip(IO);
617 eNextState nwc_get_one_host_ip(AsyncIO *IO)
619 AsyncNetworker *NW = IO->Data;
621 * here we start with the lookup of one host.
626 syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
629 "NWC client[%ld]: looking up %s-Record %s : %d ...\n",
631 (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
632 NW->IO.ConnectMe->Host,
633 NW->IO.ConnectMe->Port);
635 QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
636 NW->IO.ConnectMe->Host,
639 nwc_get_one_host_ip_done);
640 IO->NextState = eReadDNSReply;
641 return IO->NextState;
644 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
646 eReadState NWC_ReadServerStatus(AsyncIO *IO)
648 // AsyncNetworker *NW = IO->Data;
649 eReadState Finished = eBufferNotEmpty;
651 switch (IO->NextState) {
656 case eTerminateConnection:
658 Finished = eReadFail;
664 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
676 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
681 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
683 double Timeout = 0.0;
685 syslog(LOG_DEBUG, "NWC3: %s\n", __FUNCTION__);
687 switch (NextTCPState) {
693 Timeout = NWC_ReadTimeouts[NW->State];
704 case eTerminateConnection:
707 case eReadMore://// TODO
710 SetNextTimeout(&NW->IO, Timeout);
714 eNextState NWC_DispatchReadDone(AsyncIO *IO)
716 syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
717 AsyncNetworker *NW = IO->Data;
720 rc = NWC_ReadHandlers[NW->State](NW);
723 NWC_SetTimeout(rc, NW);
726 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
728 syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
729 AsyncNetworker *NW = IO->Data;
732 rc = NWC_SendHandlers[NW->State](NW);
733 NWC_SetTimeout(rc, NW);
737 /*****************************************************************************/
738 /* Networker CLIENT ERROR CATCHERS */
739 /*****************************************************************************/
740 eNextState NWC_Terminate(AsyncIO *IO)
742 syslog(LOG_DEBUG, "Nw: %s\n", __FUNCTION__);
743 FinalizeNetworker(IO);
747 eNextState NWC_Timeout(AsyncIO *IO)
749 syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
751 return NWC_FailNetworkConnection(IO);
753 eNextState NWC_ConnFail(AsyncIO *IO)
755 /// AsyncNetworker *NW = IO->Data;
757 syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
758 //// StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); todo
759 return NWC_FailNetworkConnection(IO);
761 eNextState NWC_DNSFail(AsyncIO *IO)
763 /// AsyncNetworker *NW = IO->Data;
765 syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
766 //// StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); todo
767 return NWC_FailNetworkConnection(IO);
769 eNextState NWC_Shutdown(AsyncIO *IO)
771 syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
772 //// pop3aggr *pMsg = IO->Data;
774 FinalizeNetworker(IO);
779 eNextState nwc_connect_ip(AsyncIO *IO)
781 AsyncNetworker *NW = IO->Data;
783 syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
784 syslog(LOG_DEBUG, "network: polling <%s>\n", ChrPtr(NW->node));
785 syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n",
790 return InitEventIO(IO, NW,
796 void RunNetworker(AsyncNetworker *NW)
800 ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
803 NW->IO.SendDone = NWC_DispatchWriteDone;
804 NW->IO.ReadDone = NWC_DispatchReadDone;
805 NW->IO.Terminate = NWC_Terminate;
806 NW->IO.LineReader = NWC_ReadServerStatus;
807 NW->IO.ConnFail = NWC_ConnFail;
808 NW->IO.DNSFail = NWC_DNSFail;
809 NW->IO.Timeout = NWC_Timeout;
810 NW->IO.ShutdownAbort = NWC_Shutdown;
812 NW->IO.SendBuf.Buf = NewStrBufPlain(NULL, 1024);
813 NW->IO.RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
814 NW->IO.IOBuf = NewStrBuf();
816 NW->IO.NextState = eReadMessage;
817 SubC = CloneContext (&networker_client_CC);
818 SubC->session_specific_data = (char*) NW;
819 NW->IO.CitContext = SubC;
821 if (NW->IO.ConnectMe->IsIP) {
822 QueueEventContext(&NW->IO,
825 else { /* uneducated admin has chosen to add DNS to the equation... */
826 QueueEventContext(&NW->IO,
827 nwc_get_one_host_ip);
832 * Poll other Citadel nodes and transfer inbound/outbound network data.
833 * Set "full" to nonzero to force a poll of every node, or to zero to poll
834 * only nodes to which we have data to send.
836 void network_poll_other_citadel_nodes(int full_poll, char *working_ignetcfg)
847 if ((working_ignetcfg == NULL) || (*working_ignetcfg == '\0')) {
848 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
851 CfgData = NewStrBufPlain(working_ignetcfg, -1);
852 Line = NewStrBufPlain(NULL, StrLength(CfgData));
857 /* Use the string tokenizer to grab one line at a time */
858 StrBufSipLine(Line, CfgData, &CfgPtr);
859 Done = CfgPtr == StrBufNOTNULL;
860 if (StrLength(Line) > 0)
862 if(server_shutting_down)
863 return;/* TODO free stuff*/
866 NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
867 memset(NW, 0, sizeof(AsyncNetworker));
869 NW->node = NewStrBufPlain(NULL, StrLength(Line));
870 NW->host = NewStrBufPlain(NULL, StrLength(Line));
871 NW->port = NewStrBufPlain(NULL, StrLength(Line));
872 NW->secret = NewStrBufPlain(NULL, StrLength(Line));
874 StrBufExtract_NextToken(NW->node, Line, &lptr, '|');
875 StrBufExtract_NextToken(NW->secret, Line, &lptr, '|');
876 StrBufExtract_NextToken(NW->host, Line, &lptr, '|');
877 StrBufExtract_NextToken(NW->port, Line, &lptr, '|');
878 if ( (StrLength(NW->node) != 0) &&
879 (StrLength(NW->secret) != 0) &&
880 (StrLength(NW->host) != 0) &&
881 (StrLength(NW->port) != 0))
886 NW->SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
887 StrBufAppendBufPlain(NW->SpoolFileName, HKEY("/"), 0);
888 StrBufAppendBuf(NW->SpoolFileName, NW->node, 0);
889 if (access(ChrPtr(NW->SpoolFileName), R_OK) == 0) {
895 NW->Url = NewStrBufPlain(NULL, StrLength(Line));
896 StrBufPrintf(NW->Url, "citadel://:%s@%s:%s",
900 if (!network_talking_to(SKEY(NW->node), NTT_CHECK))
902 network_talking_to(SKEY(NW->node), NTT_ADD);
910 FreeStrBuf(&CfgData);
916 void network_do_clientqueue(void)
918 char *working_ignetcfg;
919 int full_processing = 1;
920 static time_t last_run = 0L;
923 * Run the full set of processing tasks no more frequently
924 * than once every n seconds
926 if ( (time(NULL) - last_run) < config.c_net_freq ) {
928 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
929 config.c_net_freq - (time(NULL)- last_run)
933 working_ignetcfg = load_working_ignetcfg();
935 * Poll other Citadel nodes. Maybe. If "full_processing" is set
936 * then we poll everyone. Otherwise we only poll nodes we have stuff
939 network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
940 if (working_ignetcfg)
941 free(working_ignetcfg);
949 CTDL_MODULE_INIT(network_client)
953 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
955 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER);
957 return "network_client";