leth / PHP-IPAddress

IP Address utility classes for PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong IP categorization

eteubert opened this issue · comments

The IP ::ffff:141.44.23.50 is categorized as IPv4 but it's IPv6.
It seems to be valid (based on http://www.helpsystems.com/intermapper/ipv6-address-validation).
I got the error report from podlove/podlove-publisher#600.

This is (roughly) how I use the library in my code:

<?php
use Leth\IPAddress\IP, Leth\IPAddress\IPv4, Leth\IPAddress\IPv6;

$ip = IP\Address::factory($_SERVER['REMOTE_ADDR']);
if (method_exists($ip, 'as_IPv6_address')) {
    $ip = $ip->as_IPv6_address();
}
$ip_string = $ip->format(IP\Address::FORMAT_COMPACT);

I'm using version 1.0.1 (but the categorization method didn't change in 1.0.2 so I don't think it matters).

There's a few places this would need to be fixed:

I noticed this was still outstanding (and is still an issue - for example, ::ffff:64.230.15.223 is rejected as an invalid IPv4 address.) I'll take a look at what is needed to solve this and see if I can handle it.

As another note to this, this is really only used for dual stack devices, and I'm starting to wonder if these should be cast to IPv4 representations. See http://www.tcpipguide.com/free/t_IPv6IPv4AddressEmbedding.htm for an example. I guess they should technically be represented in IPv6 format, since they are being input as an IPv4 address embedded in IPv6 - if anyone has any thoughts, let me know, and I'll consider it before starting work on this. (I realize this issue is from 2014, but on the off chance anyone is watching..)

Auto-casting them sounds nice but depending on where it was done it might make it impossible to represent that address in v6, in which case I'd err on the side of not auto-casting.

They certainly should be parsed if they're a legal address format.

Perhaps there's also some room to make it easier to switch between the two; we have v4 to v6, but not v6 to v4 and maybe we also want a method to ask if it's possible for a given address, although you could just attempt it and catch the exception.

@leth Yeah I think I agree. If it is input in IPv6 format, I think it should just remain that way, regardless of the circumstance.

I have this on my todo list to look at, as I need to fix it for my own use.. so I'll try to get a PR in pretty soon.

This should be resolved by #25 :)