X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;ds=inline;f=src%2Fnode.c;h=4f4f599932caa1d6985afdc689b1a9d43b670310;hb=dd2cf09a9ac438b65a1f4c9dcff8d87a2b504538;hp=d2553625dbf9425c08b5e538a3d98b825715e714;hpb=ec0c16b9b63f361b11a757ee1641d562e4811f93;p=meshlink diff --git a/src/node.c b/src/node.c index d2553625..4f4f5999 100644 --- a/src/node.c +++ b/src/node.c @@ -1,7 +1,6 @@ /* node.c -- node tree management - Copyright (C) 2001 Guus Sliepen , - 2001 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 @@ -13,97 +12,134 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - $Id: node.c,v 1.1.2.1 2001/10/10 08:49:47 guus Exp $ + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -avl_tree_t *node_tree; /* Known nodes, sorted by name */ +#include "system.h" + +#include "hash.h" +#include "logger.h" +#include "meshlink_internal.h" +#include "net.h" +#include "netutl.h" +#include "node.h" +#include "splay_tree.h" +#include "utils.h" +#include "xalloc.h" -int node_compare(connection_t *a, connection_t *b) -{ - return strcmp(a->name, b->name); +static int node_compare(const node_t *a, const node_t *b) { + return strcmp(a->name, b->name); } -void init_nodes(void) -{ -cp - node_tree = avl_alloc_tree((avl_compare_t)node_compare, NULL); -cp +void init_nodes(meshlink_handle_t *mesh) { + mesh->nodes = splay_alloc_tree((splay_compare_t) node_compare, (splay_action_t) free_node); + mesh->node_udp_cache = hash_alloc(0x100, sizeof(sockaddr_t)); } -void exit_nodes(void) -{ -cp - avl_delete_tree(node_tree); -cp +void exit_nodes(meshlink_handle_t *mesh) { + if(mesh->node_udp_cache) { + hash_free(mesh->node_udp_cache); + } + + if(mesh->nodes) { + splay_delete_tree(mesh->nodes); + } + + mesh->node_udp_cache = NULL; + mesh->nodes = NULL; } -node_t *new_node(void) -{ - node_t *n = (node_t *)xmalloc_and_zero(sizeof(*n)); -cp - n->subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, NULL); - n->queue = list_alloc((list_action_t)free); -cp - return n; +node_t *new_node(void) { + node_t *n = xzalloc(sizeof(*n)); + + n->edge_tree = new_edge_tree(); + n->mtu = MTU; + n->maxmtu = MTU; + n->devclass = _DEV_CLASS_MAX; + + return n; +} + +void free_node(node_t *n) { + n->status.destroyed = true; + + utcp_exit(n->utcp); + + if(n->edge_tree) { + free_edge_tree(n->edge_tree); + } + + sockaddrfree(&n->address); + + ecdsa_free(n->ecdsa); + sptps_stop(&n->sptps); + + if(n->mtutimeout.cb) { + abort(); + } + + free(n->name); + + free(n); } -void free_node(node_t *n) -{ -cp - if(n->queue) - list_delete_list(n->queue); - if(n->name) - free(n->name); - if(n->hostname) - free(n->hostname); - if(n->key) - free(n->key); - if(n->config) - clear_config(&n->config); - free(n); -cp +void node_add(meshlink_handle_t *mesh, node_t *n) { + n->mesh = mesh; + splay_insert(mesh->nodes, n); } -node_t *lookup_node(char *name) -{ - node_t n; -cp - n.name = name; - return avl_search(node_tree, &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(mesh, e); + } + + splay_delete(mesh->nodes, n); } +node_t *lookup_node(meshlink_handle_t *mesh, const char *name) { + const node_t n = {.name = (char *)name}; + node_t *result; + + result = splay_search(mesh->nodes, &n); + + return result; +} -int read_host_config(nodet *n) -{ - char *fname; - int x; -cp - asprintf(&fname, "%s/hosts/%s", confbase, n->name); - x = read_config_file(&n->config, fname); - free(fname); -cp - return x; +node_t *lookup_node_udp(meshlink_handle_t *mesh, const sockaddr_t *sa) { + return hash_search(mesh->node_udp_cache, sa); } -void dump_nodes(void) -{ - avl_node_t *node; - node_t *n; -cp - syslog(LOG_DEBUG, _("Nodes:")); - - for(node = node_tree->head; node; node = node->next) - { - n = (connection_t *)node->data; - syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"), - n->name, n->hostname, n->port, n->options, - n->socket, n->meta_socket, n->status); - } - - syslog(LOG_DEBUG, _("End of nodes.")); -cp +void update_node_udp(meshlink_handle_t *mesh, node_t *n, const sockaddr_t *sa) { + if(n == mesh->self) { + logger(mesh, MESHLINK_WARNING, "Trying to update UDP address of mesh->self!"); + return; + } + + hash_insert(mesh->node_udp_cache, &n->address, NULL); + + if(sa) { + n->address = *sa; + n->sock = 0; + + 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; + } + } + + hash_insert(mesh->node_udp_cache, sa, n); + + meshlink_hint_address(mesh, (meshlink_node_t *)n, &sa->sa); + + if(mesh->log_level >= MESHLINK_DEBUG) { + char *hostname = sockaddr2hostname(&n->address); + logger(mesh, MESHLINK_DEBUG, "UDP address of %s set to %s", n->name, hostname); + free(hostname); + } + } }