- Lots of little stuff modified
[tinc] / src / connlist.c
1 /*
2     connlist.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: connlist.c,v 1.1.2.8 2000/10/24 15:46:16 guus Exp $
21 */
22
23 #include <syslog.h>
24
25 #include "net.h"        /* Don't ask. */
26 #include "config.h"
27 #include "conf.h"
28 #include <utils.h>
29
30 #include "system.h"
31
32 /* Root of the connection list */
33
34 conn_list_t *conn_list = NULL;
35 conn_list_t *myself = NULL;
36
37 /* Creation and deletion of conn_list elements */
38
39 conn_list_t *new_conn_list(void)
40 {
41   conn_list_t *p = (conn_list_t *)xmalloc(sizeof(*p));
42 cp
43   /* initialise all those stupid pointers at once */
44   memset(p, '\0', sizeof(*p));
45 cp
46   return p;
47 }
48
49 void free_conn_list(conn_list_t *p)
50 {
51 cp
52   if(p->sq)
53     destroy_queue(p->sq);
54   if(p->rq)
55     destroy_queue(p->rq);
56   if(p->name && p->name!=unknown)
57     free(p->name);
58   if(p->hostname)
59     free(p->hostname);
60   if(p->rsa_key)
61     RSA_free(p->rsa_key);
62   if(p->cipher_pktkey)
63     free(p->cipher_pktkey);
64   if(p->buffer)
65     free(p->buffer);
66   free(p);
67 cp
68 }
69
70 /*
71   remove all marked connections
72 */
73 void prune_conn_list(void)
74 {
75   conn_list_t *p, *prev = NULL, *next = NULL;
76 cp
77   for(p = conn_list; p != NULL; )
78     {
79       next = p->next;
80
81       if(p->status.remove)
82         {
83           if(prev)
84             prev->next = next;
85           else
86             conn_list = next;
87
88           free_conn_list(p);
89         }
90       else
91         prev = p;
92
93       p = next;
94     }
95 cp
96 }
97
98 /*
99   free all elements of conn_list
100 */
101 void destroy_conn_list(void)
102 {
103   conn_list_t *p, *next;
104 cp
105   for(p = conn_list; p != NULL; )
106     {
107       next = p->next;
108       free_conn_list(p);
109       p = next;
110     }
111
112   conn_list = NULL;
113 cp
114 }
115
116 /* Linked list management */
117
118 void conn_list_add(conn_list_t *cl)
119 {
120 cp
121   cl->next = conn_list;
122   cl->prev = NULL;
123   if(cl->next)
124     cl->next->prev = cl;
125   conn_list = cl;
126 cp
127 }
128
129 void conn_list_del(conn_list_t *cl)
130 {
131 cp
132   if(cl->prev)
133     cl->prev->next = cl->next;
134   else
135     conn_list = cl->next;
136   
137   cl->next->prev = cl->prev;
138   free_conn_list(cl);
139 cp
140 }
141
142 /* Lookup functions */
143
144 conn_list_t *lookup_id(char *name)
145 {
146   conn_list_t *p;
147 cp
148   for(p = conn_list; p != NULL; p = p->next)
149     if(p->status.active)
150       if(strcmp(name, p->name) == 0)
151         break;
152 cp
153   return p;
154 }
155
156 conn_list_t *lookup_conn_list_mac(mac_t address)
157 {
158   conn_list_t *p;
159 cp
160   for(p = conn_list; p != NULL; p = p->next)
161     if(lookup_subnet_mac(p->subnets, address))
162       break;
163 cp
164   return p;
165 }
166
167 conn_list_t *lookup_conn_list_ipv4(ipv4_t address)
168 {
169   conn_list_t *p;
170 cp
171   for(p = conn_list; p != NULL; p = p->next)
172     if(lookup_subnet_ipv4(p->subnets, address))
173       break;
174 cp
175   return p;
176 }
177
178 conn_list_t *lookup_conn_list_ipv6(ipv6_t address)
179 {
180   conn_list_t *p;
181 cp
182   for(p = conn_list; p != NULL; p = p->next)
183     if(lookup_subnet_ipv6(p->subnets, address))
184       break;
185 cp
186   return p;
187 }
188
189 /* Debugging */
190
191 void dump_conn_list(void)
192 {
193   conn_list_t *p;
194   subnet_t *s;
195   char *netstr;
196 cp
197   syslog(LOG_DEBUG, _("Connection list:"));
198
199   syslog(LOG_DEBUG, _("%s at %s port %hd flags %d sockets %d, %d status %04x"),
200          myself->name, myself->hostname, myself->port, myself->flags,
201          myself->socket, myself->meta_socket, myself->status);
202
203   for(s = myself->subnets; s != NULL; s = s->next)
204     {
205       netstr = net2str(s);
206       syslog(LOG_DEBUG, ": %s", netstr);
207       free(netstr);
208     }
209
210   for(p = conn_list; p != NULL; p = p->next)
211     {
212       syslog(LOG_DEBUG, _("%s at %s port %hd flags %d sockets %d, %d status %04x"),
213              p->name, p->hostname, p->port, p->flags,
214              p->socket, p->meta_socket, p->status);
215
216       for(s = p->subnets; s != NULL; s = s->next)
217         {
218           netstr = net2str(s);
219           syslog(LOG_DEBUG, ": %s", netstr);
220           free(netstr);
221         }
222     }
223
224   syslog(LOG_DEBUG, _("End of connection list."));
225 cp
226 }
227
228 int read_host_config(conn_list_t *cl)
229 {
230   char *fname;
231   int x;
232 cp
233   asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
234   x = read_config_file(&cl->config, fname);
235   free(fname);
236 cp
237   return x;
238 }