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