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