org.apache.tomcat.jni.Socket Java Examples

The following examples show how to use org.apache.tomcat.jni.Socket. 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: AprEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Port in use.
 */
@Override
public int getLocalPort() {
    long s = serverSock;
    if (s == 0) {
        return -1;
    } else {
        long sa;
        try {
            sa = Address.get(Socket.APR_LOCAL, s);
            Sockaddr addr = Address.getInfo(sa);
            return addr.port;
        } catch (Exception e) {
            return -1;
        }
    }
}
 
Example #2
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
protected void populateRemoteHost() {
    if (closed) {
        return;
    }
    try {
        long socket = getSocket().longValue();
        long sa = Address.get(Socket.APR_REMOTE, socket);
        remoteHost = Address.getnameinfo(sa, 0);
        if (remoteAddr == null) {
            remoteAddr = Address.getip(sa);
        }
    } catch (Exception e) {
        log.warn(sm.getString("endpoint.warn.noRemoteHost", getSocket()), e);
    }
}
 
Example #3
Source File: AprSocketSink.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
/**
 * Send incoming data to stream.
 */
@Override
public void handleData(ByteBuffer buf, Link link) {
    if (buf == null)
        return;

    if (socketWrapper.shutdown)
        return;

    try {
        if (verbose)
            System.out.println("[" + this + "] INFO: Writing data to stream: " + buf + ".");

        // FIXME: If pull is destroyed or socket is closed, segfault will happen here
        Socket.send(socket, buf.data, buf.offset, buf.length);
    } catch (Exception e) {
        System.err.println("[" + this + "] ERROR: " + e.getMessage());
        closeStream();
    }
}
 
Example #4
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void destroySocket(long socket) {
    connections.remove(Long.valueOf(socket));
    if (log.isDebugEnabled()) {
        String msg = sm.getString("endpoint.debug.destroySocket",
                Long.valueOf(socket));
        if (log.isTraceEnabled()) {
            log.trace(msg, new Exception());
        } else {
            log.debug(msg);
        }
    }
    // Be VERY careful if you call this method directly. If it is called
    // twice for the same socket the JVM will core. Currently this is only
    // called from Poller.closePollset() to ensure kept alive connections
    // are closed when calling stop() followed by start().
    if (socket != 0) {
        Socket.destroy(socket);
        countDownConnection();
    }
}
 
Example #5
Source File: AprEndpoint.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
private void destroySocket(long socket) {
    connections.remove(Long.valueOf(socket));
    if (log.isDebugEnabled()) {
        String msg = sm.getString("endpoint.debug.destroySocket",
                Long.valueOf(socket));
        if (log.isTraceEnabled()) {
            log.trace(msg, new Exception());
        } else {
            log.debug(msg);
        }
    }
    // Be VERY careful if you call this method directly. If it is called
    // twice for the same socket the JVM will core. Currently this is only
    // called from Poller.closePollset() to ensure kept alive connections
    // are closed when calling stop() followed by start().
    if (socket != 0) {
        Socket.destroy(socket);
        countDownConnection();
    }
}
 
Example #6
Source File: AprSocketWrapperImpl.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
/**
 * Connect this socket wrapper to remote server and start main loop on
 * AprSocketSource stdout link, to watch for incoming data, and
 * AprSocketSink stdin link, to pull for outgoing data.
 */
@Override
public void connect(InetSocketAddress address) throws IOException {
    try {
        inetAddress = Address.info(address.getHostName(), Socket.APR_UNSPEC, address.getPort(), 0, pool);
        socket = Socket.create(Address.getInfo(inetAddress).family, Socket.SOCK_STREAM, Socket.APR_PROTO_TCP, pool);
    } catch (Exception e) {
        throw new IOException("[" + this + "] ERROR: Cannot create socket for \"" + address + "\".", e);
    }

    int ret = Socket.connect(socket, inetAddress);
    if (ret != 0)
        throw new IOException("[" + this + "] ERROR: Cannot connect to remote host \"" + address + "\": " + Error.strerror(ret));

    source.setSocket(socket);
    sink.setSocket(socket);

    // Start polling for data to send to remote sever
    runMainLoop(IN, STDIN, true, true);

    // Push incoming data from server to handlers
    runMainLoop(OUT, STDOUT, false, false);

}
 
Example #7
Source File: AprEndpoint.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Port in use.
 */
@Override
public int getLocalPort() {
    long s = serverSock;
    if (s == 0) {
        return -1;
    } else {
        long sa;
        try {
            sa = Address.get(Socket.APR_LOCAL, s);
            Sockaddr addr = Address.getInfo(sa);
            return addr.port;
        } catch (Exception e) {
            return -1;
        }
    }
}
 
Example #8
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 #9
Source File: AjpAprProcessor.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Read at least the specified amount of bytes, and place them
 * in the input buffer.
 */
protected boolean read(int n)
    throws IOException {

    if (inputBuffer.capacity() - inputBuffer.limit() <=
            n - inputBuffer.remaining()) {
        inputBuffer.compact();
        inputBuffer.limit(inputBuffer.position());
        inputBuffer.position(0);
    }
    int nRead;
    while (inputBuffer.remaining() < n) {
        nRead = Socket.recvbb
            (socketWrapper.getSocket().longValue(), inputBuffer.limit(),
                    inputBuffer.capacity() - inputBuffer.limit());
        if (nRead > 0) {
            inputBuffer.limit(inputBuffer.limit() + nRead);
        } else {
            throw new IOException(sm.getString("ajpprocessor.failedread"));
        }
    }

    return true;

}
 
Example #10
Source File: AprSocketWrapperImpl.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
void destroyPull() {
    if (shutdowned)
        return;

    // Causes segfault in AprSocketSource.poll() method, so this function must be called from it
    try {
        Socket.close(socket);
        // or
        // Socket.shutdown(socket, Socket.APR_SHUTDOWN_READWRITE);
        Pool.destroy(pool);
    } catch (Exception e) {
        s_logger.info("[ignored]"
                + "failure during network cleanup: " + e.getLocalizedMessage());
    }

}
 
Example #11
Source File: AprEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private void destroySocket(long socket) {
    connections.remove(Long.valueOf(socket));
    if (log.isDebugEnabled()) {
        String msg = sm.getString("endpoint.debug.destroySocket",
                Long.valueOf(socket));
        if (log.isTraceEnabled()) {
            log.trace(msg, new Exception());
        } else {
            log.debug(msg);
        }
    }
    // Be VERY careful if you call this method directly. If it is called
    // twice for the same socket the JVM will core. Currently this is only
    // called from Poller.closePollset() to ensure kept alive connections
    // are closed when calling stop() followed by start().
    if (socket != 0) {
        Socket.destroy(socket);
        countDownConnection();
    }
}
 
Example #12
Source File: AjpAprProcessor.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
protected void output(byte[] src, int offset, int length)
        throws IOException {
    outputBuffer.put(src, offset, length);
    
    long socketRef = socketWrapper.getSocket().longValue();
    
    if (outputBuffer.position() > 0) {
        if ((socketRef != 0) && Socket.sendbb(socketRef, 0, outputBuffer.position()) < 0) {
            // There are no re-tries so clear the buffer to prevent a
            // possible overflow if the buffer is used again. BZ53119.
            outputBuffer.clear();
            throw new IOException(sm.getString("ajpprocessor.failedsend"));
        }
        outputBuffer.clear();
    }
}
 
Example #13
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 #14
Source File: AjpAprProcessor.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Read at least the specified amount of bytes, and place them
 * in the input buffer.
 */
protected boolean read(int n)
    throws IOException {

    if (inputBuffer.capacity() - inputBuffer.limit() <=
            n - inputBuffer.remaining()) {
        inputBuffer.compact();
        inputBuffer.limit(inputBuffer.position());
        inputBuffer.position(0);
    }
    int nRead;
    while (inputBuffer.remaining() < n) {
        nRead = Socket.recvbb
            (socketWrapper.getSocket().longValue(), inputBuffer.limit(),
                    inputBuffer.capacity() - inputBuffer.limit());
        if (nRead > 0) {
            inputBuffer.limit(inputBuffer.limit() + nRead);
        } else {
            throw new IOException(sm.getString("ajpprocessor.failedread"));
        }
    }

    return true;

}
 
Example #15
Source File: AjpAprProcessor.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
protected void output(byte[] src, int offset, int length)
        throws IOException {
    outputBuffer.put(src, offset, length);
    
    long socketRef = socketWrapper.getSocket().longValue();
    
    if (outputBuffer.position() > 0) {
        if ((socketRef != 0) && Socket.sendbb(socketRef, 0, outputBuffer.position()) < 0) {
            // There are no re-tries so clear the buffer to prevent a
            // possible overflow if the buffer is used again. BZ53119.
            outputBuffer.clear();
            throw new IOException(sm.getString("ajpprocessor.failedsend"));
        }
        outputBuffer.clear();
    }
}
 
Example #16
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 #17
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public InetSocketAddress getLocalAddress() throws IOException {
    long s = serverSock;
    if (s == 0) {
        return null;
    } else {
        long sa;
        try {
            sa = Address.get(Socket.APR_LOCAL, s);
        } catch (IOException ioe) {
            // re-throw
            throw ioe;
        } catch (Exception e) {
            // wrap
            throw new IOException(e);
        }
        Sockaddr addr = Address.getInfo(sa);
        if (addr.hostname == null) {
            // any local address
            if (addr.family == Socket.APR_INET6) {
                return new InetSocketAddress("::", addr.port);
            } else {
                return new InetSocketAddress("0.0.0.0", addr.port);
            }
        }
        return new InetSocketAddress(addr.hostname, addr.port);
    }
}
 
Example #18
Source File: TestXxxEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private void destroyAprSocket(long serverSock, long pool) {
    if (serverSock != 0) {
        Socket.shutdown(serverSock, Socket.APR_SHUTDOWN_READWRITE);
        Socket.close(serverSock);
        Socket.destroy(serverSock);
    }

    if (pool != 0) {
        Pool.destroy(pool);
        pool = 0;
    }
}
 
Example #19
Source File: AprEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Deallocate APR memory pools, and close server socket.
 */
@Override
public void unbind() throws Exception {
    if (running) {
        stop();
    }

    // Destroy pool if it was initialised
    if (serverSockPool != 0) {
        Pool.destroy(serverSockPool);
        serverSockPool = 0;
    }

    // Close server socket if it was initialised
    if (serverSock != 0) {
        Socket.close(serverSock);
        serverSock = 0;
    }

    sslContext = 0;

    // Close all APR memory pools and resources if initialised
    if (rootPool != 0) {
        Pool.destroy(rootPool);
        rootPool = 0;
    }

    handler.recycle();
}
 
Example #20
Source File: InternalAprOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Callback to write data from the buffer.
 */
private void flushBuffer()
    throws IOException {
    if (bbuf.position() > 0) {
        if (Socket.sendbb(socket, 0, bbuf.position()) < 0) {
            throw new IOException();
        }
        bbuf.clear();
    }
}
 
Example #21
Source File: UpgradeAprProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public UpgradeAprProcessor(SocketWrapper<Long> wrapper,
        UpgradeInbound upgradeInbound) {
    super(upgradeInbound);

    Socket.timeoutSet(wrapper.getSocket().longValue(),
            upgradeInbound.getReadTimeout());

    this.socket = wrapper.getSocket().longValue();
}
 
Example #22
Source File: TestXxxEndpoint.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void destroyAprSocket(long serverSock, long pool) {
    if (serverSock != 0) {
        Socket.shutdown(serverSock, Socket.APR_SHUTDOWN_READWRITE);
        Socket.close(serverSock);
        Socket.destroy(serverSock);
    }

    if (pool != 0) {
        Pool.destroy(pool);
        pool = 0;
    }
}
 
Example #23
Source File: AjpAprProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Read at least the specified amount of bytes, and place them
 * in the input buffer.
 */
protected boolean readt(int n, boolean useAvailableData)
    throws IOException {

    if (useAvailableData && inputBuffer.remaining() == 0) {
        return false;
    }
    if (inputBuffer.capacity() - inputBuffer.limit() <=
            n - inputBuffer.remaining()) {
        inputBuffer.compact();
        inputBuffer.limit(inputBuffer.position());
        inputBuffer.position(0);
    }
    int nRead;
    while (inputBuffer.remaining() < n) {
        nRead = Socket.recvbb
            (socketWrapper.getSocket().longValue(), inputBuffer.limit(),
                inputBuffer.capacity() - inputBuffer.limit());
        if (nRead > 0) {
            inputBuffer.limit(inputBuffer.limit() + nRead);
        } else {
            if ((-nRead) == Status.ETIMEDOUT || (-nRead) == Status.TIMEUP) {
                return false;
            } else {
                throw new IOException(sm.getString("ajpprocessor.failedread"));
            }
        }
    }

    return true;

}
 
Example #24
Source File: AprEndpoint.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Deallocate APR memory pools, and close server socket.
 */
@Override
public void unbind() throws Exception {
    if (running) {
        stop();
    }

    // Destroy pool if it was initialised
    if (serverSockPool != 0) {
        Pool.destroy(serverSockPool);
        serverSockPool = 0;
    }

    // Close server socket if it was initialised
    if (serverSock != 0) {
        Socket.close(serverSock);
        serverSock = 0;
    }

    sslContext = 0;

    // Close all APR memory pools and resources if initialised
    if (rootPool != 0) {
        Pool.destroy(rootPool);
        rootPool = 0;
    }

    handler.recycle();
}
 
Example #25
Source File: InternalAprOutputBuffer.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Callback to write data from the buffer.
 */
private void flushBuffer()
    throws IOException {
    if (bbuf.position() > 0) {
        if (Socket.sendbb(socket, 0, bbuf.position()) < 0) {
            throw new IOException();
        }
        bbuf.clear();
    }
}
 
Example #26
Source File: InternalAprOutputBuffer.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Send an acknowledgment.
 */
@Override
public void sendAck()
    throws IOException {

    if (!committed) {
        if (Socket.send(socket, Constants.ACK_BYTES, 0, Constants.ACK_BYTES.length) < 0)
            throw new IOException(sm.getString("iib.failedwrite"));
    }

}
 
Example #27
Source File: InternalAprOutputBuffer.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public void init(SocketWrapper<Long> socketWrapper,
        AbstractEndpoint<Long> endpoint) throws IOException {

    socket = socketWrapper.getSocket().longValue();
    Socket.setsbb(this.socket, bbuf);
}
 
Example #28
Source File: AprProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public AprProcessor(SocketWrapper<Long> wrapper,
        HttpUpgradeHandler httpUpgradeProcessor, AprEndpoint endpoint,
        int asyncWriteBufferSize) {
    super(httpUpgradeProcessor,
            new AprServletInputStream(wrapper),
            new AprServletOutputStream(wrapper, asyncWriteBufferSize, endpoint));

    Socket.timeoutSet(wrapper.getSocket().longValue(), INFINITE_TIMEOUT);
}
 
Example #29
Source File: UpgradeAprProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public int read() throws IOException {
    byte[] bytes = new byte[1];
    int result = Socket.recv(socket, bytes, 0, 1);
    if (result == -1) {
        return -1;
    } else {
        return bytes[0] & 0xFF;
    }
}
 
Example #30
Source File: UpgradeAprProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public void write(int b) throws IOException {
    int result = Socket.send(socket, new byte[] {(byte) b}, 0, 1);
    if (result != 1) {
        throw new IOException(sm.getString("apr.write.error",
                Integer.valueOf(-result)));
    }
}