NetworkInterface.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* NetworkInterface.java
  2. Copyright (C) 2002 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., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 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.Enumeration;
  33. import java.util.Vector;
  34. /**
  35. * @author Michael Koch <konqueror@gmx.de>
  36. * @since 1.4
  37. */
  38. public final class NetworkInterface
  39. {
  40. private static Vector networkInterfaces;
  41. private String name;
  42. private Vector inetAddresses;
  43. private NetworkInterface (String name, InetAddress address)
  44. {
  45. this.name = name;
  46. this.inetAddresses = new Vector (1, 1);
  47. this.inetAddresses.add (address);
  48. }
  49. private native static Vector getRealNetworkInterfaces ()
  50. throws SocketException;
  51. /**
  52. * Returns the name of the network interface
  53. */
  54. public String getName ()
  55. {
  56. return name;
  57. }
  58. /**
  59. * Returns all available addresses of the network interface
  60. *
  61. * If a @see SecurityManager is available all addresses are checked
  62. * with @see SecurityManager::checkConnect() if they are available.
  63. * Only InetAddresses are returned where the security manager doesn't
  64. * thrown an exception.
  65. *
  66. * @return An enumeration of all addresses.
  67. */
  68. public Enumeration getInetAddresses ()
  69. {
  70. SecurityManager s = System.getSecurityManager ();
  71. if (s == null)
  72. return inetAddresses.elements ();
  73. Vector tmpInetAddresses = new Vector (1, 1);
  74. for (Enumeration addresses = inetAddresses.elements ();
  75. addresses.hasMoreElements (); )
  76. {
  77. InetAddress addr = (InetAddress) addresses.nextElement ();
  78. try
  79. {
  80. s.checkConnect (addr.getHostAddress (), 58000);
  81. tmpInetAddresses.add (addr);
  82. }
  83. catch (SecurityException e)
  84. {
  85. }
  86. }
  87. return tmpInetAddresses.elements ();
  88. }
  89. /**
  90. * Returns the display name of the interface
  91. */
  92. public String getDisplayName ()
  93. {
  94. return name;
  95. }
  96. /**
  97. * Returns an network interface by name
  98. *
  99. * @param name The name of the interface to return
  100. *
  101. * @exception SocketException If an error occurs
  102. * @exception NullPointerException If the specified name is null
  103. */
  104. public static NetworkInterface getByName (String name)
  105. throws SocketException
  106. {
  107. if (networkInterfaces == null)
  108. networkInterfaces = getRealNetworkInterfaces ();
  109. for (Enumeration e = networkInterfaces.elements ();
  110. e.hasMoreElements (); )
  111. {
  112. NetworkInterface tmp = (NetworkInterface) e.nextElement ();
  113. if (name.equals (tmp.getName ()))
  114. return tmp;
  115. }
  116. throw new SocketException ("no network interface with this name exists");
  117. }
  118. /**
  119. * Return a network interface by its address
  120. *
  121. * @param addr The address of the interface to return
  122. *
  123. * @exception SocketException If an error occurs
  124. * @exception NullPointerException If the specified addess is null
  125. */
  126. public static NetworkInterface getByInetAddress (InetAddress addr)
  127. throws SocketException
  128. {
  129. if (networkInterfaces == null)
  130. networkInterfaces = getRealNetworkInterfaces ();
  131. for (Enumeration interfaces = networkInterfaces.elements ();
  132. interfaces.hasMoreElements (); )
  133. {
  134. NetworkInterface tmp = (NetworkInterface) interfaces.nextElement ();
  135. for (Enumeration addresses = tmp.inetAddresses.elements ();
  136. addresses.hasMoreElements (); )
  137. {
  138. if (addr.equals ((InetAddress) addresses.nextElement ()))
  139. return tmp;
  140. }
  141. }
  142. throw new SocketException (
  143. "no network interface is bound to such an IP address");
  144. }
  145. /**
  146. * Return an Enumeration of all available network interfaces
  147. *
  148. * @exception SocketException If an error occurs
  149. */
  150. public static Enumeration getNetworkInterfaces ()
  151. throws SocketException
  152. {
  153. if (networkInterfaces == null)
  154. networkInterfaces = getRealNetworkInterfaces ();
  155. Enumeration tmp = networkInterfaces.elements ();
  156. if (tmp.hasMoreElements ())
  157. return tmp;
  158. return null;
  159. }
  160. /**
  161. * Checks if the current instance is equal to obj
  162. *
  163. * @param obj The object to compare with
  164. */
  165. public boolean equals (Object obj)
  166. {
  167. if (!(obj instanceof NetworkInterface))
  168. return false;
  169. NetworkInterface tmp = (NetworkInterface) obj;
  170. return (name.equals (tmp.name)
  171. && inetAddresses.equals (tmp.inetAddresses));
  172. }
  173. /**
  174. * Returns the hashcode of the current instance
  175. */
  176. public int hashCode ()
  177. {
  178. // FIXME: hash correctly
  179. return name.hashCode () + inetAddresses.hashCode ();
  180. }
  181. /**
  182. * Returns a string representation of the interface
  183. */
  184. public String toString ()
  185. {
  186. // FIXME: check if this is correct
  187. String result;
  188. String separator = System.getProperty ("line.separator");
  189. result = "name: " + getDisplayName () + " (" + getName () +
  190. ") addresses:" + separator;
  191. for (Enumeration e = inetAddresses.elements ();
  192. e.hasMoreElements (); )
  193. {
  194. InetAddress address = (InetAddress) e.nextElement ();
  195. result += address.toString () + separator;
  196. }
  197. return result;
  198. }
  199. } // class NetworkInterface