X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fnode.c;h=3e4dec957f98f410b3912dc9dcf9904dca64fc4c;hb=a1b50920b9a52f86ca6e33fcb24d7fa34313a1ee;hp=171b95fe78544753a43cf633a6a9106e8f9f8aef;hpb=a86faaf34711d6b0f278b670d70a229a3cf0d479;p=meshlink diff --git a/src/node.c b/src/node.c index 171b95fe..3e4dec95 100644 --- a/src/node.c +++ b/src/node.c @@ -1,7 +1,6 @@ /* node.c -- node tree management - Copyright (C) 2001-2013 Guus Sliepen , - 2001-2005 Ivo Timmermans + Copyright (C) 2014 Guus Sliepen , This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,6 +21,7 @@ #include "hash.h" #include "logger.h" +#include "meshlink_internal.h" #include "net.h" #include "netutl.h" #include "node.h" @@ -29,23 +29,20 @@ #include "utils.h" #include "xalloc.h" -splay_tree_t *node_tree; static hash_t *node_udp_cache; -node_t *myself; - static int node_compare(const node_t *a, const node_t *b) { return strcmp(a->name, b->name); } -void init_nodes(void) { - node_tree = splay_alloc_tree((splay_compare_t) node_compare, (splay_action_t) free_node); +void init_nodes(meshlink_handle_t *mesh) { + mesh->nodes = splay_alloc_tree((splay_compare_t) node_compare, (splay_action_t) free_node); node_udp_cache = hash_alloc(0x100, sizeof(sockaddr_t)); } -void exit_nodes(void) { +void exit_nodes(meshlink_handle_t *mesh) { hash_free(node_udp_cache); - splay_delete_tree(node_tree); + splay_delete_tree(mesh->nodes); } node_t *new_node(void) { @@ -65,15 +62,11 @@ void free_node(node_t *n) { sockaddrfree(&n->address); - cipher_close(n->incipher); - digest_close(n->indigest); - cipher_close(n->outcipher); - digest_close(n->outdigest); - ecdsa_free(n->ecdsa); sptps_stop(&n->sptps); - timeout_del(&n->mtutimeout); + if(n->mtutimeout.cb) + abort(); if(n->hostname) free(n->hostname); @@ -87,32 +80,35 @@ void free_node(node_t *n) { free(n); } -void node_add(node_t *n) { - splay_insert(node_tree, n); +void node_add(meshlink_handle_t *mesh, node_t *n) { + n->mesh = mesh; + splay_insert(mesh->nodes, n); } -void node_del(node_t *n) { +void node_del(meshlink_handle_t *mesh, node_t *n) { + timeout_del(&mesh->loop, &n->mtutimeout); + for splay_each(edge_t, e, n->edge_tree) - edge_del(e); + edge_del(mesh, e); - splay_delete(node_tree, n); + splay_delete(mesh->nodes, n); } -node_t *lookup_node(char *name) { +node_t *lookup_node(meshlink_handle_t *mesh, char *name) { node_t n = {NULL}; n.name = name; - return splay_search(node_tree, &n); + return splay_search(mesh->nodes, &n); } -node_t *lookup_node_udp(const sockaddr_t *sa) { +node_t *lookup_node_udp(meshlink_handle_t *mesh, const sockaddr_t *sa) { return hash_search(node_udp_cache, sa); } -void update_node_udp(node_t *n, const sockaddr_t *sa) { - if(n == myself) { - logger(DEBUG_ALWAYS, LOG_WARNING, "Trying to update UDP address of myself!"); +void update_node_udp(meshlink_handle_t *mesh, node_t *n, const sockaddr_t *sa) { + if(n == mesh->self) { + logger(DEBUG_ALWAYS, LOG_WARNING, "Trying to update UDP address of mesh->self!"); return; } @@ -121,8 +117,8 @@ void update_node_udp(node_t *n, const sockaddr_t *sa) { if(sa) { n->address = *sa; n->sock = 0; - for(int i = 0; i < listen_sockets; i++) { - if(listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) { + for(int i = 0; i < mesh->listen_sockets; i++) { + if(mesh->listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) { n->sock = i; break; }