JavaScript gets IP access is IPV6 How to verify _javascript skills

Source: Internet
Author: User
Tags bitwise

1, what is IPV6?

This problem can go to Baidu or Google find, there should be a great harvest, in fact, is mainly to solve the problem of IPV4 address, V4 address is 32, that is: 192.168.1.1, and V6 address is 128, than the V4 enlarged too many times.

2, IPV6 have subnet mask?

It can be said that there is, it can be said that there is, because there is a prefix in the IPV6, it is equivalent to the subnet mask in IPV4, but the name is not the same; Say no, also can, because the address of the IPV6 is too big, claiming to give the earth every sand an address, so that the concept of the subnet is diluted, It's not necessary.

3, the IPV6 address

IPV6 address writing on the Internet can also find a lot of, I do not detail, generally as follows: 1205::FFF0:FFD1:1021/120, the following 120 is a prefix, equivalent to the IPV4 subnet mask.

4, the IP address calculation method

IPV6 and IPV4 network segment calculation, as for IPV4, such as 192.168.1.1/255.255.255.0, then its network segment is from the 192.168.1.0~192.168.1.255, this is how to calculate it? According to the ISP's rules, Its starting address is: This IP address and subnet mask are evaluated by bitwise AND operation, and its termination address is: The negation of this IP address and subnet mask (no sign bit) is evaluated by bitwise OR operation.

IPV6 is the same principle as IPV4, because the IPV6 prefix is too long to be expressed in 255.255.255.0 form, so it is usually written directly to numbers, such as 120 or 128. 120 indicates that the first 120 digits of the prefix are 1, followed by 0, which is equivalent to the subnet mask in IPV4, which computes the IPV6 address and IPV4.

The above has explained the IPV6 's brief knowledge, the following to deal with the IPV6 Web site.

The

IPV6 URL is written on a compatible 0 compression method, so you need to convert the IPV6 URL, the following is a JS conversion code:

function Transitip (IPADDR)//will IPV6 address complement {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, the content is placed in the Ipaddrs, but not standard return initaddr (IPADDRS);//obtained the full string of the IP 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 the IPV6 address judgment is:

function IsIPv6 (str)//ipv6 address Judge 
{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);
}

Then we explain the conversion of the next IPV4 to IPV6:

IPV4 to IPV6 URL conversion is very simple, simply convert the IPV4 address to hexadecimal format, to a group of two paragraphs, and then add:: FFFF can

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 ("Incorrect IP address!");
 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 >) {alert ("Incorrect subnet mask!");
 Return The method of/*** IPV4 to IPV6 is very simple 1, first of its 16 to 2, less than two bits before adding 0 3, V4 will produce 4 16 in total, compared to the first two and the last two respectively combined 4, address front plus "0000:0000:0000:0000:000
 0:FFFF: "Can **/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 + = ":";
 }//above produce V6 address section correct sixtemp = "0000:0000:0000:0000:0000:FFFF:" + sixtemp; /*** The subnet mask is processed below, subnet masks can be written in two ways, numbers or 255.255.255.0 1, the first treatment is simpler, only this value plus 96 (128-32) can be 2, the second need to be divided into four, each paragraph to 2 into the system, see which one began to become 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;
 }//below 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 turn 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 ("Incorrect IP address!");
 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 + = ":";
 }//above produce V6 address section correct sixtemp = "0000:0000:0000:0000:0000:FFFF:" + sixtemp;
return sixtemp; }

The

, of course, includes in the above method the regular judgment of the IPV4 address and mask, which supports masks in the form of 255.255.255.0 and/or 32.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.