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