Convertendo endereço CIDR para máscara de sub-rede e endereço de rede

Dado um endereço CIDR, por exemplo, 192.168.10.0/24

  • Como determinar o comprimento da máscara? ( 24 )
  • Como determinar o endereço da máscara? ( 255.255.255.0 )
  • Como determinar o endereço de rede? ( 192.168.10.0 )

Ele é coberto pelos utilitários do apache.

Veja esta URL: http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/util/SubnetUtils.html

 String subnet = "192.168.0.3/31"; SubnetUtils utils = new SubnetUtils(subnet); utils.getInfo().isInRange(address) 

Nota: Para uso com / 32 sub-redes CIDR, por exemplo, é necessário adicionar a seguinte declaração:

 utils.setInclusiveHostCount(true); 

É assim que você faria em Java,

  String[] parts = addr.split("/"); String ip = parts[0]; int prefix; if (parts.length < 2) { prefix = 0; } else { prefix = Integer.parseInt(parts[1]); } int mask = 0xffffffff << (32 - prefix); System.out.println("Prefix=" + prefix); System.out.println("Address=" + ip); int value = mask; byte[] bytes = new byte[]{ (byte)(value >>> 24), (byte)(value >> 16 & 0xff), (byte)(value >> 8 & 0xff), (byte)(value & 0xff) }; InetAddress netAddr = InetAddress.getByAddress(bytes); System.out.println("Mask=" + netAddr.getHostAddress()); 

Seguindo a resposta de Yuriy: Para obter todo o intervalo de endereços IP, a class SubnetUtils do Apache Java oferece os seguintes methods:

 String[] addresses = utils.getInfo().getAllAddresses(); 

Para baixar o jar contendo a class, acesse: http://repo1.maven.org/maven2/commons-net/commons-net/3.0.1/commons-net-3.0.1.jar

O código-fonte: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java?view=markup

ID do Maven:

   commons-net        commons-net        3.0.1 

A biblioteca Java IPAddress oferece suporte a IPv4 e IPv6 de maneira polimórfica, incluindo sub-redes. O javadoc está disponível no link. Isenção de responsabilidade: Eu sou o gerente de projetos.

Todos os casos de uso que você listou são suportados para IPv4 e Ipv6 de forma transparente.

  String str = "192.168.10.0/24"; IPAddressString addrString = new IPAddressString(str); try { IPAddress addr = addrString.toAddress(); Integer prefix = addr.getNetworkPrefixLength(); //24 IPAddress mask = addr.getNetwork().getNetworkMask(prefix, false);//255.255.255.0 IPAddress networkAddr = addr.mask(mask); //192.168.10.0 IPAddress networkAddrOtherWay = addr.getLower().removePrefixLength(); //192.168.10.0 ... } catch(AddressStringException e) { //e.getMessage provides validation issue } 

Linha de comando do Linux ipcalc. Você pode usar rapidamente:

 $ipcalc 192.168.10.0/24 Address: 192.168.10.0 11000000.10101000.00001010. 00000000 Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000 Wildcard: 0.0.0.255 00000000.00000000.00000000. 11111111 => Network: 192.168.10.0/24 11000000.10101000.00001010. 00000000 HostMin: 192.168.10.1 11000000.10101000.00001010. 00000001 HostMax: 192.168.10.254 11000000.10101000.00001010. 11111110 Broadcast: 192.168.10.255 11000000.10101000.00001010. 11111111 Hosts/Net: 254 Class C, Private Internet 

O algoritmo está em pseudocódigo (na verdade, PHP), você pode traduzi-lo para java você mesmo.
Algoritmo daqui .

 //$ipNetmask = "192.168.1.12/30"; list($ip, $netmask) = split( "/", $ipNetmask ); $ip_elements_decimal = split( "[.]", $ip ); $netmask_result=""; for($i=1; $i <= $netmask; $i++) { $netmask_result .= "1"; } for($i=$netmask+1; $i <= 32; $i++) { $netmask_result .= "0"; } $netmask_ip_binary_array = str_split( $netmask_result, 8 ); $netmask_ip_decimal_array = array(); foreach( $netmask_ip_binary_array as $k => $v ){ $netmask_ip_decimal_array[$k] = bindec( $v ); // "100" => 4 $network_address_array[$k] = ( $netmask_ip_decimal_array[$k] & $ip_elements_decimal[$k] ); } $network_address = join( ".", $network_address_array ); // ------------------------------------------------ // TCP/IP NETWORK INFORMATION // ------------------------------------------------ // IP Entered = ..................: 192.168.1.12 // CIDR = ........................: /30 // Netmask = .....................: 255.255.255.252 // Network Address = .............: 192.168.1.12 // Broadcast Address = ...........: 192.168.1.15 // Usable IP Addresses = .........: 2 // First Usable IP Address = .....: 192.168.1.13 // Last Usable IP Address = ......: 192.168.1.14 

Você pode usar org.springframework.security.web.util.IpAddressMatcher do Spring Framework.

este é o meu groovy 🙂

 //IP calculator by ku1gun // input String inputAddr = "12.34.56.78/20"; //magic def(String ipAddrBin, String maskAddrBin, String invertedMaskBin, int hostsCount) = getIpAddrAndCidrMaskBin(inputAddr); String broadcastAddr = retrieveBroadcastAddr(ipAddrBin, invertedMaskBin); String ipAddr = getTenBaseAddrValueFromBin(ipAddrBin); String maskAddr = getTenBaseAddrValueFromBin(maskAddrBin); String invertedMask = getTenBaseAddrValueFromBin(invertedMaskBin); String networkAddr = retrieveNetworkAddr(ipAddrBin, maskAddrBin); def (String ipMinVal, String ipMaxVal) = getMinMaxIpRangeValues(networkAddr, broadcastAddr) //Output "debug" results System.out.println("Variables:"); System.out.println("ipInput: " + ipAddr); System.out.println("MaskInput: " + maskAddr); System.out.println("invertedMask: " + invertedMask); System.out.println("-----------------------"); System.out.println("Binaries:"); System.out.println("ipBin: " + ipAddrBin); System.out.println("MaskInBin: " + maskAddrBin); System.out.println("InvertedMaskBin: " + invertedMaskBin); System.out.println("-----------------------"); System.out.println("Results:"); System.out.println("maskAddr: " + maskAddr); System.out.println("hostsCount: " + hostsCount); System.out.println("networkAddr: " + networkAddr); System.out.println("broadcastAddr: " + broadcastAddr); System.out.println("ipMinVal: " + ipMinVal); System.out.println("ipMaxVal: " + ipMaxVal); System.out.println("-----------------------"); System.out.println("IP range list:"); long ipStart = host2long(ipMinVal); long ipEnd = host2long(ipMaxVal); for (long i=ipStart; i<=ipEnd; i++) { System.out.println(long2dotted(i)); } //general methods def getIpAddrAndCidrMaskBin(String inputAddrStr) { def netMask = ""; def invNetMask = ""; def cidrAddressList = inputAddrStr.tokenize("\\/") def baseIPAddress = cidrAddressList.first() def cidrIPMask = cidrAddressList.last().toInteger() //retrieve binaryNetMask and binaryInvertedNetMask for(i=0; i<32; i++) { if(i=0 ? addr[i] : 0)) << 8*(3-i); } return ip; } public static int[] ip2intarray(String host) { Integer[] address = [-1,-1,-1,-1]; int i=0; StringTokenizer tokens = new StringTokenizer(host,"."); if (tokens.countTokens() > 4) return null; while (tokens.hasMoreTokens()) { try { address[i++] = Integer.parseInt(tokens.nextToken()) & 0xFF; } catch(NumberFormatException nfe) { return null; } } return address; } public static String long2dotted(long ip) { // if ip is bigger than 255.255.255.255 or smaller than 0.0.0.0 if (ip > 4294967295l || ip < 0) { throw new IllegalArgumentException("invalid ip"); } StringBuilder ipAddress = new StringBuilder(); for (int i = 3; i >= 0; i--) { int shift = i * 8; ipAddress.append((ip & (0xff << shift)) >> shift); if (i > 0) { ipAddress.append("."); } } return ipAddress.toString(); } 

Classe Apache Java SubnetUtils oferece ajuda para fazer um pouco disso:

 String[] parts = ipv4Cidr.split("/"); if (parts[1].equals("0")) { // This accepts all ip addresses. Technically not a subnet. maskLength = 0; maskAdress = "0.0.0.0" } else { maskLength = Integer.parseInt(parts[1]); cidrInfo = new SubnetUtils(ipv4Cidr).getInfo(); maskAdress = cidrInfo.asInteger(cidrInfo.getNetmask()); networkAddress = cidrInfo.getNetworkAddress() } 

Aqui está um exemplo simples do Groovy

 def cidrAddress = '192.168.10.0/24' def cidrAddressList = cidrAddress.tokenize("\\/") def baseIPAddress = cidrAddressList.first() def cidrIPMask = cidrAddressList.last().toInteger() def netMaskList = [] Integer fullOctets = cidrIPMask.intdiv(8) fullOctets.times {netMaskList.add('255')} def remainder = cidrIPMask % 8 netMaskList.add((256 - (2 ** (8 - remainder))).toString()) netMaskList.addAll(['0','0','0','0']) def netMask = netMaskList.flatten().getAt(0..3).join('.') return [cidrAddress,baseIPAddress,cidrIPMask,netMask] 
 --plsql ip_calc by ku1gun with a as ( select '12.34.56.78/20' ip from dual ),b as ( select utl_raw.concat(utl_raw.substr(utl_raw.cast_from_binary_integer(to_number(regexp_substr(ip,'[^.]+',1,1)),2),1,1),utl_raw.substr(utl_raw.cast_from_binary_integer(to_number(regexp_substr(ip,'[^.]+',1,2)),2),1,1),utl_raw.substr(utl_raw.cast_from_binary_integer(to_number(regexp_substr(ip,'[^.]+',1,3)),2),1,1),utl_raw.substr(utl_raw.cast_from_binary_integer(to_number(regexp_substr(replace(ip,'/','.'),'[^.]+',1,4)),2),1,1)) ip, utl_raw.cast_from_binary_integer(power(2,32-to_number(regexp_substr(ip,'[^/]+',1,2)))-1) wildcard, utl_raw.bit_xor(utl_raw.cast_from_binary_integer(-1),utl_raw.cast_from_binary_integer(power(2,32-to_number(regexp_substr(ip,'[^/]+',1,2)))-1)) mask from a ),c as ( select utl_raw.bit_and(ip,mask) network_address, utl_raw.bit_or(utl_raw.bit_and(ip,mask),wildcard) broadcast_address, utl_raw.cast_from_binary_integer(utl_raw.cast_to_binary_integer(utl_raw.bit_and(ip,mask))+1) first_adress, utl_raw.cast_from_binary_integer(utl_raw.cast_to_binary_integer(utl_raw.bit_or(utl_raw.bit_and(ip,mask),wildcard))-1) last_adress, utl_raw.cast_from_binary_integer(utl_raw.cast_to_binary_integer(utl_raw.bit_and(ip,mask))+level) ip_address from b connect by level 
Intereting Posts