Search in sources :

Example 51 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class ConvertToHash method getHash.

/**
 *  Convert any kind of destination String to a hash
 *
 *  @return null on failure
 */
public static Hash getHash(String peer) {
    if (peer == null)
        return null;
    String peerLC = peer.toLowerCase(Locale.US);
    // b64 hash
    if (peer.length() == 44 && !peerLC.endsWith(".i2p")) {
        byte[] b = Base64.decode(peer);
        if (b != null && b.length == Hash.HASH_LENGTH)
            return Hash.create(b);
    }
    // b64 hash.i2p
    if (peer.length() == 48 && peerLC.endsWith(".i2p")) {
        byte[] b = Base64.decode(peer.substring(0, 44));
        if (b != null && b.length == Hash.HASH_LENGTH)
            return Hash.create(b);
    }
    // b64 dest.i2p
    if (peer.length() >= 520 && peerLC.endsWith(".i2p")) {
        try {
            Destination d = new Destination();
            d.fromBase64(peer.substring(0, peer.length() - 4));
            return d.calculateHash();
        } catch (DataFormatException dfe) {
        }
    }
    // b64 dest
    if (peer.length() >= 516 && !peerLC.endsWith(".i2p")) {
        try {
            Destination d = new Destination();
            d.fromBase64(peer);
            return d.calculateHash();
        } catch (DataFormatException dfe) {
        }
    }
    // even if the leaseset is not found
    if (peer.length() == 60 && peerLC.endsWith(".b32.i2p")) {
        byte[] b = Base32.decode(peer.substring(0, 52));
        if (b != null && b.length == Hash.HASH_LENGTH)
            return Hash.create(b);
    }
    // b32 hash
    if (peer.length() == 52 && !peerLC.endsWith(".i2p")) {
        byte[] b = Base32.decode(peer);
        if (b != null && b.length == Hash.HASH_LENGTH)
            return Hash.create(b);
    }
    // example.i2p
    Destination d = I2PAppContext.getGlobalContext().namingService().lookup(peer);
    if (d != null)
        return d.calculateHash();
    return null;
}
Also used : Destination(net.i2p.data.Destination) DataFormatException(net.i2p.data.DataFormatException)

Example 52 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class RouterPrivateKeyFile method getRouterIdentity.

/**
 *  Read it in from the file.
 *  Also sets the local privKey and signingPrivKey.
 */
public RouterIdentity getRouterIdentity() throws IOException, DataFormatException {
    InputStream in = null;
    try {
        in = new BufferedInputStream(new FileInputStream(this.file));
        RouterIdentity ri = new RouterIdentity();
        ri.readBytes(in);
        privKey = new PrivateKey();
        privKey.readBytes(in);
        SigType type = ri.getSigningPublicKey().getType();
        if (type == null)
            throw new DataFormatException("Unknown sig type");
        signingPrivKey = new SigningPrivateKey(type);
        signingPrivKey.readBytes(in);
        // set it a Destination, so we may call validateKeyPairs()
        // or other methods
        dest = new Destination();
        dest.setPublicKey(ri.getPublicKey());
        dest.setSigningPublicKey(ri.getSigningPublicKey());
        dest.setCertificate(ri.getCertificate());
        dest.setPadding(ri.getPadding());
        return ri;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ioe) {
            }
        }
    }
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) Destination(net.i2p.data.Destination) SigningPrivateKey(net.i2p.data.SigningPrivateKey) PrivateKey(net.i2p.data.PrivateKey) DataFormatException(net.i2p.data.DataFormatException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SigType(net.i2p.crypto.SigType)

Example 53 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class ClientConnectionRunner method distributeMessage.

/**
 * Distribute the message.  If the dest is local, it blocks until its passed
 * to the target ClientConnectionRunner (which then fires it into a MessageReceivedJob).
 * If the dest is remote, it blocks until it is added into the ClientMessagePool
 */
MessageId distributeMessage(SendMessageMessage message) {
    Payload payload = message.getPayload();
    Destination dest = message.getDestination();
    MessageId id = new MessageId();
    id.setMessageId(getNextMessageId());
    long expiration = 0;
    int flags = 0;
    if (message.getType() == SendMessageExpiresMessage.MESSAGE_TYPE) {
        SendMessageExpiresMessage msg = (SendMessageExpiresMessage) message;
        expiration = msg.getExpirationTime();
        flags = msg.getFlags();
    }
    if ((!_dontSendMSM) && message.getNonce() != 0)
        _acceptedPending.add(id);
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("** Receiving message " + id.getMessageId() + " with payload of size " + payload.getSize() + " for session " + message.getSessionId());
    // long beforeDistribute = _context.clock().now();
    // the following blocks as described above
    Destination fromDest = getDestination(message.getSessionId());
    if (fromDest != null)
        _manager.distributeMessage(fromDest, dest, payload, id, message.getNonce(), expiration, flags);
    // + timeToDistribute);
    return id;
}
Also used : Destination(net.i2p.data.Destination) SendMessageExpiresMessage(net.i2p.data.i2cp.SendMessageExpiresMessage) Payload(net.i2p.data.Payload) MessageId(net.i2p.data.i2cp.MessageId)

Example 54 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class ClientConnectionRunner method requestLeaseSet.

/**
 * Request that a particular client authorize the Leases contained in the
 * LeaseSet, after which the onCreateJob is queued up.  If that doesn't occur
 * within the timeout specified, queue up the onFailedJob.  This call does not
 * block.
 *
 * Job args are always null, may need some fixups if we start using them.
 *
 * @param h the Destination's hash
 * @param set LeaseSet with requested leases - this object must be updated to contain the
 *            signed version (as well as any changed/added/removed Leases)
 *            The LeaseSet contains Leases and destination only, it is unsigned.
 * @param expirationTime ms to wait before failing
 * @param onCreateJob Job to run after the LeaseSet is authorized, null OK
 * @param onFailedJob Job to run after the timeout passes without receiving authorization, null OK
 */
void requestLeaseSet(Hash h, LeaseSet set, long expirationTime, Job onCreateJob, Job onFailedJob) {
    if (_dead) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Requesting leaseSet from a dead client: " + set);
        if (onFailedJob != null)
            _context.jobQueue().addJob(onFailedJob);
        return;
    }
    SessionParams sp = _sessions.get(h);
    if (sp == null) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Requesting leaseSet for an unknown sesssion");
        return;
    }
    // We can't use LeaseSet.equals() here because the dest, keys, and sig on
    // the new LeaseSet are null. So we compare leases one by one.
    // In addition, the client rewrites the expiration time of all the leases to
    // the earliest one, so we can't use Lease.equals() or Lease.getEndDate().
    // So compare by tunnel ID, and then by gateway.
    // (on the remote possibility that two gateways are using the same ID).
    // TunnelPool.locked_buildNewLeaseSet() ensures that leases are sorted,
    // so the comparison will always work.
    int leases = set.getLeaseCount();
    // synch so _currentLeaseSet isn't changed out from under us
    LeaseSet current = null;
    Destination dest = sp.dest;
    LeaseRequestState state;
    synchronized (this) {
        current = sp.currentLeaseSet;
        if (current != null && current.getLeaseCount() == leases) {
            for (int i = 0; i < leases; i++) {
                if (!current.getLease(i).getTunnelId().equals(set.getLease(i).getTunnelId()))
                    break;
                if (!current.getLease(i).getGateway().equals(set.getLease(i).getGateway()))
                    break;
                if (i == leases - 1) {
                    if (_log.shouldLog(Log.INFO))
                        _log.info("Requested leaseSet hasn't changed");
                    if (onCreateJob != null)
                        _context.jobQueue().addJob(onCreateJob);
                    // no change
                    return;
                }
            }
        }
        if (_log.shouldLog(Log.INFO))
            _log.info("Current leaseSet " + current + "\nNew leaseSet " + set);
        state = sp.leaseRequest;
        if (state != null) {
            LeaseSet requested = state.getRequested();
            LeaseSet granted = state.getGranted();
            long ours = set.getEarliestLeaseDate();
            if (((requested != null) && (requested.getEarliestLeaseDate() > ours)) || ((granted != null) && (granted.getEarliestLeaseDate() > ours))) {
                // theirs is newer
                if (_log.shouldLog(Log.DEBUG))
                    _log.debug("Already requesting, theirs newer, do nothing: " + state);
            } else {
                // ours is newer, so wait a few secs and retry
                set.setDestination(dest);
                Rerequest timer = new Rerequest(set, expirationTime, onCreateJob, onFailedJob);
                sp.rerequestTimer = timer;
                _context.simpleTimer2().addEvent(timer, 3 * 1000);
                if (_log.shouldLog(Log.DEBUG))
                    _log.debug("Already requesting, ours newer, wait 3 sec: " + state);
            }
            // already requesting
            return;
        } else {
            set.setDestination(dest);
            if (current == null && _context.tunnelManager().getOutboundClientTunnelCount(h) <= 0) {
                // at startup of a client, where we don't have a leaseset, wait for
                // an outbound tunnel also, so the client doesn't start sending data
                // before we are ready
                Rerequest timer = new Rerequest(set, expirationTime, onCreateJob, onFailedJob);
                sp.rerequestTimer = timer;
                _context.simpleTimer2().addEvent(timer, 1000);
                if (_log.shouldLog(Log.DEBUG))
                    _log.debug("No current LS but no OB tunnels, wait 1 sec for " + h);
                return;
            } else {
                // so the timer won't fire off with an older LS request
                sp.rerequestTimer = null;
                sp.leaseRequest = state = new LeaseRequestState(onCreateJob, onFailedJob, _context.clock().now() + expirationTime, set);
                if (_log.shouldLog(Log.DEBUG))
                    _log.debug("New request: " + state);
            }
        }
    }
    _context.jobQueue().addJob(new RequestLeaseSetJob(_context, this, state));
}
Also used : LeaseSet(net.i2p.data.LeaseSet) Destination(net.i2p.data.Destination)

Example 55 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class ClientManager method unregisterConnection.

/**
 *  Remove all sessions for this runner.
 */
public void unregisterConnection(ClientConnectionRunner runner) {
    synchronized (_pendingRunners) {
        _pendingRunners.remove(runner);
    }
    List<SessionId> ids = runner.getSessionIds();
    List<Destination> dests = runner.getDestinations();
    if (_log.shouldLog(Log.WARN))
        _log.warn("Unregistering (dropping) a client connection with ids: " + ids);
    synchronized (_runners) {
        for (SessionId id : ids) {
            _runnerSessionIds.remove(id);
        }
        for (Destination dest : dests) {
            _runners.remove(dest);
            _runnersByHash.remove(dest.calculateHash());
        }
        // just in case
        for (Iterator<ClientConnectionRunner> iter = _runners.values().iterator(); iter.hasNext(); ) {
            ClientConnectionRunner r = iter.next();
            if (r.equals(runner))
                iter.remove();
        }
        for (Iterator<ClientConnectionRunner> iter = _runnersByHash.values().iterator(); iter.hasNext(); ) {
            ClientConnectionRunner r = iter.next();
            if (r.equals(runner))
                iter.remove();
        }
    }
}
Also used : Destination(net.i2p.data.Destination) SessionId(net.i2p.data.i2cp.SessionId)

Aggregations

Destination (net.i2p.data.Destination)149 IOException (java.io.IOException)46 DataFormatException (net.i2p.data.DataFormatException)33 Properties (java.util.Properties)29 I2PException (net.i2p.I2PException)26 Hash (net.i2p.data.Hash)18 ArrayList (java.util.ArrayList)13 File (java.io.File)12 I2PSessionException (net.i2p.client.I2PSessionException)12 SigType (net.i2p.crypto.SigType)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 I2PSession (net.i2p.client.I2PSession)10 I2PSocket (net.i2p.client.streaming.I2PSocket)10 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)7 I2PClient (net.i2p.client.I2PClient)7 I2PSocketOptions (net.i2p.client.streaming.I2PSocketOptions)7 Test (org.junit.Test)6