1. What is IPV6?
You can search for this problem on Baidu or Google, and you will get a lot of benefits. In fact, it is mainly to solve the problem of insufficient IPV4 addresses. The V4 address is 32 bits, that is, 192.168.1.1, while the V6 address is 128 bits, which is much larger than V4.
2. Does IPV6 have a subnet mask?
It can be said that there is, or that there is no. It is because there is a prefix in IPV6. It is equivalent to the subnet mask in IPV4, but the names are different; it is said that there is no, or that, because the address of IPV6 is too large, and it is said that it can give each sand on the earth an address, which will fade the concept of subnet and no longer need it.
3. IPV6 address
The IPV6 address writing method can also be found on the Internet, so I won't list it in detail, generally as follows: 1205::ffff0:ffd1:1021/120, the latter 120 represents the prefix, which is equivalent to the subnet mask in IPV4.
4. How to calculate IP address
The calculation method of IPV6 and IPV4 is the same. For IPV4, for example, 192.168.1.1/255.255.255.0, then its network segment is calculated from 192.168.1.0~192.168.1.255. How is this calculated? According to the provisions of the ISP, its starting address is: this IP address and subnet mask are obtained by bit and operation; its end address is: this IP address and subnet mask are in bit or operation.
IPV6 is also the same in principle as IPV4. Because the IPV6 prefix is too long, it cannot be expressed in the form of 255.255.255.0, so numbers are usually written directly, such as 120 or 128, etc. 120 means that the first 120 bits of this prefix are all 1, and the next one is 0. This prefix is equivalent to the subnet mask in IPV4. The calculation of IPV6 address is the same as IPV4.
The above has explained the brief knowledge of IPV6, and we will deal with the IPV6 website.
The URL of IPV6 needs to be compatible with zero compression method, so the URL of IPV6 needs to be converted. The following is the JS conversion code:
function transitIp(ipaddr)//Complete IPV6 address { var ipaddress = ipaddr.split("/"); var ipaddrs = ipaddress[0].split(":"); if(ipaddrs.length<8) { var count = 0; for(var i=0;i<ipaddrs.length;i++) { if(ipaddrs[i]=="") { if(count==1) { ipaddrs[i] = addZero(4); continue; } ipaddrs[i] = addZero((9-ipaddrs.length)*4); count++; } else { ipaddrs[i] += ":"; } } } else if(ipaddrs.length==8) { for(var i=0;i<8;i++) { ipaddrs[i] += ":"; } } ////The above completion is completed, the content is placed in ipaddrs, but it is not standard return initaddr(ipaddrs);//The complete string of the ip address is obtained}function addZero(num){ var zerostr = ""; for(var i=1;i<num+1;i++) { zerostr+="0"; if(i%4==0) { zerostr+=":"; } } return zerostr;}function initaddr(ipaddrs){ var iparray =""; for(var i=0;i<ipaddrs.length;i++) { iparray+=ipaddrs[i]; } if(iparray.charAt(iparray.length-1)==':') { iparray = iparray.substr(0,iparray.length-1); } //var iparrays = iparray.split(":"); //return iparrays; return iparray;}The regular expression for IPV6 address judgment is:
function isIPv6(str)//IPV6 address judgment{ return /:/.test(str) &&str.match(/:/g).length<8 &&/::/.test(str) ?(str.match(/::/g).length==1 &&/^::$|^(::)?([/da-f]{1,4}(:|::))*[/da-f]{1,4}(::)?$/i.test(str)) :/^([/da-f]{1,4}:){7}[/da-f]{1,4}$/i.test(str);}Next, let’s explain the conversion from IPV4 to IPV6:
The conversion of IPV4 to IPV6 URLs is very simple. Just convert the IPV4 address to hexadecimal format, take two segments into a group, and then add ::ffff before it.
The JS code is as follows:
function four2six(fouraddr,fourmask)//IPV4 to IPV6, including address and mask { var reg = fouraddr.match(/^(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])$/); if(reg==null) { alert("IP address is incorrect!"); return; } if(fourmask.indexOf(".")!=-1) { reg = fourmask.match(/^(254|252|248|240|224|192|128|0)/.0/.0/.0$|^(255/.(254|252|248|240|224|192|128|0)/.0/.0)$|^(255/.255/.(255|254|252|248|240|224|192|128|0)/.0)$|^(255/.255/.255/.(255|254|252|248|240|224|192|128|0))$/); if(reg==null) { alert("Subnet mask is incorrect!"); return; } } else { var mask = parseInt(fourmask, 10); if(mask<0 || mask > 32) { alert("Subnet mask is incorrect!"); return; } } /*** The method of converting IPV4 to IPV6 is very simple 1. First convert each segment into hexadecimal 2. Add 0 before less than two digits 3. V4 will produce a total of 4 hexadecimal digits, which are combined with the first two and the last two respectively 4. Add "0000:0000:0000:0000:0000:0000:0000:0000:0000:0000:0000:0000:0000:ffff:"**/ var sixtemp = ""; var fouraddrs = fouraddr.split("."); for (var i=0; i<fouraddrs.length; i++) { var addr4ip = parseInt(fouraddrs[i], 10); var addrtemp = addr4ip.toString(16); if(addrtemp.length==1) { addrtemp = "0" + addrtemp; } sixtemp += addrtemp; if(i==1) { sixtemp += ":"; } } //The above generated V6 address segment is correct sixtemp = "0000:0000:0000:0000:0000:0000:0000:0000:ffff:" + sixtemp; /*** The subnet mask can be processed below. There are two ways to write subnet masks, number or 255.255.255.0 1. The first method is relatively simple to process. Just add 96 (128-32) to this value. 2. The second type needs to be divided into four segments, each segment is converted into binary, and see which bit becomes 0 at the beginning. **/ var masktemp = 96; if(fourmask.indexOf(".")==-1) { masktemp += parseInt(fourmask); } else { var masks = fourmask.split("."); for ( var i=0; i<masks.length; i++) { var mask4ip = parseInt(masks[i], 10); var mask4temp = mask4ip.toString(2); if(mask4temp.length!=8) { for(var j=0;j<8-mask4temp;j++) { mask4temp = "0"+mask4temp; } } //The following is a position var flagtemp = false; for(var j=0;j<8;j++) { if(mask4temp.charAt(j)=='0') { flagtemp = true; masktemp += i*8 + j; break; } if(j==7&&i==3) { flagtemp = true; masktemp = 128; break; } } if(flagtemp) { break; } } } return sixtemp + "/" + masktemp;}function four2sixip(fouraddr)//IPV4 to IPV6, only to address { var reg = fouraddr.match(/^(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])$/); if(reg==null) { alert("IP address is incorrect!"); return; } var sixtemp = ""; var fouraddrs = fouraddr.split("."); for (var i=0; i<fouraddrs.length; i++) { var addr4ip = parseInt(fouraddrs[i], 10); var addrtemp = addr4ip.toString(16); if(addrtemp.length==1) { addrtemp = "0" + addrtemp; } sixtemp += addrtemp; if(i==1) { sixtemp += ":"; } } //The above V6 address segment is correct sixtemp = "0000:0000:0000:0000:0000:ffff:" + sixtemp; return sixtemp;}Of course, the above method includes regular judgment of IPV4 address and mask, and supports two types of masks in the form of 255.255.255.0 and /32.