Search in sources :

Example 1 with DeletionInfoPacket

use of i2p.bote.packet.dht.DeletionInfoPacket in project i2p.i2p-bote by i2p.

the class KademliaDHT method findDeleteAuthorizationKey.

@Override
public UniqueId findDeleteAuthorizationKey(Hash dhtKey, Hash verificationHash) throws InterruptedException {
    final Collection<Destination> closeNodes = getClosestNodes(dhtKey);
    log.info("Querying " + closeNodes.size() + " peers with DeletionQueries for Kademlia key " + dhtKey);
    DhtStorageHandler storageHandler = storageHandlers.get(EncryptedEmailPacket.class);
    if (storageHandler instanceof DeletionAwareDhtFolder) {
        DeletionAwareDhtFolder<?> folder = (DeletionAwareDhtFolder<?>) storageHandler;
        UniqueId delAuthorization = folder.getDeleteAuthorization(dhtKey);
        if (delAuthorization != null)
            return delAuthorization;
    } else
        log.error("StorageHandler for EncryptedEmailPackets is not a DeletionAwareDhtFolder!");
    // Send the DeletionQueries
    PacketBatch batch = new PacketBatch();
    for (Destination node : closeNodes) if (// local has already been taken care of
    !localDestination.equals(node))
        batch.putPacket(new DeletionQuery(dhtKey), node);
    sendQueue.send(batch);
    batch.awaitSendCompletion();
    // wait for replies
    batch.awaitFirstReply(RESPONSE_TIMEOUT, TimeUnit.SECONDS);
    log.info(batch.getResponses().size() + " response packets received for deletion query for hash " + dhtKey);
    sendQueue.remove(batch);
    Map<Destination, DataPacket> responses = batch.getResponses();
    for (DataPacket response : responses.values()) if (response instanceof DeletionInfoPacket) {
        DeletionInfoPacket delInfo = (DeletionInfoPacket) response;
        DeletionRecord delRecord = delInfo.getEntry(dhtKey);
        if (delRecord != null) {
            boolean valid = Util.isDeleteAuthorizationValid(verificationHash, delRecord.delAuthorization);
            if (valid)
                return delRecord.delAuthorization;
        }
    }
    return null;
}
Also used : Destination(net.i2p.data.Destination) UniqueId(i2p.bote.UniqueId) DeletionQuery(i2p.bote.packet.dht.DeletionQuery) DeletionInfoPacket(i2p.bote.packet.dht.DeletionInfoPacket) DeletionRecord(i2p.bote.packet.dht.DeletionRecord) DhtStorageHandler(i2p.bote.network.DhtStorageHandler) PacketBatch(i2p.bote.network.PacketBatch) DataPacket(i2p.bote.packet.DataPacket) DeletionAwareDhtFolder(i2p.bote.folder.DeletionAwareDhtFolder)

Example 2 with DeletionInfoPacket

use of i2p.bote.packet.dht.DeletionInfoPacket in project i2p.i2p-bote by i2p.

the class IndexPacketFolderTest method testProcessDeleteRequest.

@Test
public void testProcessDeleteRequest() throws GeneralSecurityException, MalformedPacketException, PasswordException {
    IndexPacketFolder folder = new IndexPacketFolder(folderDir);
    // create another packet with the same destination as emailPacket1
    EncryptedEmailPacket emailPacket3 = new EncryptedEmailPacket(unencryptedPacket2, destination1);
    IndexPacket indexPacket = new IndexPacket(destination1);
    indexPacket.put(emailPacket1);
    indexPacket.put(emailPacket3);
    folder.store(indexPacket);
    assertEquals("Folder should have exactly one element!", 1, folder.getElements().size());
    // add two entries and delete them via delete requests
    Hash dest1Hash = destination1.getHash();
    IndexPacketDeleteRequest delRequest = new IndexPacketDeleteRequest(dest1Hash);
    Hash dhtKey1 = emailPacket1.getDhtKey();
    Hash dhtKey3 = emailPacket3.getDhtKey();
    delRequest.put(dhtKey1, unencryptedPacket1.getDeleteAuthorization());
    delRequest.put(dhtKey3, unencryptedPacket2.getDeleteAuthorization());
    folder.process(delRequest);
    DhtStorablePacket storedPacket = folder.retrieve(dest1Hash);
    assertTrue(storedPacket instanceof IndexPacket);
    assertEquals("The index packet should have no entries!", 0, ((IndexPacket) storedPacket).getNumEntries());
    // verify that there is one deletion file containing two entries
    File[] files = folder.getStorageDirectory().listFiles(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith("DEL_");
        }
    });
    assertEquals(1, files.length);
    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 newDelRequest = folder.storeAndCreateDeleteRequest(indexPacket);
    assertTrue(newDelRequest instanceof IndexPacketDeleteRequest);
    IndexPacketDeleteRequest newIndexPacketDelRequest = (IndexPacketDeleteRequest) newDelRequest;
    assertEquals(newIndexPacketDelRequest.getDeleteAuthorization(dhtKey1), unencryptedPacket1.getDeleteAuthorization());
    assertEquals(newIndexPacketDelRequest.getDeleteAuthorization(dhtKey3), unencryptedPacket2.getDeleteAuthorization());
}
Also used : DhtStorablePacket(i2p.bote.packet.dht.DhtStorablePacket) IndexPacketDeleteRequest(i2p.bote.packet.dht.IndexPacketDeleteRequest) DeletionRecord(i2p.bote.packet.dht.DeletionRecord) EncryptedEmailPacket(i2p.bote.packet.dht.EncryptedEmailPacket) Hash(net.i2p.data.Hash) DataPacket(i2p.bote.packet.DataPacket) FilenameFilter(java.io.FilenameFilter) DeletionInfoPacket(i2p.bote.packet.dht.DeletionInfoPacket) IndexPacket(i2p.bote.packet.dht.IndexPacket) File(java.io.File) DeleteRequest(i2p.bote.packet.dht.DeleteRequest) IndexPacketDeleteRequest(i2p.bote.packet.dht.IndexPacketDeleteRequest) Test(org.junit.Test)

Example 3 with DeletionInfoPacket

use of i2p.bote.packet.dht.DeletionInfoPacket in project i2p.i2p-bote by i2p.

the class IndexPacketFolder method storeAndCreateDeleteRequest.

@Override
public DeleteRequest storeAndCreateDeleteRequest(DhtStorablePacket packetToStore) {
    if (!(packetToStore instanceof IndexPacket))
        throw new IllegalArgumentException("Invalid packet type: " + packetToStore.getClass().getSimpleName() + "; this folder only stores packets of type " + IndexPacket.class.getSimpleName() + ".");
    IndexPacket indexPacketToStore = (IndexPacket) packetToStore;
    // set the time stamps before merging so existing ones don't change
    setTimeStamps(indexPacketToStore);
    // use super.retrieve() because we don't want to delete time stamps here
    DhtStorablePacket existingPacket = super.retrieve(packetToStore.getDhtKey());
    // make a Delete Request that contains any known-to-be-deleted DHT keys from the store request
    String delFileName = getDeletionFileName(packetToStore.getDhtKey());
    DeletionInfoPacket delInfo = createDelInfoPacket(delFileName);
    IndexPacketDeleteRequest delRequest = null;
    if (delInfo != null) {
        delRequest = null;
        for (IndexPacketEntry entry : indexPacketToStore) {
            DeletionRecord delRecord = delInfo.getEntry(entry.emailPacketKey);
            if (delRecord != null) {
                // leave delRequest null until a DeletionRecord is found
                if (delRequest == null)
                    delRequest = new IndexPacketDeleteRequest(indexPacketToStore.getDhtKey());
                delRequest.put(delRecord.dhtKey, delRecord.delAuthorization);
            }
        }
    }
    // remove deleted entries from indexPacketToStore so they are not re-stored
    if (delInfo != null && !delInfo.isEmpty())
        for (DeletionRecord delRecord : delInfo) indexPacketToStore.remove(delRecord.dhtKey);
    // The resulting packet may be too big for a datagram but we don't split it until a peer asks for it.
    if (existingPacket instanceof IndexPacket)
        indexPacketToStore = new IndexPacket(indexPacketToStore, (IndexPacket) existingPacket);
    else if (existingPacket != null)
        log.error("Packet of type " + existingPacket.getClass().getSimpleName() + " found in IndexPacketFolder.");
    // don't merge, but overwrite
    super.store(indexPacketToStore);
    return delRequest;
}
Also used : DhtStorablePacket(i2p.bote.packet.dht.DhtStorablePacket) DeletionInfoPacket(i2p.bote.packet.dht.DeletionInfoPacket) IndexPacketDeleteRequest(i2p.bote.packet.dht.IndexPacketDeleteRequest) DeletionRecord(i2p.bote.packet.dht.DeletionRecord) IndexPacket(i2p.bote.packet.dht.IndexPacket) IndexPacketEntry(i2p.bote.packet.dht.IndexPacketEntry)

Example 4 with DeletionInfoPacket

use of i2p.bote.packet.dht.DeletionInfoPacket in project i2p.i2p-bote by i2p.

the class EmailPacketFolder method getDeleteAuthorization.

@Override
public UniqueId getDeleteAuthorization(Hash dhtKey) {
    String delFileName = getDeletionFileName(dhtKey);
    DeletionInfoPacket delInfo = createDelInfoPacket(delFileName);
    if (delInfo != null) {
        DeletionRecord delRecord = delInfo.getEntry(dhtKey);
        if (delRecord != null)
            return delRecord.delAuthorization;
    }
    return null;
}
Also used : DeletionInfoPacket(i2p.bote.packet.dht.DeletionInfoPacket) DeletionRecord(i2p.bote.packet.dht.DeletionRecord)

Example 5 with DeletionInfoPacket

use of i2p.bote.packet.dht.DeletionInfoPacket in project i2p.i2p-bote by i2p.

the class DeletionAwareDhtFolder method addToDeletedPackets.

/**
 * Adds a DHT key to the list of deleted packets and sets the delete authorization key.
 * If the key is already on the list, nothing happens.
 * @param delFileName
 * @param dhtKey
 * @param delAuthorization
 */
protected void addToDeletedPackets(String delFileName, Hash dhtKey, UniqueId delAuthorization) {
    DeletionInfoPacket packet = createDelInfoPacket(delFileName);
    if (packet == null) {
        log.debug("Creating a new Deletion Info Packet file: <" + delFileName + ">");
        packet = new DeletionInfoPacket();
    }
    packet.put(dhtKey, delAuthorization);
    add(packet, delFileName);
}
Also used : DeletionInfoPacket(i2p.bote.packet.dht.DeletionInfoPacket)

Aggregations

DeletionInfoPacket (i2p.bote.packet.dht.DeletionInfoPacket)8 DeletionRecord (i2p.bote.packet.dht.DeletionRecord)6 DeleteRequest (i2p.bote.packet.dht.DeleteRequest)4 UniqueId (i2p.bote.UniqueId)3 DataPacket (i2p.bote.packet.DataPacket)3 DhtStorablePacket (i2p.bote.packet.dht.DhtStorablePacket)3 EncryptedEmailPacket (i2p.bote.packet.dht.EncryptedEmailPacket)3 Hash (net.i2p.data.Hash)3 DeletionAwareDhtFolder (i2p.bote.folder.DeletionAwareDhtFolder)2 DhtStorageHandler (i2p.bote.network.DhtStorageHandler)2 DeletionQuery (i2p.bote.packet.dht.DeletionQuery)2 EmailPacketDeleteRequest (i2p.bote.packet.dht.EmailPacketDeleteRequest)2 IndexPacket (i2p.bote.packet.dht.IndexPacket)2 IndexPacketDeleteRequest (i2p.bote.packet.dht.IndexPacketDeleteRequest)2 File (java.io.File)2 Test (org.junit.Test)2 PacketBatch (i2p.bote.network.PacketBatch)1 ResponsePacket (i2p.bote.packet.ResponsePacket)1 FindClosePeersPacket (i2p.bote.packet.dht.FindClosePeersPacket)1 IndexPacketEntry (i2p.bote.packet.dht.IndexPacketEntry)1