No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / mrouted / inet.c
blob6ef01915bf1c26b9d2ded3088b102d1abb8fc5c8
1 /* $NetBSD: inet.c,v 1.11 2003/05/16 22:59:50 dsl Exp $ */
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.
8 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
9 * Leland Stanford Junior University.
13 #include "defs.h"
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 */
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}.)
28 int
29 inet_valid_host(u_int32_t naddr)
31 u_int32_t addr;
33 addr = ntohl(naddr);
35 return (!(IN_MULTICAST(addr) ||
36 IN_BADCLASS (addr) ||
37 (addr & 0xff000000) == 0));
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.
45 int
46 inet_valid_mask(u_int32_t mask)
48 if (~(((mask & -mask) - 1) | mask) != 0) {
49 /* Mask is not contiguous */
50 return (FALSE);
53 return (TRUE);
57 * Verify that a given subnet number and mask pair are credible.
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.
65 int
66 inet_valid_subnet(u_int32_t nsubnet, u_int32_t nmask)
68 u_int32_t subnet, mask;
70 subnet = ntohl(nsubnet);
71 mask = ntohl(nmask);
73 if ((subnet & mask) != subnet) return (FALSE);
75 if (subnet == 0)
76 return (mask == 0);
78 if (IN_CLASSA(subnet)) {
79 if (mask < 0xff000000 ||
80 (subnet & 0xff000000) == 0x7f000000 ||
81 (subnet & 0xff000000) == 0x00000000) return (FALSE);
83 else if (IN_CLASSD(subnet) || IN_BADCLASS(subnet)) {
84 /* Above Class C address space */
85 return (FALSE);
87 if (subnet & ~mask) {
88 /* Host bits are set in the subnet */
89 return (FALSE);
91 if (!inet_valid_mask(mask)) {
92 /* Netmask is not contiguous */
93 return (FALSE);
96 return (TRUE);
101 * Convert an IP address in u_long (network) format into a printable string.
103 char *
104 inet_fmt(u_int32_t addr)
106 u_char *a;
107 char *s = ss[++ss_index & SS_MASK];
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);
116 * Convert an IP subnet number in u_long (network) format into a printable
117 * string including the netmask as a number of bits.
119 char *
120 inet_fmts(u_int32_t addr, u_int32_t mask)
122 u_char *a, *m;
123 int bits;
124 char *s = ss[++ss_index & SS_MASK];
126 if ((addr == 0) && (mask == 0)) {
127 snprintf(s, sizeof ss[0], "default");
128 return (s);
130 a = (u_char *)&addr;
131 m = (u_char *)&mask;
132 bits = 33 - ffs(ntohl(mask));
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);
140 return (s);
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".)
149 u_int32_t
150 inet_parse(char *s, int *mask_p)
152 u_int32_t a = 0;
153 u_int a0, a1, a2, a3;
154 char c;
155 int n;
157 if (sscanf(s, "%u.%u.%u.%u%n", &a0, &a1, &a2, &a3, &n) != 4)
158 return 0xffffffff;
159 if (a0 > 255 || a1 > 255 || a2 > 255 || a3 > 255)
160 return 0xffffffff;
162 if (mask_p == 0) {
163 if (s[n] != 0)
164 return 0xffffffff;
165 } else {
166 if (sscanf(s + n, "/%u%c", &n, &c) != 1 || n > 32)
167 return 0xffffffff;
168 *mask_p = n;
171 ((u_char *)&a)[0] = a0;
172 ((u_char *)&a)[1] = a1;
173 ((u_char *)&a)[2] = a2;
174 ((u_char *)&a)[3] = a3;
176 return (a);
181 * inet_cksum extracted from:
182 * P I N G . C
184 * Author -
185 * Mike Muuss
186 * U. S. Army Ballistic Research Laboratory
187 * December, 1983
188 * Modified at Uc Berkeley
190 * (ping.c) Status -
191 * Public Domain. Distribution Unlimited.
193 * I N _ C K S U M
195 * Checksum routine for Internet Protocol family headers (C Version)
199 inet_cksum(u_int16_t *addr, u_int len)
201 int nleft = (int)len;
202 u_int16_t *w = addr;
203 int32_t sum = 0;
204 union {
205 u_int16_t w;
206 u_int8_t b[2];
207 } answer;
210 * Our algorithm is simple, using a 32 bit accumulator (sum),
211 * we add sequential 16 bit words to it, and at the end, fold
212 * back all the carry bits from the top 16 bits into the lower
213 * 16 bits.
215 while (nleft > 1) {
216 sum += *w++;
217 nleft -= 2;
220 /* mop up an odd byte, if necessary */
221 if (nleft == 1) {
222 answer.w = 0;
223 answer.b[0] = *(u_char *)w ;
224 sum += answer.w;
228 * add back carry outs from top 16 bits to low 16 bits
230 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
231 sum += (sum >> 16); /* add carry */
232 answer.w = ~sum; /* truncate to 16 bits */
233 return (answer.w);