1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49:
59: public final class NetworkInterface
60: {
61: private String name;
62: private Vector<InetAddress> inetAddresses;
63:
64: NetworkInterface(String name, InetAddress address)
65: {
66: this.name = name;
67: this.inetAddresses = new Vector(1, 1);
68: this.inetAddresses.add(address);
69: }
70:
71: NetworkInterface(String name, InetAddress[] addresses)
72: {
73: this.name = name;
74: this.inetAddresses = new Vector(addresses.length, 1);
75:
76: for (int i = 0; i < addresses.length; i++)
77: this.inetAddresses.add(addresses[i]);
78: }
79:
80:
85: public String getName()
86: {
87: return name;
88: }
89:
90:
100: public Enumeration<InetAddress> getInetAddresses()
101: {
102: SecurityManager s = System.getSecurityManager();
103:
104: if (s == null)
105: return inetAddresses.elements();
106:
107: Vector<InetAddress> tmpInetAddresses = new Vector<InetAddress>(1, 1);
108:
109: for (Enumeration<InetAddress> addresses = inetAddresses.elements();
110: addresses.hasMoreElements();)
111: {
112: InetAddress addr = addresses.nextElement();
113: try
114: {
115: s.checkConnect(addr.getHostAddress(), 58000);
116: tmpInetAddresses.add(addr);
117: }
118: catch (SecurityException e)
119: {
120:
121: }
122: }
123:
124: return tmpInetAddresses.elements();
125: }
126:
127:
132: public String getDisplayName()
133: {
134: return name;
135: }
136:
137:
148: public static NetworkInterface getByName(String name)
149: throws SocketException
150: {
151: for (Enumeration e = getNetworkInterfaces(); e.hasMoreElements();)
152: {
153: NetworkInterface tmp = (NetworkInterface) e.nextElement();
154:
155: if (name.equals(tmp.getName()))
156: return tmp;
157: }
158:
159:
160: return null;
161: }
162:
163:
173: public static NetworkInterface getByInetAddress(InetAddress addr)
174: throws SocketException
175: {
176: for (Enumeration interfaces = getNetworkInterfaces();
177: interfaces.hasMoreElements();)
178: {
179: NetworkInterface tmp = (NetworkInterface) interfaces.nextElement();
180:
181: for (Enumeration addresses = tmp.inetAddresses.elements();
182: addresses.hasMoreElements();)
183: {
184: if (addr.equals((InetAddress) addresses.nextElement()))
185: return tmp;
186: }
187: }
188:
189: throw new SocketException("no network interface is bound to such an IP address");
190: }
191:
192: static private Collection condense(Collection interfaces)
193: {
194: final Map condensed = new HashMap();
195:
196: final Iterator interfs = interfaces.iterator();
197: while (interfs.hasNext()) {
198:
199: final NetworkInterface face = (NetworkInterface) interfs.next();
200: final String name = face.getName();
201:
202: if (condensed.containsKey(name))
203: {
204: final NetworkInterface conface = (NetworkInterface) condensed.get(name);
205: if (!conface.inetAddresses.containsAll(face.inetAddresses))
206: {
207: final Iterator faceAddresses = face.inetAddresses.iterator();
208: while (faceAddresses.hasNext())
209: {
210: final InetAddress faceAddress = (InetAddress) faceAddresses.next();
211: if (!conface.inetAddresses.contains(faceAddress))
212: {
213: conface.inetAddresses.add(faceAddress);
214: }
215: }
216: }
217: }
218: else
219: {
220: condensed.put(name, face);
221: }
222: }
223:
224: return condensed.values();
225: }
226:
227:
234: public static Enumeration<NetworkInterface> getNetworkInterfaces()
235: throws SocketException
236: {
237: Vector<NetworkInterface> networkInterfaces =
238: VMNetworkInterface.getInterfaces();
239:
240: if (networkInterfaces.isEmpty())
241: return null;
242:
243: Collection condensed = condense(networkInterfaces);
244:
245: return Collections.enumeration(condensed);
246: }
247:
248:
255: public boolean equals(Object obj)
256: {
257: if (! (obj instanceof NetworkInterface))
258: return false;
259:
260: NetworkInterface tmp = (NetworkInterface) obj;
261:
262: return (name.equals(tmp.name) && inetAddresses.equals(tmp.inetAddresses));
263: }
264:
265:
270: public int hashCode()
271: {
272:
273: return name.hashCode() + inetAddresses.hashCode();
274: }
275:
276:
281: public String toString()
282: {
283:
284: String result;
285: String separator = System.getProperty("line.separator");
286:
287: result =
288: "name: " + getDisplayName() + " (" + getName() + ") addresses:"
289: + separator;
290:
291: for (Enumeration e = inetAddresses.elements(); e.hasMoreElements();)
292: {
293: InetAddress address = (InetAddress) e.nextElement();
294: result += address.toString() + ";" + separator;
295: }
296:
297: return result;
298: }
299: }