use of net.i2p.data.i2cp.SessionId in project i2p.i2p by i2p.
the class LocalClientMessageEventListener method handleHostLookup.
/**
* Look only in current local dests
*/
@Override
protected void handleHostLookup(HostLookupMessage message) {
Hash h = message.getHash();
String name = message.getHostname();
long reqID = message.getReqID();
SessionId sessID = message.getSessionId();
if (h == null && name != null && name.length() == 60) {
// convert a b32 lookup to a hash lookup
String nlc = name.toLowerCase(Locale.US);
if (nlc.endsWith(".b32.i2p")) {
byte[] b = Base32.decode(nlc.substring(0, 52));
if (b != null && b.length == Hash.HASH_LENGTH) {
h = Hash.create(b);
}
}
}
Destination d = null;
if (h != null)
d = ((LocalClientConnectionRunner) _runner).localLookup(h);
HostReplyMessage msg;
if (d != null)
msg = new HostReplyMessage(sessID, d, reqID);
else
msg = new HostReplyMessage(sessID, HostReplyMessage.RESULT_FAILURE, reqID);
try {
_runner.doSend(msg);
} catch (I2CPMessageException ime) {
ime.printStackTrace();
}
}
use of net.i2p.data.i2cp.SessionId in project i2p.i2p by i2p.
the class I2CPMessageProducer method updateTunnels.
/**
* Update number of tunnels
*
* @param tunnels 0 for original configured number
*/
public void updateTunnels(I2PSessionImpl session, int tunnels) throws I2PSessionException {
ReconfigureSessionMessage msg = new ReconfigureSessionMessage();
SessionConfig cfg = new SessionConfig(session.getMyDestination());
Properties props = session.getOptions();
if (tunnels > 0) {
Properties newprops = new Properties();
newprops.putAll(props);
props = newprops;
props.setProperty("inbound.quantity", "" + tunnels);
props.setProperty("outbound.quantity", "" + tunnels);
props.setProperty("inbound.backupQuantity", "0");
props.setProperty("outbound.backupQuantity", "0");
}
cfg.setOptions(props);
try {
cfg.signSessionConfig(session.getPrivateKey());
} catch (DataFormatException dfe) {
throw new I2PSessionException("Unable to sign the session config", dfe);
}
msg.setSessionConfig(cfg);
SessionId sid = session.getSessionId();
if (sid == null) {
_log.error(session.toString() + " update config w/o session", new Exception());
return;
}
msg.setSessionId(sid);
session.sendMessage(msg);
}
use of net.i2p.data.i2cp.SessionId in project i2p.i2p by i2p.
the class I2CPMessageProducer method sendMessage.
/**
* Package up and send the payload to the router for delivery
*
* @param nonce 0 to 0xffffffff; if 0, the router will not reply with a MessageStatusMessage
* @since 0.8.4
*/
public void sendMessage(I2PSessionImpl session, Destination dest, long nonce, byte[] payload, long expires, int flags) throws I2PSessionException {
if (!updateBps(payload.length, expires))
// drop the message... send fail notification?
return;
SendMessageMessage msg;
if (expires > 0 || flags > 0) {
SendMessageExpiresMessage smsg = new SendMessageExpiresMessage();
smsg.setExpiration(expires);
smsg.setFlags(flags);
msg = smsg;
} else
msg = new SendMessageMessage();
msg.setDestination(dest);
SessionId sid = session.getSessionId();
if (sid == null) {
_log.error(session.toString() + " send message w/o session", new Exception());
return;
}
msg.setSessionId(sid);
msg.setNonce(nonce);
Payload data = createPayload(dest, payload, null, null, null, null);
msg.setPayload(data);
session.sendMessage(msg);
}
use of net.i2p.data.i2cp.SessionId in project i2p.i2p by i2p.
the class I2PSessionImpl method getPrefix.
/**
* try hard to make a decent identifier as this will appear in error logs
*/
protected String getPrefix() {
StringBuilder buf = new StringBuilder();
buf.append('[');
buf.append(_state.toString()).append(' ');
String s = _options.getProperty("inbound.nickname");
if (s != null)
buf.append(s);
else
buf.append(getClass().getSimpleName());
SessionId id = _sessionId;
if (id != null)
buf.append(" #").append(id.getSessionId());
buf.append("]: ");
return buf.toString();
}
use of net.i2p.data.i2cp.SessionId 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;
}
Aggregations