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