Blob
1 #include <u.h>2 #include <libc.h>3 #include <ip.h>5 /*6 * well known IP addresses7 */8 uchar IPv4bcast[IPaddrlen] = {9 0, 0, 0, 0,10 0, 0, 0, 0,11 0, 0, 0xff, 0xff,12 0xff, 0xff, 0xff, 0xff13 };14 uchar IPv4allsys[IPaddrlen] = {15 0, 0, 0, 0,16 0, 0, 0, 0,17 0, 0, 0xff, 0xff,18 0xe0, 0, 0, 0x0119 };20 uchar IPv4allrouter[IPaddrlen] = {21 0, 0, 0, 0,22 0, 0, 0, 0,23 0, 0, 0xff, 0xff,24 0xe0, 0, 0, 0x0225 };26 uchar IPallbits[IPaddrlen] = {27 0xff, 0xff, 0xff, 0xff,28 0xff, 0xff, 0xff, 0xff,29 0xff, 0xff, 0xff, 0xff,30 0xff, 0xff, 0xff, 0xff31 };32 uchar IPnoaddr[IPaddrlen];34 /*35 * prefix of all v4 addresses36 */37 uchar v4prefix[IPaddrlen] = {38 0, 0, 0, 0,39 0, 0, 0, 0,40 0, 0, 0xff, 0xff,41 0, 0, 0, 042 };44 int45 isv4(uchar *ip)46 {47 return memcmp(ip, v4prefix, IPv4off) == 0;48 }50 /*51 * the following routines are unrolled with no memset's to speed52 * up the usual case53 */54 void55 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 }75 int76 v6tov4(uchar *v4, uchar *v6)77 {78 if(v6[0] == 079 && v6[1] == 080 && v6[2] == 081 && v6[3] == 082 && v6[4] == 083 && v6[5] == 084 && v6[6] == 085 && v6[7] == 086 && v6[8] == 087 && v6[9] == 088 && v6[10] == 0xff89 && 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 }