2 * The mrouted program is covered by the license in the accompanying file
3 * named "LICENSE". Use of the mrouted program represents acceptance of
4 * the terms and conditions listed in that file.
6 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
7 * Leland Stanford Junior University.
10 * inet.c,v 3.8.4.2 1998/01/06 01:57:44 fenner Exp
14 static const char rcsid[] =
23 char s1[19]; /* buffers to hold the string representations */
24 char s2[19]; /* of IP addresses, to be passed to inet_fmt() */
25 char s3[19]; /* or inet_fmts(). */
30 * Verify that a given IP address is credible as a host address.
31 * (Without a mask, cannot detect addresses of the form {subnet,0} or
35 inet_valid_host(naddr)
38 register u_int32 addr;
42 return (!(IN_MULTICAST(addr) ||
44 (addr & 0xff000000) == 0));
48 * Verify that a given netmask is plausible;
49 * make sure that it is a series of 1's followed by
50 * a series of 0's with no discontiguous 1's.
56 if (~(((mask & -mask) - 1) | mask) != 0) {
57 /* Mask is not contiguous */
65 * Verify that a given subnet number and mask pair are credible.
67 * With CIDR, almost any subnet and mask are credible. mrouted still
68 * can't handle aggregated class A's, so we still check that, but
69 * otherwise the only requirements are that the subnet address is
70 * within the [ABC] range and that the host bits of the subnet
74 inet_valid_subnet(nsubnet, nmask)
75 u_int32 nsubnet, nmask;
77 register u_int32 subnet, mask;
79 subnet = ntohl(nsubnet);
82 if ((subnet & mask) != subnet) return (FALSE);
87 if (IN_CLASSA(subnet)) {
88 if (mask < 0xff000000 ||
89 (subnet & 0xff000000) == 0x7f000000 ||
90 (subnet & 0xff000000) == 0x00000000) return (FALSE);
92 else if (IN_CLASSD(subnet) || IN_BADCLASS(subnet)) {
93 /* Above Class C address space */
97 /* Host bits are set in the subnet */
100 if (!inet_valid_mask(mask)) {
101 /* Netmask is not contiguous */
110 * Convert an IP address in u_long (network) format into a printable string.
120 sprintf(s, "%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
126 * Convert an IP subnet number in u_long (network) format into a printable
127 * string including the netmask as a number of bits.
130 inet_fmts(addr, mask, s)
134 register u_char *a, *m;
137 if ((addr == 0) && (mask == 0)) {
138 sprintf(s, "default");
143 bits = 33 - ffs(ntohl(mask));
145 if (m[3] != 0) sprintf(s, "%u.%u.%u.%u/%d", a[0], a[1], a[2], a[3],
147 else if (m[2] != 0) sprintf(s, "%u.%u.%u/%d", a[0], a[1], a[2], bits);
148 else if (m[1] != 0) sprintf(s, "%u.%u/%d", a[0], a[1], bits);
149 else sprintf(s, "%u/%d", a[0], bits);
155 * Convert the printable string representation of an IP address into the
156 * u_long (network) format. Return 0xffffffff on error. (To detect the
157 * legal address with that value, you must explicitly compare the string
158 * with "255.255.255.255".)
166 u_int a0 = 0, a1 = 0, a2 = 0, a3 = 0;
170 i = sscanf(s, "%u.%u.%u.%u%c", &a0, &a1, &a2, &a3, &c);
171 if (i < n || i > 4 || a0 > 255 || a1 > 255 || a2 > 255 || a3 > 255)
174 ((u_char *)&a)[0] = a0;
175 ((u_char *)&a)[1] = a1;
176 ((u_char *)&a)[2] = a2;
177 ((u_char *)&a)[3] = a3;
184 * inet_cksum extracted from:
189 * U. S. Army Ballistic Research Laboratory
191 * Modified at Uc Berkeley
194 * Public Domain. Distribution Unlimited.
198 * Checksum routine for Internet Protocol family headers (C Version)
202 inet_cksum(addr, len)
206 register int nleft = (int)len;
207 register u_short *w = addr;
209 register int sum = 0;
212 * Our algorithm is simple, using a 32 bit accumulator (sum),
213 * we add sequential 16 bit words to it, and at the end, fold
214 * back all the carry bits from the top 16 bits into the lower
222 /* mop up an odd byte, if necessary */
224 *(u_char *) (&answer) = *(u_char *)w ;
229 * add back carry outs from top 16 bits to low 16 bits
231 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
232 sum += (sum >> 16); /* add carry */
233 answer = ~sum; /* truncate to 16 bits */