To determine whether the source of IP is telecommunications or netcom

Source: Internet
Author: User

Determine the source of IP is telecommunications or netcom, often used in the site's background program. The typical course is to query the database tutorial, the database is stored in the IP range of Netcom (or telecommunications), and then through the range search to determine whether the IP in the area of netcom (or telecommunications). But every time you have to query the database, the efficiency is obviously very low.

Not much nonsense, let's start with a super fast algorithm. Since to be extremely fast, no more than O (1) of the complexity, so open up a huge buffer, with the classic space for time, by looking at the table one step can be judged. How do you define the size of the table and key? Look at this file first from Cnc.txt.
(Http://www.etherdream.com/funnyscript/iprange/cnc.txt)

This document is China Netcom's routing table. Careful observation, it is not difficult to find that the maximum number of masks is no more than 24 (that is, 255.255.255.0). In fact, the 24-bit mask is very at least, after all, a network segment of a netcom only divided 256 IP, is quite less. Since the mask number is only 24, So the last IP is negligible, and the first 3 bits of IP have a combination of 256^3 (=16m). So the first 3 bits of IP as key,16m table length, exactly define the IP corresponding network segment table. Can be image 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

Check the time to take the IP before 3 digits, it is possible to determine the type of this IP in the test table if the corresponding is true. In fact, in this case, only the IP and Netcom state (not netcom network segment as telecommunications), so only 1bit can save each record. The memory of the table takes only 16m/8= 2m. The following is to use the ASP tutorial to achieve this function.

First, the routing table is converted into a 2m-size cache table. Taking into account the speed of ASP, here in advance with the C program directly processing, and then save as a 2m binary file. asp reads the data stream through Adbdo.stream, and is cached in the Appliction collection. The so-called data stream is actually a byte () variable, which can be handled by MIDB,ASCB such binary functions.

Initialization function:

Sub init ()
       if LenB (Application ("CNC")) then
               Exit Sub
        End If

       with Server.CreateObject ("ADODB.stream")
  & nbsp;          . Type = 1
               Open
               loadfromfile Server.MapPath ("Cnc.dat")
               application ("cnc") =. Read
               Close
       End with
End Sub

/blockquote>


Through the cache of the Appliction collection, you don't have to read the file every time. 2m of memory is also acceptable. The next thing is to analyze the IP address and convert the top 3 digits to a number, because each record is saved in bits, so it's also divisible 8来 to byte () Position. Finally, the MoD operation is used to correspond to the specific bit of the byte. It's a bit complicated, but it's quite simple to realize:

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 and 2^ (Val mod 8)) <> 0
End Function


IPISCNC (IP), returns whether the IP address is netcom.

This key two functions are done, and then test:

Sub Main ()
On Error Resume Next

Init ()

If Err Then
Response.Write "System error:" & Err.Description
Exit Sub
End If

Dim ip
ip = Request.ServerVariables ("REMOTE_ADDR")

If IPISCNC (IP) Then
Response.Write IP & "belong to Netcom IP"
Else
Response.Write IP & "belong to telecom IP"
End If
End Sub
Main ()


An error catch is added, considering that the INIT function requires a read of the file. However, usually IPISCNC is not wrong, because REMOTE_ADDR return is necessarily the correct format of the IP.

Whenever you access ASP, in addition to the first time to load the file, the rest of the time only three or four lines of code can be judged, the real space to change time.

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.