Search in sources :

Example 56 with DataFormatException

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

the class IndexPacket method toByteArray.

@Override
public byte[] toByteArray() {
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    DataOutputStream dataStream = new DataOutputStream(byteStream);
    try {
        writeHeader(dataStream);
        destinationHash.writeBytes(dataStream);
        dataStream.writeInt(entries.size());
        for (IndexPacketEntry entry : entries) {
            dataStream.write(entry.emailPacketKey.toByteArray());
            dataStream.write(entry.delVerificationHash.toByteArray());
            // store as seconds
            dataStream.writeInt((int) (entry.storeTime / 1000L));
        }
    } catch (IOException e) {
        log.error("Can't write to ByteArrayOutputStream/DataOutputStream.", e);
    } catch (DataFormatException e) {
        log.error("Can't write destination hash to output stream: " + destinationHash, e);
    }
    return byteStream.toByteArray();
}
Also used : DataFormatException(net.i2p.data.DataFormatException) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 57 with DataFormatException

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

the class I2PPacketDispatcher method messageAvailable.

@Override
public void messageAvailable(I2PSession session, int msgId, long size) {
    byte[] msg = new byte[0];
    try {
        msg = session.receiveMessage(msgId);
    } catch (I2PSessionException e) {
        log.error("Can't get new message from I2PSession.", e);
    }
    if (msg == null) {
        log.error("I2PSession returned a null message: msgId=" + msgId + ", size=" + size + ", " + session);
        return;
    }
    I2PDatagramDissector datagramDissector = new I2PDatagramDissector();
    try {
        datagramDissector.loadI2PDatagram(msg);
        // TODO keep this line or remove it?
        datagramDissector.verifySignature();
        byte[] payload = datagramDissector.extractPayload();
        Destination sender = datagramDissector.getSender();
        dispatchPacket(payload, sender);
    } catch (DataFormatException e) {
        log.error("Invalid datagram received.", e);
    } catch (I2PInvalidDatagramException e) {
        log.error("Datagram failed verification.", e);
    } catch (Exception e) {
        log.error("Error processing datagram.", e);
    }
}
Also used : Destination(net.i2p.data.Destination) I2PDatagramDissector(net.i2p.client.datagram.I2PDatagramDissector) DataFormatException(net.i2p.data.DataFormatException) I2PSessionException(net.i2p.client.I2PSessionException) I2PInvalidDatagramException(net.i2p.client.datagram.I2PInvalidDatagramException) DataFormatException(net.i2p.data.DataFormatException) I2PSessionException(net.i2p.client.I2PSessionException) MalformedPacketException(i2p.bote.packet.MalformedPacketException) I2PInvalidDatagramException(net.i2p.client.datagram.I2PInvalidDatagramException)

Example 58 with DataFormatException

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

the class RelayPacketHandler method packetReceived.

@Override
public void packetReceived(CommunicationPacket packet, Destination sender, long receiveTime) {
    if (packet instanceof RelayRequest && dht.isReady()) {
        RelayRequest relayRequest = (RelayRequest) packet;
        CommunicationPacket payload;
        try {
            payload = relayRequest.getStoredPacket(i2pSession);
        } catch (DataFormatException e) {
            log.error("Invalid RelayRequest received from peer " + Util.toBase32(sender), e);
            return;
        } catch (MalformedPacketException e) {
            log.error("Invalid RelayRequest received from peer " + Util.toBase32(sender), e);
            return;
        }
        log.debug("Received a relay request, payload: " + payload);
        if (payload instanceof RelayRequest) {
            log.debug("Relay packet is of type " + payload.getClass().getSimpleName() + ", storing it in the relay packet folder.");
            relayPacketFolder.add((RelayRequest) payload);
            confirm(sender, relayRequest);
        } else if (payload instanceof StoreRequest) {
            log.debug("Relay packet is of type " + payload.getClass().getSimpleName() + ", storing it in the DHT.");
            final DhtStorablePacket dhtPacket = ((StoreRequest) payload).getPacketToStore();
            // do dht.store() in a separate thread so we don't block the notifier thread
            dhtTaskExecutor.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        dht.store(dhtPacket);
                        log.debug("Finished storing DHT packet: " + dhtPacket);
                    } catch (InterruptedException e) {
                        log.debug("Interrupted while storing packet in the DHT.");
                    } catch (DhtException e) {
                        log.error("Error storing packet in the DHT: " + dhtPacket, e);
                    }
                }
            });
            confirm(sender, relayRequest);
        } else
            log.error("Don't know how to handle relay packet of type " + payload.getClass());
    }
}
Also used : DhtStorablePacket(i2p.bote.packet.dht.DhtStorablePacket) DataFormatException(net.i2p.data.DataFormatException) StoreRequest(i2p.bote.packet.dht.StoreRequest) CommunicationPacket(i2p.bote.packet.CommunicationPacket) MalformedPacketException(i2p.bote.packet.MalformedPacketException) RelayRequest(i2p.bote.packet.relay.RelayRequest)

Example 59 with DataFormatException

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

the class SeedlessAnnounce method doSeedlessAnnounce.

private synchronized void doSeedlessAnnounce() {
    List<String> seedlessServers = seedlessScrapeServers.getSeedlessServers();
    if (seedlessServers.isEmpty()) {
        // try again in a minute.
        log.error("SeedlessServers.isEmpty, will retry shortly.");
        lastSeedlessAnnounce = System.currentTimeMillis() - (interval - TimeUnit.MINUTES.toMillis(1));
        return;
    }
    // Announce to 10 servers.
    // We do this over the i2pSocket.
    int successful = Math.min(10, seedlessServers.size());
    log.debug("Try to announce to " + successful + " Seedless Servers");
    Collections.shuffle(seedlessServers, new Random());
    Iterator<String> it = seedlessServers.iterator();
    String line;
    I2PSocket I2P;
    InputStream Iin;
    OutputStream Iout;
    BufferedReader data;
    Boolean didsomething = false;
    BufferedWriter output;
    while (successful > 0 && it.hasNext()) {
        lastSeedlessAnnounce = System.currentTimeMillis();
        String b32 = it.next();
        Destination dest = null;
        I2P = null;
        try {
            lastSeedlessAnnounce = System.currentTimeMillis();
            // deprecated dest = I2PTunnel.destFromName(b32);
            dest = I2PAppContext.getGlobalContext().namingService().lookup(b32);
            lastSeedlessAnnounce = System.currentTimeMillis();
            if (dest == null) {
                log.debug("Could not find the destination: <" + b32 + ">");
                continue;
            }
            line = dest.toBase64();
            dest = new Destination();
            dest.fromBase64(line);
            I2P = socketManager.connect(dest);
            // I2P.setReadTimeout(0); // temp bugfix, this *SHOULD* be the default
            // make readers/writers
            Iin = I2P.getInputStream();
            Iout = I2P.getOutputStream();
            output = new BufferedWriter(new OutputStreamWriter(Iout));
            output.write(announceString);
            output.flush();
            data = new BufferedReader(new InputStreamReader(Iin));
            // Check for success.
            line = data.readLine();
            if (line != null) {
                if (line.contains(" 200 ")) {
                    log.debug("Announced to " + b32);
                    successful--;
                    didsomething = true;
                } else {
                    log.debug("Announce to " + b32 + " Failed with Error " + line);
                    log.debug("We sent " + announceString);
                }
            }
            while ((line = data.readLine()) != null) {
            }
        } catch (DataFormatException ex) {
            log.debug("Not base64!", ex);
        } catch (ConnectException ex) {
            log.debug("ConnectException", ex);
        } catch (NoRouteToHostException ex) {
            log.debug("NoRouteToHostException", ex);
        } catch (InterruptedIOException ex) {
            log.debug("InterruptedIOException", ex);
        } catch (IOException ex) {
            log.debug("IOException", ex);
            ex.printStackTrace();
        } catch (I2PException ex) {
            log.debug("I2PException", ex);
        }
        if (I2P != null) {
            try {
                I2P.close();
            } catch (IOException ex) {
            // don't care.
            }
        }
    }
    if (!didsomething) {
        // try again in 1 minute.
        lastSeedlessAnnounce = System.currentTimeMillis() - (interval - TimeUnit.MINUTES.toMillis(1));
        return;
    }
    lastSeedlessAnnounce = System.currentTimeMillis();
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) InterruptedIOException(java.io.InterruptedIOException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) I2PSocket(net.i2p.client.streaming.I2PSocket) OutputStream(java.io.OutputStream) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) NoRouteToHostException(java.net.NoRouteToHostException) BufferedWriter(java.io.BufferedWriter) DataFormatException(net.i2p.data.DataFormatException) Random(java.util.Random) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) ConnectException(java.net.ConnectException)

Example 60 with DataFormatException

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

the class ElGamal2048_DSA1024 method createPublicKeyPair.

@Override
public PublicKeyPair createPublicKeyPair(String base64) throws GeneralSecurityException {
    Destination i2pDestination;
    // add a null certificate
    base64 += "AAAA";
    try {
        i2pDestination = new Destination(base64);
    } catch (DataFormatException e) {
        throw new KeyException("Can't create I2P destination from Base64: <" + base64 + ">", e);
    }
    PublicKeyPair keyPair = new PublicKeyPair();
    keyPair.encryptionKey = new ElGamalPublicKey(i2pDestination.getPublicKey());
    keyPair.signingKey = new DSAPublicKey(i2pDestination.getSigningPublicKey());
    return keyPair;
}
Also used : Destination(net.i2p.data.Destination) DataFormatException(net.i2p.data.DataFormatException) KeyException(java.security.KeyException)

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