KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > net > InetAddressUtil


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: InetAddressUtil.java 43095 2004-06-28 08:52:33Z andreas $ */
19
20 package org.apache.lenya.net;
21
22 import java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24
25 import org.apache.log4j.Category;
26
27 /**
28  * A utility class for InetAddress. Also see http://jodies.de/ipcalc
29  */

30 public class InetAddressUtil {
31
32     private static final Category log = Category.getInstance(InetAddressUtil.class);
33
34     /**
35      * Ctor.
36      */

37     private InetAddressUtil() {
38     }
39
40     /**
41      * Checks if a subnet contains a specific IP address.
42      *
43      * @param network The network address.
44      * @param netmask The subnet mask.
45      * @param ip The IP address to check.
46      * @return A boolean value.
47      */

48     public static boolean contains(InetAddress JavaDoc network, InetAddress JavaDoc netmask, InetAddress JavaDoc ip) {
49         if(log.isDebugEnabled()) {
50             log.debug("=======================================");
51             log.debug("Checking IP address: " + ip + " in " + network + " / " + netmask);
52         }
53         
54         byte[] networkBytes = network.getAddress();
55         byte[] netmaskBytes = netmask.getAddress();
56         byte[] ipBytes = ip.getAddress();
57         
58         /* check IPv4/v6-compatibility or parameters: */
59         if(networkBytes.length != netmaskBytes.length
60             || netmaskBytes.length != ipBytes.length)
61         {
62             /*
63              * FIXME: If network and netmask have the same size
64              * should already be checked whenever
65              * org.apache.lenya.ac.(impl.Abstract)IPRange
66              * is set. In that case the user should be notified
67              * of this configuration-error instead of silently
68              * accepting the buggy IPRange as one not matching
69              * any host!
70              * (Note that changes to the public API of IPRange
71              * and other classes would be necessary to fix this
72              * problem. This method and therefore this whole
73              * class would probably be obsolete in that case.)
74              */

75             if(log.isDebugEnabled()) {
76                 log.debug
77                     ("Network address " + network + ", subnet mask "
78                      + netmask + " and/or host address " + ip
79                      + " have different sizes! (return false ...)");
80                 log.debug("=======================================");
81             }
82             return false;
83         }
84         
85         /* Check if the masked network and ip addresses match: */
86         for(int i=0; i<netmaskBytes.length; i++) {
87             int mask = netmaskBytes[i] & 0xff;
88             if((networkBytes[i] & mask) != (ipBytes[i] & mask)) {
89                 if(log.isDebugEnabled()) {
90                     log.debug
91                         (ip + " is not in " + network + " / " + netmask);
92                     log.debug("=======================================");
93                 }
94                 return false;
95             }
96         }
97         if(log.isDebugEnabled()) {
98             log.debug
99                 (ip + " is in " + network + " / " + netmask);
100             log.debug("=======================================");
101         }
102         return true;
103     }
104
105     /**
106      * Returns the n-th part of an InetAddress.
107      * @param ip The address.
108      * @param partNumber The number of the part.
109      * @return An integer value.
110      *
111      * @deprecated This was an internal implementation detail of the
112      * method {@link #contains} and should never have been
113      * made public. (And it's inefficient and unnecessary
114      * too, as well as broken for IPv6. ;-)
115      * Use <code>ip.getAddress()[partNumber]</code>
116      * instead.
117      */

118     public static int getClassPart(InetAddress JavaDoc ip, int partNumber) {
119         String JavaDoc[] parts = ip.getHostAddress().split("\\.");
120         String JavaDoc part = parts[partNumber];
121         return new Integer JavaDoc(part).intValue();
122     }
123
124     /**
125      * Check netmask, e.g. 255.255.255.240 is fine, 255.255.240.16 is illegal (needs to be 255.255.240.0)
126      * @param netmask The netmask address.
127      * @return An integer value. -1 if illegal netmask, otherwise 0, 1, 2, 3
128      *
129      * @deprecated This was an internal implementation detail of the
130      * method {@link #contains} and should never have been
131      * made public. Furthermore it's broken for IPv6.
132      * (However, there is no real replacement. If you
133      * need this functionality, you should rewrite it
134      * yourself.)
135      */

136     public static int checkNetmask(InetAddress JavaDoc netmask) {
137         String JavaDoc[] parts = netmask.getHostAddress().split("\\.");
138         Integer JavaDoc[] numbers = new Integer JavaDoc[4];
139         for (int i = 0; i < 4; i++) {
140             numbers[i] = new Integer JavaDoc(parts[i]);
141         }
142
143         for (int i = 0; i < 4; i++) {
144             log.debug(".checkNetmask(): Check part: " + numbers[i]);
145             if (0 <= numbers[i].intValue() && numbers[i].intValue() <= 255) {
146                 if (numbers[i].intValue() != 255) {
147                     for (int k = i + 1; k < 4; k++) {
148                         if (numbers[k].intValue() != 0) {
149                             log.error(".checkNetmask(): Illegal Netmask: " + netmask);
150                             return -1;
151                         }
152                     }
153                     return i;
154                 } else {
155                     continue;
156                 }
157             } else {
158                 // FIXME: This check not really be necessary because java.net.UnknownHostException should be thrown long time before
159
log.error(".checkNetmask(): Illegal Netmask: " + netmask);
160                 return -1;
161             }
162         }
163         if (log.isDebugEnabled()) {
164             log.debug("All parts equal 255: " + netmask);
165         }
166         return 3;
167     }
168
169     /**
170      * Converts a string to an IP addres.
171      * @param string The IP address, represented by a string.
172      * @return An InetAddress object.
173      * @throws AccessControlException when something went wrong.
174      *
175      * @deprecated This was an internal implementation detail of the
176      * method {@link #contains} and should never have been
177      * made public. (And it's unnecessary
178      * too, as well as broken for IPv6. ;-)
179      * Use <code>InetAddress.getByName(string)</code>
180      * instead.
181      */

182     public static InetAddress JavaDoc getAddress(String JavaDoc string) throws UnknownHostException JavaDoc {
183         String JavaDoc[] strings = string.split("\\.");
184
185         InetAddress JavaDoc address;
186         byte[] numbers = new byte[strings.length];
187         for (int i = 0; i < strings.length; i++) {
188             int number = Integer.parseInt(strings[i]);
189             if (number > 127) {
190                 number = number - 256;
191             }
192             numbers[i] = (byte) number;
193         }
194
195         address = InetAddress.getByAddress(numbers);
196         return address;
197     }
198
199 }
200
Popular Tags