2 node.c -- node management
4 Copyright (C) 2003-2004 Guus Sliepen <guus@tinc-vpn.org>,
5 2003-2004 Ivo Timmermans <ivo@tinc-vpn.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "logger/logger.h"
29 #include "support/avl.h"
30 #include "support/xalloc.h"
37 static int node_compare(const node_t *a, const node_t *b) {
38 return strcmp(a->name, b->name);
41 bool node_validname(const char *name) {
43 if(!isalnum(*name) && *name != '_')
49 bool node_init(void) {
52 nodes = avl_tree_new((avl_compare_t)node_compare, (avl_action_t)node_free);
55 if(!cfg_get_string(tinc_cfg, "Name", NULL, &myself->name) || !myself->name) {
56 logger(LOG_ERR, _("rt: name for tinc daemon required!"));
61 if(!node_validname(myself->name)) {
62 logger(LOG_ERR, _("rt: invalid name for myself!"));
67 myself->cfg = cfg_tree_new();
69 asprintf(&cfgfilename, "%s/hosts/%s", tinc_confbase, myself->name);
71 if(!cfg_read_file(myself->cfg, cfgfilename)) {
82 bool node_exit(void) {
87 node_t *node_new(void) {
91 node->subnets = subnet_tree_new();
92 node->edges = edge_tree_new();
93 node->queue = avl_tree_new(NULL, (avl_action_t)free);
98 void node_free(node_t *node) {
100 avl_tree_free(node->queue);
103 subnet_tree_free(node->subnets);
106 edge_tree_free(node->edges);
108 replace(node->name, NULL);
113 void node_add(node_t *node) {
114 avl_add(nodes, node);
117 void node_del(node_t *node) {
121 avl_foreach(node->subnets, subnet, subnet_del(subnet));
122 avl_foreach(node->edges, edge, edge_del(edge));
124 avl_del(nodes, node);
127 node_t *node_get(char *name) {
132 return avl_get(nodes, &search);