Search in sources :

Example 6 with I2PSessionException

use of net.i2p.client.I2PSessionException in project i2p.i2p by i2p.

the class KRPC method messageAvailable.

// I2PSessionMuxedListener interface ----------------
/**
 * Instruct the client that the given session has received a message
 *
 * Will be called only if you register via addMuxedSessionListener().
 * Will be called only for the proto(s) and toPort(s) you register for.
 *
 * @param session session to notify
 * @param msgId message number available
 * @param size size of the message - why it's a long and not an int is a mystery
 * @param proto 1-254 or 0 for unspecified
 * @param fromPort 1-65535 or 0 for unspecified
 * @param toPort 1-65535 or 0 for unspecified
 */
public void messageAvailable(I2PSession session, int msgId, long size, int proto, int fromPort, int toPort) {
    // TODO throttle
    try {
        byte[] payload = session.receiveMessage(msgId);
        if (payload == null)
            return;
        _rxPkts.incrementAndGet();
        _rxBytes.addAndGet(payload.length);
        if (toPort == _qPort) {
            // repliable
            I2PDatagramDissector dgDiss = new I2PDatagramDissector();
            dgDiss.loadI2PDatagram(payload);
            payload = dgDiss.getPayload();
            Destination from = dgDiss.getSender();
            // TODO per-dest throttle
            receiveMessage(from, fromPort, payload);
        } else if (toPort == _rPort) {
            // raw
            receiveMessage(null, fromPort, payload);
        } else {
            if (_log.shouldLog(Log.WARN))
                _log.warn("msg on bad port");
        }
    } catch (DataFormatException e) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("bad msg");
    } catch (I2PInvalidDatagramException e) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("bad msg");
    } catch (I2PSessionException e) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("bad msg");
    }
}
Also used : Destination(net.i2p.data.Destination) I2PDatagramDissector(net.i2p.client.datagram.I2PDatagramDissector) DataFormatException(net.i2p.data.DataFormatException) I2PSessionException(net.i2p.client.I2PSessionException) I2PInvalidDatagramException(net.i2p.client.datagram.I2PInvalidDatagramException)

Example 7 with I2PSessionException

use of net.i2p.client.I2PSessionException in project i2p.i2p by i2p.

the class I2PSocketEepGet method sendRequest.

/**
 *  Look up the address, get a socket from the I2PSocketManager supplied in the constructor,
 *  and send the request.
 *
 *  @param timeout ignored
 */
@Override
protected void sendRequest(SocketTimeout timeout) throws IOException {
    if (_outputStream == null) {
        File outFile = new File(_outputFile);
        if (outFile.exists())
            _alreadyTransferred = outFile.length();
    }
    if (_proxyIn != null)
        try {
            _proxyIn.close();
        } catch (IOException ioe) {
        }
    if (_proxyOut != null)
        try {
            _proxyOut.close();
        } catch (IOException ioe) {
        }
    if (_socket != null)
        try {
            _socket.close();
        } catch (IOException ioe) {
        }
    try {
        URI url = new URI(_actualURL);
        if ("http".equals(url.getScheme())) {
            String host = url.getHost();
            if (host == null)
                throw new MalformedURLException("no hostname: " + _actualURL);
            int port = url.getPort();
            if (port <= 0 || port > 65535)
                port = 80;
            // as the naming service accepts B64KEY (but not B64KEY.i2p atm)
            if ("i2p".equals(host)) {
                String file = url.getRawPath();
                try {
                    int slash = 1 + file.substring(1).indexOf('/');
                    host = file.substring(1, slash);
                    _actualURL = "http://" + host + file.substring(slash);
                    String query = url.getRawQuery();
                    if (query != null)
                        _actualURL = _actualURL + '?' + query;
                } catch (IndexOutOfBoundsException ioobe) {
                    throw new MalformedURLException("Bad /i2p/ format: " + _actualURL);
                }
            }
            // Use existing I2PSession for lookups.
            // This is much more efficient than using the naming service
            Destination dest;
            I2PSession sess = _socketManager.getSession();
            if (sess != null && !sess.isClosed()) {
                try {
                    if (host.length() == 60 && host.endsWith(".b32.i2p")) {
                        byte[] b = Base32.decode(host.substring(0, 52));
                        if (b != null) {
                            Hash h = Hash.create(b);
                            dest = sess.lookupDest(h, 20 * 1000);
                        } else {
                            dest = null;
                        }
                    } else {
                        dest = sess.lookupDest(host, 20 * 1000);
                    }
                } catch (I2PSessionException ise) {
                    dest = null;
                }
            } else {
                dest = _context.namingService().lookup(host);
            }
            if (dest == null)
                throw new UnknownHostException("Unknown or non-i2p host: " + host);
            // Set the timeouts, using the other existing options in the socket manager
            // This currently duplicates what SocketTimeout is doing in EepGet,
            // but when that's ripped out of EepGet to use setsotimeout, we'll need this.
            Properties props = new Properties();
            props.setProperty(I2PSocketOptions.PROP_CONNECT_TIMEOUT, "" + CONNECT_TIMEOUT);
            props.setProperty(I2PSocketOptions.PROP_READ_TIMEOUT, "" + INACTIVITY_TIMEOUT);
            // This is important - even if the underlying socket doesn't have a connect delay,
            // we want to set it for this connection, so the request headers will go out
            // in the SYN packet, saving one RTT.
            props.setProperty(PROP_CONNECT_DELAY, CONNECT_DELAY);
            I2PSocketOptions opts = _socketManager.buildOptions(props);
            opts.setPort(port);
            _socket = _socketManager.connect(dest, opts);
        } else {
            throw new MalformedURLException("Unsupported protocol: " + _actualURL);
        }
    } catch (URISyntaxException use) {
        IOException ioe = new MalformedURLException("Bad URL");
        ioe.initCause(use);
        throw ioe;
    } catch (I2PException ie) {
        throw new IOException("I2P error", ie);
    }
    _proxyIn = _socket.getInputStream();
    _proxyOut = _socket.getOutputStream();
    // SocketTimeout doesn't take an I2PSocket, but no matter, because we
    // always close our socket in fetch() above.
    // timeout.setSocket(_socket);
    String req = getRequest();
    _proxyOut.write(DataHelper.getUTF8(req));
    _proxyOut.flush();
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Hash(net.i2p.data.Hash) Properties(java.util.Properties) URI(java.net.URI) I2PSession(net.i2p.client.I2PSession) I2PSessionException(net.i2p.client.I2PSessionException) File(java.io.File)

Example 8 with I2PSessionException

use of net.i2p.client.I2PSessionException in project i2p.i2p by i2p.

the class I2CPMessageProducer method createPayload.

/**
 * Create a new signed payload and send it off to the destination
 *
 * @param tag unused - no end-to-end crypto
 * @param tags unused - no end-to-end crypto
 * @param key unused - no end-to-end crypto
 * @param newKey unused - no end-to-end crypto
 */
private Payload createPayload(Destination dest, byte[] payload, SessionTag tag, SessionKey key, Set<SessionTag> tags, SessionKey newKey) throws I2PSessionException {
    if (dest == null)
        throw new I2PSessionException("No destination specified");
    if (payload == null)
        throw new I2PSessionException("No payload specified");
    Payload data = new Payload();
    if (!END_TO_END_CRYPTO) {
        data.setEncryptedData(payload);
        return data;
    }
    // no padding at this level
    // the garlic may pad, and the tunnels may pad, and the transports may pad
    int size = payload.length;
    byte[] encr = _context.elGamalAESEngine().encrypt(payload, dest.getPublicKey(), key, tags, tag, newKey, size);
    // yes, in an intelligent component, newTags would be queued for confirmation along with key, and
    // generateNewTags would only generate tags if necessary
    data.setEncryptedData(encr);
    // + data.calculateHash());
    return data;
}
Also used : I2PSessionException(net.i2p.client.I2PSessionException) Payload(net.i2p.data.Payload)

Example 9 with I2PSessionException

use of net.i2p.client.I2PSessionException in project i2p.i2p by i2p.

the class I2CPMessageProducer method connect.

/**
 * Send all the messages that a client needs to send to a router to establish
 * a new session.
 */
public void connect(I2PSessionImpl session) throws I2PSessionException {
    updateBandwidth(session);
    CreateSessionMessage msg = new CreateSessionMessage();
    SessionConfig cfg = new SessionConfig(session.getMyDestination());
    cfg.setOptions(session.getOptions());
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("config created");
    try {
        cfg.signSessionConfig(session.getPrivateKey());
    } catch (DataFormatException dfe) {
        throw new I2PSessionException("Unable to sign the session config", dfe);
    }
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("config signed");
    msg.setSessionConfig(cfg);
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("config loaded into message");
    session.sendMessage_unchecked(msg);
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("config message sent");
}
Also used : DataFormatException(net.i2p.data.DataFormatException) SessionConfig(net.i2p.data.i2cp.SessionConfig) I2PSessionException(net.i2p.client.I2PSessionException) CreateSessionMessage(net.i2p.data.i2cp.CreateSessionMessage)

Example 10 with I2PSessionException

use of net.i2p.client.I2PSessionException in project i2p.i2p by i2p.

the class I2CPMessageProducer method createLeaseSet.

/**
 * Create a new signed leaseSet in response to a request to do so and send it
 * to the router
 */
public void createLeaseSet(I2PSessionImpl session, LeaseSet leaseSet, SigningPrivateKey signingPriv, PrivateKey priv) throws I2PSessionException {
    CreateLeaseSetMessage msg = new CreateLeaseSetMessage();
    msg.setLeaseSet(leaseSet);
    msg.setPrivateKey(priv);
    msg.setSigningPrivateKey(signingPriv);
    SessionId sid = session.getSessionId();
    if (sid == null) {
        _log.error(session.toString() + " create LS w/o session", new Exception());
        return;
    }
    msg.setSessionId(sid);
    session.sendMessage_unchecked(msg);
}
Also used : CreateLeaseSetMessage(net.i2p.data.i2cp.CreateLeaseSetMessage) SessionId(net.i2p.data.i2cp.SessionId) DataFormatException(net.i2p.data.DataFormatException) I2PSessionException(net.i2p.client.I2PSessionException)

Aggregations

I2PSessionException (net.i2p.client.I2PSessionException)44 IOException (java.io.IOException)18 DataFormatException (net.i2p.data.DataFormatException)15 Properties (java.util.Properties)13 I2PSession (net.i2p.client.I2PSession)11 Destination (net.i2p.data.Destination)11 SessionId (net.i2p.data.i2cp.SessionId)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InterruptedIOException (java.io.InterruptedIOException)5 I2PException (net.i2p.I2PException)5 File (java.io.File)4 GeneralSecurityException (java.security.GeneralSecurityException)4 I2PClient (net.i2p.client.I2PClient)4 FileInputStream (java.io.FileInputStream)3 UnknownHostException (java.net.UnknownHostException)3 Payload (net.i2p.data.Payload)3 Log (net.i2p.util.Log)3 BufferedInputStream (java.io.BufferedInputStream)2 DataInputStream (java.io.DataInputStream)2