]> git.meshlink.io Git - meshlink/commitdiff
Replace the connection_tree with a connection_list.
authorGuus Sliepen <guus@tinc-vpn.org>
Sun, 7 Oct 2012 19:59:53 +0000 (21:59 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Sun, 7 Oct 2012 19:59:53 +0000 (21:59 +0200)
The tree functions were never used on the connection_tree, a list is more appropriate.
Also be more paranoid about connections disappearing while traversing the list.

15 files changed:
src/connection.c
src/connection.h
src/control.c
src/graph.c
src/logger.c
src/meta.c
src/net.c
src/net_packet.c
src/net_setup.c
src/net_socket.c
src/protocol_auth.c
src/protocol_edge.c
src/protocol_key.c
src/route.c
src/sptps_test.c

index 7999b6fb08a28ef1146324f2890f11f93f43ff57..3591f13e4350bea2055b24e6627e535e1fcf39ab 100644 (file)
@@ -21,7 +21,7 @@
 
 #include "system.h"
 
-#include "splay_tree.h"
+#include "list.h"
 #include "cipher.h"
 #include "conf.h"
 #include "control_common.h"
 #include "utils.h"
 #include "xalloc.h"
 
-splay_tree_t *connection_tree; /* Meta connections */
+list_t *connection_list;       /* Meta connections */
 connection_t *everyone;
 
-static int connection_compare(const connection_t *a, const connection_t *b) {
-       return a < b ? -1 : a == b ? 0 : 1;
-}
-
 void init_connections(void) {
-       connection_tree = splay_alloc_tree((splay_compare_t) connection_compare, (splay_action_t) free_connection);
+       connection_list = list_alloc((list_action_t) free_connection);
        everyone = new_connection();
        everyone->name = xstrdup("everyone");
        everyone->hostname = xstrdup("BROADCAST");
 }
 
 void exit_connections(void) {
-       splay_delete_tree(connection_tree);
+       list_delete_list(connection_list);
        free_connection(everyone);
 }
 
@@ -91,19 +87,22 @@ void free_connection(connection_t *c) {
 }
 
 void connection_add(connection_t *c) {
-       splay_insert(connection_tree, c);
+       list_insert_tail(connection_list, c);
 }
 
 void connection_del(connection_t *c) {
-       splay_delete(connection_tree, c);
+       for(list_node_t *node = connection_list->head; node; node = node->next) {
+               if(node->data == c) {
+                       list_delete_node(connection_list, node);
+                       return;
+               }
+       }
 }
 
 bool dump_connections(connection_t *cdump) {
-       splay_node_t *node;
-       connection_t *c;
-
-       for(node = connection_tree->head; node; node = node->next) {
-               c = node->data;
+       for(list_node_t *node = connection_list->head, *next; node; node = next) {
+               next = node->next;
+               connection_t *c = node->data;
                send_request(cdump, "%d %d %s %s %x %d %x",
                                CONTROL, REQ_DUMP_CONNECTIONS,
                                c->name, c->hostname, c->options, c->socket,
index 5730fbb84e9779bf32ad999ffb5c7e2188e15a4a..ec342b39f4c96cd45f9150fc26d317ae7d32ccd9 100644 (file)
@@ -25,7 +25,7 @@
 #include "cipher.h"
 #include "digest.h"
 #include "rsa.h"
-#include "splay_tree.h"
+#include "list.h"
 #include "sptps.h"
 
 #define OPTION_INDIRECT                0x0001
@@ -100,7 +100,7 @@ typedef struct connection_t {
        splay_tree_t *config_tree;      /* Pointer to configuration tree belonging to him */
 } connection_t;
 
-extern splay_tree_t *connection_tree;
+extern list_t *connection_list;
 extern connection_t *everyone;
 
 extern void init_connections(void);
index 956d6ee10bd066174c4ef406ec8d71020b31de9d..46300ed9503bf0c4677d476ac86e335b3288f62c 100644 (file)
@@ -100,13 +100,12 @@ bool control_h(connection_t *c, const char *request) {
                case REQ_DISCONNECT: {
                        char name[MAX_STRING_SIZE];
                        connection_t *other;
-                       splay_node_t *node, *next;
                        bool found = false;
 
                        if(sscanf(request, "%*d %*d " MAX_STRING, name) != 1)
                                return control_return(c, REQ_DISCONNECT, -1);
 
-                       for(node = connection_tree->head; node; node = next) {
+                       for(list_node_t *node = connection_list->head, *next; node; node = next) {
                                next = node->next;
                                other = node->data;
                                if(strcmp(other->name, name))
index 621dd9be3ae0246e316a7b96760a1d29fb5adb25..3cc99c65606c2409814f0715d36586d9f211f506 100644 (file)
 
 #include "system.h"
 
-#include "splay_tree.h"
 #include "config.h"
 #include "connection.h"
 #include "device.h"
 #include "edge.h"
 #include "graph.h"
+#include "list.h"
 #include "logger.h"
 #include "netutl.h"
 #include "node.h"
 */
 
 static void mst_kruskal(void) {
-       splay_node_t *node, *next;
-       edge_t *e;
-       node_t *n;
-       connection_t *c;
-
        /* Clear MST status on connections */
 
-       for(node = connection_tree->head; node; node = node->next) {
-               c = node->data;
+       for(list_node_t *node = connection_list->head; node; node = node->next) {
+               connection_t *c = node->data;
                c->status.mst = false;
        }
 
@@ -82,16 +77,16 @@ static void mst_kruskal(void) {
 
        /* Clear visited status on nodes */
 
-       for(node = node_tree->head; node; node = node->next) {
-               n = node->data;
+       for(splay_node_t *node = node_tree->head; node; node = node->next) {
+               node_t *n = node->data;
                n->status.visited = false;
        }
 
        /* Add safe edges */
 
-       for(node = edge_weight_tree->head; node; node = next) {
+       for(splay_node_t *node = edge_weight_tree->head, *next; node; node = next) {
                next = node->next;
-               e = node->data;
+               edge_t *e = node->data;
 
                if(!e->reverse || (e->from->status.visited && e->to->status.visited))
                        continue;
index 56bee84566fafd1afbd9d2d1570e5ed34b8897a1..5de88bdeb5a9321eb09bd3182904de80ec54f0a3 100644 (file)
@@ -140,7 +140,8 @@ void logger(int level, int priority, const char *format, ...) {
        if(logcontrol) {
                suppress = true;
                logcontrol = false;
-               for(splay_node_t *node = connection_tree->head; node; node = node->next) {
+               for(list_node_t *node = connection_list->head, *next; node; node = next) {
+                       next = node->next;
                        connection_t *c = node->data;
                        if(!c->status.log)
                                continue;
index 9b2ecc267f38869800e5b9eb436bfde2a60ba554..7562792e2f4aa83da535ac2277f97976eec97248 100644 (file)
@@ -21,7 +21,6 @@
 
 #include "system.h"
 
-#include "splay_tree.h"
 #include "cipher.h"
 #include "connection.h"
 #include "logger.h"
@@ -77,11 +76,9 @@ bool send_meta(connection_t *c, const char *buffer, int length) {
 }
 
 void broadcast_meta(connection_t *from, const char *buffer, int length) {
-       splay_node_t *node;
-       connection_t *c;
-
-       for(node = connection_tree->head; node; node = node->next) {
-               c = node->data;
+       for(list_node_t *node = connection_list->head, *next; node; node = next) {
+               next = node->next;
+               connection_t *c = node->data;
 
                if(c != from && c->status.active)
                        send_meta(c, buffer, length);
index 9a32d628957238daa5df7c97408afa24c522e6b2..ddca1c9b9a8b8a29a9ea2a7225904cafebb08fab 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -23,7 +23,6 @@
 #include "system.h"
 
 #include "utils.h"
-#include "splay_tree.h"
 #include "conf.h"
 #include "connection.h"
 #include "device.h"
@@ -158,13 +157,11 @@ void terminate_connection(connection_t *c, bool report) {
   and close the connection.
 */
 static void timeout_handler(int fd, short events, void *event) {
-       splay_node_t *node, *next;
-       connection_t *c;
        time_t now = time(NULL);
 
-       for(node = connection_tree->head; node; node = next) {
+       for(list_node_t *node = connection_list->head, *next; node; node = next) {
                next = node->next;
-               c = node->data;
+               connection_t *c = node->data;
 
                if(c->status.control)
                        continue;
@@ -249,10 +246,7 @@ static void sigalrm_handler(int signal, short events, void *data) {
 }
 
 int reload_configuration(void) {
-       connection_t *c;
-       splay_node_t *node, *next;
        char *fname;
-       struct stat s;
 
        /* Reread our own configuration file */
 
@@ -278,18 +272,16 @@ int reload_configuration(void) {
        /* If StrictSubnet is set, expire deleted Subnets and read new ones in */
 
        if(strictsubnets) {
-               subnet_t *subnet;
-
-               for(node = subnet_tree->head; node; node = node->next) {
-                       subnet = node->data;
+               for(splay_node_t *node = subnet_tree->head; node; node = node->next) {
+                       subnet_t *subnet = node->data;
                        subnet->expires = 1;
                }
 
                load_all_subnets();
 
-               for(node = subnet_tree->head; node; node = next) {
+               for(splay_node_t *node = subnet_tree->head, *next; node; node = next) {
                        next = node->next;
-                       subnet = node->data;
+                       subnet_t *subnet = node->data;
                        if(subnet->expires == 1) {
                                send_del_subnet(everyone, subnet);
                                if(subnet->owner->status.reachable)
@@ -304,9 +296,7 @@ int reload_configuration(void) {
                        }
                }
        } else { /* Only read our own subnets back in */
-               subnet_t *subnet, *s2;
-
-               for(node = myself->subnet_tree->head; node; node = node->next) {
+               for(splay_node_t *node = myself->subnet_tree->head; node; node = node->next) {
                        subnet_t *subnet = node->data;
                        if(!subnet->expires)
                                subnet->expires = 1;
@@ -315,6 +305,8 @@ int reload_configuration(void) {
                config_t *cfg = lookup_config(config_tree, "Subnet");
 
                while(cfg) {
+                       subnet_t *subnet, *s2;
+
                        if(!get_config_subnet(cfg, &subnet))
                                continue;
 
@@ -332,7 +324,7 @@ int reload_configuration(void) {
                        cfg = lookup_config_next(config_tree, cfg);
                }
 
-               for(node = myself->subnet_tree->head; node; node = next) {
+               for(splay_node_t *node = myself->subnet_tree->head, *next; node; node = next) {
                        next = node->next;
                        subnet_t *subnet = node->data;
                        if(subnet->expires == 1) {
@@ -349,14 +341,15 @@ int reload_configuration(void) {
 
        /* Close connections to hosts that have a changed or deleted host config file */
 
-       for(node = connection_tree->head; node; node = next) {
-               c = node->data;
+       for(list_node_t *node = connection_list->head, *next; node; node = next) {
+               connection_t *c = node->data;
                next = node->next;
 
                if(c->status.control)
                        continue;
 
                xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
+               struct stat s;
                if(stat(fname, &s) || s.st_mtime > last_config_check) {
                        logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name);
                        terminate_connection(c, c->status.active);
@@ -370,12 +363,9 @@ int reload_configuration(void) {
 }
 
 void retry(void) {
-       connection_t *c;
-       splay_node_t *node, *next;
-
-       for(node = connection_tree->head; node; node = next) {
+       for(list_node_t *node = connection_list->head, *next; node; node = next) {
                next = node->next;
-               c = node->data;
+               connection_t *c = node->data;
                
                if(c->outgoing && !c->node) {
                        if(timeout_initialized(&c->outgoing->ev))
index 843926934df62b6d0befd10dab6782d9e4af2014..c9a666377cecd4ec4fd05c7b4bf97eb827f39806 100644 (file)
@@ -36,7 +36,6 @@
 #include LZO1X_H
 #endif
 
-#include "splay_tree.h"
 #include "cipher.h"
 #include "conf.h"
 #include "connection.h"
@@ -750,10 +749,6 @@ void send_packet(node_t *n, vpn_packet_t *packet) {
 /* Broadcast a packet using the minimum spanning tree */
 
 void broadcast_packet(const node_t *from, vpn_packet_t *packet) {
-       splay_node_t *node;
-       connection_t *c;
-       node_t *n;
-
        // Always give ourself a copy of the packet.
        if(from != myself)
                send_packet(myself, packet);
@@ -771,8 +766,9 @@ void broadcast_packet(const node_t *from, vpn_packet_t *packet) {
                // This guarantees all nodes receive the broadcast packet, and
                // usually distributes the sending of broadcast packets over all nodes.
                case BMODE_MST:
-                       for(node = connection_tree->head; node; node = node->next) {
-                               c = node->data;
+                       for(list_node_t *node = connection_list->head, *next; node; node = next) {
+                               next = node->next;
+                               connection_t *c = node->data;
 
                                if(c->status.active && c->status.mst && c != from->nexthop->connection)
                                        send_packet(c->node, packet);
@@ -786,8 +782,8 @@ void broadcast_packet(const node_t *from, vpn_packet_t *packet) {
                        if(from != myself)
                                break;
 
-                       for(node = node_tree->head; node; node = node->next) {
-                               n = node->data;
+                       for(splay_node_t *node = node_tree->head; node; node = node->next) {
+                               node_t *n = node->data;
 
                                if(n->status.reachable && ((n->via == myself && n->nexthop == n) || n->via == n))
                                        send_packet(n, packet);
index fd595a931776591762e36406da8ff7a22f285547..b1a8476ddbee6c51066e3852751fe71537109b6d 100644 (file)
@@ -22,7 +22,6 @@
 
 #include "system.h"
 
-#include "splay_tree.h"
 #include "cipher.h"
 #include "conf.h"
 #include "connection.h"
@@ -972,14 +971,9 @@ bool setup_network(void) {
   close all open network connections
 */
 void close_network_connections(void) {
-       splay_node_t *node, *next;
-       connection_t *c;
-       char *envp[5];
-       int i;
-
-       for(node = connection_tree->head; node; node = next) {
+       for(list_node_t *node = connection_list->head, *next; node; node = next) {
                next = node->next;
-               c = node->data;
+               connection_t *c = node->data;
                /* Keep control connections open until the end, so they know when we really terminated */
                if(c->status.control)
                        c->socket = -1;
@@ -995,13 +989,14 @@ void close_network_connections(void) {
                free_connection(myself->connection);
        }
 
-       for(i = 0; i < listen_sockets; i++) {
+       for(int i = 0; i < listen_sockets; i++) {
                event_del(&listen_socket[i].ev_tcp);
                event_del(&listen_socket[i].ev_udp);
                close(listen_socket[i].tcp);
                close(listen_socket[i].udp);
        }
 
+       char *envp[5];
        xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
        xasprintf(&envp[1], "DEVICE=%s", device ? : "");
        xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
@@ -1018,7 +1013,7 @@ void close_network_connections(void) {
 
        if(myport) free(myport);
 
-       for(i = 0; i < 4; i++)
+       for(int i = 0; i < 4; i++)
                free(envp[i]);
 
        devops.close();
index 0f0580ecef97195137981fc7fd635803ac1d3ea7..a39c2d2813f8532e32634ca01907cbf53fd3c8ee 100644 (file)
@@ -22,7 +22,6 @@
 
 #include "system.h"
 
-#include "splay_tree.h"
 #include "conf.h"
 #include "connection.h"
 #include "logger.h"
@@ -627,9 +626,9 @@ void try_outgoing_connections(void) {
 
        /* Terminate any connections whose outgoing_t is to be deleted. */
 
-       for(splay_node_t *n = connection_tree->head, *next; n; n = next) {
-               next = n->next;
-               connection_t *c = n->data;
+       for(list_node_t *node = connection_list->head, *next; node; node = next) {
+               next = node->next;
+               connection_t *c = node->data;
                if(c->outgoing && c->outgoing->timeout == -1) {
                        c->outgoing = NULL;
                        logger(DEBUG_CONNECTIONS, LOG_INFO, "No more outgoing connection to %s", c->name);
@@ -639,10 +638,10 @@ void try_outgoing_connections(void) {
 
        /* Delete outgoing_ts for which there is no ConnectTo. */
 
-       for(list_node_t *i = outgoing_list->head, *next; i; i = next) {
-               next = i->next;
-               outgoing = i->data;
+       for(list_node_t *node = outgoing_list->head, *next; node; node = next) {
+               next = node->next;
+               outgoing = node->data;
                if(outgoing->timeout == -1)
-                       list_delete_node(outgoing_list, i);
+                       list_delete_node(outgoing_list, node);
        }
 }
index 908ab2472cb2ad20981f4fed904588090f87a56f..59917c6c93f254a39af173c2564406e6f360b9d6 100644 (file)
@@ -20,7 +20,6 @@
 
 #include "system.h"
 
-#include "splay_tree.h"
 #include "conf.h"
 #include "connection.h"
 #include "control.h"
index e1d2051b2186be9e5c14fe27dc35e694c41b7f7b..d1a11f41f0d050d239aa854c43f139ca89f7fafd 100644 (file)
@@ -21,7 +21,6 @@
 
 #include "system.h"
 
-#include "splay_tree.h"
 #include "conf.h"
 #include "connection.h"
 #include "edge.h"
index f34a70da103dc1f2830090f8a2ccee7901f1dd07..7e122068edd672c7a8117107675dab425bd6b59b 100644 (file)
@@ -20,7 +20,6 @@
 
 #include "system.h"
 
-#include "splay_tree.h"
 #include "cipher.h"
 #include "connection.h"
 #include "crypto.h"
 static bool mykeyused = false;
 
 void send_key_changed(void) {
-       splay_node_t *node;
-       connection_t *c;
-
        send_request(everyone, "%d %x %s", KEY_CHANGED, rand(), myself->name);
 
        /* Immediately send new keys to directly connected nodes to keep UDP mappings alive */
 
-       for(node = connection_tree->head; node; node = node->next) {
-               c = node->data;
+       for(list_node_t *node = connection_list->head, *next; node; node = next) {
+               next = node->next;
+               connection_t *c = node->data;
                if(c->status.active && c->node && c->node->status.reachable) {
                        if(!c->node->status.sptps)
                                send_ans_key(c->node);
@@ -55,7 +52,7 @@ void send_key_changed(void) {
        /* Force key exchange for connections using SPTPS */
 
        if(experimental) {
-               for(node = node_tree->head; node; node = node->next) {
+               for(splay_node_t *node = node_tree->head; node; node = node->next) {
                        node_t *n = node->data;
                        if(n->status.reachable && n->status.validkey && n->status.sptps)
                                sptps_force_kex(&n->sptps);
index 4c4312cd01a7729b3fa56fff7f65b224cd650d94..9b939580b4a58b86800bd00b40ec30afb374a973 100644 (file)
@@ -20,7 +20,6 @@
 
 #include "system.h"
 
-#include "splay_tree.h"
 #include "connection.h"
 #include "control_common.h"
 #include "ethernet.h"
@@ -187,15 +186,12 @@ static void swap_mac_addresses(vpn_packet_t *packet) {
 }
        
 static void age_subnets(int fd, short events, void *data) {
-       subnet_t *s;
-       connection_t *c;
-       splay_node_t *node, *next, *node2;
        bool left = false;
        time_t now = time(NULL);
 
-       for(node = myself->subnet_tree->head; node; node = next) {
+       for(splay_node_t *node = myself->subnet_tree->head, *next; node; node = next) {
                next = node->next;
-               s = node->data;
+               subnet_t *s = node->data;
                if(s->expires && s->expires < now) {
                        if(debug_level >= DEBUG_TRAFFIC) {
                                char netstr[MAXNETSTR];
@@ -203,8 +199,9 @@ static void age_subnets(int fd, short events, void *data) {
                                        logger(DEBUG_TRAFFIC, LOG_INFO, "Subnet %s expired", netstr);
                        }
 
-                       for(node2 = connection_tree->head; node2; node2 = node2->next) {
-                               c = node2->data;
+                       for(list_node_t *node = connection_list->head, *next; node; node = next) {
+                               next = node->next;
+                               connection_t *c = node->data;
                                if(c->status.active)
                                        send_del_subnet(c, s);
                        }
@@ -221,11 +218,7 @@ static void age_subnets(int fd, short events, void *data) {
 }
 
 static void learn_mac(mac_t *address) {
-       subnet_t *subnet;
-       splay_node_t *node;
-       connection_t *c;
-
-       subnet = lookup_subnet_mac(myself, address);
+       subnet_t *subnet = lookup_subnet_mac(myself, address);
 
        /* If we don't know this MAC address yet, store it */
 
@@ -244,8 +237,9 @@ static void learn_mac(mac_t *address) {
 
                /* And tell all other tinc daemons it's our MAC */
 
-               for(node = connection_tree->head; node; node = node->next) {
-                       c = node->data;
+               for(list_node_t *node = connection_list->head, *next; node; node = next) {
+                       next = node->next;
+                       connection_t *c = node->data;
                        if(c->status.active)
                                send_add_subnet(c, subnet);
                }
@@ -878,7 +872,8 @@ static void route_mac(node_t *source, vpn_packet_t *packet) {
 
 static void send_pcap(vpn_packet_t *packet) {
        pcap = false;
-       for(splay_node_t *node = connection_tree->head; node; node = node->next) {
+       for(list_node_t *node = connection_list->head, *next; node; node = next) {
+               next = node->next;
                connection_t *c = node->data;
                if(!c->status.pcap)
                        continue;
index 7af8522e136a3d5406a7599c94cb4e3cc76e5797..d8b25d4e9b79bd13a799118fee6820b41e3736e8 100644 (file)
@@ -26,7 +26,7 @@
 
 // Symbols necessary to link with logger.o
 bool send_request(void *c, const char *msg, ...) { return false; }
-struct splay_tree_t *connection_tree = NULL;
+struct list_t *connection_list = NULL;
 bool send_meta(void *c, const char *msg , int len) { return false; }
 char *logfilename = NULL;