]> git.ipfire.org Git - thirdparty/strongswan.git/blob - linux/lib/libfreeswan/rangetosubnet.c
- import of strongswan-2.7.0
[thirdparty/strongswan.git] / linux / lib / libfreeswan / rangetosubnet.c
1 /*
2 * express an address range as a subnet (if possible)
3 * Copyright (C) 2000 Henry Spencer.
4 *
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Library General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 * License for more details.
14 *
15 * RCSID $Id: rangetosubnet.c,v 1.1 2004/03/15 20:35:26 as Exp $
16 */
17 #include "internal.h"
18 #include "freeswan.h"
19
20 /*
21 - rangetosubnet - turn an address range into a subnet, if possible
22 *
23 * A range which is a valid subnet will have a network part which is the
24 * same in the from value and the to value, followed by a host part which
25 * is all 0 in the from value and all 1 in the to value.
26 */
27 err_t
28 rangetosubnet(from, to, dst)
29 const ip_address *from;
30 const ip_address *to;
31 ip_subnet *dst;
32 {
33 unsigned const char *fp;
34 unsigned const char *tp;
35 unsigned fb;
36 unsigned tb;
37 unsigned const char *f;
38 unsigned const char *t;
39 size_t n;
40 size_t n2;
41 int i;
42 int nnet;
43 unsigned m;
44
45 if (addrtypeof(from) != addrtypeof(to))
46 return "mismatched address types";
47 n = addrbytesptr(from, &fp);
48 if (n == 0)
49 return "unknown address type";
50 n2 = addrbytesptr(to, &tp);
51 if (n != n2)
52 return "internal size mismatch in rangetosubnet";
53
54 f = fp;
55 t = tp;
56 nnet = 0;
57 for (i = n; i > 0 && *f == *t; i--, f++, t++)
58 nnet += 8;
59 if (i > 0 && !(*f == 0x00 && *t == 0xff)) { /* mid-byte bdry. */
60 fb = *f++;
61 tb = *t++;
62 i--;
63 m = 0x80;
64 while ((fb&m) == (tb&m)) {
65 fb &= ~m;
66 tb |= m;
67 m >>= 1;
68 nnet++;
69 }
70 if (fb != 0x00 || tb != 0xff)
71 return "not a valid subnet";
72 }
73 for (; i > 0 && *f == 0x00 && *t == 0xff; i--, f++, t++)
74 continue;
75
76 if (i != 0)
77 return "invalid subnet";
78
79 return initsubnet(from, nnet, 'x', dst);
80 }
81
82
83
84 #ifdef RANGETOSUBNET_MAIN
85
86 #include <stdio.h>
87
88 void regress(void);
89
90 int
91 main(int argc, char *argv[])
92 {
93 ip_address start;
94 ip_address stop;
95 ip_subnet sub;
96 char buf[100];
97 const char *oops;
98 size_t n;
99 int af;
100 int i;
101
102 if (argc == 2 && strcmp(argv[1], "-r") == 0) {
103 regress();
104 fprintf(stderr, "regress() returned?!?\n");
105 exit(1);
106 }
107
108 if (argc < 3) {
109 fprintf(stderr, "Usage: %s [-6] start stop\n", argv[0]);
110 fprintf(stderr, " or: %s -r\n", argv[0]);
111 exit(2);
112 }
113
114 af = AF_INET;
115 i = 1;
116 if (strcmp(argv[i], "-6") == 0) {
117 af = AF_INET6;
118 i++;
119 }
120
121 oops = ttoaddr(argv[i], 0, af, &start);
122 if (oops != NULL) {
123 fprintf(stderr, "%s: start conversion failed: %s\n", argv[0], oops);
124 exit(1);
125 }
126 oops = ttoaddr(argv[i+1], 0, af, &stop);
127 if (oops != NULL) {
128 fprintf(stderr, "%s: stop conversion failed: %s\n", argv[0], oops);
129 exit(1);
130 }
131 oops = rangetosubnet(&start, &stop, &sub);
132 if (oops != NULL) {
133 fprintf(stderr, "%s: rangetosubnet failed: %s\n", argv[0], oops);
134 exit(1);
135 }
136 n = subnettot(&sub, 0, buf, sizeof(buf));
137 if (n > sizeof(buf)) {
138 fprintf(stderr, "%s: reverse conversion", argv[0]);
139 fprintf(stderr, " failed: need %ld bytes, have only %ld\n",
140 (long)n, (long)sizeof(buf));
141 exit(1);
142 }
143 printf("%s\n", buf);
144
145 exit(0);
146 }
147
148 struct rtab {
149 int family;
150 char *start;
151 char *stop;
152 char *output; /* NULL means error expected */
153 } rtab[] = {
154 {4, "1.2.3.0", "1.2.3.255", "1.2.3.0/24"},
155 {4, "1.2.3.0", "1.2.3.7", "1.2.3.0/29"},
156 {4, "1.2.3.240", "1.2.3.255", "1.2.3.240/28"},
157 {4, "0.0.0.0", "255.255.255.255", "0.0.0.0/0"},
158 {4, "1.2.3.4", "1.2.3.4", "1.2.3.4/32"},
159 {4, "1.2.3.0", "1.2.3.254", NULL},
160 {4, "1.2.3.0", "1.2.3.126", NULL},
161 {4, "1.2.3.0", "1.2.3.125", NULL},
162 {4, "1.2.0.0", "1.2.255.255", "1.2.0.0/16"},
163 {4, "1.2.0.0", "1.2.0.255", "1.2.0.0/24"},
164 {4, "1.2.255.0", "1.2.255.255", "1.2.255.0/24"},
165 {4, "1.2.255.0", "1.2.254.255", NULL},
166 {4, "1.2.255.1", "1.2.255.255", NULL},
167 {4, "1.2.0.1", "1.2.255.255", NULL},
168 {6, "1:2:3:4:5:6:7:0", "1:2:3:4:5:6:7:ffff", "1:2:3:4:5:6:7:0/112"},
169 {6, "1:2:3:4:5:6:7:0", "1:2:3:4:5:6:7:fff", "1:2:3:4:5:6:7:0/116"},
170 {6, "1:2:3:4:5:6:7:f0", "1:2:3:4:5:6:7:ff", "1:2:3:4:5:6:7:f0/124"},
171 {4, NULL, NULL, NULL},
172 };
173
174 void
175 regress()
176 {
177 struct rtab *r;
178 int status = 0;
179 ip_address start;
180 ip_address stop;
181 ip_subnet sub;
182 char buf[100];
183 const char *oops;
184 size_t n;
185 int af;
186
187 for (r = rtab; r->start != NULL; r++) {
188 af = (r->family == 4) ? AF_INET : AF_INET6;
189 oops = ttoaddr(r->start, 0, af, &start);
190 if (oops != NULL) {
191 printf("surprise failure converting `%s'\n", r->start);
192 exit(1);
193 }
194 oops = ttoaddr(r->stop, 0, af, &stop);
195 if (oops != NULL) {
196 printf("surprise failure converting `%s'\n", r->stop);
197 exit(1);
198 }
199 oops = rangetosubnet(&start, &stop, &sub);
200 if (oops != NULL && r->output == NULL)
201 {} /* okay, error expected */
202 else if (oops != NULL) {
203 printf("`%s'-`%s' rangetosubnet failed: %s\n",
204 r->start, r->stop, oops);
205 status = 1;
206 } else if (r->output == NULL) {
207 printf("`%s'-`%s' rangetosubnet succeeded unexpectedly\n",
208 r->start, r->stop);
209 status = 1;
210 } else {
211 n = subnettot(&sub, 0, buf, sizeof(buf));
212 if (n > sizeof(buf)) {
213 printf("`%s'-`%s' subnettot failed: need %ld\n",
214 r->start, r->stop, (long)n);
215 status = 1;
216 } else if (strcmp(r->output, buf) != 0) {
217 printf("`%s'-`%s' gave `%s', expected `%s'\n",
218 r->start, r->stop, buf, r->output);
219 status = 1;
220 }
221 }
222 }
223 exit(status);
224 }
225
226 #endif /* RANGETOSUBNET_MAIN */