215b156b77ff201273d44ac8f6baee1e4413a73f
[tinc] / src / subnet.c
1 /*
2     subnet.c -- handle subnet lookups and lists
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: subnet.c,v 1.1.2.7 2000/10/28 21:05:20 guus Exp $
21 */
22
23 #include <syslog.h>
24
25 #include "config.h"
26 #include <utils.h>
27
28 #include <xalloc.h>
29 #include "subnet.h"
30 #include "net.h"
31 #include "conf.h"
32 #include "system.h"
33
34 /* lists type of subnet */
35
36 subnet_t *subnet_list[SUBNET_TYPES] = { NULL };
37
38 /* Allocating and freeing space for subnets */
39
40 subnet_t *new_subnet(void)
41 {
42 cp
43   return (subnet_t *)xmalloc(sizeof(subnet_t));
44 }
45
46 void free_subnet(subnet_t *subnet)
47 {
48 cp
49   free(subnet);
50 }
51
52 /* Linked list management */
53
54 void subnet_add(conn_list_t *cl, subnet_t *subnet)
55 {
56   subnet_t *p = NULL;
57   subnet_t *q = NULL;
58 cp
59   subnet->owner = cl;
60
61   /* Link it into the owners list of subnets (unsorted) */
62
63   subnet->next = cl->subnets;
64   subnet->prev = NULL;
65   if(subnet->next)
66     subnet->next->prev = subnet;
67   cl->subnets = subnet;
68   
69   /* And now add it to the global subnet list (sorted) */
70
71   /* Sort on size of subnet mask (IPv4 only at the moment!)
72   
73      Three cases: subnet_list[] = NULL -> just add this subnet
74                   insert before first -> add it in front of list
75                   rest: insert after another subnet
76    */
77 cp
78   if(subnet_list[subnet->type])
79     {
80       p = q = subnet_list[subnet->type];
81
82       for(; p; p = p->global_next)
83         {
84           if(subnet->net.ipv4.mask >= p->net.ipv4.mask)
85             break;
86
87           q = p;
88         }
89      }
90 cp  
91   if(p == subnet_list[subnet->type])    /* First two cases */
92     {
93       /* Insert in front */
94       subnet->global_next = subnet_list[subnet->type];
95       subnet->global_prev = NULL;
96       subnet_list[subnet->type] = subnet;
97     }
98   else                                  /* Third case */
99     {
100       /* Insert after q */
101       subnet->global_next = q->global_next;
102       subnet->global_prev = q;
103       q->global_next = subnet;
104     }
105 cp
106   if(subnet->global_next)
107     subnet->global_next->global_prev = subnet;
108 cp
109 }
110
111 void subnet_del(subnet_t *subnet)
112 {
113 cp
114   /* Remove it from owner's list */
115
116   if(subnet->prev)
117     {
118       subnet->prev->next = subnet->next;
119     }
120   else
121     {
122       subnet->owner->subnets = subnet->next;
123     }
124
125   subnet->next->prev = subnet->prev;
126
127   /* Remove it from the global list */
128   
129   if(subnet->global_prev)
130     {
131       subnet->global_prev->global_next = subnet->global_next;
132     }
133   else
134     {
135       subnet_list[subnet->type] = subnet->global_next;
136     }
137
138   subnet->global_next->global_prev = subnet->global_prev;
139   
140   free_subnet(subnet);
141 cp
142 }
143
144 /* Ascii representation of subnets */
145
146 subnet_t *str2net(char *subnetstr)
147 {
148   int type;
149   subnet_t *subnet;
150 cp
151   if(sscanf(subnetstr, "%d,", &type) != 1)
152     return NULL;
153 cp
154   subnet = new_subnet();
155 cp
156   switch(type)
157     {
158       case SUBNET_MAC:
159         if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
160                    &subnet->net.mac.address.x[0],
161                    &subnet->net.mac.address.x[1],
162                    &subnet->net.mac.address.x[2],
163                    &subnet->net.mac.address.x[3],
164                    &subnet->net.mac.address.x[4],
165                    &subnet->net.mac.address.x[5]) != 7)
166           {
167             free_subnet(subnet);
168             return NULL;
169           }
170         break;
171       case SUBNET_IPV4:
172         if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
173           {
174             free_subnet(subnet);
175             return NULL;
176           }
177         break;
178       case SUBNET_IPV6:
179         if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
180                    &subnet->net.ipv6.address.x[0],
181                    &subnet->net.ipv6.address.x[1],
182                    &subnet->net.ipv6.address.x[2],
183                    &subnet->net.ipv6.address.x[3],
184                    &subnet->net.ipv6.address.x[4],
185                    &subnet->net.ipv6.address.x[5],
186                    &subnet->net.ipv6.address.x[6],
187                    &subnet->net.ipv6.address.x[7],
188                    &subnet->net.ipv6.mask.x[0],
189                    &subnet->net.ipv6.mask.x[1],
190                    &subnet->net.ipv6.mask.x[2],
191                    &subnet->net.ipv6.mask.x[3],
192                    &subnet->net.ipv6.mask.x[4],
193                    &subnet->net.ipv6.mask.x[5],
194                    &subnet->net.ipv6.mask.x[6],
195                    &subnet->net.ipv6.mask.x[7]) != 17)
196           {
197             free_subnet(subnet);
198             return NULL;
199           }
200         break;
201       default:
202         free_subnet(subnet);
203         return NULL;
204     }
205 cp
206   return subnet;
207 }
208
209 char *net2str(subnet_t *subnet)
210 {
211   char *netstr;
212 cp
213   switch(subnet->type)
214     {
215       case SUBNET_MAC:
216         asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
217                    subnet->net.mac.address.x[0],
218                    subnet->net.mac.address.x[1],
219                    subnet->net.mac.address.x[2],
220                    subnet->net.mac.address.x[3],
221                    subnet->net.mac.address.x[4],
222                    subnet->net.mac.address.x[5]);
223         break;
224       case SUBNET_IPV4:
225         asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
226         break;
227       case SUBNET_IPV6:
228         asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
229                    subnet->net.ipv6.address.x[0],
230                    subnet->net.ipv6.address.x[1],
231                    subnet->net.ipv6.address.x[2],
232                    subnet->net.ipv6.address.x[3],
233                    subnet->net.ipv6.address.x[4],
234                    subnet->net.ipv6.address.x[5],
235                    subnet->net.ipv6.address.x[6],
236                    subnet->net.ipv6.address.x[7],
237                    subnet->net.ipv6.mask.x[0],
238                    subnet->net.ipv6.mask.x[1],
239                    subnet->net.ipv6.mask.x[2],
240                    subnet->net.ipv6.mask.x[3],
241                    subnet->net.ipv6.mask.x[4],
242                    subnet->net.ipv6.mask.x[5],
243                    subnet->net.ipv6.mask.x[6],
244                    subnet->net.ipv6.mask.x[7]);
245         break;
246       default:
247         asprintf(&netstr, _("unknown"));
248     }
249 cp
250   return netstr;
251 }
252
253 /* Subnet lookup routines */
254
255 subnet_t *lookup_subnet_mac(mac_t address)
256 {
257   subnet_t *subnet;
258 cp
259   for(subnet = subnet_list[SUBNET_MAC]; subnet != NULL; subnet = subnet->global_next)
260     {
261       if(memcmp(&address, &subnet->net.mac.address, sizeof(address)) == 0)
262         break;
263     }
264 cp
265   return subnet;
266 }
267
268 subnet_t *lookup_subnet_ipv4(ipv4_t address)
269 {
270   subnet_t *subnet;
271 cp
272   for(subnet = subnet_list[SUBNET_IPV4]; subnet != NULL; subnet = subnet->global_next)
273     {
274       if((address & subnet->net.ipv4.mask) == subnet->net.ipv4.address)
275         break;
276     }
277 cp
278   return subnet;
279 }
280
281 subnet_t *lookup_subnet_ipv6(ipv6_t address)
282 {
283   subnet_t *subnet;
284   int i;
285 cp
286   for(subnet = subnet_list[SUBNET_IPV6]; subnet != NULL; subnet = subnet->global_next)
287     {
288       for(i=0; i<8; i++)
289         if((address.x[i] & subnet->net.ipv6.mask.x[i]) != subnet->net.ipv6.address.x[i])
290           break;
291       if(i=8)
292         break;
293     }
294 cp
295   return subnet;
296 }
297
298 void dump_subnet_list(void)
299 {
300   subnet_t *subnet;
301   char *netstr;
302 cp
303   syslog(LOG_DEBUG, _("Subnet list:"));
304
305   for(subnet = subnet_list[SUBNET_IPV4]; subnet != NULL; subnet = subnet->global_next)
306     {
307       netstr = net2str(subnet);
308       syslog(LOG_DEBUG, "%s owner %s", netstr, subnet->owner->name);
309       free(netstr);
310     }
311
312   syslog(LOG_DEBUG, _("End of subnet list."));
313 cp
314 }