1.1 restoration
[mir.git] / source / mir / util / InternetFunctions.java
index 2dd057b..2260d4d 100755 (executable)
 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