KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > security > ip > IPRange


1 // $Id: IPRange.java 3567 2003-06-30 22:32:03Z shuber $
2
//
3
// ____.
4
// __/\ ______| |__/\. _______
5
// __ .____| | \ | +----+ \
6
// _______| /--| | | - \ _ | : - \_________
7
// \\______: :---| : : | : | \________>
8
// |__\---\_____________:______: :____|____:_____\
9
// /_____|
10
//
11
// . . . i n j a h i a w e t r u s t . . .
12
//
13

14 package org.jahia.security.ip;
15
16
17 /**
18  * This class represents an IP Range, which are represented by an IP address and
19  * and a subnet mask. The standards describing modern routing protocols often
20  * refer to the extended-network-prefix-length rather than the subnet mask. The
21  * prefix length is equal to the number of contiguous one-bits in the
22  * traditional subnet mask. This means that specifying the network address
23  * 130.5.5.25 with a subnet mask of 255.255.255.0 can also be expressed as
24  * 130.5.5.25/24. The /<prefix-length> notation is more compact and easier to
25  * understand than writing out the mask in its traditional dotted-decimal
26  * format.<br/>
27  * <br/>
28  * <code>
29  * &nbsp;&nbsp;&nbsp;&nbsp;130.5.5.25&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
30  * <b>10</b>000010 . 00000101 . 00000101 . 00011001<br/>
31  * &nbsp;&nbsp;&nbsp;&nbsp;255.255.255.0&nbsp;&nbsp;
32  * 11111111 . 11111111 . 11111111 . 00000000<br/>
33  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
34  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<--extended-network-prefix --><br/>
35  * or<br/>
36  * &nbsp;&nbsp;&nbsp;&nbsp;130.5.5.25/24&nbsp;&nbsp;
37  * <b>10</b>000010 . 00000101 . 00000101 . 00011001<br/>
38  * </code>
39  * <br/>
40  * This class supports both standards : the extended network prefix and the
41  * subnet mask.
42  *
43  * @author Fulco Houkes
44  * @version 1.0
45  *
46  * @see IPNumber
47  */

48 public class IPRange implements Cloneable JavaDoc
49 {
50
51     /** IP address */
52     private IPNumber mIPAddress = null;
53
54     /** IP subnet mask */
55     private IPNumber mIPSubnetMask = null;
56
57     /** extended network prefix */
58     private int mExtendedNetworkPrefix = 0;
59
60
61     //-------------------------------------------------------------------------
62
/**
63      * Constructor.
64      *
65      * @param ip
66      * String representation of the IP address. The two following formats
67      * are supported :<br/>
68      * <li/>xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx
69      * <li/>xxx.xxx.xxx.xxx/xx <- extended network prefix
70      *
71      * @exception InvalidIPRangeException
72      * Throws this exception when the specified string doesn't represent
73      * a valid IP address.
74      */

75     public IPRange (String JavaDoc range)
76         throws InvalidIPRangeException
77     {
78         parseRange (range);
79     }
80
81
82     //-------------------------------------------------------------------------
83
/**
84      * Constructor.
85      *
86      * @param ipAddress
87      * Reference to the IP address number
88      * @param subnetMask
89      * Reference to the subnet mask
90      *
91      * @exception InvalidIPRangeException
92      * Throws this exception when the combination of the IP address and the
93      * subnet mask does not define a valid IP range.
94      * @exception InvalidIPNUmberException
95      * Throws this exception if the specified IP address or subnet mask
96      * do ne define a valid IP number.
97      */

98     public IPRange (IPNumber ipAddress, IPNumber subnetMask)
99         throws InvalidIPRangeException,
100                 InvalidIPNumberException
101     {
102         if ((ipAddress == null) || (subnetMask == null)) {
103             throw new InvalidIPNumberException ();
104         }
105
106         mIPAddress = ipAddress;
107         mIPSubnetMask = subnetMask;
108
109         mExtendedNetworkPrefix = computeNetworkPrefixFromMask (subnetMask);
110         if (mExtendedNetworkPrefix == -1) {
111             throw new InvalidIPRangeException ();
112         }
113     }
114
115
116     //-------------------------------------------------------------------------
117
/**
118      * Constructor.
119      *
120      * @param ipAddress
121      * The reference on the IP address.
122      * @param extendedNetworkPrefix
123      * The extended network prefix (0-32).
124      *
125      * @exception InvalidIPNumberException
126      * Throws this exception if the specified IP address is not valid.
127      * @exception InvalidIPRangeException
128      * Throws this exception if the specified extended network prefix
129      * is not valid.
130      */

131     public IPRange (IPNumber ipAddress, int extendedNetworkPrefix)
132         throws InvalidIPNumberException,
133                 InvalidIPRangeException
134     {
135         if (ipAddress == null) {
136             throw new InvalidIPNumberException();
137         }
138
139         if ((extendedNetworkPrefix < 0) || (extendedNetworkPrefix > 32)) {
140             throw new InvalidIPRangeException();
141         }
142
143         mIPAddress = ipAddress;
144         mExtendedNetworkPrefix = extendedNetworkPrefix;
145         mIPSubnetMask = computeMaskFromNetworkPrefix (extendedNetworkPrefix);
146     }
147
148     //-------------------------------------------------------------------------
149
/**
150      * Private constructor used for cloning.
151      *
152      * @param ipAddress
153      * Reference to the IP address number
154      * @param subnetMask
155      * Reference to the subnet mask
156      * @param extendedNetworkPrefix
157      * The extended network prefix (0-32).
158      */

159     private IPRange (IPNumber ipAddress, IPNumber subnetMask,
160                      int extendedNetworkPrefix)
161     {
162         mIPAddress = ipAddress;
163         mIPSubnetMask = subnetMask;
164         mExtendedNetworkPrefix = extendedNetworkPrefix;
165     }
166
167
168     //-------------------------------------------------------------------------
169
/**
170      * Return the encapsulated IP address.
171      *
172      * @return
173      * The IP address.
174      */

175     public final IPNumber getIPAddress () {
176         return mIPAddress;
177     }
178
179
180     //-------------------------------------------------------------------------
181
/**
182      * Return the encapsulated subnet mask
183      *
184      * @return
185      * The IP range's subnet mask.
186      */

187     public final IPNumber getIPSubnetMask () {
188         return mIPSubnetMask;
189     }
190
191
192     //-------------------------------------------------------------------------
193
/**
194      * Return the extended extended network prefix.
195      *
196      * @return
197      * Return the extended network prefix.
198      */

199     public final int getExtendedNetworkPrefix () {
200         return mExtendedNetworkPrefix;
201     }
202
203
204     //-------------------------------------------------------------------------
205
/**
206      * Convert the IP Range into a string representation.
207      *
208      * @return
209      * Return the string representation of the IP Address following the
210      * common format xxx.xxx.xxx.xxx/xx (IP address/extended network
211      * prefixs).
212      */

213     public String JavaDoc toString ()
214     {
215         StringBuffer JavaDoc result = new StringBuffer JavaDoc (mIPAddress.toString());
216         result.append ("/");
217         result.append (mExtendedNetworkPrefix);
218
219         return result.toString();
220     }
221
222
223     //-------------------------------------------------------------------------
224
/**
225      * Compare the specified IP range to the encapsulated one.
226      *
227      * @param another
228      * The IP range to be compared.
229      *
230      * @return
231      * Return <code>true</code> if the encapsulated IP range is the same
232      * as the specified one, otherwise return <code>false</code>.
233      */

234     public boolean equals (Object JavaDoc another)
235     {
236         if (another instanceof IPRange) {
237             IPRange range = (IPRange)another;
238
239             return (mIPAddress.equals (range.getIPAddress()) &&
240                    (mExtendedNetworkPrefix == range.mExtendedNetworkPrefix));
241         }
242         return false;
243     }
244
245
246     //-------------------------------------------------------------------------
247
/**
248      * Parse the IP range string representation.
249      *
250      * @param range
251      * String representation of the IP range.
252      *
253      * @exception InvalidIPRangeException
254      * Throws this exception if the specified range is not a valid IP
255      * network range.
256      */

257     protected void parseRange (String JavaDoc range)
258         throws InvalidIPRangeException
259     {
260         if (range == null) {
261             throw new InvalidIPRangeException (range);
262         }
263
264         int index = range.indexOf ("/");
265         if (index == -1) {
266             throw new InvalidIPRangeException (range);
267         }
268
269         try {
270             mIPAddress = new IPNumber (range.substring (0, index));
271
272             String JavaDoc subnetStr = range.substring (index+1);
273
274             // try to convert the remaining part of the range into a decimal
275
// value.
276
try {
277                 mExtendedNetworkPrefix = Integer.parseInt (subnetStr);
278                 if ((mExtendedNetworkPrefix < 0) ||
279                     (mExtendedNetworkPrefix > 32)) {
280                     throw new InvalidIPRangeException (range);
281                 }
282
283                 mIPSubnetMask =
284                         computeMaskFromNetworkPrefix (mExtendedNetworkPrefix);
285                 subnetStr = null;
286             }
287             catch (NumberFormatException JavaDoc ex) {
288
289                 // the remaining part is not a valid decimal value.
290
// Check if it's a decimal-dotted notation.
291
mIPSubnetMask = new IPNumber (subnetStr);
292                 subnetStr = null;
293
294                 // create the corresponding subnet decimal
295
mExtendedNetworkPrefix =
296                         computeNetworkPrefixFromMask (mIPSubnetMask);
297                 if (mExtendedNetworkPrefix == -1) {
298                     throw new InvalidIPRangeException (range);
299                 }
300             }
301
302         }
303         catch (InvalidIPNumberException ex) {
304             throw new InvalidIPRangeException (range);
305         }
306         catch (IndexOutOfBoundsException JavaDoc ex) {
307             throw new InvalidIPRangeException (range);
308         }
309     }
310
311
312     //-------------------------------------------------------------------------
313
/**
314      * Compute the extended network prefix from the IP subnet mask.
315      *
316      * @param mask
317      * Reference to the subnet mask IP number.
318      * @return
319      * Return the extended network prefix. Return -1 if the specified mask cannot
320      * be converted into a extended prefix network.
321      */

322     private int computeNetworkPrefixFromMask (IPNumber mask)
323     {
324         int result = 0;
325         int tmp = mask.getIPNumber();
326
327         while ((tmp & 0x00000001) == 0x00000001) {
328             result++;
329             tmp = tmp >>> 1;
330         }
331
332         if (tmp != 0) {
333             return -1;
334         }
335         return result;
336     }
337
338
339     //-------------------------------------------------------------------------
340
/**
341      * Convert a extended network prefix integer into an IP number.
342      *
343      * @param prefix
344      * The network prefix number.
345      *
346      * @return
347      * Return the IP number corresponding to the extended network prefix.
348      */

349     private IPNumber computeMaskFromNetworkPrefix (int prefix)
350         throws InvalidIPNumberException
351     {
352         int subnet = 0;
353         for (int i=0; i<prefix; i++) {
354             subnet = subnet << 1;
355             subnet += 1;
356         }
357         return new IPNumber (subnet);
358     }
359
360
361     //-------------------------------------------------------------------------
362
/**
363      * Check if the specified IP address is in the encapsulated range.
364      *
365      * @param address
366      * The IP address to be tested.
367      *
368      * @return
369      * Return <code>true</code> if the specified IP address is in the
370      * encapsulated IP range, otherwise return <code>false</code>.
371      */

372     public boolean isIPAddressInRange (IPNumber address)
373     {
374         int result1 = address.getIPNumber() & mIPSubnetMask.getIPNumber();
375         int result2 = mIPAddress.getIPNumber() & mIPSubnetMask.getIPNumber();
376
377         return result1 == result2;
378     }
379
380
381     //-------------------------------------------------------------------------
382
/**
383      * Clone the encapsulated IP Range.
384      *
385      * @return
386      * Return a new object representing the encapsulated IP Range.
387      */

388     public Object JavaDoc clone ()
389     {
390         return new IPRange ((IPNumber)mIPAddress.clone(),
391                             (IPNumber)mIPSubnetMask.clone(),
392                             mExtendedNetworkPrefix);
393     }
394 }
395
396
Popular Tags