Java Code Examples for org.apache.tomcat.jni.Socket#optSet()

The following examples show how to use org.apache.tomcat.jni.Socket#optSet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UpgradeAprProcessor.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public int read(boolean block, byte[] bytes, int off, int len)
        throws IOException {
    if (!block) {
        Socket.optSet(socket, Socket.APR_SO_NONBLOCK, -1);
    }
    try {
        int result = Socket.recv(socket, bytes, off, len);
        if (result > 0) {
            return result;
        } else if (-result == Status.EAGAIN) {
            return 0;
        } else {
            throw new IOException(sm.getString("apr.read.error",
                    Integer.valueOf(-result)));
        }
    } finally {
        if (!block) {
            Socket.optSet(socket, Socket.APR_SO_NONBLOCK, 0);
        }
    }
}
 
Example 2
Source File: UpgradeAprProcessor.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public int read(boolean block, byte[] bytes, int off, int len)
        throws IOException {
    if (!block) {
        Socket.optSet(socket, Socket.APR_SO_NONBLOCK, -1);
    }
    try {
        int result = Socket.recv(socket, bytes, off, len);
        if (result > 0) {
            return result;
        } else if (-result == Status.EAGAIN) {
            return 0;
        } else {
            throw new IOException(sm.getString("apr.read.error",
                    Integer.valueOf(-result)));
        }
    } finally {
        if (!block) {
            Socket.optSet(socket, Socket.APR_SO_NONBLOCK, 0);
        }
    }
}
 
Example 3
Source File: AprEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Process the specified connection.
 */
protected boolean setSocketOptions(long socket) {
    // Process the connection
    int step = 1;
    try {

        // 1: Set socket options: timeout, linger, etc
        if (socketProperties.getSoLingerOn() && socketProperties.getSoLingerTime() >= 0)
            Socket.optSet(socket, Socket.APR_SO_LINGER, socketProperties.getSoLingerTime());
        if (socketProperties.getTcpNoDelay())
            Socket.optSet(socket, Socket.APR_TCP_NODELAY, (socketProperties.getTcpNoDelay() ? 1 : 0));
        Socket.timeoutSet(socket, socketProperties.getSoTimeout() * 1000);

        // 2: SSL handshake
        step = 2;
        if (sslContext != 0) {
            SSLSocket.attach(sslContext, socket);
            if (SSLSocket.handshake(socket) != 0) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("endpoint.err.handshake") + ": " + SSL.getLastError());
                }
                return false;
            }
        }

    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        if (log.isDebugEnabled()) {
            if (step == 2) {
                log.debug(sm.getString("endpoint.err.handshake"), t);
            } else {
                log.debug(sm.getString("endpoint.err.unexpected"), t);
            }
        }
        // Tell to close the socket
        return false;
    }
    return true;
}
 
Example 4
Source File: AprEndpoint.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Process the specified connection.
 */
protected boolean setSocketOptions(long socket) {
    // Process the connection
    int step = 1;
    try {

        // 1: Set socket options: timeout, linger, etc
        if (socketProperties.getSoLingerOn() && socketProperties.getSoLingerTime() >= 0)
            Socket.optSet(socket, Socket.APR_SO_LINGER, socketProperties.getSoLingerTime());
        if (socketProperties.getTcpNoDelay())
            Socket.optSet(socket, Socket.APR_TCP_NODELAY, (socketProperties.getTcpNoDelay() ? 1 : 0));
        Socket.timeoutSet(socket, socketProperties.getSoTimeout() * 1000);

        // 2: SSL handshake
        step = 2;
        if (sslContext != 0) {
            SSLSocket.attach(sslContext, socket);
            if (SSLSocket.handshake(socket) != 0) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("endpoint.err.handshake") + ": " + SSL.getLastError());
                }
                return false;
            }
        }

    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        if (log.isDebugEnabled()) {
            if (step == 2) {
                log.debug(sm.getString("endpoint.err.handshake"), t);
            } else {
                log.debug(sm.getString("endpoint.err.unexpected"), t);
            }
        }
        // Tell to close the socket
        return false;
    }
    return true;
}
 
Example 5
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Process the specified connection.
 * @param socketWrapper The socket wrapper
 * @return <code>true</code> if the socket was correctly configured
 *  and processing may continue, <code>false</code> if the socket needs to be
 *  close immediately
 */
protected boolean setSocketOptions(SocketWrapperBase<Long> socketWrapper) {
    long socket = socketWrapper.getSocket().longValue();
    // Process the connection
    int step = 1;
    try {

        // 1: Set socket options: timeout, linger, etc
        if (socketProperties.getSoLingerOn() && socketProperties.getSoLingerTime() >= 0)
            Socket.optSet(socket, Socket.APR_SO_LINGER, socketProperties.getSoLingerTime());
        if (socketProperties.getTcpNoDelay())
            Socket.optSet(socket, Socket.APR_TCP_NODELAY, (socketProperties.getTcpNoDelay() ? 1 : 0));
        Socket.timeoutSet(socket, socketProperties.getSoTimeout() * 1000);

        // 2: SSL handshake
        step = 2;
        if (sslContext != 0) {
            SSLSocket.attach(sslContext, socket);
            if (SSLSocket.handshake(socket) != 0) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("endpoint.err.handshake") + ": " + SSL.getLastError());
                }
                return false;
            }

            if (negotiableProtocols.size() > 0) {
                byte[] negotiated = new byte[256];
                int len = SSLSocket.getALPN(socket, negotiated);
                String negotiatedProtocol =
                        new String(negotiated, 0, len, StandardCharsets.UTF_8);
                if (negotiatedProtocol.length() > 0) {
                    socketWrapper.setNegotiatedProtocol(negotiatedProtocol);
                    if (log.isDebugEnabled()) {
                        log.debug(sm.getString("endpoint.alpn.negotiated", negotiatedProtocol));
                    }
                }
            }
        }
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        if (log.isDebugEnabled()) {
            if (step == 2) {
                log.debug(sm.getString("endpoint.err.handshake"), t);
            } else {
                log.debug(sm.getString("endpoint.err.unexpected"), t);
            }
        }
        // Tell to close the socket
        return false;
    }
    return true;
}
 
Example 6
Source File: TestXxxEndpoint.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private long createAprSocket(int port, long pool)
             throws Exception {
    /**
     * Server socket "pointer".
     */
    long serverSock = 0;

    String address = InetAddress.getByName("localhost").getHostAddress();

    // Create the APR address that will be bound
    int family = Socket.APR_INET;
    if (Library.APR_HAVE_IPV6) {
        if (!OS.IS_BSD && !OS.IS_WIN32 && !OS.IS_WIN64)
            family = Socket.APR_UNSPEC;
     }

    long inetAddress = 0;
    try {
        inetAddress = Address.info(address, family,
                                   port, 0, pool);
        // Create the APR server socket
        serverSock = Socket.create(Address.getInfo(inetAddress).family,
                                   Socket.SOCK_STREAM,
                                   Socket.APR_PROTO_TCP, pool);
    } catch (Exception ex) {
        log.error("Could not create socket for address '" + address + "'");
        return 0;
    }

    if (OS.IS_UNIX) {
        Socket.optSet(serverSock, Socket.APR_SO_REUSEADDR, 1);
    }
    // Deal with the firewalls that tend to drop the inactive sockets
    Socket.optSet(serverSock, Socket.APR_SO_KEEPALIVE, 1);
    // Bind the server socket
    int ret = Socket.bind(serverSock, inetAddress);
    if (ret != 0) {
        log.error("Could not bind: " + Error.strerror(ret));
        throw (new Exception(Error.strerror(ret)));
    }
    return serverSock;
}
 
Example 7
Source File: AprServletInputStream.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
protected int doRead(boolean block, byte[] b, int off, int len)
        throws IOException {

    if (closed) {
        throw new IOException(sm.getString("apr.closed", Long.valueOf(socket)));
    }

    Lock readLock = wrapper.getBlockingStatusReadLock();
    WriteLock writeLock = wrapper.getBlockingStatusWriteLock();

    boolean readDone = false;
    int result = 0;
    try {
        readLock.lock();
        if (wrapper.getBlockingStatus() == block) {
            result = Socket.recv(socket, b, off, len);
            readDone = true;
        }
    } finally {
        readLock.unlock();
    }

    if (!readDone) {
        try {
            writeLock.lock();
            wrapper.setBlockingStatus(block);
            // Set the current settings for this socket
            Socket.optSet(socket, Socket.APR_SO_NONBLOCK, (block ? 0 : 1));
            // Downgrade the lock
            try {
                readLock.lock();
                writeLock.unlock();
                result = Socket.recv(socket, b, off, len);
            } finally {
                readLock.unlock();
            }
        } finally {
            // Should have been released above but may not have been on some
            // exception paths
            if (writeLock.isHeldByCurrentThread()) {
                writeLock.unlock();
            }
        }
    }

    if (result > 0) {
        eagain = false;
        return result;
    } else if (-result == Status.EAGAIN) {
        eagain = true;
        return 0;
    } else if (-result == Status.APR_EGENERAL && wrapper.isSecure()) {
        // Not entirely sure why this is necessary. Testing to date has not
        // identified any issues with this but log it so it can be tracked
        // if it is suspected of causing issues in the future.
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("apr.read.sslGeneralError",
                    Long.valueOf(socket), wrapper));
        }
        eagain = true;
        return 0;
    } else if (-result == Status.APR_EOF) {
        throw new EOFException(sm.getString("apr.clientAbort"));
    } else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
            (-result == Status.APR_OS_START_SYSERR + 10053)) {
        // 10053 on Windows is connection aborted
        throw new EOFException(sm.getString("apr.clientAbort"));
    } else {
        throw new IOException(sm.getString("apr.read.error",
                Integer.valueOf(-result), Long.valueOf(socket), wrapper));
    }
}
 
Example 8
Source File: TestXxxEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private long createAprSocket(int port, long pool)
             throws Exception {
    /**
     * Server socket "pointer".
     */
    long serverSock = 0;

    String address = InetAddress.getByName("localhost").getHostAddress();

    // Create the APR address that will be bound
    int family = Socket.APR_INET;
    if (Library.APR_HAVE_IPV6) {
        if (!OS.IS_BSD && !OS.IS_WIN32 && !OS.IS_WIN64)
            family = Socket.APR_UNSPEC;
     }

    long inetAddress = 0;
    try {
        inetAddress = Address.info(address, family,
                                   port, 0, pool);
        // Create the APR server socket
        serverSock = Socket.create(Address.getInfo(inetAddress).family,
                                   Socket.SOCK_STREAM,
                                   Socket.APR_PROTO_TCP, pool);
    } catch (Exception ex) {
        log.error("Could not create socket for address '" + address + "'");
        return 0;
    }

    if (OS.IS_UNIX) {
        Socket.optSet(serverSock, Socket.APR_SO_REUSEADDR, 1);
    }
    // Deal with the firewalls that tend to drop the inactive sockets
    Socket.optSet(serverSock, Socket.APR_SO_KEEPALIVE, 1);
    // Bind the server socket
    int ret = Socket.bind(serverSock, inetAddress);
    if (ret != 0) {
        log.error("Could not bind: " + Error.strerror(ret));
        throw (new Exception(Error.strerror(ret)));
    }
    return serverSock;
}
 
Example 9
Source File: AprServletInputStream.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
protected int doRead(boolean block, byte[] b, int off, int len)
        throws IOException {

    if (closed) {
        throw new IOException(sm.getString("apr.closed", Long.valueOf(socket)));
    }

    Lock readLock = wrapper.getBlockingStatusReadLock();
    WriteLock writeLock = wrapper.getBlockingStatusWriteLock();

    boolean readDone = false;
    int result = 0;
    try {
        readLock.lock();
        if (wrapper.getBlockingStatus() == block) {
            result = Socket.recv(socket, b, off, len);
            readDone = true;
        }
    } finally {
        readLock.unlock();
    }

    if (!readDone) {
        try {
            writeLock.lock();
            wrapper.setBlockingStatus(block);
            // Set the current settings for this socket
            Socket.optSet(socket, Socket.APR_SO_NONBLOCK, (block ? 0 : 1));
            // Downgrade the lock
            try {
                readLock.lock();
                writeLock.unlock();
                result = Socket.recv(socket, b, off, len);
            } finally {
                readLock.unlock();
            }
        } finally {
            // Should have been released above but may not have been on some
            // exception paths
            if (writeLock.isHeldByCurrentThread()) {
                writeLock.unlock();
            }
        }
    }

    if (result > 0) {
        eagain = false;
        return result;
    } else if (-result == Status.EAGAIN) {
        eagain = true;
        return 0;
    } else if (-result == Status.APR_EGENERAL && wrapper.isSecure()) {
        // Not entirely sure why this is necessary. Testing to date has not
        // identified any issues with this but log it so it can be tracked
        // if it is suspected of causing issues in the future.
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("apr.read.sslGeneralError",
                    Long.valueOf(socket), wrapper));
        }
        eagain = true;
        return 0;
    } else if (-result == Status.APR_EOF) {
        throw new EOFException(sm.getString("apr.clientAbort"));
    } else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
            (-result == Status.APR_OS_START_SYSERR + 10053)) {
        // 10053 on Windows is connection aborted
        throw new EOFException(sm.getString("apr.clientAbort"));
    } else {
        throw new IOException(sm.getString("apr.read.error",
                Integer.valueOf(-result), Long.valueOf(socket), wrapper));
    }
}
 
Example 10
Source File: TestXxxEndpoint.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private long createAprSocket(int port, long pool)
             throws Exception {
    /**
     * Server socket "pointer".
     */
    long serverSock = 0;

    String address = InetAddress.getByName("localhost").getHostAddress();

    // Create the APR address that will be bound
    int family = Socket.APR_INET;
    if (Library.APR_HAVE_IPV6) {
        if (!OS.IS_BSD && !OS.IS_WIN32 && !OS.IS_WIN64)
            family = Socket.APR_UNSPEC;
     }

    long inetAddress = 0;
    try {
        inetAddress = Address.info(address, family,
                                   port, 0, pool);
        // Create the APR server socket
        serverSock = Socket.create(Address.getInfo(inetAddress).family,
                                   Socket.SOCK_STREAM,
                                   Socket.APR_PROTO_TCP, pool);
    } catch (Exception ex) {
        log.error("Could not create socket for address '" + address + "'");
        return 0;
    }

    if (OS.IS_UNIX) {
        Socket.optSet(serverSock, Socket.APR_SO_REUSEADDR, 1);
    }
    // Deal with the firewalls that tend to drop the inactive sockets
    Socket.optSet(serverSock, Socket.APR_SO_KEEPALIVE, 1);
    // Bind the server socket
    int ret = Socket.bind(serverSock, inetAddress);
    if (ret != 0) {
        log.error("Could not bind: " + Error.strerror(ret));
        throw (new Exception(Error.strerror(ret)));
    }
    return serverSock;
}