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();
}
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);
}
}
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());
}
}
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();
}
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;
}
Aggregations