1919import java .io .BufferedReader ;
2020import java .io .InputStreamReader ;
2121import java .lang .reflect .Array ;
22+ import java .math .BigInteger ;
2223import java .net .InetAddress ;
2324import java .net .InterfaceAddress ;
2425import java .net .NetworkInterface ;
@@ -1151,19 +1152,25 @@ public static int getIp6CidrSize(String ip6Cidr) {
11511152 return network .getNetmask ().asPrefixLength ();
11521153 }
11531154
1154- //FIXME: only able to cover lower 32 bits
1155+ // Can cover 127 bits
11551156 public static String getIp6FromRange (String ip6Range ) {
11561157 String [] ips = ip6Range .split ("-" );
11571158 String startIp = ips [0 ];
11581159 IPv6Address start = IPv6Address .fromString (startIp );
1159- // Find a random number based on lower 32 bits
1160- long gap = countIp6InRange ( ip6Range );
1161- if ( gap > Integer . MAX_VALUE ) {
1162- gap = Integer . MAX_VALUE ;
1160+ BigInteger gap = countIp6InRange ( ip6Range );
1161+ BigInteger next = new BigInteger ( gap . bitLength (), _rand );
1162+ while ( next . compareTo ( gap ) >= 0 ) {
1163+ next = new BigInteger ( gap . bitLength (), _rand ) ;
11631164 }
1164- int next = _rand .nextInt ((int )(gap ));
1165- // And a number based on the difference of lower 32 bits
1166- IPv6Address ip = start .add (next );
1165+ BigInteger startInt = convertIPv6AddressToBigInteger (start );
1166+ BigInteger resultInt = startInt .add (next );
1167+ InetAddress resultAddr ;
1168+ try {
1169+ resultAddr = InetAddress .getByAddress (resultInt .toByteArray ());
1170+ } catch (UnknownHostException e ) {
1171+ return null ;
1172+ }
1173+ IPv6Address ip = IPv6Address .fromInetAddress (resultAddr );
11671174 return ip .toString ();
11681175 }
11691176
@@ -1173,11 +1180,21 @@ public static String getDuidLL(String macAddress) {
11731180 return duid ;
11741181 }
11751182
1176- //FIXME: only able to cover lower 64 bits
1177- public static long countIp6InRange (String ip6Range ) {
1183+ private static BigInteger convertIPv6AddressToBigInteger (IPv6Address addr ) {
1184+ InetAddress inetAddr ;
1185+ try {
1186+ inetAddr = addr .toInetAddress ();
1187+ } catch (UnknownHostException e ) {
1188+ return null ;
1189+ }
1190+ return new BigInteger (inetAddr .getAddress ());
1191+ }
1192+
1193+ // Can cover 127 bits
1194+ public static BigInteger countIp6InRange (String ip6Range ) {
11781195 String [] ips = ip6Range .split ("-" );
11791196 String startIp = ips [0 ];
1180- String endIp = null ;
1197+ String endIp = ips [ 0 ] ;
11811198 if (ips .length > 1 ) {
11821199 endIp = ips [1 ];
11831200 }
@@ -1186,13 +1203,14 @@ public static long countIp6InRange(String ip6Range) {
11861203 start = IPv6Address .fromString (startIp );
11871204 end = IPv6Address .fromString (endIp );
11881205 } catch (IllegalArgumentException ex ) {
1189- return 0 ;
1206+ return null ;
11901207 }
1191- long startLow = start .getLowBits (), endLow = end .getLowBits ();
1192- if (startLow > endLow ) {
1193- return 0 ;
1208+ BigInteger startInt = convertIPv6AddressToBigInteger (start );
1209+ BigInteger endInt = convertIPv6AddressToBigInteger (end );
1210+ if (startInt .compareTo (endInt ) > 0 ) {
1211+ return null ;
11941212 }
1195- return endLow - startLow + 1 ;
1213+ return endInt . subtract ( startInt ). add ( BigInteger . ONE ) ;
11961214 }
11971215
11981216 public static boolean isIp6InRange (String ip6 , String ip6Range ) {
0 commit comments