Search in sources :

Example 26 with I2PSocket

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

the class SAMv3StreamSession method accept.

/**
 * Accept a single incoming STREAM on the socket stolen from the handler.
 * As of version 3.2 (0.9.24), multiple simultaneous accepts are allowed.
 * Accepts and forwarding may not be done at the same time.
 *
 * @param handler The handler that communicates with the requesting client
 * @param verbose If true, SAM will send the Base64-encoded peer Destination of an
 *                incoming socket as the first line of data sent to its client
 *                on the handler socket
 *
 * @throws DataFormatException if the destination is not valid
 * @throws ConnectException if the destination refuses connections
 * @throws NoRouteToHostException if the destination can't be reached
 * @throws InterruptedIOException if the connection timeouts
 * @throws I2PException if there's another I2P-related error
 * @throws IOException
 */
public void accept(SAMv3Handler handler, boolean verbose) throws I2PException, InterruptedIOException, IOException, SAMException {
    synchronized (this.socketServerLock) {
        if (this.socketServer != null) {
            if (_log.shouldWarn())
                _log.warn("a forwarding server is already defined for this destination");
            throw new SAMException("a forwarding server is already defined for this destination");
        }
    }
    I2PSocket i2ps = null;
    _acceptors.incrementAndGet();
    try {
        if (_acceptQueue != null)
            i2ps = acceptSocket();
        else
            i2ps = socketMgr.getServerSocket().accept();
    } finally {
        _acceptors.decrementAndGet();
    }
    SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
    if (rec == null || i2ps == null)
        throw new InterruptedIOException();
    if (verbose) {
        handler.notifyStreamIncomingConnection(i2ps.getPeerDestination(), i2ps.getPort(), i2ps.getLocalPort());
    }
    handler.stealSocket();
    ReadableByteChannel fromClient = handler.getClientSocket();
    ReadableByteChannel fromI2P = Channels.newChannel(i2ps.getInputStream());
    WritableByteChannel toClient = handler.getClientSocket();
    WritableByteChannel toI2P = Channels.newChannel(i2ps.getOutputStream());
    SAMBridge bridge = handler.getBridge();
    (new I2PAppThread(rec.getThreadGroup(), new Pipe(fromClient, toI2P, bridge), "AcceptV3 SAMPipeClientToI2P")).start();
    (new I2PAppThread(rec.getThreadGroup(), new Pipe(fromI2P, toClient, bridge), "AcceptV3 SAMPipeI2PToClient")).start();
}
Also used : InterruptedIOException(java.io.InterruptedIOException) ReadableByteChannel(java.nio.channels.ReadableByteChannel) I2PSocket(net.i2p.client.streaming.I2PSocket) WritableByteChannel(java.nio.channels.WritableByteChannel) I2PAppThread(net.i2p.util.I2PAppThread)

Example 27 with I2PSocket

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

the class I2PSnarkUtil method connect.

/**
 * connect to the given destination
 */
I2PSocket connect(PeerID peer) throws IOException {
    I2PSocketManager mgr = _manager;
    if (mgr == null)
        throw new IOException("No socket manager");
    Destination addr = peer.getAddress();
    if (addr == null)
        throw new IOException("Null address");
    if (addr.equals(getMyDestination()))
        throw new IOException("Attempt to connect to myself");
    Hash dest = addr.calculateHash();
    if (_banlist.contains(dest))
        throw new IOException("Not trying to contact " + dest.toBase64() + ", as they are banlisted");
    try {
        // TODO opts.setPort(xxx); connect(addr, opts)
        // DHT moved above 6881 in 0.9.9
        I2PSocket rv = _manager.connect(addr);
        if (rv != null)
            _banlist.remove(dest);
        return rv;
    } catch (I2PException ie) {
        _banlist.add(dest);
        _context.simpleTimer2().addEvent(new Unbanlist(dest), 10 * 60 * 1000);
        IOException ioe = new IOException("Unable to reach the peer " + peer);
        ioe.initCause(ie);
        throw ioe;
    }
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) I2PSocketManager(net.i2p.client.streaming.I2PSocketManager) I2PSocket(net.i2p.client.streaming.I2PSocket) IOException(java.io.IOException) Hash(net.i2p.data.Hash)

Example 28 with I2PSocket

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

the class I2Plistener method run.

/**
 * Simply listen on I2P port, and thread connections
 */
public void run() {
    boolean g = false;
    I2PSocket sessSocket = null;
    int conn = 0;
    try {
        try {
            serverSocket.setSoTimeout(50);
            while (lives.get()) {
                try {
                    sessSocket = serverSocket.accept();
                    g = true;
                } catch (ConnectException ce) {
                    g = false;
                } catch (SocketTimeoutException ste) {
                    g = false;
                }
                if (g) {
                    g = false;
                    conn++;
                    // toss the connection to a new thread.
                    I2PtoTCP conn_c = new I2PtoTCP(sessSocket, info, database, lives);
                    Thread t = new I2PAppThread(conn_c, Thread.currentThread().getName() + " I2PtoTCP " + conn);
                    t.start();
                }
            }
        } catch (I2PException e) {
            // bad stuff
            System.out.println("Exception " + e);
        }
    } finally {
        try {
            serverSocket.close();
        } catch (I2PException ex) {
        }
    // System.out.println("I2Plistener: Close");
    }
}
Also used : I2PException(net.i2p.I2PException) SocketTimeoutException(java.net.SocketTimeoutException) I2PSocket(net.i2p.client.streaming.I2PSocket) I2PAppThread(net.i2p.util.I2PAppThread) ConnectException(java.net.ConnectException) I2PAppThread(net.i2p.util.I2PAppThread)

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