Search in sources :

Example 31 with Log

use of net.i2p.util.Log in project i2p.i2p by i2p.

the class UDPPacket method acquire.

/**
 *  @param inbound unused
 */
public static UDPPacket acquire(RouterContext ctx, boolean inbound) {
    UDPPacket rv = null;
    if (CACHE) {
        rv = _packetCache.poll();
        if (rv != null) {
            synchronized (rv) {
                if (!rv._released) {
                    Log log = rv._context.logManager().getLog(UDPPacket.class);
                    log.error("Unreleased cached packet", new Exception());
                    rv = null;
                } else {
                    rv.init(ctx);
                }
            }
        }
    }
    if (rv == null)
        rv = new UDPPacket(ctx);
    return rv;
}
Also used : Log(net.i2p.util.Log)

Example 32 with Log

use of net.i2p.util.Log in project i2p.i2p by i2p.

the class UDPPacket method validate.

/**
 * Validate the packet against the MAC specified, returning true if the
 * MAC matches, false otherwise.
 */
public synchronized boolean validate(SessionKey macKey) {
    verifyNotReleased();
    // _beforeValidate = _context.clock().now();
    boolean eq = false;
    Arrays.fill(_validateBuf, (byte) 0);
    // validate by comparing _data[0:15] and
    // HMAC(payload + IV + (payloadLength ^ protocolVersion), macKey)
    int payloadLength = _packet.getLength() - MAC_SIZE - IV_SIZE;
    if (payloadLength > 0) {
        int off = 0;
        System.arraycopy(_data, _packet.getOffset() + MAC_SIZE + IV_SIZE, _validateBuf, off, payloadLength);
        off += payloadLength;
        System.arraycopy(_data, _packet.getOffset() + MAC_SIZE, _validateBuf, off, IV_SIZE);
        off += IV_SIZE;
        DataHelper.toLong(_validateBuf, off, 2, payloadLength);
        off += 2;
        eq = _context.hmac().verify(macKey, _validateBuf, 0, off, _data, _packet.getOffset(), MAC_SIZE);
        if (!eq) {
            // this is relatively frequent, as you can get old keys in PacketHandler.
            Log log = _context.logManager().getLog(UDPPacket.class);
            if (log.shouldLog(Log.INFO)) {
                byte[] calc = new byte[32];
                _context.hmac().calculate(macKey, _validateBuf, 0, off, calc, 0);
                StringBuilder str = new StringBuilder(512);
                str.append("Bad HMAC:\n\t");
                str.append(_packet.getLength()).append(" byte pkt, ");
                str.append(payloadLength).append(" byte payload");
                str.append("\n\tFrom: ").append(getRemoteHost().toString());
                str.append("\n\tIV:   ").append(Base64.encode(_validateBuf, payloadLength, IV_SIZE));
                str.append("\n\tIV2:  ").append(Base64.encode(_data, MAC_SIZE, IV_SIZE));
                str.append("\n\tGiven Len: ").append(DataHelper.fromLong(_validateBuf, payloadLength + IV_SIZE, 2));
                str.append("\n\tCalc HMAC: ").append(Base64.encode(calc, 0, MAC_SIZE));
                str.append("\n\tRead HMAC: ").append(Base64.encode(_data, _packet.getOffset(), MAC_SIZE));
                str.append("\n\tUsing key: ").append(macKey.toBase64());
                if (DataHelper.eq(macKey.getData(), 0, _context.routerHash().getData(), 0, 32))
                    str.append(" (Intro)");
                else
                    str.append(" (Session)");
                str.append("\n\tRaw:       ").append(Base64.encode(_data, _packet.getOffset(), _packet.getLength()));
                log.info(str.toString(), new Exception());
            }
        }
    } else {
        Log log = _context.logManager().getLog(UDPPacket.class);
        if (log.shouldLog(Log.WARN))
            log.warn("Payload length is " + payloadLength + ", too short! From: " + getRemoteHost() + '\n' + net.i2p.util.HexDump.dump(_data, _packet.getOffset(), _packet.getLength()));
    }
    // _afterValidate = _context.clock().now();
    _validateCount++;
    return eq;
}
Also used : Log(net.i2p.util.Log)

Example 33 with Log

use of net.i2p.util.Log in project i2p.i2p by i2p.

the class Router method getConfig.

/**
 *  this does not use ctx.getConfigDir(), must provide a full path in filename
 *  Caller must synchronize
 *
 *  @param ctx will be null at startup when called from constructor
 */
private static Properties getConfig(RouterContext ctx, String filename) {
    Log log = null;
    if (ctx != null) {
        log = ctx.logManager().getLog(Router.class);
        if (log.shouldLog(Log.DEBUG))
            log.debug("Config file: " + filename, new Exception("location"));
    }
    Properties props = new Properties();
    try {
        File f = new File(filename);
        if (f.canRead()) {
            DataHelper.loadProps(props, f);
            props.remove(PROP_SHUTDOWN_IN_PROGRESS);
        } else {
            if (log != null)
                log.warn("Configuration file " + filename + " does not exist");
        // normal not to exist at first install
        // else
        // System.err.println("WARNING: Configuration file " + filename + " does not exist");
        }
    } catch (IOException ioe) {
        if (log != null)
            log.error("Error loading the router configuration from " + filename, ioe);
        else
            System.err.println("Error loading the router configuration from " + filename + ": " + ioe);
    }
    return props;
}
Also used : EventLog(net.i2p.router.util.EventLog) Log(net.i2p.util.Log) IOException(java.io.IOException) OrderedProperties(net.i2p.util.OrderedProperties) Properties(java.util.Properties) File(java.io.File) GeneralSecurityException(java.security.GeneralSecurityException) DataFormatException(net.i2p.data.DataFormatException) IOException(java.io.IOException)

Example 34 with Log

use of net.i2p.util.Log in project i2p.i2p by i2p.

the class RouterClock method setOffset.

/**
 * @since 0.7.12
 * @param offsetMs the delta from System.currentTimeMillis() (NOT the delta from now())
 */
private synchronized void setOffset(long offsetMs, boolean force, int stratum) {
    long delta = offsetMs - _offset;
    if (!force) {
        if (!_isSystemClockBad && (offsetMs > MAX_OFFSET || offsetMs < 0 - MAX_OFFSET)) {
            Log log = getLog();
            if (log.shouldLog(Log.WARN))
                log.warn("Maximum offset shift exceeded [" + offsetMs + "], NOT HONORING IT");
            return;
        }
        // only allow substantial modifications before the first 10 minutes
        if (_alreadyChanged && (System.currentTimeMillis() - _startedOn > 10 * 60 * 1000)) {
            if ((delta > MAX_LIVE_OFFSET) || (delta < 0 - MAX_LIVE_OFFSET)) {
                Log log = getLog();
                if (log.shouldLog(Log.WARN))
                    log.warn("The clock has already been updated, ignoring request to change it by " + delta + " to " + offsetMs, new Exception());
                return;
            }
        }
        // let's be perfect
        if (delta == 0) {
            getLog().debug("Not changing offset, delta=0");
            _alreadyChanged = true;
            return;
        }
        // only listen to a worse stratum if it's been a while
        if (_alreadyChanged && stratum > _lastStratum && System.currentTimeMillis() - _lastChanged < MIN_DELAY_FOR_WORSE_STRATUM) {
            Log log = getLog();
            if (log.shouldLog(Log.DEBUG))
                log.debug("Ignoring update from a stratum " + stratum + " clock, we recently had an update from a stratum " + _lastStratum + " clock");
            return;
        }
        // If so configured, check sanity of proposed clock offset
        if (_context.getBooleanPropertyDefaultTrue("router.clockOffsetSanityCheck") && _alreadyChanged) {
            // Try calculating peer clock skew
            long currentPeerClockSkew = ((RouterContext) _context).commSystem().getFramedAveragePeerClockSkew(33);
            // Predict the effect of applying the proposed clock offset
            long predictedPeerClockSkew = currentPeerClockSkew + delta;
            // Fail sanity check if applying the offset would increase peer clock skew
            Log log = getLog();
            if ((Math.abs(predictedPeerClockSkew) > (Math.abs(currentPeerClockSkew) + 5 * 1000)) || (Math.abs(predictedPeerClockSkew) > 20 * 1000)) {
                if (log.shouldWarn())
                    log.warn("Ignoring clock offset " + offsetMs + "ms (current " + _offset + "ms) since it would increase peer clock skew from " + currentPeerClockSkew + "ms to " + predictedPeerClockSkew + "ms. Stratrum: " + stratum);
                return;
            } else {
                if (log.shouldInfo())
                    log.info("Approving clock offset " + offsetMs + "ms (current " + _offset + "ms) since it would decrease peer clock skew from " + currentPeerClockSkew + "ms to " + predictedPeerClockSkew + "ms. Stratrum: " + stratum);
            }
        }
    // check sanity
    }
    // This allows NTP to trump a peer offset after a soft restart
    if (_alreadyChanged && (stratum >= _lastStratum || System.currentTimeMillis() - _startedOn > 60 * 1000)) {
        // Update the target offset, slewing will take care of the rest
        if (delta > 15 * 1000)
            getLog().logAlways(Log.WARN, "Warning - Updating target clock offset to " + offsetMs + "ms from " + _offset + "ms, Stratum " + stratum);
        else if (getLog().shouldLog(Log.INFO))
            getLog().info("Updating target clock offset to " + offsetMs + "ms from " + _offset + "ms, Stratum " + stratum);
        if (!_statCreated) {
            _context.statManager().createRequiredRateStat("clock.skew", "Clock step adjustment (ms)", "Clock", new long[] { 10 * 60 * 1000, 3 * 60 * 60 * 1000, 24 * 60 * 60 * 1000 });
            _statCreated = true;
        }
        _context.statManager().addRateData("clock.skew", delta);
        if (_context.getBooleanProperty(PROP_DISABLE_ADJUSTMENT)) {
            getLog().error("Clock adjustment disabled", new Exception());
        } else {
            _desiredOffset = offsetMs;
        }
    } else {
        Log log = getLog();
        if (log.shouldLog(Log.INFO))
            log.info("Initializing clock offset to " + offsetMs + "ms, Stratum " + stratum);
        _alreadyChanged = true;
        if (_context.getBooleanProperty(PROP_DISABLE_ADJUSTMENT)) {
            log.error("Clock adjustment disabled", new Exception());
        } else {
            _offset = offsetMs;
            _desiredOffset = offsetMs;
            // this is used by the JobQueue
            fireOffsetChanged(delta);
        }
    }
    _lastChanged = System.currentTimeMillis();
    _lastStratum = stratum;
}
Also used : Log(net.i2p.util.Log)

Example 35 with Log

use of net.i2p.util.Log in project i2p.i2p by i2p.

the class CryptoChecker method warnUnavailableCrypto.

// these two are US-only and can change?
// private static String JRE7 = "http://www.oracle.com/technetwork/java/javase/documentation/java-se-7-doc-download-435117.html";
// private static String JRE8 = "http://www.oracle.com/technetwork/java/javase/documentation/jdk8-doc-downloads-2133158.html";
/**
 *  @param ctx if null, logs only to System.out (called from main)
 */
public static void warnUnavailableCrypto(RouterContext ctx) {
    if (SystemVersion.isAndroid())
        return;
    boolean unavail = false;
    Log log = null;
    for (SigType t : SigType.values()) {
        if (!t.isAvailable()) {
            if (!unavail) {
                unavail = true;
                if (ctx != null)
                    log = ctx.logManager().getLog(CryptoChecker.class);
            }
            String s = "Crypto " + t + " is not available";
            if (log != null)
                log.logAlways(Log.WARN, s);
            System.out.println("Warning: " + s);
        }
    }
    if (unavail) {
        String s = "Java version: " + System.getProperty("java.version") + " OS: " + System.getProperty("os.name") + ' ' + System.getProperty("os.arch") + ' ' + System.getProperty("os.version");
        if (log != null)
            log.logAlways(Log.WARN, s);
        System.out.println("Warning: " + s);
        if (!SystemVersion.isJava7()) {
            s = "Please consider upgrading to Java 7";
            if (log != null)
                log.logAlways(Log.WARN, s);
            System.out.println(s);
        } else if (SystemVersion.isJava9()) {
            s = "Java 9 support is beta, check for Java updates";
            if (log != null)
                log.logAlways(Log.WARN, s);
            System.out.println("Warning: " + s);
        }
        if (!CryptoCheck.isUnlimited() && !SystemVersion.isJava9()) {
            s = "Please consider installing the Java Cryptography Unlimited Strength Jurisdiction Policy Files from ";
            // if (SystemVersion.isJava8())
            // s  += JRE8;
            // else if (SystemVersion.isJava7())
            // s  += JRE7;
            // else
            s += JRE6;
            if (log != null)
                log.logAlways(Log.WARN, s);
            System.out.println(s);
        }
        s = "This crypto will be required in a future release";
        if (log != null)
            log.logAlways(Log.WARN, s);
        System.out.println("Warning: " + s);
    } else if (ctx == null) {
        // called from main()
        System.out.println("All crypto available");
    }
}
Also used : Log(net.i2p.util.Log) SigType(net.i2p.crypto.SigType)

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