- Proper initialization of rbltree structures.
[tinc] / src / connection.c
1 /*
2     connection.c -- connection list management
3     Copyright (C) 2000 Guus Sliepen <guus@sliepen.warande.net>,
4                   2000 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: connection.c,v 1.1.2.2 2000/11/20 19:41:10 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <syslog.h>
27
28 #include <rbl.h>
29
30 #include "net.h"        /* Don't ask. */
31 #include "netutl.h"
32 #include "config.h"
33 #include "conf.h"
34 #include <utils.h>
35 #include "subnet.h"
36
37 #include "xalloc.h"
38 #include "system.h"
39
40 /* Root of the connection list */
41
42 rbltree_t *connection_tree;
43 connection_t *myself = NULL;
44
45 /* Initialization and callbacks */
46
47 int connection_compare(connection_t *a, connection_t *b)
48 {
49   return strcmp(a->name, b->name);
50 }
51
52 void init_connections(void)
53 {
54   connection_tree = new_rbltree((rbl_compare_t)connection_compare, (rbl_action_t)free_connection);
55 }
56
57 /* Creation and deletion of connection elements */
58
59 connection_t *new_connection(void)
60 {
61   connection_t *p = (connection_t *)xmalloc(sizeof(*p));
62 cp
63   /* initialise all those stupid pointers at once */
64   memset(p, '\0', sizeof(*p));
65
66   p->subnet_tree = new_rbltree((rbl_compare_t)subnet_compare, NULL);
67 cp
68   return p;
69 }
70
71 void free_connection(connection_t *p)
72 {
73 cp
74   if(p->sq)
75     destroy_queue(p->sq);
76   if(p->rq)
77     destroy_queue(p->rq);
78   if(p->name && p->name!=unknown)
79     free(p->name);
80   if(p->hostname)
81     free(p->hostname);
82   if(p->rsa_key)
83     RSA_free(p->rsa_key);
84   if(p->cipher_pktkey)
85     free(p->cipher_pktkey);
86   if(p->buffer)
87     free(p->buffer);
88   if(p->config)
89     clear_config(&p->config);
90   free(p);
91 cp
92 }
93
94 /*
95   remove all marked connections
96 */
97 void prune_connection_tree(void)
98 {
99   rbl_t *rbl;
100   connection_t *cl;
101 cp
102   RBL_FOREACH(connection_tree, rbl)
103     {
104       cl = (connection_t *) rbl->data;
105       if(cl->status.remove)
106         connection_del(cl);
107     }
108 cp
109 }
110
111 /*
112   free all elements of connection
113 */
114 void destroy_connection_tree(void)
115 {
116 cp
117   rbl_delete_rbltree(connection_tree);
118 cp
119 }
120
121 /* Linked list management */
122
123 void connection_add(connection_t *cl)
124 {
125 cp
126   rbl_insert(connection_tree, cl);
127 cp
128 }
129
130 void connection_del(connection_t *cl)
131 {
132 cp
133   rbl_delete(connection_tree, cl);
134 cp
135 }
136
137 /* Lookup functions */
138
139 connection_t *lookup_id(char *name)
140 {
141   connection_t cl;
142 cp
143   cl.name = name;
144   return rbl_search(connection_tree, &cl);
145 cp
146 }
147
148 /* Debugging */
149
150 void dump_connection_list(void)
151 {
152   rbl_t *rbl;
153   connection_t *cl;
154 cp
155   syslog(LOG_DEBUG, _("Connection list:"));
156
157   syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
158          myself->name, myself->hostname, myself->port, myself->flags,
159          myself->socket, myself->meta_socket, myself->status);
160
161   RBL_FOREACH(connection_tree, rbl)
162     {
163       cl = (connection_t *)rbl->data;
164       syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
165              cl->name, cl->hostname, cl->port, cl->flags,
166              cl->socket, cl->meta_socket, cl->status);
167     }
168     
169   syslog(LOG_DEBUG, _("End of connection list."));
170 cp
171 }
172
173 int read_host_config(connection_t *cl)
174 {
175   char *fname;
176   int x;
177 cp
178   asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
179   x = read_config_file(&cl->config, fname);
180   free(fname);
181 cp
182   return x;
183 }