]> git.meshlink.io Git - meshlink/blob - src/subnet.c
- Fixing little things
[meshlink] / 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.4 2000/10/15 00:59:37 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(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(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
91                    &subnet->net.mac.address.x[0],
92                    &subnet->net.mac.address.x[1],
93                    &subnet->net.mac.address.x[2],
94                    &subnet->net.mac.address.x[3],
95                    &subnet->net.mac.address.x[4],
96                    &subnet->net.mac.address.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(subnetstr, "%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     }
137 cp
138   return subnet;
139 }
140
141 char *net2str(subnet_t *subnet)
142 {
143   char *netstr;
144 cp
145   switch(subnet->type)
146     {
147       case SUBNET_MAC:
148         asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
149                    subnet->net.mac.address.x[0],
150                    subnet->net.mac.address.x[1],
151                    subnet->net.mac.address.x[2],
152                    subnet->net.mac.address.x[3],
153                    subnet->net.mac.address.x[4],
154                    subnet->net.mac.address.x[5]);
155       case SUBNET_IPV4:
156         asprintf(&netstr, "%d,%lx:%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
157       case SUBNET_IPV6:
158         asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
159                    subnet->net.ipv6.address.x[0],
160                    subnet->net.ipv6.address.x[1],
161                    subnet->net.ipv6.address.x[2],
162                    subnet->net.ipv6.address.x[3],
163                    subnet->net.ipv6.address.x[4],
164                    subnet->net.ipv6.address.x[5],
165                    subnet->net.ipv6.address.x[6],
166                    subnet->net.ipv6.address.x[7],
167                    subnet->net.ipv6.mask.x[0],
168                    subnet->net.ipv6.mask.x[1],
169                    subnet->net.ipv6.mask.x[2],
170                    subnet->net.ipv6.mask.x[3],
171                    subnet->net.ipv6.mask.x[4],
172                    subnet->net.ipv6.mask.x[5],
173                    subnet->net.ipv6.mask.x[6],
174                    subnet->net.ipv6.mask.x[7]);
175       default:
176         netstr = NULL;
177     }
178 cp
179   return netstr;
180 }
181
182 /* Subnet lookup routines */
183
184 subnet_t *lookup_subnet_mac(subnet_t *subnets, mac_t address)
185 {
186   subnet_t *subnet;
187 cp
188   for(subnet = subnets; subnet != NULL; subnet = subnet->next)
189     {
190       if(subnet->type == SUBNET_MAC)
191         if(memcmp(&address, &subnet->net.mac.address, sizeof(address)) == 0)
192           break;
193     }
194 cp
195   return subnet;
196 }
197
198 subnet_t *lookup_subnet_ipv4(subnet_t *subnets, ipv4_t address)
199 {
200   subnet_t *subnet;
201 cp
202   for(subnet = subnets; subnet != NULL; subnet = subnet->next)
203     {
204       if(subnet->type == SUBNET_IPV4)
205         if((address & subnet->net.ipv4.mask) == subnet->net.ipv4.address)
206           break;
207     }
208 cp
209   return subnet;
210 }
211
212 subnet_t *lookup_subnet_ipv6(subnet_t *subnets, ipv6_t address)
213 {
214   subnet_t *subnet;
215   int i;
216 cp
217   for(subnet = subnets; subnet != NULL; subnet = subnet->next)
218     {
219       if(subnet->type == SUBNET_IPV6)
220         {
221           for(i=0; i<8; i++)
222             if((address.x[i] & subnet->net.ipv6.mask.x[i]) != subnet->net.ipv6.address.x[i])
223               break;
224           if(i=8)
225             break;
226         }
227     }
228 cp
229   return subnet;
230 }