# [Algorithm] ASP quickly determines whether the IP address is China Telecom or China Netcom

Source: Internet
Author: User

Determine whether the IP source is China Telecom or China Netcom, which is often used in the background program of the site. the typical practice is to query the database. The database stores the IP address range of China Netcom (or China Telecom), and then determines whether the IP address is within the range of China Netcom (or China Telecom) through range search. however, you have to query the database every time, and the efficiency is obviously very low.

Let's talk about a super-fast algorithm. since it is extremely fast, it is nothing more than the complexity of O (1), so a huge buffer zone is opened up, with a classic space for time, through the table can be determined in one step. how can we define the table size and key? Start with the cnc.txt file.
Http://www.etherdream.com/FunnyScript/IPRange/CNC.txt)

This file is the route table of China Netcom. under careful observation, it is not difficult to find that the maximum number of mask bits is 24 (limit 255.0 ). in fact, the 24-bit mask is very at least. After all, only 256 IP addresses are allocated in a network segment of China Netcom, which is quite missing. since the maximum number of bits in the mask is 24, the last IP address is negligible, and the first three IP addresses have a total of 256 ^ 3 (= 16 m) combinations. therefore, the first three digits of the IP address are used as the key and 16 MB table length, which exactly defines the table of the IP address corresponding to the network segment. it can be represented as follows:

123.0.0.0/24 => table [123.0.0] = true

202.0.0.0/16 => table [202.0.0] = true
Table [202.0.1] = true
...
Table [202.0.255] = true

Take the first three digits of the IP address when detecting the IP address. Check whether the corresponding value in the table is true to determine the type of the IP address. in fact, in this example, IP addresses are only in the China Telecom and China Netcom statuses (non-China Telecom network segments). Therefore, each record can be saved in 1 bit. the memory occupied by the table is 16 Mb/8 = 2 MB. ASP is used to implement this function.

First, convert the route table into a cache table of 2 MB. considering the ASP running speed, the C program is used for direct processing and then saved as a binary file of 2 MB. ASP through adbdo. stream reads data streams and caches them in the appliction collection. the so-called data stream is actually a byte () array variable, which can be processed by binary functions such as midb and ASCB.

Initialization function:

`Sub Init() If LenB(Application("cnc")) Then         ExitSub     EndIf With Server.CreateObject("ADODB.Stream")         .Type = 1         .Open         .LoadFromFile Server.MapPath("CNC.dat")         Application("cnc") = .Read         .Close     End With End Sub`

Through the cache of the appliction set, you do not have to read the file every time. 2 MB memory is acceptable. the next step is to analyze the IP address and convert the first three digits into a number, because each record is saved by bit, so we need to divide 8 records to correspond to byte () location. finally, the mod operation is used to correspond to the specific bit of a specific byte. it seems a little complicated, but the implementation is quite simple:

`Function IPIsCNC(IP) Dim arr, val Dim c     arr = Split(IP, ".")     val = CLng(arr(0)) * 65536 + CLng(arr(1)) * 256 + CLng(arr(2))     c = AscB(MidB(Application("cnc"), val \ 8 + 1, 1))     IPIsCNC = _         (c And2^(val Mod8)) <> 0 End Function`

Ipiscnc (IP), returns whether the IP address is China Netcom.
Now the two key functions have been completed, and we will test:

`Sub main ()On Error resume nextInit ()If err thenResponse. Write "system error:" & err. DescriptionExit subEnd ifDim IPIP = request. servervariables ("remote_addr ")If ipiscnc (IP) thenResponse. Write IP & "belongs to China Netcom IP"ElseResponse. Write IP & "Telecom IP Address"End ifEnd subMain`

Considering that the init function requires file reading, an error capture is added. However, ipiscnc is normal, because remote_addr must return an IP address in the correct format.

Each time you access ASP, except for loading files for the first time, you only need three or four lines of code to determine the time required for the space.

(C code: http://www.etherdream.com/FunnyScript/IPRange/IP.c that converts a route table to a cached file)

Related Keywords:
Related Article

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.