1 /*
2  * Copyright (c) 2007, 2013, 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 /**
29  * Defines the <em>standard</em> socket options.
30  *
31  * <p> The {@link SocketOption#name name} of each socket option defined by this
32  * class is its field name.
33  *
34  * <p> In this release, the socket options defined here are used by {@link
35  * java.nio.channels.NetworkChannel network} channels in the {@link
36  * java.nio.channels channels} package.
37  *
38  * @since 1.7
39  */
40 
41 public final class StandardSocketOptions {
StandardSocketOptions()42     private StandardSocketOptions() { }
43 
44     // -- SOL_SOCKET --
45 
46     /**
47      * Allow transmission of broadcast datagrams.
48      *
49      * <p> The value of this socket option is a {@code Boolean} that represents
50      * whether the option is enabled or disabled. The option is specific to
51      * datagram-oriented sockets sending to {@link java.net.Inet4Address IPv4}
52      * broadcast addresses. When the socket option is enabled then the socket
53      * can be used to send <em>broadcast datagrams</em>.
54      *
55      * <p> The initial value of this socket option is {@code FALSE}. The socket
56      * option may be enabled or disabled at any time. Some operating systems may
57      * require that the Java virtual machine be started with implementation
58      * specific privileges to enable this option or send broadcast datagrams.
59      *
60      * @see <a href="http://www.ietf.org/rfc/rfc919.txt">RFC&nbsp;929:
61      * Broadcasting Internet Datagrams</a>
62      * @see DatagramSocket#setBroadcast
63      */
64     public static final SocketOption<Boolean> SO_BROADCAST =
65         new StdSocketOption<Boolean>("SO_BROADCAST", Boolean.class);
66 
67     /**
68      * Keep connection alive.
69      *
70      * <p> The value of this socket option is a {@code Boolean} that represents
71      * whether the option is enabled or disabled. When the {@code SO_KEEPALIVE}
72      * option is enabled the operating system may use a <em>keep-alive</em>
73      * mechanism to periodically probe the other end of a connection when the
74      * connection is otherwise idle. The exact semantics of the keep alive
75      * mechanism is system dependent and therefore unspecified.
76      *
77      * <p> The initial value of this socket option is {@code FALSE}. The socket
78      * option may be enabled or disabled at any time.
79      *
80      * @see <a href="http://www.ietf.org/rfc/rfc1122.txt">RFC&nbsp;1122
81      * Requirements for Internet Hosts -- Communication Layers</a>
82      * @see Socket#setKeepAlive
83      */
84     public static final SocketOption<Boolean> SO_KEEPALIVE =
85         new StdSocketOption<Boolean>("SO_KEEPALIVE", Boolean.class);
86 
87     /**
88      * The size of the socket send buffer.
89      *
90      * <p> The value of this socket option is an {@code Integer} that is the
91      * size of the socket send buffer in bytes. The socket send buffer is an
92      * output buffer used by the networking implementation. It may need to be
93      * increased for high-volume connections. The value of the socket option is
94      * a <em>hint</em> to the implementation to size the buffer and the actual
95      * size may differ. The socket option can be queried to retrieve the actual
96      * size.
97      *
98      * <p> For datagram-oriented sockets, the size of the send buffer may limit
99      * the size of the datagrams that may be sent by the socket. Whether
100      * datagrams larger than the buffer size are sent or discarded is system
101      * dependent.
102      *
103      * <p> The initial/default size of the socket send buffer and the range of
104      * allowable values is system dependent although a negative size is not
105      * allowed. An attempt to set the socket send buffer to larger than its
106      * maximum size causes it to be set to its maximum size.
107      *
108      * <p> An implementation allows this socket option to be set before the
109      * socket is bound or connected. Whether an implementation allows the
110      * socket send buffer to be changed after the socket is bound is system
111      * dependent.
112      *
113      * @see Socket#setSendBufferSize
114      */
115     public static final SocketOption<Integer> SO_SNDBUF =
116         new StdSocketOption<Integer>("SO_SNDBUF", Integer.class);
117 
118 
119     /**
120      * The size of the socket receive buffer.
121      *
122      * <p> The value of this socket option is an {@code Integer} that is the
123      * size of the socket receive buffer in bytes. The socket receive buffer is
124      * an input buffer used by the networking implementation. It may need to be
125      * increased for high-volume connections or decreased to limit the possible
126      * backlog of incoming data. The value of the socket option is a
127      * <em>hint</em> to the implementation to size the buffer and the actual
128      * size may differ.
129      *
130      * <p> For datagram-oriented sockets, the size of the receive buffer may
131      * limit the size of the datagrams that can be received. Whether datagrams
132      * larger than the buffer size can be received is system dependent.
133      * Increasing the socket receive buffer may be important for cases where
134      * datagrams arrive in bursts faster than they can be processed.
135      *
136      * <p> In the case of stream-oriented sockets and the TCP/IP protocol, the
137      * size of the socket receive buffer may be used when advertising the size
138      * of the TCP receive window to the remote peer.
139      *
140      * <p> The initial/default size of the socket receive buffer and the range
141      * of allowable values is system dependent although a negative size is not
142      * allowed. An attempt to set the socket receive buffer to larger than its
143      * maximum size causes it to be set to its maximum size.
144      *
145      * <p> An implementation allows this socket option to be set before the
146      * socket is bound or connected. Whether an implementation allows the
147      * socket receive buffer to be changed after the socket is bound is system
148      * dependent.
149      *
150      * @see <a href="http://www.ietf.org/rfc/rfc1323.txt">RFC&nbsp;1323: TCP
151      * Extensions for High Performance</a>
152      * @see Socket#setReceiveBufferSize
153      * @see ServerSocket#setReceiveBufferSize
154      */
155     public static final SocketOption<Integer> SO_RCVBUF =
156         new StdSocketOption<Integer>("SO_RCVBUF", Integer.class);
157 
158     /**
159      * Re-use address.
160      *
161      * <p> The value of this socket option is a {@code Boolean} that represents
162      * whether the option is enabled or disabled. The exact semantics of this
163      * socket option are socket type and system dependent.
164      *
165      * <p> In the case of stream-oriented sockets, this socket option will
166      * usually determine whether the socket can be bound to a socket address
167      * when a previous connection involving that socket address is in the
168      * <em>TIME_WAIT</em> state. On implementations where the semantics differ,
169      * and the socket option is not required to be enabled in order to bind the
170      * socket when a previous connection is in this state, then the
171      * implementation may choose to ignore this option.
172      *
173      * <p> For datagram-oriented sockets the socket option is used to allow
174      * multiple programs bind to the same address. This option should be enabled
175      * when the socket is to be used for Internet Protocol (IP) multicasting.
176      *
177      * <p> An implementation allows this socket option to be set before the
178      * socket is bound or connected. Changing the value of this socket option
179      * after the socket is bound has no effect. The default value of this
180      * socket option is system dependent.
181      *
182      * @see <a href="http://www.ietf.org/rfc/rfc793.txt">RFC&nbsp;793: Transmission
183      * Control Protocol</a>
184      * @see ServerSocket#setReuseAddress
185      */
186     public static final SocketOption<Boolean> SO_REUSEADDR =
187         new StdSocketOption<Boolean>("SO_REUSEADDR", Boolean.class);
188 
189     /**
190      * Linger on close if data is present.
191      *
192      * <p> The value of this socket option is an {@code Integer} that controls
193      * the action taken when unsent data is queued on the socket and a method
194      * to close the socket is invoked. If the value of the socket option is zero
195      * or greater, then it represents a timeout value, in seconds, known as the
196      * <em>linger interval</em>. The linger interval is the timeout for the
197      * {@code close} method to block while the operating system attempts to
198      * transmit the unsent data or it decides that it is unable to transmit the
199      * data. If the value of the socket option is less than zero then the option
200      * is disabled. In that case the {@code close} method does not wait until
201      * unsent data is transmitted; if possible the operating system will transmit
202      * any unsent data before the connection is closed.
203      *
204      * <p> This socket option is intended for use with sockets that are configured
205      * in {@link java.nio.channels.SelectableChannel#isBlocking() blocking} mode
206      * only. The behavior of the {@code close} method when this option is
207      * enabled on a non-blocking socket is not defined.
208      *
209      * <p> The initial value of this socket option is a negative value, meaning
210      * that the option is disabled. The option may be enabled, or the linger
211      * interval changed, at any time. The maximum value of the linger interval
212      * is system dependent. Setting the linger interval to a value that is
213      * greater than its maximum value causes the linger interval to be set to
214      * its maximum value.
215      *
216      * @see Socket#setSoLinger
217      */
218     public static final SocketOption<Integer> SO_LINGER =
219         new StdSocketOption<Integer>("SO_LINGER", Integer.class);
220 
221 
222     // -- IPPROTO_IP --
223 
224     /**
225      * The Type of Service (ToS) octet in the Internet Protocol (IP) header.
226      *
227      * <p> The value of this socket option is an {@code Integer} representing
228      * the value of the ToS octet in IP packets sent by sockets to an {@link
229      * StandardProtocolFamily#INET IPv4} socket. The interpretation of the ToS
230      * octet is network specific and is not defined by this class. Further
231      * information on the ToS octet can be found in <a
232      * href="http://www.ietf.org/rfc/rfc1349.txt">RFC&nbsp;1349</a> and <a
233      * href="http://www.ietf.org/rfc/rfc2474.txt">RFC&nbsp;2474</a>. The value
234      * of the socket option is a <em>hint</em>. An implementation may ignore the
235      * value, or ignore specific values.
236      *
237      * <p> The initial/default value of the TOS field in the ToS octet is
238      * implementation specific but will typically be {@code 0}. For
239      * datagram-oriented sockets the option may be configured at any time after
240      * the socket has been bound. The new value of the octet is used when sending
241      * subsequent datagrams. It is system dependent whether this option can be
242      * queried or changed prior to binding the socket.
243      *
244      * <p> The behavior of this socket option on a stream-oriented socket, or an
245      * {@link StandardProtocolFamily#INET6 IPv6} socket, is not defined in this
246      * release.
247      *
248      * @see DatagramSocket#setTrafficClass
249      */
250     public static final SocketOption<Integer> IP_TOS =
251         new StdSocketOption<Integer>("IP_TOS", Integer.class);
252 
253     /**
254      * The network interface for Internet Protocol (IP) multicast datagrams.
255      *
256      * <p> The value of this socket option is a {@link NetworkInterface} that
257      * represents the outgoing interface for multicast datagrams sent by the
258      * datagram-oriented socket. For {@link StandardProtocolFamily#INET6 IPv6}
259      * sockets then it is system dependent whether setting this option also
260      * sets the outgoing interface for multicast datagrams sent to IPv4
261      * addresses.
262      *
263      * <p> The initial/default value of this socket option may be {@code null}
264      * to indicate that outgoing interface will be selected by the operating
265      * system, typically based on the network routing tables. An implementation
266      * allows this socket option to be set after the socket is bound. Whether
267      * the socket option can be queried or changed prior to binding the socket
268      * is system dependent.
269      *
270      * @see java.nio.channels.MulticastChannel
271      * @see MulticastSocket#setInterface
272      */
273     public static final SocketOption<NetworkInterface> IP_MULTICAST_IF =
274         new StdSocketOption<NetworkInterface>("IP_MULTICAST_IF", NetworkInterface.class);
275 
276     /**
277      * The <em>time-to-live</em> for Internet Protocol (IP) multicast datagrams.
278      *
279      * <p> The value of this socket option is an {@code Integer} in the range
280      * {@code 0 <= value <= 255}. It is used to control the scope of multicast
281      * datagrams sent by the datagram-oriented socket.
282      * In the case of an {@link StandardProtocolFamily#INET IPv4} socket
283      * the option is the time-to-live (TTL) on multicast datagrams sent by the
284      * socket. Datagrams with a TTL of zero are not transmitted on the network
285      * but may be delivered locally. In the case of an {@link
286      * StandardProtocolFamily#INET6 IPv6} socket the option is the
287      * <em>hop limit</em> which is number of <em>hops</em> that the datagram can
288      * pass through before expiring on the network. For IPv6 sockets it is
289      * system dependent whether the option also sets the <em>time-to-live</em>
290      * on multicast datagrams sent to IPv4 addresses.
291      *
292      * <p> The initial/default value of the time-to-live setting is typically
293      * {@code 1}. An implementation allows this socket option to be set after
294      * the socket is bound. Whether the socket option can be queried or changed
295      * prior to binding the socket is system dependent.
296      *
297      * @see java.nio.channels.MulticastChannel
298      * @see MulticastSocket#setTimeToLive
299      */
300     public static final SocketOption<Integer> IP_MULTICAST_TTL =
301         new StdSocketOption<Integer>("IP_MULTICAST_TTL", Integer.class);
302 
303     /**
304      * Loopback for Internet Protocol (IP) multicast datagrams.
305      *
306      * <p> The value of this socket option is a {@code Boolean} that controls
307      * the <em>loopback</em> of multicast datagrams. The value of the socket
308      * option represents if the option is enabled or disabled.
309      *
310      * <p> The exact semantics of this socket options are system dependent.
311      * In particular, it is system dependent whether the loopback applies to
312      * multicast datagrams sent from the socket or received by the socket.
313      * For {@link StandardProtocolFamily#INET6 IPv6} sockets then it is
314      * system dependent whether the option also applies to multicast datagrams
315      * sent to IPv4 addresses.
316      *
317      * <p> The initial/default value of this socket option is {@code TRUE}. An
318      * implementation allows this socket option to be set after the socket is
319      * bound. Whether the socket option can be queried or changed prior to
320      * binding the socket is system dependent.
321      *
322      * @see java.nio.channels.MulticastChannel
323      *  @see MulticastSocket#setLoopbackMode
324      */
325     public static final SocketOption<Boolean> IP_MULTICAST_LOOP =
326         new StdSocketOption<Boolean>("IP_MULTICAST_LOOP", Boolean.class);
327 
328 
329     // -- IPPROTO_TCP --
330 
331     /**
332      * Disable the Nagle algorithm.
333      *
334      * <p> The value of this socket option is a {@code Boolean} that represents
335      * whether the option is enabled or disabled. The socket option is specific to
336      * stream-oriented sockets using the TCP/IP protocol. TCP/IP uses an algorithm
337      * known as <em>The Nagle Algorithm</em> to coalesce short segments and
338      * improve network efficiency.
339      *
340      * <p> The default value of this socket option is {@code FALSE}. The
341      * socket option should only be enabled in cases where it is known that the
342      * coalescing impacts performance. The socket option may be enabled at any
343      * time. In other words, the Nagle Algorithm can be disabled. Once the option
344      * is enabled, it is system dependent whether it can be subsequently
345      * disabled. If it cannot, then invoking the {@code setOption} method to
346      * disable the option has no effect.
347      *
348      * @see <a href="http://www.ietf.org/rfc/rfc1122.txt">RFC&nbsp;1122:
349      * Requirements for Internet Hosts -- Communication Layers</a>
350      * @see Socket#setTcpNoDelay
351      */
352     public static final SocketOption<Boolean> TCP_NODELAY =
353         new StdSocketOption<Boolean>("TCP_NODELAY", Boolean.class);
354 
355 
356     private static class StdSocketOption<T> implements SocketOption<T> {
357         private final String name;
358         private final Class<T> type;
StdSocketOption(String name, Class<T> type)359         StdSocketOption(String name, Class<T> type) {
360             this.name = name;
361             this.type = type;
362         }
name()363         @Override public String name() { return name; }
type()364         @Override public Class<T> type() { return type; }
toString()365         @Override public String toString() { return name; }
366     }
367 }
368