Networker:
[citadel.git] / citadel / modules / network / serv_netspool.c
index 9538a6dbd103cb8e3d3f79d4ddee221b393e6909..de98a3cf1ff68193193cd79f507f4a806ceb5a8a 100644 (file)
 
 #include "context.h"
 #include "netconfig.h"
+#include "netspool.h"
 #include "netmail.h"
 #include "ctdl_module.h"
 
 /*
  * Learn topology from path fields
  */
-void network_learn_topology(char *node, char *path) {
+static void network_learn_topology(char *node, char *path, NetMap **the_netmap, int *netmap_changed)
+{
        char nexthop[256];
        NetMap *nmptr;
 
-       strcpy(nexthop, "");
+       *nexthop = '\0';
 
        if (num_tokens(path, '!') < 3) return;
-       for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
+       for (nmptr = *the_netmap; nmptr != NULL; nmptr = nmptr->next) {
                if (!strcasecmp(nmptr->nodename, node)) {
                        extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
                        nmptr->lastcontact = time(NULL);
-                       ++netmap_changed;
+                       (*netmap_changed) ++;
                        return;
                }
        }
@@ -115,9 +117,9 @@ void network_learn_topology(char *node, char *path) {
        strcpy(nmptr->nodename, node);
        nmptr->lastcontact = time(NULL);
        extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
-       nmptr->next = the_netmap;
-       the_netmap = nmptr;
-       ++netmap_changed;
+       nmptr->next = *the_netmap;
+       *the_netmap = nmptr;
+       (*netmap_changed) ++;
 }
 
 
@@ -379,7 +381,10 @@ int is_recipient(SpoolControl *sc, const char *Name)
 /*
  * Batch up and send all outbound traffic from the current room
  */
-void network_spoolout_room(char *room_to_spool) {
+void network_spoolout_room(RoomProcList *room_to_spool,                       
+                          char *working_ignetcfg,
+                          NetMap *the_netmap)
+{
        char buf[SIZ];
        char filename[PATH_MAX];
        SpoolControl *sc;
@@ -390,8 +395,8 @@ void network_spoolout_room(char *room_to_spool) {
         * Normally this should never happen, but once in a while maybe a room gets
         * queued for networking and then deleted before it can happen.
         */
-       if (CtdlGetRoom(&CC->room, room_to_spool) != 0) {
-               syslog(LOG_CRIT, "ERROR: cannot load <%s>\n", room_to_spool);
+       if (CtdlGetRoom(&CC->room, room_to_spool->name) != 0) {
+               syslog(LOG_CRIT, "ERROR: cannot load <%s>\n", room_to_spool->name);
                return;
        }
 
@@ -406,6 +411,9 @@ void network_spoolout_room(char *room_to_spool) {
        }
        syslog(LOG_INFO, "Networking started for <%s>\n", CC->room.QRname);
 
+       sc->working_ignetcfg = working_ignetcfg;
+       sc->the_netmap = the_netmap;
+
        /* If there are digest recipients, we have to build a digest */
        if (sc->digestrecps != NULL) {
                sc->digestfp = tmpfile();
@@ -444,7 +452,8 @@ void network_spoolout_room(char *room_to_spool) {
  * Process a buffer containing a single message from a single file
  * from the inbound queue 
  */
-void network_process_buffer(char *buffer, long size) {
+void network_process_buffer(char *buffer, long size, char *working_ignetcfg, NetMap **the_netmap, int *netmap_changed)
+{
        struct CtdlMessage *msg = NULL;
        long pos;
        int field;
@@ -491,7 +500,12 @@ void network_process_buffer(char *buffer, long size) {
 
                        /* route the message */
                        strcpy(nexthop, "");
-                       if (is_valid_node(nexthop, NULL, msg->cm_fields['D']) == 0) {
+                       if (is_valid_node(nexthop, 
+                                         NULL, 
+                                         msg->cm_fields['D'], 
+                                         working_ignetcfg, 
+                                         *the_netmap) == 0) 
+                       {
                                /* prepend our node to the path */
                                if (msg->cm_fields['P'] != NULL) {
                                        oldpath = msg->cm_fields['P'];
@@ -560,7 +574,10 @@ void network_process_buffer(char *buffer, long size) {
 
        /* Learn network topology from the path */
        if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
-               network_learn_topology(msg->cm_fields['N'], msg->cm_fields['P']);
+               network_learn_topology(msg->cm_fields['N'], 
+                                      msg->cm_fields['P'], 
+                                      the_netmap, 
+                                      netmap_changed);
        }
 
        /* Is the sending node giving us a very persuasive suggestion about
@@ -615,7 +632,13 @@ void network_process_buffer(char *buffer, long size) {
 /*
  * Process a single message from a single file from the inbound queue 
  */
-void network_process_message(FILE *fp, long msgstart, long msgend) {
+void network_process_message(FILE *fp, 
+                            long msgstart, 
+                            long msgend,
+                            char *working_ignetcfg,
+                            NetMap **the_netmap, 
+                            int *netmap_changed)
+{
        long hold_pos;
        long size;
        char *buffer;
@@ -626,7 +649,11 @@ void network_process_message(FILE *fp, long msgstart, long msgend) {
        if (buffer != NULL) {
                fseek(fp, msgstart, SEEK_SET);
                if (fread(buffer, size, 1, fp) > 0) {
-                       network_process_buffer(buffer, size);
+                       network_process_buffer(buffer, 
+                                              size, 
+                                              working_ignetcfg, 
+                                              the_netmap, 
+                                              netmap_changed);
                }
                free(buffer);
        }
@@ -638,7 +665,11 @@ void network_process_message(FILE *fp, long msgstart, long msgend) {
 /*
  * Process a single file from the inbound queue 
  */
-void network_process_file(char *filename) {
+void network_process_file(char *filename,
+                         char *working_ignetcfg,
+                         NetMap **the_netmap, 
+                         int *netmap_changed)
+{
        FILE *fp;
        long msgstart = (-1L);
        long msgend = (-1L);
@@ -662,7 +693,12 @@ void network_process_file(char *filename) {
                if (ch == 255) {
                        if (msgstart >= 0L) {
                                msgend = msgcur - 1;
-                               network_process_message(fp, msgstart, msgend);
+                               network_process_message(fp,
+                                                       msgstart,
+                                                       msgend,
+                                                       working_ignetcfg,
+                                                       the_netmap,
+                                                       netmap_changed);
                        }
                        msgstart = msgcur;
                }
@@ -672,7 +708,12 @@ void network_process_file(char *filename) {
 
        msgend = msgcur - 1;
        if (msgstart >= 0L) {
-               network_process_message(fp, msgstart, msgend);
+               network_process_message(fp,
+                                       msgstart,
+                                       msgend,
+                                       working_ignetcfg,
+                                       the_netmap,
+                                       netmap_changed);
        }
 
        fclose(fp);
@@ -683,7 +724,8 @@ void network_process_file(char *filename) {
 /*
  * Process anything in the inbound queue
  */
-void network_do_spoolin(void) {
+void network_do_spoolin(char *working_ignetcfg, NetMap **the_netmap, int *netmap_changed)
+{
        DIR *dp;
        struct dirent *d;
        struct stat statbuf;
@@ -716,7 +758,10 @@ void network_do_spoolin(void) {
                                ctdl_netin_dir,
                                d->d_name
                        );
-                       network_process_file(filename);
+                       network_process_file(filename,
+                                            working_ignetcfg,
+                                            the_netmap,
+                                            netmap_changed);
                }
        }
 
@@ -727,12 +772,14 @@ void network_do_spoolin(void) {
  * Step 1: consolidate files in the outbound queue into one file per neighbor node
  * Step 2: delete any files in the outbound queue that were for neighbors who no longer exist.
  */
-void network_consolidate_spoolout(void) {
+void network_consolidate_spoolout(char *working_ignetcfg, NetMap *the_netmap)
+{
        DIR *dp;
        struct dirent *d;
        char filename[PATH_MAX];
        char cmd[PATH_MAX];
        char nexthop[256];
+       long nexthoplen;
        int i;
        char *ptr;
 
@@ -745,9 +792,12 @@ void network_consolidate_spoolout(void) {
                        && (strcmp(d->d_name, ".."))
                        && (strchr(d->d_name, '@') != NULL)
                ) {
-                       safestrncpy(nexthop, d->d_name, sizeof nexthop);
+                       nexthoplen = safestrncpy(nexthop, d->d_name, sizeof nexthop);
                        ptr = strchr(nexthop, '@');
-                       if (ptr) *ptr = 0;
+                       if (ptr) {
+                               *ptr = 0;
+                               nexthoplen = ptr - nexthop;
+                       }                               
        
                        snprintf(filename, 
                                sizeof filename,
@@ -757,21 +807,21 @@ void network_consolidate_spoolout(void) {
                        );
        
                        syslog(LOG_DEBUG, "Consolidate %s to %s\n", filename, nexthop);
-                       if (network_talking_to(nexthop, NTT_CHECK)) {
+                       if (network_talking_to(nexthop, nexthoplen, NTT_CHECK)) {
                                syslog(LOG_DEBUG,
                                        "Currently online with %s - skipping for now\n",
                                        nexthop
                                );
                        }
                        else {
-                               network_talking_to(nexthop, NTT_ADD);
+                               network_talking_to(nexthop, nexthoplen, NTT_ADD);
                                snprintf(cmd, sizeof cmd, "/bin/cat %s >>%s/%s && /bin/rm -f %s",
                                        filename,
                                        ctdl_netout_dir, nexthop,
                                        filename
                                );
                                system(cmd);
-                               network_talking_to(nexthop, NTT_REMOVE);
+                               network_talking_to(nexthop, nexthoplen, NTT_REMOVE);
                        }
                }
        }
@@ -785,9 +835,7 @@ void network_consolidate_spoolout(void) {
        while (d = readdir(dp), d != NULL) {
                if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
                        continue;
-               ptr = strchr(d->d_name, '@');
-               if (d != NULL)
-                       continue;
+
                snprintf(filename, 
                        sizeof filename,
                        "%s/%s",
@@ -796,7 +844,11 @@ void network_consolidate_spoolout(void) {
                );
 
                strcpy(nexthop, "");
-               i = is_valid_node(nexthop, NULL, d->d_name);
+               i = is_valid_node(nexthop,
+                                 NULL,
+                                 d->d_name,
+                                 working_ignetcfg,
+                                 the_netmap);
        
                if ( (i != 0) || !IsEmptyStr(nexthop) ) {
                        unlink(filename);
@@ -840,7 +892,7 @@ CTDL_MODULE_INIT(network_spool)
        if (!threading)
        {
                create_spool_dirs();
-               CtdlRegisterCleanupHook(destroy_network_queue_room);
+//////todo             CtdlRegisterCleanupHook(destroy_network_queue_room);
        }
        return "network_spool";
 }