use of i2p.bote.packet.dht.DeletionInfoPacket in project i2p.i2p-bote by i2p.
the class EmailPacketFolder method storeAndCreateDeleteRequest.
@Override
public DeleteRequest storeAndCreateDeleteRequest(DhtStorablePacket packetToStore) {
if (!(packetToStore instanceof EncryptedEmailPacket))
throw new IllegalArgumentException("Invalid packet type: " + packetToStore.getClass().getSimpleName() + "; this folder only stores packets of type " + EncryptedEmailPacket.class.getSimpleName() + ".");
DeleteRequest delRequest = null;
// read the deletion info file for the email packet's DHT key
Hash dhtKey = packetToStore.getDhtKey();
String delFileName = getDeletionFileName(dhtKey);
DeletionInfoPacket delInfo = createDelInfoPacket(delFileName);
if (delInfo != null) {
DeletionRecord delRecord = delInfo.getEntry(dhtKey);
if (delRecord != null)
delRequest = new EmailPacketDeleteRequest(delRecord.dhtKey, delRecord.delAuthorization);
} else
// if the DHT key has not been recorded as deleted, store the email packet
store(packetToStore);
return delRequest;
}
use of i2p.bote.packet.dht.DeletionInfoPacket in project i2p.i2p-bote by i2p.
the class KademliaDHT method packetReceived.
// PacketListener implementation
@Override
public void packetReceived(CommunicationPacket packet, Destination sender, long receiveTime) {
if (packet instanceof FindClosePeersPacket)
sendPeerList((FindClosePeersPacket) packet, sender);
else if (packet instanceof StoreRequest) {
DhtStorablePacket packetToStore = ((StoreRequest) packet).getPacketToStore();
storeLocally(packetToStore, sender);
} else if (packet instanceof RetrieveRequest) {
RetrieveRequest retrieveRequest = (RetrieveRequest) packet;
DhtStorageHandler storageHandler = storageHandlers.get(retrieveRequest.getDataType());
if (storageHandler != null) {
DhtStorablePacket storedPacket = storageHandler.retrieve(retrieveRequest.getKey());
// if requested packet found, send it to the requester
Collection<ResponsePacket> response = ResponsePacket.create(storedPacket, StatusCode.OK, retrieveRequest.getPacketId());
if (storedPacket != null)
log.debug("Packet found for retrieve request: [" + retrieveRequest + "], replying to sender: [" + Util.toBase32(sender) + "]");
else
log.debug("No matching packet found for retrieve request: [" + retrieveRequest + "]");
sendQueue.send(response, sender);
} else
log.warn("No storage handler found for type " + packet.getClass().getSimpleName() + ".");
} else if (packet instanceof DeleteRequest) {
DeleteRequest delRequest = (DeleteRequest) packet;
DhtStorageHandler storageHandler = storageHandlers.get(delRequest.getDataType());
if (storageHandler instanceof DeletionAwareDhtFolder<?>)
((DeletionAwareDhtFolder<?>) storageHandler).process(delRequest);
} else if (packet instanceof DeletionQuery) {
DhtStorageHandler storageHandler = storageHandlers.get(EncryptedEmailPacket.class);
if (storageHandler instanceof DeletionAwareDhtFolder) {
Hash dhtKey = ((DeletionQuery) packet).getDhtKey();
UniqueId delAuthorization = ((DeletionAwareDhtFolder<?>) storageHandler).getDeleteAuthorization(dhtKey);
// If we know the Delete Authorization for the DHT key, send it to the peer
if (delAuthorization != null) {
DeletionInfoPacket delInfo = new DeletionInfoPacket();
delInfo.put(dhtKey, delAuthorization);
Collection<ResponsePacket> response = ResponsePacket.create(delInfo, StatusCode.OK, packet.getPacketId());
sendQueue.send(response, sender);
}
}
}
// bucketManager is not registered as a PacketListener, so notify it here
bucketManager.packetReceived(packet, sender, receiveTime);
}
use of i2p.bote.packet.dht.DeletionInfoPacket in project i2p.i2p-bote by i2p.
the class EmailPacketFolderTest method testProcessDeleteRequest.
/**
* Tests creation of delete records when processing {@link EmailPacketDeleteRequest}s,
* and matching them against email packets.
*/
@Test
public void testProcessDeleteRequest() throws GeneralSecurityException, InvalidCipherTextException, MalformedPacketException, PasswordException {
// create two packets whose base64 DHT keys start with the same two characters
byte[] emailPkt1Data = Base64.decode("RQTAJHelC-wnxq-OUAnfgqgN84zTCimrfUKw7AgDPHblcwAAAADUk24QY3EPEzk0UmB5PYQxwnEVIWGZkWJ-5ERh30l95QMA0gIs-itszDaiCa4ucekxDL6pZCVMTnI~S0nBx2FQ5Fl4-T50bNojkgJiQZNFB-vwQ4ChB~hxYIomMtbN6tiNDVwYRKjeZHbL3MAffKcwxwF3iPMl8aLLBaU6LxQ~3r7tgTiccHE39Ozm88~Hrf7H-5hWXzTXwm7j1pNJA7hc-w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iaCpSrmE98L8PZhEIfXvN3PUADSuzsbHmuHQ=");
EncryptedEmailPacket emailPacket1 = new EncryptedEmailPacket(emailPkt1Data);
EncryptedEmailPacket emailPacket2 = new EncryptedEmailPacket(emailPkt2Data);
// matches emailPkt1Data
UniqueId delAuthKey1 = new UniqueId("nJlsaGZb1k6OFze1fUsHEF0osP5Wv9Rgta8EwHl8Te8=");
// matches emailPkt2Data
UniqueId delAuthKey2 = new UniqueId("ImXwDy7HZq7pyjGwxpYmlwaKAWoQlUl2fdrBK~mTt0g=");
String prefix1 = emailPacket1.getDhtKey().toBase64().substring(0, 2);
String prefix2 = emailPacket2.getDhtKey().toBase64().substring(0, 2);
assertEquals("Error setting up test packets: prefixes of DHT keys differ", prefix1, prefix2);
assertFalse("Error setting up test packets: DHT keys should be different", emailPacket1.getDhtKey().equals(emailPacket2.getDhtKey()));
// add two packets and delete them via delete requests
packetFolder.store(emailPacket1);
assertEquals("Folder should have exactly one element!", 1, packetFolder.getElements().size());
packetFolder.store(emailPacket2);
assertEquals("Folder should have two elements!", 2, packetFolder.getElements().size());
EmailPacketDeleteRequest delRequest1 = new EmailPacketDeleteRequest(emailPacket1.getDhtKey(), delAuthKey1);
packetFolder.process(delRequest1);
assertEquals("Folder should have exactly one element!", 1, packetFolder.getElements().size());
EmailPacketDeleteRequest delRequest2 = new EmailPacketDeleteRequest(emailPacket2.getDhtKey(), delAuthKey2);
packetFolder.process(delRequest2);
assertEquals("Folder should be empty!", 0, packetFolder.getElements().size());
// verify that there is one deletion file containing two entries
File[] files = packetFolder.getStorageDirectory().listFiles();
assertEquals(1, files.length);
assertTrue(files[0].getName().startsWith("DEL_"));
DataPacket dataPacket = DataPacket.createPacket(files[0]);
assertTrue(dataPacket instanceof DeletionInfoPacket);
DeletionInfoPacket delInfoPacket = (DeletionInfoPacket) dataPacket;
Iterator<DeletionRecord> delPacketIterator = delInfoPacket.iterator();
assertTrue("DeletionInfoPacket has no elements!", delPacketIterator.hasNext());
delPacketIterator.next();
assertTrue("DeletionInfoPacket has less than one element!", delPacketIterator.hasNext());
delPacketIterator.next();
assertFalse("DeletionInfoPacket has more than two elements!", delPacketIterator.hasNext());
// verify that the two deletion records match the DHT keys and auth keys of the deleted packets
DeleteRequest newDelRequest1 = packetFolder.storeAndCreateDeleteRequest(emailPacket1);
assertTrue(newDelRequest1 instanceof EmailPacketDeleteRequest);
EmailPacketDeleteRequest newEmailPacketDelRequest1 = (EmailPacketDeleteRequest) newDelRequest1;
assertEquals(newEmailPacketDelRequest1.getDhtKey(), emailPacket1.getDhtKey());
assertEquals(newEmailPacketDelRequest1.getAuthorization(), delAuthKey1);
DeleteRequest newDelRequest2 = packetFolder.storeAndCreateDeleteRequest(emailPacket2);
assertTrue(newDelRequest2 instanceof EmailPacketDeleteRequest);
EmailPacketDeleteRequest newEmailPacketDelRequest2 = (EmailPacketDeleteRequest) newDelRequest2;
assertEquals(newEmailPacketDelRequest2.getDhtKey(), emailPacket2.getDhtKey());
assertEquals(newEmailPacketDelRequest2.getAuthorization(), delAuthKey2);
}
Aggregations