001: /*
002: * $Id: IPAddressUtils.java,v 1.38 2007/09/11 12:39:57 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.common.net;
008:
009: import java.net.InetAddress;
010: import java.net.UnknownHostException;
011: import java.util.NoSuchElementException;
012: import java.util.StringTokenizer;
013:
014: import org.xins.common.MandatoryArgumentChecker;
015: import org.xins.common.Utils;
016: import org.xins.common.text.ParseException;
017:
018: /**
019: * IP address-related utility functions.
020: *
021: * @version $Revision: 1.38 $ $Date: 2007/09/11 12:39:57 $
022: * @author <a href="mailto:ernst@ernstdehaan.com">Ernst de Haan</a>
023: *
024: * @since XINS 1.0.0
025: */
026: public final class IPAddressUtils {
027:
028: /**
029: * Constructs a new <code>IPAddressUtils</code> object.
030: */
031: private IPAddressUtils() {
032: // empty
033: }
034:
035: /**
036: * Converts an IP address in the form <em>a.b.c.d</em> to an
037: * <code>int</code>.
038: *
039: * @param ip
040: * the IP address, must be in the form:
041: * <em>a.a.a.a.</em>, where <em>a</em> is a number between 0 and 255,
042: * with no leading zeroes; cannot be <code>null</code>.
043: *
044: * @return
045: * the IP address as an <code>int</code>.
046: *
047: * @throws IllegalArgumentException
048: * if <code>ip == null</code>.
049: *
050: * @throws ParseException
051: * if <code>ip</code> cannot be parsed as an IP address.
052: */
053: public static int ipToInt(String ip)
054: throws IllegalArgumentException, ParseException {
055: MandatoryArgumentChecker.check("ip", ip);
056:
057: int value;
058:
059: // Tokenize the string
060: StringTokenizer tokenizer = new StringTokenizer(ip, ".", false);
061:
062: try {
063:
064: // Token 1 must be an IP address part
065: value = ipPartToInt(ip, tokenizer.nextToken());
066:
067: // Token 3 must be an IP address part
068: value <<= 8;
069: value += ipPartToInt(ip, tokenizer.nextToken());
070:
071: // Token 5 must be an IP address part
072: value <<= 8;
073: value += ipPartToInt(ip, tokenizer.nextToken());
074:
075: // Token 7 must be an IP address part
076: value <<= 8;
077: value += ipPartToInt(ip, tokenizer.nextToken());
078:
079: } catch (NoSuchElementException nsee) {
080: throw newParseException(ip);
081: }
082: if (tokenizer.hasMoreTokens()) {
083: throw newParseException(ip);
084: }
085:
086: return value;
087: }
088:
089: /**
090: * Converts the specified component of an IP address to a number between 0
091: * and 255.
092: *
093: * @param ip
094: * the complete IP address, needed when throwing a
095: * {@link ParseException}, should not be <code>null</code>; if it is,
096: * then the behaviour is undefined.
097: *
098: * @param part
099: * the part to convert to an <code>int</code> number, should not be
100: * <code>null</code>; if it is, then the behaviour is undefined.
101: *
102: * @return
103: * the <code>int</code> value of the part, between 0 and 255
104: * (inclusive).
105: *
106: * @throws ParseException
107: * if the part cannot be parsed.
108: */
109: private static int ipPartToInt(String ip, String part)
110: throws ParseException {
111:
112: char[] partString = part.toCharArray();
113: int length = partString.length;
114:
115: if (length == 1) {
116: char c0 = partString[0];
117: if (c0 >= '0' && c0 <= '9') {
118: return c0 - '0';
119: }
120:
121: } else if (length == 2) {
122: char c0 = partString[0];
123: char c1 = partString[1];
124:
125: if (c0 >= '1' && c0 <= '9' && c1 >= '0' && c1 <= '9') {
126: return ((c0 - '0') * 10) + (c1 - '0');
127: }
128:
129: } else if (length == 3) {
130: char c0 = partString[0];
131: char c1 = partString[1];
132: char c2 = partString[2];
133:
134: if (c0 >= '1' && c0 <= '2' && c1 >= '0' && c1 <= '9'
135: && c2 >= '0' && c2 <= '9') {
136:
137: int value = ((c0 - '0') * 100) + ((c1 - '0') * 10)
138: + (c2 - '0');
139: if (value <= 255) {
140: return value;
141: }
142: }
143: }
144:
145: throw newParseException(ip);
146: }
147:
148: /**
149: * Retrieves the localhost IP address.
150: *
151: * @return
152: * if possible the IP address for localhost, otherwise
153: * the string <code>"127.0.0.1"</code>.
154: *
155: * @since XINS 1.3.0
156: */
157: public static String getLocalHostIPAddress() {
158: try {
159: return InetAddress.getLocalHost().getHostAddress();
160: } catch (UnknownHostException exception) {
161: return "127.0.0.1";
162: }
163: }
164:
165: /**
166: * Retrieves the localhost host name. This method applies several
167: * techniques to attempt to retrieve the localhost host name.
168: *
169: * @return
170: * if possible the fully qualified host name for localhost, otherwise if
171: * possible the non-qualified host name for the localhost, otherwise
172: * the string <code>"localhost"</code>.
173: */
174: public static String getLocalHost() {
175: if (Utils.getJavaVersion() < 1.4) {
176: return getLocalHostJava13();
177: } else {
178: return getLocalHostJava14();
179: }
180: }
181:
182: /**
183: * Retrieves the localhost host name, on Java 1.3.
184: *
185: * @return
186: * if possible the fully qualified host name for localhost, otherwise if
187: * possible the non-qualified host name for the localhost, otherwise
188: * the string <code>"localhost"</code>.
189: */
190: private static String getLocalHostJava13() {
191: try {
192: return InetAddress.getLocalHost().getHostName();
193: } catch (UnknownHostException exception) {
194: return "localhost";
195: }
196: }
197:
198: /**
199: * Retrieves the localhost host name, on Java 1.4 and up. This method
200: * applies several techniques to attempt to retrieve the localhost host
201: * name.
202: *
203: * @return
204: * if possible the fully qualified host name for localhost, otherwise if
205: * possible the non-qualified host name for the localhost, otherwise
206: * the string <code>"localhost"</code>.
207: */
208: private static String getLocalHostJava14() {
209:
210: String hostname = "localhost";
211:
212: try {
213: hostname = InetAddress.getLocalHost()
214: .getCanonicalHostName();
215: } catch (UnknownHostException unknownHostException) {
216: String unknownMessage = unknownHostException.getMessage();
217: int twoDotPos = unknownMessage.indexOf(':');
218: if (twoDotPos != -1) {
219: hostname = unknownMessage.substring(0, twoDotPos);
220: }
221: } catch (SecurityException securityException) {
222: // fall through
223: }
224:
225: return hostname;
226: }
227:
228: /**
229: * Constructs a new <code>ParseException</code> for the specified malformed
230: * IP address.
231: *
232: * @param ip
233: * the malformed IP address, not <code>null</code>.
234: *
235: * @return
236: * the {@link ParseException} to throw.
237: */
238: private static ParseException newParseException(String ip) {
239:
240: // Construct the message for the exception
241: String detail = "The string \"" + ip
242: + "\" is not a valid IP address.";
243:
244: // Return the exception
245: return new ParseException(detail);
246: }
247: }
|