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) {
50 nodes = avl_tree_new((avl_compare_t)node_compare, (avl_action_t)node_free);
53 if(!cfg_get_string(tinc_cfg, "Name", NULL, &myself->name) || !myself->name) {
54 logger(LOG_ERR, _("rt: name for tinc daemon required!"));
59 if(!node_validname(myself->name)) {
60 logger(LOG_ERR, _("rt: invalid name for myself!"));
68 bool node_exit(void) {
73 node_t *node_new(void) {
77 node->subnets = subnet_tree_new();
78 node->edges = edge_tree_new();
79 node->queue = avl_tree_new(NULL, (avl_action_t)free);
84 void node_free(node_t *node) {
86 avl_tree_free(node->queue);
89 subnet_tree_free(node->subnets);
92 edge_tree_free(node->edges);
94 replace(node->name, NULL);
99 void node_add(node_t *node) {
100 avl_add(nodes, node);
103 void node_del(node_t *node) {
107 avl_foreach(node->subnets, subnet, subnet_del(subnet));
108 avl_foreach(node->edges, edge, edge_del(edge));
110 avl_del(nodes, node);
113 node_t *node_get(char *name) {
118 return avl_get(nodes, &search);