1 /* 2 * Copyright (c) 1995, 2018, 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.io.IOException; 29 import java.io.InputStream; 30 import java.io.OutputStream; 31 import java.io.FileDescriptor; 32 import java.util.Set; 33 import jdk.internal.misc.JdkRdmaSocketImplAccess; 34 import jdk.internal.misc.SharedSecrets; 35 36 /** 37 * The abstract class {@code SocketImpl} is a common superclass 38 * of all classes that actually implement sockets. It is used to 39 * create both client and server sockets. 40 * <p> 41 * A "plain" socket implements these methods exactly as 42 * described, without attempting to go through a firewall or proxy. 43 * 44 * @author unascribed 45 * @since 1.0 46 */ 47 public abstract class SocketImpl implements SocketOptions { 48 /** 49 * The actual Socket object. 50 */ 51 Socket socket = null; 52 ServerSocket serverSocket = null; 53 54 /** 55 * The file descriptor object for this socket. 56 */ 57 protected FileDescriptor fd; 58 59 /** 60 * The IP address of the remote end of this socket. 61 */ 62 protected InetAddress address; 63 64 /** 65 * The port number on the remote host to which this socket is connected. 66 */ 67 protected int port; 68 69 /** 70 * The local port number to which this socket is connected. 71 */ 72 protected int localport; 73 74 private static final JdkRdmaSocketImplAccess rsiAccess = 75 SharedSecrets.getJdkRdmaSocketImplAccess(); 76 77 /** 78 * Creates either a stream or a datagram socket. 79 * 80 * @param stream if {@code true}, create a stream socket; 81 * otherwise, create a datagram socket. 82 * @exception IOException if an I/O error occurs while creating the 83 * socket. 84 */ 85 protected abstract void create(boolean stream) throws IOException; 86 87 /** 88 * Connects this socket to the specified port on the named host. 89 * 90 * @param host the name of the remote host. 91 * @param port the port number. 92 * @exception IOException if an I/O error occurs when connecting to the 93 * remote host. 94 */ 95 protected abstract void connect(String host, int port) throws IOException; 96 97 /** 98 * Connects this socket to the specified port number on the specified host. 99 * 100 * @param address the IP address of the remote host. 101 * @param port the port number. 102 * @exception IOException if an I/O error occurs when attempting a 103 * connection. 104 */ 105 protected abstract void connect(InetAddress address, int port) throws IOException; 106 107 /** 108 * Connects this socket to the specified port number on the specified host. 109 * A timeout of zero is interpreted as an infinite timeout. The connection 110 * will then block until established or an error occurs. 111 * 112 * @param address the Socket address of the remote host. 113 * @param timeout the timeout value, in milliseconds, or zero for no timeout. 114 * @exception IOException if an I/O error occurs when attempting a 115 * connection. 116 * @since 1.4 117 */ 118 protected abstract void connect(SocketAddress address, int timeout) throws IOException; 119 120 /** 121 * Binds this socket to the specified local IP address and port number. 122 * 123 * @param host an IP address that belongs to a local interface. 124 * @param port the port number. 125 * @exception IOException if an I/O error occurs when binding this socket. 126 */ 127 protected abstract void bind(InetAddress host, int port) throws IOException; 128 129 /** 130 * Sets the maximum queue length for incoming connection indications 131 * (a request to connect) to the {@code count} argument. If a 132 * connection indication arrives when the queue is full, the 133 * connection is refused. 134 * 135 * @param backlog the maximum length of the queue. 136 * @exception IOException if an I/O error occurs when creating the queue. 137 */ 138 protected abstract void listen(int backlog) throws IOException; 139 140 /** 141 * Accepts a connection. 142 * 143 * @param s the accepted connection. 144 * @exception IOException if an I/O error occurs when accepting the 145 * connection. 146 */ 147 protected abstract void accept(SocketImpl s) throws IOException; 148 149 /** 150 * Returns an input stream for this socket. 151 * 152 * @return a stream for reading from this socket. 153 * @exception IOException if an I/O error occurs when creating the 154 * input stream. 155 */ 156 protected abstract InputStream getInputStream() throws IOException; 157 158 /** 159 * Returns an output stream for this socket. 160 * 161 * @return an output stream for writing to this socket. 162 * @exception IOException if an I/O error occurs when creating the 163 * output stream. 164 */ 165 protected abstract OutputStream getOutputStream() throws IOException; 166 167 /** 168 * Returns the number of bytes that can be read from this socket 169 * without blocking. 170 * 171 * @return the number of bytes that can be read from this socket 172 * without blocking. 173 * @exception IOException if an I/O error occurs when determining the 174 * number of bytes available. 175 */ 176 protected abstract int available() throws IOException; 177 178 /** 179 * Closes this socket. 180 * 181 * @exception IOException if an I/O error occurs when closing this socket. 182 */ 183 protected abstract void close() throws IOException; 184 185 /** 186 * Places the input stream for this socket at "end of stream". 187 * Any data sent to this socket is acknowledged and then 188 * silently discarded. 189 * 190 * If you read from a socket input stream after invoking this method on the 191 * socket, the stream's {@code available} method will return 0, and its 192 * {@code read} methods will return {@code -1} (end of stream). 193 * 194 * @exception IOException if an I/O error occurs when shutting down this 195 * socket. 196 * @see java.net.Socket#shutdownOutput() 197 * @see java.net.Socket#close() 198 * @see java.net.Socket#setSoLinger(boolean, int) 199 * @since 1.3 200 */ 201 protected void shutdownInput() throws IOException { 202 throw new IOException("Method not implemented!"); 203 } 204 205 /** 206 * Disables the output stream for this socket. 207 * For a TCP socket, any previously written data will be sent 208 * followed by TCP's normal connection termination sequence. 209 * 210 * If you write to a socket output stream after invoking 211 * shutdownOutput() on the socket, the stream will throw 212 * an IOException. 213 * 214 * @exception IOException if an I/O error occurs when shutting down this 215 * socket. 216 * @see java.net.Socket#shutdownInput() 217 * @see java.net.Socket#close() 218 * @see java.net.Socket#setSoLinger(boolean, int) 219 * @since 1.3 220 */ 221 protected void shutdownOutput() throws IOException { 222 throw new IOException("Method not implemented!"); 223 } 224 225 /** 226 * Returns the value of this socket's {@code fd} field. 227 * 228 * @return the value of this socket's {@code fd} field. 229 * @see java.net.SocketImpl#fd 230 */ 231 protected FileDescriptor getFileDescriptor() { 232 return fd; 233 } 234 235 /** 236 * Returns the value of this socket's {@code address} field. 237 * 238 * @return the value of this socket's {@code address} field. 239 * @see java.net.SocketImpl#address 240 */ 241 protected InetAddress getInetAddress() { 242 return address; 243 } 244 245 /** 246 * Returns the value of this socket's {@code port} field. 247 * 248 * @return the value of this socket's {@code port} field. 249 * @see java.net.SocketImpl#port 250 */ 251 protected int getPort() { 252 return port; 253 } 254 255 /** 256 * Returns whether or not this SocketImpl supports sending 257 * urgent data. By default, false is returned 258 * unless the method is overridden in a sub-class 259 * 260 * @return true if urgent data supported 261 * @see java.net.SocketImpl#address 262 * @since 1.4 263 */ 264 protected boolean supportsUrgentData () { 265 return false; // must be overridden in sub-class 266 } 267 268 /** 269 * Send one byte of urgent data on the socket. 270 * The byte to be sent is the low eight bits of the parameter 271 * @param data The byte of data to send 272 * @exception IOException if there is an error 273 * sending the data. 274 * @since 1.4 275 */ 276 protected abstract void sendUrgentData (int data) throws IOException; 277 278 /** 279 * Returns the value of this socket's {@code localport} field. 280 * 281 * @return the value of this socket's {@code localport} field. 282 * @see java.net.SocketImpl#localport 283 */ 284 protected int getLocalPort() { 285 return localport; 286 } 287 288 void setSocket(Socket soc) { 289 String implName = this.getClass().getName(); 290 if (!implName.equals("jdk.internal.net.rdma.RdmaSocketImpl")) 291 this.socket = soc; 292 else 293 rsiAccess.setSocket(this, soc); 294 } 295 296 Socket getSocket() { 297 return socket; 298 } 299 300 void setServerSocket(ServerSocket soc) { 301 String implName = this.getClass().getName(); 302 if (!implName.equals("jdk.internal.net.rdma.RdmaSocketImpl")) 303 this.serverSocket = soc; 304 else 305 rsiAccess.setServerSocket(this, soc); 306 } 307 308 ServerSocket getServerSocket() { 309 return serverSocket; 310 } 311 312 /** 313 * Returns the address and port of this socket as a {@code String}. 314 * 315 * @return a string representation of this socket. 316 */ 317 public String toString() { 318 return "Socket[addr=" + getInetAddress() + 319 ",port=" + getPort() + ",localport=" + getLocalPort() + "]"; 320 } 321 322 void reset() throws IOException { 323 address = null; 324 port = 0; 325 localport = 0; 326 } 327 328 /** 329 * Sets performance preferences for this socket. 330 * 331 * <p> Sockets use the TCP/IP protocol by default. Some implementations 332 * may offer alternative protocols which have different performance 333 * characteristics than TCP/IP. This method allows the application to 334 * express its own preferences as to how these tradeoffs should be made 335 * when the implementation chooses from the available protocols. 336 * 337 * <p> Performance preferences are described by three integers 338 * whose values indicate the relative importance of short connection time, 339 * low latency, and high bandwidth. The absolute values of the integers 340 * are irrelevant; in order to choose a protocol the values are simply 341 * compared, with larger values indicating stronger preferences. Negative 342 * values represent a lower priority than positive values. If the 343 * application prefers short connection time over both low latency and high 344 * bandwidth, for example, then it could invoke this method with the values 345 * {@code (1, 0, 0)}. If the application prefers high bandwidth above low 346 * latency, and low latency above short connection time, then it could 347 * invoke this method with the values {@code (0, 1, 2)}. 348 * 349 * By default, this method does nothing, unless it is overridden in 350 * a sub-class. 351 * 352 * @param connectionTime 353 * An {@code int} expressing the relative importance of a short 354 * connection time 355 * 356 * @param latency 357 * An {@code int} expressing the relative importance of low 358 * latency 359 * 360 * @param bandwidth 361 * An {@code int} expressing the relative importance of high 362 * bandwidth 363 * 364 * @since 1.5 365 */ 366 protected void setPerformancePreferences(int connectionTime, 367 int latency, 368 int bandwidth) 369 { 370 /* Not implemented yet */ 371 } 372 373 /** 374 * Called to set a socket option. 375 * 376 * @param <T> The type of the socket option value 377 * @param name The socket option 378 * 379 * @param value The value of the socket option. A value of {@code null} 380 * may be valid for some options. 381 * 382 * @throws UnsupportedOperationException if the SocketImpl does not 383 * support the option 384 * 385 * @throws IOException if an I/O error occurs, or if the socket is closed. 386 * 387 * @since 9 388 */ 389 protected <T> void setOption(SocketOption<T> name, T value) throws IOException { 390 if (name == StandardSocketOptions.SO_KEEPALIVE && 391 (getSocket() != null)) { 392 setOption(SocketOptions.SO_KEEPALIVE, value); 393 } else if (name == StandardSocketOptions.SO_SNDBUF && 394 (getSocket() != null)) { 395 setOption(SocketOptions.SO_SNDBUF, value); 396 } else if (name == StandardSocketOptions.SO_RCVBUF) { 397 setOption(SocketOptions.SO_RCVBUF, value); 398 } else if (name == StandardSocketOptions.SO_REUSEADDR) { 399 setOption(SocketOptions.SO_REUSEADDR, value); 400 } else if (name == StandardSocketOptions.SO_REUSEPORT && 401 supportedOptions().contains(name)) { 402 setOption(SocketOptions.SO_REUSEPORT, value); 403 } else if (name == StandardSocketOptions.SO_LINGER && 404 (getSocket() != null)) { 405 setOption(SocketOptions.SO_LINGER, value); 406 } else if (name == StandardSocketOptions.IP_TOS) { 407 setOption(SocketOptions.IP_TOS, value); 408 } else if (name == StandardSocketOptions.TCP_NODELAY && 409 (getSocket() != null)) { 410 setOption(SocketOptions.TCP_NODELAY, value); 411 } else { 412 throw new UnsupportedOperationException("unsupported option"); 413 } 414 } 415 416 /** 417 * Called to get a socket option. 418 * 419 * @param <T> The type of the socket option value 420 * @param name The socket option 421 * 422 * @return the value of the named option 423 * 424 * @throws UnsupportedOperationException if the SocketImpl does not 425 * support the option. 426 * 427 * @throws IOException if an I/O error occurs, or if the socket is closed. 428 * 429 * @since 9 430 */ 431 @SuppressWarnings("unchecked") 432 protected <T> T getOption(SocketOption<T> name) throws IOException { 433 if (name == StandardSocketOptions.SO_KEEPALIVE && 434 (getSocket() != null)) { 435 return (T)getOption(SocketOptions.SO_KEEPALIVE); 436 } else if (name == StandardSocketOptions.SO_SNDBUF && 437 (getSocket() != null)) { 438 return (T)getOption(SocketOptions.SO_SNDBUF); 439 } else if (name == StandardSocketOptions.SO_RCVBUF) { 440 return (T)getOption(SocketOptions.SO_RCVBUF); 441 } else if (name == StandardSocketOptions.SO_REUSEADDR) { 442 return (T)getOption(SocketOptions.SO_REUSEADDR); 443 } else if (name == StandardSocketOptions.SO_REUSEPORT && 444 supportedOptions().contains(name)) { 445 return (T)getOption(SocketOptions.SO_REUSEPORT); 446 } else if (name == StandardSocketOptions.SO_LINGER && 447 (getSocket() != null)) { 448 return (T)getOption(SocketOptions.SO_LINGER); 449 } else if (name == StandardSocketOptions.IP_TOS) { 450 return (T)getOption(SocketOptions.IP_TOS); 451 } else if (name == StandardSocketOptions.TCP_NODELAY && 452 (getSocket() != null)) { 453 return (T)getOption(SocketOptions.TCP_NODELAY); 454 } else { 455 throw new UnsupportedOperationException("unsupported option"); 456 } 457 } 458 459 private static final Set<SocketOption<?>> socketOptions; 460 461 private static final Set<SocketOption<?>> serverSocketOptions; 462 463 static { 464 socketOptions = Set.of(StandardSocketOptions.SO_KEEPALIVE, 465 StandardSocketOptions.SO_SNDBUF, 466 StandardSocketOptions.SO_RCVBUF, 467 StandardSocketOptions.SO_REUSEADDR, 468 StandardSocketOptions.SO_LINGER, 469 StandardSocketOptions.IP_TOS, 470 StandardSocketOptions.TCP_NODELAY); 471 472 serverSocketOptions = Set.of(StandardSocketOptions.SO_RCVBUF, 473 StandardSocketOptions.SO_REUSEADDR, 474 StandardSocketOptions.IP_TOS); 475 } 476 477 /** 478 * Returns a set of SocketOptions supported by this impl 479 * and by this impl's socket (Socket or ServerSocket) 480 * 481 * @return a Set of SocketOptions 482 * 483 * @since 9 484 */ 485 protected Set<SocketOption<?>> supportedOptions() { 486 if (getSocket() != null) { 487 return socketOptions; 488 } else { 489 return serverSocketOptions; 490 } 491 } 492 }