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