Estoy buscando crear una secuencia de comandos que tome un rango ipv6 o cidr como entrada, y escupa una lista de bloques /64 (o la primera IP en cada bloque /64).
Tengo una función que hace algo similar para las direcciones IP de IPv4, pero me falta la comprensión para reutilizarla para 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 y long2ip son solo ipv4.
Existe esta solución que coincide con sus requisitos y necesita que usted coincida con su: D
Su requisito es tener instaladas las extensiones GMP o BCMATH, ya que en este caso se tratará con decimales muy grandes.
<?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://stackoverflow.com/a/19497446/896244 */ 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)); } ?>Como puede ver, esta solución realiza cálculos en decimales (como cadenas).
Nota 1
La solución que proporcionó como ejemplo para IPv4 enumera todas las direcciones IP en el bloque, la que proporcioné enumera todas las direcciones IP en el bloque, puede ajustar esto usando $i+=4 en lugar de $i++
Nota 2
¿Por qué usamos GMP/BCMATH? La respuesta es que los grandes decimales en algún momento se convertirán en flotantes lo que hará que los números pierdan precisión, lo cual no es bueno para este tipo de cálculos.
Créditos
Gracias a Glavić por publicar esta respuesta sobre cómo convertir IPv6 a decimales y viceversa
Revisé esto nuevamente y la respuesta aceptada no es exactamente lo que quería, ya que enumera /128 bloques, en lugar de /64. listIPv6InBlock debe cambiarse a esto:
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 }