KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: IPNumber.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 import java.util.NoSuchElementException JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18
19
20 /**
21  * This class represents an IP number represented by an 32 bits
22  * integer value. Dotted-decimal notation divides the 32-bit Internet address
23  * into four 8-bit (byte) fields and specifies the value of each field
24  * independently as a decimal number with the fields separated by dots :<br/>
25  * <br/>
26  * <code>
27  * &nbsp;&nbsp;&nbsp;&nbsp;10010001 . 00001010 . 00100010 . 00000011<br/>
28  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;145&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
29  * &nbsp;&nbsp;10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;34
30  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3<br/>
31  * <br/>
32  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
33  * &nbsp;&nbsp;&nbsp;-> 145.10.34.3<br/>
34  * </code>
35  *
36  * <br/><br/>
37  *
38  * IP numbers are classified into three classes :<br/>
39  * <br/>
40  *
41  * class A:<br/>
42  * <br/><code>
43  * &nbsp;&nbsp;&nbsp;&nbsp;bit#&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;1&nbsp;
44  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
45  * &nbsp;&nbsp;7&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
46  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
47  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;31<br/>
48  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
49  * +--+-------------------+------------------------------+<br/>
50  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
51  * |0&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
52  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;
53  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
54  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
55  * |<br/>
56  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
57  * +--+-------------------+------------------------------+<br/>
58  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
59  * <-- network number -->&nbsp;<------- host number --------><br/>
60  * </code>
61  *
62  * <br/><br/>
63  *
64  * class B:<br/>
65  * <br/><code>
66  * &nbsp;&nbsp;&nbsp;&nbsp;bit#&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;2&nbsp;&nbsp;
67  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
68  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15&nbsp;16&nbsp;&nbsp;
69  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
70  * &nbsp;&nbsp;&nbsp;&nbsp;31<br/>
71  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
72  * +--+-------------------------+------------------------+<br/>
73  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
74  * |10|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
75  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
76  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
77  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
78  * |<br/>
79  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
80  * +--+-------------------------+------------------------+<br/>
81  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
82  * <----- network number ----->&nbsp;<---- host number -----><br/>
83  * </code>
84  *
85  * <br/><br/>
86  *
87  * class C:<br/>
88  * <br/><code>
89  * &nbsp;&nbsp;&nbsp;&nbsp;bit#&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;3&nbsp;
90  * &nbsp;
91  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
92  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;23&nbsp;24
93  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
94  * &nbsp;31<br/>
95  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
96  * +---+-----------------------------+-------------------+<br/>
97  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
98  * |110|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
99  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
100  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
101  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/>
102  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
103  * +---+-----------------------------+-------------------+<br/>
104  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
105  * <------- network number -------->&nbsp;<-- host number --><br/>
106  * </code>
107  *
108  * <br/><br/>
109  *
110  * @author Fulco Houkes
111  * @version 1.0
112  */

113 public class IPNumber implements Cloneable JavaDoc
114 {
115
116     /** IP address */
117     protected int mIPNumber = 0;
118
119
120     //-------------------------------------------------------------------------
121
/**
122      * Constructor.
123      *
124      * @param ip
125      * String representation of the IP address. The format of the ip's
126      * string representation must follow the decimal-dotted notation
127      * xxx.xxx.xxx.xxx.
128      *
129      * @exception InvalidIPNumberException
130      * Throws this exception when the specified string doesn't represent
131      * a valid IP address.
132      */

133     public IPNumber (String JavaDoc ipNumberStr)
134         throws InvalidIPNumberException
135     {
136         mIPNumber = parseIPNUmber (ipNumberStr);
137     }
138
139
140     //-------------------------------------------------------------------------
141
/**
142      * Constructor.
143      *
144      * @param ip
145      * Binary representation of the IP number.
146      */

147     public IPNumber (int number)
148     {
149         mIPNumber = number;
150     }
151
152
153     //-------------------------------------------------------------------------
154
/**
155      * Return the integer representation of the IP address.
156      *
157      * @return
158      * The IP address.
159      */

160     public final int getIPNumber () {
161         return mIPNumber;
162     }
163
164
165     //-------------------------------------------------------------------------
166
/**
167      * Return the string representation of the IP Address following the common
168      * decimal-dotted notation xxx.xxx.xxx.xxx.
169      *
170      * @return
171      * Return the string representation of the IP number.
172      */

173     public String JavaDoc toString ()
174     {
175         StringBuffer JavaDoc result = new StringBuffer JavaDoc ();
176         int temp;
177
178         temp = mIPNumber & 0x000000FF;
179         result.append (temp);
180         result.append (".");
181
182         temp = (mIPNumber >> 8) & 0x000000FF;
183         result.append (temp);
184         result.append (".");
185
186         temp = (mIPNumber >> 16) & 0x000000FF;
187         result.append (temp);
188         result.append (".");
189
190         temp = (mIPNumber >> 24) & 0x000000FF;
191         result.append (temp);
192
193         return result.toString();
194     }
195
196
197     //-------------------------------------------------------------------------
198
/**
199      * Check if the IP number is belongs to a Class A IP number.
200      *
201      * @return
202      * Return <code>true</code> if the encapsulated IP number belongs to
203      * a class A IP number, otherwise returne <code>false</code>.
204      */

205     public final boolean isClassA ()
206     {
207         return (mIPNumber & 0x00000001) == 0;
208     }
209
210
211     //-------------------------------------------------------------------------
212
/**
213      * Check if the IP number is belongs to a Class B IP number.
214      *
215      * @return
216      * Return <code>true</code> if the encapsulated IP number belongs to
217      * a class B IP number, otherwise returne <code>false</code>.
218      */

219     public final boolean isClassB ()
220     {
221         return (mIPNumber & 0x00000003) == 1;
222     }
223
224
225     //-------------------------------------------------------------------------
226
/**
227      * Check if the IP number is belongs to a Class C IP number.
228      *
229      * @return
230      * Return <code>true</code> if the encapsulated IP number belongs to
231      * a class C IP number, otherwise returne <code>false</code>.
232      */

233     public final boolean isClassC ()
234     {
235         return (mIPNumber & 0x00000007) == 3;
236     }
237
238     //-------------------------------------------------------------------------
239
/**
240      * Convert a decimal-dotted notation representation of an IP number into
241      * an 32 bits interger value.
242      *
243      * @param ipNumberStr
244      * Decimal-dotted notation (xxx.xxx.xxx.xxx) of the IP number.
245      *
246      * @return
247      * Return the 32 bits integer representation of the IP number.
248      *
249      * @exception InvalidIPNumberException
250      * Throws this exception if the specified IP number is not compliant
251      * to the decimal-dotted notation xxx.xxx.xxx.xxx.
252      */

253     protected int parseIPNUmber (String JavaDoc ipNumberStr)
254         throws InvalidIPNumberException
255     {
256         int result = 0;
257
258         if (ipNumberStr == null) {
259             throw new InvalidIPNumberException ();
260         }
261
262         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc (ipNumberStr, ".");
263         try {
264             String JavaDoc tmp = ipNumberStr;
265
266             // get the 3 first numbers
267
int offset = 0;
268             for (int i=0; i<3; i++) {
269
270                 // get the position of the first dot
271
int index = tmp.indexOf(".");
272
273                 // if there is not a dot then the ip string representation is
274
// not compliant to the decimal-dotted notation.
275
if (index != -1) {
276
277                     // get the number before the dot and convert it into
278
// an integer.
279
String JavaDoc numberStr = tmp.substring (0, index);
280                     int number = Integer.parseInt (numberStr);
281                     if ((number < 0) || (number > 255)) {
282                         throw new InvalidIPNumberException (ipNumberStr);
283                     }
284
285                     result += number << offset;
286                     offset += 8;
287                     tmp = tmp.substring (index+1);
288                 } else {
289                     throw new InvalidIPNumberException (ipNumberStr);
290                 }
291             }
292
293             // the remaining part of the string should be the last number.
294
if (tmp.length() > 0) {
295                 int number = Integer.parseInt (tmp);
296                 if ((number < 0) || (number > 255)) {
297                     throw new InvalidIPNumberException (ipNumberStr);
298                 }
299
300                 result += number << offset;
301                 mIPNumber = result;
302             } else {
303                 throw new InvalidIPNumberException (ipNumberStr);
304             }
305         }
306         catch (NoSuchElementException JavaDoc ex) {
307             throw new InvalidIPNumberException (ipNumberStr);
308         }
309         catch (NumberFormatException JavaDoc ex) {
310             throw new InvalidIPNumberException (ipNumberStr);
311         }
312
313         return result;
314     }
315
316
317     //-------------------------------------------------------------------------
318
/**
319      * Compare the specified IP number to the encapsulated one.
320      *
321      * @param another
322      * The IP number to be compared.
323      *
324      * @return
325      * Return <code>true</code> if the encapsulated IP number is the same
326      * as the specified one, otherwise return <code>false</code>.
327      */

328     public boolean equals (Object JavaDoc another)
329     {
330         if (another instanceof IPNumber) {
331             return mIPNumber == ((IPNumber)another).mIPNumber;
332         }
333         return false;
334     }
335
336     //-------------------------------------------------------------------------
337
/**
338      * Clone the encapsulated IP number.
339      *
340      * @return
341      * Return a new object representing the encapsulated IP number.
342      */

343     public Object JavaDoc clone ()
344     {
345         return new IPNumber (mIPNumber);
346     }
347 }
348
349
Popular Tags