How does JavaScript obtain IP address verification? ipvcriptipv6

Source: Internet
Author: User

How does JavaScript obtain IP address verification? ipvcriptipv6

1. What is IPV6?

To solve this problem, you can go to Baidu or Google to find it. In fact, it is mainly to solve the problem of insufficient IPV4 addresses. The V4 address is 32-bit, that is, 192.168.1.1, the V6 address is 128 bits, which is too many times larger than V4.

2. Does IPV6 have a subnet mask?

I can say yes or no. I can say yes because there is a prefix in IPV6. It is equivalent to the subnet mask in IPV4, but the name is different, because the IPV6 address is too big, it is said that it can give each sand address on the earth, so that the concept of subnet is relaxed, no need.

3. IPV6 address

I can find a lot of IPV6 addresses on the internet, and I will not list them in detail. Generally, 1205: fff0: ffd1: 1021/120. The following 120 indicates the prefix, it is equivalent to the subnet mask in IPV4.

4. IP Address Calculation Method

The Calculation of IPV6 and IPV4 network segments is the same. For IPV4, for example, 192.168.1.1/255.255.255.0, the network segment is from 192.168.1.0 ~ 192.168.1.255, how is this calculated? According to ISP regulations, its starting address is: this IP address and subnet mask are obtained by bit and operation; its ending address is: this IP address and subnet mask are obtained by bitwise OR operation (excluding the symbol bit.

IPV6 is the same as IPV4 in principle. Because the IPV6 prefix is too long, it cannot be expressed in the form of 255.255.255.255.0. Therefore, numbers such as 120 or 128 are usually written directly. 120 indicates that the first 120 bits of This prefix are both 1 and then 0. This prefix is equivalent to the subnet mask in IPV4. The calculated IPV6 address is the same as that in IPV4.

I have briefly explained the IPV6 knowledge above. Next I will handle the IPV6 website.

IPV6 URL writing must be compatible with the zero compression method. Therefore, you need to convert the IPV6 URL. The following is the conversion code of JS:

Function transitIp (ipaddr) // complete the 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] + = ":";}} /// after completing the preceding steps, place the content in ipaddrs, but return initaddr (ipaddrs) is not standard; // obtain the complete 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 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] {} (:|::) * [\ da-f] {} (:| ::)? $/I. test (str):/^ ([\ da-f] {} :) {7} [\ da-f] {} $/I. test (str );}

Next we will explain the conversion from IPV4 to IPV6:

It is very easy to convert IPV4 addresses to IPV6 URLs. You only need to convert IPV4 addresses into hexadecimal format, take two segments as a group, and then add: ffff to the front.

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 | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ 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 ("the subnet mask is incorrect! "); Return ;}} else {var mask = parseInt (fourmask, 10); if (mask <0 | mask> 32) {alert (" the subnet mask is incorrect! "); Return ;}} /*** the method for converting IPV4 to IPV6 is very simple. 1. convert each segment to hexadecimal format. 2. Add 0 to the beginning of less than two. 3. V4 generates four hexadecimal values. number, 4. Add "0000: 0000: 0000: 0000: 0000: ffff:" Before the first two and the last two, respectively. **/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: ffff:" + sixtemp; /*** the following processes the subnet mask. The subnet mask can be written in two ways: numbers or 255.255.255.0. 1. The first method is simple. You only need to add 96 (128-32) to this value) 2. The second type needs to be divided into four sections, and each segment is converted into a binary system. You can see which one is initially changed 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 ;}// determine the position var flagtemp = false below; 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 the address {var reg = fouraddr. match (/^ (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ 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 + = ":";}} // The above generated V6 address segment is correct sixtemp = "0000: 0000: 0000: 0000: 0000: ffff:" + sixtemp; return sixtemp ;}

Of course, the above method includes the regular identification of IPV4 addresses and masks. The mask can be in the form of 255.255.255.0 or/32.

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.