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