classmask.c (1908B)
1 #include "u.h" 2 #include "lib.h" 3 #include "ip.h" 4 5 static uchar classmask[4][16] = { 6 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0x00,0x00,0x00, 7 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0x00,0x00,0x00, 8 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0x00,0x00, 9 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0x00, 10 }; 11 12 static uchar v6loopback[IPaddrlen] = { 13 0, 0, 0, 0, 14 0, 0, 0, 0, 15 0, 0, 0, 0, 16 0, 0, 0, 0x01 17 }; 18 19 static uchar v6linklocal[IPaddrlen] = { 20 0xfe, 0x80, 0, 0, 21 0, 0, 0, 0, 22 0, 0, 0, 0, 23 0, 0, 0, 0 24 }; 25 static uchar v6linklocalmask[IPaddrlen] = { 26 0xff, 0xff, 0xff, 0xff, 27 0xff, 0xff, 0xff, 0xff, 28 0, 0, 0, 0, 29 0, 0, 0, 0 30 }; 31 static int v6llpreflen = 8; /* link-local prefix length in bytes */ 32 33 static uchar v6multicast[IPaddrlen] = { 34 0xff, 0, 0, 0, 35 0, 0, 0, 0, 36 0, 0, 0, 0, 37 0, 0, 0, 0 38 }; 39 static uchar v6multicastmask[IPaddrlen] = { 40 0xff, 0, 0, 0, 41 0, 0, 0, 0, 42 0, 0, 0, 0, 43 0, 0, 0, 0 44 }; 45 static int v6mcpreflen = 1; /* multicast prefix length */ 46 47 static uchar v6solicitednode[IPaddrlen] = { 48 0xff, 0x02, 0, 0, 49 0, 0, 0, 0, 50 0, 0, 0, 0x01, 51 0xff, 0, 0, 0 52 }; 53 static uchar v6solicitednodemask[IPaddrlen] = { 54 0xff, 0xff, 0xff, 0xff, 55 0xff, 0xff, 0xff, 0xff, 56 0xff, 0xff, 0xff, 0xff, 57 0xff, 0x0, 0x0, 0x0 58 }; 59 static int v6snpreflen = 13; 60 61 uchar* 62 defmask(uchar *ip) 63 { 64 if(isv4(ip)) 65 return classmask[ip[IPv4off]>>6]; 66 else { 67 if(ipcmp(ip, v6loopback) == 0) 68 return IPallbits; 69 else if(memcmp(ip, v6linklocal, v6llpreflen) == 0) 70 return v6linklocalmask; 71 else if(memcmp(ip, v6solicitednode, v6snpreflen) == 0) 72 return v6solicitednodemask; 73 else if(memcmp(ip, v6multicast, v6mcpreflen) == 0) 74 return v6multicastmask; 75 return IPallbits; 76 } 77 } 78 79 void 80 maskip(uchar *from, uchar *mask, uchar *to) 81 { 82 int i; 83 84 for(i = 0; i < IPaddrlen; i++) 85 to[i] = from[i] & mask[i]; 86 }