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   */
32  public class Subnet {
33  
34      private static final int IP_MASK = 0x80000000;
35      private static final int BYTE_MASK = 0xFF;
36  
37      private InetAddress subnet;
38      private int subnetInt;
39      private int subnetMask;
40      private int suffix;
41  
42      /**
43       * Creates a subnet from CIDR notation. For example, the subnet
44       * 192.168.0.0/24 would be created using the {@link InetAddress}  
45       * 192.168.0.0 and the mask 24.
46       * @param subnet The {@link InetAddress} of the subnet
47       * @param mask The mask
48       */
49      public Subnet(InetAddress subnet, int mask) {
50          if(subnet == null) {
51              throw new NullPointerException("Subnet address can not be null");
52          }
53          if(!(subnet instanceof Inet4Address)) {
54              throw new IllegalArgumentException("Only IPv4 supported");
55          }
56  
57          if(mask < 0 || mask > 32) {
58              throw new IllegalArgumentException("Mask has to be an integer between 0 and 32");
59          }
60          
61          this.subnet = subnet;
62          this.subnetInt = toInt(subnet);
63          this.suffix = mask;
64          
65          // binary mask for this subnet
66          this.subnetMask = IP_MASK >> (mask - 1);
67      }
68  
69      /** 
70       * Converts an IP address into an integer
71       */ 
72      private int toInt(InetAddress inetAddress) {
73          byte[] address = inetAddress.getAddress();
74          int result = 0;
75          for (int i = 0; i < address.length; i++) {
76              result <<= 8;
77              result |= address[i] & BYTE_MASK;
78          }
79          return result;
80      }
81  
82      /**
83       * Converts an IP address to a subnet using the provided 
84       * mask
85       * @param address The address to convert into a subnet
86       * @return The subnet as an integer
87       */
88      private int toSubnet(InetAddress address) {
89          return toInt(address) & subnetMask;
90      }
91      
92      /**
93       * Checks if the {@link InetAddress} is within this subnet
94       * @param address The {@link InetAddress} to check
95       * @return True if the address is within this subnet, false otherwise
96       */
97      public boolean inSubnet(InetAddress address) {
98          return toSubnet(address) == subnetInt;
99      }
100 
101     /**
102      * @see Object#toString()
103      */
104     @Override
105     public String toString() {
106         return subnet.getHostAddress() + "/" + suffix;
107     }
108 
109     @Override
110     public boolean equals(Object obj) {
111         if(!(obj instanceof Subnet)) {
112             return false;
113         }
114         
115         Subnet other = (Subnet) obj;
116         
117         return other.subnetInt == subnetInt && other.suffix == suffix;
118     }
119 
120     
121 }