rename InitEventIO to EvConnectSock, since this suits better what this function does...
[citadel.git] / citadel / modules / network / serv_networkclient.c
1 /*
2  * This module handles shared rooms, inter-Citadel mail, and outbound
3  * mailing list processing.
4  *
5  * Copyright (c) 2000-2011 by the citadel.org team
6  *
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.
11  *
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.
16  *
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
20  *
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.
27  *
28  */
29
30
31 #include "sysdep.h"
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <fcntl.h>
36 #include <ctype.h>
37 #include <signal.h>
38 #include <pwd.h>
39 #include <errno.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <dirent.h>
43 #if TIME_WITH_SYS_TIME
44 # include <sys/time.h>
45 # include <time.h>
46 #else
47 # if HAVE_SYS_TIME_H
48 #  include <sys/time.h>
49 # else
50 #  include <time.h>
51 # endif
52 #endif
53 #ifdef HAVE_SYSCALL_H
54 # include <syscall.h>
55 #else 
56 # if HAVE_SYS_SYSCALL_H
57 #  include <sys/syscall.h>
58 # endif
59 #endif
60
61 #include <sys/wait.h>
62 #include <string.h>
63 #include <limits.h>
64 #include <libcitadel.h>
65 #include "citadel.h"
66 #include "server.h"
67 #include "citserver.h"
68 #include "support.h"
69 #include "config.h"
70 #include "user_ops.h"
71 #include "database.h"
72 #include "msgbase.h"
73 #include "internet_addressing.h"
74 #include "serv_network.h"
75 #include "clientsocket.h"
76 #include "file_ops.h"
77 #include "citadel_dirs.h"
78 #include "threads.h"
79
80 #ifndef HAVE_SNPRINTF
81 #include "snprintf.h"
82 #endif
83
84 #include "context.h"
85
86 #include "netconfig.h"
87 #include "ctdl_module.h"
88
89 struct CitContext networker_client_CC;
90
91 typedef enum _eNWCState {
92         eeGreating,
93         eAuth,
94         eNDOP,
95         eREAD,
96         eReadBLOB,
97         eCLOS,
98         eNUOP,
99         eWRIT,
100         eWriteBLOB,
101         eUCLS,
102         eQUIT
103 }eNWCState;
104
105
106 typedef struct _async_networker {
107         AsyncIO IO;
108         DNSQueryParts HostLookup;
109         eNWCState State;
110         long n;
111         StrBuf *SpoolFileName;
112         StrBuf *tempFileName;
113         StrBuf *node;
114         StrBuf *host;
115         StrBuf *port;
116         StrBuf *secret;
117         StrBuf          *Url;
118 } AsyncNetworker;
119
120 typedef eNextState(*NWClientHandler)(AsyncNetworker* NW);
121 eNextState nwc_get_one_host_ip(AsyncIO *IO);
122
123 eNextState nwc_connect_ip(AsyncIO *IO);
124
125 eNextState NWC_SendQUIT(AsyncNetworker *NW);
126 eNextState NWC_DispatchWriteDone(AsyncIO *IO);
127
128 void DeleteNetworker(void *vptr)
129 {
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         ((struct CitContext*)NW->IO.CitContext)->state = CON_IDLE;
139         ((struct CitContext*)NW->IO.CitContext)->kill_me = 1;
140         FreeAsyncIOContents(&NW->IO);
141         free(NW);
142 }
143
144 #define NWC_DBG_SEND() syslog(LOG_DEBUG, "NW client[%ld]: > %s", NW->n, ChrPtr(NW->IO.SendBuf.Buf))
145 #define NWC_DBG_READ() syslog(LOG_DEBUG, "NW client[%ld]: < %s\n", NW->n, ChrPtr(NW->IO.IOBuf))
146 #define NWC_OK (strncasecmp(ChrPtr(NW->IO.IOBuf), "+OK", 3) == 0)
147
148 eNextState FinalizeNetworker(AsyncIO *IO)
149 {
150         AsyncNetworker *NW = (AsyncNetworker *)IO->Data;
151
152         network_talking_to(SKEY(NW->node), NTT_REMOVE);
153
154         DeleteNetworker(IO->Data);
155         return eAbort;
156 }
157
158 eNextState NWC_ReadGreeting(AsyncNetworker *NW)
159 {
160         char connected_to[SIZ];
161         NWC_DBG_READ();
162         /* Read the server greeting */
163         /* Check that the remote is who we think it is and warn the Aide if not */
164         extract_token (connected_to, ChrPtr(NW->IO.IOBuf), 1, ' ', sizeof connected_to);
165         if (strcmp(connected_to, ChrPtr(NW->node)) != 0)
166         {
167                 StrBufPrintf(NW->IO.ErrMsg,
168                              "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
169                              connected_to, ChrPtr(NW->node));
170                 syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
171                 CtdlAideMessage(ChrPtr(NW->IO.ErrMsg), "Network error");
172                 return eAbort;/// todo: aide message in anderer queue speichern
173         }
174         return eSendReply;
175 }
176
177 eNextState NWC_SendAuth(AsyncNetworker *NW)
178 {
179         /* We're talking to the correct node.  Now identify ourselves. */
180         StrBufPrintf(NW->IO.SendBuf.Buf, "NETP %s|%s\n", 
181                      config.c_nodename, 
182                      ChrPtr(NW->secret));
183         NWC_DBG_SEND();
184         return eSendReply;
185 }
186
187 eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
188 {
189         NWC_DBG_READ();
190         if (ChrPtr(NW->IO.IOBuf)[0] == '2')
191         {
192                 return eSendReply;
193         }
194         else
195         {
196                 StrBufPrintf(NW->IO.ErrMsg,
197                              "Connected to node \"%s\" but my secret wasn't accurate.",
198                              ChrPtr(NW->node));
199                 syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
200                 CtdlAideMessage(ChrPtr(NW->IO.ErrMsg), "Network error");
201                 
202                 return eAbort;
203         }
204 }
205
206 eNextState NWC_SendNDOP(AsyncNetworker *NW)
207 {
208         NW->tempFileName = NewStrBuf();
209         NW->SpoolFileName = NewStrBuf();
210         StrBufPrintf(NW->tempFileName, 
211                      "%s/%s.%lx%x",
212                      ctdl_netin_dir,
213                      ChrPtr(NW->node),
214                      time(NULL),// TODO: get time from libev
215                      rand());
216         StrBufPrintf(NW->SpoolFileName, 
217                      "%s/%s.%lx%x",
218                      ctdl_nettmp_dir,
219                      ChrPtr(NW->node),
220                      time(NULL),// TODO: get time from libev
221                      rand());
222
223         /* We're talking to the correct node.  Now identify ourselves. */
224         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NDOP\n"));
225         NWC_DBG_SEND();
226         return eSendReply;
227 }
228
229 eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
230 {
231         int TotalSendSize;
232         NWC_DBG_READ();
233         if (ChrPtr(NW->IO.IOBuf)[0] == '2')
234         {
235
236                 NW->IO.IOB.TotalSentAlready = 0;
237                 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
238                 syslog(LOG_DEBUG, "Expecting to transfer %ld bytes\n", NW->IO.IOB.TotalSendSize);
239                 if (TotalSendSize <= 0) {
240                         NW->State = eNUOP - 1;
241                 }
242                 else {
243                         int fd;
244                         fd = open(ChrPtr(NW->SpoolFileName), 
245                                   O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY, 
246                                   S_IRUSR|S_IWUSR);
247                         if (fd < 0)
248                         {
249                                 syslog(LOG_CRIT,
250                                        "cannot open %s: %s\n", 
251                                        ChrPtr(NW->SpoolFileName), 
252                                        strerror(errno));
253
254                                 NW->State = eQUIT - 1;
255                                 return eAbort;
256                         }
257                         FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
258                 }
259                 return eSendReply;
260         }
261         else
262         {
263                 return eAbort;
264         }
265 }
266
267 eNextState NWC_SendREAD(AsyncNetworker *NW)
268 {
269         eNextState rc;
270
271         if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
272         {
273                 /*
274                  * If shutting down we can exit here and unlink the temp file.
275                  * this shouldn't loose us any messages.
276                  */
277                 if (server_shutting_down)
278                 {
279                         FDIOBufferDelete(&NW->IO.IOB);
280                         unlink(ChrPtr(NW->tempFileName));
281                         return eAbort;
282                 }
283                 StrBufPrintf(NW->IO.SendBuf.Buf, "READ %ld|%ld\n",
284                              NW->IO.IOB.TotalSentAlready,
285                              NW->IO.IOB.TotalSendSize);
286 /*
287                              ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
288                               ? IGNET_PACKET_SIZE : 
289                               (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
290                         );
291 */
292                 NWC_DBG_SEND();
293                 return eSendReply;
294         }
295         else 
296         {
297                 NW->State = eCLOS;
298                 rc = NWC_DispatchWriteDone(&NW->IO);
299                 NWC_DBG_SEND();
300
301                 return rc;
302         }
303 }
304
305 eNextState NWC_ReadREADState(AsyncNetworker *NW)
306 {
307         NWC_DBG_READ();
308         if (ChrPtr(NW->IO.IOBuf)[0] == '6')
309         {
310                 NW->IO.IOB.ChunkSendRemain = 
311                         NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
312                 return eReadFile;
313         }
314         return eAbort;
315 }
316 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
317 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
318 {
319         NWC_DBG_READ();
320         if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
321         {
322                 NW->State ++;
323
324                 FDIOBufferDelete(&NW->IO.IOB);
325                 
326                 if (link(ChrPtr(NW->SpoolFileName), ChrPtr(NW->tempFileName)) != 0) {
327                         syslog(LOG_ALERT, 
328                                "Could not link %s to %s: %s\n",
329                                ChrPtr(NW->tempFileName), 
330                                ChrPtr(NW->SpoolFileName), 
331                                strerror(errno));
332                 }
333         
334                 unlink(ChrPtr(NW->tempFileName));
335                 return NWC_DispatchWriteDone(&NW->IO);
336         }
337         else {
338                 NW->State --;
339                 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
340                 return NWC_DispatchWriteDone(&NW->IO);
341         }
342 }
343
344 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
345 {
346         NWC_DBG_READ();
347         if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
348         {
349                 NW->State ++;
350
351                 FDIOBufferDelete(&NW->IO.IOB);
352                 
353                 if (link(ChrPtr(NW->SpoolFileName), ChrPtr(NW->tempFileName)) != 0) {
354                         syslog(LOG_ALERT, 
355                                "Could not link %s to %s: %s\n",
356                                ChrPtr(NW->tempFileName), 
357                                ChrPtr(NW->SpoolFileName), 
358                                strerror(errno));
359                 }
360         
361                 unlink(ChrPtr(NW->tempFileName));
362                 return NWC_DispatchWriteDone(&NW->IO);
363         }
364         else {
365                 NW->State --;
366                 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
367                 return NWC_DispatchWriteDone(&NW->IO);
368         }
369 }
370 eNextState NWC_SendCLOS(AsyncNetworker *NW)
371 {
372         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
373         NWC_DBG_SEND();
374         return eSendReply;
375 }
376
377 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
378 {
379         NWC_DBG_READ();
380         if (ChrPtr(NW->IO.IOBuf)[0] != '2')
381                 return eTerminateConnection;
382         return eSendReply;
383 }
384
385
386 eNextState NWC_SendNUOP(AsyncNetworker *NW)
387 {
388         eNextState rc;
389         long TotalSendSize;
390         struct stat statbuf;
391         int fd;
392
393         StrBufPrintf(NW->tempFileName,
394                      "%s/%s",
395                      ctdl_netout_dir,
396                      ChrPtr(NW->node));
397         fd = open(ChrPtr(NW->tempFileName), O_RDONLY);
398         if (fd < 0) {
399                 if (errno != ENOENT) {
400                         syslog(LOG_CRIT,
401                                "cannot open %s: %s\n", 
402                                ChrPtr(NW->tempFileName), 
403                                strerror(errno));
404                 }
405                 NW->State = eQUIT;
406                 rc = NWC_SendQUIT(NW);
407                 NWC_DBG_SEND();
408         }
409
410         if (fstat(fd, &statbuf) == -1) {
411                 syslog(9, "FSTAT FAILED %s [%s]--\n", 
412                        ChrPtr(NW->tempFileName), 
413                        strerror(errno));
414                 if (fd > 0) close(fd);
415                 return eAbort;
416         }
417         TotalSendSize = statbuf.st_size;
418         if (TotalSendSize == 0) {
419                 syslog(LOG_DEBUG,
420                        "Nothing to send.\n");
421                 NW->State = eQUIT;
422                 rc = NWC_SendQUIT(NW);
423                 NWC_DBG_SEND();
424                 return rc;
425         }
426         FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
427
428         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
429         NWC_DBG_SEND();
430         return eSendReply;
431
432 }
433 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
434 {
435         NWC_DBG_READ();
436         if (ChrPtr(NW->IO.IOBuf)[0] != '2')
437                 return eAbort;
438         return eSendReply;
439 }
440
441 eNextState NWC_SendWRIT(AsyncNetworker *NW)
442 {
443         StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT %ld\n", 
444                      NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
445         NWC_DBG_SEND();
446         return eSendReply;
447 }
448 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
449 {
450         NWC_DBG_READ();
451         if (ChrPtr(NW->IO.IOBuf)[0] != '7')
452         {
453                 return eAbort;
454         }
455
456         NW->IO.IOB.ChunkSendRemain = 
457                 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
458         return eSendFile;
459 }
460
461 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
462 {
463         eNextState rc;
464         if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
465         {
466                 NW->State ++;
467
468                 FDIOBufferDelete(&NW->IO.IOB);
469                 rc =  NWC_DispatchWriteDone(&NW->IO);
470                 NW->State --;
471                 return rc;
472         }
473         else {
474                 NW->State --;
475                 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
476                 return NWC_DispatchWriteDone(&NW->IO);
477         }
478 }
479
480 eNextState NWC_SendUCLS(AsyncNetworker *NW)
481 {
482         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
483         NWC_DBG_SEND();
484         return eSendReply;
485
486 }
487 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
488 {
489         NWC_DBG_READ();
490
491         syslog(LOG_NOTICE, "Sent %ld octets to <%s>\n", NW->IO.IOB.ChunkSize, ChrPtr(NW->node));
492         if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
493                 syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->tempFileName));
494                 unlink(ChrPtr(NW->tempFileName));
495         }
496         return eSendReply;
497 }
498
499 eNextState NWC_SendQUIT(AsyncNetworker *NW)
500 {
501         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
502
503         NWC_DBG_SEND();
504         return eSendReply;
505 }
506
507 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
508 {
509         NWC_DBG_READ();
510
511         return eAbort;
512 }
513
514
515 NWClientHandler NWC_ReadHandlers[] = {
516         NWC_ReadGreeting,
517         NWC_ReadAuthReply,
518         NWC_ReadNDOPReply,
519         NWC_ReadREADState,
520         NWC_ReadREADBlob,
521         NWC_ReadCLOSReply,
522         NWC_ReadNUOPReply,
523         NWC_ReadWRITReply,
524         NWC_SendBlobDone,
525         NWC_ReadUCLS,
526         NWC_ReadQUIT};
527
528 long NWC_ConnTimeout = 100;
529
530 const long NWC_SendTimeouts[] = {
531         100,
532         100,
533         100,
534         100,
535         100,
536         100,
537         100,
538         100
539 };
540 const ConstStr NWC[] = {
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 ")},
547         {HKEY("Connection broken during ")},
548         {HKEY("Connection broken during ")}
549 };
550
551 NWClientHandler NWC_SendHandlers[] = {
552         NULL,
553         NWC_SendAuth,
554         NWC_SendNDOP,
555         NWC_SendREAD,
556         NWC_ReadREADBlobDone,
557         NWC_SendCLOS,
558         NWC_SendNUOP,
559         NWC_SendWRIT,
560         NWC_SendBlobDone,
561         NWC_SendUCLS,
562         NWC_SendQUIT
563 };
564
565 const long NWC_ReadTimeouts[] = {
566         100,
567         100,
568         100,
569         100,
570         100,
571         100,
572         100,
573         100,
574         100,
575         100
576 };
577
578
579
580
581 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
582 {
583         AsyncNetworker *NW = IO->Data;
584         struct hostent *hostent;
585
586         QueryCbDone(IO);
587
588         hostent = NW->HostLookup.VParsedDNSReply;
589         if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) && 
590             (hostent != NULL) ) {
591                 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
592                 if (NW->IO.ConnectMe->IPv6) {
593                         memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr, 
594                                &hostent->h_addr_list[0],
595                                sizeof(struct in6_addr));
596                         
597                         NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
598                         NW->IO.ConnectMe->Addr.sin6_port   = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
599                 }
600                 else {
601                         struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
602                         /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
603 //                      addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
604                         memcpy(&addr->sin_addr.s_addr, 
605                                hostent->h_addr_list[0], 
606                                sizeof(uint32_t));
607                         
608                         addr->sin_family = hostent->h_addrtype;
609                         addr->sin_port   = htons(504);/// default citadel port
610                         
611                 }
612                 return nwc_connect_ip(IO);
613         }
614         else
615                 return eAbort;
616 }
617
618
619 eNextState nwc_get_one_host_ip(AsyncIO *IO)
620 {
621         AsyncNetworker *NW = IO->Data;
622         /* 
623          * here we start with the lookup of one host.
624          */ 
625
626         InitC_ares_dns(IO);
627
628         syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
629
630         syslog(LOG_DEBUG, 
631                       "NWC client[%ld]: looking up %s-Record %s : %d ...\n", 
632                       NW->n, 
633                       (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
634                       NW->IO.ConnectMe->Host, 
635                       NW->IO.ConnectMe->Port);
636
637         QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a, 
638                    NW->IO.ConnectMe->Host, 
639                    &NW->IO, 
640                    &NW->HostLookup, 
641                    nwc_get_one_host_ip_done);
642         IO->NextState = eReadDNSReply;
643         return IO->NextState;
644 }
645 /**
646  * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
647  */
648 eReadState NWC_ReadServerStatus(AsyncIO *IO)
649 {
650 //      AsyncNetworker *NW = IO->Data;
651         eReadState Finished = eBufferNotEmpty; 
652
653         switch (IO->NextState) {
654         case eSendDNSQuery:
655         case eReadDNSReply:
656         case eDBQuery:
657         case eConnect:
658         case eTerminateConnection:
659         case eAbort:
660                 Finished = eReadFail;
661                 break;
662         case eSendReply: 
663         case eSendMore:
664         case eReadMore:
665         case eReadMessage: 
666                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
667                 break;
668         case eReadFile:
669         case eSendFile:
670         case eReadPayload:
671                 break;
672         }
673         return Finished;
674 }
675
676
677
678 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
679 {
680         return eAbort;
681 }
682
683 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
684 {
685         double Timeout = 0.0;
686
687         syslog(LOG_DEBUG, "NWC3: %s\n", __FUNCTION__);
688
689         switch (NextTCPState) {
690         case eSendReply:
691         case eSendMore:
692                 break;
693         case eReadFile:
694         case eReadMessage:
695                 Timeout = NWC_ReadTimeouts[NW->State];
696                 break;
697         case eReadPayload:
698                 Timeout = 100000;
699                 /* TODO!!! */
700                 break;
701         case eSendDNSQuery:
702         case eReadDNSReply:
703         case eConnect:
704         case eSendFile:
705 //TODO
706         case eTerminateConnection:
707         case eDBQuery:
708         case eAbort:
709         case eReadMore://// TODO
710                 return;
711         }
712         SetNextTimeout(&NW->IO, Timeout);
713 }
714
715
716 eNextState NWC_DispatchReadDone(AsyncIO *IO)
717 {
718         syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
719         AsyncNetworker *NW = IO->Data;
720         eNextState rc;
721
722         rc = NWC_ReadHandlers[NW->State](NW);
723         if (rc != eReadMore)
724                 NW->State++;
725         NWC_SetTimeout(rc, NW);
726         return rc;
727 }
728 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
729 {
730         syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
731         AsyncNetworker *NW = IO->Data;
732         eNextState rc;
733
734         rc = NWC_SendHandlers[NW->State](NW);
735         NWC_SetTimeout(rc, NW);
736         return rc;
737 }
738
739 /*****************************************************************************/
740 /*                     Networker CLIENT ERROR CATCHERS                       */
741 /*****************************************************************************/
742 eNextState NWC_Terminate(AsyncIO *IO)
743 {
744         syslog(LOG_DEBUG, "Nw: %s\n", __FUNCTION__);
745         FinalizeNetworker(IO);
746         return eAbort;
747 }
748
749 eNextState NWC_Timeout(AsyncIO *IO)
750 {
751         syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
752
753         return NWC_FailNetworkConnection(IO);
754 }
755 eNextState NWC_ConnFail(AsyncIO *IO)
756 {
757 ///     AsyncNetworker *NW = IO->Data;
758
759         syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
760 ////    StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); todo
761         return NWC_FailNetworkConnection(IO);
762 }
763 eNextState NWC_DNSFail(AsyncIO *IO)
764 {
765 ///     AsyncNetworker *NW = IO->Data;
766
767         syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
768 ////    StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); todo
769         return NWC_FailNetworkConnection(IO);
770 }
771 eNextState NWC_Shutdown(AsyncIO *IO)
772 {
773         syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
774 ////    pop3aggr *pMsg = IO->Data;
775
776         FinalizeNetworker(IO);
777         return eAbort;
778 }
779
780
781 eNextState nwc_connect_ip(AsyncIO *IO)
782 {
783         AsyncNetworker *NW = IO->Data;
784
785         syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
786         syslog(LOG_DEBUG, "network: polling <%s>\n", ChrPtr(NW->node));
787         syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n", 
788                ChrPtr(NW->node), 
789                ChrPtr(NW->host),
790                ChrPtr(NW->port));
791         
792         return EvConnectSock(IO, NW, 
793                              NWC_ConnTimeout, 
794                              NWC_ReadTimeouts[0],
795                              1);
796 }
797
798 void RunNetworker(AsyncNetworker *NW)
799 {
800         CitContext *SubC;
801
802         ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
803
804         NW->IO.Data          = NW;
805         NW->IO.SendDone      = NWC_DispatchWriteDone;
806         NW->IO.ReadDone      = NWC_DispatchReadDone;
807         NW->IO.Terminate     = NWC_Terminate;
808         NW->IO.LineReader    = NWC_ReadServerStatus;
809         NW->IO.ConnFail      = NWC_ConnFail;
810         NW->IO.DNS.Fail      = NWC_DNSFail;
811         NW->IO.Timeout       = NWC_Timeout;
812         NW->IO.ShutdownAbort = NWC_Shutdown;
813         
814         NW->IO.SendBuf.Buf   = NewStrBufPlain(NULL, 1024);
815         NW->IO.RecvBuf.Buf   = NewStrBufPlain(NULL, 1024);
816         NW->IO.IOBuf         = NewStrBuf();
817         
818         NW->IO.NextState     = eReadMessage;
819         SubC = CloneContext (&networker_client_CC);
820         SubC->session_specific_data = (char*) NW;
821         NW->IO.CitContext = SubC;
822
823         safestrncpy(SubC->cs_host, 
824                     ChrPtr(NW->host),
825                     sizeof(SubC->cs_host)); 
826
827         if (NW->IO.ConnectMe->IsIP) {
828                 QueueEventContext(&NW->IO,
829                                   nwc_connect_ip);
830         }
831         else { /* uneducated admin has chosen to add DNS to the equation... */
832                 QueueEventContext(&NW->IO,
833                                   nwc_get_one_host_ip);
834         }
835 }
836
837 /*
838  * Poll other Citadel nodes and transfer inbound/outbound network data.
839  * Set "full" to nonzero to force a poll of every node, or to zero to poll
840  * only nodes to which we have data to send.
841  */
842 void network_poll_other_citadel_nodes(int full_poll, char *working_ignetcfg)
843 {
844         AsyncNetworker *NW;
845         StrBuf *CfgData;
846         StrBuf *Line;
847         const char *lptr;
848         const char *CfgPtr;
849         int Done;
850         
851         int poll = 0;
852         
853         if ((working_ignetcfg == NULL) || (*working_ignetcfg == '\0')) {
854                 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
855                 return;
856         }
857         CfgData = NewStrBufPlain(working_ignetcfg, -1);
858         Line = NewStrBufPlain(NULL, StrLength(CfgData));
859         Done = 0;
860         CfgPtr = NULL;
861         while (!Done)
862         {
863                 /* Use the string tokenizer to grab one line at a time */
864                 StrBufSipLine(Line, CfgData, &CfgPtr);
865                 Done = CfgPtr == StrBufNOTNULL;
866                 if (StrLength(Line) > 0)
867                 {
868                         if(server_shutting_down)
869                                 return;/* TODO free stuff*/
870                         lptr = NULL;
871                         poll = 0;
872                         NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
873                         memset(NW, 0, sizeof(AsyncNetworker));
874                         
875                         NW->node = NewStrBufPlain(NULL, StrLength(Line));
876                         NW->host = NewStrBufPlain(NULL, StrLength(Line));
877                         NW->port = NewStrBufPlain(NULL, StrLength(Line));
878                         NW->secret = NewStrBufPlain(NULL, StrLength(Line));
879                         
880                         StrBufExtract_NextToken(NW->node, Line, &lptr, '|');
881                         StrBufExtract_NextToken(NW->secret, Line, &lptr, '|');
882                         StrBufExtract_NextToken(NW->host, Line, &lptr, '|');
883                         StrBufExtract_NextToken(NW->port, Line, &lptr, '|');
884                         if ( (StrLength(NW->node) != 0) && 
885                              (StrLength(NW->secret) != 0) &&
886                              (StrLength(NW->host) != 0) &&
887                              (StrLength(NW->port) != 0))
888                         {
889                                 poll = full_poll;
890                                 if (poll == 0)
891                                 {
892                                         NW->SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
893                                         StrBufAppendBufPlain(NW->SpoolFileName, HKEY("/"), 0);
894                                         StrBufAppendBuf(NW->SpoolFileName, NW->node, 0);
895                                         if (access(ChrPtr(NW->SpoolFileName), R_OK) == 0) {
896                                                 poll = 1;
897                                         }
898                                 }
899                         }
900                         if (poll) {
901                                 NW->Url = NewStrBufPlain(NULL, StrLength(Line));
902                                 StrBufPrintf(NW->Url, "citadel://:%s@%s:%s", 
903                                              ChrPtr(NW->secret),
904                                              ChrPtr(NW->host),
905                                              ChrPtr(NW->port));
906                                 if (!network_talking_to(SKEY(NW->node), NTT_CHECK))
907                                 {
908                                         network_talking_to(SKEY(NW->node), NTT_ADD);
909                                         RunNetworker(NW);
910                                         continue;
911                                 }
912                         }
913                         DeleteNetworker(NW);
914                 }
915         }
916         FreeStrBuf(&CfgData);
917         FreeStrBuf(&Line);
918
919 }
920
921
922 void network_do_clientqueue(void)
923 {
924         char *working_ignetcfg;
925         int full_processing = 1;
926         static time_t last_run = 0L;
927
928         /*
929          * Run the full set of processing tasks no more frequently
930          * than once every n seconds
931          */
932         if ( (time(NULL) - last_run) < config.c_net_freq ) {
933                 full_processing = 0;
934                 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
935                         config.c_net_freq - (time(NULL)- last_run)
936                 );
937         }
938
939         working_ignetcfg = load_working_ignetcfg();
940         /*
941          * Poll other Citadel nodes.  Maybe.  If "full_processing" is set
942          * then we poll everyone.  Otherwise we only poll nodes we have stuff
943          * to send to.
944          */
945         network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
946         if (working_ignetcfg)
947                 free(working_ignetcfg);
948 }
949
950
951
952 /*
953  * Module entry point
954  */
955 CTDL_MODULE_INIT(network_client)
956 {
957         if (!threading)
958         {
959                 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
960                 
961                 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER);
962         }
963         return "network_client";
964 }