Re: 0.0.0.0 & iplong()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 06/05/2012 05:40 AM, Matijn Woudt wrote:
On Tue, Jun 5, 2012 at 1:07 PM, jas<jason.gerfen@xxxxxxxxx>  wrote:
On 06/04/2012 12:48 PM, Matijn Woudt wrote:

On Mon, Jun 4, 2012 at 8:38 PM, jas<jason.gerfen@xxxxxxxxx>    wrote:

On 06/04/2012 11:33 AM, Matijn Woudt wrote:


On Mon, Jun 4, 2012 at 6:54 PM, jas<jason.gerfen@xxxxxxxxx>      wrote:


Not sure if this is a bug or not...

I have run into an error when performing a conditional using iplong()
and
the ~ bitwise operator

$ip = '0.0.0.0';
$mask = '24';

$end = (ip2long($ip) || (~ip2long($mask))) + 1;

PHP Fatal error:  Unsupported operand types

I even tried to typecast the mask to (int)


The error is probably not where you suspect it to be. ip2long will
return false for ip2long($mask), because $mask is not a valid IP
address. The ~ operator is not supported for false.

- Matijn



Strange... this works

$ip = '10.0.2.0';
$mask = '24';

Anything using 0.0.0.0 fails using the bitmask to calculate valid CIDR
ranges.


Yes, because with $ip being 0.0.0.0, the result of ip2long($ip) will
be zero. Zero evaluates to false when PHP is going to parse the ||
operator, and because of that, it continues to parse the rest.
When the result of ip2long($ip) is greater that zero, in case of some
other valid IP, it will evaluate to true and PHP will not even execute
the rest after the || because the result will be true anyway.
Did you mean to use a single | operator, as a bitwise OR?

- Matijn


With any ACL rule using iptables, hosts.allow/hosts.deny, routers where
0.0.0.0/24 is specified (all of the internet ipv4 addresses) this is a valid
range using CIDR notation.

It seems like a bug to me


Nope, there's no bug here. Can you explain what you're trying to do
with ip2long($mask)?

- Matijn

As stated previously using CIDR notation such as 192.168.0.0/24, 10.0.0.0/24 perform validation on an IP existing within said subnet range.

visiting ip: 192.168.0.22

acl allow range: 192.168.0.0/24
acl deny range: 0.0.0.0/24

CIDR notation allows me to utilize shorthand notation for specifying a range of IP's

In those two cases any ip within 192.168.0.0 - 192.168.0.255 is allowed while anything else residing between 0.0.0.0 - 255.255.255.255 is denied

The problem is I can perform operations on CIDR ranges such as the following:
192.168.0.0/24
192.168.1.0/16
10.0.0.0/24

But cannot use the traditional 0.0.0.0/24 because iplong() evaluates 0.0.0.0 as false thereby invalidating a valid CIDR notation when evaluated as

(ip2long($ip) || (~ip2long($mask))) + 1;

From my own tests only 0.0.0.0/24 fails or as shown in the example code above

(ip2long(0.0.0.0) || (~ip2long(24))) + 1;

The class I am using to test with can be found at http://www.php.net/manual/en/function.ip2long.php#108812

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux