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