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/06/13 16:00:39 UTC
svn commit: r954226 - in /james/server/trunk: domain-api/
domain-api/src/main/java/org/apache/james/api/dnsservice/
domain-api/src/main/java/org/apache/james/api/dnsservice/model/
domain-api/src/main/java/org/apache/james/api/dnsservice/util/ domain-ap...
Author: eric
Date: Sun Jun 13 14:00:38 2010
New Revision: 954226
URL: http://svn.apache.org/viewvc?rev=954226&view=rev
Log:
NetMatcher for IPv4 and IPv6 + Inet4Network and Inet6Network + Unit tests (JAMES-402)
Added:
james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/
james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/Inet4Network.java (with props)
james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/Inet6Network.java (with props)
james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/InetNetwork.java (with props)
james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/util/InetNetworkBuilder.java (with props)
james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/AbstractDNSServerTest.java (with props)
james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/DNSFixture.java (with props)
james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/model/
james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/model/InetNetworkTest.java (with props)
james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/util/
james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/util/InetNetworkBuilderTest.java (with props)
james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/util/NetMatcherTest.java (with props)
Removed:
james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/util/InetNetwork.java
Modified:
james/server/trunk/domain-api/pom.xml
james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/DNSService.java
james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/TemporaryResolutionException.java
james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/util/NetMatcher.java
james/server/trunk/domain-api/src/main/java/org/apache/james/api/domainlist/DomainList.java
james/server/trunk/domain-api/src/main/java/org/apache/james/api/domainlist/ManageableDomainList.java
james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/AbstractDNSServer.java
james/server/trunk/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
Modified: james/server/trunk/domain-api/pom.xml
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/pom.xml?rev=954226&r1=954225&r2=954226&view=diff
==============================================================================
--- james/server/trunk/domain-api/pom.xml (original)
+++ james/server/trunk/domain-api/pom.xml Sun Jun 13 14:00:38 2010
@@ -37,5 +37,9 @@
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
Modified: james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/DNSService.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/DNSService.java?rev=954226&r1=954225&r2=954226&view=diff
==============================================================================
--- james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/DNSService.java (original)
+++ james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/DNSService.java Sun Jun 13 14:00:38 2010
@@ -16,9 +16,6 @@
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
-
-
-
package org.apache.james.api.dnsservice;
import java.net.InetAddress;
@@ -28,25 +25,23 @@ import java.util.Iterator;
import org.apache.mailet.HostAddress;
-
/**
* Provides abstraction for DNS resolutions. The interface is Mail specific.
* It may be a good idea to make the interface more generic or expose
* commonly needed DNS methods.
- *
*/
public interface DNSService {
/**
* The component role used by components implementing this service
*/
- String ROLE = "org.apache.james.api.dnsservice.DNSService";
+ String ROLE = DNSService.class.getName();
/**
* <p>Return a prioritized unmodifiable list of host handling mail
* for the domain.</p>
*
- * <p>First lookup MX hosts, then MX hosts of the CNAME adress, and
+ * <p>First lookup MX hosts, then MX hosts of the CNAME address, and
* if no server is found return the IP of the hostname</p>
*
* @param hostname domain name to look up
@@ -79,7 +74,6 @@ public interface DNSService {
* potentially multiple A records for each MX record. DNS lookups
* are deferred until actually needed.
*
- * @since v2.2.0a16-unstable
* @param domainName - the domain for which to find mail servers
* @return an Iterator over HostAddress instances, sorted by priority
* @throws TemporaryResolutionException get thrown on temporary problems
@@ -97,11 +91,13 @@ public interface DNSService {
public InetAddress getByName(String host) throws UnknownHostException;
/**
+ * @see java.net.InetAddress#getLocalHost()
+ */
+ public InetAddress getLocalHost() throws UnknownHostException;
+
+ /**
* @see org.xbill.DNS.Address#getHostName(InetAddress)
*/
public String getHostName(InetAddress addr);
- /**
- */
- public InetAddress getLocalHost() throws UnknownHostException;
}
Modified: james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/TemporaryResolutionException.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/TemporaryResolutionException.java?rev=954226&r1=954225&r2=954226&view=diff
==============================================================================
--- james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/TemporaryResolutionException.java (original)
+++ james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/TemporaryResolutionException.java Sun Jun 13 14:00:38 2010
@@ -16,14 +16,13 @@
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
-
-
-
-
package org.apache.james.api.dnsservice;
import java.io.IOException;
+/**
+ * Exception to throw when a temporary DNS resolution problem occurs.
+ */
@SuppressWarnings("serial")
public class TemporaryResolutionException extends IOException {
Added: james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/Inet4Network.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/Inet4Network.java?rev=954226&view=auto
==============================================================================
--- james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/Inet4Network.java (added)
+++ james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/Inet4Network.java Sun Jun 13 14:00:38 2010
@@ -0,0 +1,145 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.api.dnsservice.model;
+
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+/**
+ *
+ *
+ */
+public class Inet4Network implements InetNetwork {
+
+ /**
+ * The IP address on which a subnet mask is applied.
+ */
+ private InetAddress network;
+
+ /**
+ * The subnet mask to apply on the IP address.
+ */
+ private InetAddress netmask;
+
+ /**
+ * You need a IP address and an subnetmask to construct an Inet4Network.<br/>
+ * Both constructor parameters are passed via a InetAddress.
+ *
+ * @param ip the InetAddress to init the class
+ * @param netmask the InetAddress represent the netmask to init the class
+ */
+ public Inet4Network(InetAddress ip, InetAddress netmask) {
+ network = maskIP(ip, netmask);
+ this.netmask = netmask;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.james.api.dnsservice.model.InetNetwork#contains(java.net.InetAddress)
+ */
+ public boolean contains(final InetAddress ip) {
+ try {
+ return network.equals(maskIP(ip, netmask));
+ }
+ catch (IllegalArgumentException e) {
+ return false;
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ public String toString() {
+ return network.getHostAddress() + "/" + netmask.getHostAddress();
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ public int hashCode() {
+ return maskIP(network, netmask).hashCode();
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ public boolean equals(Object obj) {
+ return (obj != null)
+ && (obj instanceof InetNetwork)
+ && ((((Inet4Network) obj).network.equals(network)) && (((Inet4Network) obj).netmask.equals(netmask)));
+ }
+ /**
+ * @see #maskIP(byte[], byte[])
+ */
+ private static InetAddress maskIP(final InetAddress ip, final InetAddress mask) {
+ return maskIP(ip.getAddress(), mask.getAddress());
+ }
+
+ /**
+ * Return InetAddress generated of the passed arguments.
+ * Return Null if any error occurs
+ *
+ * @param ip the byte[] represent the ip
+ * @param mask the byte[] represent the netmask
+ * @return inetAddress the InetAddress generated of the passed arguments.
+ */
+ private static InetAddress maskIP(final byte[] ip, final byte[] mask) {
+ if (ip.length != mask.length) {
+ throw new IllegalArgumentException("IP address and mask must be of the same length.");
+ }
+ if (ip.length != 4) {
+ throw new IllegalArgumentException("IP address and mask length must be equal to 4.");
+ }
+ try {
+ byte[] maskedIp = new byte[ip.length];
+ for (int i=0; i < ip.length; i++) {
+ maskedIp[i] = (byte) (ip[i] & mask[i]);
+ }
+ return getByAddress(maskedIp);
+ } catch (UnknownHostException e) {
+ return null;
+ }
+ }
+
+ /**
+ * Return InetAddress which represent the given byte[]
+ *
+ * @param ip the byte[] represent the ip
+ * @return ip the InetAddress generated of the given byte[]
+ * @throws java.net.UnknownHostException
+ */
+ private static InetAddress getByAddress(byte[] ip) throws UnknownHostException {
+
+ InetAddress addr = null;
+
+ addr = Inet4Address.getByAddress(ip);
+
+ if (addr == null) {
+ addr = InetAddress.getByName(
+ Integer.toString(ip[0] & 0xFF, 10) + "."
+ + Integer.toString(ip[1] & 0xFF, 10) + "."
+ + Integer.toString(ip[2] & 0xFF, 10) + "."
+ + Integer.toString(ip[3] & 0xFF, 10));
+ }
+
+ return addr;
+
+ }
+
+}
Propchange: james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/Inet4Network.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/Inet6Network.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/Inet6Network.java?rev=954226&view=auto
==============================================================================
--- james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/Inet6Network.java (added)
+++ james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/Inet6Network.java Sun Jun 13 14:00:38 2010
@@ -0,0 +1,169 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.api.dnsservice.model;
+
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+/**
+ *
+ *
+ */
+public class Inet6Network implements InetNetwork {
+
+ /**
+ * The IP address on which a subnet mask is applied.
+ */
+ private InetAddress network;
+
+ /**
+ * The subnet mask to apply on the IP address.
+ */
+ private Integer netmask;
+
+ /**
+ * You need a IP address (InetAddress) and an subnetmask (Integer) to construct an Inet6Network.<br/>
+ *
+ * @param ip the InetAddress to init the class
+ * @param netmask the InetAddress represent the netmask to init the class
+ */
+ public Inet6Network(InetAddress ip, Integer netmask) {
+ network = maskIP(ip, netmask);
+ this.netmask = netmask;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.james.api.dnsservice.model.InetNetwork#contains(java.net.InetAddress)
+ */
+ public boolean contains(final InetAddress ip) {
+ try {
+ return network.equals(maskIP(ip, netmask));
+ }
+ catch (IllegalArgumentException e) {
+ return false;
+ }
+ }
+
+ /**
+ * Return String representation of this class.
+ *
+ * @return string String representation of this class
+ */
+ public String toString() {
+ return network.getHostAddress() + "/" + netmask;
+ }
+
+ /**
+ * Return hashCode representation of this class
+ *
+ * @return hashCode the hashCode representation of this class
+ */
+ public int hashCode() {
+ return maskIP(network, netmask).hashCode();
+ }
+
+ /**
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ public boolean equals(Object obj) {
+ return (obj != null)
+ && (obj instanceof InetNetwork)
+ && ((((Inet6Network) obj).network.equals(network)) && (((Inet6Network) obj).netmask.equals(netmask)));
+ }
+
+
+ /**
+ * @see #maskIP(byte[], byte[])
+ */
+ private static InetAddress maskIP(final InetAddress ip, Integer mask) {
+ byte[] maskBytes = new byte[16];
+ int i = 0;
+ while (mask > 0) {
+ maskBytes[i] = (byte) 255;
+ i++;
+ mask = (mask >> 1);
+ }
+ return maskIP(ip.getAddress(), maskBytes);
+ }
+
+ /**
+ * Return InetAddress generated of the passed arguments.
+ * Return Null if any error occurs
+ *
+ * @param ip the byte[] represent the ip
+ * @param mask the byte[] represent the netmask
+ * @return inetAddress the InetAddress generated of the passed arguments.
+ */
+ private static InetAddress maskIP(final byte[] ip, final byte[] mask) {
+ if (ip.length != mask.length) {
+ throw new IllegalArgumentException("IP address and mask must be of the same length.");
+ }
+ if (ip.length != 16) {
+ throw new IllegalArgumentException("IP address and mask length must be equal to 16.");
+ }
+ try {
+ byte[] maskedIp = new byte[ip.length];
+ for (int i=0; i < ip.length; i++) {
+ maskedIp[i] = (byte) (ip[i] & mask[i]);
+ }
+ return getByAddress(maskedIp);
+ } catch (UnknownHostException e) {
+ return null;
+ }
+ }
+
+ /**
+ * Return InetAddress which represent the given byte[]
+ *
+ * @param ip
+ * the byte[] represent the ip
+ * @return ip the InetAddress generated of the given byte[]
+ * @throws java.net.UnknownHostException
+ */
+ private static InetAddress getByAddress(byte[] ip) throws UnknownHostException {
+
+ InetAddress addr = Inet6Address.getByAddress(ip);
+
+ // TODO Don't know if this is correct?
+ if (addr == null) {
+ addr = InetAddress.getByName(
+ Integer.toString(ip[0] & 0xFF, 10) + ":"
+ + Integer.toString(ip[1] & 0xFF, 10) + ":"
+ + Integer.toString(ip[2] & 0xFF, 10) + ":"
+ + Integer.toString(ip[3] & 0xFF, 10) + ":"
+ + Integer.toString(ip[4] & 0xFF, 10) + ":"
+ + Integer.toString(ip[5] & 0xFF, 10) + ":"
+ + Integer.toString(ip[6] & 0xFF, 10) + ":"
+ + Integer.toString(ip[7] & 0xFF, 10) + ":"
+ + Integer.toString(ip[8] & 0xFF, 10) + ":"
+ + Integer.toString(ip[9] & 0xFF, 10) + ":"
+ + Integer.toString(ip[10] & 0xFF, 10) + ":"
+ + Integer.toString(ip[11] & 0xFF, 10) + ":"
+ + Integer.toString(ip[12] & 0xFF, 10) + ":"
+ + Integer.toString(ip[13] & 0xFF, 10) + ":"
+ + Integer.toString(ip[14] & 0xFF, 10) + ":"
+ + Integer.toString(ip[15] & 0xFF, 10));
+ }
+
+ return addr;
+
+ }
+
+}
Propchange: james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/Inet6Network.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/InetNetwork.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/InetNetwork.java?rev=954226&view=auto
==============================================================================
--- james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/InetNetwork.java (added)
+++ james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/InetNetwork.java Sun Jun 13 14:00:38 2010
@@ -0,0 +1,40 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.api.dnsservice.model;
+
+import java.net.InetAddress;
+
+/**
+ * An InetNetwork represents a IPv4 or IPv6 address with a subnet mask.<br/>
+ * The subnet mask allows to represent one or more host (a "network of hosts").
+ *
+ * Do not confuse the InetAddress.toString() returning a "hostname/ip_address" (optional hostname)
+ * with the InetNetwork.toString() that returns a "ip_address/subnet_mask".
+ */
+public interface InetNetwork {
+
+ /**
+ * Return true if the network contains the given name
+ *
+ * @param name hostname or ipAddress
+ * @return true if the network contains the ip address
+ */
+ boolean contains(InetAddress ip);
+
+}
Propchange: james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/model/InetNetwork.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/util/InetNetworkBuilder.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/util/InetNetworkBuilder.java?rev=954226&view=auto
==============================================================================
--- james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/util/InetNetworkBuilder.java (added)
+++ james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/util/InetNetworkBuilder.java Sun Jun 13 14:00:38 2010
@@ -0,0 +1,200 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.api.dnsservice.util;
+
+import java.net.UnknownHostException;
+
+import org.apache.james.api.dnsservice.DNSService;
+import org.apache.james.api.dnsservice.model.Inet4Network;
+import org.apache.james.api.dnsservice.model.Inet6Network;
+import org.apache.james.api.dnsservice.model.InetNetwork;
+
+/**
+ * Builds a InetNetwork (Inet4Network or Inet6Network) in function
+ * on the provided string pattern that represents a subnet.
+ *
+ * Inet4Network is constructed based on the IPv4
+ * subnet expressed in one of several formats:
+ * IPv4 Format Example
+ * Explicit address 127.0.0.1
+ * Address with a wildcard 127.0.0.*
+ * Domain name myHost.com
+ * Domain name + prefix-length myHost.com/24
+ * Domain name + mask myHost.com/255.255.255.0
+ * IP address + prefix-length 127.0.0.0/8
+ * IP + mask 127.0.0.0/255.0.0.0
+ * For more information on IP V4, see RFC 1518 and RFC 1519.
+ *
+ * Inet6Network is constructed based on the IPv4
+ * 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
+ * The following V6 formats will be supported later:
+ * Domain name myHost.com
+ * 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)
+ */
+public class InetNetworkBuilder {
+
+ /**
+ * The DNS Server used to create InetAddress for
+ * hostnames and IP adresses.
+ */
+ private DNSService dnsServer;
+
+ /**
+ * Constructs a InetNetwork.
+ *
+ * @param dnsServer the DNSService to use
+ */
+ public InetNetworkBuilder(DNSService dnsServer) {
+ this.dnsServer = dnsServer;
+ }
+
+ /**
+ * Creates a InetNetwork for the given String.
+ * Depending on the provided pattern and the platform configuration
+ * (IPv4 and/or IPv6), the returned type will be Inet4Network
+ * or Inet6Network.
+ *
+ * @param netspec the String which is will converted to InetNetwork
+ * @return network the InetNetwork
+ * @throws java.net.UnknownHostException
+ */
+ public InetNetwork getFromString(String netspec) throws UnknownHostException {
+ return isV6(netspec) ? getV6FromString(netspec) : getV4FromString(netspec);
+ }
+
+ /**
+ * Get a Inet4Network for the given String.
+ *
+ * @param netspec the String which is will converted to InetNetwork
+ * @return network the InetNetwork
+ * @throws java.net.UnknownHostException
+ */
+ private InetNetwork getV4FromString(String netspec) throws UnknownHostException {
+
+ if (netspec.endsWith("*")) {
+ netspec = normalizeV4FromAsterisk(netspec);
+ }
+ else {
+ int iSlash = netspec.indexOf('/');
+ if (iSlash == -1) {
+ netspec += "/255.255.255.255";
+ }
+ else if (netspec.indexOf('.', iSlash) == -1) {
+ netspec = normalizeV4FromCIDR(netspec);
+ }
+ }
+
+ return new Inet4Network(
+ dnsServer.getByName(netspec.substring(0, netspec.indexOf('/'))),
+ dnsServer.getByName(netspec.substring(netspec.indexOf('/') + 1)));
+ }
+
+ /**
+ * Get a Inet6Network for the given String.
+ *
+ * @param netspec the String which is will converted to InetNetwork
+ * @return network the InetNetwork
+ * @throws java.net.UnknownHostException
+ */
+ private InetNetwork getV6FromString(String netspec) throws UnknownHostException {
+
+ if (netspec.endsWith("*")) {
+ throw new UnsupportedOperationException("Wildcard for IPv6 not supported");
+ }
+ else {
+ if (netspec.indexOf('/') == -1) {
+ netspec += "/32768";
+ }
+ }
+
+ return new Inet6Network(
+ dnsServer.getByName(netspec.substring(0, netspec.indexOf('/'))),
+ new Integer(netspec.substring(netspec.indexOf('/') + 1)));
+ }
+
+ /**
+ * This converts from an uncommon "wildcard" CIDR format to "address + mask"
+ * format:
+ * * => 000.000.000.0/000.000.000.0
+ * xxx.* => xxx.000.000.0/255.000.000.0
+ * xxx.xxx.* => xxx.xxx.000.0/255.255.000.0
+ * xxx.xxx.xxx.* => xxx.xxx.xxx.0/255.255.255.0
+ *
+ * @param netspec
+ * @return addrMask the address/mask of the given argument
+ */
+ private static String normalizeV4FromAsterisk(final String netspec) {
+
+ String[] masks = {
+ "0.0.0.0/0.0.0.0",
+ "0.0.0/255.0.0.0",
+ "0.0/255.255.0.0",
+ "0/255.255.255.0" };
+
+ char[] srcb = netspec.toCharArray();
+
+ int octets = 0;
+
+ for (int i = 1; i < netspec.length(); i++) {
+ if (srcb[i] == '.')
+ octets++;
+ }
+
+ return (octets == 0) ? masks[0] : netspec.substring(0, netspec.length() - 1).concat(masks[octets]);
+
+ }
+
+ /**
+ * RFC 1518, 1519 - Classless Inter-Domain Routing (CIDR) This converts from
+ * "prefix + prefix-length" format to "address + mask" format, e.g. from
+ * xxx.xxx.xxx.xxx/yy to xxx.xxx.xxx.xxx/yyy.yyy.yyy.yyy.
+ *
+ * @param netspec the xxx.xxx.xxx.xxx/yyy format
+ * @return addrMask the xxx.xxx.xxx.xxx/yyy.yyy.yyy.yyy format
+ */
+ private static String normalizeV4FromCIDR(final String netspec) {
+
+ final int bits = 32 - Integer.parseInt(netspec.substring(netspec.indexOf('/') + 1));
+
+ final int mask = (bits == 32) ? 0 : 0xFFFFFFFF - ((1 << bits) - 1);
+
+ return netspec.substring(0, netspec.indexOf('/') + 1)
+ + Integer.toString(mask >> 24 & 0xFF, 10) + "."
+ + Integer.toString(mask >> 16 & 0xFF, 10) + "."
+ + Integer.toString(mask >> 8 & 0xFF, 10) + "."
+ + Integer.toString(mask >> 0 & 0xFF, 10);
+
+ }
+
+ /**
+ * 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(":");
+ }
+
+}
Propchange: james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/util/InetNetworkBuilder.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/util/NetMatcher.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/util/NetMatcher.java?rev=954226&r1=954225&r2=954226&view=diff
==============================================================================
--- james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/util/NetMatcher.java (original)
+++ james/server/trunk/domain-api/src/main/java/org/apache/james/api/dnsservice/util/NetMatcher.java Sun Jun 13 14:00:38 2010
@@ -16,152 +16,142 @@
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
-
package org.apache.james.api.dnsservice.util;
import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.util.Collection;
-import java.util.ArrayList;
+import java.util.HashSet;
import java.util.Iterator;
+import java.util.Set;
import org.apache.james.api.dnsservice.DNSService;
+import org.apache.james.api.dnsservice.model.InetNetwork;
/**
- * Class which can be used to check if an ipAddress match a network
+ * NetMatcher Class is used to check if an ipAddress match a network.
+ *
+ * NetMatcher provides a means for checking whether
+ * a particular IPv4 or IPv6 address or domain name is within a set of subnets.
*/
-public class NetMatcher
-{
- private DNSService dnsServer;
+public class NetMatcher {
- private ArrayList<InetNetwork> networks;
+ /**
+ * The DNS Service used to build InetNetworks.
+ */
+ private DNSService dnsServer;
+ /**
+ * The Set of InetNetwork to match against.
+ */
+ private Set<InetNetwork> networks;
/**
- * Init the class with the given networks
- *
- * @param nets a Collection which holds all networks
+ * Create a new instance of Netmatcher.
+ *
+ * @param nets a String[] which holds all networks
+ * @param dnsServer the DNSService which will be used in this class
*/
- public void initInetNetworks(final Collection<String> nets)
- {
- networks = new ArrayList<InetNetwork>();
-
- InetNetwork in = new InetNetwork(dnsServer);
-
- for (Iterator<String> iter = nets.iterator(); iter.hasNext(); ) try
- {
- InetNetwork net = in.getFromString(iter.next());
- if (!networks.contains(net)) networks.add(net);
- }
- catch (java.net.UnknownHostException uhe)
- {
- log("Cannot resolve address: " + uhe.getMessage());
- }
- networks.trimToSize();
+ public NetMatcher(final String[] nets, DNSService dnsServer) {
+ this.dnsServer = dnsServer;
+ initInetNetworks(nets);
}
/**
- * Init the class with the given networks
- *
- * @param nets a String[] which holds all networks
+ * Create a new instance of Netmatcher.
+ *
+ * @param nets a Collection which holds all networks
+ * @param dnsServer the DNSService which will be used in this class
*/
- public void initInetNetworks(final String[] nets)
- {
-
- networks = new ArrayList<InetNetwork>();
-
- InetNetwork in = new InetNetwork(dnsServer);
-
- for (int i = 0; i < nets.length; i++) try
- {
- InetNetwork net = in.getFromString(nets[i]);
- if (!networks.contains(net)) networks.add(net);
- }
- catch (java.net.UnknownHostException uhe)
- {
- log("Cannot resolve address: " + uhe.getMessage());
- }
- networks.trimToSize();
+ public NetMatcher(final Collection<String> nets, DNSService dnsServer) {
+ this.dnsServer = dnsServer;
+ initInetNetworks(nets);
}
/**
- * Return true if passed host match a network which was used to init the Netmatcher
+ * The given String may represent an IP address or a host name.
*
- * @param hostIP the ipAddress or hostname to check
- * @return true if match the network
+ * @param hostIP the ipAddress or host name to check
+ * @see #matchInetNetwork(InetAddress)
*/
- public boolean matchInetNetwork(final String hostIP)
- {
+ public boolean matchInetNetwork(final String hostIP) {
+
InetAddress ip = null;
- try
- {
+ try {
ip = dnsServer.getByName(hostIP);
- }
- catch (java.net.UnknownHostException uhe)
- {
+ } catch (UnknownHostException uhe) {
log("Cannot resolve address for " + hostIP + ": " + uhe.getMessage());
}
- boolean sameNet = false;
+ return matchInetNetwork(ip);
- if (ip != null) for (Iterator<InetNetwork> iter = networks.iterator(); (!sameNet) && iter.hasNext(); )
- {
- InetNetwork network = iter.next();
- sameNet = network.contains(ip);
- }
- return sameNet;
}
/**
- * @see #matchInetNetwork(String)
+ * Return true if passed InetAddress match a network which
+ * was used to construct the Netmatcher.
+ *
+ * @param an InetAddress
+ * @return true if match the network
*/
- public boolean matchInetNetwork(final InetAddress ip)
- {
+ public boolean matchInetNetwork(final InetAddress ip) {
+
boolean sameNet = false;
- for (Iterator<InetNetwork> iter = networks.iterator(); (!sameNet) && iter.hasNext(); )
- {
+ for (Iterator<InetNetwork> iter = networks.iterator(); (!sameNet) && iter.hasNext();) {
InetNetwork network = iter.next();
sameNet = network.contains(ip);
}
+
return sameNet;
+
}
/**
- * Create a new instance of Netmatcher
- *
- * @param nets a String[] which holds all networks
- * @param dnsServer the DNSService which will be used in this class
+ * @see InetNetwork#toString()
*/
- public NetMatcher(final String[] nets,DNSService dnsServer)
- {
- this.dnsServer = dnsServer;
- initInetNetworks(nets);
+ public String toString() {
+ return networks.toString();
}
/**
- * Create a new instance of Netmatcher
+ * Can be overwritten for logging
*
- * @param nets a Collection which holds all networks
- * @param dnsServer the DNSService which will be used in this class
- */
- public NetMatcher(final Collection<String> nets,DNSService dnsServer)
- {
- this.dnsServer = dnsServer;
- initInetNetworks(nets);
+ * @param s the String to log
+ */
+ protected void log(String s) {
}
-
+
/**
- * @see InetNetwork#toString()
+ * Init the class with the given networks.
+ *
+ * @param nets a Collection which holds all networks
*/
- public String toString() {
- return networks.toString();
+ private void initInetNetworks(final Collection<String> nets) {
+ initInetNetworks(nets.toArray(new String[]{}));
}
/**
- * Can be overwritten for loggin
+ * Init the class with the given networks.
*
- * @param s the String to log
+ * @param nets a String[] which holds all networks
*/
- protected void log(String s) { }
+ private void initInetNetworks(final String[] nets) {
+
+ networks = new HashSet<InetNetwork>();
+
+ final InetNetworkBuilder inetNetwork = new InetNetworkBuilder(dnsServer);
+
+ for (String net: nets) {
+ try {
+ InetNetwork inet = inetNetwork.getFromString(net);
+ networks.add(inet);
+ } catch (UnknownHostException uhe) {
+ log("Cannot resolve address: " + uhe.getMessage());
+ }
+ }
+
+ }
+
}
Modified: james/server/trunk/domain-api/src/main/java/org/apache/james/api/domainlist/DomainList.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/main/java/org/apache/james/api/domainlist/DomainList.java?rev=954226&r1=954225&r2=954226&view=diff
==============================================================================
--- james/server/trunk/domain-api/src/main/java/org/apache/james/api/domainlist/DomainList.java (original)
+++ james/server/trunk/domain-api/src/main/java/org/apache/james/api/domainlist/DomainList.java Sun Jun 13 14:00:38 2010
@@ -16,15 +16,13 @@
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
-
-
package org.apache.james.api.domainlist;
import java.util.List;
/**
- * This interface provide should be implementated by services which
- * offer domains for which email will accepted
+ * This interface should be implemented by services which
+ * offer domains for which email will accepted.
*
*/
public interface DomainList {
@@ -32,11 +30,11 @@ public interface DomainList {
/**
* The component role used by components implementing this service
*/
- public final static String ROLE ="org.apache.james.api.domainlist.DomainList";
+ public final static String ROLE = DomainList.class.getName();
/**
- * Return List of domains which should be used as localdomains. Return null if no
- * domains were found
+ * Return List of domains which should be used as localdomains.
+ * Return null if no domain is found.
*
* @return domains
*/
@@ -52,7 +50,7 @@ public interface DomainList {
/**
* Set to true to autodetect the hostname of the host on which
- * james is runnin, and add this to the domain service
+ * james is running, and add this to the domain service
* Default is true
*
* @param autodetect set to false for disable
Modified: james/server/trunk/domain-api/src/main/java/org/apache/james/api/domainlist/ManageableDomainList.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/main/java/org/apache/james/api/domainlist/ManageableDomainList.java?rev=954226&r1=954225&r2=954226&view=diff
==============================================================================
--- james/server/trunk/domain-api/src/main/java/org/apache/james/api/domainlist/ManageableDomainList.java (original)
+++ james/server/trunk/domain-api/src/main/java/org/apache/james/api/domainlist/ManageableDomainList.java Sun Jun 13 14:00:38 2010
@@ -16,24 +16,20 @@
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
-
-
-
package org.apache.james.api.domainlist;
-
/**
- * Services which allow to manage the DomainList should implement this interface
+ * Services which allow to manage the DomainList should implement this interface.
*/
public interface ManageableDomainList extends DomainList {
- public final static String ROLE = "org.apache.james.api.domainlist.ManageableDomainList";
+ public final static String ROLE = ManageableDomainList.class.getName();
/**
* Add domain to the service
*
* @param domain domain to add
- * @return true if successfully
+ * @return true if successfull
*/
public boolean addDomain(String domain);
@@ -41,7 +37,8 @@ public interface ManageableDomainList ex
* Remove domain from the service
*
* @param domain domain to remove
- * @return true if succesfully
+ * @return true if successfull
*/
public boolean removeDomain(String domain);
+
}
Modified: james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/AbstractDNSServer.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/AbstractDNSServer.java?rev=954226&r1=954225&r2=954226&view=diff
==============================================================================
--- james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/AbstractDNSServer.java (original)
+++ james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/AbstractDNSServer.java Sun Jun 13 14:00:38 2010
@@ -16,12 +16,8 @@
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
-
-
-
package org.apache.james.api.dnsservice;
-
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;
Added: james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/AbstractDNSServerTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/AbstractDNSServerTest.java?rev=954226&view=auto
==============================================================================
--- james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/AbstractDNSServerTest.java (added)
+++ james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/AbstractDNSServerTest.java Sun Jun 13 14:00:38 2010
@@ -0,0 +1,75 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.api.dnsservice;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import junit.framework.TestCase;
+
+/**
+ * Basic tests for AbstractDNSServer.
+ * The goal is to verify that the interface remains constants and
+ * that the built platform has access to the Internet.
+ */
+public class AbstractDNSServerTest extends TestCase {
+
+ /**
+ * Simple Mock DNSService relaying on InetAddress.
+ */
+ private static final DNSService DNS_SERVER = new AbstractDNSServer() {
+
+ public String getHostName(InetAddress inet) {
+ return inet.getCanonicalHostName();
+ }
+
+ public InetAddress[] getAllByName(String name) throws UnknownHostException {
+ return InetAddress.getAllByName(name);
+ }
+
+ public InetAddress getLocalHost() throws UnknownHostException {
+ return InetAddress.getLocalHost();
+ }
+
+ public InetAddress getByName(String host) throws UnknownHostException {
+ return InetAddress.getByName(host);
+ }
+
+ };
+
+ /**
+ * Simple localhost resolution.
+ *
+ * @throws UnknownHostException
+ */
+ public void testLocalhost() throws UnknownHostException {
+ assertEquals("localhost/127.0.0.1", DNS_SERVER.getByName("localhost").toString());
+ assertEquals("localhost", DNS_SERVER.getHostName(InetAddress.getByName("localhost")).toString());
+ }
+
+ /**
+ * Simple apache.org resolution.
+ *
+ * @throws UnknownHostException
+ */
+ public void testApache() throws UnknownHostException {
+ assertEquals(true, DNS_SERVER.getByName("www.apache.org").toString().startsWith("www.apache.org"));
+ }
+
+}
Propchange: james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/AbstractDNSServerTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/DNSFixture.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/DNSFixture.java?rev=954226&view=auto
==============================================================================
--- james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/DNSFixture.java (added)
+++ james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/DNSFixture.java Sun Jun 13 14:00:38 2010
@@ -0,0 +1,86 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.api.dnsservice;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+/**
+ * Some DNS Fixtures used by various Test related to DNS and InetNetwork.
+ */
+public class DNSFixture {
+
+ public static final String LOCALHOST_IP_V4_ADDRESS_0 = "localhost/16";
+ public static final String LOCALHOST_IP_V4_ADDRESS_1 = "172.16.0.0/16";
+ public static final String LOCALHOST_IP_V4_ADDRESS_2 = "192.168.1.0/255.255.255.0";
+ public static final String[] LOCALHOST_IP_V4_ADDRESSES = new String[]{LOCALHOST_IP_V4_ADDRESS_0, LOCALHOST_IP_V4_ADDRESS_1, LOCALHOST_IP_V4_ADDRESS_2};
+ public static final String[] LOCALHOST_IP_V4_ADDRESSES_DUPLICATE = new String[]{LOCALHOST_IP_V4_ADDRESS_1, LOCALHOST_IP_V4_ADDRESS_1, LOCALHOST_IP_V4_ADDRESS_2, LOCALHOST_IP_V4_ADDRESS_2};
+
+ public static final String LOCALHOST_IP_V6_ADDRESS_0 = "00:00:00:00:00:00:00:1";
+ public static final String LOCALHOST_IP_V6_ADDRESS_1 = "2781:0db8:1234:8612:45ee:0000:f05e:0001/48";
+ public static final String[] LOCALHOST_IP_V6_ADDRESSES = new String[]{LOCALHOST_IP_V6_ADDRESS_0, LOCALHOST_IP_V6_ADDRESS_1};
+ public static final String[] LOCALHOST_IP_V6_ADDRESSES_DUPLICATE = new String[]{LOCALHOST_IP_V6_ADDRESS_0, LOCALHOST_IP_V6_ADDRESS_0, LOCALHOST_IP_V6_ADDRESS_1, LOCALHOST_IP_V6_ADDRESS_1};
+
+ /**
+ * A Mock DNS Server that handles IPv4-only InetAddress.
+ */
+ public static final DNSService DNS_SERVER_IPV4_MOCK = new AbstractDNSServer() {
+
+ public String getHostName(InetAddress inet) {
+ return "localhost";
+ }
+
+ public InetAddress[] getAllByName(String name) throws UnknownHostException {
+ return new InetAddress[] { InetAddress.getByName("127.0.0.1")};
+ }
+
+ public InetAddress getLocalHost() throws UnknownHostException {
+ return InetAddress.getLocalHost();
+ }
+
+ public InetAddress getByName(String host) throws UnknownHostException {
+ return InetAddress.getByName(host);
+ }
+
+ };
+
+ /**
+ * A Mock DNS Server that handles IPv6-only InetAddress.
+ */
+ public static final DNSService DNS_SERVER_IPV6_MOCK = new AbstractDNSServer() {
+
+ public String getHostName(InetAddress inet) {
+ return "localhost";
+ }
+
+ public InetAddress[] getAllByName(String name) throws UnknownHostException {
+ return new InetAddress[] { InetAddress.getByName("::1")};
+ }
+
+ public InetAddress getLocalHost() throws UnknownHostException {
+ return InetAddress.getLocalHost();
+ }
+
+ public InetAddress getByName(String host) throws UnknownHostException {
+ return InetAddress.getByName(host);
+ }
+
+ };
+
+}
Propchange: james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/DNSFixture.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/model/InetNetworkTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/model/InetNetworkTest.java?rev=954226&view=auto
==============================================================================
--- james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/model/InetNetworkTest.java (added)
+++ james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/model/InetNetworkTest.java Sun Jun 13 14:00:38 2010
@@ -0,0 +1,262 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.api.dnsservice.model;
+
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.StringTokenizer;
+
+import junit.framework.TestCase;
+
+/**
+ * Test the InetNetwork class with various IPv4 and IPv6
+ * network specification.
+ */
+public class InetNetworkTest extends TestCase {
+
+ private static InetAddress address;
+
+ private static InetAddress subnetmask4;
+ private static Inet4Network network4;
+
+ private static Integer subnetmask6;
+ private static Inet6Network network6;
+
+ /**
+ * Preliminary test method to validate the InetAddress behaviour
+ * (V4 and V6 name and address).
+ *
+ * The InetAddress.toString() returns a string with format
+ * "name/ip_address". It has no indication of subnetmask.
+ * The name is optional.
+ *
+ * @throws UnknownHostException
+ */
+ public void testInetAddress() throws UnknownHostException {
+
+ // Test name alone (can be IPv4 or IPv6 depending on the OS plaform configuration).
+ address = InetAddress.getByName("localhost");
+ assertEquals(true, address instanceof InetAddress);
+ assertEquals(true, address.toString().contains("localhost"));
+
+ }
+
+ /**
+ * Test method to validate the Inet4Address behavior
+ * (name and address).
+ *
+ * The InetAddress.toString() returns a string with format
+ * "name/ip_address". It has no indication of subnetmask.
+ * The returned name is optional.
+ *
+ * @throws UnknownHostException
+ */
+ public void testInet4Address() throws UnknownHostException {
+
+ // Test Bad IP V4 address.
+ try {
+ address = InetAddress.getByAddress(getBytesFromAddress("127.0.0.1.1"));
+ assertTrue(false);
+ } catch (UnknownHostException e) {
+ assertTrue(true);
+ }
+
+ // Test IP V4 address.
+ address = InetAddress.getByAddress(getBytesFromAddress("127.0.0.1"));
+ assertEquals(true, address instanceof Inet4Address);
+ assertEquals(true, address.toString().contains("/127.0.0.1"));
+
+ // Test IP V4 with 255 values (just 'like' a subnet mask).
+ address = InetAddress.getByAddress(getBytesFromAddress("255.255.225.0"));
+ assertEquals(true, address instanceof Inet4Address);
+ assertEquals(true, address.toString().contains("/255.255.225.0"));
+
+ // Test IP V4 Address with name and IP address.
+ address = InetAddress.getByAddress("localhost", getBytesFromAddress("127.0.0.1"));
+ assertEquals(true, address instanceof Inet4Address);
+ assertEquals(true, address.toString().contains("localhost"));
+ assertEquals(true, address.toString().contains("/127.0.0.1"));
+
+ }
+
+ /**
+ * Test method to validate the Inet6Address behavior
+ * (name and address).
+ *
+ * The InetAddress.toString() returns a string with format
+ * "name/ip_address". It has no indication of subnetmask.
+ * The returned name is optional.
+ *
+ * @throws UnknownHostException
+ */
+ public void testInet6Address() throws UnknownHostException {
+
+ // Test Bad IP V6 address.
+ try {
+ address = InetAddress.getByAddress(getBytesFromAddress("0000:0000:0000:0000:0000:0000:0000:0001:00001"));
+ assertTrue(false);
+ } catch (UnknownHostException e) {
+ assertTrue(true);
+ }
+
+ // Test IP V6 address.
+ address = InetAddress.getByAddress(getBytesFromAddress("0000:0000:0000:0000:0000:0000:0000:0001"));
+ assertEquals(true, address instanceof Inet6Address);
+ assertEquals(true, address.toString().contains("/0:0:0:0:0:0:0:1"));
+
+ // Test IP V6 Address with name and IP address.
+ address = InetAddress.getByAddress("localhost", getBytesFromAddress("0000:0000:0000:0000:0000:0000:0000:0001"));
+ assertEquals(true, address instanceof Inet6Address);
+ assertEquals(true, address.toString().contains("localhost"));
+ assertEquals(true, address.toString().contains("/0:0:0:0:0:0:0:1"));
+
+ }
+
+ /**
+ * Test the Inet4Network.
+ *
+ * @throws UnknownHostException
+ */
+ public void testInet4Network() throws UnknownHostException {
+
+ // Test with null parameter.
+ address = InetAddress.getByAddress(getBytesFromAddress("127.0.0.1"));
+ try {
+ network4 = new Inet4Network(address, null);
+ assertTrue(false);
+ } catch (NullPointerException e) {
+ assertTrue(true);
+ }
+
+ // Test IP V4.
+ address = InetAddress.getByAddress(getBytesFromAddress("127.0.0.1"));
+ subnetmask4 = InetAddress.getByAddress(getBytesFromAddress("255.255.255.0"));
+ network4 = new Inet4Network(address, subnetmask4);
+ assertEquals("127.0.0.0/255.255.255.0", network4.toString());
+
+ }
+
+ /**
+ * Test the Inet6Network.
+ *
+ * @throws UnknownHostException
+ */
+ public void testInet6Network() throws UnknownHostException {
+
+ // Test with null parameter.
+ address = InetAddress.getByAddress(getBytesFromAddress("2781:0db8:1234:8612:45ee:0000:f05e:0001"));
+ try {
+ network6 = new Inet6Network(address, null);
+ assertTrue(false);
+ } catch (NullPointerException e) {
+ assertTrue(true);
+ }
+
+ // Test IP V6 with subnet mask 32768.
+ address = InetAddress.getByAddress(getBytesFromAddress("2781:0db8:1234:8612:45ee:0000:f05e:0001"));
+ subnetmask6 = 32768;
+ network6 = new Inet6Network(address, subnetmask6);
+ assertEquals("2781:db8:1234:8612:45ee:0:f05e:1/32768", network6.toString());
+
+ // Test IP V6 with subnet mask 128.
+ address = InetAddress.getByAddress(getBytesFromAddress("2781:0db8:1234:8612:45ee:0000:f05e:0001"));
+ subnetmask6 = 128;
+ network6 = new Inet6Network(address, subnetmask6);
+ assertEquals("2781:db8:1234:8612:0:0:0:0/128", network6.toString());
+
+ // Test IP V6 with subnet mask 48.
+ address = InetAddress.getByAddress(getBytesFromAddress("2781:0db8:1234:8612:45ee:0000:f05e:0001"));
+ subnetmask6 = 48;
+ network6 = new Inet6Network(address, subnetmask6);
+ assertEquals("2781:db8:1234:0:0:0:0:0/48", network6.toString());
+
+ // Test IP V6 with subnet mask 16.
+ address = InetAddress.getByAddress(getBytesFromAddress("2781:0db8:1234:8612:45ee:0000:f05e:0001"));
+ subnetmask6 = 16;
+ network6 = new Inet6Network(address, subnetmask6);
+ assertEquals("2781:db8:1200:0:0:0:0:0/16", network6.toString());
+
+ // Test IP V6 with subnet mask 2.
+ address = InetAddress.getByAddress(getBytesFromAddress("2781:0db8:1234:8612:45ee:0000:f05e:0001"));
+ subnetmask6 = 2;
+ network6 = new Inet6Network(address, subnetmask6);
+ assertEquals("2781:0:0:0:0:0:0:0/2", network6.toString());
+
+ // Test IP V6 with subnet mask 1.
+ address = InetAddress.getByAddress(getBytesFromAddress("2781:0db8:1234:8612:45ee:0000:f05e:0001"));
+ subnetmask6 = 1;
+ network6 = new Inet6Network(address, subnetmask6);
+ assertEquals("2700:0:0:0:0:0:0:0/1", network6.toString());
+
+ // Test IP V6 with subnet mask 0.
+ address = InetAddress.getByAddress(getBytesFromAddress("2781:0db8:1234:8612:45ee:0000:f05e:0001"));
+ subnetmask6 = 0;
+ network6 = new Inet6Network(address, subnetmask6);
+ assertEquals("0:0:0:0:0:0:0:0/0", network6.toString());
+
+ }
+
+ /**
+ * Returns the bytes representation of an IP address.<br/>
+ * <br/>
+ * The address must respect "xyz.xyz.xyz.xyz" format for IP V4
+ * ("127.0.0.1", "172.16.1.38",..)
+ *
+ * or wxyz:wxyz:wxyz:wxyz:wxyz:wxyz:wxyz:wxyz for IP V6
+ * ("0000:0000:0000:0000:0000:0000:0000:0001"
+ * or "2001:0db8:85a3:0000:0000:8a2e:0370:7334").
+ *
+ * @param the ip address as a string.
+ * @return the byte array representation of the ip address.
+ */
+ private byte[] getBytesFromAddress(String address) {
+
+ if (address.contains(".")) {
+ StringTokenizer st = new StringTokenizer(address, ".");
+ byte[] bytes = new byte[st.countTokens()];
+ int i=0;
+ while (st.hasMoreTokens()) {
+ Integer inb = Integer.parseInt(st.nextToken());
+ bytes[i] = inb.byteValue();
+ i++;
+ }
+ return bytes;
+ }
+
+ else if (address.contains(":")) {
+ StringTokenizer st = new StringTokenizer(address, ":");
+ byte[] bytes = new byte[st.countTokens() * 2];
+ int i=0;
+ while (st.hasMoreTokens()) {
+ String token = st.nextToken();
+ bytes[i] = (byte) Integer.parseInt(token.substring(0, 2), 16);
+ i++;
+ bytes[i] = (byte) Integer.parseInt(token.substring(2, 4), 16);
+ i++;
+ }
+ return bytes;
+ }
+
+ throw new IllegalArgumentException("The address [" + address + "] is not of the correct format. It should at least contain a . or a :");
+
+ }
+
+}
Propchange: james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/model/InetNetworkTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/util/InetNetworkBuilderTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/util/InetNetworkBuilderTest.java?rev=954226&view=auto
==============================================================================
--- james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/util/InetNetworkBuilderTest.java (added)
+++ james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/util/InetNetworkBuilderTest.java Sun Jun 13 14:00:38 2010
@@ -0,0 +1,70 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.api.dnsservice.util;
+
+import java.net.UnknownHostException;
+
+import junit.framework.TestCase;
+
+import org.apache.james.api.dnsservice.DNSFixture;
+import org.apache.james.api.dnsservice.model.InetNetwork;
+
+/**
+ * Test the InetNetworkBuilder.
+ */
+public class InetNetworkBuilderTest extends TestCase {
+
+ private InetNetworkBuilder inetNetworkBuilder;
+ private InetNetwork inetNetwork;
+
+ /**
+ * Verify that InetNetworkBuild return correctly initialized Inet4Network.
+ *
+ * @throws UnknownHostException
+ */
+ public void testInetNetworkBuilderDnsV4() throws UnknownHostException {
+
+ inetNetworkBuilder = new InetNetworkBuilder(DNSFixture.DNS_SERVER_IPV4_MOCK);
+
+ inetNetwork = inetNetworkBuilder.getFromString(DNSFixture.LOCALHOST_IP_V4_ADDRESS_0);
+ assertEquals("127.0.0.0/255.255.0.0", inetNetwork.toString());
+
+ inetNetwork = inetNetworkBuilder.getFromString(DNSFixture.LOCALHOST_IP_V4_ADDRESS_1);
+ assertEquals("172.16.0.0/255.255.0.0", inetNetwork.toString());
+
+ }
+
+ /**
+ * Verify that InetNetworkBuild return correctly initialized Inet6Network.
+ *
+ * @throws UnknownHostException
+ */
+ public void testInetNetworkBuilderDnsV6() throws UnknownHostException {
+
+ inetNetworkBuilder = new InetNetworkBuilder(DNSFixture.DNS_SERVER_IPV6_MOCK);
+
+ inetNetwork = inetNetworkBuilder.getFromString(DNSFixture.LOCALHOST_IP_V6_ADDRESS_0);
+ assertEquals("0:0:0:0:0:0:0:1/32768", inetNetwork.toString());
+
+ inetNetwork = inetNetworkBuilder.getFromString(DNSFixture.LOCALHOST_IP_V6_ADDRESS_1);
+ assertEquals("2781:db8:1234:0:0:0:0:0/48", inetNetwork.toString());
+
+ }
+
+}
Propchange: james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/util/InetNetworkBuilderTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/util/NetMatcherTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/util/NetMatcherTest.java?rev=954226&view=auto
==============================================================================
--- james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/util/NetMatcherTest.java (added)
+++ james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/util/NetMatcherTest.java Sun Jun 13 14:00:38 2010
@@ -0,0 +1,83 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.api.dnsservice.util;
+
+import java.net.UnknownHostException;
+
+import junit.framework.TestCase;
+
+import org.apache.james.api.dnsservice.DNSFixture;
+
+/**
+ * Test the NetMatcher class with various IPv4 and IPv6 parameters.
+ *
+ */
+public class NetMatcherTest extends TestCase {
+
+ /**
+ *
+ */
+ private static NetMatcher netMatcher;
+
+ /**
+ *
+ */
+ public void testIpV4NetworksUniqueness() {
+ netMatcher = new NetMatcher(DNSFixture.LOCALHOST_IP_V4_ADDRESSES_DUPLICATE, DNSFixture.DNS_SERVER_IPV4_MOCK);
+ assertEquals("[172.16.0.0/255.255.0.0, 192.168.1.0/255.255.255.0]", netMatcher.toString());
+ }
+
+ /**
+ *
+ */
+ public void testIpV6NetworksUniqueness() {
+ netMatcher = new NetMatcher(DNSFixture.LOCALHOST_IP_V6_ADDRESSES_DUPLICATE, DNSFixture.DNS_SERVER_IPV6_MOCK);
+ assertEquals("[0:0:0:0:0:0:0:1/32768, 2781:db8:1234:0:0:0:0:0/48]", netMatcher.toString());
+ }
+
+ /**
+ * @throws UnknownHostException
+ */
+ public void testIpV4Matcher() throws UnknownHostException {
+
+ netMatcher = new NetMatcher(DNSFixture.LOCALHOST_IP_V4_ADDRESSES, DNSFixture.DNS_SERVER_IPV4_MOCK);
+
+ assertEquals(true, netMatcher.matchInetNetwork("127.0.0.1"));
+ assertEquals(true, netMatcher.matchInetNetwork("localhost"));
+ assertEquals(true, netMatcher.matchInetNetwork("172.16.15.254"));
+ assertEquals(true, netMatcher.matchInetNetwork("192.168.1.254"));
+ assertEquals(false, netMatcher.matchInetNetwork("192.169.1.254"));
+
+ }
+
+ /**
+ * @throws UnknownHostException
+ */
+ public void testIpV6Matcher() throws UnknownHostException {
+
+ netMatcher = new NetMatcher(DNSFixture.LOCALHOST_IP_V6_ADDRESSES, DNSFixture.DNS_SERVER_IPV6_MOCK);
+
+ 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"));
+ assertEquals(false, netMatcher.matchInetNetwork("2781:0db8:1235:8612:45ee:ffff:fffe:0001"));
+
+ }
+
+}
Propchange: james/server/trunk/domain-api/src/test/java/org/apache/james/api/dnsservice/util/NetMatcherTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: james/server/trunk/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
URL: http://svn.apache.org/viewvc/james/server/trunk/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java?rev=954226&r1=954225&r2=954226&view=diff
==============================================================================
--- james/server/trunk/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java (original)
+++ james/server/trunk/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java Sun Jun 13 14:00:38 2010
@@ -16,40 +16,33 @@
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
-
-
-
package org.apache.james.transport.matchers;
-import org.apache.james.api.dnsservice.DNSService;
-import org.apache.james.api.dnsservice.util.NetMatcher;
+import java.util.Collection;
+import java.util.StringTokenizer;
import javax.annotation.Resource;
import javax.mail.MessagingException;
-import java.util.StringTokenizer;
-import java.util.Collection;
+
+import org.apache.james.api.dnsservice.DNSService;
+import org.apache.james.api.dnsservice.util.NetMatcher;
+import org.apache.mailet.base.GenericMatcher;
/**
- * AbstractNetworkMatcher makes writing IP Address matchers easier.
+ * AbstractNetworkMatcher makes writing IP Address matchers easier.<br/>
+ * <br/>
+ * This class extends the GenericMatcher, and as such, has access to the
+ * matcher condition via GenericMatcher.getCondition().<br/>
+ * On initialization, the init method retrieves the condition from the
+ * defined matcher and create a corresponding NetMatcher.<br/>
+ * The marcher condition has to respect the syntax waited by the NetMacher.<br/>
+ * <br/>
+ * This abstract network matcher needs to be implemented by a concrete class.<br/>
+ * The implementing concrete class will call the allowedNetworks or matchNetwork methods.
*
- * AbstractNetworkMatcher provides a means for checking to see whether
- * a particular IP address or domain is within a set of subnets
- * These subnets may be expressed in one of several formats:
- *
- * Format Example
- * explicit address 127.0.0.1
- * address with a wildcard 127.0.0.*
- * domain name myHost.com
- * domain name + prefix-length myHost.com/24
- * domain name + mask myHost.com/255.255.255.0
- * IP address + prefix-length 127.0.0.0/8
- * IP + mask 127.0.0.0/255.0.0.0
- *
- * For more information, see also: RFC 1518 and RFC 1519.
- *
- * @version $ID$
+ * @see org.apache.james.api.dnsservice.util.NetMatcher
*/
-public abstract class AbstractNetworkMatcher extends org.apache.mailet.base.GenericMatcher {
+public abstract class AbstractNetworkMatcher extends GenericMatcher {
/**
* This is a Network Matcher that should be configured to contain
@@ -62,18 +55,16 @@ public abstract class AbstractNetworkMat
*/
private DNSService dnsServer;
-
public void init() throws MessagingException {
Collection<String> nets = allowedNetworks();
if (nets != null) {
- authorizedNetworks = new NetMatcher(allowedNetworks(),dnsServer) {
+ authorizedNetworks = new NetMatcher(allowedNetworks(), dnsServer) {
protected void log(String s) {
AbstractNetworkMatcher.this.log(s);
}
};
- authorizedNetworks.initInetNetworks(allowedNetworks());
log("Authorized addresses: " + authorizedNetworks.toString());
}
}
@@ -96,7 +87,11 @@ public abstract class AbstractNetworkMat
return authorizedNetworks == null ? false : authorizedNetworks.matchInetNetwork(addr);
}
-
+ /**
+ * Injection setter for the DNSService.
+ *
+ * @param dnsService
+ */
@Resource(name="dnsserver")
public void setDNSService(DNSService dnsService) {
this.dnsServer = dnsService;
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org