Search in sources :

Example 16 with Log

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);
        }
    }
}
Also used : Log(net.i2p.util.Log)

Example 17 with Log

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();
}
Also used : DataFormatException(net.i2p.data.DataFormatException) Log(net.i2p.util.Log) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 18 with Log

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;
}
Also used : SocketException(java.net.SocketException) Log(net.i2p.util.Log) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 19 with Log

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;
}
Also used : Log(net.i2p.util.Log) PublicKey(net.i2p.data.PublicKey) RouterInfo(net.i2p.data.router.RouterInfo) VariableTunnelBuildMessage(net.i2p.data.i2np.VariableTunnelBuildMessage) TunnelBuildMessage(net.i2p.data.i2np.TunnelBuildMessage) VariableTunnelBuildMessage(net.i2p.data.i2np.VariableTunnelBuildMessage) Hash(net.i2p.data.Hash)

Example 20 with Log

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?
}
Also used : Log(net.i2p.util.Log)

Aggregations

Log (net.i2p.util.Log)94 IOException (java.io.IOException)30 File (java.io.File)13 Properties (java.util.Properties)11 DataFormatException (net.i2p.data.DataFormatException)11 FileInputStream (java.io.FileInputStream)7 GeneralSecurityException (java.security.GeneralSecurityException)7 ArrayList (java.util.ArrayList)7 Hash (net.i2p.data.Hash)6 HashMap (java.util.HashMap)5 InputStream (java.io.InputStream)4 EventLog (net.i2p.router.util.EventLog)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 I2PAppContext (net.i2p.I2PAppContext)3 I2PSession (net.i2p.client.I2PSession)3 I2PSessionException (net.i2p.client.I2PSessionException)3 SigType (net.i2p.crypto.SigType)3 RouterInfo (net.i2p.data.router.RouterInfo)3