12a501f12dc32d4d5c3f60c5bf48c2a5c8874735
[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.1 2000/10/11 10:35:15 guus Exp $
21 */
22
23 #include "config.h"
24 #include <utils.h>
25
26 #include "connlist.h"
27
28 /* Root of the connection list */
29
30 conn_list_t *conn_list = NULL;
31 conn_list_t *myself = NULL;
32
33 /* Creation and deletion of conn_list elements */
34
35 conn_list_t *new_conn_list(void)
36 {
37   conn_list_t *p = xmalloc(sizeof(*p));
38 cp
39   /* initialise all those stupid pointers at once */
40   memset(p, '\0', sizeof(*p));
41 cp
42   return p;
43 }
44
45 void free_conn_list(conn_list_t *p)
46 {
47 cp
48   if(p->sq)
49     destroy_queue(p->sq);
50   if(p->rq)
51     destroy_queue(p->rq);
52   if(p->name)
53     free(p->name);
54   if(p->hostname)
55     free(p->hostname);
56   free_key(p->public_key);
57   free_key(p->datakey);
58   free(p);
59 cp
60 }
61
62 /*
63   remove all marked connections
64 */
65 void prune_conn_list(void)
66 {
67   conn_list_t *p, *prev = NULL, *next = NULL;
68 cp
69   for(p = conn_list; p != NULL; )
70     {
71       next = p->next;
72
73       if(p->status.remove)
74         {
75           if(prev)
76             prev->next = next;
77           else
78             conn_list = next;
79
80           free_conn_element(p);
81         }
82       else
83         prev = p;
84
85       p = next;
86     }
87 cp
88 }
89
90 /*
91   free all elements of conn_list
92 */
93 void destroy_conn_list(void)
94 {
95   conn_list_t *p, *next;
96 cp
97   for(p = conn_list; p != NULL; )
98     {
99       next = p->next;
100       free_conn_element(p);
101       p = next;
102     }
103
104   conn_list = NULL;
105 cp
106 }
107
108 /* Linked list management */
109
110 void conn_list_add(conn_list_t *cl)
111 {
112 cp
113   cl->next = connlist;
114   cl->prev = NULL;
115   cl->next->prev = cl;
116   connlist = cl;
117 cp
118 }
119
120 void conn_list_del(conn_list_t *cl)
121 {
122 cp
123   if(cl->prev)
124     cl->prev->next = cl->next;
125   else
126     connlist = cl->next;
127   
128   cl->next->prev = cl->prev;
129   free_conn_list(cl);
130 cp
131 }
132
133 /* Lookup functions */
134
135 conn_list_t *lookup_conn_list_mac(mac_t address)
136 {
137   conn_list_t *p;
138 cp
139   for(p = conn_list; p != NULL; p = p->next)
140     if(lookup_subnet_mac(p, address))
141       break;
142 cp
143   return p;
144 }
145
146 conn_list_t *lookup_conn_list_ipv4(ipv4_t address)
147 {
148   conn_list_t *p;
149 cp
150   for(p = conn_list; p != NULL; p = p->next)
151     if(lookup_subnet_ipv4(p, address))
152       break;
153 cp
154   return p;
155 }
156
157 conn_list_t *lookup_conn_list_ipv6(ipv6_t address)
158 {
159   conn_list_t *p;
160 cp
161   for(p = conn_list; p != NULL; p = p->next)
162     if(lookup_subnet_ipv6(p, address))
163       break;
164 cp
165   return p;
166 }
167
168 /* Debugging */
169
170 void dump_conn_list(void)
171 {
172   conn_list_t *p;
173 cp
174   syslog(LOG_DEBUG, _("Connection list:"));
175
176   for(p = conn_list; p != NULL; p = p->next)
177     {
178       syslog(LOG_DEBUG, _("%s netmask %d.%d.%d.%d at %s port %hd flags %d sockets %d, %d status %04x"),
179              p->name, IP_ADDR_V(p->vpn_mask), p->hostname, p->port, p->flags,
180              p->socket, p->meta_socket, p->status);
181     }
182 cp
183 }