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