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