Search in sources :

Example 51 with DataFormatException

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

the class SSUDemo method loadPeers.

private void loadPeers() {
    File infoDir = getInfoDir();
    if (!infoDir.exists())
        infoDir.mkdirs();
    while (true) {
        File[] peerFiles = infoDir.listFiles();
        if ((peerFiles != null) && (peerFiles.length > 0)) {
            for (int i = 0; i < peerFiles.length; i++) {
                if (peerFiles[i].isFile() && !peerFiles[i].isHidden()) {
                    if (!_us.routerHash().toBase64().equals(peerFiles[i].getName())) {
                        System.out.println("Reading info: " + peerFiles[i].getAbsolutePath());
                        try {
                            FileInputStream in = new FileInputStream(peerFiles[i]);
                            RouterInfo ri = new RouterInfo();
                            ri.readBytes(in);
                            peerRead(ri);
                        } catch (IOException ioe) {
                            System.err.println("Error reading " + peerFiles[i].getAbsolutePath());
                            ioe.printStackTrace();
                        } catch (DataFormatException dfe) {
                            System.err.println("Corrupt " + peerFiles[i].getAbsolutePath());
                            dfe.printStackTrace();
                        }
                    }
                }
            }
        }
        try {
            Thread.sleep(30 * 1000);
        } catch (InterruptedException ie) {
        }
    }
}
Also used : DataFormatException(net.i2p.data.DataFormatException) RouterInfo(net.i2p.data.router.RouterInfo) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 52 with DataFormatException

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

the class PublishLocalRouterInfoJob method runJob.

public void runJob() {
    long last = getContext().netDb().getLastRouterInfoPublishTime();
    long now = getContext().clock().now();
    if (last + MIN_PUBLISH_DELAY > now) {
        long delay = getDelay();
        requeue(last + delay - now);
        return;
    }
    RouterInfo oldRI = getContext().router().getRouterInfo();
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("Old routerInfo contains " + oldRI.getAddresses().size() + " addresses and " + oldRI.getOptionsMap().size() + " options");
    try {
        List<RouterAddress> oldAddrs = new ArrayList<RouterAddress>(oldRI.getAddresses());
        List<RouterAddress> newAddrs = getContext().commSystem().createAddresses();
        int count = _runCount.incrementAndGet();
        RouterInfo ri = new RouterInfo(oldRI);
        if (_notFirstTime && (count % 4) != 0 && oldAddrs.size() == newAddrs.size()) {
            // 3 times out of 4, we don't republish if everything is the same...
            // If something changed, including the cost, then publish,
            // otherwise don't.
            String newcaps = getContext().router().getCapabilities();
            boolean different = !oldRI.getCapabilities().equals(newcaps);
            if (!different) {
                Comparator<RouterAddress> comp = new AddrComparator();
                Collections.sort(oldAddrs, comp);
                Collections.sort(newAddrs, comp);
                for (int i = 0; i < oldAddrs.size(); i++) {
                    // deepEquals() includes cost
                    if (!oldAddrs.get(i).deepEquals(newAddrs.get(i))) {
                        different = true;
                        break;
                    }
                }
                if (!different) {
                    if (_log.shouldLog(Log.INFO))
                        _log.info("Not republishing early because costs and caps and addresses are the same");
                    requeue(getDelay());
                    return;
                }
            }
            if (_log.shouldLog(Log.INFO))
                _log.info("Republishing early because addresses or costs or caps have changed -" + " oldCaps: " + oldRI.getCapabilities() + " newCaps: " + newcaps + " old:\n" + oldAddrs + "\nnew:\n" + newAddrs);
        }
        ri.setPublished(getContext().clock().now());
        Properties stats = getContext().statPublisher().publishStatistics();
        ri.setOptions(stats);
        ri.setAddresses(newAddrs);
        SigningPrivateKey key = getContext().keyManager().getSigningPrivateKey();
        if (key == null) {
            _log.log(Log.CRIT, "Internal error - signing private key not known?  rescheduling publish for 30s");
            requeue(30 * 1000);
            return;
        }
        ri.sign(key);
        getContext().router().setRouterInfo(ri);
        if (_log.shouldLog(Log.INFO))
            _log.info("Newly updated routerInfo is published with " + stats.size() + "/" + ri.getOptionsMap().size() + " options on " + new Date(ri.getPublished()));
        try {
            // This won't really publish until the netdb is initialized.
            getContext().netDb().publish(ri);
        } catch (IllegalArgumentException iae) {
            _log.log(Log.CRIT, "Error publishing our identity - corrupt? Restart required", iae);
            getContext().router().rebuildNewIdentity();
        }
    } catch (DataFormatException dfe) {
        _log.error("Error signing the updated local router info!", dfe);
    }
    if (_notFirstTime) {
        requeue(getDelay());
    } else {
        requeue(FIRST_TIME_DELAY);
        _notFirstTime = true;
    }
}
Also used : RouterInfo(net.i2p.data.router.RouterInfo) ArrayList(java.util.ArrayList) RouterAddress(net.i2p.data.router.RouterAddress) Properties(java.util.Properties) Date(java.util.Date) SigningPrivateKey(net.i2p.data.SigningPrivateKey) DataFormatException(net.i2p.data.DataFormatException)

Example 53 with DataFormatException

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

the class KademliaNetworkDatabaseFacade method processStoreFailure.

/**
 *  If the validate fails, call this
 *  to determine if it was because of unsupported crypto.
 *
 *  If so, this will banlist-forever the router hash or permanently negative cache the dest hash,
 *  and then throw the exception. Otherwise it does nothing.
 *
 *  @throws UnsupportedCryptoException if that's why it failed.
 *  @since 0.9.16
 */
private void processStoreFailure(Hash h, DatabaseEntry entry) throws UnsupportedCryptoException {
    if (entry.getHash().equals(h)) {
        if (entry.getType() == DatabaseEntry.KEY_TYPE_LEASESET) {
            LeaseSet ls = (LeaseSet) entry;
            Destination d = ls.getDestination();
            Certificate c = d.getCertificate();
            if (c.getCertificateType() == Certificate.CERTIFICATE_TYPE_KEY) {
                try {
                    KeyCertificate kc = c.toKeyCertificate();
                    SigType type = kc.getSigType();
                    if (type == null || !type.isAvailable() || type.getBaseAlgorithm() == SigAlgo.RSA) {
                        failPermanently(d);
                        String stype = (type != null) ? type.toString() : Integer.toString(kc.getSigTypeCode());
                        if (_log.shouldLog(Log.WARN))
                            _log.warn("Unsupported sig type " + stype + " for destination " + h);
                        throw new UnsupportedCryptoException("Sig type " + stype);
                    }
                } catch (DataFormatException dfe) {
                }
            }
        } else if (entry.getType() == DatabaseEntry.KEY_TYPE_ROUTERINFO) {
            RouterInfo ri = (RouterInfo) entry;
            RouterIdentity id = ri.getIdentity();
            Certificate c = id.getCertificate();
            if (c.getCertificateType() == Certificate.CERTIFICATE_TYPE_KEY) {
                try {
                    KeyCertificate kc = c.toKeyCertificate();
                    SigType type = kc.getSigType();
                    if (type == null || !type.isAvailable()) {
                        String stype = (type != null) ? type.toString() : Integer.toString(kc.getSigTypeCode());
                        _context.banlist().banlistRouterForever(h, "Unsupported signature type " + stype);
                        if (_log.shouldLog(Log.WARN))
                            _log.warn("Unsupported sig type " + stype + " for router " + h);
                        throw new UnsupportedCryptoException("Sig type " + stype);
                    }
                } catch (DataFormatException dfe) {
                }
            }
        }
    }
    if (_log.shouldLog(Log.WARN))
        _log.warn("Verify fail, cause unknown: " + entry);
}
Also used : LeaseSet(net.i2p.data.LeaseSet) Destination(net.i2p.data.Destination) KeyCertificate(net.i2p.data.KeyCertificate) DataFormatException(net.i2p.data.DataFormatException) RouterInfo(net.i2p.data.router.RouterInfo) RouterIdentity(net.i2p.data.router.RouterIdentity) SigType(net.i2p.crypto.SigType) Certificate(net.i2p.data.Certificate) KeyCertificate(net.i2p.data.KeyCertificate)

Example 54 with DataFormatException

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

the class Router method locked_rebuildRouterInfo.

/**
 * Rebuild and republish our routerInfo since something significant
 * has changed.
 */
private void locked_rebuildRouterInfo(boolean blockingRebuild) {
    RouterInfo ri;
    if (_routerInfo != null)
        ri = new RouterInfo(_routerInfo);
    else
        ri = new RouterInfo();
    try {
        ri.setPublished(_context.clock().now());
        Properties stats = _context.statPublisher().publishStatistics();
        ri.setOptions(stats);
        // deadlock thru createAddresses() thru SSU REA... move outside lock?
        ri.setAddresses(_context.commSystem().createAddresses());
        SigningPrivateKey key = _context.keyManager().getSigningPrivateKey();
        if (key == null) {
            _log.log(Log.CRIT, "Internal error - signing private key not known? Impossible?");
            return;
        }
        ri.sign(key);
        setRouterInfo(ri);
        if (!ri.isValid())
            throw new DataFormatException("Our RouterInfo has a bad signature");
        Republish r = new Republish(_context);
        if (blockingRebuild)
            r.timeReached();
        else
            _context.simpleTimer2().addEvent(r, 0);
    } catch (DataFormatException dfe) {
        _log.log(Log.CRIT, "Internal error - unable to sign our own address?!", dfe);
    }
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) DataFormatException(net.i2p.data.DataFormatException) RouterInfo(net.i2p.data.router.RouterInfo) OrderedProperties(net.i2p.util.OrderedProperties) Properties(java.util.Properties)

Example 55 with DataFormatException

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

the class PersistRouterInfoJob method runJob.

public void runJob() {
    Log _log = getContext().logManager().getLog(PersistRouterInfoJob.class);
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("Persisting updated router info");
    File infoFile = new File(getContext().getRouterDir(), CreateRouterInfoJob.INFO_FILENAME);
    RouterInfo info = getContext().router().getRouterInfo();
    FileOutputStream fos = null;
    synchronized (getContext().router().routerInfoFileLock) {
        try {
            fos = new SecureFileOutputStream(infoFile);
            info.writeBytes(fos);
        } catch (DataFormatException dfe) {
            _log.error("Error rebuilding the router information", dfe);
        } catch (IOException ioe) {
            _log.error("Error writing out the rebuilt router information", ioe);
        } finally {
            if (fos != null)
                try {
                    fos.close();
                } catch (IOException ioe) {
                }
        }
    }
}
Also used : DataFormatException(net.i2p.data.DataFormatException) Log(net.i2p.util.Log) RouterInfo(net.i2p.data.router.RouterInfo) FileOutputStream(java.io.FileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) File(java.io.File)

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