inet.c revision 1.2 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.
5 *
6 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
7 * Leland Stanford Junior University.
8 *
9 *
10 * $Id: inet.c,v 1.2 1995/06/01 02:25:54 mycroft Exp $
11 */
12
13
14 #include "defs.h"
15
16
17 /*
18 * Exported variables.
19 */
20 char s1[19]; /* buffers to hold the string representations */
21 char s2[19]; /* of IP addresses, to be passed to inet_fmt() */
22 char s3[19]; /* or inet_fmts(). */
23 char s4[19];
24
25
26 /*
27 * Verify that a given IP address is credible as a host address.
28 * (Without a mask, cannot detect addresses of the form {subnet,0} or
29 * {subnet,-1}.)
30 */
31 int inet_valid_host(naddr)
32 u_int32_t naddr;
33 {
34 register u_int32_t addr;
35
36 addr = ntohl(naddr);
37
38 return (!(IN_MULTICAST(addr) ||
39 IN_BADCLASS (addr) ||
40 (addr & 0xff000000) == 0));
41 }
42
43
44 /*
45 * Verify that a given subnet number and mask pair are credible.
46 *
47 * With CIDR, almost any subnet and mask are credible. mrouted still
48 * can't handle aggregated class A's, so we still check that, but
49 * otherwise the only requirements are that the subnet address is
50 * within the [ABC] range and that the host bits of the subnet
51 * are all 0.
52 */
53 int inet_valid_subnet(nsubnet, nmask)
54 u_int32_t nsubnet, nmask;
55 {
56 register u_int32_t subnet, mask;
57
58 subnet = ntohl(nsubnet);
59 mask = ntohl(nmask);
60
61 if ((subnet & mask) != subnet) return (FALSE);
62
63 if (subnet == 0 && mask == 0)
64 return (TRUE);
65
66 if (IN_CLASSA(subnet)) {
67 if (mask < 0xff000000 ||
68 (subnet & 0xff000000) == 0x7f000000) return (FALSE);
69 }
70 else if (IN_CLASSD(subnet) || IN_BADCLASS(subnet)) {
71 /* Above Class C address space */
72 return (FALSE);
73 }
74 else if (subnet & ~mask) {
75 /* Host bits are set in the subnet */
76 return (FALSE);
77 }
78
79 return (TRUE);
80 }
81
82
83 /*
84 * Convert an IP address in u_long (network) format into a printable string.
85 */
86 char *inet_fmt(addr, s)
87 u_int32_t addr;
88 char *s;
89 {
90 register u_char *a;
91
92 a = (u_char *)&addr;
93 sprintf(s, "%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
94 return (s);
95 }
96
97
98 /*
99 * Convert an IP subnet number in u_long (network) format into a printable
100 * string including the netmask as a number of bits.
101 */
102 char *inet_fmts(addr, mask, s)
103 u_int32_t addr, mask;
104 char *s;
105 {
106 register u_char *a, *m;
107 int bits;
108
109 if ((addr == 0) && (mask == 0)) {
110 sprintf(s, "default");
111 return (s);
112 }
113 a = (u_char *)&addr;
114 m = (u_char *)&mask;
115 bits = 33 - ffs(ntohl(mask));
116
117 if (m[3] != 0) sprintf(s, "%u.%u.%u.%u/%d", a[0], a[1], a[2], a[3],
118 bits);
119 else if (m[2] != 0) sprintf(s, "%u.%u.%u/%d", a[0], a[1], a[2], bits);
120 else if (m[1] != 0) sprintf(s, "%u.%u/%d", a[0], a[1], bits);
121 else sprintf(s, "%u/%d", a[0], bits);
122
123 return (s);
124 }
125
126 /*
127 * Convert the printable string representation of an IP address into the
128 * u_long (network) format. Return 0xffffffff on error. (To detect the
129 * legal address with that value, you must explicitly compare the string
130 * with "255.255.255.255".)
131 */
132 u_int32_t inet_parse(s)
133 char *s;
134 {
135 u_int32_t a = 0;
136 u_int a0, a1, a2, a3;
137 char c;
138
139 if (sscanf(s, "%u.%u.%u.%u%c", &a0, &a1, &a2, &a3, &c) != 4 ||
140 a0 > 255 || a1 > 255 || a2 > 255 || a3 > 255)
141 return (0xffffffff);
142
143 ((u_char *)&a)[0] = a0;
144 ((u_char *)&a)[1] = a1;
145 ((u_char *)&a)[2] = a2;
146 ((u_char *)&a)[3] = a3;
147
148 return (a);
149 }
150
151
152 /*
153 * inet_cksum extracted from:
154 * P I N G . C
155 *
156 * Author -
157 * Mike Muuss
158 * U. S. Army Ballistic Research Laboratory
159 * December, 1983
160 * Modified at Uc Berkeley
161 *
162 * (ping.c) Status -
163 * Public Domain. Distribution Unlimited.
164 *
165 * I N _ C K S U M
166 *
167 * Checksum routine for Internet Protocol family headers (C Version)
168 *
169 */
170 int inet_cksum(addr, len)
171 u_short *addr;
172 u_int len;
173 {
174 register int nleft = (int)len;
175 register u_short *w = addr;
176 u_short answer = 0;
177 register int sum = 0;
178
179 /*
180 * Our algorithm is simple, using a 32 bit accumulator (sum),
181 * we add sequential 16 bit words to it, and at the end, fold
182 * back all the carry bits from the top 16 bits into the lower
183 * 16 bits.
184 */
185 while( nleft > 1 ) {
186 sum += *w++;
187 nleft -= 2;
188 }
189
190 /* mop up an odd byte, if necessary */
191 if( nleft == 1 ) {
192 *(u_char *) (&answer) = *(u_char *)w ;
193 sum += answer;
194 }
195
196 /*
197 * add back carry outs from top 16 bits to low 16 bits
198 */
199 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
200 sum += (sum >> 16); /* add carry */
201 answer = ~sum; /* truncate to 16 bits */
202 return (answer);
203 }
204