View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.ipfilter;
17  
18  import org.jboss.netty.logging.InternalLogger;
19  import org.jboss.netty.logging.InternalLoggerFactory;
20  
21  import java.net.InetAddress;
22  import java.net.UnknownHostException;
23  
24  /**
25   * This class allows to check if an IP V4 or V6 Address is contained in a subnet.<BR>
26   * <p/>
27   * Supported IP V4 Formats for the Subnets are: 1.1.1.1/255.255.255.255 or 1.1.1.1/32 (CIDR-Notation)
28   * and (InetAddress,Mask) where Mask is a integer for CIDR-notation or a String for Standard Mask notation.<BR>
29   * <BR><BR>Example1:<BR>
30   * <tt>IpV4Subnet ips = new IpV4Subnet("192.168.1.0/24");</tt><BR>
31   * <tt>System.out.println("Result: "+ ips.contains("192.168.1.123"));</tt><BR>
32   * <tt>System.out.println("Result: "+ ips.contains(inetAddress2));</tt><BR>
33   * <BR>Example1 bis:<BR>
34   * <tt>IpV4Subnet ips = new IpV4Subnet(inetAddress, 24);</tt><BR>
35   * where inetAddress is 192.168.1.0 and inetAddress2 is 192.168.1.123<BR>
36   * <BR><BR>Example2:<BR>
37   * <tt>IpV4Subnet ips = new IpV4Subnet("192.168.1.0/255.255.255.0");</tt><BR>
38   * <tt>System.out.println("Result: "+ ips.contains("192.168.1.123"));</tt><BR>
39   * <tt>System.out.println("Result: "+ ips.contains(inetAddress2));</tt><BR>
40   * <BR>Example2 bis:<BR>
41   * <tt>IpV4Subnet ips = new IpV4Subnet(inetAddress, "255.255.255.0");</tt><BR>
42   * where inetAddress is 192.168.1.0 and inetAddress2 is 192.168.1.123<BR>
43   * <BR>
44   * Supported IP V6 Formats for the Subnets are: a:b:c:d:e:f:g:h/NN (CIDR-Notation)
45   * or any IPV6 notations (like a:b:c:d::/NN, a:b:c:d:e:f:w.x.y.z/NN)
46   * and (InetAddress,Mask) where Mask is a integer for CIDR-notation
47   * and (InetAddress,subnet).<BR>
48   * <BR><BR>Example1:<BR>
49   * <tt>IpSubnet ips = new IpSubnet("1fff:0:0a88:85a3:0:0:0:0/24");</tt><BR>
50   * <tt>IpSubnet ips = new IpSubnet("1fff:0:0a88:85a3::/24");</tt><BR>
51   * <tt>System.out.println("Result: "+ ips.contains("1fff:0:0a88:85a3:0:0:ac1f:8001"));</tt><BR>
52   * <tt>System.out.println("Result: "+ ips.contains(inetAddress2));</tt><BR>
53   * <BR>Example1 bis:<BR>
54   * <tt>IpSubnet ips = new IpSubnet(inetAddress, 24);</tt><BR>
55   * where inetAddress2 is 1fff:0:0a88:85a3:0:0:ac1f:8001<BR>
56   */
57  public class IpSubnet implements IpSet, Comparable<IpSubnet> {
58  
59      private static final InternalLogger logger =
60          InternalLoggerFactory.getInstance(IpSubnet.class);
61  
62      /** Internal representation */
63      private final CIDR cidr;
64  
65      /** Create IpSubnet for ALL (used for ALLOW or DENY ALL) */
66      public IpSubnet() {
67          // ALLOW or DENY ALL
68          cidr = null;
69      }
70  
71      /**
72       * Create IpSubnet using the CIDR or normal Notation<BR>
73       * i.e.:<br>
74       * IpSubnet subnet = new IpSubnet("10.10.10.0/24"); or<br>
75       * IpSubnet subnet = new IpSubnet("10.10.10.0/255.255.255.0"); or<br>
76       * IpSubnet subnet = new IpSubnet("1fff:0:0a88:85a3:0:0:0:0/24");
77       *
78       * @param netAddress a network address as string.
79       */
80      public IpSubnet(String netAddress) throws UnknownHostException {
81          cidr = CIDR.newCIDR(netAddress);
82      }
83  
84      /** Create IpSubnet using the CIDR Notation */
85      public IpSubnet(InetAddress inetAddress, int cidrNetMask) throws UnknownHostException {
86          cidr = CIDR.newCIDR(inetAddress, cidrNetMask);
87      }
88  
89      /** Create IpSubnet using the normal Notation */
90      public IpSubnet(InetAddress inetAddress, String netMask) throws UnknownHostException {
91          cidr = CIDR.newCIDR(inetAddress, netMask);
92      }
93  
94      /**
95       * Compares the given IP-Address against the Subnet and returns true if
96       * the ip is in the subnet-ip-range and false if not.
97       *
98       * @param ipAddr an ipaddress
99       * @return returns true if the given IP address is inside the currently
100      *         set network.
101      */
102     public boolean contains(String ipAddr) throws UnknownHostException {
103         InetAddress inetAddress1 = InetAddress.getByName(ipAddr);
104         return contains(inetAddress1);
105     }
106 
107     /**
108      * Compares the given InetAddress against the Subnet and returns true if
109      * the ip is in the subnet-ip-range and false if not.
110      *
111      * @return returns true if the given IP address is inside the currently
112      *         set network.
113      */
114     public boolean contains(InetAddress inetAddress) {
115         if (cidr == null) {
116             // ANY
117             return true;
118         }
119         return cidr.contains(inetAddress);
120     }
121 
122     @Override
123     public String toString() {
124         return cidr.toString();
125     }
126 
127     @Override
128     public boolean equals(Object o) {
129         if (!(o instanceof IpSubnet)) {
130             return false;
131         }
132         IpSubnet ipSubnet = (IpSubnet) o;
133         return ipSubnet.cidr.equals(cidr);
134     }
135 
136     @Override
137     public int hashCode() {
138         return cidr.hashCode();
139     }
140 
141     /** Compare two IpSubnet */
142     public int compareTo(IpSubnet o) {
143         return cidr.toString().compareTo(o.cidr.toString());
144     }
145 }