1. /*
  2. * @(#)PlainDatagramSocketImpl.java 1.19 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.net;
  8. import java.io.FileDescriptor;
  9. import java.io.IOException;
  10. import java.io.InterruptedIOException;
  11. /**
  12. * Concrete datagram and multicast socket implementation base class.
  13. * Note: This is not a public class, so that applets cannot call
  14. * into the implementation directly and hence cannot bypass the
  15. * security checks present in the DatagramSocket and MulticastSocket
  16. * classes.
  17. *
  18. * @author Pavani Diwanji
  19. */
  20. class PlainDatagramSocketImpl extends DatagramSocketImpl
  21. {
  22. /* timeout value for receive() */
  23. private int timeout = 0;
  24. /**
  25. * Load net library into runtime.
  26. */
  27. static {
  28. java.security.AccessController.doPrivileged(
  29. new sun.security.action.LoadLibraryAction("net"));
  30. init();
  31. }
  32. /**
  33. * Creates a datagram socket
  34. */
  35. protected synchronized void create() throws SocketException {
  36. fd = new FileDescriptor();
  37. datagramSocketCreate();
  38. }
  39. /**
  40. * Binds a datagram socket to a local port.
  41. */
  42. protected synchronized native void bind(int lport, InetAddress laddr)
  43. throws SocketException;
  44. /**
  45. * Sends a datagram packet. The packet contains the data and the
  46. * destination address to send the packet to.
  47. * @param packet to be sent.
  48. */
  49. protected native void send(DatagramPacket p) throws IOException;
  50. /**
  51. * Peek at the packet to see who it is from.
  52. * @param return the address which the packet came from.
  53. */
  54. protected synchronized native int peek(InetAddress i) throws IOException;
  55. /**
  56. * Receive the datagram packet.
  57. * @param Packet Received.
  58. */
  59. protected synchronized native void receive(DatagramPacket p)
  60. throws IOException;
  61. /**
  62. * Set the TTL (time-to-live) option.
  63. * @param TTL to be set.
  64. */
  65. protected native void setTimeToLive(int ttl) throws IOException;
  66. /**
  67. * Get the TTL (time-to-live) option.
  68. */
  69. protected native int getTimeToLive() throws IOException;
  70. /**
  71. * Set the TTL (time-to-live) option.
  72. * @param TTL to be set.
  73. */
  74. protected native void setTTL(byte ttl) throws IOException;
  75. /**
  76. * Get the TTL (time-to-live) option.
  77. */
  78. protected native byte getTTL() throws IOException;
  79. /**
  80. * Join the multicast group.
  81. * @param multicast address to join.
  82. */
  83. protected native void join(InetAddress inetaddr) throws IOException;
  84. /**
  85. * Leave the multicast group.
  86. * @param multicast address to leave.
  87. */
  88. protected native void leave(InetAddress inetaddr) throws IOException;
  89. /**
  90. * Close the socket.
  91. */
  92. protected void close() {
  93. if (fd != null) {
  94. datagramSocketClose();
  95. fd = null;
  96. }
  97. }
  98. protected void finalize() {
  99. close();
  100. }
  101. /**
  102. * set a value - since we only support (setting) binary options
  103. * here, o must be a Boolean
  104. */
  105. public void setOption(int optID, Object o) throws SocketException {
  106. switch (optID) {
  107. /* check type safety b4 going native. These should never
  108. * fail, since only java.Socket* has access to
  109. * PlainSocketImpl.setOption().
  110. */
  111. case SO_TIMEOUT:
  112. if (o == null || !(o instanceof Integer)) {
  113. throw new SocketException("bad argument for SO_TIMEOUT");
  114. }
  115. int tmp = ((Integer) o).intValue();
  116. if (tmp < 0)
  117. throw new IllegalArgumentException("timeout < 0");
  118. timeout = tmp;
  119. return;
  120. case SO_BINDADDR:
  121. throw new SocketException("Cannot re-bind Socket");
  122. case SO_REUSEADDR:
  123. if (o == null || !(o instanceof Integer)) {
  124. throw new SocketException("bad argument for SO_REUSEADDR");
  125. }
  126. break;
  127. case SO_RCVBUF:
  128. case SO_SNDBUF:
  129. if (o == null || !(o instanceof Integer) ||
  130. ((Integer)o).intValue() < 0) {
  131. throw new SocketException("bad argument for SO_SNDBUF or " +
  132. "SO_RCVBUF");
  133. }
  134. break;
  135. case IP_MULTICAST_IF:
  136. if (o == null || !(o instanceof InetAddress))
  137. throw new SocketException("bad argument for IP_MULTICAST_IF");
  138. break;
  139. default:
  140. throw new SocketException("invalid option: " + optID);
  141. }
  142. socketSetOption(optID, o);
  143. }
  144. /*
  145. * get option's state - set or not
  146. */
  147. public Object getOption(int optID) throws SocketException {
  148. Integer result = null;
  149. if (optID == SO_TIMEOUT) {
  150. result = new Integer(timeout);
  151. } else {
  152. int ret = socketGetOption(optID);
  153. if (optID == SO_BINDADDR || optID == IP_MULTICAST_IF) {
  154. InetAddress in = new InetAddress();
  155. in.address = ret;
  156. return in;
  157. } else if (optID == SO_RCVBUF || optID == SO_SNDBUF) {
  158. result = new Integer(ret);
  159. }
  160. }
  161. return result;
  162. }
  163. private native void datagramSocketCreate() throws SocketException;
  164. private native void datagramSocketClose();
  165. private native void socketSetOption(int opt, Object val)
  166. throws SocketException;
  167. private native int socketGetOption(int opt) throws SocketException;
  168. /**
  169. * Perform class load-time initializations.
  170. */
  171. private native static void init();
  172. }