1 /* $NetBSD: inet.c,v 1.11 2003/05/16 22:59:50 dsl Exp $ */
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.
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
29 inet_valid_host(u_int32_t naddr
)
35 return (!(IN_MULTICAST(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.
46 inet_valid_mask(u_int32_t mask
)
48 if (~(((mask
& -mask
) - 1) | mask
) != 0) {
49 /* Mask is not contiguous */
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
66 inet_valid_subnet(u_int32_t nsubnet
, u_int32_t nmask
)
68 u_int32_t subnet
, mask
;
70 subnet
= ntohl(nsubnet
);
73 if ((subnet
& mask
) != subnet
) return (FALSE
);
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 */
88 /* Host bits are set in the subnet */
91 if (!inet_valid_mask(mask
)) {
92 /* Netmask is not contiguous */
101 * Convert an IP address in u_long (network) format into a printable string.
104 inet_fmt(u_int32_t addr
)
107 char *s
= ss
[++ss_index
& SS_MASK
];
110 snprintf(s
, sizeof ss
[0], "%u.%u.%u.%u", a
[0], a
[1], a
[2], a
[3]);
116 * Convert an IP subnet number in u_long (network) format into a printable
117 * string including the netmask as a number of bits.
120 inet_fmts(u_int32_t addr
, u_int32_t mask
)
124 char *s
= ss
[++ss_index
& SS_MASK
];
126 if ((addr
== 0) && (mask
== 0)) {
127 snprintf(s
, sizeof ss
[0], "default");
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],
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
);
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".)
150 inet_parse(char *s
, int *mask_p
)
153 u_int a0
, a1
, a2
, a3
;
157 if (sscanf(s
, "%u.%u.%u.%u%n", &a0
, &a1
, &a2
, &a3
, &n
) != 4)
159 if (a0
> 255 || a1
> 255 || a2
> 255 || a3
> 255)
166 if (sscanf(s
+ n
, "/%u%c", &n
, &c
) != 1 || n
> 32)
171 ((u_char
*)&a
)[0] = a0
;
172 ((u_char
*)&a
)[1] = a1
;
173 ((u_char
*)&a
)[2] = a2
;
174 ((u_char
*)&a
)[3] = a3
;
181 * inet_cksum extracted from:
186 * U. S. Army Ballistic Research Laboratory
188 * Modified at Uc Berkeley
191 * Public Domain. Distribution Unlimited.
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
;
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 */
223 answer
.b
[0] = *(u_char
*)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 */