/**
   * Create a new connected socket. Blocks until the socket is created, unless the connectDelay
   * option (i2p.streaming.connectDelay) is set and greater than zero. If so this will return
   * immediately, and the client may quickly write initial data to the socket and this data will be
   * bundled in the SYN packet.
   *
   * @param peer Destination to connect to
   * @param options I2P socket options to be used for connecting, may be null
   * @return I2PSocket if successful
   * @throws NoRouteToHostException if the peer is not found or not reachable
   * @throws I2PException if there is some other I2P-related problem
   */
  public I2PSocket connect(Destination peer, I2PSocketOptions options)
      throws I2PException, NoRouteToHostException {
    if (options == null) options = _defaultOptions;
    ConnectionOptions opts = null;
    if (options instanceof ConnectionOptions)
      opts = new ConnectionOptions((ConnectionOptions) options);
    else opts = new ConnectionOptions(options);

    if (_log.shouldLog(Log.INFO))
      _log.info(
          "Connecting to "
              + peer.calculateHash().toBase64().substring(0, 6)
              + " with options: "
              + opts);
    // pick the subsession here
    I2PSession session = _session;
    if (!_subsessions.isEmpty()) {
      updateUserDsaList();
      Hash h = peer.calculateHash();
      if (_dsaOnly.contains(h) || (!_userDsaOnly.isEmpty() && _userDsaOnly.contains(h))) {
        // FIXME just taking the first one for now
        for (I2PSession sess : _subsessions) {
          if (sess.getMyDestination().getSigType() == SigType.DSA_SHA1) {
            session = sess;
            break;
          }
        }
      }
    }
    verifySession(session);
    // the following blocks unless connect delay > 0
    Connection con = _connectionManager.connect(peer, opts, session);
    if (con == null)
      throw new TooManyStreamsException("Too many streams, max " + _defaultOptions.getMaxConns());
    I2PSocketFull socket = new I2PSocketFull(con, _context);
    con.setSocket(socket);
    if (con.getConnectionError() != null) {
      con.disconnect(false);
      throw new NoRouteToHostException(con.getConnectionError());
    }
    return socket;
  }
 /**
  * @return a new subsession, non-null
  * @param privateKeyStream null for transient, if non-null must have same encryption keys as
  *     primary session and different signing keys
  * @param opts subsession options if any, may be null
  * @since 0.9.21
  */
 public I2PSession addSubsession(InputStream privateKeyStream, Properties opts)
     throws I2PSessionException {
   if (privateKeyStream == null) {
     // We don't actually need the same pubkey in the dest, just in the LS.
     // The dest one is unused. But this is how we find the LS keys
     // to reuse in RequestLeaseSetMessageHandler.
     ByteArrayOutputStream keyStream = new ByteArrayOutputStream(1024);
     try {
       SigType type = getSigType(opts);
       if (type != SigType.DSA_SHA1) {
         // hassle, have to set up the padding and cert, see I2PClientImpl
         throw new I2PSessionException("type " + type + " unsupported");
       }
       PublicKey pub = _session.getMyDestination().getPublicKey();
       PrivateKey priv = _session.getDecryptionKey();
       SimpleDataStructure[] keys = _context.keyGenerator().generateSigningKeys(type);
       pub.writeBytes(keyStream);
       keys[0].writeBytes(keyStream); // signing pub
       Certificate.NULL_CERT.writeBytes(keyStream);
       priv.writeBytes(keyStream);
       keys[1].writeBytes(keyStream); // signing priv
     } catch (Exception e) {
       throw new I2PSessionException("Error creating keys", e);
     }
     privateKeyStream = new ByteArrayInputStream(keyStream.toByteArray());
   }
   I2PSession rv = _session.addSubsession(privateKeyStream, opts);
   boolean added = _subsessions.add(rv);
   if (!added) {
     // shouldn't happen
     _session.removeSubsession(rv);
     throw new I2PSessionException("dup");
   }
   ConnectionOptions defaultOptions = new ConnectionOptions(opts);
   int protocol =
       defaultOptions.getEnforceProtocol() ? I2PSession.PROTO_STREAMING : I2PSession.PROTO_ANY;
   rv.addMuxedSessionListener(
       _connectionManager.getMessageHandler(), protocol, defaultOptions.getLocalPort());
   if (_log.shouldLog(Log.WARN)) _log.warn("Added subsession " + rv);
   return rv;
 }
 /**
  * @return dest or null
  * @since 0.8.4
  */
 Destination getMyDestination() {
   if (_manager == null) return null;
   I2PSession sess = _manager.getSession();
   if (sess != null) return sess.getMyDestination();
   return null;
 }
Example #4
0
 @Override
 public String getPublicDestinationInfo() {
   I2PSession ses = manager.getSession();
   return ses.getMyDestination().toBase64();
 }