455e47f02e8e2bda9903b0f810143ddcfba55942
[citadel] / 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
129 void DestroyNetworker(AsyncNetworker *NW)
130 {
131 }
132
133 #define NWC_DBG_SEND() syslog(LOG_DEBUG, "NW client[%ld]: > %s", NW->n, ChrPtr(NW->IO.SendBuf.Buf))
134 #define NWC_DBG_READ() syslog(LOG_DEBUG, "NW client[%ld]: < %s\n", NW->n, ChrPtr(NW->IO.IOBuf))
135 #define NWC_OK (strncasecmp(ChrPtr(NW->IO.IOBuf), "+OK", 3) == 0)
136
137 eNextState NWC_ReadGreeting(AsyncNetworker *NW)
138 {
139         char connected_to[SIZ];
140         NWC_DBG_READ();
141         /* Read the server greeting */
142         /* Check that the remote is who we think it is and warn the Aide if not */
143         extract_token (connected_to, ChrPtr(NW->IO.IOBuf), 1, ' ', sizeof connected_to);
144         if (strcmp(connected_to, ChrPtr(NW->node)) != 0)
145         {
146                 StrBufPrintf(NW->IO.ErrMsg,
147                              "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
148                              connected_to, ChrPtr(NW->node));
149                 syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
150                 CtdlAideMessage(ChrPtr(NW->IO.ErrMsg), "Network error");
151                 return eAbort;/// todo: aide message in anderer queue speichern
152         }
153         return eSendReply;
154 }
155
156 eNextState NWC_SendAuth(AsyncNetworker *NW)
157 {
158         /* We're talking to the correct node.  Now identify ourselves. */
159         StrBufPrintf(NW->IO.SendBuf.Buf, "NETP %s|%s\n", 
160                      config.c_nodename, 
161                      ChrPtr(NW->secret));
162         NWC_DBG_SEND();
163         return eSendReply;
164 }
165
166 eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
167 {
168         NWC_DBG_READ();
169         if (ChrPtr(NW->IO.IOBuf)[0] == '2')
170         {
171                 return eSendReply;
172         }
173         else
174         {
175                 StrBufPrintf(NW->IO.ErrMsg,
176                              "Connected to node \"%s\" but my secret wasn't accurate.",
177                              ChrPtr(NW->node));
178                 syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
179                 CtdlAideMessage(ChrPtr(NW->IO.ErrMsg), "Network error");
180                 
181                 return eAbort;
182         }
183 }
184
185 eNextState NWC_SendNDOP(AsyncNetworker *NW)
186 {
187         NW->tempFileName = NewStrBuf();
188         NW->SpoolFileName = NewStrBuf();
189         StrBufPrintf(NW->tempFileName, 
190                      "%s/%s.%lx%x",
191                      ctdl_netin_dir,
192                      ChrPtr(NW->node),
193                      time(NULL),// TODO: get time from libev
194                      rand());
195         StrBufPrintf(NW->SpoolFileName, 
196                      "%s/%s.%lx%x",
197                      ctdl_nettmp_dir,
198                      ChrPtr(NW->node),
199                      time(NULL),// TODO: get time from libev
200                      rand());
201
202         /* We're talking to the correct node.  Now identify ourselves. */
203         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NDOP\n"));
204         NWC_DBG_SEND();
205         return eSendReply;
206 }
207
208 eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
209 {
210         int TotalSendSize;
211         NWC_DBG_READ();
212         if (ChrPtr(NW->IO.IOBuf)[0] == '2')
213         {
214
215                 NW->IO.IOB.TotalSentAlready = 0;
216                 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
217                 syslog(LOG_DEBUG, "Expecting to transfer %ld bytes\n", NW->IO.IOB.TotalSendSize);
218                 if (TotalSendSize <= 0) {
219                         NW->State = eNUOP - 1;
220                 }
221                 else {
222                         int fd;
223                         fd = open(ChrPtr(NW->SpoolFileName), 
224                                   O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY, 
225                                   S_IRUSR|S_IWUSR);
226                         if (fd < 0)
227                         {
228                                 syslog(LOG_CRIT,
229                                        "cannot open %s: %s\n", 
230                                        ChrPtr(NW->SpoolFileName), 
231                                        strerror(errno));
232
233                                 NW->State = eQUIT - 1;
234                                 return eAbort;
235                         }
236                         FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
237                 }
238                 return eSendReply;
239         }
240         else
241         {
242                 return eAbort;
243         }
244 }
245
246 eNextState NWC_SendREAD(AsyncNetworker *NW)
247 {
248         if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
249         {
250                 /*
251                  * If shutting down we can exit here and unlink the temp file.
252                  * this shouldn't loose us any messages.
253                  */
254                 if (server_shutting_down)
255                 {
256                         close(NW->IO.IOB.OtherFD);
257 //////                  unlink(ChrPtr(NW->tempFileName));
258                         return eAbort;
259                 }
260                 StrBufPrintf(NW->IO.SendBuf.Buf, "READ %ld|%ld\n",
261                              NW->IO.IOB.TotalSentAlready,
262                              ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
263                               ? IGNET_PACKET_SIZE : 
264                               (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
265                         );
266                 return eSendReply;
267
268
269
270         }
271         else {} // continue sending
272         return eSendReply;
273 }
274
275 eNextState NWC_ReadREADState(AsyncNetworker *NW)
276 {
277         NWC_DBG_READ();
278         if (ChrPtr(NW->IO.IOBuf)[0] == '6')
279         {
280                 NW->IO.IOB.ChunkSendRemain = 
281                         NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
282 ///             NW->IO.IOB.TotalSentAlready += NW->IO.IOB.ChunkSize;
283 /// TODO                StrBufReadjustIOBuffer(NW->IO.RecvBuf, NW->BlobReadSize);
284                 return eReadFile;
285         }
286         return eAbort;
287 }
288 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
289 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
290 {
291         /// FlushIOBuffer(NW->IO.RecvBuf); /// TODO
292
293         ///NW->bytes_received += NW->IO.IOB.ChunkSize;
294
295         if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
296         {
297                 NW->State = eREAD - 1;
298                 return eSendReply;/* now fetch next chunk*/
299         }
300         else
301                 return NWC_ReadREADBlobDone(NW);
302 }
303
304 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
305 {
306         if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
307         {
308                 NW->State ++;
309
310                 close(NW->IO.IOB.OtherFD);
311                 
312                 if (link(ChrPtr(NW->SpoolFileName), ChrPtr(NW->tempFileName)) != 0) {
313                         syslog(LOG_ALERT, 
314                                "Could not link %s to %s: %s\n",
315                                ChrPtr(NW->tempFileName), 
316                                ChrPtr(NW->SpoolFileName), 
317                                strerror(errno));
318                 }
319         
320 /////           unlink(ChrPtr(NW->tempFileName));
321                 return NWC_DispatchWriteDone(&NW->IO);
322         }
323         else {
324                 NW->State --;
325                 return NWC_DispatchWriteDone(&NW->IO);
326         }
327 }
328 eNextState NWC_SendCLOS(AsyncNetworker *NW)
329 {
330         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
331 ////    unlink(ChrPtr(NW->tempFileName));
332         return eReadMessage;
333 }
334
335 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
336 {
337 /// todo
338         if (ChrPtr(NW->IO.IOBuf)[0] != '2')
339                 return eTerminateConnection;
340         return eSendReply;
341 }
342
343
344 eNextState NWC_SendNUOP(AsyncNetworker *NW)
345 {
346         long TotalSendSize;
347         struct stat statbuf;
348         int fd;
349
350         StrBufPrintf(NW->tempFileName,
351                      "%s/%s",
352                      ctdl_netout_dir,
353                      ChrPtr(NW->node));
354         fd = open(ChrPtr(NW->tempFileName), O_RDONLY);
355         if (fd < 0) {
356                 if (errno != ENOENT) {
357                         syslog(LOG_CRIT,
358                                "cannot open %s: %s\n", 
359                                ChrPtr(NW->tempFileName), 
360                                strerror(errno));
361                 }
362                 NW->State = eQUIT;
363                 return NWC_SendQUIT(NW);
364         }
365
366         if (fstat(fd, &statbuf) == -1) {
367                 syslog(9, "FSTAT FAILED %s [%s]--\n", 
368                        ChrPtr(NW->tempFileName), 
369                        strerror(errno));
370                 if (fd > 0) close(fd);
371                 return eAbort;
372         }
373         TotalSendSize = statbuf.st_size;
374         if (TotalSendSize == 0) {
375                 syslog(LOG_DEBUG,
376                        "Nothing to send.\n");
377                 NW->State = eQUIT;
378                 return NWC_SendQUIT(NW);
379         }
380         FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
381
382 ////    NW->bytes_written = 0;
383
384         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
385         return eSendReply;
386
387 }
388 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
389 {
390         NWC_DBG_READ();
391         if (ChrPtr(NW->IO.IOBuf)[0] != '2')
392                 return eAbort;
393         return eSendReply;
394 }
395
396 eNextState NWC_SendWRIT(AsyncNetworker *NW)
397 {
398         StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT %ld\n", 
399                      NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
400
401         return eSendReply;
402 }
403 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
404 {
405         NWC_DBG_READ();
406         if (ChrPtr(NW->IO.IOBuf)[0] != '7')
407         {
408                 return eAbort;
409         }
410
411         NW->IO.IOB.ChunkSendRemain = 
412                 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
413 ///     NW->IO.IOB.TotalSentAlready += NW->IO.IOB.ChunkSize;
414         return eSendFile;
415 }
416
417 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
418 {
419         eNextState rc;
420         if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
421         {
422                 NW->State ++;
423
424                 close(NW->IO.IOB.OtherFD);
425 //// TODO: unlink networker file?               
426                 rc =  NWC_DispatchWriteDone(&NW->IO);
427                 NW->State --;
428                 return rc;
429         }
430         else {
431                 NW->State --;
432                 return NWC_DispatchWriteDone(&NW->IO);
433         }
434 }
435
436 eNextState NWC_SendUCLS(AsyncNetworker *NW)
437 {
438         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
439         return eSendReply;
440
441 }
442 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
443 {
444         NWC_DBG_READ();
445
446         syslog(LOG_NOTICE, "Sent %ld octets to <%s>\n", NW->IO.IOB.ChunkSize, ChrPtr(NW->node));
447 ///     syslog(LOG_DEBUG, "<%s\n", buf);
448         if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
449                 syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->tempFileName));
450 ///             unlink(ChrPtr(NW->tempFileName));
451         }
452         return eSendReply;
453 }
454
455 eNextState NWC_SendQUIT(AsyncNetworker *NW)
456 {
457         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
458
459         network_talking_to(ChrPtr(NW->node), NTT_REMOVE);
460         return eSendReply;
461 }
462
463 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
464 {
465         NWC_DBG_READ();
466
467         return eTerminateConnection;
468 }
469
470
471 NWClientHandler NWC_ReadHandlers[] = {
472         NWC_ReadGreeting,
473         NWC_ReadAuthReply,
474         NWC_ReadNDOPReply,
475         NWC_ReadREADState,
476         NWC_ReadREADBlob,
477         NWC_ReadCLOSReply,
478         NWC_ReadNUOPReply,
479         NWC_ReadWRITReply,
480         NWC_SendBlobDone,
481         NWC_ReadUCLS,
482         NWC_ReadQUIT
483 };
484
485 const long NWC_SendTimeouts[] = {
486         100,
487         100,
488         100,
489         100,
490         100,
491         100,
492         100,
493         100
494 };
495 const ConstStr NWC[] = {
496         {HKEY("Connection broken during ")},
497         {HKEY("Connection broken during ")},
498         {HKEY("Connection broken during ")},
499         {HKEY("Connection broken during ")},
500         {HKEY("Connection broken during ")},
501         {HKEY("Connection broken during ")},
502         {HKEY("Connection broken during ")},
503         {HKEY("Connection broken during ")}
504 };
505
506 NWClientHandler NWC_SendHandlers[] = {
507         NULL,
508         NWC_SendAuth,
509         NWC_SendNDOP,
510         NWC_SendREAD,
511         NWC_ReadREADBlobDone,
512         NWC_SendCLOS,
513         NWC_SendNUOP,
514         NWC_SendWRIT,
515         NWC_SendBlobDone,
516         NWC_SendUCLS,
517         NWC_SendQUIT
518 };
519
520 const long NWC_ReadTimeouts[] = {
521         100,
522         100,
523         100,
524         100,
525         100,
526         100,
527         100,
528         100,
529         100,
530         100
531 };
532
533
534
535
536 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
537 {
538         AsyncNetworker *NW = IO->Data;
539         struct hostent *hostent;
540
541         QueryCbDone(IO);
542
543         hostent = NW->HostLookup.VParsedDNSReply;
544         if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) && 
545             (hostent != NULL) ) {
546                 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
547                 if (NW->IO.ConnectMe->IPv6) {
548                         memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr, 
549                                &hostent->h_addr_list[0],
550                                sizeof(struct in6_addr));
551                         
552                         NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
553                         NW->IO.ConnectMe->Addr.sin6_port   = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
554                 }
555                 else {
556                         struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
557                         /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
558 //                      addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
559                         memcpy(&addr->sin_addr.s_addr, 
560                                hostent->h_addr_list[0], 
561                                sizeof(uint32_t));
562                         
563                         addr->sin_family = hostent->h_addrtype;
564                         addr->sin_port   = htons(504);/// default citadel port
565                         
566                 }
567                 return nwc_connect_ip(IO);
568         }
569         else
570                 return eAbort;
571 }
572
573
574 eNextState nwc_get_one_host_ip(AsyncIO *IO)
575 {
576         AsyncNetworker *NW = IO->Data;
577         /* 
578          * here we start with the lookup of one host. it might be...
579          * - the relay host *sigh*
580          * - the direct hostname if there was no mx record
581          * - one of the mx'es
582          */ 
583
584         InitC_ares_dns(IO);
585
586         syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
587
588         syslog(LOG_DEBUG, 
589                       "NWC client[%ld]: looking up %s-Record %s : %d ...\n", 
590                       NW->n, 
591                       (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
592                       NW->IO.ConnectMe->Host, 
593                       NW->IO.ConnectMe->Port);
594
595         QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a, 
596                    NW->IO.ConnectMe->Host, 
597                    &NW->IO, 
598                    &NW->HostLookup, 
599                    nwc_get_one_host_ip_done);
600         IO->NextState = eReadDNSReply;
601         return IO->NextState;
602 }
603 /**
604  * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
605  */
606 eReadState NWC_ReadServerStatus(AsyncIO *IO)
607 {
608         AsyncNetworker *NW = IO->Data;
609         eReadState Finished = eBufferNotEmpty; 
610
611         switch (IO->NextState) {
612         case eSendDNSQuery:
613         case eReadDNSReply:
614         case eDBQuery:
615         case eConnect:
616         case eTerminateConnection:
617         case eAbort:
618                 Finished = eReadFail;
619                 break;
620         case eSendReply: 
621         case eSendMore:
622         case eReadMore:
623         case eReadMessage: 
624                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
625                 break;
626         case eReadFile:
627         case eSendFile:
628         case eReadPayload:
629 ////TODO                Finished = IOBufferStrLength(&IO->RecvBuf) >= NW->BlobReadSize;
630                 break;
631         }
632         return Finished;
633 }
634
635
636
637 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
638 {
639         return eTerminateConnection;
640 }
641
642
643 eNextState NWC_DispatchReadDone(AsyncIO *IO)
644 {
645         syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
646         AsyncNetworker *NW = IO->Data;
647         eNextState rc;
648
649         rc = NWC_ReadHandlers[NW->State](NW);
650         if (rc != eReadMore)
651                 NW->State++;
652         ////NWCSetTimeout(rc, NW);
653         return rc;
654 }
655 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
656 {
657         syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
658         AsyncNetworker *NW = IO->Data;
659         eNextState rc;
660
661         rc = NWC_SendHandlers[NW->State](NW);
662         ////NWCSetTimeout(rc, NW);
663         return rc;
664 }
665
666 /*****************************************************************************/
667 /*                     Networker CLIENT ERROR CATCHERS                       */
668 /*****************************************************************************/
669 eNextState NWC_Terminate(AsyncIO *IO)
670 {
671         syslog(LOG_DEBUG, "Nw: %s\n", __FUNCTION__);
672 ///     FinalizeNetworker(IO); TODO
673         return eAbort;
674 }
675
676 eNextState NWC_Timeout(AsyncIO *IO)
677 {
678 //      AsyncNetworker *NW = IO->Data;
679
680         syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
681 //      StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); todo
682         return NWC_FailNetworkConnection(IO);
683 }
684 eNextState NWC_ConnFail(AsyncIO *IO)
685 {
686 ///     AsyncNetworker *NW = IO->Data;
687
688         syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
689 ////    StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); todo
690         return NWC_FailNetworkConnection(IO);
691 }
692 eNextState NWC_DNSFail(AsyncIO *IO)
693 {
694 ///     AsyncNetworker *NW = IO->Data;
695
696         syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
697 ////    StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); todo
698         return NWC_FailNetworkConnection(IO);
699 }
700 eNextState NWC_Shutdown(AsyncIO *IO)
701 {
702         syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
703 ////    pop3aggr *pMsg = IO->Data;
704
705         ////pMsg->MyQEntry->Status = 3;
706         ///StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message retrieval."));
707 ///     FinalizePOP3AggrRun(IO); todo
708         return eAbort;
709 }
710
711
712 eNextState nwc_connect_ip(AsyncIO *IO)
713 {
714         AsyncNetworker *NW = IO->Data;
715
716         syslog(LOG_DEBUG, "NW: %s\n", __FUNCTION__);
717         syslog(LOG_DEBUG, "network: polling <%s>\n", ChrPtr(NW->node));
718         syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n", 
719                ChrPtr(NW->node), 
720                ChrPtr(NW->host),
721                ChrPtr(NW->port));
722         
723 ////    IO->ConnectMe = &NW->Pop3Host;
724         /*  Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
725
726         /////// SetConnectStatus(IO);
727
728         return InitEventIO(IO, NW, 100, 100, 1); /*
729                                                  NWC_ConnTimeout, 
730                                                  NWC_ReadTimeouts[0],
731                                                  1);*/
732 }
733
734 void RunNetworker(AsyncNetworker *NW)
735 {
736         CitContext *SubC;
737
738         ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
739
740         NW->IO.Data          = NW;
741         NW->IO.SendDone      = NWC_DispatchWriteDone;
742         NW->IO.ReadDone      = NWC_DispatchReadDone;
743         NW->IO.Terminate     = NWC_Terminate;
744         NW->IO.LineReader    = NWC_ReadServerStatus;
745         NW->IO.ConnFail      = NWC_ConnFail;
746         NW->IO.DNSFail       = NWC_DNSFail;
747         NW->IO.Timeout       = NWC_Timeout;
748         NW->IO.ShutdownAbort = NWC_Shutdown;
749         
750         NW->IO.SendBuf.Buf   = NewStrBufPlain(NULL, 1024);
751         NW->IO.RecvBuf.Buf   = NewStrBufPlain(NULL, 1024);
752         NW->IO.IOBuf         = NewStrBuf();
753         
754         NW->IO.NextState     = eReadMessage;
755         SubC = CloneContext (&networker_client_CC);
756         SubC->session_specific_data = (char*) NW;
757         NW->IO.CitContext = SubC;
758
759         if (NW->IO.ConnectMe->IsIP) {
760                 QueueEventContext(&NW->IO,
761                                   nwc_connect_ip);
762         }
763         else { /* uneducated admin has chosen to add DNS to the equation... */
764                 QueueEventContext(&NW->IO,
765                                   nwc_get_one_host_ip);
766         }
767 }
768
769 /*
770  * Poll other Citadel nodes and transfer inbound/outbound network data.
771  * Set "full" to nonzero to force a poll of every node, or to zero to poll
772  * only nodes to which we have data to send.
773  */
774 void network_poll_other_citadel_nodes(int full_poll, char *working_ignetcfg)
775 {
776         AsyncNetworker *NW;
777         StrBuf *CfgData;
778         StrBuf *Line;
779         const char *lptr;
780         const char *CfgPtr;
781         int Done;
782         
783         int poll = 0;
784         
785         if ((working_ignetcfg == NULL) || (*working_ignetcfg == '\0')) {
786                 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
787                 return;
788         }
789         CfgData = NewStrBufPlain(working_ignetcfg, -1);
790         Line = NewStrBufPlain(NULL, StrLength(CfgData));
791         Done = 0;
792         CfgPtr = NULL;
793         while (!Done)
794         {
795                 /* Use the string tokenizer to grab one line at a time */
796                 StrBufSipLine(Line, CfgData, &CfgPtr);
797                 Done = CfgPtr == StrBufNOTNULL;
798                 if (StrLength(Line) > 0)
799                 {
800                         if(server_shutting_down)
801                                 return;/* TODO free stuff*/
802                         lptr = NULL;
803                         poll = 0;
804                         NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
805                         memset(NW, 0, sizeof(AsyncNetworker));
806                         
807                         NW->node = NewStrBufPlain(NULL, StrLength(Line));
808                         NW->host = NewStrBufPlain(NULL, StrLength(Line));
809                         NW->port = NewStrBufPlain(NULL, StrLength(Line));
810                         NW->secret = NewStrBufPlain(NULL, StrLength(Line));
811                         
812                         StrBufExtract_NextToken(NW->node, Line, &lptr, '|');
813                         StrBufExtract_NextToken(NW->secret, Line, &lptr, '|');
814                         StrBufExtract_NextToken(NW->host, Line, &lptr, '|');
815                         StrBufExtract_NextToken(NW->port, Line, &lptr, '|');
816                         if ( (StrLength(NW->node) != 0) && 
817                              (StrLength(NW->secret) != 0) &&
818                              (StrLength(NW->host) != 0) &&
819                              (StrLength(NW->port) != 0))
820                         {
821                                 poll = full_poll;
822                                 if (poll == 0)
823                                 {
824                                         NW->SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
825                                         StrBufAppendBufPlain(NW->SpoolFileName, HKEY("/"), 0);
826                                         StrBufAppendBuf(NW->SpoolFileName, NW->node, 0);
827                                         if (access(ChrPtr(NW->SpoolFileName), R_OK) == 0) {
828                                                 poll = 1;
829                                         }
830                                 }
831                         }
832                         if (poll) {
833                                 NW->Url = NewStrBufPlain(NULL, StrLength(Line));
834                                 StrBufPrintf(NW->Url, "citadel://:%s@%s:%s", 
835                                              ChrPtr(NW->secret),
836                                              ChrPtr(NW->host),
837                                              ChrPtr(NW->port));
838                                 if (!network_talking_to(ChrPtr(NW->node), NTT_CHECK))
839                                 {
840                                         network_talking_to(ChrPtr(NW->node), NTT_ADD);
841                                         RunNetworker(NW);
842                                         continue;
843                                 }
844                         }
845                         DestroyNetworker(NW);
846                 }
847         }
848
849 }
850
851
852 void network_do_clientqueue(void)
853 {
854         char *working_ignetcfg;
855         int full_processing = 1;
856         static time_t last_run = 0L;
857
858         /*
859          * Run the full set of processing tasks no more frequently
860          * than once every n seconds
861          */
862         if ( (time(NULL) - last_run) < config.c_net_freq ) {
863                 full_processing = 0;
864                 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
865                         config.c_net_freq - (time(NULL)- last_run)
866                 );
867         }
868
869         working_ignetcfg = load_working_ignetcfg();
870         /*
871          * Poll other Citadel nodes.  Maybe.  If "full_processing" is set
872          * then we poll everyone.  Otherwise we only poll nodes we have stuff
873          * to send to.
874          */
875         network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
876         if (working_ignetcfg)
877                 free(working_ignetcfg);
878 }
879
880
881
882 /*
883  * Module entry point
884  */
885 CTDL_MODULE_INIT(network_client)
886 {
887         if (!threading)
888         {
889                 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
890                 
891                 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER);
892         }
893         return "network_client";
894 }