NetUtils { public static Long[] cidrToLong(final String cidr) { if (cidr == null || cidr.isEmpty()) { throw new CloudRuntimeException("empty cidr can not be converted to longs"); } final String[] cidrPair = cidr.split("\\/"); if (cidrPair.length != 2) { throw new CloudRuntimeException("cidr is not formatted correctly: "+ cidr); } final String cidrAddress = cidrPair[0]; final String cidrSize = cidrPair[1]; if (!isValidIp(cidrAddress)) { throw new CloudRuntimeException("cidr is not valid in ip space" + cidr); } long cidrSizeNum = getCidrSizeFromString(cidrSize); final long numericNetmask = netMaskFromCidr(cidrSizeNum); final long ipAddr = ip2Long(cidrAddress); final Long[] cidrlong = {ipAddr & numericNetmask, cidrSizeNum}; return cidrlong; }  static long createSequenceBasedMacAddress(final long macAddress); static String getHostName(); static InetAddress getLocalInetAddress(); static String resolveToIp(final String host); static InetAddress[] getAllLocalInetAddresses(); static String[] getLocalCidrs(); static String getDefaultHostIp(); static String getDefaultEthDevice(); static InetAddress getFirstNonLoopbackLocalInetAddress(); static InetAddress[] getInterfaceInetAddresses(final String ifName); static String getLocalIPString(); static String ipFromInetAddress(final InetAddress addr); static boolean isLocalAddress(final InetAddress addr); static boolean isLocalAddress(final String strAddress); static String getMacAddress(final InetAddress address); static long getMacAddressAsLong(final InetAddress address); static boolean ipRangesOverlap(final String startIp1, final String endIp1, final String startIp2, final String endIp2); static long ip2Long(final String ip); static String long2Ip(final long ip); static long mac2Long(final String macAddress); static String[] getNicParams(final String nicName); static String[] getNetworkParams(final NetworkInterface nic); static String prefix2Netmask(final short prefix); static String byte2Mac(final byte[] m); static String long2Mac(final long macAddress); static boolean isValidPrivateIp(final String ipAddress, final String guestIPAddress); static boolean isSiteLocalAddress(final String ipAddress); static boolean validIpRange(final String startIP, final String endIP); static boolean isValidIp(final String ip); static boolean is31PrefixCidr(final String cidr); static boolean isValidCIDR(final String cidr); static boolean isValidNetmask(final String netmask); static String getCidrFromGatewayAndNetmask(final String gatewayStr, final String netmaskStr); static String[] getIpRangeFromCidr(final String cidr, final long size); static Set<Long> getAllIpsFromCidr(final String cidr, final long size, final Set<Long> usedIps); static long getRandomIpFromCidr(final String startIp, final int size, final SortedSet<Long> avoid); static long getRandomIpFromCidr(final long cidr, final int size, final SortedSet<Long> avoid); static String getIpRangeStartIpFromCidr(final String cidr, final long size); static String getIpRangeEndIpFromCidr(final String cidr, final long size); static boolean sameSubnet(final String ip1, final String ip2, final String netmask); static boolean sameSubnetCIDR(final String ip1, final String ip2, final long cidrSize); static String getSubNet(final String ip, final String netmask); static String getCidrSubNet(final String ip, final long cidrSize); static String ipAndNetMaskToCidr(final String ip, final String netmask); static String[] ipAndNetMaskToRange(final String ip, final String netmask); static Pair<String, Integer> getCidr(final String cidr); static SupersetOrSubset isNetowrkASubsetOrSupersetOfNetworkB(final String cidrA, final String cidrB); static boolean isNetworkAWithinNetworkB(final String cidrA, final String cidrB); static Long[] cidrToLong(final String cidr); static String getCidrSubNet(final String cidr); static String getCidrNetmask(final long cidrSize); static String getCidrNetmask(final String cidr); static String cidr2Netmask(final String cidr); static long getCidrSize(final String netmask); static boolean isValidPort(final String p); static boolean isValidPort(final int p); static boolean isValidLBPort(final String p); static boolean isValidProto(final String p); static boolean isValidSecurityGroupProto(final String p); static boolean isValidAlgorithm(final String p); static boolean isValidAutoScaleAction(final String p); static String getLinkLocalNetMask(); static String getLinkLocalGateway(); static String getLinkLocalCIDR(); static String[] getLinkLocalIPRange(final int size); static String getLinkLocalIpEnd(); static String portRangeToString(final int portRange[]); static boolean verifyDomainNameLabel(final String hostName, final boolean isHostName); static boolean verifyDomainName(final String domainName); static String getDhcpRange(final String cidr); static boolean isSameIpRange(final String cidrA, final String cidrB); static boolean validateGuestCidr(final String cidr); static boolean verifyInstanceName(final String instanceName); static boolean isNetworksOverlap(final String cidrA, final String cidrB); static boolean isValidS2SVpnPolicy(final String policys); static boolean isValidCidrList(final String cidrList); static boolean validateGuestCidrList(final String guestCidrList); static boolean validateIcmpType(final long icmpType); static boolean validateIcmpCode(final long icmpCode); static boolean isValidIpv6(final String ip); static boolean isValidIp6Cidr(final String ip6Cidr); static int getIp6CidrSize(final String ip6Cidr); static String getIp6FromRange(final String ip6Range); static String getDuidLL(final String macAddress); static BigInteger countIp6InRange(final String ip6Range); static boolean isIp6InRange(final String ip6, final String ip6Range); static boolean isIp6InNetwork(final String ip6, final String ip6Cidr); static boolean isIp6RangeOverlap(final String ipRange1, final String ipRange2); static String getNextIp6InRange(final String currentIp, final String ipRange); static String standardizeIp6Address(final String ip6Addr); static String standardizeIp6Cidr(final String ip6Cidr); static boolean isValidVlan(String vlan); static boolean isSameIsolationId(String one, String other); static URI generateUriForPvlan(final String primaryVlan, final String isolatedPvlan); static String getPrimaryPvlanFromUri(final URI uri); static String getIsolatedPvlanFromUri(final URI uri); static String generateMacOnIncrease(final String baseMac, final long l); static boolean isIpWithtInCidrRange(final String ipAddress, final String cidr); static Boolean IsIpEqualToNetworkOrBroadCastIp(final String requestedIp, final String cidr, final long size); static boolean isNetworkorBroadcastIP(String ip, String netmask); final static String HTTP_PORT; final static String HTTPS_PORT; final static int VPN_PORT; final static int VPN_NATT_PORT; final static int VPN_L2TP_PORT; final static int HAPROXY_STATS_PORT; final static String UDP_PROTO; final static String TCP_PROTO; final static String ANY_PROTO; final static String ICMP_PROTO; final static String ALL_PROTO; final static String HTTP_PROTO; final static String SSL_PROTO; final static String ALL_CIDRS; final static int PORT_RANGE_MIN; final static int PORT_RANGE_MAX; final static int DEFAULT_AUTOSCALE_VM_DESTROY_TIME; final static int DEFAULT_AUTOSCALE_POLICY_INTERVAL_TIME; final static int DEFAULT_AUTOSCALE_POLICY_QUIET_TIME; }
@Test public void testCidrToLongArray() { final String cidr = "10.192.10.10/10"; Long[] netbits = NetUtils.cidrToLong(cidr); assertEquals("unexpected cidrsize " + netbits[1],10l, netbits[1].longValue()); assertEquals("(un)expected <" + 0x0ac00000L + "> netaddress " + netbits[0].longValue(),netbits[0].longValue(),0x0ac00000l); }