You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by er...@apache.org on 2010/10/14 11:06:32 UTC

svn commit: r1022432 - in /james/server/trunk/common-util/src: main/java/org/apache/james/util/inetnetwork/ main/java/org/apache/james/util/inetnetwork/model/ test/java/org/apache/james/util/netmatcher/

Author: eric
Date: Thu Oct 14 09:06:31 2010
New Revision: 1022432

URL: http://svn.apache.org/viewvc?rev=1022432&view=rev
Log:
Deal with ip V4/V6 mixed environments - Manage the v6 subnetmask % delimiter - Should solve NPE (JAMES-1061)

Modified:
    james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/InetNetworkBuilder.java
    james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/model/Inet4Network.java
    james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/model/Inet6Network.java
    james/server/trunk/common-util/src/test/java/org/apache/james/util/netmatcher/NetMatcherTest.java

Modified: james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/InetNetworkBuilder.java
URL: http://svn.apache.org/viewvc/james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/InetNetworkBuilder.java?rev=1022432&r1=1022431&r2=1022432&view=diff
==============================================================================
--- james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/InetNetworkBuilder.java (original)
+++ james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/InetNetworkBuilder.java Thu Oct 14 09:06:31 2010
@@ -45,10 +45,12 @@ import org.apache.james.util.inetnetwork
  * subnet expressed in one of several formats:
  *     IPv6 Format                     Example
  *     Explicit address                0000:0000:0000:0000:0000:0000:0000:0001
- *     IP address + subnet mask        0000:0000:0000:0000:0000:0000:0000:0001/64
+ *     IP address + subnet mask (/)   0000:0000:0000:0000:0000:0000:0000:0001/64
+ *     IP address + subnet mask (%)   0000:0000:0000:0000:0000:0000:0000:0001%64
  *     The following V6 formats will be supported later:
  *     Domain name                     myHost.com
- *     Domain name + mask              myHost.com/48
+ *     Domain name + mask (/)          myHost.com/48
+ *     Domain name + mask (%)          myHost.com%48
  *     Explicit shorted address        ::1
  * For more information on IP V6, see RFC 2460. (see also http://en.wikipedia.org/wiki/IPv6_address) 
  */
@@ -58,7 +60,7 @@ public class InetNetworkBuilder {
      * The DNS Server used to create InetAddress for
      * hostnames and IP adresses.
      */
-    private DNSService dnsServer;
+    private DNSService dnsService;
 
     /**
      * Constructs a InetNetwork.
@@ -66,7 +68,7 @@ public class InetNetworkBuilder {
      * @param dnsServer the DNSService to use
      */
     public InetNetworkBuilder(DNSService dnsServer) {
-        this.dnsServer = dnsServer;
+        this.dnsService = dnsServer;
     }
 
     /**
@@ -84,6 +86,16 @@ public class InetNetworkBuilder {
     }
 
     /**
+     * Returns true if the string parameters is a IPv6 pattern.
+     * Currently, only tests for presence of ':'.
+     * @param address
+     * @return boolean
+     */
+    public static boolean isV6(String netspec) {
+        return netspec.contains(":");
+    }
+
+    /**
      * Get a Inet4Network for the given String.
      * 
      * @param netspec the String which is will converted to InetNetwork
@@ -106,8 +118,8 @@ public class InetNetworkBuilder {
         }
 
         return new Inet4Network(
-                dnsServer.getByName(netspec.substring(0, netspec.indexOf('/'))), 
-                dnsServer.getByName(netspec.substring(netspec.indexOf('/') + 1)));
+                dnsService.getByName(netspec.substring(0, netspec.indexOf('/'))), 
+                dnsService.getByName(netspec.substring(netspec.indexOf('/') + 1)));
     }
 
     /**
@@ -122,14 +134,16 @@ public class InetNetworkBuilder {
         if (netspec.endsWith("*")) {
             throw new UnsupportedOperationException("Wildcard for IPv6 not supported");
         }
-        else {
-            if (netspec.indexOf('/') == -1) {
-                netspec += "/32768";
-            }
+        
+        // Netmask can be separated with %
+        netspec.replaceAll("%", "/");
+        
+        if (netspec.indexOf('/') == -1) {
+            netspec += "/32768";
         }
 
         return new Inet6Network(
-                dnsServer.getByName(netspec.substring(0, netspec.indexOf('/'))), 
+                dnsService.getByName(netspec.substring(0, netspec.indexOf('/'))), 
                 new Integer(netspec.substring(netspec.indexOf('/') + 1)));
     }
 
@@ -187,14 +201,4 @@ public class InetNetworkBuilder {
     
     }
     
-    /**
-     * Returns true if the string parameters is a IPv6 pattern.
-     * Currently, only tests for presence of ':'.
-     * @param address
-     * @return boolean
-     */
-    private static boolean isV6(String netspec) {
-        return netspec.contains(":");
-    }
-
 }

Modified: james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/model/Inet4Network.java
URL: http://svn.apache.org/viewvc/james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/model/Inet4Network.java?rev=1022432&r1=1022431&r2=1022432&view=diff
==============================================================================
--- james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/model/Inet4Network.java (original)
+++ james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/model/Inet4Network.java Thu Oct 14 09:06:31 2010
@@ -22,6 +22,8 @@ import java.net.Inet4Address;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 
+import org.apache.james.util.inetnetwork.InetNetworkBuilder;
+
 /**
  * 
  * 
@@ -54,6 +56,9 @@ public class Inet4Network implements Ine
      * @see org.apache.james.api.dnsservice.model.InetNetwork#contains(java.net.InetAddress)
      */
     public boolean contains(final InetAddress ip) {
+        if (InetNetworkBuilder.isV6(ip.getHostAddress())) {
+            return false;
+        }
         try {
             return network.equals(maskIP(ip, netmask));
         }

Modified: james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/model/Inet6Network.java
URL: http://svn.apache.org/viewvc/james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/model/Inet6Network.java?rev=1022432&r1=1022431&r2=1022432&view=diff
==============================================================================
--- james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/model/Inet6Network.java (original)
+++ james/server/trunk/common-util/src/main/java/org/apache/james/util/inetnetwork/model/Inet6Network.java Thu Oct 14 09:06:31 2010
@@ -22,6 +22,8 @@ import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 
+import org.apache.james.util.inetnetwork.InetNetworkBuilder;
+
 /**
  * 
  * 
@@ -53,6 +55,9 @@ public class Inet6Network implements Ine
      * @see org.apache.james.api.dnsservice.model.InetNetwork#contains(java.net.InetAddress)
      */
     public boolean contains(final InetAddress ip) {
+        if (! InetNetworkBuilder.isV6(ip.getHostAddress())) {
+            return false;
+        }
         try {
             return network.equals(maskIP(ip, netmask));
         }

Modified: james/server/trunk/common-util/src/test/java/org/apache/james/util/netmatcher/NetMatcherTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/common-util/src/test/java/org/apache/james/util/netmatcher/NetMatcherTest.java?rev=1022432&r1=1022431&r2=1022432&view=diff
==============================================================================
--- james/server/trunk/common-util/src/test/java/org/apache/james/util/netmatcher/NetMatcherTest.java (original)
+++ james/server/trunk/common-util/src/test/java/org/apache/james/util/netmatcher/NetMatcherTest.java Thu Oct 14 09:06:31 2010
@@ -70,10 +70,26 @@ public class NetMatcherTest extends Test
     /**
      * @throws UnknownHostException 
      */
+    public void testIpV4MatcherWithIpV6() throws UnknownHostException {
+        
+        netMatcher = new NetMatcher(DNSFixture.LOCALHOST_IP_V4_ADDRESSES, DNSFixture.DNS_SERVER_IPV4_MOCK);
+        
+        assertEquals(false, netMatcher.matchInetNetwork("0:0:0:0:0:0:0:1%0"));
+        assertEquals(false, netMatcher.matchInetNetwork("00:00:00:00:00:00:00:1"));
+        assertEquals(false, netMatcher.matchInetNetwork("00:00:00:00:00:00:00:2"));
+        assertEquals(false, netMatcher.matchInetNetwork("2781:0db8:1234:8612:45ee:ffff:fffe:0001"));
+        assertEquals(false, netMatcher.matchInetNetwork("2781:0db8:1235:8612:45ee:ffff:fffe:0001"));
+    
+    }
+
+    /**
+     * @throws UnknownHostException 
+     */
     public void testIpV6Matcher() throws UnknownHostException {
         
         netMatcher = new NetMatcher(DNSFixture.LOCALHOST_IP_V6_ADDRESSES, DNSFixture.DNS_SERVER_IPV6_MOCK);
         
+        assertEquals(true, netMatcher.matchInetNetwork("0:0:0:0:0:0:0:1%0"));
         assertEquals(true, netMatcher.matchInetNetwork("00:00:00:00:00:00:00:1"));
         assertEquals(false, netMatcher.matchInetNetwork("00:00:00:00:00:00:00:2"));
         assertEquals(true, netMatcher.matchInetNetwork("2781:0db8:1234:8612:45ee:ffff:fffe:0001"));
@@ -81,4 +97,19 @@ public class NetMatcherTest extends Test
     
     }
 
+    /**
+     * @throws UnknownHostException 
+     */
+    public void testIpV6MatcherWithIpV4() throws UnknownHostException {
+        
+        netMatcher = new NetMatcher(DNSFixture.LOCALHOST_IP_V6_ADDRESSES, DNSFixture.DNS_SERVER_IPV6_MOCK);
+        
+        assertEquals(false, netMatcher.matchInetNetwork("127.0.0.1"));
+        assertEquals(false, netMatcher.matchInetNetwork("localhost"));
+        assertEquals(false, netMatcher.matchInetNetwork("172.16.15.254"));
+        assertEquals(false, netMatcher.matchInetNetwork("192.168.1.254"));
+        assertEquals(false, netMatcher.matchInetNetwork("192.169.1.254"));
+        
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org