4eee529059483ee051ca5567bd223e813ec5bb52
[oweals/tinc.git] / rt / node.c
1 /*
2     node.c -- node management
3
4     Copyright (C) 2003-2004 Guus Sliepen <guus@tinc-vpn.org>,
5                   2003-2004 Ivo Timmermans <ivo@tinc-vpn.org>
6
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.
11
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.
16
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.
20
21     $Id$
22 */
23
24 #include "system.h"
25
26 #include "cfg/cfg.h"
27 #include "logger/logger.h"
28 #include "rt/node.h"
29 #include "support/avl.h"
30 #include "support/xalloc.h"
31 #include "tincd.h"
32
33 avl_tree_t *nodes;
34
35 node_t *myself;
36
37 static int node_compare(const node_t *a, const node_t *b) {
38         return strcmp(a->name, b->name);
39 }
40
41 bool node_validname(const char *name) {
42         for(; *name; name++)
43                 if(!isalnum(*name) && *name != '_')
44                         return false;
45
46         return true;
47 }
48
49 bool node_init(void) {
50         char *cfgfilename;
51
52         nodes = avl_tree_new((avl_compare_t)node_compare, (avl_action_t)node_free);
53         myself = node_new();
54
55         if(!cfg_get_string(tinc_cfg, "Name", NULL, &myself->name) || !myself->name) {
56                 logger(LOG_ERR, _("rt: name for tinc daemon required!"));
57                 node_exit();
58                 return false;
59         }
60
61         if(!node_validname(myself->name)) {
62                 logger(LOG_ERR, _("rt: invalid name for myself!"));
63                 node_exit();
64                 return false;
65         }
66
67         myself->cfg = cfg_tree_new();
68
69         asprintf(&cfgfilename, "%s/hosts/%s", tinc_confbase, myself->name);
70
71         if(!cfg_read_file(myself->cfg, cfgfilename)) {
72                 free(cfgfilename);
73                 node_exit();
74                 return false;
75         }
76
77         free(cfgfilename);
78         
79         return true;
80 }
81
82 bool node_exit(void) {
83         avl_tree_del(nodes);
84         return true;
85 }
86
87 node_t *node_new(void) {
88         node_t *node;
89
90         clear(new(node));
91         node->subnets = subnet_tree_new();
92         node->edges = edge_tree_new();
93         node->queue = avl_tree_new(NULL, (avl_action_t)free);
94
95         return node;
96 }
97
98 void node_free(node_t *node) {
99         if(node->queue)
100                 avl_tree_free(node->queue);
101
102         if(node->subnets)
103                 subnet_tree_free(node->subnets);
104
105         if(node->edges)
106                 edge_tree_free(node->edges);
107
108         replace(node->name, NULL);
109
110         free(node);
111 }
112
113 void node_add(node_t *node) {
114         avl_add(nodes, node);
115 }
116
117 void node_del(node_t *node) {
118         edge_t *edge;
119         subnet_t *subnet;
120
121         avl_foreach(node->subnets, subnet, subnet_del(subnet));
122         avl_foreach(node->edges, edge, edge_del(edge));
123
124         avl_del(nodes, node);
125 }
126
127 node_t *node_get(char *name) {
128         node_t search = {0};
129
130         search.name = name;
131
132         return avl_get(nodes, &search);
133 }
134