JavaScript Gets IP Gets how IPV6 checks

  • 2021-06-28 10:21:24
  • OfStack

1. What is IPV6?

This problem can be found in Baidu or Google. There should be great gains. In fact, the main purpose is to solve the problem that the IPV4 address is not enough. 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?

You can say yes or no, because there is a prefix in IPV6, which is equivalent to the subnet mask in IPV4, but with different names.No, it's OK, because the address of IPV6 is so large that it's supposed to give one address to every sand on the earth, which makes the concept of subnets less important.

3. Address of IPV6

The IPV6 address notation can also be found on the Internet in many ways, which I will not list in detail. 1 is generally as follows: 1205:: fff0:ffd1:1021/120, followed by 120 which represents the prefix, equivalent to the subnet mask in IPV4.

4. Calculation method of IP address

For IPV4, if 192.168.1.1/255.255.255.0, then its segments are from 192.168.1.0 to 192.168.1.255, how is this calculated? & #63;According to ISP, its starting address is: this IP address and subnet mask are obtained bitwise and operationally;Its termination address is the reverse of this IP address and subnet mask (excluding symbol bits) by bit or operation.

IPV6 is also the same as IPV4 in principle, because the IPV6 prefix is too long to be expressed as 255.255.255.0 at all, so numbers are usually written directly, such as 120 or 128.120 means that the prefix 120 bits are all 1, followed by 0, which is equivalent to the subnet mask in IPV4, calculating the IPV6 address and IPV41-like.

The brief knowledge of IPV6 has been explained above. 1. The next step is to deal with the IPV6 web address.

IPV6 requires zero compression compatibility in its web address writing, so it is necessary to convert the IPV6 web address. Here is the JS conversion code:


function transitIp(ipaddr)// take IPV6 Address Completion 
{
 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] += ":";
 }
 }
 //// Complete the above and place the content in ipaddrs Medium, but not standard 
 return initaddr(ipaddrs);// Obtained ip Complete string of address 
}
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 determination 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 we describe the conversion from IPV4 to IPV6:

The conversion from IPV4 to IPV6 is simple. Simply convert the IPV4 address to 106-bit format, group the two paragraphs into a group, and then add the following:: ffff

The JS code is as follows:


function four2six(fouraddr,fourmask)//IPV4 turn IPV6 , including addresses and masks 
{
 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 The 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\.(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(" Incorrect subnet mask !");
 return;
 }
 }
 else
 {
 var mask = parseInt(fourmask, 10);
 if(mask<0 || mask > 32)
 {
 alert(" Incorrect subnet mask !");
 return;
 }
 }
 /***
 IPV4 turn IPV6 The method is simple 
 1 , first turn each paragraph to 16 Binary 
 2 , less than two previous additions 0
 3 , V41 Cogeneration 4 individual 16 Binary number, combined separately from the first two and the last two 
 4 , Address plus "0000:0000:0000:0000:0000:ffff:" that will do 
 **/
 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 += ":";
 }
 }
 // Generated above V6 Address segment correct 
 sixtemp = "0000:0000:0000:0000:0000:ffff:" + sixtemp;
 /***
  Processing subnet mask below, subnet mask can be written in two ways , Number or 255.255.255.0 Writing 
 1 And 1 The process is simple, just add this number 96 ( 128-32 ) 
 2 And 2 Species need to be divided into 4 Segments, each to 2 Binary, see which 1 Bits begin to change to 0
 **/
 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;
 }
 }
 // Judge the location below 
 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 turn IPV6 , address only 
{
 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 The 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 += ":";
 }
 }
 // Generated above V6 Address segment correct 
 sixtemp = "0000:0000:0000:0000:0000:ffff:" + sixtemp;
 return sixtemp;
}

Of course, the above method includes a regular judgment of the IPV4 address and mask in the form of 255.255.255.0 and/32.


Related articles: