use of net.i2p.util.Log in project i2p.i2p by i2p.
the class Subscriber method send.
/**
* Doesn't really "send" anywhere, just subscribes or unsubscribes the destination
*
* @param dest to subscribe or unsubscribe
* @param data must be a single byte, 0 to subscribe, 1 to unsubscribe
*/
public void send(Destination dest, byte[] data) {
if (dest == null || data.length < 1) {
// invalid packet
Log log = I2PAppContext.getGlobalContext().logManager().getLog(getClass());
if (log.shouldWarn())
log.warn("bad subscription from " + dest);
} else {
byte ctrl = data[0];
if (ctrl == 0) {
if (!this.subscriptions.contains(dest)) {
// subscribe
System.out.println("Add subscription: " + dest.toBase64().substring(0, 4));
this.subscriptions.add(dest);
this.multi.add(dest);
}
// else already subscribed
} else if (ctrl == 1) {
// unsubscribe
System.out.println("Remove subscription: " + dest.toBase64().substring(0, 4));
boolean removed = this.subscriptions.remove(dest);
if (removed)
multi.remove(dest);
} else {
// invalid packet
Log log = I2PAppContext.getGlobalContext().logManager().getLog(getClass());
if (log.shouldWarn())
log.warn("bad subscription from " + dest);
}
}
}
use of net.i2p.util.Log in project i2p.i2p by i2p.
the class SessionConfig method getBytes.
private byte[] getBytes() {
if (_destination == null)
return null;
if (_options == null)
return null;
if (_creationDate == null)
return null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
// _log.debug("PubKey size for destination: " + _destination.getPublicKey().getData().length);
// _log.debug("SigningKey size for destination: " + _destination.getSigningPublicKey().getData().length);
_destination.writeBytes(out);
// UTF-8
DataHelper.writeProperties(out, _options, true);
DataHelper.writeDate(out, _creationDate);
} catch (IOException ioe) {
Log log = I2PAppContext.getGlobalContext().logManager().getLog(SessionConfig.class);
log.error("IOError signing", ioe);
return null;
} catch (DataFormatException dfe) {
Log log = I2PAppContext.getGlobalContext().logManager().getLog(SessionConfig.class);
log.error("Error writing out the bytes for signing/verification", dfe);
return null;
}
return out.toByteArray();
}
use of net.i2p.util.Log in project i2p.i2p by i2p.
the class MTU method getMTU.
/**
* The MTU for the socket interface, if available.
* Not available for Java 5.
*
* Note that we don't return the value for the default interface if
* we can't find the address. Finding the default interface is hard,
* altough we could perhaps just look for the first non-loopback address.
* But the MTU of the default route probably isn't relevant.
*
* @param ia null ok
* @return 0 if Java 5, or if not bound to an address;
* limited to range MIN_MTU to LARGE_MTU.
*/
public static int getMTU(InetAddress ia) {
if (ia == null || !hasMTU)
return 0;
Enumeration<NetworkInterface> ifcs;
try {
ifcs = NetworkInterface.getNetworkInterfaces();
} catch (SocketException se) {
return 0;
} catch (java.lang.Error e) {
// at net.i2p.util.Addresses.getAddresses ...
return 0;
}
if (ifcs != null) {
while (ifcs.hasMoreElements()) {
NetworkInterface ifc = ifcs.nextElement();
for (Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements(); ) {
InetAddress addr = addrs.nextElement();
if (ia.equals(addr)) {
try {
// testing
// return ifc.getMTU();
boolean isIPv6 = addr instanceof Inet6Address;
int mtu = ifc.getMTU();
if ((isIPv6 && mtu < PeerState.MIN_IPV6_MTU) || (!isIPv6 && mtu < PeerState.MIN_MTU)) {
Log log = I2PAppContext.getGlobalContext().logManager().getLog(MTU.class);
log.logAlways(Log.WARN, "Unusually low MTU " + mtu + " for interface " + ia + ", consider disabling");
}
return rectify(isIPv6, mtu);
} catch (SocketException se) {
// ignore
} catch (Throwable t) {
// version detection wrong or the JVM doesn't support it
return 0;
}
}
}
}
}
return 0;
}
use of net.i2p.util.Log in project i2p.i2p by i2p.
the class BuildRequestor method createTunnelBuildMessage.
/**
* @since 0.7.12
*/
/**
**
*we can assume everybody supports variable now...
*keep this here for the next time we change the build protocol
* private static boolean supportsVariable(RouterContext ctx, Hash h) {
* RouterInfo ri = ctx.netDb().lookupRouterInfoLocally(h);
* if (ri == null)
* return false;
* String v = ri.getVersion();
* return VersionComparator.comp(v, MIN_VARIABLE_VERSION) >= 0;
* }
***
*/
/**
* If the tunnel is short enough, and everybody in the tunnel, and the
* OBEP or IBGW for the paired tunnel, all support the new variable-sized tunnel build message,
* then use that, otherwise the old 8-entry version.
* @return null on error
*/
private static TunnelBuildMessage createTunnelBuildMessage(RouterContext ctx, TunnelPool pool, PooledTunnelCreatorConfig cfg, TunnelInfo pairedTunnel, BuildExecutor exec) {
Log log = ctx.logManager().getLog(BuildRequestor.class);
long replyTunnel = 0;
Hash replyRouter;
boolean useVariable = SEND_VARIABLE && cfg.getLength() <= MEDIUM_RECORDS;
if (cfg.isInbound()) {
// replyTunnel = 0; // as above
replyRouter = ctx.routerHash();
/**
**
*we can assume everybody supports variable now...
*keep this here for the next time we change the build protocol
* if (useVariable) {
* // check the reply OBEP and all the tunnel peers except ourselves
* if (!supportsVariable(ctx, pairedTunnel.getPeer(pairedTunnel.getLength() - 1))) {
* useVariable = false;
* } else {
* for (int i = 0; i < cfg.getLength() - 1; i++) {
* if (!supportsVariable(ctx, cfg.getPeer(i))) {
* useVariable = false;
* break;
* }
* }
* }
* }
***
*/
} else {
replyTunnel = pairedTunnel.getReceiveTunnelId(0).getTunnelId();
replyRouter = pairedTunnel.getPeer(0);
/**
**
*we can assume everybody supports variable now
*keep this here for the next time we change the build protocol
* if (useVariable) {
* // check the reply IBGW and all the tunnel peers except ourselves
* if (!supportsVariable(ctx, replyRouter)) {
* useVariable = false;
* } else {
* for (int i = 1; i < cfg.getLength() - 1; i++) {
* if (!supportsVariable(ctx, cfg.getPeer(i))) {
* useVariable = false;
* break;
* }
* }
* }
* }
***
*/
}
// populate and encrypt the message
TunnelBuildMessage msg;
List<Integer> order;
if (useVariable) {
if (cfg.getLength() <= SHORT_RECORDS) {
msg = new VariableTunnelBuildMessage(ctx, SHORT_RECORDS);
order = new ArrayList<Integer>(SHORT_ORDER);
} else {
msg = new VariableTunnelBuildMessage(ctx, MEDIUM_RECORDS);
order = new ArrayList<Integer>(MEDIUM_ORDER);
}
} else {
msg = new TunnelBuildMessage(ctx);
order = new ArrayList<Integer>(ORDER);
}
// This is in BuildExecutor.buildTunnel() now
// long replyMessageId = ctx.random().nextLong(I2NPMessage.MAX_ID_VALUE);
// cfg.setReplyMessageId(replyMessageId);
// randomized placement within the message
Collections.shuffle(order, ctx.random());
cfg.setReplyOrder(order);
if (log.shouldLog(Log.DEBUG))
log.debug("Build order: " + order + " for " + cfg);
for (int i = 0; i < msg.getRecordCount(); i++) {
int hop = order.get(i).intValue();
PublicKey key = null;
if (BuildMessageGenerator.isBlank(cfg, hop)) {
// erm, blank
} else {
Hash peer = cfg.getPeer(hop);
RouterInfo peerInfo = ctx.netDb().lookupRouterInfoLocally(peer);
if (peerInfo == null) {
if (log.shouldLog(Log.WARN))
log.warn("Peer selected for hop " + i + "/" + hop + " was not found locally: " + peer + " for " + cfg);
return null;
} else {
key = peerInfo.getIdentity().getPublicKey();
}
}
if (log.shouldLog(Log.DEBUG))
log.debug(cfg.getReplyMessageId() + ": record " + i + "/" + hop + " has key " + key);
BuildMessageGenerator.createRecord(i, hop, msg, cfg, replyRouter, replyTunnel, ctx, key);
}
BuildMessageGenerator.layeredEncrypt(ctx, msg, cfg, order);
return msg;
}
use of net.i2p.util.Log in project i2p.i2p by i2p.
the class BuildRequestor method buildZeroHop.
private static void buildZeroHop(RouterContext ctx, TunnelPool pool, PooledTunnelCreatorConfig cfg, BuildExecutor exec) {
Log log = ctx.logManager().getLog(BuildRequestor.class);
if (log.shouldLog(Log.DEBUG))
log.debug("Build zero hop tunnel " + cfg);
exec.buildComplete(cfg, pool);
if (cfg.isInbound())
ctx.tunnelDispatcher().joinInbound(cfg);
else
ctx.tunnelDispatcher().joinOutbound(cfg);
pool.addTunnel(cfg);
exec.buildSuccessful(cfg);
ExpireJob expireJob = new ExpireJob(ctx, cfg, pool);
cfg.setExpireJob(expireJob);
ctx.jobQueue().addJob(expireJob);
// can it get much easier?
}
Aggregations