php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75715 ip2long
Submitted: 2017-12-21 16:01 UTC Modified: 2017-12-22 09:45 UTC
From: igronus at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 7.1.12 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: igronus at gmail dot com
New email:
PHP Version: OS:

 

 [2017-12-21 16:01 UTC] igronus at gmail dot com
Description:
------------
Function ip2long (and filter_var with second argument FILTER_VALIDATE_IP too) doesn't return false when argument is ip-address with zero in last octet (127.0.0.0 for example). However this argument is non-valid ip-address.

Test script:
---------------
$ip = '192.168.0.0';
var_dump(ip2long($ip));
var_dump(filter_var($ip, FILTER_VALIDATE_IP));


Expected result:
----------------
It must return false in both cases.

Actual result:
--------------
int(3232235520)
string(11) "192.168.0.0"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-12-21 16:10 UTC] levim@php.net
According to https://serverfault.com/a/10989/100096:

> It depends on the subnet of the IP address in question. In general, the first and last addresses in a subnet are used as the network identifier and broadcast address, respectively. All other addresses in the subnet can be assigned to hosts on that subnet.

> For example, IP addresses of networks with subnet masks of at least 24 bits ending in .0 or .255 can never be assigned to hosts. Such "last" addresses of a subnet are considered "broadcast" addresses and all hosts on the corresponding subnet will respond to it.

> Theoretically, there could be situations where you can assign an address ending in .0: for example, if you have a subnet like 192.168.0.0/255.255.0.0, you are allowed to assign a host the address 192.168.1.0. It could create confusion though, so it's not a very common practice.

> In your example 10.6.43.0 with subnet 255.255.252.0 (22 bit subnet mask)
means subnet ID 10.6.40.0, a host address range from 10.6.40.1 to 10.6.43.254 and a broadcast address 10.6.43.255. So in theory, your example 10.6.43.0 would be allowed as a valid host address.

Server Fault is not necessarily authoritative and I am not a subject expert. Can you provide a link to an RFC or some other authoritative source indicating it is not permitted? Otherwise I will close this as not-a-bug.
 [2017-12-21 16:13 UTC] levim@php.net
-Status: Open +Status: Feedback
 [2017-12-22 07:51 UTC] igronus at gmail dot com
It is legal:
https://en.wikipedia.org/wiki/IPv4#Addresses_ending_in_0_or_255

However, I think it should be mentioned in documentation because some devices and software can't work with such ip-addresses.
 [2017-12-22 09:45 UTC] daverandom@php.net
-Status: Feedback +Status: Not a bug
 [2017-12-22 09:45 UTC] daverandom@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

Any valid dotted-quad string is a valid IPv4 address, including 0.0.0.0 and 255.255.255.255. Whether or not it is a valid *host* address requires more information than just the address - specifically it also requires the subnet mask. Regardless, neither ip2long() nor FILTER_VALIDATE_IP are intended to validate whether something is a valid host address.

In general this not particularly useful information. Knowing whether something is a valid host address within a given subnet does not tell you whether there is actually a reachable host with that address, nor does it tell you whether a particular service is provided by that host. As an application developer, one would still need to attempt communication with that host before a meaningful error could be generated.

As a side note, ip2long() is generally not particularly useful in PHP. None of PHP's built-in functions work with IP addresses as integers, and treating them as such doesn't play nice with code that wants to support both 32 and 64 bit systems. If you want to convert an IP address to a form that can be used for bitwise operations (such as validating whether an address is a valid host address in a subnet ;-) ), use inet_pton() instead, which converts it to a string (byte array) which can used directly with all bitwise operators except shifts. It also supports IPv6, which can be easily disambiguated by checking length of the returned string.

With all that said, I would like to see some better built-in primitives for working with things like networking information in PHP, but that is not really bug, it's more of a feature request that would likely be better served either by expanding an existing extension or creating a new extension.

I'm closing this bug because both of the mentioned APIs work as intended, but if you would like to discuss this matter further please come and find me in room #11 (PHP) on Stack Overflow chat, or email me directly.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 07:01:29 2024 UTC