ipaux.c (1514B)
1 #include "u.h" 2 #include "lib.h" 3 #include "ip.h" 4 5 /* 6 * well known IP addresses 7 */ 8 uchar IPv4bcast[IPaddrlen] = { 9 0, 0, 0, 0, 10 0, 0, 0, 0, 11 0, 0, 0xff, 0xff, 12 0xff, 0xff, 0xff, 0xff 13 }; 14 uchar IPv4allsys[IPaddrlen] = { 15 0, 0, 0, 0, 16 0, 0, 0, 0, 17 0, 0, 0xff, 0xff, 18 0xe0, 0, 0, 0x01 19 }; 20 uchar IPv4allrouter[IPaddrlen] = { 21 0, 0, 0, 0, 22 0, 0, 0, 0, 23 0, 0, 0xff, 0xff, 24 0xe0, 0, 0, 0x02 25 }; 26 uchar IPallbits[IPaddrlen] = { 27 0xff, 0xff, 0xff, 0xff, 28 0xff, 0xff, 0xff, 0xff, 29 0xff, 0xff, 0xff, 0xff, 30 0xff, 0xff, 0xff, 0xff 31 }; 32 uchar IPnoaddr[IPaddrlen]; 33 34 /* 35 * prefix of all v4 addresses 36 */ 37 uchar v4prefix[IPaddrlen] = { 38 0, 0, 0, 0, 39 0, 0, 0, 0, 40 0, 0, 0xff, 0xff, 41 0, 0, 0, 0 42 }; 43 44 int 45 isv4(uchar *ip) 46 { 47 return memcmp(ip, v4prefix, IPv4off) == 0; 48 } 49 50 /* 51 * the following routines are unrolled with no memset's to speed 52 * up the usual case 53 */ 54 void 55 v4tov6(uchar *v6, uchar *v4) 56 { 57 v6[0] = 0; 58 v6[1] = 0; 59 v6[2] = 0; 60 v6[3] = 0; 61 v6[4] = 0; 62 v6[5] = 0; 63 v6[6] = 0; 64 v6[7] = 0; 65 v6[8] = 0; 66 v6[9] = 0; 67 v6[10] = 0xff; 68 v6[11] = 0xff; 69 v6[12] = v4[0]; 70 v6[13] = v4[1]; 71 v6[14] = v4[2]; 72 v6[15] = v4[3]; 73 } 74 75 int 76 v6tov4(uchar *v4, uchar *v6) 77 { 78 if(v6[0] == 0 79 && v6[1] == 0 80 && v6[2] == 0 81 && v6[3] == 0 82 && v6[4] == 0 83 && v6[5] == 0 84 && v6[6] == 0 85 && v6[7] == 0 86 && v6[8] == 0 87 && v6[9] == 0 88 && v6[10] == 0xff 89 && v6[11] == 0xff) 90 { 91 v4[0] = v6[12]; 92 v4[1] = v6[13]; 93 v4[2] = v6[14]; 94 v4[3] = v6[15]; 95 return 0; 96 } else { 97 memset(v4, 0, 4); 98 if(memcmp(v6, IPnoaddr, IPaddrlen) == 0) 99 return 0; 100 return -1; 101 } 102 }