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
12 * $FreeBSD: src/usr.sbin/mrouted/inet.c,v 1.11 1999/08/28 01:17:04 peter Exp $
13 * $DragonFly: src/usr.sbin/mrouted/inet.c,v 1.2 2003/06/17 04:29:57 dillon Exp $
21 char s1[19]; /* buffers to hold the string representations */
22 char s2[19]; /* of IP addresses, to be passed to inet_fmt() */
23 char s3[19]; /* or inet_fmts(). */
28 * Verify that a given IP address is credible as a host address.
29 * (Without a mask, cannot detect addresses of the form {subnet,0} or
33 inet_valid_host(naddr)
36 register u_int32 addr;
40 return (!(IN_MULTICAST(addr) ||
42 (addr & 0xff000000) == 0));
46 * Verify that a given netmask is plausible;
47 * make sure that it is a series of 1's followed by
48 * a series of 0's with no discontiguous 1's.
54 if (~(((mask & -mask) - 1) | mask) != 0) {
55 /* Mask is not contiguous */
63 * Verify that a given subnet number and mask pair are credible.
65 * With CIDR, almost any subnet and mask are credible. mrouted still
66 * can't handle aggregated class A's, so we still check that, but
67 * otherwise the only requirements are that the subnet address is
68 * within the [ABC] range and that the host bits of the subnet
72 inet_valid_subnet(nsubnet, nmask)
73 u_int32 nsubnet, nmask;
75 register u_int32 subnet, mask;
77 subnet = ntohl(nsubnet);
80 if ((subnet & mask) != subnet) return (FALSE);
85 if (IN_CLASSA(subnet)) {
86 if (mask < 0xff000000 ||
87 (subnet & 0xff000000) == 0x7f000000 ||
88 (subnet & 0xff000000) == 0x00000000) return (FALSE);
90 else if (IN_CLASSD(subnet) || IN_BADCLASS(subnet)) {
91 /* Above Class C address space */
95 /* Host bits are set in the subnet */
98 if (!inet_valid_mask(mask)) {
99 /* Netmask is not contiguous */
108 * Convert an IP address in u_long (network) format into a printable string.
118 sprintf(s, "%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
124 * Convert an IP subnet number in u_long (network) format into a printable
125 * string including the netmask as a number of bits.
128 inet_fmts(addr, mask, s)
132 register u_char *a, *m;
135 if ((addr == 0) && (mask == 0)) {
136 sprintf(s, "default");
141 bits = 33 - ffs(ntohl(mask));
143 if (m[3] != 0) sprintf(s, "%u.%u.%u.%u/%d", a[0], a[1], a[2], a[3],
145 else if (m[2] != 0) sprintf(s, "%u.%u.%u/%d", a[0], a[1], a[2], bits);
146 else if (m[1] != 0) sprintf(s, "%u.%u/%d", a[0], a[1], bits);
147 else sprintf(s, "%u/%d", a[0], bits);
153 * Convert the printable string representation of an IP address into the
154 * u_long (network) format. Return 0xffffffff on error. (To detect the
155 * legal address with that value, you must explicitly compare the string
156 * with "255.255.255.255".)
164 u_int a0 = 0, a1 = 0, a2 = 0, a3 = 0;
168 i = sscanf(s, "%u.%u.%u.%u%c", &a0, &a1, &a2, &a3, &c);
169 if (i < n || i > 4 || a0 > 255 || a1 > 255 || a2 > 255 || a3 > 255)
172 ((u_char *)&a)[0] = a0;
173 ((u_char *)&a)[1] = a1;
174 ((u_char *)&a)[2] = a2;
175 ((u_char *)&a)[3] = a3;
182 * inet_cksum extracted from:
187 * U. S. Army Ballistic Research Laboratory
189 * Modified at Uc Berkeley
192 * Public Domain. Distribution Unlimited.
196 * Checksum routine for Internet Protocol family headers (C Version)
200 inet_cksum(addr, len)
204 register int nleft = (int)len;
205 register u_short *w = addr;
207 register int sum = 0;
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
220 /* mop up an odd byte, if necessary */
222 *(u_char *) (&answer) = *(u_char *)w ;
227 * add back carry outs from top 16 bits to low 16 bits
229 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
230 sum += (sum >> 16); /* add carry */
231 answer = ~sum; /* truncate to 16 bits */