inet.c revision 1.11 1 /* $NetBSD: inet.c,v 1.11 2003/05/16 22:59:50 dsl Exp $ */
2
3 /*
4 * The mrouted program is covered by the license in the accompanying file
5 * named "LICENSE". Use of the mrouted program represents acceptance of
6 * the terms and conditions listed in that file.
7 *
8 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
9 * Leland Stanford Junior University.
10 */
11
12
13 #include "defs.h"
14
15
16 /* buffers to hold the string representations */
17 /* of IP addresses, returned by inet_fmt{s}() */
18 #define SS_MASK ((1 << 3) - 1)
19 static char ss[SS_MASK + 1][32];
20 static int ss_index = 0; /* index into above */
21
22
23 /*
24 * Verify that a given IP address is credible as a host address.
25 * (Without a mask, cannot detect addresses of the form {subnet,0} or
26 * {subnet,-1}.)
27 */
28 int
29 inet_valid_host(u_int32_t naddr)
30 {
31 u_int32_t addr;
32
33 addr = ntohl(naddr);
34
35 return (!(IN_MULTICAST(addr) ||
36 IN_BADCLASS (addr) ||
37 (addr & 0xff000000) == 0));
38 }
39
40 /*
41 * Verify that a given netmask is plausible;
42 * make sure that it is a series of 1's followed by
43 * a series of 0's with no discontiguous 1's.
44 */
45 int
46 inet_valid_mask(u_int32_t mask)
47 {
48 if (~(((mask & -mask) - 1) | mask) != 0) {
49 /* Mask is not contiguous */
50 return (FALSE);
51 }
52
53 return (TRUE);
54 }
55
56 /*
57 * Verify that a given subnet number and mask pair are credible.
58 *
59 * With CIDR, almost any subnet and mask are credible. mrouted still
60 * can't handle aggregated class A's, so we still check that, but
61 * otherwise the only requirements are that the subnet address is
62 * within the [ABC] range and that the host bits of the subnet
63 * are all 0.
64 */
65 int
66 inet_valid_subnet(u_int32_t nsubnet, u_int32_t nmask)
67 {
68 u_int32_t subnet, mask;
69
70 subnet = ntohl(nsubnet);
71 mask = ntohl(nmask);
72
73 if ((subnet & mask) != subnet) return (FALSE);
74
75 if (subnet == 0)
76 return (mask == 0);
77
78 if (IN_CLASSA(subnet)) {
79 if (mask < 0xff000000 ||
80 (subnet & 0xff000000) == 0x7f000000 ||
81 (subnet & 0xff000000) == 0x00000000) return (FALSE);
82 }
83 else if (IN_CLASSD(subnet) || IN_BADCLASS(subnet)) {
84 /* Above Class C address space */
85 return (FALSE);
86 }
87 if (subnet & ~mask) {
88 /* Host bits are set in the subnet */
89 return (FALSE);
90 }
91 if (!inet_valid_mask(mask)) {
92 /* Netmask is not contiguous */
93 return (FALSE);
94 }
95
96 return (TRUE);
97 }
98
99
100 /*
101 * Convert an IP address in u_long (network) format into a printable string.
102 */
103 char *
104 inet_fmt(u_int32_t addr)
105 {
106 u_char *a;
107 char *s = ss[++ss_index & SS_MASK];
108
109 a = (u_char *)&addr;
110 snprintf(s, sizeof ss[0], "%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
111 return (s);
112 }
113
114
115 /*
116 * Convert an IP subnet number in u_long (network) format into a printable
117 * string including the netmask as a number of bits.
118 */
119 char *
120 inet_fmts(u_int32_t addr, u_int32_t mask)
121 {
122 u_char *a, *m;
123 int bits;
124 char *s = ss[++ss_index & SS_MASK];
125
126 if ((addr == 0) && (mask == 0)) {
127 snprintf(s, sizeof ss[0], "default");
128 return (s);
129 }
130 a = (u_char *)&addr;
131 m = (u_char *)&mask;
132 bits = 33 - ffs(ntohl(mask));
133
134 if (m[3] != 0) snprintf(s, sizeof ss[0], "%u.%u.%u.%u/%d", a[0], a[1], a[2], a[3],
135 bits);
136 else if (m[2] != 0) snprintf(s, sizeof ss[0], "%u.%u.%u/%d", a[0], a[1], a[2], bits);
137 else if (m[1] != 0) snprintf(s, sizeof ss[0], "%u.%u/%d", a[0], a[1], bits);
138 else snprintf(s, sizeof ss[0], "%u/%d", a[0], bits);
139
140 return (s);
141 }
142
143 /*
144 * Convert the printable string representation of an IP address into the
145 * u_long (network) format. Return 0xffffffff on error. (To detect the
146 * legal address with that value, you must explicitly compare the string
147 * with "255.255.255.255".)
148 */
149 u_int32_t
150 inet_parse(char *s, int *mask_p)
151 {
152 u_int32_t a = 0;
153 u_int a0, a1, a2, a3;
154 char c;
155 int mask = 0;
156 int n;
157
158 n = sscanf(s, "%u.%u.%u.%u/%d%c", &a0, &a1, &a2, &a3, &mask, &c);
159 if (n < 4 || (n == 4 && mask_p) || (n == 5 && !mask_p) || n >= 6)
160 return (0xffffffff);
161
162 if (a0 > 255 || a1 > 255 || a2 > 255 || a3 > 255)
163 return (0xffffffff);
164
165 ((u_char *)&a)[0] = a0;
166 ((u_char *)&a)[1] = a1;
167 ((u_char *)&a)[2] = a2;
168 ((u_char *)&a)[3] = a3;
169
170 if (mask_p)
171 *mask_p = mask;
172
173 return (a);
174 }
175
176
177 /*
178 * inet_cksum extracted from:
179 * P I N G . C
180 *
181 * Author -
182 * Mike Muuss
183 * U. S. Army Ballistic Research Laboratory
184 * December, 1983
185 * Modified at Uc Berkeley
186 *
187 * (ping.c) Status -
188 * Public Domain. Distribution Unlimited.
189 *
190 * I N _ C K S U M
191 *
192 * Checksum routine for Internet Protocol family headers (C Version)
193 *
194 */
195 int
196 inet_cksum(u_int16_t *addr, u_int len)
197 {
198 int nleft = (int)len;
199 u_int16_t *w = addr;
200 int32_t sum = 0;
201 union {
202 u_int16_t w;
203 u_int8_t b[2];
204 } answer;
205
206 /*
207 * Our algorithm is simple, using a 32 bit accumulator (sum),
208 * we add sequential 16 bit words to it, and at the end, fold
209 * back all the carry bits from the top 16 bits into the lower
210 * 16 bits.
211 */
212 while (nleft > 1) {
213 sum += *w++;
214 nleft -= 2;
215 }
216
217 /* mop up an odd byte, if necessary */
218 if (nleft == 1) {
219 answer.w = 0;
220 answer.b[0] = *(u_char *)w ;
221 sum += answer.w;
222 }
223
224 /*
225 * add back carry outs from top 16 bits to low 16 bits
226 */
227 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
228 sum += (sum >> 16); /* add carry */
229 answer.w = ~sum; /* truncate to 16 bits */
230 return (answer.w);
231 }
232