PageRenderTime 226ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/libjava/classpath/vm/reference/gnu/java/net/VMPlainSocketImpl.java

https://bitbucket.org/pizzafactory/pf-gcc
Java | 511 lines | 312 code | 58 blank | 141 comment | 37 complexity | b8a1c446ee9a3a87ababa09fe9bce660 MD5 | raw file
  1. /* VMPlainSocketImpl.java -- VM interface for default socket implementation
  2. Copyright (C) 2005, 2006 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 gnu.java.net;
  32. import java.io.IOException;
  33. import java.net.Inet4Address;
  34. import java.net.Inet6Address;
  35. import java.net.InetAddress;
  36. import java.net.InetSocketAddress;
  37. import java.net.NetworkInterface;
  38. import java.net.SocketException;
  39. import java.net.SocketOptions;
  40. import gnu.classpath.Configuration;
  41. import gnu.java.nio.VMChannel;
  42. /**
  43. * The VM interface for {@link gnu.java.net.PlainSocketImpl}.
  44. *
  45. * @author Ingo Proetel (proetel@aicas.com)
  46. * @author Roman Kennke (kennke@aicas.com)
  47. */
  48. public final class VMPlainSocketImpl
  49. {
  50. /** Option id for time to live
  51. */
  52. private static final int CP_IP_TTL = 0x1E61;
  53. private final State nfd;
  54. /**
  55. * Static initializer to load native library.
  56. */
  57. static
  58. {
  59. if (Configuration.INIT_LOAD_LIBRARY)
  60. {
  61. System.loadLibrary("javanet");
  62. }
  63. }
  64. public VMPlainSocketImpl()
  65. {
  66. // XXX consider adding security check here.
  67. nfd = new State();
  68. }
  69. public VMPlainSocketImpl(VMChannel channel) throws IOException
  70. {
  71. this();
  72. nfd.setChannelFD(channel.getState());
  73. }
  74. public State getState()
  75. {
  76. return nfd;
  77. }
  78. /** This method exists to hide the CP_IP_TTL value from
  79. * higher levels.
  80. *
  81. * Always think of JNode ... :)
  82. */
  83. public void setTimeToLive(int ttl)
  84. throws SocketException
  85. {
  86. try
  87. {
  88. setOption(nfd.getNativeFD(), CP_IP_TTL, ttl);
  89. }
  90. catch (IOException ioe)
  91. {
  92. SocketException se = new SocketException();
  93. se.initCause(ioe);
  94. throw se;
  95. }
  96. }
  97. public int getTimeToLive()
  98. throws SocketException
  99. {
  100. try
  101. {
  102. return getOption(nfd.getNativeFD(), CP_IP_TTL);
  103. }
  104. catch (IOException ioe)
  105. {
  106. SocketException se = new SocketException();
  107. se.initCause(ioe);
  108. throw se;
  109. }
  110. }
  111. public void setOption(int optionId, Object optionValue)
  112. throws SocketException
  113. {
  114. int value;
  115. if (optionValue instanceof Integer)
  116. value = ((Integer) optionValue).intValue();
  117. else if (optionValue instanceof Boolean)
  118. // Switching off the linger behavior is done by setting
  119. // the value to -1. This is how the Java API interprets
  120. // it.
  121. value = ((Boolean) optionValue).booleanValue()
  122. ? 1
  123. : (optionId == SocketOptions.SO_LINGER)
  124. ? -1
  125. : 0;
  126. else
  127. throw new IllegalArgumentException("option value type "
  128. + optionValue.getClass().getName());
  129. try
  130. {
  131. setOption(nfd.getNativeFD(), optionId, value);
  132. }
  133. catch (IOException ioe)
  134. {
  135. SocketException se = new SocketException();
  136. se.initCause(ioe);
  137. throw se;
  138. }
  139. }
  140. private static native void setOption(int fd, int id, int value)
  141. throws SocketException;
  142. public void setMulticastInterface(int optionId, InetAddress addr)
  143. throws SocketException
  144. {
  145. try
  146. {
  147. if (addr instanceof Inet4Address)
  148. setMulticastInterface(nfd.getNativeFD(), optionId, (Inet4Address) addr);
  149. else if (addr instanceof Inet6Address)
  150. {
  151. NetworkInterface iface = NetworkInterface.getByInetAddress(addr);
  152. setMulticastInterface6(nfd.getNativeFD(), optionId, iface.getName());
  153. }
  154. else
  155. throw new SocketException("Unknown address format: " + addr);
  156. }
  157. catch (SocketException se)
  158. {
  159. throw se;
  160. }
  161. catch (IOException ioe)
  162. {
  163. SocketException se = new SocketException();
  164. se.initCause(ioe);
  165. throw se;
  166. }
  167. }
  168. private static native void setMulticastInterface(int fd,
  169. int optionId,
  170. Inet4Address addr);
  171. private static native void setMulticastInterface6(int fd,
  172. int optionId,
  173. String ifName);
  174. /**
  175. * Get a socket option. This implementation is only required to support
  176. * socket options that are boolean values, which include:
  177. *
  178. * SocketOptions.IP_MULTICAST_LOOP
  179. * SocketOptions.SO_BROADCAST
  180. * SocketOptions.SO_KEEPALIVE
  181. * SocketOptions.SO_OOBINLINE
  182. * SocketOptions.SO_REUSEADDR
  183. * SocketOptions.TCP_NODELAY
  184. *
  185. * and socket options that are integer values, which include:
  186. *
  187. * SocketOptions.IP_TOS
  188. * SocketOptions.SO_LINGER
  189. * SocketOptions.SO_RCVBUF
  190. * SocketOptions.SO_SNDBUF
  191. * SocketOptions.SO_TIMEOUT
  192. *
  193. * @param optionId The option ID to fetch.
  194. * @return A {@link Boolean} or {@link Integer} containing the socket
  195. * option.
  196. * @throws SocketException
  197. */
  198. public Object getOption(int optionId) throws SocketException
  199. {
  200. int value;
  201. try
  202. {
  203. value = getOption(nfd.getNativeFD(), optionId);
  204. }
  205. catch (IOException ioe)
  206. {
  207. SocketException se = new SocketException();
  208. se.initCause(ioe);
  209. throw se;
  210. }
  211. switch (optionId)
  212. {
  213. case SocketOptions.IP_MULTICAST_LOOP:
  214. case SocketOptions.SO_BROADCAST:
  215. case SocketOptions.SO_KEEPALIVE:
  216. case SocketOptions.SO_OOBINLINE:
  217. case SocketOptions.SO_REUSEADDR:
  218. case SocketOptions.TCP_NODELAY:
  219. return Boolean.valueOf(value != 0);
  220. case SocketOptions.IP_TOS:
  221. case SocketOptions.SO_LINGER:
  222. case SocketOptions.SO_RCVBUF:
  223. case SocketOptions.SO_SNDBUF:
  224. case SocketOptions.SO_TIMEOUT:
  225. return new Integer(value);
  226. default:
  227. throw new SocketException("getting option " + optionId +
  228. " not supported here");
  229. }
  230. }
  231. private static native int getOption(int fd, int id) throws SocketException;
  232. /**
  233. * Returns an Inet4Address or Inet6Address instance belonging to the
  234. * interface which is set as the multicast interface.
  235. *
  236. * The optionId is provided to make it possible that the native
  237. * implementation may do something different depending on whether
  238. * the value is SocketOptions.IP_MULTICAST_IF or
  239. * SocketOptions.IP_MULTICAST_IF2.
  240. */
  241. public InetAddress getMulticastInterface(int optionId)
  242. throws SocketException
  243. {
  244. try
  245. {
  246. return getMulticastInterface(nfd.getNativeFD(), optionId);
  247. }
  248. catch (IOException ioe)
  249. {
  250. SocketException se = new SocketException();
  251. se.initCause(ioe);
  252. throw se;
  253. }
  254. }
  255. private static native InetAddress getMulticastInterface(int fd,
  256. int optionId);
  257. /**
  258. * Binds this socket to the given local address and port.
  259. *
  260. * @param address The address to bind to; the InetAddress is either
  261. * an IPv4 or IPv6 address.
  262. * @throws IOException If binding fails; for example, if the port
  263. * in the given InetSocketAddress is privileged, and the current
  264. * process has insufficient privileges.
  265. */
  266. public void bind(InetSocketAddress address) throws IOException
  267. {
  268. InetAddress addr = address.getAddress();
  269. if (addr instanceof Inet4Address)
  270. {
  271. bind (nfd.getNativeFD(), addr.getAddress(), address.getPort());
  272. }
  273. else if (addr instanceof Inet6Address)
  274. bind6 (nfd.getNativeFD(), addr.getAddress(), address.getPort());
  275. else
  276. throw new SocketException ("unsupported address type");
  277. }
  278. /**
  279. * Native bind function for IPv4 addresses. The addr array must be
  280. * exactly four bytes long.
  281. *
  282. * VMs without native support need not implement this.
  283. *
  284. * @param fd The native file descriptor integer.
  285. * @param addr The IPv4 address, in network byte order.
  286. * @param port The port to bind to.
  287. * @throws IOException
  288. */
  289. private static native void bind(int fd, byte[] addr, int port)
  290. throws IOException;
  291. /**
  292. * Native bind function for IPv6 addresses. The addr array must be
  293. * exactly sixteen bytes long.
  294. *
  295. * VMs without native support need not implement this.
  296. *
  297. * @param fd The native file descriptor integer.
  298. * @param addr The IPv6 address, in network byte order.
  299. * @param port The port to bind to.
  300. * @throws IOException
  301. */
  302. private static native void bind6(int fd, byte[] addr, int port)
  303. throws IOException;
  304. /**
  305. * Listen on this socket for incoming connections.
  306. *
  307. * @param backlog The backlog of connections.
  308. * @throws IOException If listening fails.
  309. * @see gnu.java.nio.VMChannel#accept()
  310. */
  311. public void listen(int backlog) throws IOException
  312. {
  313. listen(nfd.getNativeFD(), backlog);
  314. }
  315. /**
  316. * Native listen function. VMs without native support need not implement
  317. * this.
  318. *
  319. * @param fd The file descriptor integer.
  320. * @param backlog The listen backlog size.
  321. * @throws IOException
  322. */
  323. private static native void listen(int fd, int backlog) throws IOException;
  324. public void join(InetAddress group) throws IOException
  325. {
  326. if (group instanceof Inet4Address)
  327. join(nfd.getNativeFD(), group.getAddress());
  328. else if (group instanceof Inet6Address)
  329. join6(nfd.getNativeFD(), group.getAddress());
  330. else
  331. throw new IllegalArgumentException("unknown address type");
  332. }
  333. private static native void join(int fd, byte[] addr) throws IOException;
  334. private static native void join6(int fd, byte[] addr) throws IOException;
  335. public void leave(InetAddress group) throws IOException
  336. {
  337. if (group instanceof Inet4Address)
  338. leave(nfd.getNativeFD(), group.getAddress());
  339. else if (group instanceof Inet6Address)
  340. leave6(nfd.getNativeFD(), group.getAddress());
  341. else
  342. throw new IllegalArgumentException("unknown address type");
  343. }
  344. private static native void leave(int fd, byte[] addr) throws IOException;
  345. private static native void leave6(int fd, byte[] addr) throws IOException;
  346. public void joinGroup(InetSocketAddress addr, NetworkInterface netif)
  347. throws IOException
  348. {
  349. InetAddress address = addr.getAddress();
  350. if (address instanceof Inet4Address)
  351. joinGroup(nfd.getNativeFD(), address.getAddress(),
  352. netif != null ? netif.getName() : null);
  353. else if (address instanceof Inet6Address)
  354. joinGroup6(nfd.getNativeFD(), address.getAddress(),
  355. netif != null ? netif.getName() : null);
  356. else
  357. throw new IllegalArgumentException("unknown address type");
  358. }
  359. private static native void joinGroup(int fd, byte[] addr, String ifname)
  360. throws IOException;
  361. private static native void joinGroup6(int fd, byte[] addr, String ifname)
  362. throws IOException;
  363. public void leaveGroup(InetSocketAddress addr, NetworkInterface netif)
  364. throws IOException
  365. {
  366. InetAddress address = addr.getAddress();
  367. if (address instanceof Inet4Address)
  368. leaveGroup(nfd.getNativeFD(), address.getAddress(),
  369. netif != null ? netif.getName() : null);
  370. else if (address instanceof Inet6Address)
  371. leaveGroup6(nfd.getNativeFD(), address.getAddress(),
  372. netif != null ? netif.getName() : null);
  373. else
  374. throw new IllegalArgumentException("unknown address type");
  375. }
  376. private static native void leaveGroup(int fd, byte[] addr, String ifname)
  377. throws IOException;
  378. private static native void leaveGroup6(int fd, byte[] addr, String ifname)
  379. throws IOException;
  380. public void shutdownInput() throws IOException
  381. {
  382. shutdownInput(nfd.getNativeFD());
  383. }
  384. private static native void shutdownInput(int native_fd) throws IOException;
  385. public void shutdownOutput() throws IOException
  386. {
  387. shutdownOutput(nfd.getNativeFD());
  388. }
  389. private static native void shutdownOutput(int native_fd) throws IOException;
  390. public void sendUrgentData(int data) throws IOException
  391. {
  392. sendUrgentData(nfd.getNativeFD(), data);
  393. }
  394. private static native void sendUrgentData(int natfive_fd, int data) throws IOException;
  395. public void close() throws IOException
  396. {
  397. nfd.close();
  398. }
  399. // Inner classes.
  400. /**
  401. * Our wrapper for the native file descriptor. In this implementation,
  402. * it is a simple wrapper around {@link VMChannel.State}, to simplify
  403. * management of the native state.
  404. */
  405. public final class State
  406. {
  407. private VMChannel.State channelFd;
  408. State()
  409. {
  410. channelFd = null;
  411. }
  412. public boolean isValid()
  413. {
  414. if (channelFd != null)
  415. return channelFd.isValid();
  416. return false;
  417. }
  418. public int getNativeFD() throws IOException
  419. {
  420. return channelFd.getNativeFD();
  421. }
  422. public void setChannelFD(final VMChannel.State nfd) throws IOException
  423. {
  424. if (this.channelFd != null && this.channelFd.isValid())
  425. throw new IOException("file descriptor already initialized");
  426. this.channelFd = nfd;
  427. }
  428. public void close() throws IOException
  429. {
  430. if (channelFd == null)
  431. throw new IOException("invalid file descriptor");
  432. channelFd.close();
  433. }
  434. protected void finalize() throws Throwable
  435. {
  436. try
  437. {
  438. if (isValid())
  439. close();
  440. }
  441. finally
  442. {
  443. super.finalize();
  444. }
  445. }
  446. }
  447. }