2

I've read multiple questions (and answers) before I posted this, but I can't seem to get it somehow.. How do I calculate an IP range using REMOTE_ADDR? For example I've given exlusive access to the range: 197.168.178.1 - 197.168.178.10 When I access the site with 197.168.178.6 I want to get access. How do I manage that?

What I got so far is a List of the class IpRange - these IP addresses are specified in the database and can be altered to any range. The ID is used to identify which range is accessing the site.

public class IpRange
{
    public int ID { get; set; }
    public System.Net.IPAddress RangeStart { get; set; }
    public System.Net.IPAddress RangeEnd { get; set; }
}

And I've tried to match the incoming IP address (Request.ServerVariables["REMOTE_ADDR"]) to this range using the next method;

    private static int MatchAddress(System.Net.IPAddress IP, List<IpRange> range)
    {
        int Value = -1;
        foreach (IpRange item in range)
        {
            if (item.RangeStart.Address >= IP.Address && IP.Address <= item.RangeEnd.Address)
            {
                Value = item.ID;
                break;
            }
        }
        return Value;
    }

The method Address is obsolete and most likely returns something completely different than I think it does; according the warning I should use IPAddress.Equals, but that method can only compare two addresses to see if it's the same, I want to know if it's defined within the range.

What is a method to match this (or should I use another component to do this)?

P.S. My knowdledge of networking is well below par, I know that, so it might be a really dumb question, but I'm lost atm.

Leon
  • 919
  • 6
  • 21
  • Your logic is the wrong way around anyway - You're looking for a value such that the *start* of the range is **greater** or equal to it, and the *end* of the range is **lesser** or equal to it. – Damien_The_Unbeliever Jul 18 '13 at 12:29
  • [This answer](http://stackoverflow.com/a/2138724/15498) looks like it might be of use to you. – Damien_The_Unbeliever Jul 18 '13 at 13:10
  • @Damien_The_Unbeliever indeed I see, my logic is flawed; I've been trying all kinds of stuff so most likely this is edit 9000 and I've overlooked that part. The answer you suggested is indeed working for me - or at least 99% of it. Is there an option to mark this question as duplicate? – Leon Jul 18 '13 at 13:52
  • @Leon - I can't remember whether you can participate in a "vote to close" (you need a high rep to do it to other peoples questions, but I can't remember the rules for what you can do on your own questions). I've added a close vote with a link to that question. – Damien_The_Unbeliever Jul 18 '13 at 14:38

2 Answers2

1

You can use Regular Expressions in this case. sPattern is the String that contains the Regular Expression and IPs contains the test cases. Here's an example of a Console Application:

using System;
using System.Text.RegularExpressions;

namespace TestsApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string sPattern = "197.168.178.([1-9]|10)$";
            string[] IPs = 
                {
                    "197.168.178.1",
                    "197.168.178.10",
                    "197.168.178.5",
                    "197.168.178.255",
                    "255.255.255.0"
                };
            foreach (string s in IPs)
            {
                Console.Write("{0,24}", s);

                if (Regex.IsMatch(s, sPattern, RegexOptions.IgnoreCase))
                    Console.WriteLine("  (match for '{0}' found)", sPattern);
                else
                    Console.WriteLine("  No match!");
            }
            Console.ReadLine(); //Pause
        }
    }
}
ᗩИᎠЯƎᗩ
  • 2,122
  • 5
  • 29
  • 41
  • The IP ranges are not specified by me and can be altered in the database, so hardcoded regex isn't an option :( -- updated the question for that. – Leon Jul 18 '13 at 12:09
  • Well, this was just an example. You can easily write an algorythm to manage the building of the right regex. – ᗩИᎠЯƎᗩ Jul 18 '13 at 12:12
  • Either Regex or even by plain foreach you could make an alogrithm. How will the database specify those ranges , will they be 2 values min n max? – Rameez Ahmed Sayad Jul 18 '13 at 12:22
1

Normally you define a ip address with a range by providing an address and a mask! This tuple can be checked against a third address to find out if it matches:

IP Address: 192.168.178.1
Netmask:    255.255.255.16

Check addr: 192.168.178.9 -> falls into range.
Check addr: 192.168.238.3 -> doesn't fall into range.

That's the way networking happens. ;)

Oliver
  • 43,366
  • 8
  • 94
  • 151