From 237e7adbd01a3c1fd1e9f7bb63b7d4e2e04378ab Mon Sep 17 00:00:00 2001 From: Wilfried Goesgens Date: Thu, 8 Sep 2011 17:18:12 +0000 Subject: [PATCH] Remove global variables; replace by stack passing. --- citadel/modules/network/netconfig.h | 13 ++-- citadel/modules/network/netspool.h | 4 +- citadel/modules/network/serv_netconfig.c | 56 +++++++-------- citadel/modules/network/serv_netmail.c | 20 +++++- citadel/modules/network/serv_netspool.c | 72 ++++++++++++++++---- citadel/modules/network/serv_network.c | 29 +++----- citadel/modules/network/serv_networkclient.c | 11 ++- 7 files changed, 124 insertions(+), 81 deletions(-) diff --git a/citadel/modules/network/netconfig.h b/citadel/modules/network/netconfig.h index 4e1e26433..37e1e0c3e 100644 --- a/citadel/modules/network/netconfig.h +++ b/citadel/modules/network/netconfig.h @@ -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); diff --git a/citadel/modules/network/netspool.h b/citadel/modules/network/netspool.h index e4c8c9fff..a998138a8 100644 --- a/citadel/modules/network/netspool.h +++ b/citadel/modules/network/netspool.h @@ -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); diff --git a/citadel/modules/network/serv_netconfig.c b/citadel/modules/network/serv_netconfig.c index 970057133..c6acdf55d 100644 --- a/citadel/modules/network/serv_netconfig.c +++ b/citadel/modules/network/serv_netconfig.c @@ -92,43 +92,26 @@ #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", 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( diff --git a/citadel/modules/network/serv_netmail.c b/citadel/modules/network/serv_netmail.c index e2b2f2183..ce94c1fc4 100644 --- a/citadel/modules/network/serv_netmail.c +++ b/citadel/modules/network/serv_netmail.c @@ -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; } diff --git a/citadel/modules/network/serv_netspool.c b/citadel/modules/network/serv_netspool.c index 544bf1f04..c01402645 100644 --- a/citadel/modules/network/serv_netspool.c +++ b/citadel/modules/network/serv_netspool.c @@ -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); diff --git a/citadel/modules/network/serv_network.c b/citadel/modules/network/serv_network.c index bf5f1d3b9..3f284ce4a 100644 --- a/citadel/modules/network/serv_network.c +++ b/citadel/modules/network/serv_network.c @@ -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"); diff --git a/citadel/modules/network/serv_networkclient.c b/citadel/modules/network/serv_networkclient.c index 7f045243a..4027922ec 100644 --- a/citadel/modules/network/serv_networkclient.c +++ b/citadel/modules/network/serv_networkclient.c @@ -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); } -- 2.30.2