2965ced20a726180277eef5a2cb4030e1c17a9a5
[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.10 2000/10/29 00:02:17 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 "xalloc.h"
31 #include "system.h"
32
33 /* Root of the connection list */
34
35 conn_list_t *conn_list = NULL;
36 conn_list_t *myself = NULL;
37
38 /* Creation and deletion of conn_list elements */
39
40 conn_list_t *new_conn_list(void)
41 {
42   conn_list_t *p = (conn_list_t *)xmalloc(sizeof(*p));
43 cp
44   /* initialise all those stupid pointers at once */
45   memset(p, '\0', sizeof(*p));
46 cp
47   return p;
48 }
49
50 void free_conn_list(conn_list_t *p)
51 {
52 cp
53   if(p->sq)
54     destroy_queue(p->sq);
55   if(p->rq)
56     destroy_queue(p->rq);
57   if(p->name && p->name!=unknown)
58     free(p->name);
59   if(p->hostname)
60     free(p->hostname);
61   if(p->rsa_key)
62     RSA_free(p->rsa_key);
63   if(p->cipher_pktkey)
64     free(p->cipher_pktkey);
65   if(p->buffer)
66     free(p->buffer);
67   free(p);
68 cp
69 }
70
71 /*
72   remove all marked connections
73 */
74 void prune_conn_list(void)
75 {
76   conn_list_t *p, *prev = NULL, *next = NULL;
77 cp
78   for(p = conn_list; p != NULL; )
79     {
80       next = p->next;
81
82       if(p->status.remove)
83         conn_list_del(p);
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_list(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
118   if(cl->next)
119     cl->next->prev = cl;
120
121   conn_list = cl;
122 cp
123 }
124
125 void conn_list_del(conn_list_t *cl)
126 {
127 cp
128   if(cl->prev)
129     cl->prev->next = cl->next;
130   else
131     conn_list = cl->next;
132   
133   if(cl->next)
134     cl->next->prev = cl->prev;
135
136   free_conn_list(cl);
137 cp
138 }
139
140 /* Lookup functions */
141
142 conn_list_t *lookup_id(char *name)
143 {
144   conn_list_t *p;
145 cp
146   for(p = conn_list; p != NULL; p = p->next)
147     if(p->status.active)
148       if(strcmp(name, p->name) == 0)
149         break;
150 cp
151   return p;
152 }
153
154 /* Debugging */
155
156 void dump_conn_list(void)
157 {
158   conn_list_t *p;
159   subnet_t *s;
160   char *netstr;
161 cp
162   syslog(LOG_DEBUG, _("Connection list:"));
163
164   syslog(LOG_DEBUG, _("%s at %s port %hd flags %d sockets %d, %d status %04x"),
165          myself->name, myself->hostname, myself->port, myself->flags,
166          myself->socket, myself->meta_socket, myself->status);
167
168   for(s = myself->subnets; s != NULL; s = s->next)
169     {
170       netstr = net2str(s);
171       syslog(LOG_DEBUG, "  %s", netstr);
172       free(netstr);
173     }
174
175   for(p = conn_list; p != NULL; p = p->next)
176     {
177       syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
178              p->name, p->hostname, p->port, p->flags,
179              p->socket, p->meta_socket, p->status);
180
181       for(s = p->subnets; s != NULL; s = s->next)
182         {
183           netstr = net2str(s);
184           syslog(LOG_DEBUG, "  %s", netstr);
185           free(netstr);
186         }
187     }
188
189   syslog(LOG_DEBUG, _("End of connection list."));
190 cp
191 }
192
193 int read_host_config(conn_list_t *cl)
194 {
195   char *fname;
196   int x;
197 cp
198   asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
199   x = read_config_file(&cl->config, fname);
200   free(fname);
201 cp
202   return x;
203 }