Search in sources :

Example 11 with I2PSocket

use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.

the class I2PTunnelServer method blockingHandle.

/**
 *  This is run in a thread from a limited-size thread pool via Handler.run(),
 *  except for a standard server (this class, no extension, as determined in getUsePool()),
 *  it is run directly in the acceptor thread (see run()).
 *
 *  In either case, this method and any overrides must spawn a thread and return quickly.
 *  If blocking while reading the headers (as in HTTP and IRC), the thread pool
 *  may be exhausted.
 *
 *  See PROP_USE_POOL, DEFAULT_USE_POOL, PROP_HANDLER_COUNT, DEFAULT_HANDLER_COUNT
 */
protected void blockingHandle(I2PSocket socket) {
    if (_log.shouldLog(Log.INFO))
        _log.info("Incoming connection to '" + toString() + "' port " + socket.getLocalPort() + " from: " + socket.getPeerDestination().calculateHash() + " port " + socket.getPort());
    long afterAccept = getTunnel().getContext().clock().now();
    long afterSocket = -1;
    // threads.
    try {
        socket.setReadTimeout(readTimeout);
        Socket s = getSocket(socket.getPeerDestination().calculateHash(), socket.getLocalPort());
        afterSocket = getTunnel().getContext().clock().now();
        Thread t = new I2PTunnelRunner(s, socket, slock, null, null, null, (I2PTunnelRunner.FailCallback) null);
        // run in the unlimited client pool
        // t.start();
        _clientExecutor.execute(t);
        long afterHandle = getTunnel().getContext().clock().now();
        long timeToHandle = afterHandle - afterAccept;
        if ((timeToHandle > 1000) && (_log.shouldLog(Log.WARN)))
            _log.warn("Took a while to handle the request for " + remoteHost + ':' + remotePort + " [" + timeToHandle + ", socket create: " + (afterSocket - afterAccept) + "]");
    } catch (SocketException ex) {
        try {
            socket.reset();
        } catch (IOException ioe) {
        }
        if (_log.shouldLog(Log.ERROR))
            _log.error("Error connecting to server " + remoteHost + ':' + remotePort, ex);
    } catch (IOException ex) {
        _log.error("Error while waiting for I2PConnections", ex);
    }
}
Also used : SocketException(java.net.SocketException) IOException(java.io.IOException) Socket(java.net.Socket) I2PServerSocket(net.i2p.client.streaming.I2PServerSocket) I2PSocket(net.i2p.client.streaming.I2PSocket) I2PAppThread(net.i2p.util.I2PAppThread)

Example 12 with I2PSocket

use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.

the class I2PTunnelDCCClient method clientConnectionRun.

/**
 *  Accept one connection only.
 */
protected void clientConnectionRun(Socket s) {
    I2PSocket i2ps = null;
    if (_log.shouldLog(Log.INFO))
        _log.info("Opening DCC connection to " + _dest + ':' + _remotePort);
    Destination dest = _context.namingService().lookup(_dest);
    if (dest == null) {
        _log.error("Could not find leaseset for DCC connection to " + _dest + ':' + _remotePort);
        closeSocket(s);
        stop();
        notifyEvent(CONNECT_STOP_EVENT, Integer.valueOf(getLocalPort()));
        return;
    }
    I2PSocketOptions opts = sockMgr.buildOptions();
    opts.setPort(_remotePort);
    try {
        i2ps = createI2PSocket(dest, opts);
        Thread t = new Runner(s, i2ps);
        // we are called from an unlimited thread pool, so run inline
        // t.start();
        t.run();
    } catch (IOException ex) {
        _log.error("Could not make DCC connection to " + _dest + ':' + _remotePort, ex);
        notifyEvent(CONNECT_STOP_EVENT, Integer.valueOf(getLocalPort()));
    } catch (I2PException ex) {
        _log.error("Could not make DCC connection to " + _dest + ':' + _remotePort, ex);
        notifyEvent(CONNECT_STOP_EVENT, Integer.valueOf(getLocalPort()));
    } finally {
        // only because we are running it inline
        closeSocket(s);
        if (i2ps != null) {
            try {
                i2ps.close();
            } catch (IOException ioe) {
            }
        }
    }
    stop();
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) I2PTunnelRunner(net.i2p.i2ptunnel.I2PTunnelRunner) I2PSocket(net.i2p.client.streaming.I2PSocket) I2PSocketOptions(net.i2p.client.streaming.I2PSocketOptions) IOException(java.io.IOException)

Example 13 with I2PSocket

use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.

the class I2PTunnelDCCServer method close.

@Override
public boolean close(boolean forced) {
    _outgoing.clear();
    _active.clear();
    for (I2PSocket s : _sockList) {
        try {
            s.close();
        } catch (IOException ioe) {
        }
    }
    _sockList.clear();
    return super.close(forced);
}
Also used : I2PSocket(net.i2p.client.streaming.I2PSocket) IOException(java.io.IOException)

Example 14 with I2PSocket

use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.

the class I2PTunnelDCCServer method blockingHandle.

/**
 *  An incoming DCC connection, only accept for a known port.
 *  Passed through without filtering.
 */
@Override
protected void blockingHandle(I2PSocket socket) {
    if (_log.shouldLog(Log.INFO))
        _log.info("Incoming connection to '" + toString() + "' from: " + socket.getPeerDestination().calculateHash().toBase64());
    try {
        expireOutbound();
        int myPort = socket.getLocalPort();
        // Port is a one-time-use only
        LocalAddress local = _outgoing.remove(Integer.valueOf(myPort));
        if (local == null) {
            if (_log.shouldLog(Log.WARN))
                _log.warn("Rejecting incoming DCC connection for unknown port " + myPort);
            try {
                socket.close();
            } catch (IOException ioe) {
            }
            return;
        }
        if (_log.shouldLog(Log.WARN))
            _log.warn("Incoming DCC connection for I2P port " + myPort + " sending to " + local.ia + ':' + local.port);
        try {
            Socket s = new Socket(local.ia, local.port);
            _sockList.add(socket);
            Thread t = new I2PTunnelRunner(s, socket, slock, null, null, _sockList, (I2PTunnelRunner.FailCallback) null);
            // run in the unlimited client pool
            // t.start();
            _clientExecutor.execute(t);
            local.socket = socket;
            local.expire = getTunnel().getContext().clock().now() + OUTBOUND_EXPIRE;
            _active.put(Integer.valueOf(myPort), local);
        } catch (SocketException ex) {
            try {
                socket.reset();
            } catch (IOException ioe) {
            }
            _log.error("Error relaying incoming DCC connection to IRC client at " + local.ia + ':' + local.port, ex);
        }
    } catch (IOException ex) {
        _log.error("Error while waiting for I2PConnections", ex);
    }
}
Also used : SocketException(java.net.SocketException) IOException(java.io.IOException) I2PTunnelRunner(net.i2p.i2ptunnel.I2PTunnelRunner) Socket(java.net.Socket) I2PSocket(net.i2p.client.streaming.I2PSocket)

Example 15 with I2PSocket

use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.

the class I2PSOCKSTunnel method clientConnectionRun.

protected void clientConnectionRun(Socket s) {
    I2PSocket destSock = null;
    try {
        try {
            s.setSoTimeout(INITIAL_SO_TIMEOUT);
        } catch (SocketException ioe) {
        }
        SOCKSServer serv = SOCKSServerFactory.createSOCKSServer(_context, s, getTunnel().getClientOptions());
        Socket clientSock = serv.getClientSocket();
        try {
            s.setSoTimeout(0);
        } catch (SocketException ioe) {
        }
        destSock = serv.getDestinationI2PSocket(this);
        Thread t = new I2PTunnelRunner(clientSock, destSock, sockLock, null, null, mySockets, (I2PTunnelRunner.FailCallback) null);
        // we are called from an unlimited thread pool, so run inline
        // t.start();
        t.run();
    } catch (SOCKSException e) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Error from SOCKS connection", e);
    } finally {
        // only because we are running it inline
        closeSocket(s);
        if (destSock != null)
            try {
                destSock.close();
            } catch (IOException ioe) {
            }
    }
}
Also used : SocketException(java.net.SocketException) I2PSocket(net.i2p.client.streaming.I2PSocket) SOCKSException(net.i2p.socks.SOCKSException) I2PTunnelRunner(net.i2p.i2ptunnel.I2PTunnelRunner) IOException(java.io.IOException) Socket(java.net.Socket) I2PSocket(net.i2p.client.streaming.I2PSocket)

Aggregations

I2PSocket (net.i2p.client.streaming.I2PSocket)28 IOException (java.io.IOException)21 I2PException (net.i2p.I2PException)15 Destination (net.i2p.data.Destination)12 Socket (java.net.Socket)8 I2PSocketOptions (net.i2p.client.streaming.I2PSocketOptions)8 I2PAppThread (net.i2p.util.I2PAppThread)7 SocketException (java.net.SocketException)6 Properties (java.util.Properties)5 SOCKSException (net.i2p.socks.SOCKSException)5 InterruptedIOException (java.io.InterruptedIOException)4 OutputStream (java.io.OutputStream)4 ConnectException (java.net.ConnectException)4 SocketTimeoutException (java.net.SocketTimeoutException)4 Outproxy (net.i2p.app.Outproxy)4 Hash (net.i2p.data.Hash)4 DataOutputStream (java.io.DataOutputStream)3 InputStream (java.io.InputStream)3 List (java.util.List)3 I2PSession (net.i2p.client.I2PSession)3