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.3 2004/03/15 18:10:28 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(u_int32 naddr
)
39 return (!(IN_MULTICAST(addr
) ||
41 (addr
& 0xff000000) == 0));
45 * Verify that a given netmask is plausible;
46 * make sure that it is a series of 1's followed by
47 * a series of 0's with no discontiguous 1's.
50 inet_valid_mask(u_int32 mask
)
52 if (~(((mask
& -mask
) - 1) | mask
) != 0) {
53 /* Mask is not contiguous */
61 * Verify that a given subnet number and mask pair are credible.
63 * With CIDR, almost any subnet and mask are credible. mrouted still
64 * can't handle aggregated class A's, so we still check that, but
65 * otherwise the only requirements are that the subnet address is
66 * within the [ABC] range and that the host bits of the subnet
70 inet_valid_subnet(u_int32 nsubnet
, u_int32 nmask
)
74 subnet
= ntohl(nsubnet
);
77 if ((subnet
& mask
) != subnet
) return (FALSE
);
82 if (IN_CLASSA(subnet
)) {
83 if (mask
< 0xff000000 ||
84 (subnet
& 0xff000000) == 0x7f000000 ||
85 (subnet
& 0xff000000) == 0x00000000) return (FALSE
);
87 else if (IN_CLASSD(subnet
) || IN_BADCLASS(subnet
)) {
88 /* Above Class C address space */
92 /* Host bits are set in the subnet */
95 if (!inet_valid_mask(mask
)) {
96 /* Netmask is not contiguous */
105 * Convert an IP address in u_long (network) format into a printable string.
108 inet_fmt(u_int32 addr
, char *s
)
113 sprintf(s
, "%u.%u.%u.%u", a
[0], a
[1], a
[2], a
[3]);
119 * Convert an IP subnet number in u_long (network) format into a printable
120 * string including the netmask as a number of bits.
123 inet_fmts(u_int32 addr
, u_int32 mask
, char *s
)
128 if ((addr
== 0) && (mask
== 0)) {
129 sprintf(s
, "default");
134 bits
= 33 - ffs(ntohl(mask
));
136 if (m
[3] != 0) sprintf(s
, "%u.%u.%u.%u/%d", a
[0], a
[1], a
[2], a
[3],
138 else if (m
[2] != 0) sprintf(s
, "%u.%u.%u/%d", a
[0], a
[1], a
[2], bits
);
139 else if (m
[1] != 0) sprintf(s
, "%u.%u/%d", a
[0], a
[1], bits
);
140 else sprintf(s
, "%u/%d", a
[0], bits
);
146 * Convert the printable string representation of an IP address into the
147 * u_long (network) format. Return 0xffffffff on error. (To detect the
148 * legal address with that value, you must explicitly compare the string
149 * with "255.255.255.255".)
152 inet_parse(char *s
, int n
)
155 u_int a0
= 0, a1
= 0, a2
= 0, a3
= 0;
159 i
= sscanf(s
, "%u.%u.%u.%u%c", &a0
, &a1
, &a2
, &a3
, &c
);
160 if (i
< n
|| i
> 4 || a0
> 255 || a1
> 255 || a2
> 255 || a3
> 255)
163 ((u_char
*)&a
)[0] = a0
;
164 ((u_char
*)&a
)[1] = a1
;
165 ((u_char
*)&a
)[2] = a2
;
166 ((u_char
*)&a
)[3] = a3
;
173 * inet_cksum extracted from:
178 * U. S. Army Ballistic Research Laboratory
180 * Modified at Uc Berkeley
183 * Public Domain. Distribution Unlimited.
187 * Checksum routine for Internet Protocol family headers (C Version)
191 inet_cksum(u_short
*addr
, u_int len
)
193 int nleft
= (int)len
;
199 * Our algorithm is simple, using a 32 bit accumulator (sum),
200 * we add sequential 16 bit words to it, and at the end, fold
201 * back all the carry bits from the top 16 bits into the lower
209 /* mop up an odd byte, if necessary */
211 *(u_char
*) (&answer
) = *(u_char
*)w
;
216 * add back carry outs from top 16 bits to low 16 bits
218 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
219 sum
+= (sum
>> 16); /* add carry */
220 answer
= ~sum
; /* truncate to 16 bits */