MFC r1.6 r1.30 r1.28 (HEAD):
[dragonfly.git] / usr.sbin / mrouted / inet.c
blob190c1a6bfff16441a69bbd7d1b7f027bb9d46f81
1 /*
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 $
16 #include "defs.h"
19 * Exported variables.
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(). */
24 char s4[19];
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
30 * {subnet,-1}.)
32 int
33 inet_valid_host(u_int32 naddr)
35 u_int32 addr;
37 addr = ntohl(naddr);
39 return (!(IN_MULTICAST(addr) ||
40 IN_BADCLASS (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.
49 int
50 inet_valid_mask(u_int32 mask)
52 if (~(((mask & -mask) - 1) | mask) != 0) {
53 /* Mask is not contiguous */
54 return (FALSE);
57 return (TRUE);
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
67 * are all 0.
69 int
70 inet_valid_subnet(u_int32 nsubnet, u_int32 nmask)
72 u_int32 subnet, mask;
74 subnet = ntohl(nsubnet);
75 mask = ntohl(nmask);
77 if ((subnet & mask) != subnet) return (FALSE);
79 if (subnet == 0)
80 return (mask == 0);
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 */
89 return (FALSE);
91 if (subnet & ~mask) {
92 /* Host bits are set in the subnet */
93 return (FALSE);
95 if (!inet_valid_mask(mask)) {
96 /* Netmask is not contiguous */
97 return (FALSE);
100 return (TRUE);
105 * Convert an IP address in u_long (network) format into a printable string.
107 char *
108 inet_fmt(u_int32 addr, char *s)
110 u_char *a;
112 a = (u_char *)&addr;
113 sprintf(s, "%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
114 return (s);
119 * Convert an IP subnet number in u_long (network) format into a printable
120 * string including the netmask as a number of bits.
122 char *
123 inet_fmts(u_int32 addr, u_int32 mask, char *s)
125 u_char *a, *m;
126 int bits;
128 if ((addr == 0) && (mask == 0)) {
129 sprintf(s, "default");
130 return (s);
132 a = (u_char *)&addr;
133 m = (u_char *)&mask;
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],
137 bits);
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);
142 return (s);
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".)
151 u_int32
152 inet_parse(char *s, int n)
154 u_int32 a = 0;
155 u_int a0 = 0, a1 = 0, a2 = 0, a3 = 0;
156 int i;
157 char c;
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)
161 return (0xffffffff);
163 ((u_char *)&a)[0] = a0;
164 ((u_char *)&a)[1] = a1;
165 ((u_char *)&a)[2] = a2;
166 ((u_char *)&a)[3] = a3;
168 return (a);
173 * inet_cksum extracted from:
174 * P I N G . C
176 * Author -
177 * Mike Muuss
178 * U. S. Army Ballistic Research Laboratory
179 * December, 1983
180 * Modified at Uc Berkeley
182 * (ping.c) Status -
183 * Public Domain. Distribution Unlimited.
185 * I N _ C K S U M
187 * Checksum routine for Internet Protocol family headers (C Version)
191 inet_cksum(u_short *addr, u_int len)
193 int nleft = (int)len;
194 u_short *w = addr;
195 u_short answer = 0;
196 int sum = 0;
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
202 * 16 bits.
204 while (nleft > 1) {
205 sum += *w++;
206 nleft -= 2;
209 /* mop up an odd byte, if necessary */
210 if (nleft == 1) {
211 *(u_char *) (&answer) = *(u_char *)w ;
212 sum += answer;
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 */
221 return (answer);