Search in sources :

Example 36 with I2PAppThread

use of net.i2p.util.I2PAppThread in project i2p.i2p by i2p.

the class SAMv3StreamSession method startForwardingIncoming.

/**
 *  Forward sockets from I2P to the host/port provided.
 *  Accepts and forwarding may not be done at the same time.
 */
public void startForwardingIncoming(Properties props, boolean sendPorts) throws SAMException, InterruptedIOException {
    SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
    boolean verbose = !Boolean.parseBoolean(props.getProperty("SILENT"));
    if (rec == null)
        throw new InterruptedIOException();
    String portStr = props.getProperty("PORT");
    if (portStr == null) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("receiver port not specified");
        throw new SAMException("receiver port not specified");
    }
    int port = Integer.parseInt(portStr);
    String host = props.getProperty("HOST");
    if (host == null) {
        host = rec.getHandler().getClientIP();
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("no host specified. Taken from the client socket : " + host + ':' + port);
    }
    boolean isSSL = Boolean.parseBoolean(props.getProperty("SSL"));
    if (_acceptors.get() > 0) {
        if (_log.shouldWarn())
            _log.warn("an accepting server is already defined for this destination");
        throw new SAMException("an accepting server is already defined for this destination");
    }
    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");
        }
        this.socketServer = this.socketMgr.getServerSocket();
    }
    SocketForwarder forwarder = new SocketForwarder(host, port, isSSL, verbose, sendPorts);
    (new I2PAppThread(rec.getThreadGroup(), forwarder, "SAMV3StreamForwarder")).start();
}
Also used : InterruptedIOException(java.io.InterruptedIOException) I2PAppThread(net.i2p.util.I2PAppThread)

Example 37 with I2PAppThread

use of net.i2p.util.I2PAppThread 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 38 with I2PAppThread

use of net.i2p.util.I2PAppThread in project i2p.i2p by i2p.

the class MasterSession method start.

/**
 *  Overridden to start the acceptor.
 */
@Override
public void start() {
    Thread t = new I2PAppThread(streamAcceptor, "SAMMasterAcceptor");
    t.start();
}
Also used : I2PAppThread(net.i2p.util.I2PAppThread) I2PAppThread(net.i2p.util.I2PAppThread)

Example 39 with I2PAppThread

use of net.i2p.util.I2PAppThread in project i2p.i2p by i2p.

the class SAMBridge method startThread.

/**
 *  @since 0.9.6
 */
private void startThread() {
    I2PAppThread t = new I2PAppThread(this, "SAMListener " + _listenPort);
    if (Boolean.parseBoolean(System.getProperty("sam.shutdownOnOOM"))) {
        t.addOOMEventThreadListener(new I2PAppThread.OOMEventListener() {

            public void outOfMemory(OutOfMemoryError err) {
                err.printStackTrace();
                System.err.println("OOMed, die die die");
                System.exit(-1);
            }
        });
    }
    t.start();
    _runner = t;
}
Also used : I2PAppThread(net.i2p.util.I2PAppThread)

Example 40 with I2PAppThread

use of net.i2p.util.I2PAppThread in project i2p.i2p by i2p.

the class SAMBridge method run.

public void run() {
    if (serverSocket == null)
        return;
    changeState(RUNNING);
    if (_mgr != null)
        _mgr.register(this);
    I2PAppContext.getGlobalContext().portMapper().register(_useSSL ? PortMapper.SVC_SAM_SSL : PortMapper.SVC_SAM, _listenHost != null ? _listenHost : "127.0.0.1", _listenPort);
    try {
        while (acceptConnections) {
            SocketChannel s = serverSocket.accept();
            if (_log.shouldLog(Log.DEBUG))
                _log.debug("New connection from " + s.socket().getInetAddress().toString() + ":" + s.socket().getPort());
            class HelloHandler implements Runnable, Handler {

                private final SocketChannel s;

                private final SAMBridge parent;

                HelloHandler(SocketChannel s, SAMBridge parent) {
                    this.s = s;
                    this.parent = parent;
                }

                public void run() {
                    parent.register(this);
                    try {
                        SAMHandler handler = SAMHandlerFactory.createSAMHandler(s, i2cpProps, parent);
                        if (handler == null) {
                            if (_log.shouldLog(Log.DEBUG))
                                _log.debug("SAM handler has not been instantiated");
                            try {
                                s.close();
                            } catch (IOException e) {
                            }
                            return;
                        }
                        handler.startHandling();
                    } catch (SAMException e) {
                        if (_log.shouldLog(Log.ERROR))
                            _log.error("SAM error: " + e.getMessage(), e);
                        String reply = "HELLO REPLY RESULT=I2P_ERROR MESSAGE=\"" + e.getMessage() + "\"\n";
                        SAMHandler.writeString(reply, s);
                        try {
                            s.close();
                        } catch (IOException ioe) {
                        }
                    } catch (Exception ee) {
                        try {
                            s.close();
                        } catch (IOException ioe) {
                        }
                        _log.log(Log.CRIT, "Unexpected error handling SAM connection", ee);
                    } finally {
                        parent.unregister(this);
                    }
                }

                /**
                 * @since 0.9.20
                 */
                public void stopHandling() {
                    try {
                        s.close();
                    } catch (IOException ioe) {
                    }
                }
            }
            new I2PAppThread(new HelloHandler(s, this), "SAM HelloHandler").start();
        }
        changeState(STOPPING);
    } catch (Exception e) {
        if (acceptConnections)
            _log.error("Unexpected error while listening for connections", e);
        else
            e = null;
        changeState(STOPPING, e);
    } finally {
        try {
            if (_log.shouldLog(Log.DEBUG))
                _log.debug("Shutting down, closing server socket");
            if (serverSocket != null)
                serverSocket.close();
        } catch (IOException e) {
        }
        I2PAppContext.getGlobalContext().portMapper().unregister(_useSSL ? PortMapper.SVC_SAM_SSL : PortMapper.SVC_SAM);
        stopHandlers();
        changeState(STOPPED);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) IOException(java.io.IOException) DataFormatException(net.i2p.data.DataFormatException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) I2PAppThread(net.i2p.util.I2PAppThread)

Aggregations

I2PAppThread (net.i2p.util.I2PAppThread)52 IOException (java.io.IOException)18 I2PException (net.i2p.I2PException)9 I2PSocket (net.i2p.client.streaming.I2PSocket)7 InterruptedIOException (java.io.InterruptedIOException)6 ConnectException (java.net.ConnectException)5 Socket (java.net.Socket)5 I2PSessionException (net.i2p.client.I2PSessionException)5 Destination (net.i2p.data.Destination)5 File (java.io.File)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ServerSocket (java.net.ServerSocket)3 SocketTimeoutException (java.net.SocketTimeoutException)3 UnknownHostException (java.net.UnknownHostException)3 Properties (java.util.Properties)3 I2PServerSocket (net.i2p.client.streaming.I2PServerSocket)3 DataFormatException (net.i2p.data.DataFormatException)3 FileNotFoundException (java.io.FileNotFoundException)2