f1b0eba0aeca1a2b4796d3c71ac2dd8528cb20c2
[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.3 2000/10/14 17:04:13 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)
57     free(p->name);
58   if(p->hostname)
59     free(p->hostname);
60   if(p->public_key)
61     RSA_free(p->public_key);
62   if(p->cipher_pktkey)
63     free(p->cipher_pktkey);
64   free(p);
65 cp
66 }
67
68 /*
69   remove all marked connections
70 */
71 void prune_conn_list(void)
72 {
73   conn_list_t *p, *prev = NULL, *next = NULL;
74 cp
75   for(p = conn_list; p != NULL; )
76     {
77       next = p->next;
78
79       if(p->status.remove)
80         {
81           if(prev)
82             prev->next = next;
83           else
84             conn_list = next;
85
86           free_conn_list(p);
87         }
88       else
89         prev = p;
90
91       p = next;
92     }
93 cp
94 }
95
96 /*
97   free all elements of conn_list
98 */
99 void destroy_conn_list(void)
100 {
101   conn_list_t *p, *next;
102 cp
103   for(p = conn_list; p != NULL; )
104     {
105       next = p->next;
106       free_conn_list(p);
107       p = next;
108     }
109
110   conn_list = NULL;
111 cp
112 }
113
114 /* Linked list management */
115
116 void conn_list_add(conn_list_t *cl)
117 {
118 cp
119   cl->next = conn_list;
120   cl->prev = NULL;
121   cl->next->prev = cl;
122   conn_list = cl;
123 cp
124 }
125
126 void conn_list_del(conn_list_t *cl)
127 {
128 cp
129   if(cl->prev)
130     cl->prev->next = cl->next;
131   else
132     conn_list = cl->next;
133   
134   cl->next->prev = cl->prev;
135   free_conn_list(cl);
136 cp
137 }
138
139 /* Lookup functions */
140
141 conn_list_t *lookup_id(char *name)
142 {
143   conn_list_t *p;
144 cp
145   for(p = conn_list; p != NULL; p = p->next)
146     if(strcmp(name, p->name) == 0)
147       break;
148 cp
149   return p;
150 }
151
152 conn_list_t *lookup_conn_list_mac(mac_t address)
153 {
154   conn_list_t *p;
155 cp
156   for(p = conn_list; p != NULL; p = p->next)
157     if(lookup_subnet_mac(p->subnets, address))
158       break;
159 cp
160   return p;
161 }
162
163 conn_list_t *lookup_conn_list_ipv4(ipv4_t address)
164 {
165   conn_list_t *p;
166 cp
167   for(p = conn_list; p != NULL; p = p->next)
168     if(lookup_subnet_ipv4(p->subnets, address))
169       break;
170 cp
171   return p;
172 }
173
174 conn_list_t *lookup_conn_list_ipv6(ipv6_t address)
175 {
176   conn_list_t *p;
177 cp
178   for(p = conn_list; p != NULL; p = p->next)
179     if(lookup_subnet_ipv6(p->subnets, address))
180       break;
181 cp
182   return p;
183 }
184
185 /* Debugging */
186
187 void dump_conn_list(void)
188 {
189   conn_list_t *p;
190   subnet_t *s;
191   char *netstr;
192 cp
193   syslog(LOG_DEBUG, _("Connection list:"));
194
195   for(p = conn_list; p != NULL; p = p->next)
196     {
197       syslog(LOG_DEBUG, _("%s at %s port %hd flags %d sockets %d, %d status %04x"),
198              p->name, p->hostname, p->port, p->flags,
199              p->socket, p->meta_socket, p->status);
200       for(s = p->subnets; s != NULL; s = s->next)
201         {
202           netstr = net2str(s);
203           syslog(LOG_DEBUG, ": %s", netstr);
204           free(netstr);
205         }
206     }
207
208   syslog(LOG_DEBUG, _("End of connection list."));
209 cp
210 }
211
212 int read_host_config(conn_list_t *cl)
213 {
214   char *fname;
215   int x;
216 cp
217   asprintf(fname, "%s/hosts/%s", confbase, cl->name);
218   x = read_config_file(&cl->config, fname);
219   free(fname);
220 cp
221   return x;
222 }