生成给定范围内的IPv6地址列表

问题描述 投票:0回答:1

在Maria DB表中,我有两个varbinary(16)字段,表示IPv6范围的起点和终点的IPv6地址。

我想使用PHP在此范围之间循环,并生成范围内的每个IPv6地址。我试图将二进制转换为十进制来执行循环并增加十进制数,但循环不会产生任何迭代。

有帮助吗?

//The $IpStart_v6_FromDb/$IpStart_v6_End Vars are produced with INET6_ATON MariaDB function
$IpStartBin = decbin($IpStart_v6_FromDb);
$IpEndBin = decbin($IpEnd_v6_FromDb);
$ArIpRange = array();
$ArIpRange[] = $IpStartBin;
$x=0;
for(;;)
{
    if ($IpStartBin==$IpEndBin) break;
    $tLastIpBin = $ArIpRange[$x];
    $tNextIpBin = decbin( (bindec($tLastIpBin) + 1) );
    if ($tNextIpBin==$IpEndBin) break;
    $ArIpRange[] = $tNextIpBin;
    $x++;
}
foreach ($ArIpRange as $v)
{
    echo "<br>IP range item:<br>".base64_encode($v); //debug
}

[编辑]

我很尴尬地说我认为IPv6地址的长度是64位。

php ip-address ipv6
1个回答
1
投票

所以,一些简单的故障排除或reading the manual会告诉你,decbin期望一个整数作为输入。因此,你可以获得两个变量的零回报。

此外,即使您确实纠正了这个问题(通过使用bindec),您也在谈论128位数字,除非您是未来的,否则PHP本身不能处理。

我建议将它们作为字符串处理。首先使用来自this answer的代码对它们进行标准化(填充缺失的零并用零替换::),使用this answer中的代码查找并删除匹配的前缀,然后通过将它们转换为更小的数字来处理其余的。

并且,如评论中所述,请确保您不要尝试处理太大的范围,否则您将使您的服务器不满意。

<?php
// https://stackoverflow.com/a/55521768/1255289
// Generate a list of IPv6 addresses within a given range

function expand_ipv6(string $ip): ?string
{
    // https://stackoverflow.com/a/12095836/1255289
    $hex = unpack("H*", inet_pton($ip))[1] ?? "";
    return (strlen($hex) === 32) ? implode(":", str_split($hex, 4)) : null;
}

$IpStart_v6_FromDb = "2001:db8::1234";
$IpEnd_v6_FromDb = "2001:db8::1299";

$ip1 = expand_ipv6($IpStart_v6_FromDb);
$ip2 = expand_ipv6($IpEnd_v6_FromDb);
if ($ip1 === null || $ip2 === null) {
    die;
}

// https://stackoverflow.com/a/35838357/1255289
// length is 39 to account for 7 colons
for ($i = 0; $i < 39 && $ip1[$i] === $ip2[$i]; $i++);

$ipv6_prefix = substr($ip1, 0, $i);
$ipv6_start = hexdec(substr($ip1, $i));
$ipv6_end = hexdec(substr($ip2, $i));

if (strlen($ipv6_prefix) < 26) {
    // adjust this to requirements to prevent too large ranges
    die;
}

for ($a = $ipv6_start; $a <= $ipv6_end; $a++) {
    $hex = dechex($a);
    printf("%s%s\n", $ipv6_prefix, implode(":", str_split($hex, 4)));
}
© www.soinside.com 2019 - 2024. All rights reserved.