NetworkInterface.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* NetworkInterface.java --
  2. Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.net;
  32. import java.util.Collection;
  33. import java.util.Collections;
  34. import java.util.Enumeration;
  35. import java.util.HashMap;
  36. import java.util.Iterator;
  37. import java.util.Map;
  38. import java.util.Vector;
  39. /**
  40. * This class models a network interface on the host computer. A network
  41. * interface contains a name (typically associated with a specific
  42. * hardware adapter) and a list of addresses that are bound to it.
  43. * For example, an ethernet interface may be named "eth0" and have the
  44. * address 192.168.1.101 assigned to it.
  45. *
  46. * @author Michael Koch (konqueror@gmx.de)
  47. * @since 1.4
  48. */
  49. public final class NetworkInterface
  50. {
  51. private String name;
  52. private Vector<InetAddress> inetAddresses;
  53. NetworkInterface(String name, InetAddress address)
  54. {
  55. this.name = name;
  56. this.inetAddresses = new Vector(1, 1);
  57. this.inetAddresses.add(address);
  58. }
  59. NetworkInterface(String name, InetAddress[] addresses)
  60. {
  61. this.name = name;
  62. this.inetAddresses = new Vector(addresses.length, 1);
  63. for (int i = 0; i < addresses.length; i++)
  64. this.inetAddresses.add(addresses[i]);
  65. }
  66. /**
  67. * Returns the name of the network interface
  68. *
  69. * @return The name of the interface.
  70. */
  71. public String getName()
  72. {
  73. return name;
  74. }
  75. /**
  76. * Returns all available addresses of the network interface
  77. *
  78. * If a @see SecurityManager is available all addresses are checked
  79. * with @see SecurityManager::checkConnect() if they are available.
  80. * Only <code>InetAddresses</code> are returned where the security manager
  81. * doesn't throw an exception.
  82. *
  83. * @return An enumeration of all addresses.
  84. */
  85. public Enumeration<InetAddress> getInetAddresses()
  86. {
  87. SecurityManager s = System.getSecurityManager();
  88. if (s == null)
  89. return inetAddresses.elements();
  90. Vector<InetAddress> tmpInetAddresses = new Vector<InetAddress>(1, 1);
  91. for (Enumeration<InetAddress> addresses = inetAddresses.elements();
  92. addresses.hasMoreElements();)
  93. {
  94. InetAddress addr = addresses.nextElement();
  95. try
  96. {
  97. s.checkConnect(addr.getHostAddress(), 58000);
  98. tmpInetAddresses.add(addr);
  99. }
  100. catch (SecurityException e)
  101. {
  102. // Ignore.
  103. }
  104. }
  105. return tmpInetAddresses.elements();
  106. }
  107. /**
  108. * Returns the display name of the interface
  109. *
  110. * @return The display name of the interface
  111. */
  112. public String getDisplayName()
  113. {
  114. return name;
  115. }
  116. /**
  117. * Returns an network interface by name
  118. *
  119. * @param name The name of the interface to return
  120. *
  121. * @return a <code>NetworkInterface</code> object representing the interface,
  122. * or null if there is no interface with that name.
  123. *
  124. * @exception SocketException If an error occurs
  125. * @exception NullPointerException If the specified name is null
  126. */
  127. public static NetworkInterface getByName(String name)
  128. throws SocketException
  129. {
  130. for (Enumeration e = getNetworkInterfaces(); e.hasMoreElements();)
  131. {
  132. NetworkInterface tmp = (NetworkInterface) e.nextElement();
  133. if (name.equals(tmp.getName()))
  134. return tmp;
  135. }
  136. // No interface with the given name found.
  137. return null;
  138. }
  139. /**
  140. * Return a network interface by its address
  141. *
  142. * @param addr The address of the interface to return
  143. *
  144. * @return the interface, or <code>null</code> if none found
  145. *
  146. * @exception SocketException If an error occurs
  147. * @exception NullPointerException If the specified addess is null
  148. */
  149. public static NetworkInterface getByInetAddress(InetAddress addr)
  150. throws SocketException
  151. {
  152. for (Enumeration interfaces = getNetworkInterfaces();
  153. interfaces.hasMoreElements();)
  154. {
  155. NetworkInterface tmp = (NetworkInterface) interfaces.nextElement();
  156. for (Enumeration addresses = tmp.inetAddresses.elements();
  157. addresses.hasMoreElements();)
  158. {
  159. if (addr.equals((InetAddress) addresses.nextElement()))
  160. return tmp;
  161. }
  162. }
  163. throw new SocketException("no network interface is bound to such an IP address");
  164. }
  165. static private Collection condense(Collection interfaces)
  166. {
  167. final Map condensed = new HashMap();
  168. final Iterator interfs = interfaces.iterator();
  169. while (interfs.hasNext()) {
  170. final NetworkInterface face = (NetworkInterface) interfs.next();
  171. final String name = face.getName();
  172. if (condensed.containsKey(name))
  173. {
  174. final NetworkInterface conface = (NetworkInterface) condensed.get(name);
  175. if (!conface.inetAddresses.containsAll(face.inetAddresses))
  176. {
  177. final Iterator faceAddresses = face.inetAddresses.iterator();
  178. while (faceAddresses.hasNext())
  179. {
  180. final InetAddress faceAddress = (InetAddress) faceAddresses.next();
  181. if (!conface.inetAddresses.contains(faceAddress))
  182. {
  183. conface.inetAddresses.add(faceAddress);
  184. }
  185. }
  186. }
  187. }
  188. else
  189. {
  190. condensed.put(name, face);
  191. }
  192. }
  193. return condensed.values();
  194. }
  195. /**
  196. * Return an <code>Enumeration</code> of all available network interfaces
  197. *
  198. * @return all interfaces
  199. *
  200. * @exception SocketException If an error occurs
  201. */
  202. public static Enumeration<NetworkInterface> getNetworkInterfaces()
  203. throws SocketException
  204. {
  205. Vector<NetworkInterface> networkInterfaces =
  206. VMNetworkInterface.getInterfaces();
  207. if (networkInterfaces.isEmpty())
  208. return null;
  209. Collection condensed = condense(networkInterfaces);
  210. return Collections.enumeration(condensed);
  211. }
  212. /**
  213. * Checks if the current instance is equal to obj
  214. *
  215. * @param obj The object to compare with
  216. *
  217. * @return <code>true</code> if equal, <code>false</code> otherwise
  218. */
  219. public boolean equals(Object obj)
  220. {
  221. if (! (obj instanceof NetworkInterface))
  222. return false;
  223. NetworkInterface tmp = (NetworkInterface) obj;
  224. return (name.equals(tmp.name) && inetAddresses.equals(tmp.inetAddresses));
  225. }
  226. /**
  227. * Returns the hashcode of the current instance
  228. *
  229. * @return the hashcode
  230. */
  231. public int hashCode()
  232. {
  233. // FIXME: hash correctly
  234. return name.hashCode() + inetAddresses.hashCode();
  235. }
  236. /**
  237. * Returns a string representation of the interface
  238. *
  239. * @return the string
  240. */
  241. public String toString()
  242. {
  243. // FIXME: check if this is correct
  244. String result;
  245. String separator = System.getProperty("line.separator");
  246. result =
  247. "name: " + getDisplayName() + " (" + getName() + ") addresses:"
  248. + separator;
  249. for (Enumeration e = inetAddresses.elements(); e.hasMoreElements();)
  250. {
  251. InetAddress address = (InetAddress) e.nextElement();
  252. result += address.toString() + ";" + separator;
  253. }
  254. return result;
  255. }
  256. }