Author: toad
Date: 2006-01-07 23:31:16 +0000 (Sat, 07 Jan 2006)
New Revision: 7811
Removed:
trunk/freenet/src/freenet/store/BaseFreenetStore.java
trunk/freenet/src/freenet/store/DataStore.java
Modified:
trunk/freenet/src/freenet/keys/ClientSSK.java
trunk/freenet/src/freenet/keys/InsertableClientSSK.java
trunk/freenet/src/freenet/keys/Key.java
trunk/freenet/src/freenet/keys/NodeCHK.java
trunk/freenet/src/freenet/node/TextModeClientInterface.java
trunk/freenet/src/freenet/node/Version.java
Log:
334:
MAKESSK works.
Modified: trunk/freenet/src/freenet/keys/ClientSSK.java
===================================================================
--- trunk/freenet/src/freenet/keys/ClientSSK.java 2006-01-07 23:17:48 UTC
(rev 7810)
+++ trunk/freenet/src/freenet/keys/ClientSSK.java 2006-01-07 23:31:16 UTC
(rev 7811)
@@ -21,6 +21,8 @@
/** Encrypted hashed docname */
public final byte[] ehDocname;
+ static final int CRYPTO_KEY_LENGTH = 32;
+
public ClientSSK(String docName, DSAPublicKey pubKey, byte[] cryptoKey)
{
this.docName = docName;
this.pubKey = pubKey;
Modified: trunk/freenet/src/freenet/keys/InsertableClientSSK.java
===================================================================
--- trunk/freenet/src/freenet/keys/InsertableClientSSK.java 2006-01-07
23:17:48 UTC (rev 7810)
+++ trunk/freenet/src/freenet/keys/InsertableClientSSK.java 2006-01-07
23:31:16 UTC (rev 7811)
@@ -9,9 +9,11 @@
import org.spaceroots.mantissa.random.MersenneTwister;
import freenet.crypt.DSA;
+import freenet.crypt.DSAGroup;
import freenet.crypt.DSAPrivateKey;
import freenet.crypt.DSAPublicKey;
import freenet.crypt.DSASignature;
+import freenet.crypt.Global;
import freenet.crypt.PCFBMode;
import freenet.crypt.RandomSource;
import freenet.crypt.UnsupportedCipherException;
@@ -157,5 +159,18 @@
return buf;
}
}
+
+ public static InsertableClientSSK createRandom(RandomSource r) {
+ byte[] ckey = new byte[CRYPTO_KEY_LENGTH];
+ r.nextBytes(ckey);
+ DSAGroup g = Global.DSAgroupBigA;
+ DSAPrivateKey privKey = new DSAPrivateKey(g, r);
+ DSAPublicKey pubKey = new DSAPublicKey(g, privKey);
+ return new InsertableClientSSK("", pubKey, privKey, ckey);
+ }
+
+ public FreenetURI getInsertURI() {
+ return new FreenetURI("SSK", docName,
privKey.getX().toByteArray(), cryptoKey, null);
+ }
}
Modified: trunk/freenet/src/freenet/keys/Key.java
===================================================================
--- trunk/freenet/src/freenet/keys/Key.java 2006-01-07 23:17:48 UTC (rev
7810)
+++ trunk/freenet/src/freenet/keys/Key.java 2006-01-07 23:31:16 UTC (rev
7811)
@@ -30,8 +30,6 @@
/** Whatever its type, it will need a routingKey ! */
final byte[] routingKey;
- /** 32 bytes for hash, 2 bytes for type */
- public static final short KEY_SIZE_ON_DISK = 34;
/** Code for 256-bit AES with PCFB */
static final short ALGO_AES_PCFB_256 = 1;
Modified: trunk/freenet/src/freenet/keys/NodeCHK.java
===================================================================
--- trunk/freenet/src/freenet/keys/NodeCHK.java 2006-01-07 23:17:48 UTC (rev
7810)
+++ trunk/freenet/src/freenet/keys/NodeCHK.java 2006-01-07 23:31:16 UTC (rev
7811)
@@ -18,6 +18,9 @@
*/
public class NodeCHK extends Key {
+ /** 32 bytes for hash, 2 bytes for type */
+ public static final short KEY_SIZE_ON_DISK = 34;
+
public NodeCHK(byte[] routingKey2) {
super(routingKey2);
if(routingKey2.length != KEY_LENGTH)
Modified: trunk/freenet/src/freenet/node/TextModeClientInterface.java
===================================================================
--- trunk/freenet/src/freenet/node/TextModeClientInterface.java 2006-01-07
23:17:48 UTC (rev 7810)
+++ trunk/freenet/src/freenet/node/TextModeClientInterface.java 2006-01-07
23:31:16 UTC (rev 7811)
@@ -28,6 +28,7 @@
import freenet.crypt.RandomSource;
import freenet.io.comm.PeerParseException;
import freenet.keys.FreenetURI;
+import freenet.keys.InsertableClientSSK;
import freenet.support.ArrayBucket;
import freenet.support.Bucket;
import freenet.support.BucketTools;
@@ -394,6 +395,10 @@
System.out.println("Insert threw: "+t);
t.printStackTrace();
}
+ } else if(uline.startsWith("MAKESSK")) {
+ InsertableClientSSK key = InsertableClientSSK.createRandom(r);
+ System.out.println("Insert URI:
"+key.getInsertURI().toString(false));
+ System.out.println("Request URI:
"+key.getURI().toString(false));
} else if(uline.startsWith("STATUS")) {
SimpleFieldSet fs = n.exportFieldSet();
System.out.println(fs.toString());
Modified: trunk/freenet/src/freenet/node/Version.java
===================================================================
--- trunk/freenet/src/freenet/node/Version.java 2006-01-07 23:17:48 UTC (rev
7810)
+++ trunk/freenet/src/freenet/node/Version.java 2006-01-07 23:31:16 UTC (rev
7811)
@@ -20,7 +20,7 @@
public static final String protocolVersion = "1.0";
/** The build number of the current revision */
- public static final int buildNumber = 333;
+ public static final int buildNumber = 334;
/** Oldest build of Fred we will talk to */
public static final int lastGoodBuild = 332;
Deleted: trunk/freenet/src/freenet/store/BaseFreenetStore.java
===================================================================
--- trunk/freenet/src/freenet/store/BaseFreenetStore.java 2006-01-07
23:17:48 UTC (rev 7810)
+++ trunk/freenet/src/freenet/store/BaseFreenetStore.java 2006-01-07
23:31:16 UTC (rev 7811)
@@ -1,162 +0,0 @@
-package freenet.store;
-
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.RandomAccessFile;
-
-import freenet.keys.CHKBlock;
-import freenet.keys.CHKVerifyException;
-import freenet.keys.KeyBlock;
-import freenet.keys.NodeCHK;
-import freenet.keys.NodeSSK;
-import freenet.keys.SSKBlock;
-import freenet.keys.SSKVerifyException;
-import freenet.support.Fields;
-import freenet.support.Logger;
-
-/**
- * @author amphibian
- *
- * Freenet datastore.
- */
-public class BaseFreenetStore implements FreenetStore {
-
- final DataStore dataStore;
- final DataStore headersStore;
-
- static final int DATA_BLOCK_SIZE = 32*1024;
- static final int HEADER_BLOCK_SIZE = 512;
-
- public BaseFreenetStore(String filename, long maxBlocks) throws
FileNotFoundException, Exception {
- this(new RandomAccessFile(filename+"-store","rw"), new
RandomAccessFile(filename+"-storeindex","rw"),
- new RandomAccessFile(filename+"-header","rw"), new
RandomAccessFile(filename+"-headerindex","rw"), maxBlocks);
- }
-
- public BaseFreenetStore(RandomAccessFile storeFile, RandomAccessFile
storeIndexFile,
- RandomAccessFile headerStoreFile, RandomAccessFile
headerStoreIndexFile, long maxBlocks) throws Exception {
- dataStore = new DataStore(storeIndexFile, storeFile, DATA_BLOCK_SIZE,
maxBlocks);
- headersStore = new DataStore(headerStoreIndexFile, headerStoreFile,
HEADER_BLOCK_SIZE, maxBlocks);
- }
-
- /**
- * @param storeFilename The name of the file containing the store.
- * @param headerStoreFilename The name of the file containing the headers
store.
- * @param maxBlocks The maximum number of chunks stored in this store.
- */
- public BaseFreenetStore(String storeFilename, String headerStoreFilename,
long maxBlocks) throws Exception {
- dataStore = new DataStore(new File(storeFilename), new
File(storeFilename+".index"), DATA_BLOCK_SIZE, maxBlocks);
- // FIXME: What's the right size? 512 is probably enough for SSKs?
- headersStore = new DataStore(new File(headerStoreFilename), new
File(headerStoreFilename+".index"), HEADER_BLOCK_SIZE, maxBlocks);
- }
-
- /**
- * Retrieve a block.
- * @return null if there is no such block stored, otherwise the block.
- */
- public synchronized CHKBlock fetch(NodeCHK chk, boolean dontPromote)
throws IOException {
- byte[] data = dataStore.getDataForBlock(chk, dontPromote);
- if(data == null) {
- if(headersStore.getDataForBlock(chk, true) != null) {
- Logger.normal(this, "Deleting: "+chk+" headers, no data");
- headersStore.delete(chk);
- }
- return null;
- }
- byte[] headers = headersStore.getDataForBlock(chk, dontPromote);
- if(headers == null) {
- // No headers, delete
- Logger.normal(this, "Deleting: "+chk+" data, no headers");
- dataStore.delete(chk);
- return null;
- }
- // Decode
- int headerLen = ((headers[0] & 0xff) << 8) + (headers[1] & 0xff);
- if(headerLen > HEADER_BLOCK_SIZE-2) {
- Logger.normal(this, "Invalid header data on "+chk+", deleting");
- dataStore.delete(chk);
- headersStore.delete(chk);
- return null;
- }
- byte[] buf = new byte[headerLen];
- System.arraycopy(headers, 2, buf, 0, headerLen);
- Logger.minor(this, "Get key: "+chk);
- Logger.minor(this, "Raw headers: "+headers.length+" bytes, hash
"+Fields.hashCode(headers));
- Logger.minor(this, "Headers: "+headerLen+" bytes, hash
"+Fields.hashCode(buf));
- Logger.minor(this, "Data: "+data.length+" bytes, hash
"+Fields.hashCode(data));
- try {
- return new CHKBlock(data, buf, chk);
- } catch (CHKVerifyException e) {
- Logger.normal(this, "Does not verify, deleting: "+chk);
- dataStore.delete(chk);
- headersStore.delete(chk);
- return null;
- }
- }
-
- /**
- * Retrieve a block.
- * @return null if there is no such block stored, otherwise the block.
- */
- public synchronized SSKBlock fetch(NodeSSK chk, boolean dontPromote)
throws IOException {
- byte[] data = dataStore.getDataForBlock(chk, dontPromote);
- if(data == null) {
- if(headersStore.getDataForBlock(chk, true) != null) {
- Logger.normal(this, "Deleting: "+chk+" headers, no data");
- headersStore.delete(chk);
- }
- return null;
- }
- byte[] headers = headersStore.getDataForBlock(chk, dontPromote);
- if(headers == null) {
- // No headers, delete
- Logger.normal(this, "Deleting: "+chk+" data, no headers");
- dataStore.delete(chk);
- return null;
- }
- // Decode
- int headerLen = ((headers[0] & 0xff) << 8) + (headers[1] & 0xff);
- if(headerLen > HEADER_BLOCK_SIZE-2) {
- Logger.normal(this, "Invalid header data on "+chk+", deleting");
- dataStore.delete(chk);
- headersStore.delete(chk);
- return null;
- }
- byte[] buf = new byte[headerLen];
- System.arraycopy(headers, 2, buf, 0, headerLen);
- Logger.minor(this, "Get key: "+chk);
- Logger.minor(this, "Raw headers: "+headers.length+" bytes, hash
"+Fields.hashCode(headers));
- Logger.minor(this, "Headers: "+headerLen+" bytes, hash
"+Fields.hashCode(buf));
- Logger.minor(this, "Data: "+data.length+" bytes, hash
"+Fields.hashCode(data));
- try {
- return new SSKBlock(data, buf, chk, true);
- } catch (SSKVerifyException e) {
- Logger.normal(this, "Does not verify, deleting: "+chk);
- dataStore.delete(chk);
- headersStore.delete(chk);
- return null;
- }
- }
-
- /**
- * Store a block.
- */
- public synchronized void put(KeyBlock block) throws IOException {
- byte[] data = block.getRawData();
- byte[] headers = block.getRawHeaders();
- int hlen = headers.length;
- if(data.length != DATA_BLOCK_SIZE || hlen > HEADER_BLOCK_SIZE-2)
- throw new IllegalArgumentException("Too big - data:
"+data.length+" should be "+
- DATA_BLOCK_SIZE+", headers: "+hlen+" - should be
"+(HEADER_BLOCK_SIZE-2));
- byte[] hbuf = new byte[HEADER_BLOCK_SIZE];
- hbuf[0] = (byte)(hlen >> 8);
- hbuf[1] = (byte)(hlen & 0xff);
- System.arraycopy(headers, 0, hbuf, 2, hlen);
- Logger.minor(this, "Put key: "+block.getKey());
- Logger.minor(this, "Raw headers: "+hbuf.length+" bytes, hash
"+Fields.hashCode(hbuf));
- Logger.minor(this, "Headers: "+hlen+" bytes, hash
"+Fields.hashCode(headers));
- Logger.minor(this, "Data: "+data.length+" bytes, hash
"+Fields.hashCode(data));
- dataStore.addDataAsBlock(block.getKey(), data);
- headersStore.addDataAsBlock(block.getKey(), hbuf);
- }
-}
Deleted: trunk/freenet/src/freenet/store/DataStore.java
===================================================================
--- trunk/freenet/src/freenet/store/DataStore.java 2006-01-07 23:17:48 UTC
(rev 7810)
+++ trunk/freenet/src/freenet/store/DataStore.java 2006-01-07 23:31:16 UTC
(rev 7811)
@@ -1,243 +0,0 @@
-/*
- * Dijjer - A Peer to Peer HTTP Cache
- * Copyright (C) 2004,2005 Change.Tv, Inc
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-package freenet.store;
-
-import freenet.keys.Key;
-import freenet.support.Logger;
-
-import java.io.*;
-import java.util.*;
-
-public class DataStore extends Store {
-
- public class ATimeComparator implements Comparator {
-
- public int compare(Object arg0, Object arg1) {
- DataBlock db0 = (DataBlock) arg0;
- DataBlock db1 = (DataBlock) arg1;
- long a0 = db0.getLastAccessTime();
- long a1 = db1.getLastAccessTime();
- if(a0 < a1) return -1;
- if(a0 > a1) return 1;
- return 0;
- }
-
- }
-
- public static final String VERSION = "$Id: DataStore.java,v 1.5
2005/08/20 21:21:21 amphibian Exp $";
-
- private RandomAccessFile _index;
- private final int blockSize;
-
- public DataStore(RandomAccessFile indexFile, RandomAccessFile dataFile,
int blockSize, long maxBlocks) throws Exception {
- super(dataFile, maxBlocks);
- _index = indexFile;
- this.blockSize = blockSize;
- readStore();
- }
-
- public DataStore(File index, File data, int blockSize, long maxBlocks)
throws Exception {
- super(data, maxBlocks);
- _index = new RandomAccessFile(index, "rw");
- this.blockSize = blockSize;
- readStore();
- }
-
- public synchronized void shutdown(boolean exit) throws IOException {
- super.shutdown(exit);
- _index.close();
- }
-
- protected synchronized void readStore() throws IOException {
- _index.seek(0);
- int recordNum = 0;
-
- Vector v = new Vector();
-
- try {
- while (_index.getFilePointer() < _index.length()) {
-
- Key k = Key.read(_index);
- long atime = _index.readLong();
- DataBlock dataBlock = new DataBlock(recordNum,
- k, atime);
-
- getKeyMap().put(dataBlock.getKey(), dataBlock);
- getRecordNumberList().add(recordNum, dataBlock);
- v.add(dataBlock);
- recordNum++;
- }
- } catch (EOFException e) {
- // Chopped off in the middle of a key
- Logger.normal(this, "Store index truncated");
- return;
- } finally {
- DataBlock[] blocks = (DataBlock[]) v.toArray(new
DataBlock[v.size()]);
- Arrays.sort(blocks, new ATimeComparator());
- for(int i=0;i<blocks.length;i++) {
- updateLastAccess(blocks[i]);
- }
- }
- }
-
- protected void deleteBlock(Block block, boolean wipeFromDisk) throws
IOException {
- DataBlock dataBlock = (DataBlock) block;
- getKeyMap().remove(dataBlock.getKey());
- getAccessTimeList().remove(dataBlock);
- if (wipeFromDisk) {
- DataBlock lastDataBlock = getLastBlock();
- setRecordNumber(dataBlock.getRecordNumber(),
lastDataBlock);
- _index.setLength(_index.length() -
DataBlock.SIZE_ON_DISK);
- getBlockStore().setLength(getBlockStore().length() -
blockSize);
- } else {
-
getRecordNumberList().remove(dataBlock.getRecordNumber());
- }
- }
-
- public synchronized void addDataAsBlock(Key key, byte[] data) throws
IOException {
- if (getKeyMap().containsKey(key)) {
- Logger.minor(this, "Already have key: "+key);
- return;
- }
-
- if (_index.length() / DataBlock.SIZE_ON_DISK < getMaxBlocks()) {
- int recnum = (int) (_index.length() /
DataBlock.SIZE_ON_DISK);
- createAndOverwrite(recnum, key, data);
- } else {
- DataBlock oldest = (DataBlock)
getAccessTimeList().getFirst();
- deleteBlock(oldest, false);
-
- int recNo = oldest.getRecordNumber();
- createAndOverwrite(recNo, key, data);
- }
- }
-
- /**
- * Moves this record to a new position, overwriting whatever on-disk
data was there previously, but not deleting the
- * old on-disk data for this record.
- *
- */
- protected void setRecordNumber(int newRecNo, DataBlock dataBlock)
throws IOException {
- if (newRecNo == dataBlock.getRecordNumber()) {
- return;
- }
- _index.seek((long)newRecNo * DataBlock.SIZE_ON_DISK);
- dataBlock.getKey().write(_index);
- _index.writeLong(dataBlock.getLastAccessTime());
-
- byte[] ba = new byte[blockSize];
- getBlockStore().seek(dataBlock.positionInDataFile());
- getBlockStore().readFully(ba);
- getBlockStore().seek((long)newRecNo * blockSize);
- getBlockStore().write(ba);
-
- getRecordNumberList().remove(dataBlock.getRecordNumber());
- dataBlock.setRecordNumber(newRecNo);
- getRecordNumberList().add(newRecNo, dataBlock);
-
-
- }
-
- /**
- * Creates a new block, overwriting the data on disk for an existing
block
- * (but *not* deleting that block from RAM)
- */
- private void createAndOverwrite(int recnum, Key key, byte[] data)
throws IOException {
- Logger.minor(this, "createAndOverwrite("+recnum+","+key+")");
- DataBlock b = new DataBlock(recnum, key,
System.currentTimeMillis());
- _index.seek((long)recnum * DataBlock.SIZE_ON_DISK);
-
- key.write(_index);
- getKeyMap().put(key, b);
-
- _index.writeLong(b.getLastAccessTime());
- getAccessTimeList().addLast(b);
-
- getBlockStore().seek((long)recnum * blockSize);
- getBlockStore().write(data);
- getRecordNumberList().add(recnum, b);
- }
-
- public synchronized byte[] getDataForBlock(Key key, boolean
dontPromote) throws IOException {
- DataBlock b = getBlockByKey(key);
- if (b == null) {
- return null;
- } else {
- Logger.minor(this, "Reading block: "+b.getRecordNumber());
- return readData(b, dontPromote);
- }
- }
-
- public Set getAllKeys() {
- return ((Map)getKeyMap().clone()).keySet();
- }
-
- private byte[] readData(DataBlock dataBlock, boolean dontPromote)
throws IOException {
- byte[] ba = new byte[blockSize];
- getBlockStore().seek(dataBlock.positionInDataFile());
- getBlockStore().readFully(ba);
- dataBlock.setLastAccessTime(System.currentTimeMillis()) ;
-
- if(!dontPromote) {
- getAccessTimeList().remove(dataBlock);
- getAccessTimeList().addLast(dataBlock);
- _index.seek(dataBlock.positionInIndexFile() +
DataBlock.KEY_SIZE);
- _index.writeLong(dataBlock.getLastAccessTime());
- }
- return ba;
-
- }
-
-
- private DataBlock getBlockByKey(Key key) {
- return (DataBlock) getKeyMap().get(key);
- }
-
- public DataBlock getLastBlock() {
- return (DataBlock) getRecordNumberList().lastElement();
- }
-
- public int getCacheSize() {
- return getAccessTimeList().size();
- }
-
- class DataBlock extends Block {
-
- public static final String VERSION = "$Id: DataStore.java,v 1.5
2005/08/20 21:21:21 amphibian Exp $";
-
- private static final short KEY_SIZE = Key.KEY_SIZE_ON_DISK;
- private static final short ACCESS_TIME_SIZE = 8;
- private static final short SIZE_ON_DISK = KEY_SIZE +
ACCESS_TIME_SIZE;
-
- public DataBlock(int recordNum, Key key, long accessTime) {
- super(recordNum, key, accessTime);
- }
-
- public long positionInIndexFile() {
- /* key + 8 byte last access time */
- return getRecordNumber() * SIZE_ON_DISK;
- }
-
- public long positionInDataFile() {
- return getRecordNumber() * blockSize;
- }
- }
-}
-