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