-
Notifications
You must be signed in to change notification settings - Fork 1
/
inet.c
301 lines (257 loc) · 7.02 KB
/
inet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* The mrouted program is covered by the license in the accompanying file
* named "LICENSE.mrouted". Use of the mrouted program represents acceptance of
* the terms and conditions listed in that file.
*
* The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
* Leland Stanford Junior University.
*
*
* inet.c,v 3.8.4.1 1997/01/29 19:49:33 fenner Exp
*/
#define C(x) ((x) & 0xff)
#include "defs.h"
/*
* Exported variables.
*/
char s1[MAX_INET_BUF_LEN]; /* buffers to hold the string representations */
char s2[MAX_INET_BUF_LEN]; /* of IP addresses, to be passed to inet_fmt() */
char s3[MAX_INET_BUF_LEN];
char s4[MAX_INET_BUF_LEN];
/*
* Verify that a given IP address is credible as a host address.
* (Without a mask, cannot detect addresses of the form {subnet,0} or
* {subnet,-1}.)
*/
int inet_valid_host(uint32_t naddr)
{
uint32_t addr;
addr = ntohl(naddr);
return !(IN_MULTICAST(addr) ||
IN_BADCLASS (addr) ||
(addr & 0xff000000) == 0);
}
/*
* Verify that a given netmask is plausible;
* make sure that it is a series of 1's followed by
* a series of 0's with no discontiguous 1's.
*/
int inet_valid_mask(uint32_t mask)
{
if (~(((mask & -mask) - 1) | mask) != 0) {
/* Mask is not contiguous */
return FALSE;
}
return TRUE;
}
/*
* Verify that a given subnet number and mask pair are credible.
*
* With CIDR, almost any subnet and mask are credible. mrouted still
* can't handle aggregated class A's, so we still check that, but
* otherwise the only requirements are that the subnet address is
* within the [ABC] range and that the host bits of the subnet
* are all 0.
*/
int inet_valid_subnet(uint32_t nsubnet, uint32_t nmask)
{
uint32_t subnet, mask;
subnet = ntohl(nsubnet);
mask = ntohl(nmask);
if ((subnet & mask) != subnet)
return FALSE;
if (subnet == 0)
return mask == 0;
if (IN_CLASSA(subnet)) {
if (mask < 0xff000000 ||
(subnet & 0xff000000) == 0x7f000000 ||
(subnet & 0xff000000) == 0x00000000)
return FALSE;
}
else if (IN_CLASSD(subnet) || IN_BADCLASS(subnet)) {
/* Above Class C address space */
return FALSE;
}
if (subnet & ~mask) {
/* Host bits are set in the subnet */
return FALSE;
}
if (!inet_valid_mask(mask)) {
/* Netmask is not contiguous */
return FALSE;
}
return TRUE;
}
/*
* Convert an IP address in uint32_t (network) format into a printable string.
*/
char *inet_fmt(uint32_t addr, char *s, size_t len)
{
uint8_t *a;
a = (uint8_t *)&addr;
snprintf(s, len, "%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
return s;
}
/*
* Convert the printable string representation of an IP address into the
* uint32_t (network) format. Return 0xffffffff on error. (To detect the
* legal address with that value, you must explicitly compare the string
* with "255.255.255.255".)
* The return value is in network order.
*/
uint32_t inet_parse(char *s, int n)
{
uint32_t a = 0;
u_int a0 = 0, a1 = 0, a2 = 0, a3 = 0;
int i;
char c;
i = sscanf(s, "%u.%u.%u.%u%c", &a0, &a1, &a2, &a3, &c);
if (i < n || i > 4 || a0 > 255 || a1 > 255 || a2 > 255 || a3 > 255)
return 0xffffffff;
((uint8_t *)&a)[0] = a0;
((uint8_t *)&a)[1] = a1;
((uint8_t *)&a)[2] = a2;
((uint8_t *)&a)[3] = a3;
return a;
}
/*
* inet_cksum extracted from:
* P I N G . C
*
* Author -
* Mike Muuss
* U. S. Army Ballistic Research Laboratory
* December, 1983
* Modified at Uc Berkeley
*
* (ping.c) Status -
* Public Domain. Distribution Unlimited.
*
* I N _ C K S U M
*
* Checksum routine for Internet Protocol family headers (C Version)
*
*/
int inet_cksum(uint16_t *addr, u_int len)
{
int sum = 0;
int nleft = (int)len;
uint16_t *w = addr;
uint16_t answer = 0;
/*
* Our algorithm is simple, using a 32 bit accumulator (sum),
* we add sequential 16 bit words to it, and at the end, fold
* back all the carry bits from the top 16 bits into the lower
* 16 bits.
*/
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
/* mop up an odd byte, if necessary */
if (nleft == 1) {
*(uint8_t *) (&answer) = *(uint8_t *)w ;
sum += answer;
}
/*
* add back carry outs from top 16 bits to low 16 bits
*/
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* truncate to 16 bits */
return answer;
}
/*
* Called by following netname() to create a mask specified network address.
*/
void trimdomain(char *cp)
{
static char domain[MAXHOSTNAMELEN + 1];
static int first = 1;
char *s;
if (first) {
first = 0;
if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
(s = strchr(domain, '.')))
(void) strlcpy(domain, s + 1, sizeof(domain));
else
domain[0] = 0;
}
if (domain[0]) {
while ((cp = strchr(cp, '.'))) {
if (!strcasecmp(cp + 1, domain)) {
*cp = 0;
break;
}
cp++;
}
}
}
static uint32_t forgemask(uint32_t a)
{
uint32_t m;
if (IN_CLASSA(a))
m = IN_CLASSA_NET;
else if (IN_CLASSB(a))
m = IN_CLASSB_NET;
else
m = IN_CLASSC_NET;
return (m);
}
static void domask(char *dst, size_t len, uint32_t addr, uint32_t mask)
{
int b, i;
if (!mask || (forgemask(addr) == mask)) {
*dst = '\0';
return;
}
i = 0;
for (b = 0; b < 32; b++) {
if (mask & (1 << b)) {
int bb;
i = b;
for (bb = b+1; bb < 32; bb++) {
if (!(mask & (1 << bb))) {
i = -1; /* noncontig */
break;
}
}
break;
}
}
if (i == -1)
snprintf(dst, len, "&0x%x", mask);
else
snprintf(dst, len, "/%d", 32 - i);
}
/*
* Return the name of the network whose address is given.
* The address is assumed to be that of a net or subnet, not a host.
*/
char *netname(uint32_t addr, uint32_t mask)
{
static char line[MAXHOSTNAMELEN + 4];
uint32_t omask;
uint32_t i;
i = ntohl(addr);
omask = mask = ntohl(mask);
if ((i & 0xffffff) == 0)
snprintf(line, sizeof(line), "%u", C(i >> 24));
else if ((i & 0xffff) == 0)
snprintf(line, sizeof(line), "%u.%u", C(i >> 24) , C(i >> 16));
else if ((i & 0xff) == 0)
snprintf(line, sizeof(line), "%u.%u.%u", C(i >> 24), C(i >> 16), C(i >> 8));
else
snprintf(line, sizeof(line), "%u.%u.%u.%u", C(i >> 24),
C(i >> 16), C(i >> 8), C(i));
domask(line + strlen(line), sizeof(line) - strlen(line), i, omask);
return line;
}
/**
* Local Variables:
* version-control: t
* indent-tabs-mode: t
* c-file-style: "ellemtel"
* c-basic-offset: 4
* End:
*/