Search in sources :

Example 11 with UniqueId

use of i2p.bote.UniqueId in project i2p.i2p-bote by i2p.

the class MessageIdCache method read.

private void read(File cacheFile) {
    idList = Collections.synchronizedList(new ArrayList<UniqueId>());
    if (!cacheFile.exists()) {
        log.debug("Message ID cache file doesn't exist: <" + cacheFile.getAbsolutePath() + ">");
        return;
    }
    log.debug("Reading message ID cache file: <" + cacheFile.getAbsolutePath() + ">");
    BufferedReader input = null;
    try {
        input = new BufferedReader(new FileReader(cacheFile));
        while (true) {
            String idString = input.readLine();
            if (// EOF
            idString == null)
                break;
            UniqueId id = new UniqueId(idString);
            idList.add(id);
        }
    } catch (IOException e) {
        log.error("Can't read message ID cache file.", e);
    } finally {
        if (input != null)
            try {
                input.close();
            } catch (IOException e) {
                log.error("Error closing BufferedReader.", e);
            }
    }
}
Also used : UniqueId(i2p.bote.UniqueId) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException)

Example 12 with UniqueId

use of i2p.bote.UniqueId in project i2p.i2p-bote by i2p.

the class EmailPacketFolder method process.

/**
 * Deletes email packets.
 * @param delRequest An instance of {@link EmailPacketDeleteRequest}
 */
@Override
public synchronized void process(DeleteRequest delRequest) {
    log.debug("Processing delete request: " + delRequest);
    if (!(delRequest instanceof EmailPacketDeleteRequest))
        log.error("Invalid type of delete request for EmailPacketFolder: " + delRequest.getClass());
    EmailPacketDeleteRequest emailPacketDelRequest = (EmailPacketDeleteRequest) delRequest;
    // see if the packet exists
    Hash dhtKey = emailPacketDelRequest.getDhtKey();
    DhtStorablePacket storedPacket = retrieve(dhtKey);
    if (storedPacket instanceof EncryptedEmailPacket) {
        // verify
        Hash verificationHash = ((EncryptedEmailPacket) storedPacket).getDeleteVerificationHash();
        UniqueId delAuthorization = emailPacketDelRequest.getAuthorization();
        boolean valid = Util.isDeleteAuthorizationValid(verificationHash, delAuthorization);
        if (valid)
            delete(dhtKey, delAuthorization);
        else
            log.debug("Invalid Delete Authorization in EmailPacketDeleteRequest. Should be: <" + verificationHash.toBase64() + ">");
    } else if (storedPacket != null)
        log.debug("EncryptedEmailPacket expected for DHT key <" + dhtKey + ">, found " + storedPacket.getClass().getSimpleName());
}
Also used : EmailPacketDeleteRequest(i2p.bote.packet.dht.EmailPacketDeleteRequest) DhtStorablePacket(i2p.bote.packet.dht.DhtStorablePacket) UniqueId(i2p.bote.UniqueId) EncryptedEmailPacket(i2p.bote.packet.dht.EncryptedEmailPacket) Hash(net.i2p.data.Hash)

Example 13 with UniqueId

use of i2p.bote.UniqueId 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);
}
Also used : DhtStorablePacket(i2p.bote.packet.dht.DhtStorablePacket) RetrieveRequest(i2p.bote.packet.dht.RetrieveRequest) UniqueId(i2p.bote.UniqueId) StoreRequest(i2p.bote.packet.dht.StoreRequest) Hash(net.i2p.data.Hash) FindClosePeersPacket(i2p.bote.packet.dht.FindClosePeersPacket) DeletionAwareDhtFolder(i2p.bote.folder.DeletionAwareDhtFolder) DeletionQuery(i2p.bote.packet.dht.DeletionQuery) DeletionInfoPacket(i2p.bote.packet.dht.DeletionInfoPacket) ResponsePacket(i2p.bote.packet.ResponsePacket) DhtStorageHandler(i2p.bote.network.DhtStorageHandler) DeleteRequest(i2p.bote.packet.dht.DeleteRequest)

Example 14 with UniqueId

use of i2p.bote.UniqueId in project i2p.i2p-bote by i2p.

the class IncompleteEmailFolder method addEmailPacket.

/**
 * Stores an <code>UnencryptedEmailPacket</code> in the folder and returns <code>true</code>
 * if an email was completed as a result of adding the packet.
 * @param packetToStore
 * @see i2p.bote.folder.PacketFolder#add(I2PBotePacket, String)
 */
public synchronized boolean addEmailPacket(UnencryptedEmailPacket packetToStore) {
    UniqueId messageId = packetToStore.getMessageId();
    // if a previously assembled (completed) email contained the message ID, ignore the email packet
    if (messageIdCache.contains(messageId)) {
        log.debug("Discarding email packet because the message ID matches a previously received email. Packet: " + packetToStore);
        return false;
    }
    add(packetToStore, getFilename(packetToStore));
    // TODO possible optimization: if getNumFragments == 1, no need to check for other packet files
    File[] finishedPacketFiles = getAllMatchingFiles(messageId);
    // if all packets of the email are available, assemble them into an email
    if (finishedPacketFiles.length == packetToStore.getNumFragments()) {
        assemble(finishedPacketFiles);
        messageIdCache.add(messageId);
        return true;
    }
    return false;
}
Also used : UniqueId(i2p.bote.UniqueId) File(java.io.File)

Example 15 with UniqueId

use of i2p.bote.UniqueId in project i2p.i2p-bote by i2p.

the class IndexPacketFolder method process.

/**
 * Deletes index packet entries.
 * @param delRequest An instance of {@link IndexPacketDeleteRequest}
 */
@Override
public synchronized void process(DeleteRequest delRequest) {
    log.debug("Processing delete request: " + delRequest);
    if (!(delRequest instanceof IndexPacketDeleteRequest))
        log.error("Invalid type of delete request for IndexPacketFolder: " + delRequest.getClass());
    IndexPacketDeleteRequest indexPacketDelRequest = (IndexPacketDeleteRequest) delRequest;
    Hash destHash = indexPacketDelRequest.getEmailDestHash();
    DhtStorablePacket storedPacket = retrieve(destHash);
    if (storedPacket instanceof IndexPacket) {
        IndexPacket indexPacket = (IndexPacket) storedPacket;
        Collection<Hash> keysToDelete = indexPacketDelRequest.getDhtKeys();
        for (Hash keyToDelete : keysToDelete) {
            // verify
            Hash verificationHash = indexPacket.getDeleteVerificationHash(keyToDelete);
            if (verificationHash == null)
                log.debug("Email packet key " + keyToDelete + " from IndexPacketDeleteRequest not found in index packet for destination " + destHash);
            else {
                UniqueId delAuthorization = indexPacketDelRequest.getDeleteAuthorization(keyToDelete);
                boolean valid = Util.isDeleteAuthorizationValid(verificationHash, delAuthorization);
                if (valid)
                    remove(indexPacket, keyToDelete, delAuthorization);
                else
                    log.debug("Invalid delete verification hash in IndexPacketDeleteRequest. Should be: <" + verificationHash.toBase64() + ">");
            }
        }
    } else if (storedPacket != null)
        log.debug("IndexPacket expected for DHT key <" + destHash + ">, found " + storedPacket.getClass().getSimpleName());
}
Also used : DhtStorablePacket(i2p.bote.packet.dht.DhtStorablePacket) IndexPacketDeleteRequest(i2p.bote.packet.dht.IndexPacketDeleteRequest) UniqueId(i2p.bote.UniqueId) IndexPacket(i2p.bote.packet.dht.IndexPacket) Hash(net.i2p.data.Hash)

Aggregations

UniqueId (i2p.bote.UniqueId)25 ByteArrayInputStream (java.io.ByteArrayInputStream)9 Before (org.junit.Before)9 EncryptedEmailPacket (i2p.bote.packet.dht.EncryptedEmailPacket)7 EmailDestination (i2p.bote.email.EmailDestination)6 Hash (net.i2p.data.Hash)6 UnencryptedEmailPacket (i2p.bote.packet.dht.UnencryptedEmailPacket)5 File (java.io.File)5 DataPacket (i2p.bote.packet.DataPacket)3 DeletionInfoPacket (i2p.bote.packet.dht.DeletionInfoPacket)3 DhtStorablePacket (i2p.bote.packet.dht.DhtStorablePacket)3 EmailPacketDeleteRequest (i2p.bote.packet.dht.EmailPacketDeleteRequest)3 IOException (java.io.IOException)3 Destination (net.i2p.data.Destination)3 EmailIdentity (i2p.bote.email.EmailIdentity)2 DeletionAwareDhtFolder (i2p.bote.folder.DeletionAwareDhtFolder)2 DhtStorageHandler (i2p.bote.network.DhtStorageHandler)2 ResponsePacket (i2p.bote.packet.ResponsePacket)2 DeleteRequest (i2p.bote.packet.dht.DeleteRequest)2 DeletionQuery (i2p.bote.packet.dht.DeletionQuery)2