IP address limitation, if it is between the range of IP addresses

Well, Friday afternoon, and I had a long week, so I would really like to help! I currently have a list of IP ranges, as shown below:

List<IPRange> ipRanges = new List<IPRange>(); ipRanges.Add(new IPRange { From = "145.36.0.0", To = "145.36.255.255" }); ipRanges.Add(new IPRange { From = "194.183.227.184", To = "194.183.227.191" }); ipRanges.Add(new IPRange { From = "193.131.192.0", To = "193.131.223.255" }); 

After receiving the IP address of the client, if it is somewhere between these sets of ranges, they need to be redirected to another location.

For instance,

If someone visited the site with IP 192.168.0.1 , they will be allowed access. If they are visited using 145.36.1.0 , they will not be allowed access, since it falls between the first range in this list.

I could divide each IP into a period and determine where the range starts to change, and then perform a comparison, but it will be hard on the server.

I know that IPs are just decimal numbers, but I'm not sure how this works.

Has anyone come across this before?

Greetings, Sean.

+4
source share
5 answers

Convert each IP address to a number , and then check if the user's address is between these numbers.

 public double Dot2LongIP(string DottedIP) { int i; string [] arrDec; double num = 0; if (DottedIP == "") { return 0; } else { arrDec = DottedIP.Split('.'); for(i = arrDec.Length - 1; i >= 0 ; i --) { num += ((int.Parse(arrDec[i])%256) * Math.Pow(256 ,(3 - i ))); } return num; } } 
+7
source

I would convert the IP addresses to 32-bit numbers and then do a simple> = From and <= To to check if it is in the range.

For example, 192.168.1.1 → 192 * 256 ^ 3 + 168 * 256 ^ 2 + 1 * 256 + 1.

Work with your values, 145.36.0.0 → 2435055616 and 145.36.0.0 → 2435121151. So, 145.36.200.30 → 2435106846 and falls into this range, so it is valid. But 145.35.255.255 → 2435055615 is not in the range (a little), so it fails.

+7
source

I would write my IPRange class so that getter / setters internally convert the IP string to a number:

 new IPRange { From = "145.36.0.0", To = "145.36.255.255" } 

Internally installed:

 int from = 145036000000; int to = 145036255255; 

Then add the .IsInRange method (ip string), which converts the incoming IP address to the form name int and does the usual comparison.

 public bool IsInRange(string ipStr) { int ip = ConvertIPStringToInt(ipStr); return (from <= ip && ip <= to); } 

Thus, you do not need to split the IP addresses in the range by periods each time it is verified.

+1
source

Just for fun (and some semblance of completeness) - another way obvious for this is to ensure that you always use 3 digits for each segment of the IP address when saving as a string, i.e. 145.36.0.0 should be 145.036. 000,000 - this way the strings will be directly comparable.

And it would be less obvious to have an explicit class of IP addresses and collapse your own set of comparison logic (I am skeptical that in the depths of the .NET platform there is still something similar ...)

+1
source

I read about this a few days ago.

You can convert and compare your IP ranges.

 IF exists (SELECT * from dbo.sysobjects WHERE id = object_id(N'[dbo].[IsPrivateIP]') AND OBJECTPROPERTY(id, N'IsScalarFunction') = 1) DROP FUNCTION [dbo].[IsPrivateIP] GO CREATE FUNCTION dbo.IsPrivateIP( @vcIPAddress varchar(15)) /************************************************************************** DESCRIPTION: Returns Numeric IP if not private, otherwise returns null PARAMETERS: @vcIPAddress - The string containing a valid IP RETURNS: IP converted to bigint or null if a private IP USAGE: SELECT dbo.IsPrivateIP( '207.158.26.10') DEPENDANCIES: dbo.IPStringToNumber() function AUTHOR: Karen Gayda DATE: 06/11/2003 MODIFICATION HISTORY: WHO DATE DESCRIPTION --- ---------- --------------------------------------------------- ***************************************************************************/ RETURNS bigint AS BEGIN DECLARE @biClassALo bigint , @biClassAHi bigint , @biClassBLo bigint , @biClassBHi bigint , @biClassCLo bigint , @biClassCHi bigint , @biIP bigint, @bTemp int SET @biClassALo = 167772160 SET @biClassAHi = 169549375 SET @biClassBLo = 2885681152 SET @biClassBHi = 2887778303 SET @biClassCLo = 3232235520 SET @biClassCHi = 3232301055 SET @biIP = dbo.IPStringToNumber(@vcIPAddress) IF @biIP BETWEEN @biClassALo AND @biClassAHi OR @biIP BETWEEN @biClassBLo AND @biClassBHi OR @biIP BETWEEN @biClassCLo AND @biClassCHi SET @biIP = NULL RETURN @biIP END GO 

Here is the IPStringToNumber function that he needs:

 IF exists (SELECT * from dbo.sysobjects WHERE id = object_id(N'[dbo].[IPStringToNumber]') AND OBJECTPROPERTY(id, N'IsScalarFunction') = 1) DROP FUNCTION [dbo].[IPStringToNumber] GO CREATE FUNCTION dbo.IPStringToNumber( @vcIPAddress varchar(15)) /************************************************************************** DESCRIPTION: Returns Numeric IP, otherwise returns null PARAMETERS: @vcIPAddress - The string containing a valid IP RETURNS: IP converted to bigint or null if not a valid IP USAGE: SELECT dbo.IPStringToNumber( '10.255.255.255') AUTHOR: Karen Gayda DATE: 06/11/2003 MODIFICATION HISTORY: WHO DATE DESCRIPTION --- ---------- --------------------------------------------------- ***************************************************************************/ RETURNS bigint AS BEGIN DECLARE @biOctetA bigint, @biOctetB bigint, @biOctetC bigint, @biOctetD bigint, @biIP bigint DECLARE @tblArray TABLE ( OctetID smallint, --Array index Octet bigint --Array element contents ) --split the IP string and insert each octet into a table row INSERT INTO @tblArray SELECT ElementID, Convert(bigint,Element) FROM dbo.Split(@vcIPAddress, '.') --check that there are four octets and that they are within valid ranges IF (SELECT COUNT(*) FROM @tblArray WHERE Octet BETWEEN 0 AND 255) = 4 BEGIN SET @biOctetA = (SELECT (Octet * 256 * 256 * 256) FROM @tblArray WHERE OctetID = 1) SET @biOctetB = (SELECT (Octet * 256 * 256 ) FROM @tblArray WHERE OctetID = 2) SET @biOctetC = (SELECT (Octet * 256 ) FROM @tblArray WHERE OctetID = 3) SET @biOctetD = (SELECT (Octet) FROM @tblArray WHERE OctetID = 4) SET @biIP = @biOctetA + @biOctetB + @biOctetC + @biOctetD END RETURN(@biIP) END 

Literature:

http://www.sqlservercentral.com/scripts/Miscellaneous/31036/

http://www.sqlservercentral.com/Authors/Scripts/kgayda/17134/

0
source

All Articles