001 package railo.commons.net; 002 003 import java.net.InetAddress; 004 005 import railo.commons.lang.StringUtil; 006 import railo.runtime.op.Caster; 007 import railo.runtime.type.util.ListUtil; 008 009 public class IPUtil { 010 011 012 public static boolean isIPv4(String ip) { 013 String[] arr = ListUtil.trimItems(ListUtil.trim(ListUtil.listToStringArray(ip, '.'))); 014 if(arr.length!=4) return false; 015 016 int tmp; 017 for(int i=0;i<arr.length;i++){ 018 tmp=Caster.toIntValue(arr[i],-1); 019 if(tmp<0 || tmp>255) return false; 020 } 021 return true; 022 } 023 024 public static boolean isIPv62(String ip) { 025 if(ip.indexOf(':') == -1) return false; 026 String[] arr = ListUtil.trimItems(ListUtil.trim(ListUtil.listToStringArray(ip, ':'))); 027 if(arr.length!=8) return false; 028 String str; 029 int _int; 030 for(int i=0;i<arr.length;i++){ 031 str=arr[i]; 032 if(!StringUtil.isEmpty(str)) { 033 try{ 034 _int=Integer.parseInt(str,16); 035 } 036 catch(Throwable t){t.printStackTrace(); 037 _int=-1; 038 } 039 if(_int<0 || _int> 65535) 040 return false; 041 } 042 } 043 return true; 044 } 045 046 public static boolean isIPv4(InetAddress addr) { 047 return addr.getAddress().length==4; 048 } 049 public static boolean isIPv6(InetAddress addr) { 050 return !isIPv4(addr); 051 } 052 053 /*public static void main(String[] args) throws UnknownHostException { 054 long start=System.currentTimeMillis(); 055 print.o(isIPv4(InetAddress.getByName("localhost"))); 056 print.o(isIPv4(InetAddress.getByName("0.0.0.0"))); 057 print.o(isIPv4(InetAddress.getByName("127.0.0.1"))); 058 print.o(isIPv4(InetAddress.getByName("255.255.255.255"))); 059 print.o(isIPv6(InetAddress.getByName("0:0:0:0:0:0:0:1%0"))); 060 print.o(System.currentTimeMillis()-start); 061 }*/ 062 063 }