View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  
21  package org.apache.mina.filter.firewall;
22  
23  import java.net.Inet4Address;
24  import java.net.InetAddress;
25  
26  /**
27   * A IP subnet using the CIDR notation. Currently, only IP version 4
28   * address are supported.
29   *
30   * @author The Apache MINA Project (dev@mina.apache.org)
31   * @version $Rev: 636538 $, $Date: 2008-03-12 22:19:27 +0000 (Wed, 12 Mar 2008) $
32   */
33  public class Subnet {
34  
35  	private static final int IP_MASK = 0x80000000;
36  	private static final int BYTE_MASK = 0xFF;
37  
38  	private InetAddress subnet;
39  	private int subnetInt;
40  	private int subnetMask;
41  	private int suffix;
42  
43  	/**
44  	 * Creates a subnet from CIDR notation. For example, the subnet
45  	 * 192.168.0.0/24 would be created using the {@link InetAddress}  
46  	 * 192.168.0.0 and the mask 24.
47  	 * @param subnet The {@link InetAddress} of the subnet
48  	 * @param mask The mask
49  	 */
50  	public Subnet(InetAddress subnet, int mask) {
51  		if(subnet == null) {
52  			throw new NullPointerException("Subnet address can not be null");
53  		}
54  		if(!(subnet instanceof Inet4Address)) {
55  			throw new IllegalArgumentException("Only IPv4 supported");
56  		}
57  
58  		if(mask < 0 || mask > 32) {
59  			throw new IllegalArgumentException("Mask has to be an integer between 0 and 32");
60  		}
61  		
62  		this.subnet = subnet;
63  		this.subnetInt = toInt(subnet);
64  		this.suffix = mask;
65  		
66  		// binary mask for this subnet
67  		this.subnetMask = IP_MASK >> (mask - 1);
68  	}
69  
70  	/** 
71  	 * Converts an IP address into an integer
72  	 */ 
73  	private int toInt(InetAddress inetAddress) {
74  		byte[] address = inetAddress.getAddress();
75  		int result = 0;
76  		for (int i = 0; i < address.length; i++) {
77  			result <<= 8;
78  			result |= address[i] & BYTE_MASK;
79  		}
80  		return result;
81  	}
82  
83  	/**
84  	 * Converts an IP address to a subnet using the provided 
85  	 * mask
86  	 * @param address The address to convert into a subnet
87  	 * @return The subnet as an integer
88  	 */
89  	private int toSubnet(InetAddress address) {
90  		return toInt(address) & subnetMask;
91  	}
92  	
93  	/**
94  	 * Checks if the {@link InetAddress} is within this subnet
95  	 * @param address The {@link InetAddress} to check
96  	 * @return True if the address is within this subnet, false otherwise
97  	 */
98  	public boolean inSubnet(InetAddress address) {
99  		return toSubnet(address) == subnetInt;
100 	}
101 
102 	/**
103 	 * @see Object#toString()
104 	 */
105 	@Override
106 	public String toString() {
107 		return subnet.getHostAddress() + "/" + suffix;
108 	}
109 
110 	@Override
111 	public boolean equals(Object obj) {
112 		if(!(obj instanceof Subnet)) {
113 			return false;
114 		}
115 		
116 		Subnet other = (Subnet) obj;
117 		
118 		return other.subnetInt == subnetInt && other.suffix == suffix;
119 	}
120 
121 	
122 }