Remove global variables; replace by stack passing.
authorWilfried Goesgens <dothebart@citadel.org>
Thu, 8 Sep 2011 17:18:12 +0000 (17:18 +0000)
committerWilfried Goesgens <dothebart@citadel.org>
Thu, 8 Sep 2011 17:18:12 +0000 (17:18 +0000)
citadel/modules/network/netconfig.h
citadel/modules/network/netspool.h
citadel/modules/network/serv_netconfig.c
citadel/modules/network/serv_netmail.c
citadel/modules/network/serv_netspool.c
citadel/modules/network/serv_network.c
citadel/modules/network/serv_networkclient.c

index 4e1e2643323fa1072906f9bb981224171ea672f3..37e1e0c3e874bb8f48d370c72b5ea9242a99ea70 100644 (file)
@@ -7,12 +7,7 @@ struct  NetMap {
        char nexthop[SIZ];
 };
 
-
-NetMap *the_netmap;
-int netmap_changed;
-char *working_ignetcfg;
-
-void load_working_ignetcfg(void);
-void read_network_map(void);
-void write_network_map(void);
-int is_valid_node(char *nexthop, char *secret, char *node);
+char* load_working_ignetcfg(void);
+NetMap *read_network_map(void);
+void write_network_map(NetMap *the_netmap, int netmap_changed);
+int is_valid_node(char *nexthop, char *secret, char *node, char *working_ignetcfg, NetMap *the_netmap);
index e4c8c9ffffef485b77703b8c2eba2e183ed7a4a9..a998138a830fc8e10e02dea28ceb3aef3d406b65 100644 (file)
@@ -22,8 +22,8 @@ struct SpoolControl {
 
 
 void network_spoolout_room(char *room_to_spool);
-void network_do_spoolin(void);
-void network_consolidate_spoolout(void);
+void network_do_spoolin(char *working_ignetcfg, NetMap *the_netmap, int *netmap_changed);
+void network_consolidate_spoolout(char *working_ignetcfg, NetMap *the_netmap);
 void free_spoolcontrol_struct(SpoolControl **scc);
 int writenfree_spoolcontrol_file(SpoolControl **scc, char *filename);
 int read_spoolcontrol_file(SpoolControl **scc, char *filename);
index 970057133bb4e4c49dedbdf6bb92d453034e7c05..c6acdf55d62aa103f524328cfacc85a4b6468d2a 100644 (file)
 #include "ctdl_module.h"
 
 
-/*
- * We build a map of network nodes during processing.
- */
-NetMap *the_netmap = NULL;
-int netmap_changed = 0;
-char *working_ignetcfg = NULL;
-
 /*
  * Load or refresh the Citadel network (IGnet) configuration for this node.
  */
-void load_working_ignetcfg(void) {
-       char *cfg;
-       char *oldcfg;
-
-       cfg = CtdlGetSysConfig(IGNETCFG);
-       if (cfg == NULL) {
-               cfg = strdup("");
-       }
-
-       oldcfg = working_ignetcfg;
-       working_ignetcfg = cfg;
-       if (oldcfg != NULL) {
-               free(oldcfg);
-       }
+char* load_working_ignetcfg(void) {
+       return CtdlGetSysConfig(IGNETCFG);
 }
 
+
 /* 
  * Read the network map from its configuration file into memory.
  */
-void read_network_map(void) {
+NetMap *read_network_map(void) {
        char *serialized_map = NULL;
        int i;
        char buf[SIZ];
-       NetMap *nmptr;
+       NetMap *nmptr, *the_netmap;
 
+       the_netmap = NULL;
        serialized_map = CtdlGetSysConfig(IGNETMAP);
-       if (serialized_map == NULL) return;     /* if null, no entries */
+       if (serialized_map == NULL) return NULL;        /* if null, no entries */
 
        /* Use the string tokenizer to grab one line at a time */
        for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
@@ -142,14 +125,14 @@ void read_network_map(void) {
        }
 
        free(serialized_map);
-       netmap_changed = 0;
+       return the_netmap;
 }
 
 
 /*
  * Write the network map from memory back to the configuration file.
  */
-void write_network_map(void) {
+void write_network_map(NetMap *the_netmap, int netmap_changed) {
        char *serialized_map = NULL;
        NetMap *nmptr;
 
@@ -186,13 +169,19 @@ void write_network_map(void) {
 }
 
 
+
 /* 
  * Check the network map and determine whether the supplied node name is
  * valid.  If it is not a neighbor node, supply the name of a neighbor node
  * which is the next hop.  If it *is* a neighbor node, we also fill in the
  * shared secret.
  */
-int is_valid_node(char *nexthop, char *secret, char *node) {
+int is_valid_node(char *nexthop, 
+                 char *secret, 
+                 char *node, 
+                 char *working_ignetcfg, 
+                 NetMap *the_netmap)
+{
        int i;
        char linebuf[SIZ];
        char buf[SIZ];
@@ -206,8 +195,8 @@ int is_valid_node(char *nexthop, char *secret, char *node) {
        /*
         * First try the neighbor nodes
         */
-       if (working_ignetcfg == NULL) {
-               syslog(LOG_ERR, "working_ignetcfg is NULL!\n");
+       if ((working_ignetcfg == NULL) || (*working_ignetcfg == '\0')) {
+               syslog(LOG_ERR, "working_ignetcfg is empty!\n");
                if (nexthop != NULL) {
                        strcpy(nexthop, "");
                }
@@ -374,6 +363,7 @@ void cmd_snet(char *argbuf) {
  */
 void cmd_netp(char *cmdbuf)
 {
+       char *working_ignetcfg;
        char node[256];
        char pass[256];
        int v;
@@ -387,8 +377,8 @@ void cmd_netp(char *cmdbuf)
        extract_token(pass, cmdbuf, 1, '|', sizeof pass);
 
        /* load the IGnet Configuration to check node validity */
-       load_working_ignetcfg();
-       v = is_valid_node(nexthop, secret, node);
+       working_ignetcfg = load_working_ignetcfg();
+       v = is_valid_node(nexthop, secret, node, working_ignetcfg, NULL); //// TODO do we need the netmap?
 
        if (v != 0) {
                snprintf(err_buf, sizeof err_buf,
@@ -398,6 +388,7 @@ void cmd_netp(char *cmdbuf)
                syslog(LOG_WARNING, "%s", err_buf);
                cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
                CtdlAideMessage(err_buf, "IGNet Networking.");
+               free(working_ignetcfg);
                return;
        }
 
@@ -409,12 +400,14 @@ void cmd_netp(char *cmdbuf)
                syslog(LOG_WARNING, "%s", err_buf);
                cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
                CtdlAideMessage(err_buf, "IGNet Networking.");
+               free(working_ignetcfg);
                return;
        }
 
        if (network_talking_to(node, NTT_CHECK)) {
                syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
                cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
+               free(working_ignetcfg);
                return;
        }
 
@@ -424,6 +417,7 @@ void cmd_netp(char *cmdbuf)
                CC->net_node, CC->cs_host, CC->cs_addr
        );
        cprintf("%d authenticated as network node '%s'\n", CIT_OK, CC->net_node);
+       free(working_ignetcfg);
 }
 
 int netconfig_check_roomaccess(
index e2b2f2183af4b4f579bcbff9f9ed0046f32b5583..ce94c1fc4a3741b054fc487f061fb22a6afac96b 100644 (file)
@@ -256,7 +256,11 @@ void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc) {
 /*
  * Spools out one message from the list.
  */
-void network_spool_msg(long msgnum, void *userdata) {
+void network_spool_msg(long msgnum, 
+                      void *userdata, 
+                      char *working_ignetcfg,
+                      NetMap *the_netmap)
+{
        SpoolControl *sc;
        int i;
        char *newpath = NULL;
@@ -428,7 +432,12 @@ void network_spool_msg(long msgnum, void *userdata) {
                                if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
                                        ok_to_participate = 1;
                                }
-                               if (is_valid_node(NULL, NULL, msg->cm_fields['N']) == 0) {
+                               if (is_valid_node(NULL, 
+                                                 NULL, 
+                                                 msg->cm_fields['N'], 
+                                                 working_ignetcfg, 
+                                                 the_netmap) == 0)
+                               {
                                        ok_to_participate = 1;
                                }
                        }
@@ -510,7 +519,12 @@ void network_spool_msg(long msgnum, void *userdata) {
                        send = 1;
 
                        /* Check for valid node name */
-                       if (is_valid_node(NULL, NULL, mptr->remote_nodename) != 0) {
+                       if (is_valid_node(NULL, 
+                                         NULL, 
+                                         mptr->remote_nodename, 
+                                         working_ignetcfg, 
+                                         the_netmap) != 0)
+                       {
                                syslog(LOG_ERR, "Invalid node <%s>\n", mptr->remote_nodename);
                                send = 0;
                        }
index 544bf1f04a60d1c0bd636f8cf3e04934e63ce4d5..c014026458964f927ea1b0c82f25626dbcc3b629 100644 (file)
@@ -95,7 +95,7 @@
 /*
  * Learn topology from path fields
  */
-void network_learn_topology(char *node, char *path) {
+void network_learn_topology(char *node, char *path, NetMap *the_netmap, int *netmap_changed) {
        char nexthop[256];
        NetMap *nmptr;
 
@@ -106,7 +106,7 @@ void network_learn_topology(char *node, char *path) {
                if (!strcasecmp(nmptr->nodename, node)) {
                        extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
                        nmptr->lastcontact = time(NULL);
-                       ++netmap_changed;
+                       (*netmap_changed) ++;
                        return;
                }
        }
@@ -118,7 +118,7 @@ void network_learn_topology(char *node, char *path) {
        extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
        nmptr->next = the_netmap;
        the_netmap = nmptr;
-       ++netmap_changed;
+       (*netmap_changed) ++;
 }
 
 
@@ -445,7 +445,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;
@@ -492,7 +493,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'];
@@ -561,7 +567,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
@@ -616,7 +625,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;
@@ -627,7 +642,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);
        }
@@ -639,7 +658,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);
@@ -663,7 +686,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;
                }
@@ -673,7 +701,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);
@@ -684,7 +717,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;
@@ -717,7 +751,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);
                }
        }
 
@@ -728,7 +765,8 @@ 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];
@@ -797,7 +835,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);
index bf5f1d3b946fb9b33efa73491743b1fa9f673cba..3f284ce4ad1e9089d8528accabc4e1c647dbe1a4 100644 (file)
@@ -269,7 +269,6 @@ void network_queue_room(struct ctdlroom *qrbuf, void *data) {
 void destroy_network_queue_room(void)
 {
        struct RoomProcList *cur, *p;
-       NetMap *nmcur, *nmp;
 
        cur = rplist;
        begin_critical_section(S_RPLIST);
@@ -281,18 +280,6 @@ void destroy_network_queue_room(void)
        }
        rplist = NULL;
        end_critical_section(S_RPLIST);
-
-       nmcur = the_netmap;
-       while (nmcur != NULL)
-       {
-               nmp = nmcur->next;
-               free (nmcur);
-               nmcur = nmp;            
-       }
-       the_netmap = NULL;
-       if (working_ignetcfg != NULL)
-               free (working_ignetcfg);
-       working_ignetcfg = NULL;
 }
 
 
@@ -420,6 +407,9 @@ void network_do_queue(void) {
        static time_t last_run = 0L;
        struct RoomProcList *ptr;
        int full_processing = 1;
+       char *working_ignetcfg;
+       NetMap *the_netmap = NULL;
+       int netmap_changed = 0;
 
        /*
         * Run the full set of processing tasks no more frequently
@@ -444,12 +434,12 @@ void network_do_queue(void) {
        doing_queue = 1;
 
        /* Load the IGnet Configuration into memory */
-       load_working_ignetcfg();
+       working_ignetcfg = load_working_ignetcfg();
 
        /*
         * Load the network map and filter list into memory.
         */
-       read_network_map();
+       the_netmap = read_network_map();
        load_network_filter_list();
 
        /* 
@@ -488,16 +478,19 @@ void network_do_queue(void) {
 
        /* If there is anything in the inbound queue, process it */
        if (!server_shutting_down) {
-               network_do_spoolin();
+               network_do_spoolin(working_ignetcfg, 
+                                  the_netmap,
+                                  &netmap_changed);
        }
 
        /* Save the network map back to disk */
-       write_network_map();
+       write_network_map(the_netmap, netmap_changed);
 
        /* Free the filter list in memory */
        free_netfilter_list();
 
-       network_consolidate_spoolout();
+       network_consolidate_spoolout(working_ignetcfg, the_netmap);
+       free(working_ignetcfg);
 
        syslog(LOG_DEBUG, "network: queue run completed\n");
 
index 7f045243ab43b741cc4afe795234d3c3f8597ea1..4027922ec4d572a0a594ccbb7e364f0f0ceeeb56 100644 (file)
@@ -86,6 +86,7 @@
 #include "netconfig.h"
 #include "ctdl_module.h"
 
+
 /*
  * receive network spool from the remote system
  */
@@ -381,7 +382,8 @@ bail:
  * Set "full" to nonzero to force a poll of every node, or to zero to poll
  * only nodes to which we have data to send.
  */
-void network_poll_other_citadel_nodes(int full_poll) {
+void network_poll_other_citadel_nodes(int full_poll, char *working_ignetcfg)
+{
        int i;
        char linebuf[256];
        char node[SIZ];
@@ -430,6 +432,7 @@ void network_poll_other_citadel_nodes(int full_poll) {
 
 void network_do_clientqueue(void)
 {
+       char *working_ignetcfg;
        int full_processing = 1;
        static time_t last_run = 0L;
 
@@ -444,13 +447,15 @@ void network_do_clientqueue(void)
                );
        }
 
-
+       working_ignetcfg = load_working_ignetcfg();
        /*
         * Poll other Citadel nodes.  Maybe.  If "full_processing" is set
         * then we poll everyone.  Otherwise we only poll nodes we have stuff
         * to send to.
         */
-       network_poll_other_citadel_nodes(full_processing);
+       network_poll_other_citadel_nodes(full_processing, working_ignetcfg);
+       if (working_ignetcfg)
+               free(working_ignetcfg);
 }