2002-08-01 12:33:14 +04:00
|
|
|
/* $NetBSD: inet.c,v 1.7 2002/08/01 08:33:14 itojun Exp $ */
|
1995-10-09 06:51:32 +03:00
|
|
|
|
1994-01-11 23:15:44 +03:00
|
|
|
/*
|
|
|
|
* The mrouted program is covered by the license in the accompanying file
|
|
|
|
* named "LICENSE". 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exported variables.
|
|
|
|
*/
|
1995-06-01 06:25:45 +04:00
|
|
|
char s1[19]; /* buffers to hold the string representations */
|
|
|
|
char s2[19]; /* of IP addresses, to be passed to inet_fmt() */
|
|
|
|
char s3[19]; /* or inet_fmts(). */
|
|
|
|
char s4[19];
|
1994-01-11 23:15:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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}.)
|
|
|
|
*/
|
1995-12-10 13:06:55 +03:00
|
|
|
int
|
2002-07-14 20:30:41 +04:00
|
|
|
inet_valid_host(u_int32_t naddr)
|
1994-01-11 23:15:44 +03:00
|
|
|
{
|
2002-07-14 20:30:41 +04:00
|
|
|
u_int32_t addr;
|
1994-01-11 23:15:44 +03:00
|
|
|
|
|
|
|
addr = ntohl(naddr);
|
|
|
|
|
|
|
|
return (!(IN_MULTICAST(addr) ||
|
|
|
|
IN_BADCLASS (addr) ||
|
|
|
|
(addr & 0xff000000) == 0));
|
|
|
|
}
|
|
|
|
|
1995-12-10 13:06:55 +03:00
|
|
|
/*
|
|
|
|
* 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
|
2002-07-14 20:30:41 +04:00
|
|
|
inet_valid_mask(u_int32_t mask)
|
1995-12-10 13:06:55 +03:00
|
|
|
{
|
|
|
|
if (~(((mask & -mask) - 1) | mask) != 0) {
|
|
|
|
/* Mask is not contiguous */
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (TRUE);
|
|
|
|
}
|
1994-01-11 23:15:44 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify that a given subnet number and mask pair are credible.
|
1995-06-01 06:25:45 +04:00
|
|
|
*
|
|
|
|
* 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.
|
1994-01-11 23:15:44 +03:00
|
|
|
*/
|
1995-12-10 13:06:55 +03:00
|
|
|
int
|
2002-07-14 20:30:41 +04:00
|
|
|
inet_valid_subnet(u_int32_t nsubnet, u_int32_t nmask)
|
1994-01-11 23:15:44 +03:00
|
|
|
{
|
2002-07-14 20:30:41 +04:00
|
|
|
u_int32_t subnet, mask;
|
1994-01-11 23:15:44 +03:00
|
|
|
|
|
|
|
subnet = ntohl(nsubnet);
|
|
|
|
mask = ntohl(nmask);
|
|
|
|
|
|
|
|
if ((subnet & mask) != subnet) return (FALSE);
|
|
|
|
|
1995-12-10 13:06:55 +03:00
|
|
|
if (subnet == 0)
|
|
|
|
return (mask == 0);
|
1995-06-01 06:25:45 +04:00
|
|
|
|
1994-01-11 23:15:44 +03:00
|
|
|
if (IN_CLASSA(subnet)) {
|
|
|
|
if (mask < 0xff000000 ||
|
1995-12-10 13:06:55 +03:00
|
|
|
(subnet & 0xff000000) == 0x7f000000 ||
|
|
|
|
(subnet & 0xff000000) == 0x00000000) return (FALSE);
|
1994-01-11 23:15:44 +03:00
|
|
|
}
|
1995-06-01 06:25:45 +04:00
|
|
|
else if (IN_CLASSD(subnet) || IN_BADCLASS(subnet)) {
|
|
|
|
/* Above Class C address space */
|
|
|
|
return (FALSE);
|
1994-01-11 23:15:44 +03:00
|
|
|
}
|
1995-12-10 13:06:55 +03:00
|
|
|
if (subnet & ~mask) {
|
1995-06-01 06:25:45 +04:00
|
|
|
/* Host bits are set in the subnet */
|
|
|
|
return (FALSE);
|
1994-01-11 23:15:44 +03:00
|
|
|
}
|
1995-12-10 13:06:55 +03:00
|
|
|
if (!inet_valid_mask(mask)) {
|
|
|
|
/* Netmask is not contiguous */
|
|
|
|
return (FALSE);
|
|
|
|
}
|
1994-01-11 23:15:44 +03:00
|
|
|
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert an IP address in u_long (network) format into a printable string.
|
|
|
|
*/
|
1995-12-10 13:06:55 +03:00
|
|
|
char *
|
2002-07-14 20:30:41 +04:00
|
|
|
inet_fmt(u_int32_t addr, char *s)
|
1994-01-11 23:15:44 +03:00
|
|
|
{
|
2002-07-14 20:30:41 +04:00
|
|
|
u_char *a;
|
1994-01-11 23:15:44 +03:00
|
|
|
|
|
|
|
a = (u_char *)&addr;
|
|
|
|
sprintf(s, "%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert an IP subnet number in u_long (network) format into a printable
|
1995-06-01 06:25:45 +04:00
|
|
|
* string including the netmask as a number of bits.
|
1994-01-11 23:15:44 +03:00
|
|
|
*/
|
1995-12-10 13:06:55 +03:00
|
|
|
char *
|
2002-07-14 20:30:41 +04:00
|
|
|
inet_fmts(u_int32_t addr, u_int32_t mask, char *s)
|
1994-01-11 23:15:44 +03:00
|
|
|
{
|
2002-07-14 20:30:41 +04:00
|
|
|
u_char *a, *m;
|
1995-06-01 06:25:45 +04:00
|
|
|
int bits;
|
1994-01-11 23:15:44 +03:00
|
|
|
|
1995-06-01 06:25:45 +04:00
|
|
|
if ((addr == 0) && (mask == 0)) {
|
|
|
|
sprintf(s, "default");
|
|
|
|
return (s);
|
|
|
|
}
|
1994-01-11 23:15:44 +03:00
|
|
|
a = (u_char *)&addr;
|
|
|
|
m = (u_char *)&mask;
|
1995-06-01 06:25:45 +04:00
|
|
|
bits = 33 - ffs(ntohl(mask));
|
1994-01-11 23:15:44 +03:00
|
|
|
|
1995-06-01 06:25:45 +04:00
|
|
|
if (m[3] != 0) sprintf(s, "%u.%u.%u.%u/%d", a[0], a[1], a[2], a[3],
|
|
|
|
bits);
|
|
|
|
else if (m[2] != 0) sprintf(s, "%u.%u.%u/%d", a[0], a[1], a[2], bits);
|
|
|
|
else if (m[1] != 0) sprintf(s, "%u.%u/%d", a[0], a[1], bits);
|
|
|
|
else sprintf(s, "%u/%d", a[0], bits);
|
1994-01-11 23:15:44 +03:00
|
|
|
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert the printable string representation of an IP address into the
|
|
|
|
* u_long (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".)
|
|
|
|
*/
|
1995-12-10 13:06:55 +03:00
|
|
|
u_int32_t
|
2002-07-14 20:30:41 +04:00
|
|
|
inet_parse(char *s)
|
1994-01-11 23:15:44 +03:00
|
|
|
{
|
1995-06-01 06:25:45 +04:00
|
|
|
u_int32_t a = 0;
|
1994-01-11 23:15:44 +03:00
|
|
|
u_int a0, a1, a2, a3;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if (sscanf(s, "%u.%u.%u.%u%c", &a0, &a1, &a2, &a3, &c) != 4 ||
|
|
|
|
a0 > 255 || a1 > 255 || a2 > 255 || a3 > 255)
|
|
|
|
return (0xffffffff);
|
|
|
|
|
|
|
|
((u_char *)&a)[0] = a0;
|
|
|
|
((u_char *)&a)[1] = a1;
|
|
|
|
((u_char *)&a)[2] = a2;
|
|
|
|
((u_char *)&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)
|
|
|
|
*
|
|
|
|
*/
|
1995-12-10 13:06:55 +03:00
|
|
|
int
|
2002-08-01 07:40:34 +04:00
|
|
|
inet_cksum(u_int16_t *addr, u_int len)
|
1994-01-11 23:15:44 +03:00
|
|
|
{
|
2002-07-14 20:30:41 +04:00
|
|
|
int nleft = (int)len;
|
2002-08-01 07:40:34 +04:00
|
|
|
u_int16_t *w = addr;
|
|
|
|
int32_t sum = 0;
|
2002-08-01 12:33:14 +04:00
|
|
|
union {
|
|
|
|
u_int16_t w;
|
|
|
|
u_int8_t b[2];
|
|
|
|
} answer;
|
1994-01-11 23:15:44 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
1995-12-10 13:06:55 +03:00
|
|
|
while (nleft > 1) {
|
1994-01-11 23:15:44 +03:00
|
|
|
sum += *w++;
|
|
|
|
nleft -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mop up an odd byte, if necessary */
|
1995-12-10 13:06:55 +03:00
|
|
|
if (nleft == 1) {
|
2002-08-01 12:33:14 +04:00
|
|
|
answer.w = 0;
|
|
|
|
answer.b[0] = *(u_char *)w ;
|
|
|
|
sum += answer.w;
|
1994-01-11 23:15:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 */
|
2002-08-01 12:33:14 +04:00
|
|
|
answer.w = ~sum; /* truncate to 16 bits */
|
|
|
|
return (answer.w);
|
1994-01-11 23:15:44 +03:00
|
|
|
}
|