CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2003
    Location
    Corvallis, OR
    Posts
    315

    validate client ip address is within range

    I have a range of IP address, 10.0.0.0/8 (which translates 10.0.0.0 to 10.0.0.255).... and I have an IP that I would like to validate is within this range. Like so...
    Code:
    string sClientIP = "10.0.0.2";
    string sValidIPRange = "10.0.0.0/8";
    
    bool bReturn = IsInRange(sClientIP,sValidIPRange);
    Question is... what does the IsInRange fxn look like and is there such a function already available in .NET? How do I parse out a block of IPs (10.0.0.0 to 10.0.0.255) from a single string that uses the slash (/) notation?

  2. #2
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: validate client ip address is within range

    Code:
    string[] parts = sValidIPRange.Split('.');
    string rangePart = parts[parts.Length - 1];
    
    string[] range = rangePart .Split('/');
    int startRange,endRange;
    bool res;
    res = Int32.TryParse(range[0],startRange);
    res = Int32.TryParse(range[1],endRange);
    
    parts = sClientIP.Split('.');
    int lastPart;
    res = Int32.TryParse(parts[parts.Length - 1],lastPart);
    
    return lastPart>=startRange && lastPart<=endRange;

  3. #3
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890

    Re: validate client ip address is within range

    Quote Originally Posted by sambo
    I have a range of IP address, 10.0.0.0/8 (which translates 10.0.0.0 to 10.0.0.255).... and I have an IP that I would like to validate is within this range. Like so...
    Code:
    string sClientIP = "10.0.0.2";
    string sValidIPRange = "10.0.0.0/8";
    
    bool bReturn = IsInRange(sClientIP,sValidIPRange);
    Question is... what does the IsInRange fxn look like and is there such a function already available in .NET? How do I parse out a block of IPs (10.0.0.0 to 10.0.0.255) from a single string that uses the slash (/) notation?
    >>>
    use System.Net.IPAddress
    Search Google for help
    - Software Architect

  4. #4
    Join Date
    May 2003
    Location
    Corvallis, OR
    Posts
    315

    Re: validate client ip address is within range

    I am very familiar with the IPAddress class... but how does that answer my question? I don't see any functionality in that class that does what I am asking. Am I missing something?

  5. #5
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: validate client ip address is within range

    u can validate IP addresses using regular expressions

  6. #6
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: validate client ip address is within range

    Quote Originally Posted by pareshgh
    use System.Net.IPAddress
    Search Google for help
    Quote Originally Posted by sambo
    I am very familiar with the IPAddress class... but how does that answer my question? I don't see any functionality in that class that does what I am asking. Am I missing something?
    IPAddress class do not support range of IPs in the way you described (with slash). You will have to parse the string somehow to retrieve the range. You might use regular expressions as aniskhan said, or parse the string like I suggested.

  7. #7
    Join Date
    May 2003
    Location
    Corvallis, OR
    Posts
    315

    Re: validate client ip address is within range

    Check this out... its outstanding!

    NetCalculator

  8. #8
    Join Date
    May 2003
    Location
    Corvallis, OR
    Posts
    315

    Re: validate client ip address is within range

    To pareshgh...

    I find it very interesting that you would give me negative feedback on a question I posed that you yourself gave an insufficient answer to.

    To answer your admonition about searching for the answer... I did search for the answer... and I finally found it after 3 DAYS!

    If you don't like my question, then don't bother piping in with a mediocre reply.

  9. #9
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: validate client ip address is within range

    I must agree with sambo. It appears that pareshgh decided to give everybody bad reputation, regardless of the content of the message, although he gave answer which is not so good.
    Well, I hope everybody will think twice before spreading bad reputation.

  10. #10
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890

    Re: validate client ip address is within range

    I didn't really intend to decrease anyone's reputation..

    thanks for pointing it out.

    let me know how can I spread reputation for you all. thanks !!

    bye and have a nice day..
    - Software Architect

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured