Big and bad commit of my current tree...
[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.2 2000/10/11 10:35:17 guus Exp $
21 */
22
23 #include "config.h"
24 #include <utils.h>
25
26 #include <xalloc.h>
27 #include "subnet.h"
28 #include "net.h"
29
30 /* Allocating and freeing space for subnets */
31
32 subnet_t *new_subnet(void)
33 {
34 cp
35   return (subnet_t *)xmalloc(sizeof(subnet_t));
36 }
37
38 void free_subnet(subnet_t *subnet)
39 {
40 cp
41   free(subnet);
42 }
43
44 /* Linked list management */
45
46 void subnet_add(conn_list_t *cl, subnet_t *subnet)
47 {
48 cp
49   /* FIXME: do sorting on netmask size if necessary */
50
51   subnet->next = cl->subnets->next;
52   subnet->prev = NULL;
53   subnet->next->prev = subnet;
54   cl->subnets = subnet;
55 cp
56 }
57
58 void subnet_del(conn_list_t *cl, subnet_t *subnet)
59 {
60 cp
61   if(subnet->prev)
62     {
63       subnet->prev->next = subnet->next;
64     }
65   else
66     {
67       subnet->owner->subnets = subnet->next;
68     }
69
70   subnet->next->prev = subnet->prev;
71   free_subnet(subnet);
72 cp
73 }
74
75 /* Ascii representation of subnets */
76
77 subnet_t *str2net(char *subnetstr)
78 {
79   int type;
80   subnet_t *subnet;
81 cp
82   if(sscanf(subnetstr, "%d,", &type) != 1)
83     return NULL;
84
85   subnet = new_subnet();
86
87   switch(type)
88     {
89       case SUBNET_MAC:
90         if(sscanf(netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
91                    &subnet->net.mac.x[0],
92                    &subnet->net.mac.x[1],
93                    &subnet->net.mac.x[2],
94                    &subnet->net.mac.x[3],
95                    &subnet->net.mac.x[4],
96                    &subnet->net.mac.x[5]) != 7)
97           {
98             free_subnet(subnet);
99             return NULL;
100           }
101         break;
102       case SUBNET_IPv4:
103         if(sscanf(subnetstr, "%d,%lx:%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
104           {
105             free_subnet(subnet);
106             return NULL;
107           }
108         break;
109       case SUBNET_IPv6:
110         if(sscanf(netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
111                    &subnet->net.ipv6.address.x[0],
112                    &subnet->net.ipv6.address.x[1],
113                    &subnet->net.ipv6.address.x[2],
114                    &subnet->net.ipv6.address.x[3],
115                    &subnet->net.ipv6.address.x[4],
116                    &subnet->net.ipv6.address.x[5],
117                    &subnet->net.ipv6.address.x[6],
118                    &subnet->net.ipv6.address.x[7],
119                    &subnet->net.ipv6.mask.x[0],
120                    &subnet->net.ipv6.mask.x[1],
121                    &subnet->net.ipv6.mask.x[2],
122                    &subnet->net.ipv6.mask.x[3],
123                    &subnet->net.ipv6.mask.x[4],
124                    &subnet->net.ipv6.mask.x[5],
125                    &subnet->net.ipv6.mask.x[6],
126                    &subnet->net.ipv6.mask.x[7]) != 17)
127           {
128             free_subnet(subnet);
129             return NULL;
130           }
131         break;
132                 break;
133       default:
134         free_subnet(subnet);
135         return NULL;
136 cp
137   return subnet;
138 }
139
140 char *net2str(subnet_t *subnet)
141 {
142   char *netstr;
143 cp
144   switch(subnet->type)
145     {
146       case SUBNET_MAC:
147         asprintf(netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
148                    subnet->net.mac.x[0],
149                    subnet->net.mac.x[1],
150                    subnet->net.mac.x[2],
151                    subnet->net.mac.x[3],
152                    subnet->net.mac.x[4],
153                    subnet->net.mac.x[5]);
154       case SUBNET_IPv4:
155         asprintf(netstr, "%d,%lx:%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
156       case SUBNET_IPv6:
157         asprintf(netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
158                    subnet->net.ipv6.address.x[0],
159                    subnet->net.ipv6.address.x[1],
160                    subnet->net.ipv6.address.x[2],
161                    subnet->net.ipv6.address.x[3],
162                    subnet->net.ipv6.address.x[4],
163                    subnet->net.ipv6.address.x[5],
164                    subnet->net.ipv6.address.x[6],
165                    subnet->net.ipv6.address.x[7],
166                    subnet->net.ipv6.mask.x[0],
167                    subnet->net.ipv6.mask.x[1],
168                    subnet->net.ipv6.mask.x[2],
169                    subnet->net.ipv6.mask.x[3],
170                    subnet->net.ipv6.mask.x[4],
171                    subnet->net.ipv6.mask.x[5],
172                    subnet->net.ipv6.mask.x[6],
173                    subnet->net.ipv6.mask.x[7]);
174       default:
175         netstr = NULL;
176     }
177 cp
178   return netstr;
179 }
180
181 /* Subnet lookup routines */
182
183 subnet_t *lookup_subnet_mac(subnet_t *subnets, mac_t address)
184 {
185   subnet_t *subnet;
186 cp
187   for(subnet = subnets; subnet != NULL; subnet = subnet->next)
188     {
189       if(subnet->type == SUBNET_MAC)
190         if(memcmp(&address, &subnet->net.mac.address, sizeof(address)) == 0)
191           break;
192     }
193 cp
194   return subnet;
195 }
196
197 subnet_t *lookup_subnet_ipv4(subnet_t *subnets, ipv4_t address)
198 {
199   subnet_t *subnet;
200 cp
201   for(subnet = subnets; subnet != NULL; subnet = subnet->next)
202     {
203       if(subnet->type == SUBNET_IPV4)
204         if((address & subnet->net.ipv4.mask) == subnet->net.ipv4.address)
205           break;
206     }
207 cp
208   return subnet;
209 }
210
211 subnet_t *lookup_subnet_ipv6(subnet_t *subnets, ipv6_t address)
212 {
213   subnet_t *subnet;
214 cp
215   for(subnet = subnets; subnet != NULL; subnet = subnet->next)
216     {
217       if(subnet->type == SUBNET_IPV6)
218         {
219           for(i=0; i<8; i++)
220             if((address.x[i] & subnet->net.ipv6.mask.x[i]) != subnet->net.ipv6.address.x[i])
221               break;
222           if(i=8)
223             break;
224         }
225     }
226 cp
227   return subnet;
228 }