This example include java code that can be used to check whether a given ip string is valid, inner or public ip or not. Please see code comments for detail explanation.
1.Check If A String Is Valid IP
/* This method check whether addStr is a valid ip address or not. * addStr : A given ip string. * return : true means valid, false means invalid. * */ public static boolean isValidIp(String addStr) { boolean ret = true; // If addStr is empty or reserved address then return false. if("".equals(addStr) || addStr==null || "0.0.0.0".equals(addStr)) { return false; } // Split ip address string into 4 length string array. String ipEle[] = addStr.split("\\."); // If ip address do not include four parts return false. if(ipEle.length!=4) { return false; }else { // Loop for the four parts in ip address. for(int i=0;i<4;i++) { String ipEleStr = ipEle[i]; // If not integer return false. if(!StringTool.isInteger(ipEleStr)) { return false; }else { // If less than zero or bigger than 256 return false. if(Integer.parseInt(ipEleStr)<0 || Integer.parseInt(ipEleStr)>=256) { return false; }else { // If the first part in ip address is 127 return false. if(i==0 && Integer.parseInt(ipEleStr)==127) { return false; } } } } } return ret; }
2. Check If A String Is Inner IP
/* This method is used to check whether ipAddress is inner ip or not. * */ public static boolean isInnerIP(String ipAddress) { boolean isInnerIp = false; // Get number value for the ip string. long ipNum = getIpNum(ipAddress); // Get number value for inner ip range string. 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"); long dBegin = getIpNum("224.0.0.0"); long dEnd = getIpNum("239.255.255.255"); // If ipAddress exist in one of inner ip range then it is an inner ip. isInnerIp = isInner(ipNum,aBegin,aEnd) || isInner(ipNum,bBegin,bEnd) || isInner(ipNum,cBegin,cEnd)|| isInner(ipNum,dBegin,dEnd) || ipAddress.equals("127.0.0.1") || ipAddress.equals("255.255.255.255") || ipAddress.equals("224.0.0.1") || ipAddress.equals("0.0.0.0"); return isInnerIp; } /* * If userIp's value between begin and end then return true. * */ private static boolean isInner(long userIp,long begin,long end) { return (userIp>=begin) && (userIp<=end); }
3. Check If A String Is Public IP
/* Checkout whether ipStr is a public network ip. * ipStr : String ip address. * return : true means ipStr is a public network ip, false means ipStr is not a public network ip. * */ public static boolean isPublicInternetIp(String ipStr) { boolean ret = true; if(StringTool.isEmpty(ipStr)) { ret = false; }else { ipStr = ipStr.trim(); // If ip is not valid return false. if(!URLTool.isValidIp(ipStr)) { ret = false; }else { //If ip is reserved local network address ip return false. if(ipStr.startsWith("0.") || ipStr.startsWith("10.") || ipStr.startsWith("127.") || ipStr.startsWith("192.168.") || ipStr.startsWith("169.254.")) { ret = false; }else { // Split ip address into four parts array. String ipColumnArr[] = ipStr.split(GeneralConstants.SEPERATOR_DOT); // If the array dose not include four parts return false. if(ipColumnArr.length!=4) { ret = false; }else { String ipC0 = ipColumnArr[0]; String ipC1 = ipColumnArr[1]; String ipC2 = ipColumnArr[2]; String ipC3 = ipColumnArr[3]; // If any part in ip address is not an integer. if(!StringTool.isInteger(ipC0) || !StringTool.isInteger(ipC1) || !StringTool.isInteger(ipC2) || !StringTool.isInteger(ipC3)) { ret = false; }else { /* * Check reserved ip address: * 224.0.0.0 to 255.255.255.255 * 100.64.x.x-100.127.x.x[3] 、172.16.x.x-172.31.x.x、 * */ // Check first two parts. int ipC0Int = Integer.parseInt(ipC0); int ipC1Int = Integer.parseInt(ipC1); // Filter out reserved ip address. if(ipC0Int >= 224) { ret = false; }else { if(ipC0Int==100) { if(ipC1Int>=64 && ipC1Int<=127) { ret = false; } }else if(ipC0Int==172) { if(ipC1Int>=16 && ipC1Int<=31) { ret = false; } } } } } } } } return ret; }