cidrToIPv4 static method

List<String>? cidrToIPv4(
  1. String cidr
)

Returns the IPv4 ranges for the cidr.

Implementation

static List<String>? cidrToIPv4(String cidr) {
  if (!cidr.contains('/')) return null;
  final arr = cidr.split('/');
  if (arr.length != 2 ||
      !isIPv4(arr[0]) ||
      !prefixPattern.hasMatch(arr[1]) ||
      int.parse(arr[1]) > 32) {
    return null;
  }

  final prefix = int.parse(arr[1]);
  var startLong = ipv4ToDecimal(arr[0])!.toInt();
  startLong &= (-1 << (32 - prefix));
  final ipStart = decimalToIPv4(BigInt.from(startLong))!;
  var total = 1 << (32 - prefix);
  var endLong = startLong + total - 1;
  if (endLong > 4294967295) endLong = 4294967295;
  final ipEnd = decimalToIPv4(BigInt.from(endLong))!;
  return [ipStart, ipEnd];
}