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