/** * Unsupported * @throws I2PSessionException always * @since 0.9.25 */ public boolean sendBytes(String s, byte[] b, int pr, int fp, int tp, boolean sendLeaseSet, int sendTags, int tagThreshold, int expiration) throws I2PSessionException { throw new I2PSessionException("Unsupported in STREAM or MASTER session"); }
/** * Unsupported * @throws I2PSessionException always * @since 0.9.25 moved from subclass SAMv3StreamSession to implement SAMMessageSess */ public boolean sendBytes(String s, byte[] b, int pr, int fp, int tp) throws I2PSessionException { throw new I2PSessionException("Unsupported in STREAM or MASTER session"); }
/** * Add this message to the writer's queue. * Blocking if queue is full. * @throws I2PSessionException if we wait too long or are interrupted */ public void addMessage(I2CPMessage msg) throws I2PSessionException { try { if (!_messagesToWrite.offer(msg, MAX_SEND_WAIT, TimeUnit.MILLISECONDS)) throw new I2PSessionException("Timed out waiting while write queue was full"); } catch (InterruptedException ie) { throw new I2PSessionException("Interrupted while write queue was full", ie); } }
/** * Throws I2PSessionException if uninitialized, closed or closing. * Blocks if opening. * * @since 0.9.23 */ protected void verifyOpen() throws I2PSessionException { synchronized (_stateLock) { while (true) { switch (_state) { case INIT: throw new I2PSessionException("Not open, must call connect() first"); case OPENING: // fall thru case GOTDATE: try { _stateLock.wait(5*1000); continue; } catch (InterruptedException ie) { throw new I2PSessionException("Interrupted", ie); } case OPEN: return; case CLOSING: // fall thru case CLOSED: throw new I2PSessionException("Already closed"); } } } }
/** * The InternalClientManager interface. * Connect to the router, receiving a message queue to talk to the router with. * @throws I2PSessionException if the router isn't ready * @since 0.8.3 */ public I2CPMessageQueue connect() throws I2PSessionException { if (_manager != null) return _manager.internalConnect(); throw new I2PSessionException("No manager yet"); } }
/** * Deliver an I2CP message to the router. * Does NOT check state. Call only from connect() or other methods that need to * send messages when not in OPEN state. * * @throws I2PSessionException if the message is malformed or there is an error writing it out * @since 0.9.23 */ void sendMessage_unchecked(I2CPMessage message) throws I2PSessionException { if (_queue != null) { // internal try { if (!_queue.offer(message, MAX_SEND_WAIT)) throw new I2PSessionException("Timed out waiting while write queue was full"); } catch (InterruptedException ie) { throw new I2PSessionException("Interrupted", ie); } } else { ClientWriterRunner writer = _writer; if (writer == null) { throw new I2PSessionException("Already closed or not open"); } else { writer.addMessage(message); } } }
/** * Create a new payload. * No more end-to-end encryption, just set the "encrypted" data to the payload. */ private static Payload createPayload(byte[] payload) throws I2PSessionException { if (payload == null) throw new I2PSessionException("No payload specified"); Payload data = new Payload(); data.setEncryptedData(payload); return data; }
protected I2PSessionImpl(I2PSessionImpl primary, InputStream destKeyStream, Properties options) throws I2PSessionException { this(primary.getContext(), options, primary.getHandlerMap(), primary.getProducer(), true); _availabilityNotifier = new AvailabilityNotifier(); try { readDestination(destKeyStream); } catch (DataFormatException dfe) { throw new I2PSessionException("Error reading the destination key stream", dfe); } catch (IOException ioe) { throw new I2PSessionException("Error reading the destination key stream", ioe); } }
client.createDestination(keyStream, getSigType(opts)); } catch (I2PException e) { throw new I2PSessionException("Error creating keys", e); } catch (IOException e) { throw new I2PSessionException("Error creating keys", e);
/** * @param primary must be a I2PSessionMuxedImpl */ public SubSession(I2PSession primary, InputStream destKeyStream, Properties options) throws I2PSessionException { super((I2PSessionMuxedImpl)primary, destKeyStream, options); _primary = (I2PSessionMuxedImpl) primary; if (!getDecryptionKey().equals(_primary.getDecryptionKey())) throw new I2PSessionException("encryption key mismatch"); if (getPrivateKey().equals(_primary.getPrivateKey())) throw new I2PSessionException("signing key must differ"); // state management }
/** * The InternalClientManager interface. * Connects to the router, receiving a message queue to talk to the router with. * @throws I2PSessionException if the router isn't ready * @since 0.8.3 */ public I2CPMessageQueue internalConnect() throws I2PSessionException { if (!_isStarted) throw new I2PSessionException("Router client manager is shut down"); LinkedBlockingQueue<I2CPMessage> in = new LinkedBlockingQueue<I2CPMessage>(INTERNAL_QUEUE_SIZE); LinkedBlockingQueue<I2CPMessage> out = new LinkedBlockingQueue<I2CPMessage>(INTERNAL_QUEUE_SIZE); I2CPMessageQueue myQueue = new I2CPMessageQueueImpl(in, out); I2CPMessageQueue hisQueue = new I2CPMessageQueueImpl(out, in); ClientConnectionRunner runner = new QueuedClientConnectionRunner(_ctx, this, myQueue); registerConnection(runner); return hisQueue; }
/** * Create a new session, reading the Destination, PrivateKey, and SigningPrivateKey * from the destKeyStream, and using the specified options to connect to the router * * As of 0.9.19, defaults in options are honored. * * This does NOT validate consistency of the destKeyStream, * e.g. pubkey/privkey match or valid offline sig. The router does that. * * @param destKeyStream stream containing the private key data, * format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile} * @param options set of options to configure the router with, if null will use System properties * @throws I2PSessionException if there is a problem loading the private keys */ public I2PSessionImpl(I2PAppContext context, InputStream destKeyStream, Properties options) throws I2PSessionException { this(context, options, new I2PClientMessageHandlerMap(context), new I2CPMessageProducer(context), true); _availabilityNotifier = new AvailabilityNotifier(); try { readDestination(destKeyStream); } catch (DataFormatException dfe) { throw new I2PSessionException("Error reading the destination key stream", dfe); } catch (IOException ioe) { throw new I2PSessionException("Error reading the destination key stream", ioe); } }
throw new I2PSessionException("Router does not support subsessions"); SubSession sub; synchronized(_subsessionLock) { if (_subsessions.size() > _subsessionMap.size()) throw new I2PSessionException("Subsession request already pending"); sub = new SubSession(this, privateKeyStream, opts); for (SubSession ss : _subsessions) { if (ss.getDecryptionKey().equals(sub.getDecryptionKey()) && ss.getPrivateKey().equals(sub.getPrivateKey())) { throw new I2PSessionException("Dup subsession");
/** * Blocking. Waits a max of 5 seconds. * But shouldn't take long. * Implemented in 0.8.3 in I2PSessionImpl; * previously was available only in I2PSimpleSession. * Multiple outstanding lookups are now allowed. * @return null on failure */ public int[] bandwidthLimits() throws I2PSessionException { synchronized (_stateLock) { // not before GOTDATE if (_state == State.CLOSED || _state == State.INIT || _state == State.OPENING) { if (_log.shouldLog(Log.INFO)) _log.info("Session closed, cannot get bw limits"); return null; } } sendMessage_unchecked(new GetBandwidthLimitsMessage()); try { synchronized (_bwReceivedLock) { _bwReceivedLock.wait(5*1000); } } catch (InterruptedException ie) { throw new I2PSessionException("Interrupted", ie); } return _bwLimits; }
public void handleMessage(I2CPMessage message, I2PSessionImpl session) { if (_log.shouldLog(Log.DEBUG)) _log.debug("Handle message " + message); String reason = ((DisconnectMessage)message).getReason(); session.propogateError(reason, new I2PSessionException("Disconnect Message received: " + reason)); session.destroySession(false); if (reason.contains("restart")) { Thread t = new I2PAppThread(new Reconnector(session), "Reconnect " + session, true); t.start(); } }
/** * Deliver an I2CP message to the router * May block for several seconds if the write queue to the router is full * * @throws I2PSessionException if the message is malformed or there is an error writing it out */ @Override void sendMessage(I2CPMessage message) throws I2PSessionException { // workaround for now, as primary will send out our CreateSession // from his connect, while we are still closed. // If we did it in connect() we wouldn't need this if (isClosed() && message.getType() != CreateSessionMessage.MESSAGE_TYPE && message.getType() != CreateLeaseSetMessage.MESSAGE_TYPE) throw new I2PSessionException("Already closed"); _primary.sendMessage_unchecked(message); }
cfg.signSessionConfig(session.getPrivateKey()); } catch (DataFormatException dfe) { throw new I2PSessionException("Unable to sign the session config", dfe);
state.waitForAccept(_context.clock().now() + getTimeout()); } catch (InterruptedException ie) { throw new I2PSessionException("interrupted"); } finally { _sendingStates.remove(Long.valueOf(nonce));
public void handleMessage(I2CPMessage message, I2PSessionImpl session) { if (_log.shouldLog(Log.DEBUG)) _log.debug("Handle message " + message); SessionStatusMessage msg = (SessionStatusMessage) message; session.setSessionId(msg.getSessionId()); switch (msg.getStatus()) { case SessionStatusMessage.STATUS_CREATED: _log.info("Session created successfully"); break; case SessionStatusMessage.STATUS_DESTROYED: _log.warn("Session destroyed"); session.propogateError("Destroyed", new I2PSessionException("Session Status Message received")); //session.destroySession(); session.reconnect(); // la la la break; case SessionStatusMessage.STATUS_INVALID: _log.warn("Session invalid"); session.propogateError("Invalid", new I2PSessionException("Session Status Message received")); session.destroySession(); // ok, honor this destroy message, because we're b0rked break; case SessionStatusMessage.STATUS_UPDATED: _log.info("Session status updated"); break; default: if (_log.shouldLog(Log.WARN)) _log.warn("Unknown session status sent: " + msg.getStatus()); } return; } }
/** * 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(getRouterOptions(session)); if (session.isOffline()) { cfg.setOfflineSignature(session.getOfflineExpiration(), session.getTransientSigningPublicKey(), session.getOfflineSignature()); } try { cfg.signSessionConfig(session.getPrivateKey()); } catch (DataFormatException dfe) { throw new I2PSessionException("Unable to sign the session config", dfe); } msg.setSessionConfig(cfg); session.sendMessage_unchecked(msg); }