conterm/libip/classmask.c
David du Colombier 23a48c7cfe devip: add support for IPv6
Addresses are now stored as uchar[16] instead
of ulong, with enough room for IPv6.

Generic IP functions have been removed from
devip.c and replaced by libip, imported from
Plan 9.

Names and addresses are resolved using either
gethostbyname() or getaddrinfo() functions.

On Windows, IPv6 name resolution is not enabled,
because mingw32 doesn't provide inet_ntop().

R=rsc
http://codereview.appspot.com/6408044
2012-08-03 21:30:17 +02:00

87 lines
1.9 KiB
C

#include <u.h>
#include <libc.h>
#include <ip.h>
static uchar classmask[4][16] = {
0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0x00,0x00,0x00,
0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0x00,0x00,0x00,
0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0x00,0x00,
0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0x00,
};
static uchar v6loopback[IPaddrlen] = {
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0x01
};
static uchar v6linklocal[IPaddrlen] = {
0xfe, 0x80, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
static uchar v6linklocalmask[IPaddrlen] = {
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
0, 0, 0, 0,
0, 0, 0, 0
};
static int v6llpreflen = 8; /* link-local prefix length in bytes */
static uchar v6multicast[IPaddrlen] = {
0xff, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
static uchar v6multicastmask[IPaddrlen] = {
0xff, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
static int v6mcpreflen = 1; /* multicast prefix length */
static uchar v6solicitednode[IPaddrlen] = {
0xff, 0x02, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0x01,
0xff, 0, 0, 0
};
static uchar v6solicitednodemask[IPaddrlen] = {
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
0xff, 0x0, 0x0, 0x0
};
static int v6snpreflen = 13;
uchar*
defmask(uchar *ip)
{
if(isv4(ip))
return classmask[ip[IPv4off]>>6];
else {
if(ipcmp(ip, v6loopback) == 0)
return IPallbits;
else if(memcmp(ip, v6linklocal, v6llpreflen) == 0)
return v6linklocalmask;
else if(memcmp(ip, v6solicitednode, v6snpreflen) == 0)
return v6solicitednodemask;
else if(memcmp(ip, v6multicast, v6mcpreflen) == 0)
return v6multicastmask;
return IPallbits;
}
}
void
maskip(uchar *from, uchar *mask, uchar *to)
{
int i;
for(i = 0; i < IPaddrlen; i++)
to[i] = from[i] & mask[i];
}