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 if (NW->IO.CitContext != NULL) {
139 ((struct CitContext*)NW->IO.CitContext)->state = CON_IDLE;
140 ((struct CitContext*)NW->IO.CitContext)->kill_me = 1;
142 FreeAsyncIOContents(&NW->IO);
146 #define NWC_DBG_SEND() syslog(LOG_DEBUG, "NW client[%ld]: > %s", NW->n, ChrPtr(NW->IO.SendBuf.Buf))
147 #define NWC_DBG_READ() syslog(LOG_DEBUG, "NW client[%ld]: < %s\n", NW->n, ChrPtr(NW->IO.IOBuf))
148 #define NWC_OK (strncasecmp(ChrPtr(NW->IO.IOBuf), "+OK", 3) == 0)
150 eNextState FinalizeNetworker(AsyncIO *IO)
152 AsyncNetworker *NW = (AsyncNetworker *)IO->Data;
154 network_talking_to(SKEY(NW->node), NTT_REMOVE);
156 DeleteNetworker(IO->Data);
160 eNextState NWC_ReadGreeting(AsyncNetworker *NW)
162 char connected_to[SIZ];
164 /* Read the server greeting */
165 /* Check that the remote is who we think it is and warn the Aide if not */
166 extract_token (connected_to, ChrPtr(NW->IO.IOBuf), 1, ' ', sizeof connected_to);
167 if (strcmp(connected_to, ChrPtr(NW->node)) != 0)
169 StrBufPrintf(NW->IO.ErrMsg,
170 "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
171 connected_to, ChrPtr(NW->node));
172 syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
173 CtdlAideMessage(ChrPtr(NW->IO.ErrMsg), "Network error");
174 return eAbort;/// todo: aide message in anderer queue speichern
179 eNextState NWC_SendAuth(AsyncNetworker *NW)
181 /* We're talking to the correct node. Now identify ourselves. */
182 StrBufPrintf(NW->IO.SendBuf.Buf, "NETP %s|%s\n",
189 eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
192 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
198 StrBufPrintf(NW->IO.ErrMsg,
199 "Connected to node \"%s\" but my secret wasn't accurate.",
201 syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
202 CtdlAideMessage(ChrPtr(NW->IO.ErrMsg), "Network error");
208 eNextState NWC_SendNDOP(AsyncNetworker *NW)
210 NW->tempFileName = NewStrBuf();
211 NW->SpoolFileName = NewStrBuf();
212 StrBufPrintf(NW->tempFileName,
216 time(NULL),// TODO: get time from libev
218 StrBufPrintf(NW->SpoolFileName,
222 time(NULL),// TODO: get time from libev
225 /* We're talking to the correct node. Now identify ourselves. */
226 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NDOP\n"));
231 eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
235 if (ChrPtr(NW->IO.IOBuf)[0] == '2')
238 NW->IO.IOB.TotalSentAlready = 0;
239 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
240 syslog(LOG_DEBUG, "Expecting to transfer %ld bytes\n", NW->IO.IOB.TotalSendSize);
241 if (TotalSendSize <= 0) {
242 NW->State = eNUOP - 1;
246 fd = open(ChrPtr(NW->SpoolFileName),
247 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
252 "cannot open %s: %s\n",
253 ChrPtr(NW->SpoolFileName),
256 NW->State = eQUIT - 1;
259 FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
269 eNextState NWC_SendREAD(AsyncNetworker *NW)
273 if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
276 * If shutting down we can exit here and unlink the temp file.
277 * this shouldn't loose us any messages.
279 if (server_shutting_down)
281 FDIOBufferDelete(&NW->IO.IOB);
282 unlink(ChrPtr(NW->tempFileName));
285 StrBufPrintf(NW->IO.SendBuf.Buf, "READ %ld|%ld\n",
286 NW->IO.IOB.TotalSentAlready,
287 NW->IO.IOB.TotalSendSize);
289 ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
290 ? IGNET_PACKET_SIZE :
291 (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
300 rc = NWC_DispatchWriteDone(&NW->IO);
307 eNextState NWC_ReadREADState(AsyncNetworker *NW)
310 if (ChrPtr(NW->IO.IOBuf)[0] == '6')
312 NW->IO.IOB.ChunkSendRemain =
313 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
318 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
319 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
322 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
326 FDIOBufferDelete(&NW->IO.IOB);
328 if (link(ChrPtr(NW->SpoolFileName), ChrPtr(NW->tempFileName)) != 0) {
330 "Could not link %s to %s: %s\n",
331 ChrPtr(NW->tempFileName),
332 ChrPtr(NW->SpoolFileName),
336 unlink(ChrPtr(NW->tempFileName));
337 return NWC_DispatchWriteDone(&NW->IO);
341 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
342 return NWC_DispatchWriteDone(&NW->IO);
346 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
349 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
353 FDIOBufferDelete(&NW->IO.IOB);
355 if (link(ChrPtr(NW->SpoolFileName), ChrPtr(NW->tempFileName)) != 0) {
357 "Could not link %s to %s: %s\n",
358 ChrPtr(NW->tempFileName),
359 ChrPtr(NW->SpoolFileName),
363 unlink(ChrPtr(NW->tempFileName));
364 return NWC_DispatchWriteDone(&NW->IO);
368 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
369 return NWC_DispatchWriteDone(&NW->IO);
372 eNextState NWC_SendCLOS(AsyncNetworker *NW)
374 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
379 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
382 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
383 return eTerminateConnection;
388 eNextState NWC_SendNUOP(AsyncNetworker *NW)
395 StrBufPrintf(NW->tempFileName,
399 fd = open(ChrPtr(NW->tempFileName), O_RDONLY);
401 if (errno != ENOENT) {
403 "cannot open %s: %s\n",
404 ChrPtr(NW->tempFileName),
408 rc = NWC_SendQUIT(NW);
412 if (fstat(fd, &statbuf) == -1) {
413 syslog(9, "FSTAT FAILED %s [%s]--\n",
414 ChrPtr(NW->tempFileName),
416 if (fd > 0) close(fd);
419 TotalSendSize = statbuf.st_size;
420 if (TotalSendSize == 0) {
422 "Nothing to send.\n");
424 rc = NWC_SendQUIT(NW);
428 FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
430 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
435 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
438 if (ChrPtr(NW->IO.IOBuf)[0] != '2')
443 eNextState NWC_SendWRIT(AsyncNetworker *NW)
445 StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT %ld\n",
446 NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
450 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
453 if (ChrPtr(NW->IO.IOBuf)[0] != '7')
458 NW->IO.IOB.ChunkSendRemain =
459 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
463 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
466 if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
470 FDIOBufferDelete(&NW->IO.IOB);
471 rc = NWC_DispatchWriteDone(&NW->IO);
477 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
478 return NWC_DispatchWriteDone(&NW->IO);
482 eNextState NWC_SendUCLS(AsyncNetworker *NW)
484 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
489 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
493 syslog(LOG_NOTICE, "Sent %ld octets to <%s>\n", NW->IO.IOB.ChunkSize, ChrPtr(NW->node));
494 if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
495 syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->tempFileName));
496 unlink(ChrPtr(NW->tempFileName));
501 eNextState NWC_SendQUIT(AsyncNetworker *NW)
503 StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
509 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
517 NWClientHandler NWC_ReadHandlers[] = {
530 long NWC_ConnTimeout = 100;
532 const long NWC_SendTimeouts[] = {
542 const ConstStr NWC[] = {
543 {HKEY("Connection broken during ")},
544 {HKEY("Connection broken during ")},
545 {HKEY("Connection broken during ")},
546 {HKEY("Connection broken during ")},
547 {HKEY("Connection broken during ")},
548 {HKEY("Connection broken during ")},
549 {HKEY("Connection broken during ")},
550 {HKEY("Connection broken during ")}
553 NWClientHandler NWC_SendHandlers[] = {
558 NWC_ReadREADBlobDone,
567 const long NWC_ReadTimeouts[] = {
583 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
585 AsyncNetworker *NW = IO->Data;
586 struct hostent *hostent;
590 hostent = NW->HostLookup.VParsedDNSReply;
591 if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) &&
592 (hostent != NULL) ) {
593 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
594 if (NW->IO.ConnectMe->IPv6) {
595 memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr,
596 &hostent->h_addr_list[0],
597 sizeof(struct in6_addr));
599 NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
600 NW->IO.ConnectMe->Addr.sin6_port = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
603 struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
604 /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
605 // addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
606 memcpy(&addr->sin_addr.s_addr,
607 hostent->h_addr_list[0],
610 addr->sin_family = hostent->h_addrtype;
611 addr->sin_port = htons(504);/// default citadel port
614 return nwc_connect_ip(IO);
621 eNextState nwc_get_one_host_ip(AsyncIO *IO)
623 AsyncNetworker *NW = IO->Data;
625 * here we start with the lookup of one host.
630 syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
633 "NWC client[%ld]: looking up %s-Record %s : %d ...\n",
635 (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
636 NW->IO.ConnectMe->Host,
637 NW->IO.ConnectMe->Port);
639 QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
640 NW->IO.ConnectMe->Host,
643 nwc_get_one_host_ip_done);
644 IO->NextState = eReadDNSReply;
645 return IO->NextState;
648 * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
650 eReadState NWC_ReadServerStatus(AsyncIO *IO)
652 // AsyncNetworker *NW = IO->Data;
653 eReadState Finished = eBufferNotEmpty;
655 switch (IO->NextState) {
660 case eTerminateConnection:
662 Finished = eReadFail;
668 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
680 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
685 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
687 double Timeout = 0.0;
689 syslog(LOG_DEBUG, "NWC3: %s\n", __FUNCTION__);
691 switch (NextTCPState) {
697 Timeout = NWC_ReadTimeouts[NW->State];
708 case eTerminateConnection:
711 case eReadMore://// TODO
714 SetNextTimeout(&NW->IO, Timeout);
718 eNextState NWC_DispatchReadDone(AsyncIO *IO)
720 syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
721 AsyncNetworker *NW = IO->Data;
724 rc = NWC_ReadHandlers[NW->State](NW);
727 NWC_SetTimeout(rc, NW);
730 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
732 syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
733 AsyncNetworker *NW = IO->Data;
736 rc = NWC_SendHandlers[NW->State](NW);
737 NWC_SetTimeout(rc, NW);
741 /*****************************************************************************/
742 /* Networker CLIENT ERROR CATCHERS */
743 /*****************************************************************************/
744 eNextState NWC_Terminate(AsyncIO *IO)
746 syslog(LOG_DEBUG, "Nw: %s\n", __FUNCTION__);
747 FinalizeNetworker(IO);
751 eNextState NWC_Timeout(AsyncIO *IO)
753 syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
755 return NWC_FailNetworkConnection(IO);
757 eNextState NWC_ConnFail(AsyncIO *IO)
759 /// AsyncNetworker *NW = IO->Data;
761 syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
762 //// StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); todo
763 return NWC_FailNetworkConnection(IO);
765 eNextState NWC_DNSFail(AsyncIO *IO)
767 /// AsyncNetworker *NW = IO->Data;
769 syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
770 //// StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); todo
771 return NWC_FailNetworkConnection(IO);
773 eNextState NWC_Shutdown(AsyncIO *IO)
775 syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
776 //// pop3aggr *pMsg = IO->Data;
778 FinalizeNetworker(IO);
783 eNextState nwc_connect_ip(AsyncIO *IO)
785 AsyncNetworker *NW = IO->Data;
787 syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
788 syslog(LOG_DEBUG, "network: polling <%s>\n", ChrPtr(NW->node));
789 syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n",
794 return EvConnectSock(IO,
800 void RunNetworker(AsyncNetworker *NW)
802 ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
804 InitIOStruct(&NW->IO,
807 NWC_ReadServerStatus,
809 NWC_DispatchWriteDone,
810 NWC_DispatchReadDone,
816 safestrncpy(((CitContext *)NW->IO.CitContext)->cs_host,
818 sizeof(((CitContext *)NW->IO.CitContext)->cs_host));
820 if (NW->IO.ConnectMe->IsIP) {
821 QueueEventContext(&NW->IO,
824 else { /* uneducated admin has chosen to add DNS to the equation... */
825 QueueEventContext(&NW->IO,
826 nwc_get_one_host_ip);
831 * Poll other Citadel nodes and transfer inbound/outbound network data.
832 * Set "full" to nonzero to force a poll of every node, or to zero to poll
833 * only nodes to which we have data to send.
835 void network_poll_other_citadel_nodes(int full_poll, char *working_ignetcfg)
846 if ((working_ignetcfg == NULL) || (*working_ignetcfg == '\0')) {
847 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
850 become_session(&networker_client_CC);
852 CfgData = NewStrBufPlain(working_ignetcfg, -1);
853 Line = NewStrBufPlain(NULL, StrLength(CfgData));
858 /* Use the string tokenizer to grab one line at a time */
859 StrBufSipLine(Line, CfgData, &CfgPtr);
860 Done = CfgPtr == StrBufNOTNULL;
861 if (StrLength(Line) > 0)
863 if(server_shutting_down)
864 return;/* TODO free stuff*/
867 NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
868 memset(NW, 0, sizeof(AsyncNetworker));
870 NW->node = NewStrBufPlain(NULL, StrLength(Line));
871 NW->host = NewStrBufPlain(NULL, StrLength(Line));
872 NW->port = NewStrBufPlain(NULL, StrLength(Line));
873 NW->secret = NewStrBufPlain(NULL, StrLength(Line));
875 StrBufExtract_NextToken(NW->node, Line, &lptr, '|');
876 StrBufExtract_NextToken(NW->secret, Line, &lptr, '|');
877 StrBufExtract_NextToken(NW->host, Line, &lptr, '|');
878 StrBufExtract_NextToken(NW->port, Line, &lptr, '|');
879 if ( (StrLength(NW->node) != 0) &&
880 (StrLength(NW->secret) != 0) &&
881 (StrLength(NW->host) != 0) &&
882 (StrLength(NW->port) != 0))
887 NW->SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
888 StrBufAppendBufPlain(NW->SpoolFileName, HKEY("/"), 0);
889 StrBufAppendBuf(NW->SpoolFileName, NW->node, 0);
890 if (access(ChrPtr(NW->SpoolFileName), R_OK) == 0) {
896 NW->Url = NewStrBufPlain(NULL, StrLength(Line));
897 StrBufPrintf(NW->Url, "citadel://:%s@%s:%s",
901 if (!network_talking_to(SKEY(NW->node), NTT_CHECK))
903 network_talking_to(SKEY(NW->node), NTT_ADD);
911 FreeStrBuf(&CfgData);
917 void network_do_clientqueue(void)
919 char *working_ignetcfg;
920 int full_processing = 1;
921 static time_t last_run = 0L;
924 * Run the full set of processing tasks no more frequently
925 * than once every n seconds
927 if ( (time(NULL) - last_run) < config.c_net_freq ) {
929 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
930 config.c_net_freq - (time(NULL)- last_run)
934 working_ignetcfg = load_working_ignetcfg();
936 * Poll other Citadel nodes. Maybe. If "full_processing" is set
937 * then we poll everyone. Otherwise we only poll nodes we have stuff
940 network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
941 if (working_ignetcfg)
942 free(working_ignetcfg);
950 CTDL_MODULE_INIT(network_client)
954 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
956 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER);
958 return "network_client";