1 /* 2 * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.net; 27 28 import java.lang.annotation.Native; 29 30 /** 31 * Interface of methods to get/set socket options. This interface is 32 * implemented by: <B>SocketImpl</B> and <B>DatagramSocketImpl</B>. 33 * Subclasses of these should override the methods 34 * of this interface in order to support their own options. 35 * <P> 36 * The methods and constants which specify options in this interface are 37 * for implementation only. If you're not subclassing SocketImpl or 38 * DatagramSocketImpl, <B>you won't use these directly.</B> There are 39 * type-safe methods to get/set each of these options in Socket, ServerSocket, 40 * DatagramSocket and MulticastSocket. 41 * 42 * @author David Brown 43 */ 44 45 46 public interface SocketOptions { 47 48 /** 49 * Enable/disable the option specified by <I>optID</I>. If the option 50 * is to be enabled, and it takes an option-specific "value", this is 51 * passed in <I>value</I>. The actual type of value is option-specific, 52 * and it is an error to pass something that isn't of the expected type: 53 * <BR><PRE> 54 * SocketImpl s; 55 * ... 56 * s.setOption(SO_LINGER, new Integer(10)); 57 * // OK - set SO_LINGER w/ timeout of 10 sec. 58 * s.setOption(SO_LINGER, new Double(10)); 59 * // ERROR - expects java.lang.Integer 60 *</PRE> 61 * If the requested option is binary, it can be set using this method by 62 * a java.lang.Boolean: 63 * <BR><PRE> 64 * s.setOption(TCP_NODELAY, Boolean.TRUE); 65 * // OK - enables TCP_NODELAY, a binary option 66 * </PRE> 67 * <BR> 68 * Any option can be disabled using this method with a Boolean.FALSE: 69 * <BR><PRE> 70 * s.setOption(TCP_NODELAY, Boolean.FALSE); 71 * // OK - disables TCP_NODELAY 72 * s.setOption(SO_LINGER, Boolean.FALSE); 73 * // OK - disables SO_LINGER 74 * </PRE> 75 * <BR> 76 * For an option that has a notion of on and off, and requires 77 * a non-boolean parameter, setting its value to anything other than 78 * <I>Boolean.FALSE</I> implicitly enables it. 79 * <BR> 80 * Throws SocketException if the option is unrecognized, 81 * the socket is closed, or some low-level error occurred 82 * <BR> 83 * @param optID identifies the option 84 * @param value the parameter of the socket option 85 * @throws SocketException if the option is unrecognized, 86 * the socket is closed, or some low-level error occurred 87 * @see #getOption(int) 88 */ 89 public void 90 setOption(int optID, Object value) throws SocketException; 91 92 /** 93 * Fetch the value of an option. 94 * Binary options will return java.lang.Boolean.TRUE 95 * if enabled, java.lang.Boolean.FALSE if disabled, e.g.: 96 * <BR><PRE> 97 * SocketImpl s; 98 * ... 99 * Boolean noDelay = (Boolean)(s.getOption(TCP_NODELAY)); 100 * if (noDelay.booleanValue()) { 101 * // true if TCP_NODELAY is enabled... 102 * ... 103 * } 104 * </PRE> 105 * <P> 106 * For options that take a particular type as a parameter, 107 * getOption(int) will return the parameter's value, else 108 * it will return java.lang.Boolean.FALSE: 109 * <PRE> 110 * Object o = s.getOption(SO_LINGER); 111 * if (o instanceof Integer) { 112 * System.out.print("Linger time is " + ((Integer)o).intValue()); 113 * } else { 114 * // the true type of o is java.lang.Boolean.FALSE; 115 * } 116 * </PRE> 117 * 118 * @param optID an {@code int} identifying the option to fetch 119 * @return the value of the option 120 * @throws SocketException if the socket is closed 121 * @throws SocketException if <I>optID</I> is unknown along the 122 * protocol stack (including the SocketImpl) 123 * @see #setOption(int, java.lang.Object) 124 */ 125 public Object getOption(int optID) throws SocketException; 126 127 /** 128 * The java-supported BSD-style options. 129 */ 130 131 /** 132 * Disable Nagle's algorithm for this connection. Written data 133 * to the network is not buffered pending acknowledgement of 134 * previously written data. 135 *<P> 136 * Valid for TCP only: SocketImpl. 137 * 138 * @see Socket#setTcpNoDelay 139 * @see Socket#getTcpNoDelay 140 */ 141 142 @Native public static final int TCP_NODELAY = 0x0001; 143 144 /** 145 * Fetch the local address binding of a socket (this option cannot 146 * be "set" only "gotten", since sockets are bound at creation time, 147 * and so the locally bound address cannot be changed). The default local 148 * address of a socket is INADDR_ANY, meaning any local address on a 149 * multi-homed host. A multi-homed host can use this option to accept 150 * connections to only one of its addresses (in the case of a 151 * ServerSocket or DatagramSocket), or to specify its return address 152 * to the peer (for a Socket or DatagramSocket). The parameter of 153 * this option is an InetAddress. 154 * <P> 155 * This option <B>must</B> be specified in the constructor. 156 * <P> 157 * Valid for: SocketImpl, DatagramSocketImpl 158 * 159 * @see Socket#getLocalAddress 160 * @see DatagramSocket#getLocalAddress 161 */ 162 163 @Native public static final int SO_BINDADDR = 0x000F; 164 165 /** Sets SO_REUSEADDR for a socket. This is used only for MulticastSockets 166 * in java, and it is set by default for MulticastSockets. 167 * <P> 168 * Valid for: DatagramSocketImpl 169 */ 170 171 @Native public static final int SO_REUSEADDR = 0x04; 172 173 /** Sets SO_REUSEPORT for a socket. This option enables and disables 174 * the ability to have multiple sockets listen to the same address 175 * and port. 176 * <P> 177 * Valid for: SocketImpl, DatagramSocketImpl 178 * 179 * @since 9 180 * @see StandardSocketOptions#SO_REUSEPORT 181 */ 182 @Native public static final int SO_REUSEPORT = 0x0E; 183 184 /** 185 * Sets SO_BROADCAST for a socket. This option enables and disables 186 * the ability of the process to send broadcast messages. It is supported 187 * for only datagram sockets and only on networks that support 188 * the concept of a broadcast message (e.g. Ethernet, token ring, etc.), 189 * and it is set by default for DatagramSockets. 190 * @since 1.4 191 */ 192 193 @Native public static final int SO_BROADCAST = 0x0020; 194 195 /** Set which outgoing interface on which to send multicast packets. 196 * Useful on hosts with multiple network interfaces, where applications 197 * want to use other than the system default. Takes/returns an InetAddress. 198 * <P> 199 * Valid for Multicast: DatagramSocketImpl 200 * 201 * @see MulticastSocket#setInterface(InetAddress) 202 * @see MulticastSocket#getInterface() 203 */ 204 205 @Native public static final int IP_MULTICAST_IF = 0x10; 206 207 /** Same as above. This option is introduced so that the behaviour 208 * with IP_MULTICAST_IF will be kept the same as before, while 209 * this new option can support setting outgoing interfaces with either 210 * IPv4 and IPv6 addresses. 211 * 212 * NOTE: make sure there is no conflict with this 213 * @see MulticastSocket#setNetworkInterface(NetworkInterface) 214 * @see MulticastSocket#getNetworkInterface() 215 * @since 1.4 216 */ 217 @Native public static final int IP_MULTICAST_IF2 = 0x1f; 218 219 /** 220 * This option enables or disables local loopback of multicast datagrams. 221 * This option is enabled by default for Multicast Sockets. 222 * @since 1.4 223 */ 224 225 @Native public static final int IP_MULTICAST_LOOP = 0x12; 226 227 /** 228 * This option sets the type-of-service or traffic class field 229 * in the IP header for a TCP or UDP socket. 230 * @since 1.4 231 */ 232 233 @Native public static final int IP_TOS = 0x3; 234 235 /** 236 * Specify a linger-on-close timeout. This option disables/enables 237 * immediate return from a <B>close()</B> of a TCP Socket. Enabling 238 * this option with a non-zero Integer <I>timeout</I> means that a 239 * <B>close()</B> will block pending the transmission and acknowledgement 240 * of all data written to the peer, at which point the socket is closed 241 * <I>gracefully</I>. Upon reaching the linger timeout, the socket is 242 * closed <I>forcefully</I>, with a TCP RST. Enabling the option with a 243 * timeout of zero does a forceful close immediately. If the specified 244 * timeout value exceeds 65,535 it will be reduced to 65,535. 245 * <P> 246 * Valid only for TCP: SocketImpl 247 * 248 * @see Socket#setSoLinger 249 * @see Socket#getSoLinger 250 */ 251 @Native public static final int SO_LINGER = 0x0080; 252 253 /** Set a timeout on blocking Socket operations: 254 * <PRE> 255 * ServerSocket.accept(); 256 * SocketInputStream.read(); 257 * DatagramSocket.receive(); 258 * </PRE> 259 * 260 * <P> The option must be set prior to entering a blocking 261 * operation to take effect. If the timeout expires and the 262 * operation would continue to block, 263 * <B>java.io.InterruptedIOException</B> is raised. The Socket is 264 * not closed in this case. 265 * 266 * <P> Valid for all sockets: SocketImpl, DatagramSocketImpl 267 * 268 * @see Socket#setSoTimeout 269 * @see ServerSocket#setSoTimeout 270 * @see DatagramSocket#setSoTimeout 271 */ 272 @Native public static final int SO_TIMEOUT = 0x1006; 273 274 /** 275 * Set a hint the size of the underlying buffers used by the 276 * platform for outgoing network I/O. When used in set, this is a 277 * suggestion to the kernel from the application about the size of 278 * buffers to use for the data to be sent over the socket. When 279 * used in get, this must return the size of the buffer actually 280 * used by the platform when sending out data on this socket. 281 * 282 * Valid for all sockets: SocketImpl, DatagramSocketImpl 283 * 284 * @see Socket#setSendBufferSize 285 * @see Socket#getSendBufferSize 286 * @see DatagramSocket#setSendBufferSize 287 * @see DatagramSocket#getSendBufferSize 288 */ 289 @Native public static final int SO_SNDBUF = 0x1001; 290 291 /** 292 * Set a hint the size of the underlying buffers used by the 293 * platform for incoming network I/O. When used in set, this is a 294 * suggestion to the kernel from the application about the size of 295 * buffers to use for the data to be received over the 296 * socket. When used in get, this must return the size of the 297 * buffer actually used by the platform when receiving in data on 298 * this socket. 299 * 300 * Valid for all sockets: SocketImpl, DatagramSocketImpl 301 * 302 * @see Socket#setReceiveBufferSize 303 * @see Socket#getReceiveBufferSize 304 * @see DatagramSocket#setReceiveBufferSize 305 * @see DatagramSocket#getReceiveBufferSize 306 */ 307 @Native public static final int SO_RCVBUF = 0x1002; 308 309 /** 310 * When the keepalive option is set for a TCP socket and no data 311 * has been exchanged across the socket in either direction for 312 * 2 hours (NOTE: the actual value is implementation dependent), 313 * TCP automatically sends a keepalive probe to the peer. This probe is a 314 * TCP segment to which the peer must respond. 315 * One of three responses is expected: 316 * 1. The peer responds with the expected ACK. The application is not 317 * notified (since everything is OK). TCP will send another probe 318 * following another 2 hours of inactivity. 319 * 2. The peer responds with an RST, which tells the local TCP that 320 * the peer host has crashed and rebooted. The socket is closed. 321 * 3. There is no response from the peer. The socket is closed. 322 * 323 * The purpose of this option is to detect if the peer host crashes. 324 * 325 * Valid only for TCP socket: SocketImpl 326 * 327 * @see Socket#setKeepAlive 328 * @see Socket#getKeepAlive 329 */ 330 @Native public static final int SO_KEEPALIVE = 0x0008; 331 332 /** 333 * When the OOBINLINE option is set, any TCP urgent data received on 334 * the socket will be received through the socket input stream. 335 * When the option is disabled (which is the default) urgent data 336 * is silently discarded. 337 * 338 * @see Socket#setOOBInline 339 * @see Socket#getOOBInline 340 */ 341 @Native public static final int SO_OOBINLINE = 0x1003; 342 }