Querying an instance of an IP address by ipip.net implementing a database

Source: Internet
Author: User
The last time we introduced the method of querying IP address details using pure database.
However, the pure database is provided by the user feedback, a lot of data description is not accurate, so I went online to find some other IP database, finally found ipip.net this site provides IP database.

IPIP offers a free version of the database available for both paid and free two versions, which we can use directly.
Download address https://www.ipip.net/download.html (need to register an account first)
There is a PHP parsing class in the package, there is a 17monipdb.dat file is the database, we just need to use it.

Copy the 17monipdb.dat to the program's home directory, using the following code:

Imports system.ioimports system.textpublic Class ipip Shared offset as Integer shared index as UInteger () = New UInt Eger (255) {} shared DataBuffer as Byte () shared IndexBuffer as Byte () shared lastmodifytime as Long = 0L Share    D ipfile as String shared rwlock as New threading.readerwriterlock Shared Sub New () Load ("17monipdb.dat") End Sub Shared Sub Load (ByVal filename as String) ipfile = New FileInfo (filename). FullName load () End Sub Shared Sub Load () rwlock. AcquireWriterLock ( -1) Dim fi as New FileInfo (ipfile) LastModifyTime = fi. Lastwritetime.ticks Try DataBuffer = File.readallbytes (fi.            FullName) Dim indexlength = Bytestolong (DataBuffer (0), DataBuffer (1), DataBuffer (2), DataBuffer (3)) IndexBuffer = New Byte (indexLength-1) {} array.copy (DataBuffer, 4, indexbuffer, 0, Indexlength) of Fset = CType (Indexlength, Integer) for LP As Integer = 0 to 255 index (LP) = Bytestolong (_ IndexBuffer (LP * 4 + 3), _                 INDEXBUFFER (LP * 4 + 2), _ IndexBuffer (LP * 4 + 1), _ IndexBuffer (LP * 4) _ ) Next Catch ex as Exception Throw ex End Try rwlock. Releasewriterlock () End Sub Private Shared Function Bytestolong (ByVal A as Byte, ByVal B as Byte, ByVal C as Byte, b Yval D as Byte) as UInteger Return (CType (A, UInteger) <<) or (CType (b, UInteger) << +) or (CType (c , UInteger) << 8) Or d End Function Shared function Find (ByVal IP As String) as String () Rwlock. AcquireReaderLock ( -1) Dim ips = IP.        Split (".") Dim Ip_prefix_value = Integer.parse (IPs (0)) Dim ip2long_value As Long = Bytestolong (byte.parse (IPs (0)), Byte.parse ( IPs (1)), Byte.parse (IPs (2)), Byte.parse (IPs (3))) Dim start = Index (ip_prefix_value) Dim Max_comp_len =offset-1028 Dim index_offset As Long = -1l Dim index_length As Integer =-1 Dim b As Byte = 0 Start = start * 8 + 1024x768 While start < Max_comp_len If Bytestolong (indexbuffer (start + 0), Indexbuf Fer (start + 1), IndexBuffer (start + 2), IndexBuffer (start + 3)) >= Ip2long_value then Index_offset = Byt  Estolong (b, indexbuffer (start + 6), IndexBuffer (start + 5), IndexBuffer (start + 4)) Index_length = &hff and IndexBuffer (start + 7) Exit while end If start + = 8 End while Dim Areabytes = New Byte (index_length-1) {} array.copy (DataBuffer, offset + index_offset-1024, areabytes, 0, Index_ Length) Dim ret as String () = Encoding.UTF8.GetString (areabytes). Split (VbTab) rwlock. Releasereaderlock () Return ret end Function End Class

This code I translated from the official C # version, and removed some redundant code, leaving only the core functionality.
Without the complete test, there is a bug please feedback to me.

The way to use it is simple:

DIM ret = Ipip. Find ("127.0.0.1" separates all information with newline Dim Ipdesc = String.Join (vbCrLf, ret)
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.