X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Futil%2FInternetFunctions.java;h=2260d4db8ca650f015e66e41db05b806e0ff9980;hb=63e0ee1fb8038eb6d8f0190cf38c3b3ab2727216;hp=2dd057b1db32649fe1d9ff9995cfc590272043db;hpb=8b91e8d8bf4a31a88440a404e83238dcf32f8f4a;p=mir.git diff --git a/source/mir/util/InternetFunctions.java b/source/mir/util/InternetFunctions.java index 2dd057b1..2260d4db 100755 --- a/source/mir/util/InternetFunctions.java +++ b/source/mir/util/InternetFunctions.java @@ -32,18 +32,19 @@ package mir.util; import java.util.List; +import java.net.InetAddress; public class InternetFunctions { private InternetFunctions() { } public static boolean isIpAddressInNetwork(String anIpAddress, String aNetwork) throws Exception { - long ipAddress = parseIPAddress(anIpAddress); + long ipAddress = parseHostNameOrIPAddress(anIpAddress); long network = 0; long netMask = (1L<<32)-1; List networkParts = StringRoutines.separateString(aNetwork, "/"); - network = parseIPAddress((String) networkParts.get(0)); + network = parseHostNameOrIPAddress((String) networkParts.get(0)); if (networkParts.size()>=2) { netMask=parseNetmask((String) networkParts.get(1)); } @@ -51,6 +52,15 @@ public class InternetFunctions { return (ipAddress & netMask ) == (network & netMask); } + public static long parseHostNameOrIPAddress(String aHostName) throws Exception { + InetAddress addr = InetAddress.getByName(aHostName.trim()); + return + ((((long) addr.getAddress()[0])&255) << 24) + + ((((long) addr.getAddress()[1])&255) << 16) + + ((((long) addr.getAddress()[2])&255) << 8) + + ((((long) addr.getAddress()[3])&255)); + } + public static long parseIPAddress(String anIpAddress) throws Exception { int[] parts = {0,0,0,0}; int i; @@ -61,7 +71,7 @@ public class InternetFunctions { try { for (i=0; i<4; i++) { - parts[i] = Integer.parseInt((String) stringParts.get(i)); + parts[i] = Integer.parseInt(((String) stringParts.get(i)).trim()); } } catch (Throwable t) { @@ -93,4 +103,5 @@ public class InternetFunctions { return (1L<<32)-1; } + } \ No newline at end of file