d2553625dbf9425c08b5e538a3d98b825715e714
[tinc] / src / node.c
1 /*
2     node.c -- node tree management
3     Copyright (C) 2001 Guus Sliepen <guus@sliepen.warande.net>,
4                   2001 Ivo Timmermans <itimmermans@bigfoot.com>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: node.c,v 1.1.2.1 2001/10/10 08:49:47 guus Exp $
21 */
22
23 avl_tree_t *node_tree;          /* Known nodes, sorted by name */
24
25 int node_compare(connection_t *a, connection_t *b)
26 {
27   return strcmp(a->name, b->name);
28 }
29
30 void init_nodes(void)
31 {
32 cp
33   node_tree = avl_alloc_tree((avl_compare_t)node_compare, NULL);
34 cp
35 }
36
37 void exit_nodes(void)
38 {
39 cp
40   avl_delete_tree(node_tree);
41 cp
42 }
43
44 node_t *new_node(void)
45 {
46   node_t *n = (node_t *)xmalloc_and_zero(sizeof(*n));
47 cp
48   n->subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
49   n->queue = list_alloc((list_action_t)free);
50 cp
51   return n;
52 }
53
54 void free_node(node_t *n)
55 {
56 cp
57   if(n->queue)
58     list_delete_list(n->queue);
59   if(n->name)
60     free(n->name);
61   if(n->hostname)
62     free(n->hostname);
63   if(n->key)
64     free(n->key);
65   if(n->config)
66     clear_config(&n->config);
67   free(n);
68 cp
69 }
70
71 node_t *lookup_node(char *name)
72 {
73   node_t n;
74 cp
75   n.name = name;
76   return avl_search(node_tree, &n);
77 }
78
79
80 int read_host_config(nodet *n)
81 {
82   char *fname;
83   int x;
84 cp
85   asprintf(&fname, "%s/hosts/%s", confbase, n->name);
86   x = read_config_file(&n->config, fname);
87   free(fname);
88 cp
89   return x;
90 }
91
92 void dump_nodes(void)
93 {
94   avl_node_t *node;
95   node_t *n;
96 cp
97   syslog(LOG_DEBUG, _("Nodes:"));
98
99   for(node = node_tree->head; node; node = node->next)
100     {
101       n = (connection_t *)node->data;
102       syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"),
103              n->name, n->hostname, n->port, n->options,
104              n->socket, n->meta_socket, n->status);
105     }
106     
107   syslog(LOG_DEBUG, _("End of nodes."));
108 cp
109 }