< prev index next >

src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java

Print this page




  26 package java.net;
  27 
  28 import java.io.FileDescriptor;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.io.OutputStream;
  32 
  33 import java.security.AccessController;
  34 import java.security.PrivilegedActionException;
  35 import java.security.PrivilegedExceptionAction;
  36 import java.util.Collections;
  37 import java.util.HashSet;
  38 import java.util.Objects;
  39 import java.util.Set;
  40 
  41 import sun.net.ConnectionResetException;
  42 import sun.net.NetHooks;
  43 import sun.net.PlatformSocketImpl;
  44 import sun.net.ResourceManager;
  45 import sun.net.ext.ExtendedSocketOptions;
  46 import sun.net.util.IPAddressUtil;
  47 import sun.net.util.SocketExceptions;
  48 
  49 /**
  50  * Default Socket Implementation. This implementation does
  51  * not implement any security checks.
  52  * Note this class should <b>NOT</b> be public.
  53  *
  54  * @author  Steven B. Byrne
  55  */
  56 abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSocketImpl {
  57     /* instance variable for SO_TIMEOUT */
  58     int timeout;   // timeout in millisec
  59     // traffic class
  60     private int trafficClass;
  61 
  62     private boolean shut_rd = false;
  63     private boolean shut_wr = false;
  64 
  65     private SocketInputStream socketInputStream = null;
  66     private SocketOutputStream socketOutputStream = null;


 141             }
 142         } else {
 143             fd = new FileDescriptor();
 144             socketCreate(true);
 145             SocketCleanable.register(fd);
 146         }
 147     }
 148 
 149     /**
 150      * Creates a socket and connects it to the specified port on
 151      * the specified host.
 152      * @param host the specified host
 153      * @param port the specified port
 154      */
 155     protected void connect(String host, int port)
 156         throws UnknownHostException, IOException
 157     {
 158         boolean connected = false;
 159         try {
 160             InetAddress address = InetAddress.getByName(host);
 161             // recording this.address as supplied by caller before calling connect
 162             this.address = address;
 163             this.port = port;
 164             if (address.isLinkLocalAddress()) {
 165                 address = IPAddressUtil.toScopedAddress(address);
 166             }
 167 
 168             connectToAddress(address, port, timeout);
 169             connected = true;
 170         } finally {
 171             if (!connected) {
 172                 try {
 173                     close();
 174                 } catch (IOException ioe) {
 175                     /* Do nothing. If connect threw an exception then
 176                        it will be passed up the call stack */
 177                 }
 178             }
 179             isConnected = connected;
 180         }
 181     }
 182 
 183     /**
 184      * Creates a socket and connects it to the specified address on
 185      * the specified port.
 186      * @param address the address
 187      * @param port the specified port
 188      */
 189     protected void connect(InetAddress address, int port) throws IOException {
 190         // recording this.address as supplied by caller before calling connect
 191         this.address = address;
 192         this.port = port;
 193         if (address.isLinkLocalAddress()) {
 194             address = IPAddressUtil.toScopedAddress(address);
 195         }
 196 
 197         try {
 198             connectToAddress(address, port, timeout);
 199             isConnected = true;
 200             return;
 201         } catch (IOException e) {
 202             // everything failed
 203             close();
 204             throw e;
 205         }
 206     }
 207 
 208     /**
 209      * Creates a socket and connects it to the specified address on
 210      * the specified port.
 211      * @param address the address
 212      * @param timeout the timeout value in milliseconds, or zero for no timeout.
 213      * @throws IOException if connection fails
 214      * @throws  IllegalArgumentException if address is null or is a
 215      *          SocketAddress subclass not supported by this socket
 216      * @since 1.4
 217      */
 218     protected void connect(SocketAddress address, int timeout)
 219             throws IOException {
 220         boolean connected = false;
 221         try {
 222             if (address == null || !(address instanceof InetSocketAddress))
 223                 throw new IllegalArgumentException("unsupported address type");
 224             InetSocketAddress addr = (InetSocketAddress) address;
 225             if (addr.isUnresolved())
 226                 throw new UnknownHostException(addr.getHostName());
 227             // recording this.address as supplied by caller before calling connect
 228             InetAddress ia = addr.getAddress();
 229             this.address = ia;
 230             this.port = addr.getPort();
 231             if (ia.isLinkLocalAddress()) {
 232                 ia = IPAddressUtil.toScopedAddress(ia);
 233             }
 234             connectToAddress(ia, port, timeout);
 235             connected = true;
 236         } finally {
 237             if (!connected) {
 238                 try {
 239                     close();
 240                 } catch (IOException ioe) {
 241                     /* Do nothing. If connect threw an exception then
 242                        it will be passed up the call stack */
 243                 }
 244             }
 245             isConnected = connected;
 246         }
 247     }
 248 
 249     private void connectToAddress(InetAddress address, int port, int timeout) throws IOException {
 250         if (address.isAnyLocalAddress()) {
 251             doConnect(InetAddress.getLocalHost(), port, timeout);
 252         } else {
 253             doConnect(address, port, timeout);
 254         }


 442             throw new IllegalArgumentException("Invalid value '" + value + "'");
 443 
 444         if (isClosedOrPending())
 445             throw new SocketException("Socket closed");
 446 
 447         if (name == StandardSocketOptions.SO_KEEPALIVE) {
 448             setOption(SocketOptions.SO_KEEPALIVE, value);
 449         } else if (name == StandardSocketOptions.SO_SNDBUF) {
 450             if (((Integer)value).intValue() < 0)
 451                 throw new IllegalArgumentException("Invalid send buffer size:" + value);
 452             setOption(SocketOptions.SO_SNDBUF, value);
 453         } else if (name == StandardSocketOptions.SO_RCVBUF) {
 454             if (((Integer)value).intValue() < 0)
 455                 throw new IllegalArgumentException("Invalid recv buffer size:" + value);
 456             setOption(SocketOptions.SO_RCVBUF, value);
 457         } else if (name == StandardSocketOptions.SO_REUSEADDR) {
 458             setOption(SocketOptions.SO_REUSEADDR, value);
 459         } else if (name == StandardSocketOptions.SO_REUSEPORT) {
 460             setOption(SocketOptions.SO_REUSEPORT, value);
 461         } else if (name == StandardSocketOptions.SO_LINGER ) {
 462             if (((Integer)value).intValue() < 0)
 463                 setOption(SocketOptions.SO_LINGER, false);
 464             else
 465                 setOption(SocketOptions.SO_LINGER, value);
 466         } else if (name == StandardSocketOptions.IP_TOS) {
 467             int i = ((Integer)value).intValue();
 468             if (i < 0 || i > 255)
 469                 throw new IllegalArgumentException("Invalid IP_TOS value: " + value);
 470             setOption(SocketOptions.IP_TOS, value);
 471         } else if (name == StandardSocketOptions.TCP_NODELAY) {
 472             setOption(SocketOptions.TCP_NODELAY, value);
 473         } else if (extendedOptions.isOptionSupported(name)) {
 474             extendedOptions.setOption(fd, name, value);
 475         } else {
 476             throw new AssertionError("unknown option: " + name);
 477         }
 478     }
 479 
 480     @Override
 481     @SuppressWarnings("unchecked")
 482     protected <T> T getOption(SocketOption<T> name) throws IOException {
 483         Objects.requireNonNull(name);
 484         if (!supportedOptions().contains(name))
 485             throw new UnsupportedOperationException("'" + name + "' not supported");
 486 
 487         if (isClosedOrPending())
 488             throw new SocketException("Socket closed");
 489 
 490         if (name == StandardSocketOptions.SO_KEEPALIVE) {
 491             return (T)getOption(SocketOptions.SO_KEEPALIVE);
 492         } else if (name == StandardSocketOptions.SO_SNDBUF) {
 493             return (T)getOption(SocketOptions.SO_SNDBUF);
 494         } else if (name == StandardSocketOptions.SO_RCVBUF) {
 495             return (T)getOption(SocketOptions.SO_RCVBUF);
 496         } else if (name == StandardSocketOptions.SO_REUSEADDR) {
 497             return (T)getOption(SocketOptions.SO_REUSEADDR);
 498         } else if (name == StandardSocketOptions.SO_REUSEPORT) {
 499             return (T)getOption(SocketOptions.SO_REUSEPORT);
 500         } else if (name == StandardSocketOptions.SO_LINGER) {
 501             Object value = getOption(SocketOptions.SO_LINGER);
 502             if (value instanceof Boolean) {
 503                 assert ((Boolean)value).booleanValue() == false;
 504                 value = -1;
 505             }
 506             return (T)value;
 507         } else if (name == StandardSocketOptions.IP_TOS) {
 508             return (T)getOption(SocketOptions.IP_TOS);
 509         } else if (name == StandardSocketOptions.TCP_NODELAY) {
 510             return (T)getOption(SocketOptions.TCP_NODELAY);
 511         } else if (extendedOptions.isOptionSupported(name)) {
 512             return (T) extendedOptions.getOption(fd, name);
 513         } else {
 514             throw new AssertionError("unknown option: " + name);
 515         }
 516     }
 517 
 518     /**
 519      * The workhorse of the connection operation.  Tries several times to
 520      * establish a connection to the given <host, port>.  If unsuccessful,
 521      * throws an IOException indicating what went wrong.
 522      */
 523 
 524     synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException {
 525         synchronized (fdLock) {
 526             if (!closePending && !isBound) {


 541                 releaseFD();
 542             }
 543         } catch (IOException e) {
 544             close();
 545             throw SocketExceptions.of(e, new InetSocketAddress(address, port));
 546         }
 547     }
 548 
 549     /**
 550      * Binds the socket to the specified address of the specified local port.
 551      * @param address the address
 552      * @param lport the port
 553      */
 554     protected synchronized void bind(InetAddress address, int lport)
 555         throws IOException
 556     {
 557        synchronized (fdLock) {
 558             if (!closePending && !isBound) {
 559                 NetHooks.beforeTcpBind(fd, address, lport);
 560             }
 561         }
 562         if (address.isLinkLocalAddress()) {
 563             address = IPAddressUtil.toScopedAddress(address);
 564         }
 565         socketBind(address, lport);
 566         isBound = true;
 567     }
 568 
 569     /**
 570      * Listens, for a specified amount of time, for connections.
 571      * @param count the amount of time to listen for connections
 572      */
 573     protected synchronized void listen(int count) throws IOException {
 574         socketListen(count);
 575     }
 576 
 577     /**
 578      * Accepts connections.
 579      * @param si the socket impl
 580      */
 581     protected void accept(SocketImpl si) throws IOException {
 582         si.fd = new FileDescriptor();
 583         acquireFD();




  26 package java.net;
  27 
  28 import java.io.FileDescriptor;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.io.OutputStream;
  32 
  33 import java.security.AccessController;
  34 import java.security.PrivilegedActionException;
  35 import java.security.PrivilegedExceptionAction;
  36 import java.util.Collections;
  37 import java.util.HashSet;
  38 import java.util.Objects;
  39 import java.util.Set;
  40 
  41 import sun.net.ConnectionResetException;
  42 import sun.net.NetHooks;
  43 import sun.net.PlatformSocketImpl;
  44 import sun.net.ResourceManager;
  45 import sun.net.ext.ExtendedSocketOptions;

  46 import sun.net.util.SocketExceptions;
  47 
  48 /**
  49  * Default Socket Implementation. This implementation does
  50  * not implement any security checks.
  51  * Note this class should <b>NOT</b> be public.
  52  *
  53  * @author  Steven B. Byrne
  54  */
  55 abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSocketImpl {
  56     /* instance variable for SO_TIMEOUT */
  57     int timeout;   // timeout in millisec
  58     // traffic class
  59     private int trafficClass;
  60 
  61     private boolean shut_rd = false;
  62     private boolean shut_wr = false;
  63 
  64     private SocketInputStream socketInputStream = null;
  65     private SocketOutputStream socketOutputStream = null;


 140             }
 141         } else {
 142             fd = new FileDescriptor();
 143             socketCreate(true);
 144             SocketCleanable.register(fd);
 145         }
 146     }
 147 
 148     /**
 149      * Creates a socket and connects it to the specified port on
 150      * the specified host.
 151      * @param host the specified host
 152      * @param port the specified port
 153      */
 154     protected void connect(String host, int port)
 155         throws UnknownHostException, IOException
 156     {
 157         boolean connected = false;
 158         try {
 159             InetAddress address = InetAddress.getByName(host);


 160             this.port = port;
 161             this.address = address;


 162 
 163             connectToAddress(address, port, timeout);
 164             connected = true;
 165         } finally {
 166             if (!connected) {
 167                 try {
 168                     close();
 169                 } catch (IOException ioe) {
 170                     /* Do nothing. If connect threw an exception then
 171                        it will be passed up the call stack */
 172                 }
 173             }
 174             isConnected = connected;
 175         }
 176     }
 177 
 178     /**
 179      * Creates a socket and connects it to the specified address on
 180      * the specified port.
 181      * @param address the address
 182      * @param port the specified port
 183      */
 184     protected void connect(InetAddress address, int port) throws IOException {


 185         this.port = port;
 186         this.address = address;


 187 
 188         try {
 189             connectToAddress(address, port, timeout);
 190             isConnected = true;
 191             return;
 192         } catch (IOException e) {
 193             // everything failed
 194             close();
 195             throw e;
 196         }
 197     }
 198 
 199     /**
 200      * Creates a socket and connects it to the specified address on
 201      * the specified port.
 202      * @param address the address
 203      * @param timeout the timeout value in milliseconds, or zero for no timeout.
 204      * @throws IOException if connection fails
 205      * @throws  IllegalArgumentException if address is null or is a
 206      *          SocketAddress subclass not supported by this socket
 207      * @since 1.4
 208      */
 209     protected void connect(SocketAddress address, int timeout)
 210             throws IOException {
 211         boolean connected = false;
 212         try {
 213             if (address == null || !(address instanceof InetSocketAddress))
 214                 throw new IllegalArgumentException("unsupported address type");
 215             InetSocketAddress addr = (InetSocketAddress) address;
 216             if (addr.isUnresolved())
 217                 throw new UnknownHostException(addr.getHostName());



 218             this.port = addr.getPort();
 219             this.address = addr.getAddress();
 220 
 221             connectToAddress(this.address, port, timeout);

 222             connected = true;
 223         } finally {
 224             if (!connected) {
 225                 try {
 226                     close();
 227                 } catch (IOException ioe) {
 228                     /* Do nothing. If connect threw an exception then
 229                        it will be passed up the call stack */
 230                 }
 231             }
 232             isConnected = connected;
 233         }
 234     }
 235 
 236     private void connectToAddress(InetAddress address, int port, int timeout) throws IOException {
 237         if (address.isAnyLocalAddress()) {
 238             doConnect(InetAddress.getLocalHost(), port, timeout);
 239         } else {
 240             doConnect(address, port, timeout);
 241         }


 429             throw new IllegalArgumentException("Invalid value '" + value + "'");
 430 
 431         if (isClosedOrPending())
 432             throw new SocketException("Socket closed");
 433 
 434         if (name == StandardSocketOptions.SO_KEEPALIVE) {
 435             setOption(SocketOptions.SO_KEEPALIVE, value);
 436         } else if (name == StandardSocketOptions.SO_SNDBUF) {
 437             if (((Integer)value).intValue() < 0)
 438                 throw new IllegalArgumentException("Invalid send buffer size:" + value);
 439             setOption(SocketOptions.SO_SNDBUF, value);
 440         } else if (name == StandardSocketOptions.SO_RCVBUF) {
 441             if (((Integer)value).intValue() < 0)
 442                 throw new IllegalArgumentException("Invalid recv buffer size:" + value);
 443             setOption(SocketOptions.SO_RCVBUF, value);
 444         } else if (name == StandardSocketOptions.SO_REUSEADDR) {
 445             setOption(SocketOptions.SO_REUSEADDR, value);
 446         } else if (name == StandardSocketOptions.SO_REUSEPORT) {
 447             setOption(SocketOptions.SO_REUSEPORT, value);
 448         } else if (name == StandardSocketOptions.SO_LINGER ) {
 449             setOption(SocketOptions.SO_LINGER, value);



 450         } else if (name == StandardSocketOptions.IP_TOS) {
 451             int i = ((Integer)value).intValue();
 452             if (i < 0 || i > 255)
 453                 throw new IllegalArgumentException("Invalid IP_TOS value: " + value);
 454             setOption(SocketOptions.IP_TOS, value);
 455         } else if (name == StandardSocketOptions.TCP_NODELAY) {
 456             setOption(SocketOptions.TCP_NODELAY, value);
 457         } else if (extendedOptions.isOptionSupported(name)) {
 458             extendedOptions.setOption(fd, name, value);
 459         } else {
 460             throw new AssertionError("unknown option: " + name);
 461         }
 462     }
 463 
 464     @Override
 465     @SuppressWarnings("unchecked")
 466     protected <T> T getOption(SocketOption<T> name) throws IOException {
 467         Objects.requireNonNull(name);
 468         if (!supportedOptions().contains(name))
 469             throw new UnsupportedOperationException("'" + name + "' not supported");
 470 
 471         if (isClosedOrPending())
 472             throw new SocketException("Socket closed");
 473 
 474         if (name == StandardSocketOptions.SO_KEEPALIVE) {
 475             return (T)getOption(SocketOptions.SO_KEEPALIVE);
 476         } else if (name == StandardSocketOptions.SO_SNDBUF) {
 477             return (T)getOption(SocketOptions.SO_SNDBUF);
 478         } else if (name == StandardSocketOptions.SO_RCVBUF) {
 479             return (T)getOption(SocketOptions.SO_RCVBUF);
 480         } else if (name == StandardSocketOptions.SO_REUSEADDR) {
 481             return (T)getOption(SocketOptions.SO_REUSEADDR);
 482         } else if (name == StandardSocketOptions.SO_REUSEPORT) {
 483             return (T)getOption(SocketOptions.SO_REUSEPORT);
 484         } else if (name == StandardSocketOptions.SO_LINGER) {
 485             return (T)getOption(SocketOptions.SO_LINGER);





 486         } else if (name == StandardSocketOptions.IP_TOS) {
 487             return (T)getOption(SocketOptions.IP_TOS);
 488         } else if (name == StandardSocketOptions.TCP_NODELAY) {
 489             return (T)getOption(SocketOptions.TCP_NODELAY);
 490         } else if (extendedOptions.isOptionSupported(name)) {
 491             return (T) extendedOptions.getOption(fd, name);
 492         } else {
 493             throw new AssertionError("unknown option: " + name);
 494         }
 495     }
 496 
 497     /**
 498      * The workhorse of the connection operation.  Tries several times to
 499      * establish a connection to the given <host, port>.  If unsuccessful,
 500      * throws an IOException indicating what went wrong.
 501      */
 502 
 503     synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException {
 504         synchronized (fdLock) {
 505             if (!closePending && !isBound) {


 520                 releaseFD();
 521             }
 522         } catch (IOException e) {
 523             close();
 524             throw SocketExceptions.of(e, new InetSocketAddress(address, port));
 525         }
 526     }
 527 
 528     /**
 529      * Binds the socket to the specified address of the specified local port.
 530      * @param address the address
 531      * @param lport the port
 532      */
 533     protected synchronized void bind(InetAddress address, int lport)
 534         throws IOException
 535     {
 536        synchronized (fdLock) {
 537             if (!closePending && !isBound) {
 538                 NetHooks.beforeTcpBind(fd, address, lport);
 539             }



 540         }
 541         socketBind(address, lport);
 542         isBound = true;
 543     }
 544 
 545     /**
 546      * Listens, for a specified amount of time, for connections.
 547      * @param count the amount of time to listen for connections
 548      */
 549     protected synchronized void listen(int count) throws IOException {
 550         socketListen(count);
 551     }
 552 
 553     /**
 554      * Accepts connections.
 555      * @param si the socket impl
 556      */
 557     protected void accept(SocketImpl si) throws IOException {
 558         si.fd = new FileDescriptor();
 559         acquireFD();


< prev index next >