Source code: com/flexstor/common/util/InetUtil.java
1 /*
2 * InetUtil.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:30 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.util;
12
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15
16 /**
17 * InetUtil returns information for a specific server.
18 * Given a server name, this class can determine all IP Addresses of the server
19 * and also compares if this server has other names.
20 */
21 public class InetUtil
22 {
23 InetAddress host;
24 InetAddress[] allAddresses;
25
26 /**
27 * With no arguments, this constructor will load information
28 * about the local host.
29 * @exception UnknownHostException if no IP address for the
30 * host could be found.
31 */
32 public InetUtil()
33 throws UnknownHostException
34 {
35 host = InetAddress.getLocalHost();
36 }
37
38 /**
39 * Loads information about the specified host.
40 * The host name can either be a DNS or IP Address.
41 * @param host the name of the host
42 * @exception UnknownHostException if no IP address for the
43 * host could be found.
44 */
45 public InetUtil( String sHost )
46 throws UnknownHostException
47 {
48 host = InetAddress.getByName( sHost );
49 }
50
51 /**
52 * Determines if this object holds an instance of the local host
53 */
54 public boolean isLocalHost()
55 {
56 try
57 {
58 return isLocalHost( host );
59 }
60 catch ( UnknownHostException uhe )
61 {
62 return false;
63 }
64 }
65
66 /**
67 * Determines if the argument string represents the host name of the local host
68 */
69 public static boolean isLocalHost( String sHost )
70 throws UnknownHostException
71 {
72 return isLocalHost( InetAddress.getByName( sHost ) );
73 }
74
75 /**
76 * Determines if the argument InetAddress represents the local host
77 */
78 public static boolean isLocalHost( InetAddress aHost )
79 throws UnknownHostException
80 {
81 //System.out.println( "Host is: " + aHost.getHostName() + " (" + aHost.getHostAddress() + ")" );
82 InetAddress[] localAddresses = InetAddress.getAllByName( InetAddress.getLocalHost().getHostAddress() );
83 if ( localAddresses != null && localAddresses.length > 0 )
84 {
85 for ( int i = 0; i < localAddresses.length; i++ )
86 {
87 /*if ( i == 0 )
88 System.out.println( "Local Host is: " + localAddresses[i].getHostName() + " (" + localAddresses[i].getHostAddress() + ")" );
89 else
90 System.out.println( " a.k.a.: " + localAddresses[i].getHostName() + " (" + localAddresses[i].getHostAddress() + ")" );
91 */
92 if ( aHost.equals(localAddresses[i]) )
93 return true;
94 }
95 }
96 return false;
97 }
98
99 /*
100 * Determines if the argument string represents one of the several possible names for
101 * this host.
102 */
103 public boolean isThisHost( String sHost )
104 throws UnknownHostException
105 {
106 return isThisHost( InetAddress.getByName( sHost ) );
107 }
108
109 /*
110 * Determines if the argument InetAddress represents one of the several possible addresses for
111 * this host.
112 */
113 public boolean isThisHost( InetAddress aHost )
114 throws UnknownHostException
115 {
116 if ( allAddresses == null )
117 allAddresses = InetAddress.getAllByName( host.getHostAddress() );
118
119 if ( allAddresses != null && allAddresses.length > 0 )
120 {
121 for ( int i = 0; i < allAddresses.length; i++ )
122 {
123 if ( aHost.equals(allAddresses[i]) )
124 return true;
125 }
126 }
127 return false;
128 }
129
130 /*
131 * Returns the name of the local host. If the name cannot be determined,
132 * this method returns the string "localhost".
133 */
134 public static String getLocalHostName()
135 {
136 try
137 {
138 String localHost = InetAddress.getLocalHost().getHostName();
139 //System.out.println( "Local host name is: " + localHost );
140 return localHost;
141 }
142 catch ( UnknownHostException uhe )
143 {
144 //System.out.println( "Local host name is: localhost" );
145 return "localhost";
146 }
147 }
148
149 /**
150 * Returns the IP Address for this local machine, or null if the
151 * address cannot be determined.
152 * @return the IP Address
153 */
154 public static String getIPAddress()
155 {
156 String sIP = null;
157
158 try
159 {
160 sIP = InetAddress.getLocalHost().getHostAddress();
161 }
162 catch ( Exception e ) { }
163
164 if ( sIP == null || sIP.equals("") || sIP.equals("127.0.0.1") )
165 return null;
166
167 return sIP;
168 }
169
170
171 /**
172 * Returns the host name for this local machine, or null if the
173 * name cannot be determined.
174 * @return the host name
175 */
176 public static String getHostName()
177 {
178 String sName = null;
179
180 try
181 {
182 sName = InetAddress.getLocalHost().getHostName();
183 sName = sName.toLowerCase();
184 }
185 catch ( Exception e ) { }
186
187 if ( sName == null || sName.equals("") || sName.equals("localhost") )
188 return null;
189
190 return sName;
191 }
192 }