Search in sources :

Example 76 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class DeliveryInstructions method writeBytes.

/**
 * @deprecated unused
 */
@Deprecated
public void writeBytes(OutputStream out) throws DataFormatException, IOException {
    if ((_deliveryMode < 0) || (_deliveryMode > FLAG_MODE_TUNNEL))
        throw new DataFormatException("Invalid data: mode = " + _deliveryMode);
    long flags = getFlags();
    // if (_log.shouldLog(Log.DEBUG))
    // _log.debug("Write flags: " + flags + " mode: " + getDeliveryMode()
    // + " =?= " + flagMode(flags));
    byte[] additionalInfo = getAdditionalInfo();
    out.write((byte) flags);
    if (additionalInfo != null) {
        out.write(additionalInfo);
        out.flush();
    }
}
Also used : DataFormatException(net.i2p.data.DataFormatException)

Example 77 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class GarlicClove method readBytes.

/**
 */
public int readBytes(byte[] source, int offset) throws DataFormatException {
    int cur = offset;
    _instructions = DeliveryInstructions.create(source, offset);
    cur += _instructions.getSize();
    // _log.debug("Read instructions: " + _instructions);
    try {
        I2NPMessageHandler handler = new I2NPMessageHandler(_context);
        cur += handler.readMessage(source, cur);
        _msg = handler.lastRead();
    } catch (I2NPMessageException ime) {
        throw new DataFormatException("Unable to read the message from a garlic clove", ime);
    }
    _cloveId = DataHelper.fromLong(source, cur, 4);
    cur += 4;
    _expiration = DataHelper.fromDate(source, cur);
    cur += DataHelper.DATE_LENGTH;
    // if (_log.shouldLog(Log.DEBUG))
    // _log.debug("CloveID read: " + _cloveId + " expiration read: " + _expiration);
    // _certificate = new Certificate();
    // cur += _certificate.readBytes(source, cur);
    _certificate = Certificate.create(source, cur);
    cur += _certificate.size();
    // _log.debug("Read cert: " + _certificate);
    return cur - offset;
}
Also used : DataFormatException(net.i2p.data.DataFormatException)

Example 78 with DataFormatException

use of net.i2p.data.DataFormatException 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 79 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class I2PTunnel method runLookup.

/**
 * Perform a lookup of the name specified
 * Deprecated - only used by CLI
 *
 * Sets the event "lookupResult" = base64 of the destination, or an error message
 *
 * @param args {name}
 * @param l logger to receive events and output
 */
private void runLookup(String[] args, Logging l) {
    if (args.length != 1) {
        l.log("lookup <name>\n" + "   try to resolve the name into a destination key");
        notifyEvent("lookupResult", "invalidUsage");
    } else {
        try {
            boolean ssl = Boolean.parseBoolean(_clientOptions.getProperty("i2cp.SSL"));
            String user = _clientOptions.getProperty("i2cp.username");
            String pw = _clientOptions.getProperty("i2cp.password");
            Destination dest = destFromName(args[0], host, port, ssl, user, pw);
            if (dest == null) {
                l.log("Unknown host: " + args[0]);
                notifyEvent("lookupResult", "unkown host");
            } else {
                l.log(dest.toBase64());
                notifyEvent("lookupResult", dest.toBase64());
            }
        } catch (DataFormatException dfe) {
            l.log("Unknown or invalid host: " + args[0]);
            notifyEvent("lookupResult", "invalid host");
        }
    }
}
Also used : Destination(net.i2p.data.Destination) DataFormatException(net.i2p.data.DataFormatException)

Example 80 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class PersistDHT method loadDHT.

public static synchronized void loadDHT(KRPC krpc, File file) {
    Log log = I2PAppContext.getGlobalContext().logManager().getLog(PersistDHT.class);
    int count = 0;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));
        String line = null;
        while ((line = br.readLine()) != null) {
            if (line.startsWith("#"))
                continue;
            try {
                krpc.heardAbout(new NodeInfo(line));
                count++;
            // TODO limit number? this will flush the router's SDS caches
            } catch (IllegalArgumentException iae) {
                if (log.shouldLog(Log.WARN))
                    log.warn("Error reading DHT entry", iae);
            } catch (DataFormatException dfe) {
                if (log.shouldLog(Log.WARN))
                    log.warn("Error reading DHT entry", dfe);
            }
        }
    } catch (IOException ioe) {
        if (log.shouldLog(Log.WARN) && file.exists())
            log.warn("Error reading the DHT File", ioe);
    } finally {
        if (br != null)
            try {
                br.close();
            } catch (IOException ioe) {
            }
    }
    if (log.shouldLog(Log.INFO))
        log.info("Loaded " + count + " nodes from " + file);
}
Also used : DataFormatException(net.i2p.data.DataFormatException) InputStreamReader(java.io.InputStreamReader) Log(net.i2p.util.Log) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Aggregations

DataFormatException (net.i2p.data.DataFormatException)112 IOException (java.io.IOException)53 Destination (net.i2p.data.Destination)32 Properties (java.util.Properties)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 FileInputStream (java.io.FileInputStream)16 Hash (net.i2p.data.Hash)14 File (java.io.File)13 SigType (net.i2p.crypto.SigType)13 I2PSessionException (net.i2p.client.I2PSessionException)12 InputStream (java.io.InputStream)11 PrivateKey (net.i2p.data.PrivateKey)11 SigningPrivateKey (net.i2p.data.SigningPrivateKey)11 SigningPublicKey (net.i2p.data.SigningPublicKey)11 RouterInfo (net.i2p.data.router.RouterInfo)11 Signature (net.i2p.data.Signature)10 FileOutputStream (java.io.FileOutputStream)8 InterruptedIOException (java.io.InterruptedIOException)8 HashMap (java.util.HashMap)8 PublicKey (net.i2p.data.PublicKey)8