将IPv6 CIDR分割为/64块,在php中。

12

我想创建一个脚本,以IPv6范围或CIDR作为输入,并输出/64块的列表(或每个/64块的第一个IP)。

我有一个类似于IPv4 IP的功能,但我缺乏重新用于ipv6的理解。

Function BreakTo30($CIDR)
{
    $CIDR = explode("/", $CIDR); // this breaks the CIDR block into octlets and /notation
    $octet = ip2long($CIDR[0]); //turn the first 3 octets into a long for calculating later
    $NumberOf30s = pow(2,(30-$CIDR[1]))-1; //calculate the number of /30s in the CIDR block
    $OutputArray = array();
    for ($i=-4; $i<4 * $NumberOf30s; $OutputArray[] = (long2ip($octet + ($i += 4)))); //fancy math to output each /30
    return $OutputArray; //returns an array of ranges

}

ip2long和long2ip仅适用于IPv4。

2个回答

8

有一个解决方案符合你的要求,需要你安装GMP或BCMATH扩展程序,因为在这种情况下你会处理非常大的十进制数。

<?php
$cidr ="2001:adb8:85a3:1111:1111:8a2e:3270:7334/120";
$all = listIPv6InBlock($cidr);

echo "CIDR is $cidr<br/>\r\n";
echo "Count is ". count($all)."<br/>\r\n";

printAddresses($all);

function listIPv6InBlock($CIDR)
{
    $CIDR = explode("/", $CIDR); // this breaks the CIDR block into octlets and /notation
    $octet = ip2long_v6($CIDR[0]); //turn the first 3 octets into a long for calculating later
    $NumberOfIPs = pow(2,(128-$CIDR[1]))-1; //calculate the number of /30s in the CIDR block
    $OutputArray = array();
    for ($i=0; $i< $NumberOfIPs; $i++){
        $OutputArray[] = long2ip_v6(bcadd($octet,"$i"));
    }
    return $OutputArray; //returns an array of ranges
}

function printAddresses($arr){
    foreach($arr as $ip){
        echo "$ip <br/>\r\n";       
    }
}

/*
 *The following two functions are credited to (https://stackoverflow.com/users/67332/glavi%C4%87) 
 * who gave this answer :https://dev59.com/hGMl5IYBdhLWcg3wmn44#19497446
 */
function ip2long_v6($ip) {
    $ip_n = inet_pton($ip);
    $bin = '';
    for ($bit = strlen($ip_n) - 1; $bit >= 0; $bit--) {
        $bin = sprintf('%08b', ord($ip_n[$bit])) . $bin;
    }

    if (function_exists('gmp_init')) {
        return gmp_strval(gmp_init($bin, 2), 10);
    } elseif (function_exists('bcadd')) {
        $dec = '0';
        for ($i = 0; $i < strlen($bin); $i++) {
            $dec = bcmul($dec, '2', 0);
            $dec = bcadd($dec, $bin[$i], 0);
        }
        return $dec;
    } else {
        trigger_error('GMP or BCMATH extension not installed!', E_USER_ERROR);
    }
}

function long2ip_v6($dec) {
    if (function_exists('gmp_init')) {
        $bin = gmp_strval(gmp_init($dec, 10), 2);
    } elseif (function_exists('bcadd')) {
        $bin = '';
        do {
            $bin = bcmod($dec, '2') . $bin;
            $dec = bcdiv($dec, '2', 0);
        } while (bccomp($dec, '0'));
    } else {
        trigger_error('GMP or BCMATH extension not installed!', E_USER_ERROR);
    }

    $bin = str_pad($bin, 128, '0', STR_PAD_LEFT);
    $ip = array();
    for ($bit = 0; $bit <= 7; $bit++) {
        $bin_part = substr($bin, $bit * 16, 16);
        $ip[] = dechex(bindec($bin_part));
    }
    $ip = implode(':', $ip);
    return inet_ntop(inet_pton($ip));
}
?>

如您所见,此解决方案是对小数(作为字符串)进行计算。

注1

您提供的IPv4示例解决方案列出了块中每四个IP地址中的一个,而我提供的则列出了块中的所有IP地址,您可以通过使用$i+=4代替$i++来进行调整。

注2

为什么要使用GMP/BCMATH?答案是,大小数在某些时候会转换成浮点数,这会导致数字失去精度,这对于这种计算不是好事。

鸣谢

感谢Glavić发布了有关如何将IPv6转换为小数和反之的答案


@Moist_Gerbil,那我什么时候能得到赏金? - Ahmad Hajjar
1
现在应该已经获得奖励了。 - Moist_Gerbil

0

我再次仔细看了一遍,采纳的答案并不完全是我想要的,因为它列出了 /128 块,而不是 /64。listIPv6InBlock 需要更改为:

function listIPv6InBlock($CIDR)
{
    $CIDR = explode("/", $CIDR); // this breaks the CIDR block into octlets and /notation
    $octet = ip2long_v6($CIDR[0]); 
    $NumberOfIPs = pow(2,(64-$CIDR[1])); //calculate the number of /64s in the CIDR block
    $OutputArray = array();
    $a = gmp_init($octet);
    $b = gmp_init('18446744073709551616'); // long /64

    for ($i=0; $i< $NumberOfIPs; $i++){
        $c = gmp_mul($b,$i);
        $d = gmp_add($a,$c);

        $OutputArray[] = long2ip_v6(gmp_strval($d));
    }
    return $OutputArray; //returns an array of ranges
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接