Search in sources :

Example 66 with Log

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

the class ConnectionOptions method error.

private static void error(String s) {
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    Log log = ctx.logManager().getLog(ConnectionOptions.class);
    log.error(s);
}
Also used : I2PAppContext(net.i2p.I2PAppContext) Log(net.i2p.util.Log)

Example 67 with Log

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

the class Packet method verifySignature.

/**
 * Determine whether the signature on the data is valid.
 *
 * @param ctx Application context
 * @param from the Destination the data came from
 * @param buffer data to validate with signature
 * @return true if the signature exists and validates against the data,
 *         false otherwise.
 */
public boolean verifySignature(I2PAppContext ctx, Destination from, byte[] buffer) {
    if (!isFlagSet(FLAG_SIGNATURE_INCLUDED))
        return false;
    if (_optionSignature == null)
        return false;
    // prevent receiveNewSyn() ... !active ... sendReset() ... verifySignature ... NPE
    if (from == null)
        return false;
    int size = writtenSize();
    if (buffer == null)
        buffer = new byte[size];
    SigningPublicKey spk = from.getSigningPublicKey();
    SigType type = spk.getType();
    if (type == null) {
        Log l = ctx.logManager().getLog(Packet.class);
        if (l.shouldLog(Log.WARN))
            l.warn("Unknown sig type in " + from + " cannot verify " + toString());
        return false;
    }
    int written = writePacket(buffer, 0, type.getSigLen());
    if (written != size) {
        ctx.logManager().getLog(Packet.class).error("Written " + written + " size " + size + " for " + toString(), new Exception("moo"));
        return false;
    }
    // on a close or reset packet where we have a signature without a FROM
    if (type != _optionSignature.getType() && type.getSigLen() == _optionSignature.length())
        _optionSignature = new Signature(type, _optionSignature.getData());
    boolean ok = ctx.dsa().verifySignature(_optionSignature, buffer, 0, size, spk);
    if (!ok) {
        Log l = ctx.logManager().getLog(Packet.class);
        if (l.shouldLog(Log.WARN))
            l.warn("Signature failed on " + toString(), new Exception("moo"));
    // if (false) {
    // l.error(Base64.encode(buffer, 0, size));
    // l.error("Signature: " + Base64.encode(_optionSignature.getData()));
    // }
    }
    return ok;
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) Log(net.i2p.util.Log) Signature(net.i2p.data.Signature) SigType(net.i2p.crypto.SigType) DataFormatException(net.i2p.data.DataFormatException) IOException(java.io.IOException)

Example 68 with Log

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

the class RouterInfo method doValidate.

/**
 * Actually validate the signature
 */
private void doValidate() {
    _isValid = super.verifySignature();
    _validated = true;
    if (!_isValid) {
        Log log = I2PAppContext.getGlobalContext().logManager().getLog(RouterInfo.class);
        if (log.shouldWarn()) {
            log.warn("Sig verify fail: " + toString(), new Exception("from"));
        // } else {
        // log.error("RI Sig verify fail: " + _identity.getHash());
        }
    }
}
Also used : Log(net.i2p.util.Log) DataFormatException(net.i2p.data.DataFormatException) IOException(java.io.IOException)

Example 69 with Log

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

the class I2PTunnel method destFromName.

/**
 *  @param i2cpHost may be null
 *  @param i2cpPort may be null
 *  @param user may be null
 *  @param pw may be null
 *  @since 0.9.11
 */
private static Destination destFromName(String name, String i2cpHost, String i2cpPort, boolean isSSL, String user, String pw) throws DataFormatException {
    if ((name == null) || (name.trim().length() <= 0))
        throw new DataFormatException("Empty destination provided");
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    Log log = ctx.logManager().getLog(I2PTunnel.class);
    if (name.startsWith("file:")) {
        Destination result = new Destination();
        byte[] content = null;
        FileInputStream in = null;
        try {
            in = new FileInputStream(name.substring("file:".length()));
            byte[] buf = new byte[1024];
            int read = DataHelper.read(in, buf);
            content = new byte[read];
            System.arraycopy(buf, 0, content, 0, read);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            return null;
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException io) {
                }
        }
        try {
            result.fromByteArray(content);
            return result;
        } catch (RuntimeException ex) {
            if (log.shouldLog(Log.INFO))
                log.info("File is not a binary destination - trying base64");
            try {
                byte[] decoded = Base64.decode(new String(content));
                result.fromByteArray(decoded);
                return result;
            } catch (DataFormatException dfe) {
                if (log.shouldLog(Log.WARN))
                    log.warn("File is not a base64 destination either - failing!");
                return null;
            }
        }
    } else {
        // ask naming service
        name = name.trim();
        NamingService inst = ctx.namingService();
        boolean b32 = name.length() == 60 && name.toLowerCase(Locale.US).endsWith(".b32.i2p");
        Destination d = null;
        if (ctx.isRouterContext() || !b32) {
            // Local lookup.
            // Even though we could do b32 outside router ctx here,
            // we do it below instead so we can set the host and port,
            // which we can't do with lookup()
            d = inst.lookup(name);
            if (d != null || ctx.isRouterContext() || name.length() >= 516)
                return d;
        }
        // Outside router context only,
        // try simple session to ask the router.
        I2PClient client = new I2PSimpleClient();
        Properties opts = new Properties();
        if (i2cpHost != null)
            opts.put(I2PClient.PROP_TCP_HOST, i2cpHost);
        if (i2cpPort != null)
            opts.put(I2PClient.PROP_TCP_PORT, i2cpPort);
        opts.put("i2cp.SSL", Boolean.toString(isSSL));
        if (user != null)
            opts.put("i2cp.username", user);
        if (pw != null)
            opts.put("i2cp.password", pw);
        I2PSession session = null;
        try {
            session = client.createSession(null, opts);
            session.connect();
            d = session.lookupDest(name);
        } catch (I2PSessionException ise) {
            if (log.shouldLog(Log.WARN))
                log.warn("Lookup via router failed", ise);
        } finally {
            if (session != null) {
                try {
                    session.destroySession();
                } catch (I2PSessionException ise) {
                }
            }
        }
        return d;
    }
}
Also used : Destination(net.i2p.data.Destination) I2PAppContext(net.i2p.I2PAppContext) Log(net.i2p.util.Log) IOException(java.io.IOException) OrderedProperties(net.i2p.util.OrderedProperties) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) DataFormatException(net.i2p.data.DataFormatException) NamingService(net.i2p.client.naming.NamingService) I2PSimpleClient(net.i2p.client.I2PSimpleClient) I2PSession(net.i2p.client.I2PSession) I2PSessionException(net.i2p.client.I2PSessionException) I2PClient(net.i2p.client.I2PClient)

Example 70 with Log

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

the class I2PTunnelClientBase method getSocketManager.

/**
 * This is ONLY for shared clients.
 * As of 0.9.20 this is fast, and does NOT connect the manager to the router.
 * Call verifySocketManager() for that.
 *
 * @return non-null
 * @throws IllegalArgumentException if the I2CP configuration is b0rked so
 *                                  badly that we cant create a socketManager
 */
protected static synchronized I2PSocketManager getSocketManager(I2PTunnel tunnel, String pkf) {
    // shadows instance _log
    Log _log = tunnel.getContext().logManager().getLog(I2PTunnelClientBase.class);
    if (socketManager != null && !socketManager.isDestroyed()) {
        I2PSession s = socketManager.getSession();
        if (s.isClosed() && _socketManagerState != SocketManagerState.INIT) {
            if (_log.shouldLog(Log.INFO))
                _log.info(tunnel.getClientOptions().getProperty("inbound.nickname") + ": Building a new socket manager since the old one closed [s=" + s + "]");
            tunnel.removeSession(s);
            // make sure the old one is closed
            socketManager.destroySocketManager();
            _socketManagerState = SocketManagerState.INIT;
            // We could be here a LONG time, holding the lock
            socketManager = buildSocketManager(tunnel, pkf);
            // FIXME may not be the right place for this
            I2PSession sub = addSubsession(tunnel);
            if (sub != null && _log.shouldLog(Log.WARN))
                _log.warn("Added subsession " + sub);
        } else {
            if (_log.shouldLog(Log.INFO))
                _log.info(tunnel.getClientOptions().getProperty("inbound.nickname") + ": Not building a new socket manager since the old one is open [s=" + s + "]");
            // If some other tunnel created the session, we need to add it
            // as our session too.
            // It's a Set in I2PTunnel
            tunnel.addSession(s);
        }
    } else {
        if (_log.shouldLog(Log.INFO))
            _log.info(tunnel.getClientOptions().getProperty("inbound.nickname") + ": Building a new socket manager since there is no other one");
        socketManager = buildSocketManager(tunnel, pkf);
        I2PSession sub = addSubsession(tunnel);
        if (sub != null && _log.shouldLog(Log.WARN))
            _log.warn("Added subsession " + sub);
    }
    return socketManager;
}
Also used : Log(net.i2p.util.Log) I2PSession(net.i2p.client.I2PSession)

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