General class used to determine the IP Address Source

Source: Internet
Author: User

Nowadays, many software programs need to determine the ip Address Source function. They can make pure ip database into a set of common modules.

1. First download from the Internet to the pure ip database mdb version, go to SQL

2 go to SQL

3. The database structure is as follows:

4 Content

5 What do the last two fields mean? It should be the value corresponding to the IP address.
Verify

3*256 ^ 3 = 50331648
4*256 ^ 3 = 67108864
Startid 5.0331648E + 7
Endid 6.7108864E + 7

In this way, the search is convenient. You only need to convert the ip address to the corresponding value, and then determine the ip address range in which the record is located. Then you can know the address and test it.


Select 4*256*256*256
Select top 1 * from ip

50331648
67108864
3.0.0.0 3.00000000255 New Jersey General Electric Company NULL 5.0331648E + 7 6.7108864E + 7
It's exactly the same as we thought.

6. Take an IP address and test how 218.57.241.35 is converted to 3545755940. Why are two records correct? Is the last one correct, it may be because the numeric values converted to scientific notation are fuzzy, so there are two matching records, and the mandatory modification to float comparison should be different. Modify the SQL statement and then perform the test.

7. You can write a program. Write a helper class.

Public static string GetIpRealWorldAddress (string ipAddress)
{
If (! IpAddressAvailable (ipAddress ))
{
Return "ip address problems ";
}

Long value = GetIPCount (ipAddress );
String SQL = string. format ("select * from ip where convert (float, startid) <= {0} and convert (float, endid) >={ 0}", value );
Using (SqlConnection _ SqlConnection = new SqlConnection (SQLHelper. conn ))
{
SqlCommand _ SqlCommand = new SqlCommand (SQL, _ SqlConnection );
_ SqlConnection. Open ();
SqlDataReader _ SqlDataReader = _ SqlCommand. ExecuteReader ();
If (_ SqlDataReader. Read ())
{
// [Startip], [endip], [country], [local], [startid], [endid]
String startip = (string) _ SqlDataReader ["startip"];
String endip = (string) _ SqlDataReader ["endip"];
String country = (string) _ SqlDataReader ["country"];
// Float local = (float) _ SqlDataReader ["local"];
Float startid = (float) _ SqlDataReader ["startid"];
Float endid = (float) _ SqlDataReader ["endid"];
Return country;
}
Else
{
Return "no matching record found! ";
}
}
}

 

// Obtain the ip address's long value 3.254.255.255 = 3*256 ^ 3 + 254*256 ^ 2
Public static long GetIPCount (string ipAddress)
{
IpAddress = ipAddress. Trim ();
String [] ipSecs = ipAddress. Split ('.');
Long value = 0;
For (int I = 0; I <4; I ++)
{
Int ipSecDec = int. Parse (ipSecs [I]);
Int power = 3-i;
Long ipSecValue = (long) (ipSecDec * Math. Pow (256, power ));
Value = value + ipSecValue;
}
Value = value + 1;
Return value;
}

 

/// <Summary>
/// Check whether the IP address is faulty. 1. Check whether the number of CIDR blocks is a number and whether the number is in the range of 0.
/// Monitoring from the above three aspects
/// </Summary>
/// <Param name = "ipAddress"> </param>
/// <Returns> </returns>
Private static bool IpAddressAvailable (string ipAddress)
{
IpAddress = ipAddress. Trim ();
String [] ipSecs = ipAddress. Split ('.');
If (ipSecs. Length! = 4) return false;

// If each segment can be converted to an int value, the returned result is true.
For (int I = 0; I <ipSecs. Length; I ++)
{
Try
{
Int ipSecDec = int. Parse (ipSecs [I]);
If (ipSecDec <0 | ipSecDec> 255)
{
Return false;
}
}
Catch
{
Return false;
}
}
Return true;
}
}

8. pass the test

OK. I hope you like it for the first time.

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.