inet_pton()对于某些“看起来像" IPv6地址是不完整的. IPv4地址? [英] Is inet_pton() broken for some IPv6 addresses that "look like" IPv4 addresses?

查看:60
本文介绍了inet_pton()对于某些“看起来像" IPv6地址是不完整的. IPv4地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP 5.2.17版本,并且看到以下功能可以正常工作:

I'm using PHP version 5.2.17, and I see that the following works as expected:

$x = inet_pton('::F');
$y = inet_ntop($x);
print "::F -> $y\n";

Output: ::F -> ::f

但是以下内容却没有:

$a = inet_pton('::FEEF:1886');
$b = inet_ntop($a);
print "::FEEF:1886 -> $b\n";

Output: ::FEEF:1886 -> ::254.239.24.134

我希望第二个代码段能够产生以下输出:

I would have expected the second code snippet to produce this output:

::FEEF:1886 -> ::feef:1886

IPv6地址:: FEEF:1886是什么使PHP认为它确实是IPv4地址? inet_ntop/inet_pton转换可以与其他在高" 96位(例如:: F)中具有0的地址正常工作.

What is it about the IPv6 address ::FEEF:1886 that makes PHP think it is really an IPv4 address? The inet_ntop/inet_pton conversion works correctly with other addresses having 0 in the "high" 96 bits (e.g. ::F).

我首先想到的是这可能是我的PHP版本中的错误,但是使用此在线PHP沙箱在5.6.2之前的版本中,我看到了相同的行为.因此,这是故意的(在这种情况下,我非常想知道这种现象的原因),或者是在现代版本的PHP中仍然存在的错误.

My first thought was that this might be a bug in my version of PHP, but using this online PHP sandbox I see the same behavior for PHP versions up through 5.6.2. So either this is deliberate (in which case I would dearly like to know the reason for this behavior) or a bug which persists in modern versions of PHP.

附录: :我打开了 PHP Bug 69232 于2015年3月12日发布,原因是inet_ntop()对于::/96中地址的行为表现出明显的不一致.

ADDENDUM: I opened PHP Bug 69232 on March 12, 2015 for this apparent inconsistency in the behavior of inet_ntop() for addresses in ::/96.

推荐答案

kasperd,diskwuff和JC Sama提供的答案同时提供了有用的信息和解决方法,可能对其他SO读者有用,因此,我都对它们进行了投票.但是他们没有直接解决我的原始问题,因此我添加了这个答案:

Answers provided by kasperd, diskwuff, and JC Sama offer both helpful information and workarounds which are likely to be useful to other SO readers, so I have upvoted them all. But they do not address my original question directly, so I'm adding this answer:

PHP函数inet_pton()的行为是正确的.问题是inet_ntop()不能始终一致地处理::/96中的IPv6地址. 这是PHP中的错误.

The behavior of the PHP function inet_pton() is correct. The problem is that inet_ntop() does not treat IPv6 address in ::/96 consistently. This is a bug in PHP.

这篇关于inet_pton()对于某些“看起来像" IPv6地址是不完整的. IPv4地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆