NetworkInterface.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* NetworkInterface.java --
  2. Copyright (C) 2002, 2003, 2004, 2005, 2006, 2008 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 gnu.classpath.SystemProperties;
  33. import gnu.java.lang.CPStringBuilder;
  34. import java.util.Enumeration;
  35. import java.util.Iterator;
  36. import java.util.Vector;
  37. /**
  38. * This class models a network interface on the host computer. A network
  39. * interface contains a name (typically associated with a specific
  40. * hardware adapter) and a list of addresses that are bound to it.
  41. * For example, an ethernet interface may be named "eth0" and have the
  42. * address 192.168.1.101 assigned to it.
  43. *
  44. * @author Michael Koch (konqueror@gmx.de)
  45. * @since 1.4
  46. */
  47. public final class NetworkInterface
  48. {
  49. private final VMNetworkInterface netif;
  50. private NetworkInterface(VMNetworkInterface netif)
  51. {
  52. this.netif = netif;
  53. }
  54. /** Creates an NetworkInterface instance which
  55. * represents any interface in the system. Its only
  56. * address is <code>0.0.0.0/0.0.0.0</code>. This
  57. * method is needed by {@link MulticastSocket#getNetworkInterface}
  58. */
  59. static NetworkInterface createAnyInterface()
  60. {
  61. return new NetworkInterface(new VMNetworkInterface());
  62. }
  63. /**
  64. * Returns the name of the network interface
  65. *
  66. * @return The name of the interface.
  67. */
  68. public String getName()
  69. {
  70. return netif.name;
  71. }
  72. /**
  73. * Returns all available addresses of the network interface
  74. *
  75. * If a @see SecurityManager is available all addresses are checked
  76. * with @see SecurityManager::checkConnect() if they are available.
  77. * Only <code>InetAddresses</code> are returned where the security manager
  78. * doesn't throw an exception.
  79. *
  80. * @return An enumeration of all addresses.
  81. */
  82. public Enumeration<InetAddress> getInetAddresses()
  83. {
  84. SecurityManager s = System.getSecurityManager();
  85. Vector<InetAddress> inetAddresses
  86. = new Vector<InetAddress>(netif.addresses);
  87. if (s == null)
  88. return inetAddresses.elements();
  89. Vector<InetAddress> tmpInetAddresses = new Vector<InetAddress>(1, 1);
  90. for (Enumeration<InetAddress> addresses = inetAddresses.elements();
  91. addresses.hasMoreElements();)
  92. {
  93. InetAddress addr = addresses.nextElement();
  94. try
  95. {
  96. s.checkConnect(addr.getHostAddress(), -1);
  97. tmpInetAddresses.add(addr);
  98. }
  99. catch (SecurityException e)
  100. {
  101. // Ignore.
  102. }
  103. }
  104. return tmpInetAddresses.elements();
  105. }
  106. /**
  107. * Returns the display name of the interface
  108. *
  109. * @return The display name of the interface
  110. */
  111. public String getDisplayName()
  112. {
  113. return netif.name;
  114. }
  115. /**
  116. * Returns an network interface by name
  117. *
  118. * @param name The name of the interface to return
  119. *
  120. * @return a <code>NetworkInterface</code> object representing the interface,
  121. * or null if there is no interface with that name.
  122. *
  123. * @exception SocketException If an error occurs
  124. * @exception NullPointerException If the specified name is null
  125. */
  126. public static NetworkInterface getByName(String name)
  127. throws SocketException
  128. {
  129. if (name == null)
  130. throw new NullPointerException();
  131. VMNetworkInterface[] netifs = VMNetworkInterface.getVMInterfaces();
  132. for (int i = 0; i < netifs.length; i++)
  133. {
  134. if (netifs[i].name.equals(name))
  135. return new NetworkInterface(netifs[i]);
  136. }
  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. if (addr == null)
  153. throw new NullPointerException();
  154. VMNetworkInterface[] netifs = VMNetworkInterface.getVMInterfaces();
  155. for (int i = 0; i < netifs.length; i++)
  156. {
  157. if (netifs[i].addresses.contains(addr))
  158. return new NetworkInterface(netifs[i]);
  159. }
  160. return null;
  161. }
  162. /**
  163. * Return an <code>Enumeration</code> of all available network interfaces
  164. *
  165. * @return all interfaces
  166. *
  167. * @exception SocketException If an error occurs
  168. */
  169. public static Enumeration<NetworkInterface> getNetworkInterfaces()
  170. throws SocketException
  171. {
  172. VMNetworkInterface[] netifs = VMNetworkInterface.getVMInterfaces();
  173. Vector<NetworkInterface> networkInterfaces =
  174. new Vector<NetworkInterface>(netifs.length);
  175. for (int i = 0; i < netifs.length; i++)
  176. {
  177. if (!netifs[i].addresses.isEmpty())
  178. networkInterfaces.add(new NetworkInterface(netifs[i]));
  179. }
  180. return networkInterfaces.elements();
  181. }
  182. /**
  183. * Checks if the current instance is equal to obj
  184. *
  185. * @param obj The object to compare with
  186. *
  187. * @return <code>true</code> if equal, <code>false</code> otherwise
  188. */
  189. public boolean equals(Object obj)
  190. {
  191. if (! (obj instanceof NetworkInterface))
  192. return false;
  193. NetworkInterface tmp = (NetworkInterface) obj;
  194. if (netif.name == null)
  195. return tmp.netif.name == null;
  196. return (netif.name.equals(tmp.netif.name)
  197. && (netif.addresses.equals(tmp.netif.addresses)));
  198. }
  199. /**
  200. * Returns the hashcode of the current instance
  201. *
  202. * @return the hashcode
  203. */
  204. public int hashCode()
  205. {
  206. // FIXME: hash correctly
  207. int hc = netif.addresses.hashCode();
  208. if (netif.name != null)
  209. hc += netif.name.hashCode();
  210. return hc;
  211. }
  212. /**
  213. * Returns a string representation of the interface
  214. *
  215. * @return the string
  216. */
  217. public String toString()
  218. {
  219. // FIXME: check if this is correct
  220. CPStringBuilder result;
  221. String separator = SystemProperties.getProperty("line.separator");
  222. result = new CPStringBuilder();
  223. result.append("name: ");
  224. result.append(getDisplayName());
  225. result.append(" (").append(getName()).append(") addresses:");
  226. result.append(separator);
  227. for (Iterator it = netif.addresses.iterator(); it.hasNext(); )
  228. {
  229. InetAddress address = (InetAddress) it.next();
  230. result.append(address.toString()).append(";").append(separator);
  231. }
  232. return result.toString();
  233. }
  234. /**
  235. * Determines whether this interface is ready to transfer data.
  236. *
  237. * @return whether the interface is up
  238. */
  239. public boolean isUp()
  240. throws SocketException
  241. {
  242. return VMNetworkInterface.isUp(netif.name);
  243. }
  244. /**
  245. * Determines whether this interface does point to point
  246. * transmission.
  247. *
  248. * @return whether the interface does point to point transmission
  249. */
  250. public boolean isPointToPoint()
  251. throws SocketException
  252. {
  253. return VMNetworkInterface.isPointToPoint(netif.name);
  254. }
  255. /**
  256. * Determines whether this interface is the loopback interface.
  257. *
  258. * @return whether the interface is the loopback interface
  259. */
  260. public boolean isLoopback()
  261. throws SocketException
  262. {
  263. return VMNetworkInterface.isLoopback(netif.name);
  264. }
  265. /**
  266. * Determines whether this interface supports multicast transmission.
  267. *
  268. * @return whether the interface supports multicast transmission.
  269. */
  270. public boolean supportsMulticast()
  271. throws SocketException
  272. {
  273. return VMNetworkInterface.supportsMulticast(netif.name);
  274. }
  275. }