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