Validating an IP Range [Solved]

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3752
Location: USA, GA

PostPosted: Sat Jun 16, 2007 6:45 am    Post subject: Validating an IP Range [Solved] Reply with quote

Hello Everyone,

I was wondering if you could help me out with something. I would like to know how to check an IP Range so that I can write a simple IP Range Banning script. This will be for a major project I'm going to be working on and I want to make sure the code is valid.

I'm guessing the code could be like this?

Code:
<?php
if (127.0.0.0 <= 127.0.0.6) {
    echo 'This IP Address Range is banned!';
}
?>


Any help on this would be wonderful. Maybe Aprelium could provide a function for how they validate their ranges.
Like this: http://www.aprelium.com/data/doc/2/abyssws-win-doc-html/ipformat.html

Note: The first 4 are very easy to do (Regular Expression) in PHP, but when it comes to the last ones, I have no clue what to do in order to validate them.

Kind regards, Josh.


Last edited by TRUSTAbyss on Sat Jun 16, 2007 7:51 pm; edited 2 times in total
Back to top View user's profile Send private message Visit poster's website
pkSML
-


Joined: 29 May 2006
Posts: 955
Location: Michigan, USA

PostPosted: Sat Jun 16, 2007 12:51 pm    Post subject: Reply with quote

You don't need to worry about octets.

Use ip2long

This example has a link to a functional demo.
_________________
Stephen
Need a LitlURL?


http://CodeBin.yi.org
Back to top View user's profile Send private message Visit poster's website
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3752
Location: USA, GA

PostPosted: Sat Jun 16, 2007 7:48 pm    Post subject: Reply with quote

Hello pkSML,

I looked at the user submited comments and found the function I'm looking for. The function doesn't support the Regex format but that's so easy to do without any help. The following function supports everything that Abyss supports!

Update: The code will now check for Wildcard characters to see if it's a Regex. Enjoy!

Function: netMatch()
Created By: Dirk, Stephane, and TRUSTAbyss

Supported Syntax:

- *
- 202.*
- 202.1.*
- 202.1.192.*
- 202.1.192.0-202.1.192.255: a range of IPs
- 200.36.161.0/24: a range of IP by using net masking
- 200.36.161/24: a shorten syntax similar to the above

Note: A single IP Address can also be used.

Code:
<?php
/**
 * Function: netMatch()
 * Created By: Dirk, Stephane, and Joshua H. (TRUSTAbyss)
 *
 * This function returns a boolean value.
 * Usage: netMatch("IP RANGE", "IP ADDRESS")
 */
 
function netMatch($network, $ip)
{
    $network = trim($network);
    $ip = trim($ip);
    $d = strpos($network, '-');
   
    if (preg_match("/^\*$/", $network))
    {
        $network = str_replace('*', '^.+', $network);
    }
    if (!preg_match("/\^\.\+|\.\*/", $network))
    {
        if ($d === false)
        {
            $ip_arr = explode('/', $network);
 
            if (!preg_match("/@\d*\.\d*\.\d*\.\d*@/", $ip_arr[0], $matches))
            {
                $ip_arr[0] .= '.0';    // Alternate form 194.1.4/24
            }

            $network_long = ip2long($ip_arr[0]);
            $x = ip2long($ip_arr[1]);
            $mask = long2ip($x) == $ip_arr[1] ? $x : (0xffffffff << (32 - $ip_arr[1]));
            $ip_long = ip2long($ip);
 
            return ($ip_long & $mask) == ($network_long & $mask);
        }
        else
        {
            $from = ip2long(trim(substr($network, 0, $d)));
            $to = ip2long(trim(substr($network, $d+1)));
            $ip = ip2long($ip);
       
            return ($ip >= $from and $ip <= $to);
        }
    }
    else
    {
        return preg_match("/$network/", $ip);
    }
}

// Here's an Example

if (netMatch('127.0.0.0-127.0.0.4', $_SERVER['REMOTE_ADDR']))
{
  echo 'This address range has been banned!';
}
?>


Note: I've tested the above code and it works perfectly! The Regex matches are accurate and the Ranges format also work good. I'm not sure if the function supports the last one that Abyss supports (aaa.bbb.ccc.ddd-iii.jjj.kkk.lll/n). Please test this function and let me know of any bugs. Thank You!

Kind regards, Josh
Back to top View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group