Search in sources :

Example 1 with HostLookupMessage

use of net.i2p.data.i2cp.HostLookupMessage in project i2p.i2p by i2p.

the class I2PSessionImpl method lookupDest.

/**
 *  Ask the router to lookup a Destination by host name.
 *  Blocking. See above for details.
 *  @param maxWait ms
 *  @since 0.9.11
 *  @return null on failure
 */
public Destination lookupDest(String name, long maxWait) throws I2PSessionException {
    if (name.length() == 0)
        return null;
    // Shortcut for b64
    if (name.length() >= 516) {
        try {
            return new Destination(name);
        } catch (DataFormatException dfe) {
            return null;
        }
    }
    // won't fit in Mapping
    if (name.length() >= 256 && !_context.isRouterContext())
        return null;
    synchronized (_lookupCache) {
        Destination rv = _lookupCache.get(name);
        if (rv != null)
            return rv;
    }
    if (isClosed()) {
        if (_log.shouldLog(Log.INFO))
            _log.info("Session closed, cannot lookup " + name);
        return null;
    }
    if (!_routerSupportsHostLookup) {
        // do them a favor and convert to Hash lookup
        if (name.length() == 60 && name.toLowerCase(Locale.US).endsWith(".b32.i2p"))
            return lookupDest(Hash.create(Base32.decode(name.toLowerCase(Locale.US).substring(0, 52))), maxWait);
        // else unsupported
        if (_log.shouldLog(Log.WARN))
            _log.warn("Router does not support HostLookup for " + name);
        return null;
    }
    int nonce = _lookupID.incrementAndGet() & 0x7fffffff;
    LookupWaiter waiter = new LookupWaiter(name, nonce);
    _pendingLookups.offer(waiter);
    Destination rv = null;
    try {
        if (_log.shouldLog(Log.INFO))
            _log.info("Sending HostLookup for " + name);
        SessionId id = _sessionId;
        if (id == null)
            id = new SessionId(65535);
        sendMessage_unchecked(new HostLookupMessage(id, name, nonce, maxWait));
        try {
            synchronized (waiter) {
                waiter.wait(maxWait);
                rv = waiter.destination;
            }
        } catch (InterruptedException ie) {
            throw new I2PSessionException("Interrupted", ie);
        }
    } finally {
        _pendingLookups.remove(waiter);
    }
    return rv;
}
Also used : Destination(net.i2p.data.Destination) DataFormatException(net.i2p.data.DataFormatException) HostLookupMessage(net.i2p.data.i2cp.HostLookupMessage) I2PSessionException(net.i2p.client.I2PSessionException) SessionId(net.i2p.data.i2cp.SessionId)

Example 2 with HostLookupMessage

use of net.i2p.data.i2cp.HostLookupMessage in project i2p.i2p by i2p.

the class I2PSessionImpl method lookupDest.

/**
 *  Blocking.
 *  @param maxWait ms
 *  @since 0.8.3
 *  @return null on failure
 */
public Destination lookupDest(Hash h, long maxWait) throws I2PSessionException {
    synchronized (_lookupCache) {
        Destination rv = _lookupCache.get(h);
        if (rv != null)
            return rv;
    }
    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 lookup " + h);
            return null;
        }
    }
    LookupWaiter waiter;
    long nonce;
    if (_routerSupportsHostLookup) {
        nonce = _lookupID.incrementAndGet() & 0x7fffffff;
        waiter = new LookupWaiter(h, nonce);
    } else {
        // won't be used
        nonce = 0;
        waiter = new LookupWaiter(h);
    }
    _pendingLookups.offer(waiter);
    Destination rv = null;
    try {
        if (_routerSupportsHostLookup) {
            if (_log.shouldLog(Log.INFO))
                _log.info("Sending HostLookup for " + h);
            SessionId id = _sessionId;
            if (id == null)
                id = new SessionId(65535);
            sendMessage_unchecked(new HostLookupMessage(id, h, nonce, maxWait));
        } else {
            if (_log.shouldLog(Log.INFO))
                _log.info("Sending DestLookup for " + h);
            sendMessage_unchecked(new DestLookupMessage(h));
        }
        try {
            synchronized (waiter) {
                waiter.wait(maxWait);
                rv = waiter.destination;
            }
        } catch (InterruptedException ie) {
            throw new I2PSessionException("Interrupted", ie);
        }
    } finally {
        _pendingLookups.remove(waiter);
    }
    return rv;
}
Also used : Destination(net.i2p.data.Destination) HostLookupMessage(net.i2p.data.i2cp.HostLookupMessage) I2PSessionException(net.i2p.client.I2PSessionException) DestLookupMessage(net.i2p.data.i2cp.DestLookupMessage) SessionId(net.i2p.data.i2cp.SessionId)

Aggregations

I2PSessionException (net.i2p.client.I2PSessionException)2 Destination (net.i2p.data.Destination)2 HostLookupMessage (net.i2p.data.i2cp.HostLookupMessage)2 SessionId (net.i2p.data.i2cp.SessionId)2 DataFormatException (net.i2p.data.DataFormatException)1 DestLookupMessage (net.i2p.data.i2cp.DestLookupMessage)1