NetworkConfig: move to RoomNetworkConfig; move NTT-List into its own file.
[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-2012 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, version 3.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  * ** NOTE **   A word on the S_NETCONFIGS semaphore:
16  * This is a fairly high-level type of critical section.  It ensures that no
17  * two threads work on the netconfigs files at the same time.  Since we do
18  * so many things inside these, here are the rules:
19  *  1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
20  *  2. Do *not* perform any I/O with the client during these sections.
21  *
22  */
23
24
25 #include "sysdep.h"
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <ctype.h>
31 #include <signal.h>
32 #include <pwd.h>
33 #include <errno.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <dirent.h>
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
39 # include <time.h>
40 #else
41 # if HAVE_SYS_TIME_H
42 #  include <sys/time.h>
43 # else
44 #  include <time.h>
45 # endif
46 #endif
47 #ifdef HAVE_SYSCALL_H
48 # include <syscall.h>
49 #else 
50 # if HAVE_SYS_SYSCALL_H
51 #  include <sys/syscall.h>
52 # endif
53 #endif
54
55 #include <sys/wait.h>
56 #include <string.h>
57 #include <limits.h>
58 #include <libcitadel.h>
59 #include "citadel.h"
60 #include "server.h"
61 #include "citserver.h"
62 #include "support.h"
63 #include "config.h"
64 #include "user_ops.h"
65 #include "database.h"
66 #include "msgbase.h"
67 #include "internet_addressing.h"
68 #include "serv_network.h"
69 #include "clientsocket.h"
70 #include "file_ops.h"
71 #include "citadel_dirs.h"
72 #include "threads.h"
73 #include "context.h"
74 #include "ctdl_module.h"
75
76 struct CitContext networker_client_CC;
77
78 #define NODE ChrPtr(((AsyncNetworker*)IO->Data)->node)
79 #define N ((AsyncNetworker*)IO->Data)->n
80
81 int NetworkClientDebugEnabled = 0;
82
83 #define NCDBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (NetworkClientDebugEnabled != 0))
84
85 #define EVN_syslog(LEVEL, FORMAT, ...)                                  \
86         NCDBGLOG(LEVEL) syslog(LEVEL,                                   \
87                                "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT,       \
88                                IO->ID, CCID, NODE, N, __VA_ARGS__)
89
90 #define EVNM_syslog(LEVEL, FORMAT)                                      \
91         NCDBGLOG(LEVEL) syslog(LEVEL,                                   \
92                                "IO[%ld]CC[%d]NW[%s][%ld]" FORMAT,       \
93                                IO->ID, CCID, NODE, N)
94
95 #define EVNCS_syslog(LEVEL, FORMAT, ...) \
96         NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT,      \
97                                IO->ID, NODE, N, __VA_ARGS__)
98
99 #define EVNCSM_syslog(LEVEL, FORMAT) \
100         NCDBGLOG(LEVEL) syslog(LEVEL, "IO[%ld]NW[%s][%ld]" FORMAT,      \
101                                IO->ID, NODE, N)
102
103
104 typedef enum _eNWCState {
105         eGreating,
106         eAuth,
107         eNDOP,
108         eREAD,
109         eReadBLOB,
110         eCLOS,
111         eNUOP,
112         eWRIT,
113         eWriteBLOB,
114         eUCLS,
115         eQUIT
116 }eNWCState;
117
118 typedef enum _eNWCVState {
119         eNWCVSLookup,
120         eNWCVSConnecting,
121         eNWCVSConnFail,
122         eNWCVSGreating,
123         eNWCVSAuth,
124         eNWCVSAuthFailNTT,
125         eNWCVSAuthFail,
126         eNWCVSNDOP,
127         eNWCVSNDOPDone,
128         eNWCVSNUOP,
129         eNWCVSNUOPDone,
130         eNWCVSFail
131 }eNWCVState;
132
133 ConstStr NWCStateStr[] = {
134         {HKEY("Looking up Host")},
135         {HKEY("Connecting host")},
136         {HKEY("Failed to connect")},
137         {HKEY("Rread Greeting")},
138         {HKEY("Authenticating")},
139         {HKEY("Auth failed by NTT")},
140         {HKEY("Auth failed")},
141         {HKEY("Downloading")},
142         {HKEY("Downloading Success")},
143         {HKEY("Uploading Spoolfile")},
144         {HKEY("Uploading done")},
145         {HKEY("failed")}
146 };
147
148 void SetNWCState(AsyncIO *IO, eNWCVState State)
149 {
150         CitContext* CCC = IO->CitContext;
151         memcpy(CCC->cs_clientname, NWCStateStr[State].Key, NWCStateStr[State].len + 1);
152 }
153
154 typedef struct _async_networker {
155         AsyncIO IO;
156         DNSQueryParts HostLookup;
157         eNWCState State;
158         long n;
159         StrBuf *SpoolFileName;
160         StrBuf *tempFileName;
161         StrBuf *node;
162         StrBuf *host;
163         StrBuf *port;
164         StrBuf *secret;
165         StrBuf          *Url;
166 } AsyncNetworker;
167
168 typedef eNextState(*NWClientHandler)(AsyncNetworker* NW);
169 eNextState nwc_get_one_host_ip(AsyncIO *IO);
170
171 eNextState nwc_connect_ip(AsyncIO *IO);
172
173 eNextState NWC_SendQUIT(AsyncNetworker *NW);
174 eNextState NWC_DispatchWriteDone(AsyncIO *IO);
175
176 void DeleteNetworker(void *vptr)
177 {
178         AsyncNetworker *NW = (AsyncNetworker *)vptr;
179         FreeStrBuf(&NW->SpoolFileName);
180         FreeStrBuf(&NW->tempFileName);
181         FreeStrBuf(&NW->node);
182         FreeStrBuf(&NW->host);
183         FreeStrBuf(&NW->port);
184         FreeStrBuf(&NW->secret);
185         FreeStrBuf(&NW->Url);
186         FreeStrBuf(&NW->IO.ErrMsg);
187         FreeAsyncIOContents(&NW->IO);
188         if (NW->HostLookup.VParsedDNSReply != NULL) {
189                 NW->HostLookup.DNSReplyFree(NW->HostLookup.VParsedDNSReply);
190                 NW->HostLookup.VParsedDNSReply = NULL;
191         }
192         free(NW);
193 }
194
195 #define NWC_DBG_SEND() EVN_syslog(LOG_DEBUG, ": > %s", ChrPtr(NW->IO.SendBuf.Buf))
196 #define NWC_DBG_READ() EVN_syslog(LOG_DEBUG, ": < %s\n", ChrPtr(NW->IO.IOBuf))
197 #define NWC_OK (strncasecmp(ChrPtr(NW->IO.IOBuf), "+OK", 3) == 0)
198
199 eNextState SendFailureMessage(AsyncIO *IO)
200 {
201         AsyncNetworker *NW = IO->Data;
202         long lens[2];
203         const char *strs[2];
204
205         strs[0] = ChrPtr(NW->node);
206         lens[0] = StrLength(NW->node);
207         
208         strs[1] = ChrPtr(NW->IO.ErrMsg);
209         lens[1] = StrLength(NW->IO.ErrMsg);
210         CtdlAideFPMessage(
211                 ChrPtr(NW->IO.ErrMsg),
212                 "Networker error",
213                 2, strs, (long*) &lens);
214         
215         return eAbort;
216 }
217
218 eNextState FinalizeNetworker(AsyncIO *IO)
219 {
220         AsyncNetworker *NW = (AsyncNetworker *)IO->Data;
221
222         CtdlNetworkTalkingTo(SKEY(NW->node), NTT_REMOVE);
223
224         DeleteNetworker(IO->Data);
225         return eAbort;
226 }
227
228 eNextState NWC_ReadGreeting(AsyncNetworker *NW)
229 {
230         char connected_to[SIZ];
231         AsyncIO *IO = &NW->IO;
232         SetNWCState(IO, eNWCVSGreating);
233         NWC_DBG_READ();
234         /* Read the server greeting */
235         /* Check that the remote is who we think it is and warn the Aide if not */
236         extract_token (connected_to, ChrPtr(NW->IO.IOBuf), 1, ' ', sizeof connected_to);
237         if (strcmp(connected_to, ChrPtr(NW->node)) != 0)
238         {
239                 if (NW->IO.ErrMsg == NULL)
240                         NW->IO.ErrMsg = NewStrBuf();
241                 StrBufPrintf(NW->IO.ErrMsg,
242                              "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
243                              connected_to, ChrPtr(NW->node));
244                 EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
245                 StopClientWatchers(IO, 1);
246                 return QueueDBOperation(IO, SendFailureMessage);
247         }
248         return eSendReply;
249 }
250
251 eNextState NWC_SendAuth(AsyncNetworker *NW)
252 {
253         AsyncIO *IO = &NW->IO;
254         SetNWCState(IO, eNWCVSAuth);
255         /* We're talking to the correct node.  Now identify ourselves. */
256         StrBufPrintf(NW->IO.SendBuf.Buf, "NETP %s|%s\n", 
257                      config.c_nodename, 
258                      ChrPtr(NW->secret));
259         NWC_DBG_SEND();
260         return eSendReply;
261 }
262
263 eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
264 {
265         AsyncIO *IO = &NW->IO;
266         NWC_DBG_READ();
267         if (ChrPtr(NW->IO.IOBuf)[0] == '2')
268         {
269                 return eSendReply;
270         }
271         else
272         {
273                 int Error = atol(ChrPtr(NW->IO.IOBuf));
274                 if (NW->IO.ErrMsg == NULL)
275                         NW->IO.ErrMsg = NewStrBuf();
276                 StrBufPrintf(NW->IO.ErrMsg,
277                              "Connected to node \"%s\" but my secret wasn't accurate.\nReason was:%s\n",
278                              ChrPtr(NW->node), ChrPtr(NW->IO.IOBuf) + 4);
279                 if (Error == 552) {
280                         SetNWCState(IO, eNWCVSAuthFailNTT);
281                         EVN_syslog(LOG_INFO,
282                                    "Already talking to %s; skipping this time.\n",
283                                    ChrPtr(NW->node));
284                         
285                 }
286                 else {
287                         SetNWCState(IO, eNWCVSAuthFailNTT);
288                         EVN_syslog(LOG_ERR, "%s\n", ChrPtr(NW->IO.ErrMsg));
289                         StopClientWatchers(IO, 1);
290                         return QueueDBOperation(IO, SendFailureMessage);
291                 }
292                 return eAbort;
293         }
294 }
295
296 eNextState NWC_SendNDOP(AsyncNetworker *NW)
297 {
298         AsyncIO *IO = &NW->IO;
299         SetNWCState(IO, eNWCVSNDOP);
300         NW->tempFileName = NewStrBuf();
301         NW->SpoolFileName = NewStrBuf();
302         StrBufPrintf(NW->SpoolFileName,
303                      "%s/%s.%lx%x",
304                      ctdl_netin_dir,
305                      ChrPtr(NW->node),
306                      time(NULL),// TODO: get time from libev
307                      rand());
308         StrBufStripSlashes(NW->SpoolFileName, 1);
309         StrBufPrintf(NW->tempFileName, 
310                      "%s/%s.%lx%x",
311                      ctdl_nettmp_dir,
312                      ChrPtr(NW->node),
313                      time(NULL),// TODO: get time from libev
314                      rand());
315         StrBufStripSlashes(NW->tempFileName, 1);
316         /* We're talking to the correct node.  Now identify ourselves. */
317         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NDOP\n"));
318         NWC_DBG_SEND();
319         return eSendReply;
320 }
321
322 eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
323 {
324         AsyncIO *IO = &NW->IO;
325         int TotalSendSize;
326         NWC_DBG_READ();
327         if (ChrPtr(NW->IO.IOBuf)[0] == '2')
328         {
329
330                 NW->IO.IOB.TotalSentAlready = 0;
331                 TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
332                 EVN_syslog(LOG_DEBUG, "Expecting to transfer %d bytes\n", TotalSendSize);
333                 if (TotalSendSize <= 0) {
334                         NW->State = eNUOP - 1;
335                 }
336                 else {
337                         int fd;
338                         fd = open(ChrPtr(NW->tempFileName), 
339                                   O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY, 
340                                   S_IRUSR|S_IWUSR);
341                         if (fd < 0)
342                         {
343                                 SetNWCState(IO, eNWCVSFail);
344                                 EVN_syslog(LOG_CRIT,
345                                        "cannot open %s: %s\n", 
346                                        ChrPtr(NW->tempFileName), 
347                                        strerror(errno));
348
349                                 NW->State = eQUIT - 1;
350                                 return eAbort;
351                         }
352                         FDIOBufferInit(&NW->IO.IOB, &NW->IO.RecvBuf, fd, TotalSendSize);
353                 }
354                 return eSendReply;
355         }
356         else
357         {
358                 SetNWCState(IO, eNWCVSFail);
359                 return eAbort;
360         }
361 }
362
363 eNextState NWC_SendREAD(AsyncNetworker *NW)
364 {
365         AsyncIO *IO = &NW->IO;
366         eNextState rc;
367
368         if (NW->IO.IOB.TotalSentAlready < NW->IO.IOB.TotalSendSize)
369         {
370                 /*
371                  * If shutting down we can exit here and unlink the temp file.
372                  * this shouldn't loose us any messages.
373                  */
374                 if (server_shutting_down)
375                 {
376                         FDIOBufferDelete(&NW->IO.IOB);
377                         unlink(ChrPtr(NW->tempFileName));
378                         FDIOBufferDelete(&IO->IOB);
379                         SetNWCState(IO, eNWCVSFail);
380                         return eAbort;
381                 }
382                 StrBufPrintf(NW->IO.SendBuf.Buf, "READ %ld|%ld\n",
383                              NW->IO.IOB.TotalSentAlready,
384                              NW->IO.IOB.TotalSendSize);
385 /*
386                              ((NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready > IGNET_PACKET_SIZE)
387                               ? IGNET_PACKET_SIZE : 
388                               (NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready))
389                         );
390 */
391                 NWC_DBG_SEND();
392                 return eSendReply;
393         }
394         else 
395         {
396                 NW->State = eCLOS;
397                 rc = NWC_DispatchWriteDone(&NW->IO);
398                 NWC_DBG_SEND();
399
400                 return rc;
401         }
402 }
403
404 eNextState NWC_ReadREADState(AsyncNetworker *NW)
405 {
406         AsyncIO *IO = &NW->IO;
407         NWC_DBG_READ();
408         if (ChrPtr(NW->IO.IOBuf)[0] == '6')
409         {
410                 NW->IO.IOB.ChunkSendRemain = 
411                         NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
412                 return eReadFile;
413         }
414         FDIOBufferDelete(&IO->IOB);
415         return eAbort;
416 }
417 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
418 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
419 {
420         eNextState rc;
421         AsyncIO *IO = &NW->IO;
422         NWC_DBG_READ();
423         if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
424         {
425                 NW->State ++;
426
427                 FDIOBufferDelete(&NW->IO.IOB);
428
429                 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
430                         EVN_syslog(LOG_ALERT, 
431                                "Could not link %s to %s: %s\n",
432                                ChrPtr(NW->tempFileName), 
433                                ChrPtr(NW->SpoolFileName), 
434                                strerror(errno));
435                 }
436         
437                 unlink(ChrPtr(NW->tempFileName));
438                 rc = NWC_DispatchWriteDone(&NW->IO);
439                 NW->State --;
440                 return rc;
441         }
442         else {
443                 NW->State --;
444                 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
445                 return eSendReply; //NWC_DispatchWriteDone(&NW->IO);
446         }
447 }
448
449 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
450 {
451         eNextState rc;
452         AsyncIO *IO = &NW->IO;
453 /* we don't have any data to debug print here. */
454         if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
455         {
456                 NW->State ++;
457
458                 FDIOBufferDelete(&NW->IO.IOB);
459                 if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
460                         EVN_syslog(LOG_ALERT, 
461                                "Could not link %s to %s: %s\n",
462                                ChrPtr(NW->tempFileName), 
463                                ChrPtr(NW->SpoolFileName), 
464                                strerror(errno));
465                 }
466         
467                 unlink(ChrPtr(NW->tempFileName));
468                 rc = NWC_DispatchWriteDone(&NW->IO);
469                 NW->State --;
470                 return rc;
471         }
472         else {
473                 NW->State --;
474                 NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
475                 return NWC_DispatchWriteDone(&NW->IO);
476         }
477 }
478 eNextState NWC_SendCLOS(AsyncNetworker *NW)
479 {
480         AsyncIO *IO = &NW->IO;
481         SetNWCState(IO, eNWCVSNDOPDone);
482         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("CLOS\n"));
483         NWC_DBG_SEND();
484         return eSendReply;
485 }
486
487 eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
488 {
489         AsyncIO *IO = &NW->IO;
490         NWC_DBG_READ();
491         FDIOBufferDelete(&IO->IOB);
492         if (ChrPtr(NW->IO.IOBuf)[0] != '2')
493                 return eTerminateConnection;
494         return eSendReply;
495 }
496
497
498 eNextState NWC_SendNUOP(AsyncNetworker *NW)
499 {
500         AsyncIO *IO = &NW->IO;
501         eNextState rc;
502         long TotalSendSize;
503         struct stat statbuf;
504         int fd;
505
506         SetNWCState(IO, eNWCVSNUOP);
507         StrBufPrintf(NW->SpoolFileName,
508                      "%s/%s",
509                      ctdl_netout_dir,
510                      ChrPtr(NW->node));
511         StrBufStripSlashes(NW->SpoolFileName, 1);
512
513         fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
514         if (fd < 0) {
515                 if (errno != ENOENT) {
516                         EVN_syslog(LOG_CRIT,
517                                "cannot open %s: %s\n", 
518                                ChrPtr(NW->SpoolFileName), 
519                                strerror(errno));
520                 }
521                 NW->State = eQUIT;
522                 rc = NWC_SendQUIT(NW);
523                 NWC_DBG_SEND();
524                 return rc;
525         }
526
527         if (fstat(fd, &statbuf) == -1) {
528                 EVN_syslog(LOG_CRIT, "FSTAT FAILED %s [%s]--\n", 
529                            ChrPtr(NW->SpoolFileName), 
530                            strerror(errno));
531                 if (fd > 0) close(fd);
532                 return eAbort;
533         }
534         TotalSendSize = statbuf.st_size;
535         if (TotalSendSize == 0) {
536                 EVNM_syslog(LOG_DEBUG,
537                        "Nothing to send.\n");
538                 NW->State = eQUIT;
539                 rc = NWC_SendQUIT(NW);
540                 NWC_DBG_SEND();
541                 if (fd > 0) close(fd);
542                 return rc;
543         }
544         FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
545
546         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("NUOP\n"));
547         NWC_DBG_SEND();
548         return eSendReply;
549
550 }
551 eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
552 {
553         AsyncIO *IO = &NW->IO;
554         NWC_DBG_READ();
555         if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
556                 FDIOBufferDelete(&IO->IOB);
557                 return eAbort;
558         }
559         return eSendReply;
560 }
561
562 eNextState NWC_SendWRIT(AsyncNetworker *NW)
563 {
564         AsyncIO *IO = &NW->IO;
565         StrBufPrintf(NW->IO.SendBuf.Buf, "WRIT %ld\n", 
566                      NW->IO.IOB.TotalSendSize - NW->IO.IOB.TotalSentAlready);
567         NWC_DBG_SEND();
568         return eSendReply;
569 }
570 eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
571 {
572         AsyncIO *IO = &NW->IO;
573         NWC_DBG_READ();
574         if (ChrPtr(NW->IO.IOBuf)[0] != '7')
575         {
576                 FDIOBufferDelete(&IO->IOB);
577                 return eAbort;
578         }
579
580         NW->IO.IOB.ChunkSendRemain = 
581                 NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
582         return eSendFile;
583 }
584
585 eNextState NWC_SendBlobDone(AsyncNetworker *NW)
586 {
587         AsyncIO *IO = &NW->IO;
588         eNextState rc;
589         if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
590         {
591                 NW->State ++;
592
593                 FDIOBufferDelete(&IO->IOB);
594                 rc =  NWC_DispatchWriteDone(IO);
595                 NW->State --;
596                 return rc;
597         }
598         else {
599                 NW->State --;
600                 IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
601                 rc = NWC_DispatchWriteDone(IO);
602                 NW->State --;
603                 return rc;
604         }
605 }
606
607 eNextState NWC_SendUCLS(AsyncNetworker *NW)
608 {
609         AsyncIO *IO = &NW->IO;
610         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("UCLS 1\n"));
611         NWC_DBG_SEND();
612         return eSendReply;
613
614 }
615 eNextState NWC_ReadUCLS(AsyncNetworker *NW)
616 {
617         AsyncIO *IO = &NW->IO;
618         NWC_DBG_READ();
619
620         EVN_syslog(LOG_NOTICE, "Sent %ld octets to <%s>\n", NW->IO.IOB.ChunkSize, ChrPtr(NW->node));
621         if (ChrPtr(NW->IO.IOBuf)[0] == '2') {
622                 EVN_syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
623                 unlink(ChrPtr(NW->SpoolFileName));
624         }
625         FDIOBufferDelete(&IO->IOB);
626         SetNWCState(IO, eNWCVSNUOPDone);
627         return eSendReply;
628 }
629
630 eNextState NWC_SendQUIT(AsyncNetworker *NW)
631 {
632         AsyncIO *IO = &NW->IO;
633         StrBufPlain(NW->IO.SendBuf.Buf, HKEY("QUIT\n"));
634
635         NWC_DBG_SEND();
636         return eSendReply;
637 }
638
639 eNextState NWC_ReadQUIT(AsyncNetworker *NW)
640 {
641         AsyncIO *IO = &NW->IO;
642         NWC_DBG_READ();
643
644         return eAbort;
645 }
646
647
648 NWClientHandler NWC_ReadHandlers[] = {
649         NWC_ReadGreeting,
650         NWC_ReadAuthReply,
651         NWC_ReadNDOPReply,
652         NWC_ReadREADState,
653         NWC_ReadREADBlob,
654         NWC_ReadCLOSReply,
655         NWC_ReadNUOPReply,
656         NWC_ReadWRITReply,
657         NWC_SendBlobDone,
658         NWC_ReadUCLS,
659         NWC_ReadQUIT};
660
661 long NWC_ConnTimeout = 100;
662
663 const long NWC_SendTimeouts[] = {
664         100,
665         100,
666         100,
667         100,
668         100,
669         100,
670         100,
671         100
672 };
673 const ConstStr NWC[] = {
674         {HKEY("Connection broken during ")},
675         {HKEY("Connection broken during ")},
676         {HKEY("Connection broken during ")},
677         {HKEY("Connection broken during ")},
678         {HKEY("Connection broken during ")},
679         {HKEY("Connection broken during ")},
680         {HKEY("Connection broken during ")},
681         {HKEY("Connection broken during ")}
682 };
683
684 NWClientHandler NWC_SendHandlers[] = {
685         NULL,
686         NWC_SendAuth,
687         NWC_SendNDOP,
688         NWC_SendREAD,
689         NWC_ReadREADBlobDone,
690         NWC_SendCLOS,
691         NWC_SendNUOP,
692         NWC_SendWRIT,
693         NWC_SendBlobDone,
694         NWC_SendUCLS,
695         NWC_SendQUIT
696 };
697
698 const long NWC_ReadTimeouts[] = {
699         100,
700         100,
701         100,
702         100,
703         100,
704         100,
705         100,
706         100,
707         100,
708         100
709 };
710
711
712
713
714 eNextState nwc_get_one_host_ip_done(AsyncIO *IO)
715 {
716         AsyncNetworker *NW = IO->Data;
717         struct hostent *hostent;
718
719         QueryCbDone(IO);
720
721         hostent = NW->HostLookup.VParsedDNSReply;
722         if ((NW->HostLookup.DNSStatus == ARES_SUCCESS) && 
723             (hostent != NULL) ) {
724                 memset(&NW->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
725                 if (NW->IO.ConnectMe->IPv6) {
726                         memcpy(&NW->IO.ConnectMe->Addr.sin6_addr.s6_addr, 
727                                &hostent->h_addr_list[0],
728                                sizeof(struct in6_addr));
729                         
730                         NW->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
731                         NW->IO.ConnectMe->Addr.sin6_port   = htons(atol(ChrPtr(NW->port)));//// TODO use the one from the URL.
732                 }
733                 else {
734                         struct sockaddr_in *addr = (struct sockaddr_in*) &NW->IO.ConnectMe->Addr;
735                         /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
736 //                      addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
737                         memcpy(&addr->sin_addr.s_addr, 
738                                hostent->h_addr_list[0], 
739                                sizeof(uint32_t));
740                         
741                         addr->sin_family = hostent->h_addrtype;
742                         addr->sin_port   = htons(504);/// default citadel port
743                         
744                 }
745                 return nwc_connect_ip(IO);
746         }
747         else
748                 return eAbort;
749 }
750
751
752 eNextState nwc_get_one_host_ip(AsyncIO *IO)
753 {
754         AsyncNetworker *NW = IO->Data;
755         /* 
756          * here we start with the lookup of one host.
757          */ 
758
759         EVN_syslog(LOG_DEBUG, "NWC: %s\n", __FUNCTION__);
760
761         EVN_syslog(LOG_DEBUG, 
762                    "NWC client[%ld]: looking up %s-Record %s : %d ...\n", 
763                    NW->n, 
764                    (NW->IO.ConnectMe->IPv6)? "aaaa": "a",
765                    NW->IO.ConnectMe->Host, 
766                    NW->IO.ConnectMe->Port);
767
768         QueueQuery((NW->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a, 
769                    NW->IO.ConnectMe->Host, 
770                    &NW->IO, 
771                    &NW->HostLookup, 
772                    nwc_get_one_host_ip_done);
773         IO->NextState = eReadDNSReply;
774         return IO->NextState;
775 }
776 /**
777  * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
778  */
779 eReadState NWC_ReadServerStatus(AsyncIO *IO)
780 {
781 //      AsyncNetworker *NW = IO->Data;
782         eReadState Finished = eBufferNotEmpty; 
783
784         switch (IO->NextState) {
785         case eSendDNSQuery:
786         case eReadDNSReply:
787         case eDBQuery:
788         case eConnect:
789         case eTerminateConnection:
790         case eAbort:
791                 Finished = eReadFail;
792                 break;
793         case eSendReply: 
794         case eSendMore:
795         case eReadMore:
796         case eReadMessage: 
797                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
798                 break;
799         case eReadFile:
800         case eSendFile:
801         case eReadPayload:
802                 break;
803         }
804         return Finished;
805 }
806
807
808
809 eNextState NWC_FailNetworkConnection(AsyncIO *IO)
810 {
811         SetNWCState(IO, eNWCVSConnFail);
812         StopClientWatchers(IO, 1);
813         return QueueDBOperation(IO, SendFailureMessage);
814 }
815
816 void NWC_SetTimeout(eNextState NextTCPState, AsyncNetworker *NW)
817 {
818         AsyncIO *IO = &NW->IO;
819         double Timeout = 0.0;
820
821         EVN_syslog(LOG_DEBUG, "%s - %d\n", __FUNCTION__, NextTCPState);
822
823         switch (NextTCPState) {
824         case eSendReply:
825         case eSendMore:
826                 break;
827         case eReadFile:
828         case eReadMessage:
829                 Timeout = NWC_ReadTimeouts[NW->State];
830                 break;
831         case eReadPayload:
832                 Timeout = 100000;
833                 /* TODO!!! */
834                 break;
835         case eSendDNSQuery:
836         case eReadDNSReply:
837         case eConnect:
838         case eSendFile:
839 //TODO
840         case eTerminateConnection:
841         case eDBQuery:
842         case eAbort:
843         case eReadMore://// TODO
844                 return;
845         }
846         if (Timeout > 0) {
847                 EVN_syslog(LOG_DEBUG, 
848                            "%s - %d %f\n",
849                            __FUNCTION__,
850                            NextTCPState,
851                            Timeout);
852                 SetNextTimeout(&NW->IO, Timeout*100);
853         }
854 }
855
856
857 eNextState NWC_DispatchReadDone(AsyncIO *IO)
858 {
859         EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
860         AsyncNetworker *NW = IO->Data;
861         eNextState rc;
862
863         rc = NWC_ReadHandlers[NW->State](NW);
864         if (rc != eReadMore)
865                 NW->State++;
866         NWC_SetTimeout(rc, NW);
867         return rc;
868 }
869 eNextState NWC_DispatchWriteDone(AsyncIO *IO)
870 {
871         EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
872         AsyncNetworker *NW = IO->Data;
873         eNextState rc;
874
875         rc = NWC_SendHandlers[NW->State](NW);
876         NWC_SetTimeout(rc, NW);
877         return rc;
878 }
879
880 /*****************************************************************************/
881 /*                     Networker CLIENT ERROR CATCHERS                       */
882 /*****************************************************************************/
883 eNextState NWC_Terminate(AsyncIO *IO)
884 {
885         EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
886         FinalizeNetworker(IO);
887         return eAbort;
888 }
889
890 eNextState NWC_TerminateDB(AsyncIO *IO)
891 {
892         EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
893         FinalizeNetworker(IO);
894         return eAbort;
895 }
896
897 eNextState NWC_Timeout(AsyncIO *IO)
898 {
899         AsyncNetworker *NW = IO->Data;
900         EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
901
902         if (NW->IO.ErrMsg == NULL)
903                 NW->IO.ErrMsg = NewStrBuf();
904         StrBufPrintf(NW->IO.ErrMsg, "Timeout while talking to %s \r\n", ChrPtr(NW->host));
905         return NWC_FailNetworkConnection(IO);
906 }
907 eNextState NWC_ConnFail(AsyncIO *IO)
908 {
909         AsyncNetworker *NW = IO->Data;
910
911         EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
912         if (NW->IO.ErrMsg == NULL)
913                 NW->IO.ErrMsg = NewStrBuf();
914         StrBufPrintf(NW->IO.ErrMsg, "failed to connect %s \r\n", ChrPtr(NW->host));
915
916         return NWC_FailNetworkConnection(IO);
917 }
918 eNextState NWC_DNSFail(AsyncIO *IO)
919 {
920         AsyncNetworker *NW = IO->Data;
921
922         EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
923         if (NW->IO.ErrMsg == NULL)
924                 NW->IO.ErrMsg = NewStrBuf();
925         StrBufPrintf(NW->IO.ErrMsg, "failed to look up %s \r\n", ChrPtr(NW->host));
926
927         return NWC_FailNetworkConnection(IO);
928 }
929 eNextState NWC_Shutdown(AsyncIO *IO)
930 {
931         EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
932
933         FinalizeNetworker(IO);
934         return eAbort;
935 }
936
937
938 eNextState nwc_connect_ip(AsyncIO *IO)
939 {
940         AsyncNetworker *NW = IO->Data;
941
942         SetNWCState(&NW->IO, eNWCVSConnecting);
943         EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
944         EVN_syslog(LOG_NOTICE, "Connecting to <%s> at %s:%s\n", 
945                    ChrPtr(NW->node), 
946                    ChrPtr(NW->host),
947                    ChrPtr(NW->port));
948         
949         return EvConnectSock(IO,
950                              NWC_ConnTimeout,
951                              NWC_ReadTimeouts[0],
952                              1);
953 }
954
955 static int NetworkerCount = 0;
956 void RunNetworker(AsyncNetworker *NW)
957 {
958         NW->n = NetworkerCount++;
959         CtdlNetworkTalkingTo(SKEY(NW->node), NTT_ADD);
960         syslog(LOG_DEBUG, "NW[%s][%ld]: polling\n", ChrPtr(NW->node), NW->n);
961         ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
962
963         InitIOStruct(&NW->IO,
964                      NW,
965                      eReadMessage,
966                      NWC_ReadServerStatus,
967                      NWC_DNSFail,
968                      NWC_DispatchWriteDone,
969                      NWC_DispatchReadDone,
970                      NWC_Terminate,
971                      NWC_TerminateDB,
972                      NWC_ConnFail,
973                      NWC_Timeout,
974                      NWC_Shutdown);
975
976         safestrncpy(((CitContext *)NW->IO.CitContext)->cs_host, 
977                     ChrPtr(NW->host),
978                     sizeof(((CitContext *)NW->IO.CitContext)->cs_host)); 
979
980         if (NW->IO.ConnectMe->IsIP) {
981                 SetNWCState(&NW->IO, eNWCVSLookup);
982                 QueueEventContext(&NW->IO,
983                                   nwc_connect_ip);
984         }
985         else { /* uneducated admin has chosen to add DNS to the equation... */
986                 SetNWCState(&NW->IO, eNWCVSConnecting);
987                 QueueEventContext(&NW->IO,
988                                   nwc_get_one_host_ip);
989         }
990 }
991
992 /*
993  * Poll other Citadel nodes and transfer inbound/outbound network data.
994  * Set "full" to nonzero to force a poll of every node, or to zero to poll
995  * only nodes to which we have data to send.
996  */
997 void network_poll_other_citadel_nodes(int full_poll, HashList *ignetcfg)
998 {
999         const char *key;
1000         long len;
1001         HashPos *Pos;
1002         void *vCfg;
1003         AsyncNetworker *NW;
1004         StrBuf *SpoolFileName;
1005         
1006         int poll = 0;
1007         
1008         if (GetCount(ignetcfg) ==0) {
1009                 syslog(LOG_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
1010                 return;
1011         }
1012         become_session(&networker_client_CC);
1013
1014         SpoolFileName = NewStrBufPlain(ctdl_netout_dir, -1);
1015
1016         Pos = GetNewHashPos(ignetcfg, 0);
1017
1018         while (GetNextHashPos(ignetcfg, Pos, &len, &key, &vCfg))
1019         {
1020                 /* Use the string tokenizer to grab one line at a time */
1021                 if(server_shutting_down)
1022                         return;/* TODO free stuff*/
1023                 CtdlNodeConf *pNode = (CtdlNodeConf*) vCfg;
1024                 poll = 0;
1025                 NW = (AsyncNetworker*)malloc(sizeof(AsyncNetworker));
1026                 memset(NW, 0, sizeof(AsyncNetworker));
1027                 
1028                 NW->node = NewStrBufDup(pNode->NodeName);
1029                 NW->host = NewStrBufDup(pNode->Host);
1030                 NW->port = NewStrBufDup(pNode->Port);
1031                 NW->secret = NewStrBufDup(pNode->Secret);
1032                 
1033                 if ( (StrLength(NW->node) != 0) && 
1034                      (StrLength(NW->secret) != 0) &&
1035                      (StrLength(NW->host) != 0) &&
1036                      (StrLength(NW->port) != 0))
1037                 {
1038                         poll = full_poll;
1039                         if (poll == 0)
1040                         {
1041                                 StrBufAppendBufPlain(SpoolFileName, HKEY("/"), 0);
1042                                 StrBufAppendBuf(SpoolFileName, NW->node, 0);
1043                                 StrBufStripSlashes(SpoolFileName, 1);
1044                                 
1045                                 if (access(ChrPtr(SpoolFileName), R_OK) == 0) {
1046                                         poll = 1;
1047                                 }
1048                         }
1049                 }
1050                 if (poll && 
1051                     (StrLength(NW->host) > 0) && 
1052                     strcmp("0.0.0.0", ChrPtr(NW->host)))
1053                 {
1054                         NW->Url = NewStrBuf();
1055                         StrBufPrintf(NW->Url, "citadel://%s@%s:%s", 
1056                                      ChrPtr(NW->secret),
1057                                      ChrPtr(NW->host),
1058                                      ChrPtr(NW->port));
1059                         if (!CtdlNetworkTalkingTo(SKEY(NW->node), NTT_CHECK))
1060                         {
1061                                 RunNetworker(NW);
1062                                 continue;
1063                         }
1064                 }
1065                 DeleteNetworker(NW);
1066         }
1067         FreeStrBuf(&SpoolFileName);
1068         DeleteHashPos(&Pos);
1069 }
1070
1071
1072 void network_do_clientqueue(void)
1073 {
1074         HashList *working_ignetcfg;
1075         int full_processing = 1;
1076         static time_t last_run = 0L;
1077
1078         /*
1079          * Run the full set of processing tasks no more frequently
1080          * than once every n seconds
1081          */
1082         if ( (time(NULL) - last_run) < config.c_net_freq ) {
1083                 full_processing = 0;
1084                 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
1085                         config.c_net_freq - (time(NULL)- last_run)
1086                 );
1087         }
1088
1089         working_ignetcfg = CtdlLoadIgNetCfg();
1090         /*
1091          * Poll other Citadel nodes.  Maybe.  If "full_processing" is set
1092          * then we poll everyone.  Otherwise we only poll nodes we have stuff
1093          * to send to.
1094          */
1095         network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
1096         DeleteHash(&working_ignetcfg);
1097 }
1098
1099 void LogDebugEnableNetworkClient(const int n)
1100 {
1101         NetworkClientDebugEnabled = n;
1102 }
1103 /*
1104  * Module entry point
1105  */
1106 CTDL_MODULE_INIT(network_client)
1107 {
1108         if (!threading)
1109         {
1110                 CtdlFillSystemContext(&networker_client_CC, "CitNetworker");
1111                 
1112                 CtdlRegisterSessionHook(network_do_clientqueue, EVT_TIMER, PRIO_SEND + 10);
1113                 CtdlRegisterDebugFlagHook(HKEY("networkclient"), LogDebugEnableNetworkClient, &NetworkClientDebugEnabled);
1114
1115         }
1116         return "networkclient";
1117 }