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  package org.apache.directory.server.dhcp.store;
21  
22  
23  import java.net.InetAddress;
24  import java.net.UnknownHostException;
25  import java.util.Arrays;
26  
27  
28  /**
29   * The definition of a Subnet.
30   * 
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   * @version $Rev: 545042 $, $Date: 2007-06-06 22:32:01 -0500 (Mi, 06 Jun 2007) $
33   */
34  public class Subnet extends DhcpConfigElement
35  {
36      /** the subnet's address */
37      private final InetAddress address;
38  
39      /** the subnet's netmask */
40      private final InetAddress netmask;
41  
42      /** the subnet's range: minimum address in range */
43      private InetAddress rangeMin;
44  
45      /** the subnet's range: maximum address in range */
46      private InetAddress rangeMax;
47  
48  
49      public Subnet(InetAddress address, InetAddress netmask, InetAddress rangeMin, InetAddress rangeMax)
50      {
51          // mask address to match subnet
52          byte masked[] = netmask.getAddress();
53          byte addrBytes[] = netmask.getAddress();
54          for ( int i = 0; i < addrBytes.length; i++ )
55              masked[i] &= addrBytes[i];
56  
57          if ( !Arrays.equals( masked, addrBytes ) )
58              try
59              {
60                  address = InetAddress.getByAddress( masked );
61              }
62              catch ( UnknownHostException e )
63              {
64                  // ignore - doesn't happen.
65              }
66  
67          this.address = address;
68          this.netmask = netmask;
69          this.rangeMin = rangeMin;
70          this.rangeMax = rangeMax;
71      }
72  
73  
74      public InetAddress getAddress()
75      {
76          return address;
77      }
78  
79  
80      public InetAddress getNetmask()
81      {
82          return netmask;
83      }
84  
85  
86      public InetAddress getRangeMax()
87      {
88          return rangeMax;
89      }
90  
91  
92      public void setRangeMax( InetAddress rangeMax )
93      {
94          this.rangeMax = rangeMax;
95      }
96  
97  
98      public InetAddress getRangeMin()
99      {
100         return rangeMin;
101     }
102 
103 
104     public void setRangeMin( InetAddress rangeMin )
105     {
106         this.rangeMin = rangeMin;
107     }
108 
109 
110     /**
111      * Check whether the given client address resides within this subnet and
112      * possibly range.
113      * 
114      * @param clientAddress
115      * @return boolean
116      */
117     public boolean contains( InetAddress clientAddress )
118     {
119         // check address type
120         if ( !clientAddress.getClass().equals( address.getClass() ) )
121             return false;
122 
123         byte client[] = clientAddress.getAddress();
124         byte masked[] = netmask.getAddress();
125         for ( int i = 0; i < masked.length; i++ )
126             masked[i] &= client[i];
127 
128         return Arrays.equals( masked, address.getAddress() );
129     }
130 
131 
132     /**
133      * Check whether the specified address is within the range for this subnet.
134      * 
135      * @param clientAddress
136      * @return boolean
137      */
138     public boolean isInRange( InetAddress clientAddress )
139     {
140         byte client[] = clientAddress.getAddress();
141         byte masked[] = netmask.getAddress();
142         for ( int i = 0; i < masked.length; i++ )
143             masked[i] &= client[i];
144 
145         if ( null != rangeMin )
146             if ( arrayComp( masked, rangeMin.getAddress() ) < 0 )
147                 return false;
148 
149         if ( null != rangeMin )
150             if ( arrayComp( masked, rangeMax.getAddress() ) > 0 )
151                 return false;
152 
153         return true;
154     }
155 
156 
157     private static int arrayComp( byte a1[], byte a2[] )
158     {
159         for ( int i = 0; i < a1.length && i < a2.length; i++ )
160         {
161             if ( a1[i] != a2[i] )
162                 return ( a1[i] & 0xff ) - ( a2[i] & 0xff );
163         }
164 
165         return a1.length - a2.length;
166     }
167 }