@Override public boolean equals(Object o) { if(!(o instanceof ClientCHKBlock)) return false; ClientCHKBlock block = (ClientCHKBlock) o; if(!key.equals(block.key)) return false; return block.block.equals(this.block); }
@Override public Key getNodeKey(boolean cloneKey) { return cloneKey ? getNodeCHK().cloneKey() : getNodeCHK(); }
public void readKeys(DataInputStream dis, boolean check) throws IOException { int count = check ? checkBlocks : dataBlocks; int offset = check ? dataBlocks : 0; if(commonDecryptKey != null) { int rkOffset = offset * NodeCHK.KEY_LENGTH; for(int i=0;i<count;i++) { dis.readFully(routingKeys, rkOffset, NodeCHK.KEY_LENGTH); rkOffset += NodeCHK.KEY_LENGTH; } } else { int rkOffset = offset * NodeCHK.KEY_LENGTH; int extraOffset = offset * EXTRA_BYTES_LENGTH; assert(NodeCHK.KEY_LENGTH == ClientCHK.CRYPTO_KEY_LENGTH); for(int i=0;i<count;i++) { ClientCHK key = ClientCHK.readRawBinaryKey(dis); byte[] r = key.getRoutingKey(); System.arraycopy(r, 0, routingKeys, rkOffset, NodeCHK.KEY_LENGTH); byte[] c = key.getCryptoKey(); System.arraycopy(c, 0, decryptKeys, rkOffset, NodeCHK.KEY_LENGTH); rkOffset += NodeCHK.KEY_LENGTH; byte[] e = key.getExtra(); System.arraycopy(e, 0, extraBytesForKeys, extraOffset, EXTRA_BYTES_LENGTH); extraOffset += EXTRA_BYTES_LENGTH; } } }
static void innerWriteKey(ClientCHK key, DataOutputStream dos, boolean hasSplitfileKey) throws IOException { if(hasSplitfileKey) { dos.write(key.getRoutingKey()); } else { key.writeRawBinaryKey(dos); } }
public void setKey(int i, ClientCHK key) { byte[] r = key.getRoutingKey(); System.arraycopy(r, 0, routingKeys, i * NodeCHK.KEY_LENGTH, NodeCHK.KEY_LENGTH); if(decryptKeys != null) { byte[] c = key.getCryptoKey(); System.arraycopy(c, 0, decryptKeys, i * ClientCHK.CRYPTO_KEY_LENGTH, ClientCHK.CRYPTO_KEY_LENGTH); } if(extraBytesForKeys != null) { byte[] e = key.getExtra(); System.arraycopy(e, 0, extraBytesForKeys, i * EXTRA_BYTES_LENGTH, EXTRA_BYTES_LENGTH); } }
private ClientCHKBlock encodeBlock(ClientCHK key, byte[] data) { try { return ClientCHKBlock.encodeSplitfileBlock(data, key.getCryptoKey(), key.getCryptoAlgorithm()); } catch (CHKEncodeException e) { return null; } }
public void testZeroSize() throws IOException, CHKEncodeException, CHKVerifyException, CHKDecodeException { File f = new File(tempDir, "saltstore"); FileUtil.removeAll(f); CHKStore store = new CHKStore(); SaltedHashFreenetStore<CHKBlock> saltStore = SaltedHashFreenetStore.construct(f, "testCachingFreenetStoreCHK", store, weakPRNG, 10, false, SemiOrderedShutdownHook.get(), true, true, ticker, null); CachingFreenetStoreTracker tracker = new CachingFreenetStoreTracker(0, cachingFreenetStorePeriod, ticker); CachingFreenetStore<CHKBlock> cachingStore = new CachingFreenetStore<CHKBlock>(store, saltStore, tracker); cachingStore.start(null, true); for(int i=0;i<5;i++) { String test = "test" + i; ClientCHKBlock block = encodeBlockCHK(test); store.put(block.getBlock(), false); ClientCHK key = block.getClientKey(); // It should pass straight through. assertNotNull(saltStore.fetch(key.getRoutingKey(), key.getNodeCHK().getFullKey(), false, false, false, false, null)); CHKBlock verify = store.fetch(key.getNodeCHK(), false, false, null); String data = decodeBlockCHK(verify, key); assertEquals(test, data); } cachingStore.close(); }
private boolean checkEncodedDataBlocks(byte[][] checkBlocks, boolean[] checkBlocksPresent, SplitFileSegmentKeys keys, boolean capturingBinaryBlob) { for(int i=0;i<checkBlocks.length;i++) { if(checkBlocksPresent[i]) continue; ClientCHK decodeKey = keys.getKey(i+blocksForDecode(), null, false); // Encode it to check whether the key is the same. ClientCHKBlock block; try { block = ClientCHKBlock.encodeSplitfileBlock(checkBlocks[i], decodeKey.getCryptoKey(), decodeKey.getCryptoAlgorithm()); ClientCHK actualKey = block.getClientKey(); if(!actualKey.equals(decodeKey)) { Logger.error(this, "Splitfile check block "+i+" does not encode to expected key for "+this+" for "+parent); return false; } if(capturingBinaryBlob) parent.fetcher.maybeAddToBinaryBlob(block); } catch (CHKEncodeException e) { // Impossible! parent.fail(new FetchException(FetchExceptionMode.INTERNAL_ERROR, "Decoded block could not be encoded")); Logger.error(this, "Impossible: Decoded block could not be encoded"); return false; } } return true; }
RequestSender rs = null; try { Object o = node.makeRequestSender(key.getNodeCHK(), node.maxHTL(), uid, tag, null, localOnly, ignoreStore, false, true, canWriteClientCache, realTimeFlag); if(o instanceof CHKBlock) try { rejectedOverload = true; long rtt = System.currentTimeMillis() - startTime; double targetLocation=key.getNodeCHK().toNormalizedDouble(); node.nodeStats.reportCHKOutcome(rtt, false, targetLocation, realTimeFlag); (status == RequestSender.GET_OFFER_VERIFY_FAILURE))) { long rtt = System.currentTimeMillis() - startTime; double targetLocation=key.getNodeCHK().toNormalizedDouble(); if(!rejectedOverload) requestStarters.requestCompleted(false, false, key.getNodeKey(true), realTimeFlag);
if((redirectedKey instanceof ClientCHK) && !((ClientCHK)redirectedKey).isMetadata()) { rcb.onBlockSetFinished(this, context); byte [] redirectedCryptoKey = ((ClientCHK)redirectedKey).getCryptoKey(); if (key instanceof ClientCHK && !Arrays.equals( ((ClientCHK)key).getCryptoKey(), redirectedCryptoKey)) redirectedCryptoKey = null; metadata.getMaxCompatMode(), redirectedCryptoKey, !((ClientCHK)redirectedKey).isCompressed(), true, true, context);
/** * Read a raw binary CHK. This is an ultra-compact representation, for * splitfile metadata etc. */ public static ClientCHK readRawBinaryKey(DataInputStream dis) throws IOException { return new ClientCHK(dis); }
/** * Read a key using the current settings. * @throws IOException * @throws MalformedURLException If the key could not be read due to an error in parsing the key. * REDFLAG: May want to recover from these in future, hence the short length. */ private FreenetURI readKey(DataInputStream dis) throws IOException { // Read URL if(fullKeys) { return FreenetURI.readFullBinaryKeyWithLength(dis); } else { return ClientCHK.readRawBinaryKey(dis).getURI(); } }
public byte[] getExtra() { return getExtra(cryptoAlgorithm, compressionAlgorithm, controlDocument); }
} else { for(int i=0;i<splitfileBlocks;i++) dos.write(splitfileDataKeys[i].getRoutingKey()); for(int i=0;i<splitfileCheckBlocks;i++) dos.write(splitfileCheckKeys[i].getRoutingKey());
private NodeCHK getNodeKey(int x, boolean copy) { int xr = x * NodeCHK.KEY_LENGTH; byte[] routingKey = Arrays.copyOfRange(routingKeys, xr, xr + NodeCHK.KEY_LENGTH); byte[] extra; if(commonExtraBytes != null) { extra = commonExtraBytes; } else { int xe = x * EXTRA_BYTES_LENGTH; extra = Arrays.copyOfRange(extraBytesForKeys, xe, xe + EXTRA_BYTES_LENGTH); } byte cryptoAlgorithm = ClientCHK.getCryptoAlgorithmFromExtra(extra); return new NodeCHK(routingKey, cryptoAlgorithm); }
SplitFileSegmentKeys keys = segment.readSegmentKeys(); for(int j=0;j<keys.totalKeys();j++) { keyListener.addKey(keys.getKey(j, null, false).getNodeKey(false), i, salt);
keys[i] = chk; Logger.minor(RealNodeRequestInsertTest.class, "Decoded: "+new String(newBlock.memoryDecode(), "UTF-8")); Logger.normal(RealNodeRequestInsertTest.class,"CHK: "+chk.getURI()); Logger.minor(RealNodeRequestInsertTest.class,"Headers: "+HexUtil.bytesToHex(block.getHeaders())); System.err.println("Queueing requests for "+i+" of "+INSERT_KEYS); for(int j=0;j<nodes.length;j++) { clients[j].prefetch(key.getURI(), DAYS.toMillis(1), 32768, null);
public void testSimpleCHK() throws IOException, CHKEncodeException, CHKVerifyException, CHKDecodeException { File f = new File(tempDir, "saltstore"); FileUtil.removeAll(f); CHKStore store = new CHKStore(); SaltedHashFreenetStore<CHKBlock> saltStore = SaltedHashFreenetStore.construct(f, "testCachingFreenetStoreCHK", store, weakPRNG, 10, false, SemiOrderedShutdownHook.get(), true, true, ticker, null); CachingFreenetStoreTracker tracker = new CachingFreenetStoreTracker(cachingFreenetStoreMaxSize, cachingFreenetStorePeriod, ticker); CachingFreenetStore<CHKBlock> cachingStore = new CachingFreenetStore<CHKBlock>(store, saltStore, tracker); cachingStore.start(null, true); for(int i=0;i<5;i++) { String test = "test" + i; ClientCHKBlock block = encodeBlockCHK(test); store.put(block.getBlock(), false); ClientCHK key = block.getClientKey(); // Check that it's in the cache, *not* the underlying store. assertEquals(saltStore.fetch(key.getRoutingKey(), key.getNodeCHK().getFullKey(), false, false, false, false, null), null); CHKBlock verify = store.fetch(key.getNodeCHK(), false, false, null); String data = decodeBlockCHK(verify, key); assertEquals(test, data); } cachingStore.close(); }
public synchronized byte[] checkAndGetBlockData(int blockNum) throws IOException { if(!blockChooser.hasSucceeded(blockNum)) return null; ClientCHK key = getKey(blockNum); if(key == null) return null; for(int i=0;i<blocksFetched.length;i++) { if(blocksFetched[i] == blockNum) { byte[] buf = readBlock(i); try { ClientCHKBlock block = ClientCHKBlock.encodeSplitfileBlock(buf, key.getCryptoKey(), key.getCryptoAlgorithm()); if(!(block.getClientKey().equals(key))) { Logger.error(this, "Block "+blockNum+" in blocksFound["+i+"] is not valid!"); blockChooser.onUnSuccess(blockNum); succeeded = false; finished = false; } else { return buf; } } catch (CHKEncodeException e) { // Should not be possible. Logger.error(this, "Impossible: "+e); return null; } } } Logger.error(this, "Block "+blockNum+" in blocksFound but not in blocksFetched on "+this); return null; }
public int getBlockNumber(ClientCHK key, boolean[] ignoreSlots) { byte[] rkey = key.getRoutingKey(); byte[] ckey = null; byte[] extra = null; if(ckey == null) ckey = key.getCryptoKey(); assert(ClientCHK.CRYPTO_KEY_LENGTH == NodeCHK.KEY_LENGTH); continue; if(extra == null) extra = key.getExtra(); if(commonExtraBytes != null) { if(!Arrays.equals(commonExtraBytes, extra)) continue;