public class IpUtis { /** * Convert the Ip of long type to general Ip type: xx.xx.xx.xx * * @paramip * @return */ public static String getIpFromLong(Long ip) { String s1 = String.valueOf((ip & 4278190080L) / 16777216L); String s2 = String.valueOf((ip & 16711680L) / 65536L); String s3 = String.valueOf((ip & 65280L) / 256L); String s4 = String.valueOf(ip & 255L); return s1 + "." + s2 + "." + s3 + "." + s4; } /** * Convert xx.xx.xx.xx type to long type * * @paramip * @return */ public static Long getIpFromString(String ip) { Long ipLong = 0L; String ipTemp = ip; ipLong = ipLong * 256 + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf("."))); ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length()); ipLong = ipLong * 256 + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf("."))); ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length()); ipLong = ipLong * 256 + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf("."))); ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length()); ipLong = ipLong * 256 + Long.parseLong(ipTemp); return ipLong; } /** * Get the mask according to the mask bits * * @param maskBit mask bit, such as "28", "30" * @return */ public static String getMaskByMaskBit(String maskBit) { return StringUtils.isEmpty(maskBit) ? "error, maskBit is null !" : maskBitMap().get(maskBit); } /** * Calculate the starting IP of the IP segment according to the ip/mask bits, such as the IP string 218.240.38.69/30 * * @param ip the given IP, such as 218.240.38.69 * @param maskBit the given mask bit, such as 30 * @return the string representation of the starting IP */ public static String getBeginIpStr(String ip, String maskBit) { return getIpFromLong(getBeginIpLong(ip, maskBit)); } /** * Calculate the starting IP of the IP segment according to the ip/mask bits, such as the IP string 218.240.38.69/30 * * @param ip the given IP, such as 218.240.38.69 * @param maskBit the given mask bit, such as 30 * @return the long integer representation of the starting IP */ public static Long getBeginIpLong(String ip, String maskBit) { return getIpFromString(ip) & getIpFromString(getMaskByMaskBit(maskBit)); } /** * Calculate the termination IP of the IP segment according to the ip/mask bits, such as the IP string 218.240.38.69/30 * * @param ip the given IP, such as 218.240.38.69 * @param maskBit the given mask bit, such as 30 * @return the string representation of the terminating IP */ public static String getEndIpStr(String ip, String maskBit) { return getIpFromLong(getEndIpLong(ip, maskBit)); } /** * Calculate the termination IP of the IP segment according to the ip/mask bits, such as the IP string 218.240.38.69/30 * * @param ip the given IP, such as 218.240.38.69 * @param maskBit the given mask bit, such as 30 * @return the long representation of the termination IP */ public static Long getEndIpLong(String ip, String maskBit) { return getBeginIpLong(ip, maskBit) + ~getIpFromString(getMaskByMaskBit(maskBit)); } /** * Converted to mask bits according to the subnet mask, such as 255.255.255.252 converted to mask bits of 30 * * @param netmarks * @return */ public static int getNetMask(String netmarks) { StringBuffer sbf; String str; int inetmask = 0, count = 0; String[] ipList = netmarks.split("\\."); for (int n = 0; n < ipList.length; n++) { sbf = toBin(Integer.parseInt(ipList[n])); str = sbf.reverse().toString(); count = 0; for (int i = 0; i < str.length(); i++) { i = str.indexOf('1', i); if (i == -1) { break; } count++; } inetmask += count; } return inetmask; } /** * Calculate the subnet size * * @param netmask mask bits * @return */ public static int getPoolMax(int maskBit) { if (maskBit <= 0 || maskBit >= 32) { return 0; } return (int) Math.pow(2, 32 - maskBit) - 2; } private static StringBuffer toBin(int x) { StringBuffer result = new StringBuffer(); result.append(x % 2); x /= 2; while (x > 0) { result.append(x % 2); x /= 2; } return result; } /* * Stores all mask bits and the corresponding mask key: mask bit value: mask (xxxx) */ private static Map<String, String> maskBitMap() { Map<String, String> maskBit = new HashMap<String, String>(); maskBit.put("1", "128.0.0.0"); maskBit.put("2", "192.0.0.0"); maskBit.put("3", "224.0.0.0"); maskBit.put("4", "240.0.0.0"); maskBit.put("5", "248.0.0.0"); maskBit.put("6", "252.0.0.0"); maskBit.put("7", "254.0.0.0"); maskBit.put("8", "255.0.0.0"); maskBit.put("9", "255.128.0.0"); maskBit.put("10", "255.192.0.0"); maskBit.put("11", "255.224.0.0"); maskBit.put("12", "255.240.0.0"); maskBit.put("13", "255.248.0.0"); maskBit.put("14", "255.252.0.0"); maskBit.put("15", "255.254.0.0"); maskBit.put("16", "255.255.0.0"); maskBit.put("17", "255.255.128.0"); maskBit.put("18", "255.255.192.0"); maskBit.put("19", "255.255.224.0"); maskBit.put("20", "255.255.240.0"); maskBit.put("21", "255.255.248.0"); maskBit.put("22", "255.255.252.0"); maskBit.put("23", "255.255.254.0"); maskBit.put("24", "255.255.255.0"); maskBit.put("25", "255.255.255.128"); maskBit.put("26", "255.255.255.192"); maskBit.put("27", "255.255.255.224"); maskBit.put("28", "255.255.255.240"); maskBit.put("29", "255.255.255.248"); maskBit.put("30", "255.255.255.252"); maskBit.put("31", "255.255.255.254"); maskBit.put("32", "255.255.255.255"); return maskBit; } /** * Determine whether an ip is in a network segment ip/mask IP+mask */ public static boolean isInRange(String ip, String cidr) { String[] ips = ip.split("\\."); int ipAddr = (Integer.parseInt(ips[0]) << 24) | (Integer.parseInt(ips[1]) << 16) | (Integer.parseInt(ips[2]) << 8) | Integer.parseInt(ips[3]); int type = Integer.parseInt(cidr.replaceAll(".*/", "")); int mask = 0xFFFFFFFF << (32 - type); String cidrIp = cidr.replaceAll("/.*", ""); String[] cidrIps = cidrIp.split("\\."); int cidrIpAddr = (Integer.parseInt(cidrIps[0]) << 24) | (Integer.parseInt(cidrIps[1]) << 16) | (Integer.parseInt(cidrIps[2]) << 8) | Integer.parseInt(cidrIps[3]); return (ipAddr & mask) == (cidrIpAddr & mask); } /** * Determine whether the IP address is an intranet IP * * @param ipAddress * @return */ public static boolean isInnerIP(String ipAddress) { boolean isInnerIp = false; long ipNum = getIpNum(ipAddress); /** * Private IP: Class A 10.0.0.0-10.255.255.255 Class B 172.16.0.0-172.31.255.255 Class C 192.168.0.0-192.168.255.255 Of course, the network segment 127 is the loopback address **/ long aBegin = getIpNum("10.0.0.0"); long aEnd = getIpNum("10.255.255.255"); long bBegin = getIpNum("172.16.0.0"); long bEnd = getIpNum("172.31.255.255"); long cBegin = getIpNum("192.168.0.0"); long cEnd = getIpNum("192.168.255.255"); isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd) || ipAddress.equals("127.0.0.1"); return isInnerIp; } private static long getIpNum(String ipAddress) { String[] ip = ipAddress.split("\\."); long a = Integer.parseInt(ip[0]); long b = Integer.parseInt(ip[1]); long c = Integer.parseInt(ip[2]); long d = Integer.parseInt(ip[3]); long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d; return ipNum; } private static boolean isInner(long userIp, long begin, long end) { return (userIp >= begin) && (userIp <= end); } /** * Convert the IP address of the form 127.0.0.1 to a decimal integer * * @param strIP * @return */ public static long ipToTenLong(String strIP) { long[] ip = new long[4]; // First find the location of . in the IP address string int position1 = strIP.indexOf("."); int position2 = strIP.indexOf(".", position1 + 1); int position3 = strIP.indexOf(".", position2 + 1); // Convert the string between each . to an integer ip[0] = Long.parseLong(strIP.substring(0, position1)); ip[1] = Long.parseLong(strIP.substring(position1 + 1, position2)); ip[2] = Long.parseLong(strIP.substring(position2 + 1, position3)); ip[3] = Long.parseLong(strIP.substring(position3 + 1)); return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]; } public static String ipToTwoString(String strIP) { long[] ip = new long[4]; // First find the location of . in the IP address string int position1 = strIP.indexOf("."); int position2 = strIP.indexOf(".", position1 + 1); int position3 = strIP.indexOf(".", position2 + 1); // Convert the string between each . to an integer ip[0] = Long.parseLong(strIP.substring(0, position1)); ip[1] = Long.parseLong(strIP.substring(position1 + 1, position2)); ip[2] = Long.parseLong(strIP.substring(position2 + 1, position3)); ip[3] = Long.parseLong(strIP.substring(position3 + 1)); Long ten = (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]; return Long.toBinaryString(ten); } /** * Convert the decimal integer form to an IP address in the form of 127.0.0.1 * * @param longIP * @return */ public static String tenLongToIP(long longIP) { StringBuffer sb = new StringBuffer(""); // Directly shift right by 24 bits sb.append(String.valueOf(longIP >>> 24)); sb.append("."); // Set high 8 bits to 0, then shift right by 16 bits sb.append(String.valueOf((longIP & 0x00FFFFFF) >>> 16)); sb.append("."); sb.append(String.valueOf((longIP & 0x0000FFFF) >>> 8)); sb.append("."); sb.append(String.valueOf(longIP & 0x000000FF)); return sb.toString(); } }