From bback at freenetproject.org Fri Feb 2 11:42:41 2007 From: bback at freenetproject.org (bback at freenetproject.org) Date: Fri, 2 Feb 2007 11:42:41 +0000 (UTC) Subject: [freenet-cvs] r11648 - trunk/freenet/src/freenet/node/fcp Message-ID: <20070202114242.0F3D99BC24@emu.freenetproject.org> Author: bback Date: 2007-02-02 11:42:40 +0000 (Fri, 02 Feb 2007) New Revision: 11648 Modified: trunk/freenet/src/freenet/node/fcp/ClientGet.java trunk/freenet/src/freenet/node/fcp/ClientPutBase.java trunk/freenet/src/freenet/node/fcp/ClientRequest.java trunk/freenet/src/freenet/node/fcp/ModifyPersistentRequest.java trunk/freenet/src/freenet/node/fcp/RemovePersistentRequest.java Log: - added ack of ModifyPersistentRequest - !!! added ack of RemovePersistentRequest (new message PersistentRequestRemoved) Modified: trunk/freenet/src/freenet/node/fcp/ClientGet.java =================================================================== --- trunk/freenet/src/freenet/node/fcp/ClientGet.java 2007-01-31 19:33:47 UTC (rev 11647) +++ trunk/freenet/src/freenet/node/fcp/ClientGet.java 2007-02-02 11:42:40 UTC (rev 11648) @@ -423,7 +423,7 @@ handler.queue(allDataPending); } - private FCPMessage persistentTagMessage() { + protected FCPMessage persistentTagMessage() { return new PersistentGet(identifier, uri, verbosity, priorityClass, returnType, persistenceType, targetFile, tempFile, clientToken, client.isGlobalQueue, started, fctx.maxNonSplitfileRetries); } @@ -657,5 +657,4 @@ return false; } } - } Modified: trunk/freenet/src/freenet/node/fcp/ClientPutBase.java =================================================================== --- trunk/freenet/src/freenet/node/fcp/ClientPutBase.java 2007-01-31 19:33:47 UTC (rev 11647) +++ trunk/freenet/src/freenet/node/fcp/ClientPutBase.java 2007-02-02 11:42:40 UTC (rev 11648) @@ -240,8 +240,6 @@ trySendFinalMessage(handler); } - protected abstract FCPMessage persistentTagMessage(); - public synchronized SimpleFieldSet getFieldSet() { SimpleFieldSet fs = new SimpleFieldSet(); // we will need multi-level later... fs.put("Type", getTypeName()); Modified: trunk/freenet/src/freenet/node/fcp/ClientRequest.java =================================================================== --- trunk/freenet/src/freenet/node/fcp/ClientRequest.java 2007-01-31 19:33:47 UTC (rev 11647) +++ trunk/freenet/src/freenet/node/fcp/ClientRequest.java 2007-02-02 11:42:40 UTC (rev 11648) @@ -1,20 +1,13 @@ package freenet.node.fcp; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.IOException; -import java.net.MalformedURLException; +import java.io.*; +import java.net.*; -import freenet.client.async.ClientRequester; -import freenet.keys.FreenetURI; -import freenet.support.Fields; -import freenet.support.HexUtil; -import freenet.support.Logger; -import freenet.support.SimpleFieldSet; -import freenet.support.api.Bucket; -import freenet.support.io.FileBucket; -import freenet.support.io.PaddedEphemerallyEncryptedBucket; -import freenet.support.io.SerializableToFieldSetBucket; +import freenet.client.async.*; +import freenet.keys.*; +import freenet.support.*; +import freenet.support.api.*; +import freenet.support.io.*; /** * A request process carried out by the node for an FCP client. @@ -298,6 +291,24 @@ public abstract boolean restart(); + protected abstract FCPMessage persistentTagMessage(); + + /** + * Called after a ModifyPersistentRequest. Send a PersistentTagMessage to the clients. + */ + public void requestWasModified() { + FCPMessage msg = persistentTagMessage(); + client.queueClientRequestMessage(msg, 0); + } + + /** + * Called after a RemovePersistentRequest. Send a PersistentRequestRemoved to the clients. + */ + public void requestWasRemoved() { + FCPMessage msg = new PersistentRequestRemovedMessage(getIdentifier(), global); + client.queueClientRequestMessage(msg, 0); + } + /** Utility method for storing details of a possibly encrypted bucket. */ protected void bucketToFS(SimpleFieldSet fs, String name, boolean includeSize, Bucket data) { SerializableToFieldSetBucket bucket = (SerializableToFieldSetBucket) data; Modified: trunk/freenet/src/freenet/node/fcp/ModifyPersistentRequest.java =================================================================== --- trunk/freenet/src/freenet/node/fcp/ModifyPersistentRequest.java 2007-01-31 19:33:47 UTC (rev 11647) +++ trunk/freenet/src/freenet/node/fcp/ModifyPersistentRequest.java 2007-02-02 11:42:40 UTC (rev 11648) @@ -78,6 +78,7 @@ req.setPriorityClass(priorityClass); if(req.isPersistentForever()) client.server.forceStorePersistentRequests(); + + req.requestWasModified(); } - } Modified: trunk/freenet/src/freenet/node/fcp/RemovePersistentRequest.java =================================================================== --- trunk/freenet/src/freenet/node/fcp/RemovePersistentRequest.java 2007-01-31 19:33:47 UTC (rev 11647) +++ trunk/freenet/src/freenet/node/fcp/RemovePersistentRequest.java 2007-02-02 11:42:40 UTC (rev 11648) @@ -4,8 +4,7 @@ package freenet.node.fcp; import freenet.node.Node; -import freenet.support.Fields; -import freenet.support.SimpleFieldSet; +import freenet.support.*; /** * Client telling node to remove a (completed or not) persistent request. @@ -37,7 +36,12 @@ public void run(FCPConnectionHandler handler, Node node) throws MessageInvalidException { FCPClient client = global ? handler.server.globalClient : handler.getClient(); + ClientRequest req = client.getRequest(identifier); + if(req==null){ + Logger.error(this, "Huh ? the request is null!"); + return; + } client.removeByIdentifier(identifier, true); + req.requestWasRemoved(); } - } From bback at freenetproject.org Fri Feb 2 11:43:53 2007 From: bback at freenetproject.org (bback at freenetproject.org) Date: Fri, 2 Feb 2007 11:43:53 +0000 (UTC) Subject: [freenet-cvs] r11649 - trunk/freenet/src/freenet/node/fcp Message-ID: <20070202114353.E6CB39BC24@emu.freenetproject.org> Author: bback Date: 2007-02-02 11:43:52 +0000 (Fri, 02 Feb 2007) New Revision: 11649 Added: trunk/freenet/src/freenet/node/fcp/PersistentRequestRemovedMessage.java Log: - added ack of ModifyPersistentRequest - !!! added ack of RemovePersistentRequest (new message PersistentRequestRemoved) Added: trunk/freenet/src/freenet/node/fcp/PersistentRequestRemovedMessage.java =================================================================== --- trunk/freenet/src/freenet/node/fcp/PersistentRequestRemovedMessage.java (rev 0) +++ trunk/freenet/src/freenet/node/fcp/PersistentRequestRemovedMessage.java 2007-02-02 11:43:52 UTC (rev 11649) @@ -0,0 +1,36 @@ +/* This code is part of Freenet. It is distributed under the GNU General + * Public License, version 2 (or at your option any later version). See + * http://www.gnu.org/ for further details of the GPL. */ +package freenet.node.fcp; + +import freenet.node.*; +import freenet.support.*; + +/** + * Node answer message after a RemovePersistentRequest message from client. + */ +public class PersistentRequestRemovedMessage extends FCPMessage { + + private final String ident; + private final boolean global; + + public PersistentRequestRemovedMessage(String identifier, boolean global) { + this.ident = identifier; + this.global = global; + } + + public SimpleFieldSet getFieldSet() { + SimpleFieldSet fs = new SimpleFieldSet(); + fs.put("Identifier", ident); + if(global) fs.put("Global", "true"); + return fs; + } + + public String getName() { + return "PersistentRequestRemoved"; + } + + public void run(FCPConnectionHandler handler, Node node) throws MessageInvalidException { + throw new MessageInvalidException(ProtocolErrorMessage.INVALID_MESSAGE, "PersistentRequestRemoved goes from server to client not the other way around", ident, global); + } +} From toad at freenetproject.org Fri Feb 2 12:53:12 2007 From: toad at freenetproject.org (toad at freenetproject.org) Date: Fri, 2 Feb 2007 12:53:12 +0000 (UTC) Subject: [freenet-cvs] r11650 - trunk/freenet/src/freenet/node/useralerts Message-ID: <20070202125312.3152620B245@emu.freenetproject.org> Author: toad Date: 2007-02-02 12:53:11 +0000 (Fri, 02 Feb 2007) New Revision: 11650 Modified: trunk/freenet/src/freenet/node/useralerts/PeerManagerUserAlert.java Log: Spelling Modified: trunk/freenet/src/freenet/node/useralerts/PeerManagerUserAlert.java =================================================================== --- trunk/freenet/src/freenet/node/useralerts/PeerManagerUserAlert.java 2007-02-02 11:43:52 UTC (rev 11649) +++ trunk/freenet/src/freenet/node/useralerts/PeerManagerUserAlert.java 2007-02-02 12:53:11 UTC (rev 11650) @@ -129,7 +129,7 @@ "This node has to wait too long for available bandwidth ({BWLIMIT_DELAY_TIME} > "+Node.MAX_BWLIMIT_DELAY_TIME_ALERT_THRESHOLD+"). Increase your output bandwidth limit and/or remove some peers to improve the situation."; static final String TOO_HIGH_PING = - "This node is having trouble talking with it's peers quickly enough ({PING_TIME} > "+ + "This node is having trouble talking with its peers quickly enough ({PING_TIME} > "+ Node.MAX_NODE_AVERAGE_PING_TIME_ALERT_THRESHOLD+"). Decrease your output bandwidth limit and/or remove some peers to improve the situation."; static final String NEVER_CONNECTED_TWO_WEEKS = From toad at freenetproject.org Fri Feb 2 18:39:17 2007 From: toad at freenetproject.org (toad at freenetproject.org) Date: Fri, 2 Feb 2007 18:39:17 +0000 (UTC) Subject: [freenet-cvs] r11651 - trunk/freenet/src/freenet/node Message-ID: <20070202183917.23A6E20B250@emu.freenetproject.org> Author: toad Date: 2007-02-02 18:39:16 +0000 (Fri, 02 Feb 2007) New Revision: 11651 Modified: trunk/freenet/src/freenet/node/FNPPacketMangler.java Log: Convert NPE to log error Modified: trunk/freenet/src/freenet/node/FNPPacketMangler.java =================================================================== --- trunk/freenet/src/freenet/node/FNPPacketMangler.java 2007-02-02 12:53:11 UTC (rev 11650) +++ trunk/freenet/src/freenet/node/FNPPacketMangler.java 2007-02-02 18:39:16 UTC (rev 11651) @@ -918,6 +918,10 @@ int[] alreadyReported = new int[messages.length]; MessageItem[] newMsgs = new MessageItem[messages.length]; KeyTracker kt = pn.getCurrentKeyTracker(); + if(kt == null) { + Logger.error(this, "Not connected while sending packets: "+pn); + return; + } int length = 1; length += kt.countAcks() + kt.countAckRequests() + kt.countResendRequests(); int callbacksCount = 0; From toad at freenetproject.org Sat Feb 3 14:14:55 2007 From: toad at freenetproject.org (toad at freenetproject.org) Date: Sat, 3 Feb 2007 14:14:55 +0000 (UTC) Subject: [freenet-cvs] r11652 - trunk/freenet/src/freenet/io/comm Message-ID: <20070203141455.A575A9BBE8@emu.freenetproject.org> Author: toad Date: 2007-02-03 14:14:54 +0000 (Sat, 03 Feb 2007) New Revision: 11652 Modified: trunk/freenet/src/freenet/io/comm/UdpSocketManager.java Log: Don't create a new byte[] and DatagramPacket each time. Modified: trunk/freenet/src/freenet/io/comm/UdpSocketManager.java =================================================================== --- trunk/freenet/src/freenet/io/comm/UdpSocketManager.java 2007-02-02 18:39:16 UTC (rev 11651) +++ trunk/freenet/src/freenet/io/comm/UdpSocketManager.java 2007-02-03 14:14:54 UTC (rev 11652) @@ -214,10 +214,13 @@ } private void realRun() { - DatagramPacket packet = getPacket(); + // Single receiving thread + byte[] buf = new byte[MAX_RECEIVE_SIZE]; + DatagramPacket packet = new DatagramPacket(buf, buf.length); + boolean gotPacket = getPacket(packet); // Check for timedout _filters removeTimedOutFilters(); - if (packet != null) { + if (gotPacket) { long startTime = System.currentTimeMillis(); Peer peer = new Peer(packet.getAddress(), packet.getPort()); long endTime = System.currentTimeMillis(); @@ -280,22 +283,19 @@ // Revert to 1500? private static final int MAX_RECEIVE_SIZE = 2048; - private DatagramPacket getPacket() { - // TODO: Avoid recreating the packet each time (warning, there are some issues reusing DatagramPackets, be - // careful) - DatagramPacket packet = new DatagramPacket(new byte[MAX_RECEIVE_SIZE], MAX_RECEIVE_SIZE); + private boolean getPacket(DatagramPacket packet) { try { _sock.receive(packet); // TODO: keep? IOStatisticCollector.addInfo(packet.getAddress() + ":" + packet.getPort(), packet.getLength(), 0); } catch (SocketTimeoutException e1) { - packet = null; + return false; } catch (IOException e2) { throw new RuntimeException(e2); } if(logMINOR) Logger.minor(this, "Received packet"); - return packet; + return true; } private void removeTimedOutFilters() { From toad at freenetproject.org Sat Feb 3 14:15:49 2007 From: toad at freenetproject.org (toad at freenetproject.org) Date: Sat, 3 Feb 2007 14:15:49 +0000 (UTC) Subject: [freenet-cvs] r11653 - trunk/freenet/src/freenet/io/comm Message-ID: <20070203141549.AAD419BC22@emu.freenetproject.org> Author: toad Date: 2007-02-03 14:15:49 +0000 (Sat, 03 Feb 2007) New Revision: 11653 Modified: trunk/freenet/src/freenet/io/comm/IncomingPacketFilter.java Log: Comments Modified: trunk/freenet/src/freenet/io/comm/IncomingPacketFilter.java =================================================================== --- trunk/freenet/src/freenet/io/comm/IncomingPacketFilter.java 2007-02-03 14:14:54 UTC (rev 11652) +++ trunk/freenet/src/freenet/io/comm/IncomingPacketFilter.java 2007-02-03 14:15:49 UTC (rev 11653) @@ -13,7 +13,8 @@ * Process an incoming packet. This method should call * USM.decodePacket() and USM.checkFilters() if necessary to * decode and dispatch messages. - * @param buf The buffer to read from. + * @param buf The buffer to read from. Note that this may be reused later on; any + * data to keep must be copied. * @param offset The offset to start reading from. * @param length The length in bytes to read. * @param peer The peer which sent us the packet. We only know From toad at freenetproject.org Sat Feb 3 14:30:56 2007 From: toad at freenetproject.org (toad at freenetproject.org) Date: Sat, 3 Feb 2007 14:30:56 +0000 (UTC) Subject: [freenet-cvs] r11654 - trunk/freenet/src/freenet/node Message-ID: <20070203143056.0220B9BC29@emu.freenetproject.org> Author: toad Date: 2007-02-03 14:30:55 +0000 (Sat, 03 Feb 2007) New Revision: 11654 Modified: trunk/freenet/src/freenet/node/Version.java Log: Transition time: > should be >= Modified: trunk/freenet/src/freenet/node/Version.java =================================================================== --- trunk/freenet/src/freenet/node/Version.java 2007-02-03 14:15:49 UTC (rev 11653) +++ trunk/freenet/src/freenet/node/Version.java 2007-02-03 14:30:55 UTC (rev 11654) @@ -29,7 +29,7 @@ /** Oldest build of Fred we will talk to */ private static final int oldLastGoodBuild = 1010; private static final int newLastGoodBuild = 1010; - private static final long transitionTime; + static final long transitionTime; static { final Calendar _cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); @@ -50,7 +50,7 @@ * data normally. */ public static final int lastGoodBuild() { - if(System.currentTimeMillis() > transitionTime) + if(System.currentTimeMillis() >= transitionTime) return newLastGoodBuild; else return oldLastGoodBuild; From toad at freenetproject.org Sat Feb 3 14:32:56 2007 From: toad at freenetproject.org (toad at freenetproject.org) Date: Sat, 3 Feb 2007 14:32:56 +0000 (UTC) Subject: [freenet-cvs] r11655 - trunk/freenet/src/freenet/node Message-ID: <20070203143256.750DC9BC29@emu.freenetproject.org> Author: toad Date: 2007-02-03 14:32:33 +0000 (Sat, 03 Feb 2007) New Revision: 11655 Modified: trunk/freenet/src/freenet/node/PacketSender.java trunk/freenet/src/freenet/node/PeerNode.java Log: Only update version flags when the version changes, and at the transition time. Modified: trunk/freenet/src/freenet/node/PacketSender.java =================================================================== --- trunk/freenet/src/freenet/node/PacketSender.java 2007-02-03 14:30:55 UTC (rev 11654) +++ trunk/freenet/src/freenet/node/PacketSender.java 2007-02-03 14:32:33 UTC (rev 11655) @@ -108,7 +108,20 @@ void start() { Logger.normal(this, "Starting PacketSender"); System.out.println("Starting PacketSender"); - lastTimeInSeconds = (int) (System.currentTimeMillis() / 1000); + long now = System.currentTimeMillis(); + long transition = Version.transitionTime; + if(now < transition) { + queueTimedJob(new Runnable() { + public void run() { + PeerNode[] nodes = node.peers.myPeers; + for(int i=0;i Author: toad Date: 2007-02-03 15:35:24 +0000 (Sat, 03 Feb 2007) New Revision: 11658 Modified: trunk/freenet/src/freenet/clients/http/filter/HTMLFilter.java Log: Another small memory optimisation Modified: trunk/freenet/src/freenet/clients/http/filter/HTMLFilter.java =================================================================== --- trunk/freenet/src/freenet/clients/http/filter/HTMLFilter.java 2007-02-03 14:41:37 UTC (rev 11657) +++ trunk/freenet/src/freenet/clients/http/filter/HTMLFilter.java 2007-02-03 15:35:24 UTC (rev 11658) @@ -150,11 +150,9 @@ switch (mode) { case INTEXT : if (c == '<') { - saveText(b, currentTag, w, this); - - b = new StringBuffer(100); - balt = new StringBuffer(4000); + b.setLength(0); + balt.setLength(0); mode = INTAG; } else { b.append(c); @@ -165,21 +163,21 @@ if (HTMLDecoder.isWhitespace(c)) { splitTag.add(b.toString()); mode = INTAGWHITESPACE; - b = new StringBuffer(100); + b.setLength(0); } else if ((c == '<') && Character.isWhitespace(balt.charAt(0))) { // Previous was an un-escaped < in a script. saveText(b, currentTag, w, this); - balt = new StringBuffer(4000); - b = new StringBuffer(100); + balt.setLength(0); + b.setLength(0); splitTag.clear(); } else if (c == '>') { splitTag.add(b.toString()); - b = new StringBuffer(100); + b.setLength(0); processTag(splitTag, w, this); currentTag = (String)splitTag.get(0); splitTag.clear(); - balt = new StringBuffer(4000); + balt.setLength(0); mode = INTEXT; } else if ( (b.length() == 2) @@ -253,7 +251,7 @@ case INTAGCOMMENTCLOSING : if (c == '>') { saveComment(b, w, this); - b = new StringBuffer(100); + b.setLength(0); mode = INTEXT; } else { b.append(c); @@ -275,14 +273,14 @@ killTag = false; currentTag = (String)splitTag.get(0); splitTag.clear(); - b = new StringBuffer(100); - balt = new StringBuffer(4000); + b.setLength(0); + balt.setLength(0); mode = INTEXT; } else if ((c == '<') && Character.isWhitespace(balt.charAt(0))) { // Previous was an un-escaped < in a script. saveText(balt, currentTag, w, this); - balt = new StringBuffer(4000); - b = new StringBuffer(100); + balt.setLength(0); + b.setLength(0); splitTag.clear(); mode = INTAG; } else if (HTMLDecoder.isWhitespace(c)) { From toad at freenetproject.org Sat Feb 3 16:04:57 2007 From: toad at freenetproject.org (toad at freenetproject.org) Date: Sat, 3 Feb 2007 16:04:57 +0000 (UTC) Subject: [freenet-cvs] r11659 - trunk/freenet/src/freenet/io/comm Message-ID: <20070203160457.E9A9E9BBD8@emu.freenetproject.org> Author: toad Date: 2007-02-03 16:04:57 +0000 (Sat, 03 Feb 2007) New Revision: 11659 Modified: trunk/freenet/src/freenet/io/comm/UdpSocketManager.java Log: Optimisation was broken (not working; no impact on function) Modified: trunk/freenet/src/freenet/io/comm/UdpSocketManager.java =================================================================== --- trunk/freenet/src/freenet/io/comm/UdpSocketManager.java 2007-02-03 15:35:24 UTC (rev 11658) +++ trunk/freenet/src/freenet/io/comm/UdpSocketManager.java 2007-02-03 16:04:57 UTC (rev 11659) @@ -198,10 +198,12 @@ } private void runLoop() { + byte[] buf = new byte[MAX_RECEIVE_SIZE]; + DatagramPacket packet = new DatagramPacket(buf, buf.length); while (/*_active*/true) { try { lastTimeInSeconds = (int) (System.currentTimeMillis() / 1000); - realRun(); + realRun(packet); } catch (OutOfMemoryError e) { OOMHandler.handleOOM(e); System.err.println("Will retry above failed operation..."); @@ -213,10 +215,8 @@ } } - private void realRun() { + private void realRun(DatagramPacket packet) { // Single receiving thread - byte[] buf = new byte[MAX_RECEIVE_SIZE]; - DatagramPacket packet = new DatagramPacket(buf, buf.length); boolean gotPacket = getPacket(packet); // Check for timedout _filters removeTimedOutFilters(); From toad at freenetproject.org Sat Feb 3 16:05:56 2007 From: toad at freenetproject.org (toad at freenetproject.org) Date: Sat, 3 Feb 2007 16:05:56 +0000 (UTC) Subject: [freenet-cvs] r11660 - trunk/freenet/src/freenet/io/comm Message-ID: <20070203160556.8568F9BBD8@emu.freenetproject.org> Author: toad Date: 2007-02-03 16:05:56 +0000 (Sat, 03 Feb 2007) New Revision: 11660 Modified: trunk/freenet/src/freenet/io/comm/MessageFilter.java Log: Reduce allocations in match() significantly at the cost of a few more bytes for a Vector Modified: trunk/freenet/src/freenet/io/comm/MessageFilter.java =================================================================== --- trunk/freenet/src/freenet/io/comm/MessageFilter.java 2007-02-03 16:04:57 UTC (rev 11659) +++ trunk/freenet/src/freenet/io/comm/MessageFilter.java 2007-02-03 16:05:56 UTC (rev 11660) @@ -38,6 +38,7 @@ private PeerContext _droppedConnection; private MessageType _type; private HashMap _fields = new HashMap(); + private Vector _fieldList = new Vector(1,1); PeerContext _source; private long _timeout; private int _initialTimeout; @@ -102,7 +103,8 @@ throw new IncorrectTypeException("Got " + fieldValue.getClass() + ", expected " + _type.typeOf(fieldName) + " for " + _type.getName()); } synchronized (_fields) { - _fields.put(fieldName, fieldValue); + if(_fields.put(fieldName, fieldValue) == null) + _fieldList.add(fieldName); } return this; } @@ -133,8 +135,8 @@ return false; } synchronized (_fields) { - for (Iterator iter = _fields.keySet().iterator(); iter.hasNext();) { - String fieldName = (String) iter.next(); + for (int i = 0; i < _fieldList.size(); i++) { + String fieldName = (String) _fieldList.get(i); if (!m.isSet(fieldName)) { return false; } From toad at freenetproject.org Sat Feb 3 16:28:24 2007 From: toad at freenetproject.org (toad at freenetproject.org) Date: Sat, 3 Feb 2007 16:28:24 +0000 (UTC) Subject: [freenet-cvs] r11661 - trunk/freenet/src/freenet/keys Message-ID: <20070203162824.11E5F20AFA7@emu.freenetproject.org> Author: toad Date: 2007-02-03 16:28:23 +0000 (Sat, 03 Feb 2007) New Revision: 11661 Modified: trunk/freenet/src/freenet/keys/ClientCHKBlock.java trunk/freenet/src/freenet/keys/ClientSSKBlock.java trunk/freenet/src/freenet/keys/Key.java Log: More memory optimisations Modified: trunk/freenet/src/freenet/keys/ClientCHKBlock.java =================================================================== --- trunk/freenet/src/freenet/keys/ClientCHKBlock.java 2007-02-03 16:05:56 UTC (rev 11660) +++ trunk/freenet/src/freenet/keys/ClientCHKBlock.java 2007-02-03 16:28:23 UTC (rev 11661) @@ -114,10 +114,7 @@ int size = ((hbuf[32] & 0xff) << 8) + (hbuf[33] & 0xff); if((size > 32768) || (size < 0)) throw new CHKDecodeException("Invalid size: "+size); - byte[] output = new byte[size]; - // No particular reason to check the padding, is there? - System.arraycopy(dbuf, 0, output, 0, size); - return Key.decompress(dontCompress ? false : key.isCompressed(), output, bf, Math.min(maxLength, MAX_LENGTH_BEFORE_COMPRESSION), key.compressionAlgorithm, false); + return Key.decompress(dontCompress ? false : key.isCompressed(), dbuf, size, bf, Math.min(maxLength, MAX_LENGTH_BEFORE_COMPRESSION), key.compressionAlgorithm, false); } /** Modified: trunk/freenet/src/freenet/keys/ClientSSKBlock.java =================================================================== --- trunk/freenet/src/freenet/keys/ClientSSKBlock.java 2007-02-03 16:05:56 UTC (rev 11660) +++ trunk/freenet/src/freenet/keys/ClientSSKBlock.java 2007-02-03 16:28:23 UTC (rev 11661) @@ -79,11 +79,6 @@ throw new SSKDecodeException("Data length: "+dataLength+" but data.length="+data.length); } - if(dataLength != data.length) { - byte[] realDataOutput = new byte[dataLength]; - System.arraycopy(dataOutput, 0, realDataOutput, 0, dataLength); - dataOutput = realDataOutput; - } compressionAlgorithm = (short)(((decryptedHeaders[DATA_DECRYPT_KEY_LENGTH+2] & 0xff) << 8) + (decryptedHeaders[DATA_DECRYPT_KEY_LENGTH+3] & 0xff)); decoded = true; @@ -91,7 +86,7 @@ return BucketTools.makeImmutableBucket(factory, dataOutput); } - Bucket b = Key.decompress(compressionAlgorithm >= 0, dataOutput, factory, Math.min(MAX_DECOMPRESSED_DATA_LENGTH, maxLength), compressionAlgorithm, true); + Bucket b = Key.decompress(compressionAlgorithm >= 0, dataOutput, dataLength, factory, Math.min(MAX_DECOMPRESSED_DATA_LENGTH, maxLength), compressionAlgorithm, true); return b; } Modified: trunk/freenet/src/freenet/keys/Key.java =================================================================== --- trunk/freenet/src/freenet/keys/Key.java 2007-02-03 16:05:56 UTC (rev 11660) +++ trunk/freenet/src/freenet/keys/Key.java 2007-02-03 16:28:23 UTC (rev 11661) @@ -101,7 +101,7 @@ return this.hash == o.hashCode(); } - static Bucket decompress(boolean isCompressed, byte[] output, BucketFactory bf, int maxLength, short compressionAlgorithm, boolean shortLength) throws CHKDecodeException, IOException { + static Bucket decompress(boolean isCompressed, byte[] output, int outputLength, BucketFactory bf, int maxLength, short compressionAlgorithm, boolean shortLength) throws CHKDecodeException, IOException { if(isCompressed) { if(Logger.shouldLog(Logger.MINOR, Key.class)) Logger.minor(Key.class, "Decompressing "+output.length+" bytes in decode with codec "+compressionAlgorithm); @@ -117,7 +117,7 @@ if(len > maxLength) throw new CHKDecodeException("Invalid precompressed size: "+len); Compressor decompressor = Compressor.getCompressionAlgorithmByMetadataID(compressionAlgorithm); - Bucket inputBucket = new SimpleReadOnlyArrayBucket(output, shortLength?2:4, output.length-(shortLength?2:4)); + Bucket inputBucket = new SimpleReadOnlyArrayBucket(output, shortLength?2:4, outputLength-(shortLength?2:4)); try { return decompressor.decompress(inputBucket, bf, maxLength, -1, null); } catch (CompressionOutputSizeException e) { From toad at freenetproject.org Sat Feb 3 16:31:59 2007 From: toad at freenetproject.org (toad at freenetproject.org) Date: Sat, 3 Feb 2007 16:31:59 +0000 (UTC) Subject: [freenet-cvs] r11662 - in trunk/freenet/src/freenet: keys support/io Message-ID: <20070203163159.BFA619BC1E@emu.freenetproject.org> Author: toad Date: 2007-02-03 16:31:58 +0000 (Sat, 03 Feb 2007) New Revision: 11662 Modified: trunk/freenet/src/freenet/keys/ClientSSKBlock.java trunk/freenet/src/freenet/keys/Key.java trunk/freenet/src/freenet/support/io/BucketTools.java Log: Doh. Need to pass length to makeImmutableBucket. Modified: trunk/freenet/src/freenet/keys/ClientSSKBlock.java =================================================================== --- trunk/freenet/src/freenet/keys/ClientSSKBlock.java 2007-02-03 16:28:23 UTC (rev 11661) +++ trunk/freenet/src/freenet/keys/ClientSSKBlock.java 2007-02-03 16:31:58 UTC (rev 11662) @@ -83,7 +83,7 @@ decoded = true; if(dontDecompress) { - return BucketTools.makeImmutableBucket(factory, dataOutput); + return BucketTools.makeImmutableBucket(factory, dataOutput, dataLength); } Bucket b = Key.decompress(compressionAlgorithm >= 0, dataOutput, dataLength, factory, Math.min(MAX_DECOMPRESSED_DATA_LENGTH, maxLength), compressionAlgorithm, true); Modified: trunk/freenet/src/freenet/keys/Key.java =================================================================== --- trunk/freenet/src/freenet/keys/Key.java 2007-02-03 16:28:23 UTC (rev 11661) +++ trunk/freenet/src/freenet/keys/Key.java 2007-02-03 16:31:58 UTC (rev 11662) @@ -124,7 +124,7 @@ throw new CHKDecodeException("Too big"); } } else { - return BucketTools.makeImmutableBucket(bf, output); + return BucketTools.makeImmutableBucket(bf, output, outputLength); } } Modified: trunk/freenet/src/freenet/support/io/BucketTools.java =================================================================== --- trunk/freenet/src/freenet/support/io/BucketTools.java 2007-02-03 16:28:23 UTC (rev 11661) +++ trunk/freenet/src/freenet/support/io/BucketTools.java 2007-02-03 16:31:58 UTC (rev 11662) @@ -254,9 +254,13 @@ } public static Bucket makeImmutableBucket(BucketFactory bucketFactory, byte[] data) throws IOException { - Bucket bucket = bucketFactory.makeBucket(data.length); + return makeImmutableBucket(bucketFactory, data, data.length); + } + + public static Bucket makeImmutableBucket(BucketFactory bucketFactory, byte[] data, int length) throws IOException { + Bucket bucket = bucketFactory.makeBucket(length); OutputStream os = bucket.getOutputStream(); - os.write(data); + os.write(data, 0, length); os.close(); bucket.setReadOnly(); return bucket; From toad at freenetproject.org Sat Feb 3 18:45:09 2007 From: toad at freenetproject.org (toad at freenetproject.org) Date: Sat, 3 Feb 2007 18:45:09 +0000 (UTC) Subject: [freenet-cvs] r11664 - trunk/freenet/src/freenet/node Message-ID: <20070203184509.5F8D220AFA1@emu.freenetproject.org> Author: toad Date: 2007-02-03 18:45:08 +0000 (Sat, 03 Feb 2007) New Revision: 11664 Modified: trunk/freenet/src/freenet/node/PeerNode.java Log: Fix deadlock Modified: trunk/freenet/src/freenet/node/PeerNode.java =================================================================== --- trunk/freenet/src/freenet/node/PeerNode.java 2007-02-03 17:00:40 UTC (rev 11663) +++ trunk/freenet/src/freenet/node/PeerNode.java 2007-02-03 18:45:08 UTC (rev 11664) @@ -1326,8 +1326,10 @@ /** * @return short version of toString() + * *** Note that this is not synchronized! It is used by logging in code paths that + * will deadlock if it is synchronized! *** */ - public synchronized String shortToString() { + public String shortToString() { return super.toString()+ '@' +detectedPeer+ '@' +HexUtil.bytesToHex(identity); } From toad at freenetproject.org Sat Feb 3 18:47:56 2007 From: toad at freenetproject.org (toad at freenetproject.org) Date: Sat, 3 Feb 2007 18:47:56 +0000 (UTC) Subject: [freenet-cvs] r11665 - trunk/freenet/src/freenet/node Message-ID: <20070203184756.C83C19BB7C@emu.freenetproject.org> Author: toad Date: 2007-02-03 18:47:54 +0000 (Sat, 03 Feb 2007) New Revision: 11665 Modified: trunk/freenet/src/freenet/node/PeerNode.java Log: Another locking fix Modified: trunk/freenet/src/freenet/node/PeerNode.java =================================================================== --- trunk/freenet/src/freenet/node/PeerNode.java 2007-02-03 18:45:08 UTC (rev 11664) +++ trunk/freenet/src/freenet/node/PeerNode.java 2007-02-03 18:47:54 UTC (rev 11665) @@ -1574,12 +1574,11 @@ */ public void verified(KeyTracker tracker) { long now = System.currentTimeMillis(); + KeyTracker completelyDeprecatedTracker; synchronized(this) { if(tracker == unverifiedTracker) { if(logMINOR) Logger.minor(this, "Promoting unverified tracker "+tracker+" for "+getPeer()); - if(previousTracker != null) { - previousTracker.completelyDeprecated(tracker); - } + completelyDeprecatedTracker = previousTracker; previousTracker = currentTracker; if(previousTracker != null) previousTracker.deprecated(); @@ -1594,6 +1593,9 @@ } setPeerNodeStatus(now); node.peers.addConnectedPeer(this); + if(completelyDeprecatedTracker != null) { + completelyDeprecatedTracker.completelyDeprecated(tracker); + } } private synchronized boolean invalidVersion() { From toad at freenetproject.org Sat Feb 3 17:00:42 2007 From: toad at freenetproject.org (toad at freenetproject.org) Date: Sat, 3 Feb 2007 17:00:42 +0000 (UTC) Subject: [freenet-cvs] r11663 - in trunk/freenet/src/freenet: crypt crypt/ciphers keys node support/io Message-ID: <20070203170043.1B39C9BB46@emu.freenetproject.org> Author: toad Date: 2007-02-03 17:00:40 +0000 (Sat, 03 Feb 2007) New Revision: 11663 Added: trunk/freenet/src/freenet/crypt/RijndaelPCFBMode.java Removed: trunk/freenet/src/freenet/crypt/ciphers/Twofish.java trunk/freenet/src/freenet/crypt/ciphers/Twofish_Algorithm.java trunk/freenet/src/freenet/crypt/ciphers/Twofish_Properties.java Modified: trunk/freenet/src/freenet/crypt/PCFBMode.java trunk/freenet/src/freenet/crypt/Util.java trunk/freenet/src/freenet/crypt/ciphers/Rijndael.java trunk/freenet/src/freenet/crypt/ciphers/Rijndael_Algorithm.java trunk/freenet/src/freenet/keys/ClientCHKBlock.java trunk/freenet/src/freenet/keys/ClientSSKBlock.java trunk/freenet/src/freenet/keys/InsertableClientSSK.java trunk/freenet/src/freenet/node/FNPPacketMangler.java trunk/freenet/src/freenet/support/io/PaddedEphemerallyEncryptedBucket.java Log: Prevent two int[] allocations per block encryption. PEER REVIEW WOULD BE ESPECIALLY WELCOME FOR THIS PATCH AS IT AFFECTS CRYPTO !!! Modified: trunk/freenet/src/freenet/crypt/PCFBMode.java =================================================================== --- trunk/freenet/src/freenet/crypt/PCFBMode.java 2007-02-03 16:31:58 UTC (rev 11662) +++ trunk/freenet/src/freenet/crypt/PCFBMode.java 2007-02-03 17:00:40 UTC (rev 11663) @@ -5,6 +5,8 @@ import java.io.*; +import freenet.crypt.ciphers.Rijndael; + /** * Control mechanism for the Periodic Cipher Feed Back mode. This is * a CFB variant used apparently by a number of programs, including PGP. @@ -14,17 +16,29 @@ */ public class PCFBMode { - private BlockCipher c; - private byte[] feedback_register; - private int registerPointer; + protected BlockCipher c; + protected byte[] feedback_register; + protected int registerPointer; - public PCFBMode(BlockCipher c) { + public static PCFBMode create(BlockCipher c) { + if(c instanceof Rijndael) + return new RijndaelPCFBMode((Rijndael)c); + return new PCFBMode(c); + } + + public static PCFBMode create(BlockCipher c, byte[] iv) { + if(c instanceof Rijndael) + return new RijndaelPCFBMode((Rijndael)c, iv); + return new PCFBMode(c, iv); + } + + PCFBMode(BlockCipher c) { this.c = c; feedback_register = new byte[c.getBlockSize() >> 3]; registerPointer = feedback_register.length; } - public PCFBMode(BlockCipher c, byte[] iv) { + PCFBMode(BlockCipher c, byte[] iv) { this(c); System.arraycopy(iv, 0, feedback_register, 0, feedback_register.length); } @@ -149,6 +163,3 @@ registerPointer=0; } } - - - Added: trunk/freenet/src/freenet/crypt/RijndaelPCFBMode.java =================================================================== --- trunk/freenet/src/freenet/crypt/RijndaelPCFBMode.java (rev 0) +++ trunk/freenet/src/freenet/crypt/RijndaelPCFBMode.java 2007-02-03 17:00:40 UTC (rev 11663) @@ -0,0 +1,38 @@ +package freenet.crypt; + +import freenet.crypt.ciphers.Rijndael; + +/** + * Optimised PCFBMode for Rijndael. + * All this actually does is avoid two new int[4]'s per cycle. + */ +public final class RijndaelPCFBMode extends PCFBMode { + + /** Temporary variables to remove allocations from inner crypto loop. These are wiped + * by the encrypt function. */ + private final int[] a, t; + + // Refills the encrypted buffer with data. + //private synchronized void refillBuffer() { + protected void refillBuffer() { + // Encrypt feedback into result + ((Rijndael)c).encipher(feedback_register, feedback_register, a, t); + + registerPointer=0; + } + + public RijndaelPCFBMode(Rijndael c) { + super(c); + int tempSize = c.getTempArraySize(); + a = new int[tempSize]; + t = new int[tempSize]; + } + + public RijndaelPCFBMode(Rijndael c, byte[] iv) { + super(c); + int tempSize = c.getTempArraySize(); + a = new int[tempSize]; + t = new int[tempSize]; + } + +} Modified: trunk/freenet/src/freenet/crypt/Util.java =================================================================== --- trunk/freenet/src/freenet/crypt/Util.java 2007-02-03 16:31:58 UTC (rev 11662) +++ trunk/freenet/src/freenet/crypt/Util.java 2007-02-03 17:00:40 UTC (rev 11663) @@ -12,7 +12,6 @@ import java.util.Random; import freenet.crypt.ciphers.Rijndael; -import freenet.crypt.ciphers.Twofish; import freenet.support.HexUtil; import freenet.support.Loader; import net.i2p.util.NativeBigInteger; @@ -24,7 +23,6 @@ static { SHA1.class.toString(); JavaSHA1.class.toString(); - Twofish.class.toString(); Rijndael.class.toString(); } Modified: trunk/freenet/src/freenet/crypt/ciphers/Rijndael.java =================================================================== --- trunk/freenet/src/freenet/crypt/ciphers/Rijndael.java 2007-02-03 16:31:58 UTC (rev 11662) +++ trunk/freenet/src/freenet/crypt/ciphers/Rijndael.java 2007-02-03 17:00:40 UTC (rev 11663) @@ -92,6 +92,22 @@ Rijndael_Algorithm.blockEncrypt(block, result, 0, sessionKey, cryptBlockSize/8); } + /** + * @return Size of temporary int[] a, t. If these are passed in, this can speed + * things up by avoiding unnecessary allocations between rounds. + */ + public synchronized final int getTempArraySize() { + return cryptBlockSize/(8*4); + } + + public synchronized final void encipher(byte[] block, byte[] result, int[] a, int[] t) { + if(block.length != blocksize/8) + throw new IllegalArgumentException(); + if(a.length != t.length || t.length != cryptBlockSize/(8*4)) + throw new IllegalArgumentException(); + Rijndael_Algorithm.blockEncrypt(block, result, 0, sessionKey, cryptBlockSize/8, a, t); + } + public synchronized final void decipher(byte[] block, byte[] result) { if(block.length != blocksize/8) throw new IllegalArgumentException(); Modified: trunk/freenet/src/freenet/crypt/ciphers/Rijndael_Algorithm.java =================================================================== --- trunk/freenet/src/freenet/crypt/ciphers/Rijndael_Algorithm.java 2007-02-03 16:31:58 UTC (rev 11662) +++ trunk/freenet/src/freenet/crypt/ciphers/Rijndael_Algorithm.java 2007-02-03 17:00:40 UTC (rev 11663) @@ -611,6 +611,30 @@ blockEncrypt(in, result, inOffset, sessionKey); return; } + + int BC = blockSize / 4; + + int[] a = new int[BC]; + int[] t = new int[BC]; // temporary work array + + blockEncrypt(in, result, inOffset, sessionKey, blockSize, a, t); + } + + /** + * Encrypt exactly one block of plaintext. + * + * @param in The plaintext. + * @param result The buffer into which to write the resulting ciphertext. + * @param inOffset Index of in from which to start considering data. + * @param sessionKey The session key to use for encryption. + * @param blockSize The block size in bytes of this Rijndael. + */ + public static final void + blockEncrypt (byte[] in, byte[] result, int inOffset, Object sessionKey, int blockSize, int[] a, int[] t) { + if (blockSize == BLOCK_SIZE) { + blockEncrypt(in, result, inOffset, sessionKey); + return; + } if (RDEBUG) trace(IN, "blockEncrypt("+in+", "+inOffset+", "+sessionKey+", "+blockSize+ ')'); Object[] sKey = (Object[]) sessionKey; // extract encryption round keys int[][] Ke = (int[][]) sKey[0]; @@ -621,8 +645,6 @@ int s1 = shifts[SC][1][0]; int s2 = shifts[SC][2][0]; int s3 = shifts[SC][3][0]; - int[] a = new int[BC]; - int[] t = new int[BC]; // temporary work array int i; int j = 0, tt; Deleted: trunk/freenet/src/freenet/crypt/ciphers/Twofish.java =================================================================== --- trunk/freenet/src/freenet/crypt/ciphers/Twofish.java 2007-02-03 16:31:58 UTC (rev 11662) +++ trunk/freenet/src/freenet/crypt/ciphers/Twofish.java 2007-02-03 17:00:40 UTC (rev 11663) @@ -1,66 +0,0 @@ -package freenet.crypt.ciphers; - -import freenet.crypt.BlockCipher; -import freenet.crypt.UnsupportedCipherException; -import freenet.support.Logger; -import java.security.InvalidKeyException; - -/* - This code is part of the Java Adaptive Network Client by Ian Clarke. - It is distributed under the GNU Public Licence (GPL) version 2. See - http://www.gnu.org/ for further details of the GPL. -*/ - -/** - * Interfaces with the Twofish AES candidate to implement the Twofish - * algorithm - */ -public final class Twofish implements BlockCipher { - private Object sessionKey; - private int keysize; - - // for Util.getCipherByName.. and yes, screw you too, java - public Twofish(Integer keysize) throws UnsupportedCipherException { - this(keysize.intValue()); - } - - public Twofish(int keysize) throws UnsupportedCipherException { - if (! ((keysize == 64) || - (keysize == 128) || - (keysize == 192) || - (keysize == 256))) - throw new UnsupportedCipherException("Invalid keysize"); - this.keysize=keysize; - } - - public Twofish() { - this.keysize = 128; - } - - public final int getBlockSize() { - return 128; - } - - public final int getKeySize() { - return keysize; - } - - public final void initialize(byte[] key) { - try { - byte[] nkey=new byte[keysize>>3]; - System.arraycopy(key, 0, nkey, 0, nkey.length); - sessionKey=Twofish_Algorithm.makeKey(nkey); - } catch (InvalidKeyException e) { - e.printStackTrace(); - Logger.error(this,"Invalid key"); - } - } - - public final void encipher(byte[] block, byte[] result) { - Twofish_Algorithm.blockEncrypt(block, result, 0, sessionKey); - } - - public final void decipher(byte[] block, byte[] result) { - Twofish_Algorithm.blockDecrypt(block, result, 0, sessionKey); - } -} Deleted: trunk/freenet/src/freenet/crypt/ciphers/Twofish_Algorithm.java =================================================================== --- trunk/freenet/src/freenet/crypt/ciphers/Twofish_Algorithm.java 2007-02-03 16:31:58 UTC (rev 11662) +++ trunk/freenet/src/freenet/crypt/ciphers/Twofish_Algorithm.java 2007-02-03 17:00:40 UTC (rev 11663) @@ -1,839 +0,0 @@ -/* - * Copyright (c) 1997, 1998 Systemics Ltd on behalf of - * the Cryptix Development Team. All rights reserved. - */ -package freenet.crypt.ciphers; - -import java.io.PrintWriter; -import java.security.InvalidKeyException; - -//........................................................................... -/** - * Twofish is an AES candidate algorithm. It is a balanced 128-bit Feistel - * cipher, consisting of 16 rounds. In each round, a 64-bit S-box value is - * computed from 64 bits of the block, and this value is xored into the other - * half of the block. The two half-blocks are then exchanged, and the next - * round begins. Before the first round, all input bits are xored with key- - * dependent "whitening" subkeys, and after the final round the output bits - * are xored with other key-dependent whitening subkeys; these subkeys are - * not used anywhere else in the algorithm.

- * - * Twofish was submitted by Bruce Schneier, Doug Whiting, John Kelsey, Chris - * Hall and David Wagner.

- * - * Reference:

    - *
  1. TWOFISH2.C -- Optimized C API calls for TWOFISH AES submission, - * Version 1.00, April 1998, by Doug Whiting.

- * - * Copyright © 1998 - * Systemics Ltd on behalf of the - * Cryptix Development Team. - *
All rights reserved.

- * - * @author Raif S. Naffah - */ -public final class Twofish_Algorithm // implicit no-argument constructor -{ -// Debugging methods and variables -//........................................................................... - - static final String NAME = "Twofish_Algorithm"; - static final boolean IN = true, OUT = false; - - static final boolean TDEBUG = Twofish_Properties.GLOBAL_DEBUG; - static final int debuglevel = TDEBUG ? Twofish_Properties.getLevel(NAME) : 0; -// static final PrintWriter err = TDEBUG ? Twofish_Properties.getOutput() : null; - static final PrintWriter err = null; - - static final boolean TRACE = Twofish_Properties.isTraceable(NAME); - - static void debug (String s) { err.println(">>> "+NAME+": "+s); } - static void trace (boolean in, String s) { - if (TRACE) err.println((in?"==> ":"<== ")+NAME+ '.' +s); - } - static void trace (String s) { if (TRACE) err.println("<=> "+NAME+ '.' +s); } - - -// Constants and variables -//........................................................................... - - static final int BLOCK_SIZE = 16; // bytes in a data-block - private static final int ROUNDS = 16; - - /* Subkey array indices */ - private static final int INPUT_WHITEN = 0; - private static final int OUTPUT_WHITEN = INPUT_WHITEN + BLOCK_SIZE/4; - private static final int ROUND_SUBKEYS = OUTPUT_WHITEN + BLOCK_SIZE/4; // 2*(# rounds) - - private static final int SK_STEP = 0x02020202; - private static final int SK_BUMP = 0x01010101; - private static final int SK_ROTL = 9; - - /** Fixed 8x8 permutation S-boxes */ - private static final byte[][] P = new byte[][] { - { // p0 - (byte) 0xA9, (byte) 0x67, (byte) 0xB3, (byte) 0xE8, - (byte) 0x04, (byte) 0xFD, (byte) 0xA3, (byte) 0x76, - (byte) 0x9A, (byte) 0x92, (byte) 0x80, (byte) 0x78, - (byte) 0xE4, (byte) 0xDD, (byte) 0xD1, (byte) 0x38, - (byte) 0x0D, (byte) 0xC6, (byte) 0x35, (byte) 0x98, - (byte) 0x18, (byte) 0xF7, (byte) 0xEC, (byte) 0x6C, - (byte) 0x43, (byte) 0x75, (byte) 0x37, (byte) 0x26, - (byte) 0xFA, (byte) 0x13, (byte) 0x94, (byte) 0x48, - (byte) 0xF2, (byte) 0xD0, (byte) 0x8B, (byte) 0x30, - (byte) 0x84, (byte) 0x54, (byte) 0xDF, (byte) 0x23, - (byte) 0x19, (byte) 0x5B, (byte) 0x3D, (byte) 0x59, - (byte) 0xF3, (byte) 0xAE, (byte) 0xA2, (byte) 0x82, - (byte) 0x63, (byte) 0x01, (byte) 0x83, (byte) 0x2E, - (byte) 0xD9, (byte) 0x51, (byte) 0x9B, (byte) 0x7C, - (byte) 0xA6, (byte) 0xEB, (byte) 0xA5, (byte) 0xBE, - (byte) 0x16, (byte) 0x0C, (byte) 0xE3, (byte) 0x61, - (byte) 0xC0, (byte) 0x8C, (byte) 0x3A, (byte) 0xF5, - (byte) 0x73, (byte) 0x2C, (byte) 0x25, (byte) 0x0B, - (byte) 0xBB, (byte) 0x4E, (byte) 0x89, (byte) 0x6B, - (byte) 0x53, (byte) 0x6A, (byte) 0xB4, (byte) 0xF1, - (byte) 0xE1, (byte) 0xE6, (byte) 0xBD, (byte) 0x45, - (byte) 0xE2, (byte) 0xF4, (byte) 0xB6, (byte) 0x66, - (byte) 0xCC, (byte) 0x95, (byte) 0x03, (byte) 0x56, - (byte) 0xD4, (byte) 0x1C, (byte) 0x1E, (byte) 0xD7, - (byte) 0xFB, (byte) 0xC3, (byte) 0x8E, (byte) 0xB5, - (byte) 0xE9, (byte) 0xCF, (byte) 0xBF, (byte) 0xBA, - (byte) 0xEA, (byte) 0x77, (byte) 0x39, (byte) 0xAF, - (byte) 0x33, (byte) 0xC9, (byte) 0x62, (byte) 0x71, - (byte) 0x81, (byte) 0x79, (byte) 0x09, (byte) 0xAD, - (byte) 0x24, (byte) 0xCD, (byte) 0xF9, (byte) 0xD8, - (byte) 0xE5, (byte) 0xC5, (byte) 0xB9, (byte) 0x4D, - (byte) 0x44, (byte) 0x08, (byte) 0x86, (byte) 0xE7, - (byte) 0xA1, (byte) 0x1D, (byte) 0xAA, (byte) 0xED, - (byte) 0x06, (byte) 0x70, (byte) 0xB2, (byte) 0xD2, - (byte) 0x41, (byte) 0x7B, (byte) 0xA0, (byte) 0x11, - (byte) 0x31, (byte) 0xC2, (byte) 0x27, (byte) 0x90, - (byte) 0x20, (byte) 0xF6, (byte) 0x60, (byte) 0xFF, - (byte) 0x96, (byte) 0x5C, (byte) 0xB1, (byte) 0xAB, - (byte) 0x9E, (byte) 0x9C, (byte) 0x52, (byte) 0x1B, - (byte) 0x5F, (byte) 0x93, (byte) 0x0A, (byte) 0xEF, - (byte) 0x91, (byte) 0x85, (byte) 0x49, (byte) 0xEE, - (byte) 0x2D, (byte) 0x4F, (byte) 0x8F, (byte) 0x3B, - (byte) 0x47, (byte) 0x87, (byte) 0x6D, (byte) 0x46, - (byte) 0xD6, (byte) 0x3E, (byte) 0x69, (byte) 0x64, - (byte) 0x2A, (byte) 0xCE, (byte) 0xCB, (byte) 0x2F, - (byte) 0xFC, (byte) 0x97, (byte) 0x05, (byte) 0x7A, - (byte) 0xAC, (byte) 0x7F, (byte) 0xD5, (byte) 0x1A, - (byte) 0x4B, (byte) 0x0E, (byte) 0xA7, (byte) 0x5A, - (byte) 0x28, (byte) 0x14, (byte) 0x3F, (byte) 0x29, - (byte) 0x88, (byte) 0x3C, (byte) 0x4C, (byte) 0x02, - (byte) 0xB8, (byte) 0xDA, (byte) 0xB0, (byte) 0x17, - (byte) 0x55, (byte) 0x1F, (byte) 0x8A, (byte) 0x7D, - (byte) 0x57, (byte) 0xC7, (byte) 0x8D, (byte) 0x74, - (byte) 0xB7, (byte) 0xC4, (byte) 0x9F, (byte) 0x72, - (byte) 0x7E, (byte) 0x15, (byte) 0x22, (byte) 0x12, - (byte) 0x58, (byte) 0x07, (byte) 0x99, (byte) 0x34, - (byte) 0x6E, (byte) 0x50, (byte) 0xDE, (byte) 0x68, - (byte) 0x65, (byte) 0xBC, (byte) 0xDB, (byte) 0xF8, - (byte) 0xC8, (byte) 0xA8, (byte) 0x2B, (byte) 0x40, - (byte) 0xDC, (byte) 0xFE, (byte) 0x32, (byte) 0xA4, - (byte) 0xCA, (byte) 0x10, (byte) 0x21, (byte) 0xF0, - (byte) 0xD3, (byte) 0x5D, (byte) 0x0F, (byte) 0x00, - (byte) 0x6F, (byte) 0x9D, (byte) 0x36, (byte) 0x42, - (byte) 0x4A, (byte) 0x5E, (byte) 0xC1, (byte) 0xE0 - }, - { // p1 - (byte) 0x75, (byte) 0xF3, (byte) 0xC6, (byte) 0xF4, - (byte) 0xDB, (byte) 0x7B, (byte) 0xFB, (byte) 0xC8, - (byte) 0x4A, (byte) 0xD3, (byte) 0xE6, (byte) 0x6B, - (byte) 0x45, (byte) 0x7D, (byte) 0xE8, (byte) 0x4B, - (byte) 0xD6, (byte) 0x32, (byte) 0xD8, (byte) 0xFD, - (byte) 0x37, (byte) 0x71, (byte) 0xF1, (byte) 0xE1, - (byte) 0x30, (byte) 0x0F, (byte) 0xF8, (byte) 0x1B, - (byte) 0x87, (byte) 0xFA, (byte) 0x06, (byte) 0x3F, - (byte) 0x5E, (byte) 0xBA, (byte) 0xAE, (byte) 0x5B, - (byte) 0x8A, (byte) 0x00, (byte) 0xBC, (byte) 0x9D, - (byte) 0x6D, (byte) 0xC1, (byte) 0xB1, (byte) 0x0E, - (byte) 0x80, (byte) 0x5D, (byte) 0xD2, (byte) 0xD5, - (byte) 0xA0, (byte) 0x84, (byte) 0x07, (byte) 0x14, - (byte) 0xB5, (byte) 0x90, (byte) 0x2C, (byte) 0xA3, - (byte) 0xB2, (byte) 0x73, (byte) 0x4C, (byte) 0x54, - (byte) 0x92, (byte) 0x74, (byte) 0x36, (byte) 0x51, - (byte) 0x38, (byte) 0xB0, (byte) 0xBD, (byte) 0x5A, - (byte) 0xFC, (byte) 0x60, (byte) 0x62, (byte) 0x96, - (byte) 0x6C, (byte) 0x42, (byte) 0xF7, (byte) 0x10, - (byte) 0x7C, (byte) 0x28, (byte) 0x27, (byte) 0x8C, - (byte) 0x13, (byte) 0x95, (byte) 0x9C, (byte) 0xC7, - (byte) 0x24, (byte) 0x46, (byte) 0x3B, (byte) 0x70, - (byte) 0xCA, (byte) 0xE3, (byte) 0x85, (byte) 0xCB, - (byte) 0x11, (byte) 0xD0, (byte) 0x93, (byte) 0xB8, - (byte) 0xA6, (byte) 0x83, (byte) 0x20, (byte) 0xFF, - (byte) 0x9F, (byte) 0x77, (byte) 0xC3, (byte) 0xCC, - (byte) 0x03, (byte) 0x6F, (byte) 0x08, (byte) 0xBF, - (byte) 0x40, (byte) 0xE7, (byte) 0x2B, (byte) 0xE2, - (byte) 0x79, (byte) 0x0C, (byte) 0xAA, (byte) 0x82, - (byte) 0x41, (byte) 0x3A, (byte) 0xEA, (byte) 0xB9, - (byte) 0xE4, (byte) 0x9A, (byte) 0xA4, (byte) 0x97, - (byte) 0x7E, (byte) 0xDA, (byte) 0x7A, (byte) 0x17, - (byte) 0x66, (byte) 0x94, (byte) 0xA1, (byte) 0x1D, - (byte) 0x3D, (byte) 0xF0, (byte) 0xDE, (byte) 0xB3, - (byte) 0x0B, (byte) 0x72, (byte) 0xA7, (byte) 0x1C, - (byte) 0xEF, (byte) 0xD1, (byte) 0x53, (byte) 0x3E, - (byte) 0x8F, (byte) 0x33, (byte) 0x26, (byte) 0x5F, - (byte) 0xEC, (byte) 0x76, (byte) 0x2A, (byte) 0x49, - (byte) 0x81, (byte) 0x88, (byte) 0xEE, (byte) 0x21, - (byte) 0xC4, (byte) 0x1A, (byte) 0xEB, (byte) 0xD9, - (byte) 0xC5, (byte) 0x39, (byte) 0x99, (byte) 0xCD, - (byte) 0xAD, (byte) 0x31, (byte) 0x8B, (byte) 0x01, - (byte) 0x18, (byte) 0x23, (byte) 0xDD, (byte) 0x1F, - (byte) 0x4E, (byte) 0x2D, (byte) 0xF9, (byte) 0x48, - (byte) 0x4F, (byte) 0xF2, (byte) 0x65, (byte) 0x8E, - (byte) 0x78, (byte) 0x5C, (byte) 0x58, (byte) 0x19, - (byte) 0x8D, (byte) 0xE5, (byte) 0x98, (byte) 0x57, - (byte) 0x67, (byte) 0x7F, (byte) 0x05, (byte) 0x64, - (byte) 0xAF, (byte) 0x63, (byte) 0xB6, (byte) 0xFE, - (byte) 0xF5, (byte) 0xB7, (byte) 0x3C, (byte) 0xA5, - (byte) 0xCE, (byte) 0xE9, (byte) 0x68, (byte) 0x44, - (byte) 0xE0, (byte) 0x4D, (byte) 0x43, (byte) 0x69, - (byte) 0x29, (byte) 0x2E, (byte) 0xAC, (byte) 0x15, - (byte) 0x59, (byte) 0xA8, (byte) 0x0A, (byte) 0x9E, - (byte) 0x6E, (byte) 0x47, (byte) 0xDF, (byte) 0x34, - (byte) 0x35, (byte) 0x6A, (byte) 0xCF, (byte) 0xDC, - (byte) 0x22, (byte) 0xC9, (byte) 0xC0, (byte) 0x9B, - (byte) 0x89, (byte) 0xD4, (byte) 0xED, (byte) 0xAB, - (byte) 0x12, (byte) 0xA2, (byte) 0x0D, (byte) 0x52, - (byte) 0xBB, (byte) 0x02, (byte) 0x2F, (byte) 0xA9, - (byte) 0xD7, (byte) 0x61, (byte) 0x1E, (byte) 0xB4, - (byte) 0x50, (byte) 0x04, (byte) 0xF6, (byte) 0xC2, - (byte) 0x16, (byte) 0x25, (byte) 0x86, (byte) 0x56, - (byte) 0x55, (byte) 0x09, (byte) 0xBE, (byte) 0x91 - } - }; - - /** - * Define the fixed p0/p1 permutations used in keyed S-box lookup. - * By changing the following constant definitions, the S-boxes will - * automatically get changed in the Twofish engine. - */ - private static final int P_00 = 1; - private static final int P_01 = 0; - private static final int P_02 = 0; - private static final int P_03 = P_01 ^ 1; - private static final int P_04 = 1; - - private static final int P_10 = 0; - private static final int P_11 = 0; - private static final int P_12 = 1; - private static final int P_13 = P_11 ^ 1; - private static final int P_14 = 0; - - private static final int P_20 = 1; - private static final int P_21 = 1; - private static final int P_22 = 0; - private static final int P_23 = P_21 ^ 1; - private static final int P_24 = 0; - - private static final int P_30 = 0; - private static final int P_31 = 1; - private static final int P_32 = 1; - private static final int P_33 = P_31 ^ 1; - private static final int P_34 = 1; - - /** Primitive polynomial for GF(256) */ - private static final int GF256_FDBK_2 = 0x169 / 2; - private static final int GF256_FDBK_4 = 0x169 / 4; - - /** MDS matrix */ - private static final int[][] MDS = new int[4][256]; // blank final - - private static final int RS_GF_FDBK = 0x14D; // field generator - - /** data for hexadecimal visualisation. */ - private static final char[] HEX_DIGITS = { - '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' - }; - - -// Static code - to intialise the MDS matrix -//........................................................................... - - static { - long time = System.currentTimeMillis(); - -if (TDEBUG && (debuglevel > 6)) { -System.out.println("Algorithm Name: "+Twofish_Properties.FULL_NAME); -System.out.println("Electronic Codebook (ECB) Mode"); -System.out.println(); -} - // - // precompute the MDS matrix - // - int[] m1 = new int[2]; - int[] mX = new int[2]; - int[] mY = new int[2]; - int i, j; - for (i = 0; i < 256; i++) { - j = P[0][i] & 0xFF; // compute all the matrix elements - m1[0] = j; - mX[0] = Mx_X( j ) & 0xFF; - mY[0] = Mx_Y( j ) & 0xFF; - - j = P[1][i] & 0xFF; - m1[1] = j; - mX[1] = Mx_X( j ) & 0xFF; - mY[1] = Mx_Y( j ) & 0xFF; - - MDS[0][i] = m1[P_00] << 0 | // fill matrix w/ above elements - mX[P_00] << 8 | - mY[P_00] << 16 | - mY[P_00] << 24; - MDS[1][i] = mY[P_10] << 0 | - mY[P_10] << 8 | - mX[P_10] << 16 | - m1[P_10] << 24; - MDS[2][i] = mX[P_20] << 0 | - mY[P_20] << 8 | - m1[P_20] << 16 | - mY[P_20] << 24; - MDS[3][i] = mX[P_30] << 0 | - m1[P_30] << 8 | - mY[P_30] << 16 | - mX[P_30] << 24; - } - - time = System.currentTimeMillis() - time; - -if (TDEBUG && (debuglevel > 8)) { -System.out.println("=========="); -System.out.println(); -System.out.println("Static Data"); -System.out.println(); -System.out.println("MDS[0][]:"); for(i=0;i<64;i++) { for(j=0;j<4;j++) System.out.print("0x"+intToString(MDS[0][i*4+j])+", "); System.out.println();} -System.out.println(); -System.out.println("MDS[1][]:"); for(i=0;i<64;i++) { for(j=0;j<4;j++) System.out.print("0x"+intToString(MDS[1][i*4+j])+", "); System.out.println();} -System.out.println(); -System.out.println("MDS[2][]:"); for(i=0;i<64;i++) { for(j=0;j<4;j++) System.out.print("0x"+intToString(MDS[2][i*4+j])+", "); System.out.println();} -System.out.println(); -System.out.println("MDS[3][]:"); for(i=0;i<64;i++) { for(j=0;j<4;j++) System.out.print("0x"+intToString(MDS[3][i*4+j])+", "); System.out.println();} -System.out.println(); -System.out.println("Total initialization time: "+time+" ms."); -System.out.println(); -} - } - - private static final int LFSR1( int x ) { - return (x >> 1) ^ - ((x & 0x01) != 0 ? GF256_FDBK_2 : 0); - } - - private static final int LFSR2( int x ) { - return (x >> 2) ^ - ((x & 0x02) != 0 ? GF256_FDBK_2 : 0) ^ - ((x & 0x01) != 0 ? GF256_FDBK_4 : 0); - } - - private static final int Mx_X( int x ) { return x ^ LFSR2(x); } // 5B - private static final int Mx_Y( int x ) { return x ^ LFSR1(x) ^ LFSR2(x); } // EF - - -// Basic API methods -//........................................................................... - - /** - * Expand a user-supplied key material into a session key. - * - * @param k The 64/128/192/256-bit user-key to use. - * @return This cipher's round keys. - * @exception InvalidKeyException If the key is invalid. - */ - public static final synchronized Object makeKey (byte[] k) - throws InvalidKeyException { -if (TDEBUG) trace(IN, "makeKey("+k+ ')'); - if (k == null) - throw new InvalidKeyException("Empty key"); - int length = k.length; - if (!((length == 8) || (length == 16) || (length == 24) || (length == 32))) - throw new InvalidKeyException("Incorrect key length"); - -if (TDEBUG && (debuglevel > 7)) { -System.out.println("Intermediate Session Key Values"); -System.out.println(); -System.out.println("Raw="+toString(k)); -System.out.println(); -} - int k64Cnt = length / 8; - int subkeyCnt = ROUND_SUBKEYS + 2*ROUNDS; - int[] k32e = new int[4]; // even 32-bit entities - int[] k32o = new int[4]; // odd 32-bit entities - int[] sBoxKey = new int[4]; - // - // split user key material into even and odd 32-bit entities and - // compute S-box keys using (12, 8) Reed-Solomon code over GF(256) - // - int i, j, offset = 0; - for (i = 0, j = k64Cnt-1; (i < 4) && (offset < length); i++, j--) { - k32e[i] = (k[offset++] & 0xFF) | - (k[offset++] & 0xFF) << 8 | - (k[offset++] & 0xFF) << 16 | - (k[offset++] & 0xFF) << 24; - k32o[i] = (k[offset++] & 0xFF) | - (k[offset++] & 0xFF) << 8 | - (k[offset++] & 0xFF) << 16 | - (k[offset++] & 0xFF) << 24; - sBoxKey[j] = RS_MDS_Encode( k32e[i], k32o[i] ); // reverse order - } - // compute the round decryption subkeys for PHT. these same subkeys - // will be used in encryption but will be applied in reverse order. - int q, A, B; - int[] subKeys = new int[subkeyCnt]; - for (i = q = 0; i < subkeyCnt/2; i++, q += SK_STEP) { - A = F32( k64Cnt, q , k32e ); // A uses even key entities - B = F32( k64Cnt, q+SK_BUMP, k32o ); // B uses odd key entities - B = B << 8 | B >>> 24; - A += B; - subKeys[2*i ] = A; // combine with a PHT - A += B; - subKeys[2*i + 1] = A << SK_ROTL | A >>> (32-SK_ROTL); - } - // - // fully expand the table for speed - // - int k0 = sBoxKey[0]; - int k1 = sBoxKey[1]; - int k2 = sBoxKey[2]; - int k3 = sBoxKey[3]; - int b0, b1, b2, b3; - int[] sBox = new int[4 * 256]; - for (i = 0; i < 256; i++) { - b0 = b1 = b2 = b3 = i; - switch (k64Cnt & 3) { - case 1: - sBox[ 2*i ] = MDS[0][(P[P_01][b0] & 0xFF) ^ b0(k0)]; - sBox[ 2*i+1] = MDS[1][(P[P_11][b1] & 0xFF) ^ b1(k0)]; - sBox[0x200+2*i ] = MDS[2][(P[P_21][b2] & 0xFF) ^ b2(k0)]; - sBox[0x200+2*i+1] = MDS[3][(P[P_31][b3] & 0xFF) ^ b3(k0)]; - break; - case 0: // same as 4 - b0 = (P[P_04][b0] & 0xFF) ^ b0(k3); - b1 = (P[P_14][b1] & 0xFF) ^ b1(k3); - b2 = (P[P_24][b2] & 0xFF) ^ b2(k3); - b3 = (P[P_34][b3] & 0xFF) ^ b3(k3); - case 3: - b0 = (P[P_03][b0] & 0xFF) ^ b0(k2); - b1 = (P[P_13][b1] & 0xFF) ^ b1(k2); - b2 = (P[P_23][b2] & 0xFF) ^ b2(k2); - b3 = (P[P_33][b3] & 0xFF) ^ b3(k2); - case 2: // 128-bit keys - sBox[ 2*i ] = MDS[0][(P[P_01][(P[P_02][b0] & 0xFF) ^ b0(k1)] & 0xFF) ^ b0(k0)]; - sBox[ 2*i+1] = MDS[1][(P[P_11][(P[P_12][b1] & 0xFF) ^ b1(k1)] & 0xFF) ^ b1(k0)]; - sBox[0x200+2*i ] = MDS[2][(P[P_21][(P[P_22][b2] & 0xFF) ^ b2(k1)] & 0xFF) ^ b2(k0)]; - sBox[0x200+2*i+1] = MDS[3][(P[P_31][(P[P_32][b3] & 0xFF) ^ b3(k1)] & 0xFF) ^ b3(k0)]; - } - } - - Object sessionKey = new Object[] { sBox, subKeys }; - -if (TDEBUG && (debuglevel > 7)) { -System.out.println("S-box[]:"); -for(i=0;i<64;i++) { for(j=0;j<4;j++) System.out.print("0x"+intToString(sBox[i*4+j])+", "); System.out.println();} -System.out.println(); -for(i=0;i<64;i++) { for(j=0;j<4;j++) System.out.print("0x"+intToString(sBox[256+i*4+j])+", "); System.out.println();} -System.out.println(); -for(i=0;i<64;i++) { for(j=0;j<4;j++) System.out.print("0x"+intToString(sBox[512+i*4+j])+", "); System.out.println();} -System.out.println(); -for(i=0;i<64;i++) { for(j=0;j<4;j++) System.out.print("0x"+intToString(sBox[768+i*4+j])+", "); System.out.println();} -System.out.println(); -System.out.println("User (odd, even) keys --> S-Box keys:"); -for(i=0;i 0x"+intToString(sBoxKey[k64Cnt-1-i])); } -System.out.println(); -System.out.println("Round keys:"); -for(i=0;i 6)) System.out.println("PT="+toString(in, inOffset, BLOCK_SIZE)); - - int x0 = (in[inOffset++] & 0xFF) | - (in[inOffset++] & 0xFF) << 8 | - (in[inOffset++] & 0xFF) << 16 | - (in[inOffset++] & 0xFF) << 24; - int x1 = (in[inOffset++] & 0xFF) | - (in[inOffset++] & 0xFF) << 8 | - (in[inOffset++] & 0xFF) << 16 | - (in[inOffset++] & 0xFF) << 24; - int x2 = (in[inOffset++] & 0xFF) | - (in[inOffset++] & 0xFF) << 8 | - (in[inOffset++] & 0xFF) << 16 | - (in[inOffset++] & 0xFF) << 24; - int x3 = (in[inOffset++] & 0xFF) | - (in[inOffset++] & 0xFF) << 8 | - (in[inOffset++] & 0xFF) << 16 | - (in[inOffset++] & 0xFF) << 24; - - x0 ^= sKey[INPUT_WHITEN ]; - x1 ^= sKey[INPUT_WHITEN + 1]; - x2 ^= sKey[INPUT_WHITEN + 2]; - x3 ^= sKey[INPUT_WHITEN + 3]; -if (TDEBUG && (debuglevel > 6)) System.out.println("PTw="+intToString(x0)+intToString(x1)+intToString(x2)+intToString(x3)); - - int t0, t1; - int k = ROUND_SUBKEYS; - for (int R = 0; R < ROUNDS; R += 2) { - t0 = Fe32_0( sBox, x0 ); - t1 = Fe32_3( sBox, x1 ); - x2 ^= t0 + t1 + sKey[k++]; - x2 = x2 >>> 1 | x2 << 31; - x3 = x3 << 1 | x3 >>> 31; - x3 ^= t0 + 2*t1 + sKey[k++]; -if (TDEBUG && (debuglevel > 6)) System.out.println("CT"+(R)+ '=' +intToString(x0)+intToString(x1)+intToString(x2)+intToString(x3)); - - t0 = Fe32_0( sBox, x2 ); - t1 = Fe32_3( sBox, x3 ); - x0 ^= t0 + t1 + sKey[k++]; - x0 = x0 >>> 1 | x0 << 31; - x1 = x1 << 1 | x1 >>> 31; - x1 ^= t0 + 2*t1 + sKey[k++]; -if (TDEBUG && (debuglevel > 6)) System.out.println("CT"+(R+1)+ '=' +intToString(x0)+intToString(x1)+intToString(x2)+intToString(x3)); - } - x2 ^= sKey[OUTPUT_WHITEN ]; - x3 ^= sKey[OUTPUT_WHITEN + 1]; - x0 ^= sKey[OUTPUT_WHITEN + 2]; - x1 ^= sKey[OUTPUT_WHITEN + 3]; -if (TDEBUG && (debuglevel > 6)) System.out.println("CTw="+intToString(x0)+intToString(x1)+intToString(x2)+intToString(x3)); - - result[0] = (byte) x2; - result[1] = (byte)(x2 >>> 8); - result[2] = (byte)(x2 >>> 16); - result[3] = (byte)(x2 >>> 24); - result[4] = (byte) x3; - result[5] = (byte)(x3 >>> 8); - result[6] = (byte)(x3 >>> 16); - result[7] = (byte)(x3 >>> 24); - result[8] = (byte) x0; - result[9] = (byte)(x0 >>> 8); - result[10] = (byte)(x0 >>> 16); - result[11] = (byte)(x0 >>> 24); - result[12] = (byte) x1; - result[13] = (byte)(x1 >>> 8); - result[14] = (byte)(x1 >>> 16); - result[15] = (byte)(x1 >>> 24); - -if (TDEBUG && (debuglevel > 6)) { -System.out.println("CT="+toString(result)); -System.out.println(); -} -if (TDEBUG) trace(OUT, "blockEncrypt()"); - } - - /** - * Decrypt exactly one block of ciphertext. - * - * @param in The ciphertext. - * @param inOffset Index of in from which to start considering data. - * @param sessionKey The session key to use for decryption. - */ - public static final void - blockDecrypt (byte[] in, byte[] result, int inOffset, Object sessionKey) { -if (TDEBUG) trace(IN, "blockDecrypt("+in+", "+inOffset+", "+sessionKey+ ')'); - Object[] sk = (Object[]) sessionKey; // extract S-box and session key - int[] sBox = (int[]) sk[0]; - int[] sKey = (int[]) sk[1]; - -if (TDEBUG && (debuglevel > 6)) System.out.println("CT="+toString(in, inOffset, BLOCK_SIZE)); - - int x2 = (in[inOffset++] & 0xFF) | - (in[inOffset++] & 0xFF) << 8 | - (in[inOffset++] & 0xFF) << 16 | - (in[inOffset++] & 0xFF) << 24; - int x3 = (in[inOffset++] & 0xFF) | - (in[inOffset++] & 0xFF) << 8 | - (in[inOffset++] & 0xFF) << 16 | - (in[inOffset++] & 0xFF) << 24; - int x0 = (in[inOffset++] & 0xFF) | - (in[inOffset++] & 0xFF) << 8 | - (in[inOffset++] & 0xFF) << 16 | - (in[inOffset++] & 0xFF) << 24; - int x1 = (in[inOffset++] & 0xFF) | - (in[inOffset++] & 0xFF) << 8 | - (in[inOffset++] & 0xFF) << 16 | - (in[inOffset++] & 0xFF) << 24; - - x2 ^= sKey[OUTPUT_WHITEN ]; - x3 ^= sKey[OUTPUT_WHITEN + 1]; - x0 ^= sKey[OUTPUT_WHITEN + 2]; - x1 ^= sKey[OUTPUT_WHITEN + 3]; -if (TDEBUG && (debuglevel > 6)) System.out.println("CTw="+intToString(x2)+intToString(x3)+intToString(x0)+intToString(x1)); - - int k = ROUND_SUBKEYS + 2*ROUNDS - 1; - int t0, t1; - for (int R = 0; R < ROUNDS; R += 2) { - t0 = Fe32_0( sBox, x2 ); - t1 = Fe32_3( sBox, x3 ); - x1 ^= t0 + 2*t1 + sKey[k--]; - x1 = x1 >>> 1 | x1 << 31; - x0 = x0 << 1 | x0 >>> 31; - x0 ^= t0 + t1 + sKey[k--]; -if (TDEBUG && (debuglevel > 6)) System.out.println("PT"+(ROUNDS-R)+ '=' +intToString(x2)+intToString(x3)+intToString(x0)+intToString(x1)); - - t0 = Fe32_0( sBox, x0 ); - t1 = Fe32_3( sBox, x1 ); - x3 ^= t0 + 2*t1 + sKey[k--]; - x3 = x3 >>> 1 | x3 << 31; - x2 = x2 << 1 | x2 >>> 31; - x2 ^= t0 + t1 + sKey[k--]; -if (TDEBUG && (debuglevel > 6)) System.out.println("PT"+(ROUNDS-R-1)+ '=' +intToString(x2)+intToString(x3)+intToString(x0)+intToString(x1)); - } - x0 ^= sKey[INPUT_WHITEN ]; - x1 ^= sKey[INPUT_WHITEN + 1]; - x2 ^= sKey[INPUT_WHITEN + 2]; - x3 ^= sKey[INPUT_WHITEN + 3]; -if (TDEBUG && (debuglevel > 6)) System.out.println("PTw="+intToString(x2)+intToString(x3)+intToString(x0)+intToString(x1)); - - - result[0] = (byte) x0; - result[1] = (byte)(x0 >>> 8); - result[2] = (byte)(x0 >>> 16); - result[3] = (byte)(x0 >>> 24); - result[4] = (byte) x1; - result[5] = (byte)(x1 >>> 8); - result[6] = (byte)(x1 >>> 16); - result[7] = (byte)(x1 >>> 24); - result[8] = (byte) x2; - result[9] = (byte)(x2 >>> 8); - result[10] = (byte)(x2 >>> 16); - result[11] = (byte)(x2 >>> 24); - result[12] = (byte) x3; - result[13] = (byte)(x3 >>> 8); - result[14] = (byte)(x3 >>> 16); - result[15] = (byte)(x3 >>> 24); - -if (TDEBUG && (debuglevel > 6)) { -System.out.println("PT="+toString(result)); -System.out.println(); -} -if (TDEBUG) trace(OUT, "blockDecrypt()"); - } - - /** A basic symmetric encryption/decryption test. */ - public static boolean self_test() { return self_test(BLOCK_SIZE); } - - -// own methods -//........................................................................... - - private static final int b0( int x ) { return x & 0xFF; } - private static final int b1( int x ) { return (x >>> 8) & 0xFF; } - private static final int b2( int x ) { return (x >>> 16) & 0xFF; } - private static final int b3( int x ) { return (x >>> 24) & 0xFF; } - - /** - * Use (12, 8) Reed-Solomon code over GF(256) to produce a key S-box - * 32-bit entity from two key material 32-bit entities. - * - * @param k0 1st 32-bit entity. - * @param k1 2nd 32-bit entity. - * @return Remainder polynomial generated using RS code - */ - private static final int RS_MDS_Encode( int k0, int k1) { - int r = k1; - for (int i = 0; i < 4; i++) // shift 1 byte at a time - r = RS_rem( r ); - r ^= k0; - for (int i = 0; i < 4; i++) - r = RS_rem( r ); - return r; - } - - /* - * Reed-Solomon code parameters: (12, 8) reversible code:

- *

-    *   g(x) = x**4 + (a + 1/a) x**3 + a x**2 + (a + 1/a) x + 1
-    * 
- * where a = primitive root of field generator 0x14D - */ - private static final int RS_rem( int x ) { - int b = (x >>> 24) & 0xFF; - int g2 = ((b << 1) ^ ( (b & 0x80) != 0 ? RS_GF_FDBK : 0 )) & 0xFF; - int g3 = (b >>> 1) ^ ( (b & 0x01) != 0 ? (RS_GF_FDBK >>> 1) : 0 ) ^ g2 ; - int result = (x << 8) ^ (g3 << 24) ^ (g2 << 16) ^ (g3 << 8) ^ b; - return result; - } - - private static final int F32( int k64Cnt, int x, int[] k32 ) { - int b0 = b0(x); - int b1 = b1(x); - int b2 = b2(x); - int b3 = b3(x); - int k0 = k32[0]; - int k1 = k32[1]; - int k2 = k32[2]; - int k3 = k32[3]; - - int result = 0; - switch (k64Cnt & 3) { - case 1: - result = - MDS[0][(P[P_01][b0] & 0xFF) ^ b0(k0)] ^ - MDS[1][(P[P_11][b1] & 0xFF) ^ b1(k0)] ^ - MDS[2][(P[P_21][b2] & 0xFF) ^ b2(k0)] ^ - MDS[3][(P[P_31][b3] & 0xFF) ^ b3(k0)]; - break; - case 0: // same as 4 - b0 = (P[P_04][b0] & 0xFF) ^ b0(k3); - b1 = (P[P_14][b1] & 0xFF) ^ b1(k3); - b2 = (P[P_24][b2] & 0xFF) ^ b2(k3); - b3 = (P[P_34][b3] & 0xFF) ^ b3(k3); - case 3: - b0 = (P[P_03][b0] & 0xFF) ^ b0(k2); - b1 = (P[P_13][b1] & 0xFF) ^ b1(k2); - b2 = (P[P_23][b2] & 0xFF) ^ b2(k2); - b3 = (P[P_33][b3] & 0xFF) ^ b3(k2); - case 2: // 128-bit keys (optimize for this case) - result = - MDS[0][(P[P_01][(P[P_02][b0] & 0xFF) ^ b0(k1)] & 0xFF) ^ b0(k0)] ^ - MDS[1][(P[P_11][(P[P_12][b1] & 0xFF) ^ b1(k1)] & 0xFF) ^ b1(k0)] ^ - MDS[2][(P[P_21][(P[P_22][b2] & 0xFF) ^ b2(k1)] & 0xFF) ^ b2(k0)] ^ - MDS[3][(P[P_31][(P[P_32][b3] & 0xFF) ^ b3(k1)] & 0xFF) ^ b3(k0)]; - break; - } - return result; - } - - private static final int Fe32_0( int[] sBox, int x) { - return sBox[ (x << 1) & 0x1FE ] ^ - sBox[ ((x >>> 7) & 0x1FE) + 1] ^ - sBox[0x200 + ((x >>> 15) & 0x1FE) ] ^ - sBox[0x200 + ((x >>> 23) & 0x1FE) + 1]; - } - - - private static final int Fe32_3( int[] sBox, int x) { - return sBox[ (x >>> 23) & 0x1FE ] ^ - sBox[ ((x << 1) & 0x1FE) + 1] ^ - sBox[0x200 + ((x >>> 7) & 0x1FE) ] ^ - sBox[0x200 + ((x >>> 15) & 0x1FE) + 1]; - } - - /** @return The length in bytes of the Algorithm input block. */ - public static final int blockSize() { return BLOCK_SIZE; } - - /** A basic symmetric encryption/decryption test for a given key size. */ - private static boolean self_test (int keysize) { -if (TDEBUG) trace(IN, "self_test("+keysize+ ')'); - boolean ok = false; - try { - byte[] kb = new byte[keysize]; - byte[] pt = new byte[BLOCK_SIZE]; - int i; - - for (i = 0; i < keysize; i++) - kb[i] = (byte) i; - for (i = 0; i < BLOCK_SIZE; i++) - pt[i] = (byte) i; - -if (TDEBUG && (debuglevel > 6)) { -System.out.println("=========="); -System.out.println(); -System.out.println("KEYSIZE="+(8*keysize)); -System.out.println("KEY="+toString(kb)); -System.out.println(); -} - Object key = makeKey(kb); - -if (TDEBUG && (debuglevel > 6)) { -System.out.println("Intermediate Ciphertext Values (Encryption)"); -System.out.println(); -} - byte[] ct = new byte[BLOCK_SIZE]; - blockEncrypt(pt, ct, 0, key); - -if (TDEBUG && (debuglevel > 6)) { -System.out.println("Intermediate Plaintext Values (Decryption)"); -System.out.println(); -} - byte[] cpt = new byte[BLOCK_SIZE]; - blockDecrypt(ct, cpt, 0, key); - - ok = areEqual(pt, cpt); - if (!ok) - throw new RuntimeException("Symmetric operation failed"); - } catch (Exception x) { -if (TDEBUG && (debuglevel > 0)) { - debug("Exception encountered during self-test: " + x.getMessage()); - x.printStackTrace(); -} - } -if (TDEBUG && (debuglevel > 0)) debug("Self-test OK? " + ok); -if (TDEBUG) trace(OUT, "self_test()"); - return ok; - } - - -// utility static methods (from cryptix.util.core ArrayUtil and Hex classes) -//........................................................................... - - /** @return True iff the arrays have identical contents. */ - private static boolean areEqual (byte[] a, byte[] b) { - int aLength = a.length; - if (aLength != b.length) - return false; - for (int i = 0; i < aLength; i++) - if (a[i] != b[i]) - return false; - return true; - } - - /** - * Returns a string of 8 hexadecimal digits (most significant - * digit first) corresponding to the integer n, which is - * treated as unsigned. - */ - private static String intToString (int n) { - char[] buf = new char[8]; - for (int i = 7; i >= 0; i--) { - buf[i] = HEX_DIGITS[n & 0x0F]; - n >>>= 4; - } - return new String(buf); - } - - /** - * Returns a string of hexadecimal digits from a byte array. Each - * byte is converted to 2 hex symbols. - */ - private static String toString (byte[] ba) { - return toString(ba, 0, ba.length); - } - private static String toString (byte[] ba, int offset, int length) { - char[] buf = new char[length * 2]; - for (int i = offset, j = 0, k; i < offset+length; ) { - k = ba[i++]; - buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F]; - buf[j++] = HEX_DIGITS[ k & 0x0F]; - } - return new String(buf); - } - - -// main(): use to generate the Intermediate Values KAT -//........................................................................... - - public static void main (String[] args) { - self_test(16); - self_test(24); - self_test(32); - } -} Deleted: trunk/freenet/src/freenet/crypt/ciphers/Twofish_Properties.java =================================================================== --- trunk/freenet/src/freenet/crypt/ciphers/Twofish_Properties.java 2007-02-03 16:31:58 UTC (rev 11662) +++ trunk/freenet/src/freenet/crypt/ciphers/Twofish_Properties.java 2007-02-03 17:00:40 UTC (rev 11663) @@ -1,189 +0,0 @@ -/* - * Copyright (c) 1997, 1998 Systemics Ltd on behalf of - * the Cryptix Development Team. All rights reserved. - */ -package freenet.crypt.ciphers; - -import java.io.InputStream; -import java.io.PrintStream; -import java.io.PrintWriter; -import java.util.Enumeration; -import java.util.Properties; - -/** - * This class acts as a central repository for an algorithm specific - * properties. It reads an (algorithm).properties file containing algorithm- - * specific properties. When using the AES-Kit, this (algorithm).properties - * file is located in the (algorithm).jar file produced by the "jarit" batch/ - * script command.

- * - * Copyright © 1997, 1998 - * Systemics Ltd on behalf of the - * Cryptix Development Team. - *
All rights reserved.

- * - * @author David Hopwood - * @author Jill Baker - * @author Raif S. Naffah - */ -public class Twofish_Properties // implicit no-argument constructor -{ -// Constants and variables with relevant static code -//........................................................................... - - static final boolean GLOBAL_DEBUG = false; - - static final String ALGORITHM = "Twofish"; - static final double VERSION = 0.2; - static final String FULL_NAME = ALGORITHM + " ver. " + VERSION; - static final String NAME = "Twofish_Properties"; - - static final Properties properties = new Properties(); - - /** Default properties in case .properties file was not found. */ - private static final String[][] DEFAULT_PROPERTIES = { - {"Trace.Twofish_Algorithm", "true"}, - {"Debug.Level.*", "1"}, - {"Debug.Level.Twofish_Algorithm", "9"}, - }; - - static { -if (GLOBAL_DEBUG) System.err.println(">>> " + NAME + ": Looking for " + ALGORITHM + " properties"); - String it = ALGORITHM + ".properties"; - InputStream is = Twofish_Properties.class.getResourceAsStream(it); - boolean ok = is != null; - if (ok) - try { - properties.load(is); - is.close(); -if (GLOBAL_DEBUG) System.err.println(">>> " + NAME + ": Properties file loaded OK..."); - } catch (Exception x) { - ok = false; - } - if (!ok) { -if (GLOBAL_DEBUG) System.err.println(">>> " + NAME + ": WARNING: Unable to load \"" + it + "\" from CLASSPATH."); -if (GLOBAL_DEBUG) System.err.println(">>> " + NAME + ": Will use default values instead..."); - int n = DEFAULT_PROPERTIES.length; - for (int i = 0; i < n; i++) - properties.put( - DEFAULT_PROPERTIES[i][0], DEFAULT_PROPERTIES[i][1]); -if (GLOBAL_DEBUG) System.err.println(">>> " + NAME + ": Default properties now set..."); - } - } - - -// Properties methods (excluding load and save, which are deliberately not -// supported). -//........................................................................... - - /** Get the value of a property for this algorithm. */ - public static String getProperty (String key) { - return properties.getProperty(key); - } - - /** - * Get the value of a property for this algorithm, or return - * value if the property was not set. - */ - public static String getProperty (String key, String value) { - return properties.getProperty(key, value); - } - - /** List algorithm properties to the PrintStream out. */ - public static void list (PrintStream out) { - list(new PrintWriter(out, true)); - } - - /** List algorithm properties to the PrintWriter out. */ - public static void list (PrintWriter out) { - out.println("#"); - out.println("# ----- Begin "+ALGORITHM+" properties -----"); - out.println("#"); - String key, value; - Enumeration enu = properties.propertyNames(); - while (enu.hasMoreElements()) { - key = (String) enu.nextElement(); - value = getProperty(key); - out.println(key + " = " + value); - } - out.println("#"); - out.println("# ----- End "+ALGORITHM+" properties -----"); - } - -// public synchronized void load(InputStream in) throws IOException {} - - public static Enumeration propertyNames() { - return properties.propertyNames(); - } - -// public void save (OutputStream os, String comment) {} - - -// Developer support: Tracing and debugging enquiry methods (package-private) -//........................................................................... - - /** - * Return true if tracing is requested for a given class.

- * - * User indicates this by setting the tracing boolean - * property for label in the (algorithm).properties - * file. The property's key is "Trace.label".

- * - * @param label The name of a class. - * @return True iff a boolean true value is set for a property with - * the key Trace.label. - */ - static boolean isTraceable (String label) { - String s = getProperty("Trace." + label); - if (s == null) - return false; - return Boolean.valueOf(s).booleanValue(); - } - - /** - * Return the debug level for a given class.

- * - * User indicates this by setting the numeric property with key - * "Debug.Level.label".

- * - * If this property is not set, "Debug.Level.*" is looked up - * next. If neither property is set, or if the first property found is - * not a valid decimal integer, then this method returns 0. - * - * @param label The name of a class. - * @return The required debugging level for the designated class. - */ - static int getLevel (String label) { - String s = getProperty("Debug.Level." + label); - if (s == null) { - s = getProperty("Debug.Level.*"); - if (s == null) - return 0; - } - try { - return Integer.parseInt(s); - } catch (NumberFormatException e) { - return 0; - } - } - - /** - * Return the PrintWriter to which tracing and debugging output is to - * be sent.

- * - * User indicates this by setting the property with key Output - * to the literal out or err.

- * - * By default or if the set value is not allowed, System.err - * will be used. - */ - static PrintWriter getOutput() { - PrintWriter pw; - String name = getProperty("Output"); - if ((name != null) && name.equals("out")) - pw = new PrintWriter(System.out, true); - else - pw = new PrintWriter(System.err, true); - return pw; - } -} Modified: trunk/freenet/src/freenet/keys/ClientCHKBlock.java =================================================================== --- trunk/freenet/src/freenet/keys/ClientCHKBlock.java 2007-02-03 16:31:58 UTC (rev 11662) +++ trunk/freenet/src/freenet/keys/ClientCHKBlock.java 2007-02-03 17:00:40 UTC (rev 11663) @@ -89,7 +89,7 @@ if(cryptoKey.length < Node.SYMMETRIC_KEY_LENGTH) throw new CHKDecodeException("Crypto key too short"); cipher.initialize(key.cryptoKey); - PCFBMode pcfb = new PCFBMode(cipher); + PCFBMode pcfb = PCFBMode.create(cipher); byte[] hbuf = new byte[headers.length-2]; System.arraycopy(headers, 2, hbuf, 0, headers.length-2); byte[] dbuf = new byte[data.length]; @@ -182,7 +182,7 @@ throw new Error(e); } cipher.initialize(encKey); - PCFBMode pcfb = new PCFBMode(cipher); + PCFBMode pcfb = PCFBMode.create(cipher); pcfb.blockEncipher(header, 2, header.length-2); pcfb.blockEncipher(data, 0, data.length); Modified: trunk/freenet/src/freenet/keys/ClientSSKBlock.java =================================================================== --- trunk/freenet/src/freenet/keys/ClientSSKBlock.java 2007-02-03 16:31:58 UTC (rev 11662) +++ trunk/freenet/src/freenet/keys/ClientSSKBlock.java 2007-02-03 17:00:40 UTC (rev 11663) @@ -54,7 +54,7 @@ throw new Error(e); } aes.initialize(key.cryptoKey); - PCFBMode pcfb = new PCFBMode(aes); + PCFBMode pcfb = PCFBMode.create(aes); // ECB-encrypted E(H(docname)) serves as IV. pcfb.reset(key.ehDocname); pcfb.blockDecipher(decryptedHeaders, 0, decryptedHeaders.length); Modified: trunk/freenet/src/freenet/keys/InsertableClientSSK.java =================================================================== --- trunk/freenet/src/freenet/keys/InsertableClientSSK.java 2007-02-03 16:31:58 UTC (rev 11662) +++ trunk/freenet/src/freenet/keys/InsertableClientSSK.java 2007-02-03 17:00:40 UTC (rev 11663) @@ -117,7 +117,7 @@ // Encrypt data. Data encryption key = H(plaintext data). aes.initialize(origDataHash); - PCFBMode pcfb = new PCFBMode(aes); + PCFBMode pcfb = PCFBMode.create(aes); pcfb.reset(origDataHash); pcfb.blockEncipher(data, 0, data.length); Modified: trunk/freenet/src/freenet/node/FNPPacketMangler.java =================================================================== --- trunk/freenet/src/freenet/node/FNPPacketMangler.java 2007-02-03 16:31:58 UTC (rev 11662) +++ trunk/freenet/src/freenet/node/FNPPacketMangler.java 2007-02-03 17:00:40 UTC (rev 11663) @@ -167,7 +167,7 @@ BlockCipher authKey = opn.incomingSetupCipher; if(logMINOR) Logger.minor(this, "Decrypt key: "+HexUtil.bytesToHex(opn.incomingSetupKey)+" for "+peer+" : "+opn); // Does the packet match IV E( H(data) data ) ? - PCFBMode pcfb = new PCFBMode(authKey); + PCFBMode pcfb = PCFBMode.create(authKey); int ivLength = pcfb.lengthIV(); MessageDigest md = SHA256.getMessageDigest(); int digestLength = md.getDigestLength(); @@ -385,7 +385,7 @@ * Where Data = our bootID * Very similar to the surrounding wrapper in fact. */ - PCFBMode pcfb = new PCFBMode(cipher); + PCFBMode pcfb = PCFBMode.create(cipher); byte[] iv = new byte[pcfb.lengthIV()]; byte[] myRef = node.myCompressedSetupRef(); @@ -471,7 +471,7 @@ } BlockCipher cipher = pn.outgoingSetupCipher; if(logMINOR) Logger.minor(this, "Outgoing cipher: "+HexUtil.bytesToHex(pn.outgoingSetupKey)); - PCFBMode pcfb = new PCFBMode(cipher); + PCFBMode pcfb = PCFBMode.create(cipher); int paddingLength = node.random.nextInt(100); byte[] iv = new byte[pcfb.lengthIV()]; node.random.nextBytes(iv); @@ -527,7 +527,7 @@ } byte[] encKey = ctx.getKey(); BlockCipher cipher = ctx.getCipher(); - PCFBMode pcfb = new PCFBMode(cipher); + PCFBMode pcfb = PCFBMode.create(cipher); int ivLength = pcfb.lengthIV(); if(payload.length-3 < HASH_LENGTH + ivLength + 8) { Logger.error(this, "Too short phase "+i+" packet from "+replyTo+" probably from "+pn); @@ -659,7 +659,7 @@ // Verify the hash later PCFBMode pcfb; - pcfb = new PCFBMode(sessionCipher); + pcfb = PCFBMode.create(sessionCipher); // Set IV to the hash, after it is encrypted pcfb.reset(packetHash); //Logger.minor(this,"IV:\n"+HexUtil.bytesToHex(packetHash)); @@ -1440,7 +1440,7 @@ if(logMINOR) Logger.minor(this, "\nEncrypted: "+HexUtil.bytesToHex(digestTemp)+" ("+plaintext.length+" bytes plaintext)"); - PCFBMode pcfb = new PCFBMode(sessionCipher, digestTemp); + PCFBMode pcfb = PCFBMode.create(sessionCipher, digestTemp); pcfb.blockEncipher(output, digestLength, plaintext.length); //Logger.minor(this, "Ciphertext:\n"+HexUtil.bytesToHex(output, digestLength, plaintext.length)); Modified: trunk/freenet/src/freenet/support/io/PaddedEphemerallyEncryptedBucket.java =================================================================== --- trunk/freenet/src/freenet/support/io/PaddedEphemerallyEncryptedBucket.java 2007-02-03 16:31:58 UTC (rev 11662) +++ trunk/freenet/src/freenet/support/io/PaddedEphemerallyEncryptedBucket.java 2007-02-03 17:00:40 UTC (rev 11663) @@ -159,7 +159,7 @@ this.out = out; dataLength = 0; this.streamNumber = streamNumber; - pcfb = new PCFBMode(aes); + pcfb = PCFBMode.create(aes); } public void write(int b) throws IOException { @@ -235,7 +235,7 @@ public PaddedEphemerallyEncryptedInputStream(InputStream in) { this.in = in; - pcfb = new PCFBMode(aes); + pcfb = PCFBMode.create(aes); ptr = 0; } From dbkr at freenetproject.org Sun Feb 4 14:41:35 2007 From: dbkr at freenetproject.org (dbkr at freenetproject.org) Date: Sun, 4 Feb 2007 14:41:35 +0000 (UTC) Subject: [freenet-cvs] r11668 - trunk/freenet/src/freenet/crypt Message-ID: <20070204144135.E09619BBE4@emu.freenetproject.org> Author: dbkr Date: 2007-02-04 14:41:35 +0000 (Sun, 04 Feb 2007) New Revision: 11668 Modified: trunk/freenet/src/freenet/crypt/RijndaelPCFBMode.java Log: Fix to r.11663 (RijndaelPCFBMode subclass constructor was missing a paramter to the superconstructer call). Modified: trunk/freenet/src/freenet/crypt/RijndaelPCFBMode.java =================================================================== --- trunk/freenet/src/freenet/crypt/RijndaelPCFBMode.java 2007-02-04 14:39:54 UTC (rev 11667) +++ trunk/freenet/src/freenet/crypt/RijndaelPCFBMode.java 2007-02-04 14:41:35 UTC (rev 11668) @@ -29,7 +29,7 @@ } public RijndaelPCFBMode(Rijndael c, byte[] iv) { - super(c); + super(c, iv); int tempSize = c.getTempArraySize(); a = new int[tempSize]; t = new int[tempSize]; From zothar at freenetproject.org Sun Feb 4 17:03:08 2007 From: zothar at freenetproject.org (zothar at freenetproject.org) Date: Sun, 4 Feb 2007 17:03:08 +0000 (UTC) Subject: [freenet-cvs] r11675 - trunk/freenet/src/freenet/clients/http Message-ID: <20070204170308.F14819BBC9@emu.freenetproject.org> Author: zothar Date: 2007-02-04 17:03:08 +0000 (Sun, 04 Feb 2007) New Revision: 11675 Modified: trunk/freenet/src/freenet/clients/http/StatisticsToadlet.java Log: indent Modified: trunk/freenet/src/freenet/clients/http/StatisticsToadlet.java =================================================================== --- trunk/freenet/src/freenet/clients/http/StatisticsToadlet.java 2007-02-04 16:53:47 UTC (rev 11674) +++ trunk/freenet/src/freenet/clients/http/StatisticsToadlet.java 2007-02-04 17:03:08 UTC (rev 11675) @@ -153,7 +153,7 @@ DecimalFormat fix6p6 = new DecimalFormat("#####0.0#####"); DecimalFormat fix1p6sci = new DecimalFormat("0.######E0"); DecimalFormat fix3p1pct = new DecimalFormat("##0.0%"); - NumberFormat thousendPoint = NumberFormat.getInstance(); + NumberFormat thousendPoint = NumberFormat.getInstance(); double routingMissDistance = node.routingMissDistance.currentValue(); double backedOffPercent = node.backedOffPercent.currentValue(); String nodeUptimeString = TimeUtil.formatTime(nodeUptimeSeconds * 1000); // *1000 to convert to milliseconds @@ -274,7 +274,7 @@ peerStatsListenOnlyListItem.addChild("span", new String[] { "class", "title", "style" }, new String[] { "peer_listen_only", "Not connected and listen only: this node won't try to connect to these peers at all because the user has set ListenOnly on them", "border-bottom: 1px dotted; cursor: help;" }, "Listen Only"); peerStatsListenOnlyListItem.addChild("span", ":\u00a0" + numberOfListenOnly); } - nextTableCell = advancedEnabled ? overviewTableRow.addChild("td") : overviewTableRow.addChild("td", "class", "last"); + nextTableCell = advancedEnabled ? overviewTableRow.addChild("td") : overviewTableRow.addChild("td", "class", "last"); if(advancedEnabled) { @@ -378,98 +378,98 @@ } bandwidthList.addChild("li", "Output Rate:\u00a0" + SizeUtil.formatSize(output_rate, true) + "ps (of\u00a0"+SizeUtil.formatSize(outputBandwidthLimit, true)+"ps)"); bandwidthList.addChild("li", "Input Rate:\u00a0" + SizeUtil.formatSize(input_rate, true) + "ps (of\u00a0"+SizeUtil.formatSize(inputBandwidthLimit, true)+"ps)"); - nextTableCell = overviewTableRow.addChild("td"); + nextTableCell = overviewTableRow.addChild("td"); - // store size box - HTMLNode storeSizeInfobox = nextTableCell.addChild("div", "class", "infobox"); - storeSizeInfobox.addChild("div", "class", "infobox-header", "Store size"); - HTMLNode storeSizeInfoboxContent = storeSizeInfobox.addChild("div", "class", "infobox-content"); - HTMLNode storeSizeList = storeSizeInfoboxContent.addChild("ul"); - - final long fix32kb = 32 * 1024; - - long cachedKeys = node.getChkDatacache().keyCount(); - long cachedSize = cachedKeys * fix32kb; - long storeKeys = node.getChkDatastore().keyCount(); - long storeSize = storeKeys * fix32kb; - long overallKeys = cachedKeys + storeKeys; - long overallSize = cachedSize + storeSize; - -// long maxCachedKeys = node.getChkDatacache().getMaxKeys(); -// long maxStoreKeys = node.getChkDatastore().getMaxKeys(); - long maxOverallKeys = node.getMaxTotalKeys(); - long maxOverallSize = maxOverallKeys * fix32kb; - - long cachedStoreHits = node.getChkDatacache().hits(); - long cachedStoreMisses = node.getChkDatacache().misses(); - long cacheAccesses = cachedStoreHits + cachedStoreMisses; - long storeHits = node.getChkDatastore().hits(); - long storeMisses = node.getChkDatastore().misses(); - long storeAccesses = storeHits + storeMisses; - long overallAccesses = storeAccesses + cacheAccesses; - - storeSizeList.addChild("li", - "Cached keys:\u00a0" + thousendPoint.format(cachedKeys) + - " (" + SizeUtil.formatSize(cachedSize, true) + ')'); + // store size box + HTMLNode storeSizeInfobox = nextTableCell.addChild("div", "class", "infobox"); + storeSizeInfobox.addChild("div", "class", "infobox-header", "Store size"); + HTMLNode storeSizeInfoboxContent = storeSizeInfobox.addChild("div", "class", "infobox-content"); + HTMLNode storeSizeList = storeSizeInfoboxContent.addChild("ul"); + + final long fix32kb = 32 * 1024; + + long cachedKeys = node.getChkDatacache().keyCount(); + long cachedSize = cachedKeys * fix32kb; + long storeKeys = node.getChkDatastore().keyCount(); + long storeSize = storeKeys * fix32kb; + long overallKeys = cachedKeys + storeKeys; + long overallSize = cachedSize + storeSize; + +// long maxCachedKeys = node.getChkDatacache().getMaxKeys(); +// long maxStoreKeys = node.getChkDatastore().getMaxKeys(); + long maxOverallKeys = node.getMaxTotalKeys(); + long maxOverallSize = maxOverallKeys * fix32kb; + + long cachedStoreHits = node.getChkDatacache().hits(); + long cachedStoreMisses = node.getChkDatacache().misses(); + long cacheAccesses = cachedStoreHits + cachedStoreMisses; + long storeHits = node.getChkDatastore().hits(); + long storeMisses = node.getChkDatastore().misses(); + long storeAccesses = storeHits + storeMisses; + long overallAccesses = storeAccesses + cacheAccesses; + + storeSizeList.addChild("li", + "Cached keys:\u00a0" + thousendPoint.format(cachedKeys) + + " (" + SizeUtil.formatSize(cachedSize, true) + ')'); - storeSizeList.addChild("li", - "Stored keys:\u00a0" + thousendPoint.format(storeKeys) + - " (" + SizeUtil.formatSize(storeSize, true) + ')'); + storeSizeList.addChild("li", + "Stored keys:\u00a0" + thousendPoint.format(storeKeys) + + " (" + SizeUtil.formatSize(storeSize, true) + ')'); - storeSizeList.addChild("li", - "Overall size:\u00a0" + thousendPoint.format(overallKeys) + - "\u00a0/\u00a0" + thousendPoint.format(maxOverallKeys) + - " (" + SizeUtil.formatSize(overallSize, true) + - "\u00a0/\u00a0" + SizeUtil.formatSize(maxOverallSize, true) + - ")\u00a0(" + ((overallKeys*100)/maxOverallKeys) + "%)"); + storeSizeList.addChild("li", + "Overall size:\u00a0" + thousendPoint.format(overallKeys) + + "\u00a0/\u00a0" + thousendPoint.format(maxOverallKeys) + + " (" + SizeUtil.formatSize(overallSize, true) + + "\u00a0/\u00a0" + SizeUtil.formatSize(maxOverallSize, true) + + ")\u00a0(" + ((overallKeys*100)/maxOverallKeys) + "%)"); - storeSizeList.addChild("li", - "Cache hits:\u00a0" + thousendPoint.format(cachedStoreHits) + - "\u00a0/\u00a0"+thousendPoint.format(cacheAccesses) + - "\u00a0(" + ((cachedStoreHits*100) / (cacheAccesses)) + "%)"); - - storeSizeList.addChild("li", - "Store hits:\u00a0" + thousendPoint.format(storeHits) + - "\u00a0/\u00a0"+thousendPoint.format(storeAccesses) + - "\u00a0(" + ((storeHits*100) / (storeAccesses)) + "%)"); + storeSizeList.addChild("li", + "Cache hits:\u00a0" + thousendPoint.format(cachedStoreHits) + + "\u00a0/\u00a0"+thousendPoint.format(cacheAccesses) + + "\u00a0(" + ((cachedStoreHits*100) / (cacheAccesses)) + "%)"); + + storeSizeList.addChild("li", + "Store hits:\u00a0" + thousendPoint.format(storeHits) + + "\u00a0/\u00a0"+thousendPoint.format(storeAccesses) + + "\u00a0(" + ((storeHits*100) / (storeAccesses)) + "%)"); - storeSizeList.addChild("li", - "Avg. access rate:\u00a0" + thousendPoint.format(overallAccesses/nodeUptimeSeconds) + "/s"); - - nextTableCell = advancedEnabled ? overviewTableRow.addChild("td") : overviewTableRow.addChild("td", "class", "last"); + storeSizeList.addChild("li", + "Avg. access rate:\u00a0" + thousendPoint.format(overallAccesses/nodeUptimeSeconds) + "/s"); + + nextTableCell = advancedEnabled ? overviewTableRow.addChild("td") : overviewTableRow.addChild("td", "class", "last"); - // jvm stats box - HTMLNode jvmStatsInfobox = nextTableCell.addChild("div", "class", "infobox"); - jvmStatsInfobox.addChild("div", "class", "infobox-header", "JVM info"); - HTMLNode jvmStatsInfoboxContent = jvmStatsInfobox.addChild("div", "class", "infobox-content"); - HTMLNode jvmStatsList = jvmStatsInfoboxContent.addChild("ul"); + // jvm stats box + HTMLNode jvmStatsInfobox = nextTableCell.addChild("div", "class", "infobox"); + jvmStatsInfobox.addChild("div", "class", "infobox-header", "JVM info"); + HTMLNode jvmStatsInfoboxContent = jvmStatsInfobox.addChild("div", "class", "infobox-content"); + HTMLNode jvmStatsList = jvmStatsInfoboxContent.addChild("ul"); - Runtime rt = Runtime.getRuntime(); - float freeMemory = (float) rt.freeMemory(); - float totalMemory = (float) rt.totalMemory(); - float maxMemory = (float) rt.maxMemory(); + Runtime rt = Runtime.getRuntime(); + float freeMemory = (float) rt.freeMemory(); + float totalMemory = (float) rt.totalMemory(); + float maxMemory = (float) rt.maxMemory(); - long usedJavaMem = (long)(totalMemory - freeMemory); - long allocatedJavaMem = (long)totalMemory; - long maxJavaMem = (long)maxMemory; - int availableCpus = rt.availableProcessors(); - - ThreadGroup tg = Thread.currentThread().getThreadGroup(); - while(tg.getParent() != null) tg = tg.getParent(); - int threadCount = tg.activeCount(); + long usedJavaMem = (long)(totalMemory - freeMemory); + long allocatedJavaMem = (long)totalMemory; + long maxJavaMem = (long)maxMemory; + int availableCpus = rt.availableProcessors(); + + ThreadGroup tg = Thread.currentThread().getThreadGroup(); + while(tg.getParent() != null) tg = tg.getParent(); + int threadCount = tg.activeCount(); - jvmStatsList.addChild("li", "Used Java memory:\u00a0" + SizeUtil.formatSize(usedJavaMem, true)); - jvmStatsList.addChild("li", "Allocated Java memory:\u00a0" + SizeUtil.formatSize(allocatedJavaMem, true)); - jvmStatsList.addChild("li", "Maximum Java memory:\u00a0" + SizeUtil.formatSize(maxJavaMem, true)); - jvmStatsList.addChild("li", "Available CPUs:\u00a0" + availableCpus); - jvmStatsList.addChild("li", "Running threads:\u00a0" + thousendPoint.format(threadCount)); - jvmStatsList.addChild("li", "JVM Vendor:\u00a0" + System.getProperty("java.vm.vendor")); - jvmStatsList.addChild("li", "JVM Version:\u00a0" + System.getProperty("java.vm.version")); - jvmStatsList.addChild("li", "OS Name:\u00a0" + System.getProperty("os.name")); - jvmStatsList.addChild("li", "OS Version:\u00a0" + System.getProperty("os.version")); - jvmStatsList.addChild("li", "OS Architecture:\u00a0" + System.getProperty("os.arch")); + jvmStatsList.addChild("li", "Used Java memory:\u00a0" + SizeUtil.formatSize(usedJavaMem, true)); + jvmStatsList.addChild("li", "Allocated Java memory:\u00a0" + SizeUtil.formatSize(allocatedJavaMem, true)); + jvmStatsList.addChild("li", "Maximum Java memory:\u00a0" + SizeUtil.formatSize(maxJavaMem, true)); + jvmStatsList.addChild("li", "Available CPUs:\u00a0" + availableCpus); + jvmStatsList.addChild("li", "Running threads:\u00a0" + thousendPoint.format(threadCount)); + jvmStatsList.addChild("li", "JVM Vendor:\u00a0" + System.getProperty("java.vm.vendor")); + jvmStatsList.addChild("li", "JVM Version:\u00a0" + System.getProperty("java.vm.version")); + jvmStatsList.addChild("li", "OS Name:\u00a0" + System.getProperty("os.name")); + jvmStatsList.addChild("li", "OS Version:\u00a0" + System.getProperty("os.version")); + jvmStatsList.addChild("li", "OS Architecture:\u00a0" + System.getProperty("os.arch")); - // unclaimedFIFOMessageCounts box + // unclaimedFIFOMessageCounts box overviewTableRow = overviewTable.addChild("tr"); nextTableCell = overviewTableRow.addChild("td", "class", "first"); Map unclaimedFIFOMessageCountsMap = node.getUSM().getUnclaimedFIFOMessageCounts(); @@ -518,10 +518,10 @@ nextTableCell = overviewTableRow.addChild("td"); // node version information box - HTMLNode versionInfobox = nextTableCell.addChild("div", "class", "infobox"); - versionInfobox.addChild("div", "class", "infobox-header", "Node Version Information"); - HTMLNode versionInfoboxContent = versionInfobox.addChild("div", "class", "infobox-content"); - HTMLNode versionInfoboxList = versionInfoboxContent.addChild("ul"); + HTMLNode versionInfobox = nextTableCell.addChild("div", "class", "infobox"); + versionInfobox.addChild("div", "class", "infobox-header", "Node Version Information"); + HTMLNode versionInfoboxContent = versionInfobox.addChild("div", "class", "infobox-content"); + HTMLNode versionInfoboxList = versionInfoboxContent.addChild("ul"); versionInfoboxList.addChild("li", "Freenet " + Version.nodeVersion + " Build #" + Version.buildNumber() + " r" + Version.cvsRevision); if(NodeStarter.extBuildNumber < NodeStarter.RECOMMENDED_EXT_BUILD_NUMBER) { versionInfoboxList.addChild("li", "Freenet-ext Build #" + NodeStarter.extBuildNumber + '(' + NodeStarter.RECOMMENDED_EXT_BUILD_NUMBER + ") r" + NodeStarter.extRevisionNumber); @@ -539,7 +539,7 @@ // node distribution box nextTableCell = overviewTableRow.addChild("td", "class", "first"); - HTMLNode nodeCircleInfobox = nextTableCell.addChild("div", "class", "infobox"); + HTMLNode nodeCircleInfobox = nextTableCell.addChild("div", "class", "infobox"); nodeCircleInfobox.addChild("div", "class", "infobox-header", "Node\u00a0Location\u00a0Distribution (w/Swap\u00a0Age)"); HTMLNode nodeCircleInfoboxContent = nodeCircleInfobox.addChild("div", "class", "infobox-content"); addNodeCircle(nodeCircleInfoboxContent); @@ -560,7 +560,7 @@ private void addNodeCircle (HTMLNode htmlNode) { HTMLNode nodeCircleInfoboxContentDiv = htmlNode.addChild("div", new String[] { "style", "class" }, new String[] {"position: relative; height: " + ((PEER_CIRCLE_RADIUS + PEER_CIRCLE_ADDITIONAL_FREE_SPACE) * 2) + "px; width: " + ((PEER_CIRCLE_RADIUS + PEER_CIRCLE_ADDITIONAL_FREE_SPACE) * 2) + "px", "peercircle" }); - nodeCircleInfoboxContentDiv.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(0, false, 1.0), "mark" }, "|"); + nodeCircleInfoboxContentDiv.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(0, false, 1.0), "mark" }, "|"); nodeCircleInfoboxContentDiv.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(0.125, false, 1.0), "mark" }, "+"); nodeCircleInfoboxContentDiv.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(0.25, false, 1.0), "mark" }, "--"); nodeCircleInfoboxContentDiv.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(0.375, false, 1.0), "mark" }, "+"); @@ -585,7 +585,7 @@ age = MAX_CIRCLE_AGE_THRESHOLD; } strength = 1 - ((double) age / MAX_CIRCLE_AGE_THRESHOLD ); - nodeCircleInfoboxContentDiv.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(location.doubleValue(), false, strength), "connected" }, "x"); + nodeCircleInfoboxContentDiv.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(location.doubleValue(), false, strength), "connected" }, "x"); } nodeCircleInfoboxContentDiv.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(node.getLocation(), true, 1.0), "me" }, "x"); } @@ -604,7 +604,7 @@ HTMLNode peerHistogramLegendCell; HTMLNode peerHistogramGraphCell; HTMLNode peerCircleInfoboxContent = peerCircleTableCell.addChild("div", new String[] { "style", "class" }, new String[] {"position: relative; height: " + ((PEER_CIRCLE_RADIUS + PEER_CIRCLE_ADDITIONAL_FREE_SPACE) * 2) + "px; width: " + ((PEER_CIRCLE_RADIUS + PEER_CIRCLE_ADDITIONAL_FREE_SPACE) * 2) + "px", "peercircle" }); - peerCircleInfoboxContent.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(0, false, 1.0), "mark" }, "|"); + peerCircleInfoboxContent.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(0, false, 1.0), "mark" }, "|"); peerCircleInfoboxContent.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(0.125, false, 1.0), "mark" }, "+"); peerCircleInfoboxContent.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(0.25, false, 1.0), "mark" }, "--"); peerCircleInfoboxContent.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(0.375, false, 1.0), "mark" }, "+"); @@ -625,16 +625,16 @@ peerLocation = peerNodeStatus.getLocation(); peerDistance = PeerManager.distance( myLocation, peerLocation ); histogramIndex = (int) (Math.floor(peerDistance * HISTOGRAM_LENGTH * 2)); - if (peerNodeStatus.isConnected()) { - histogramConnected[histogramIndex]++; - } else { - histogramDisconnected[histogramIndex]++; + if (peerNodeStatus.isConnected()) { + histogramConnected[histogramIndex]++; + } else { + histogramDisconnected[histogramIndex]++; } - peerCircleInfoboxContent.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(peerLocation, false, (1.0 - peerNodeStatus.getPReject())), ((peerNodeStatus.isConnected())?"connected":"disconnected") }, "x"); + peerCircleInfoboxContent.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(peerLocation, false, (1.0 - peerNodeStatus.getPReject())), ((peerNodeStatus.isConnected())?"connected":"disconnected") }, "x"); } peerCircleInfoboxContent.addChild("span", new String[] { "style", "class" }, new String[] { generatePeerCircleStyleString(myLocation, true, 1.0), "me" }, "x"); // - double histogramPercent; + double histogramPercent; for (int i = 0; i < HISTOGRAM_LENGTH; i++) { peerHistogramLegendCell = peerHistogramLegendTableRow.addChild("td"); peerHistogramGraphCell = peerHistogramGraphTableRow.addChild("td", "style", "height: 100px;"); @@ -653,7 +653,7 @@ // int offset = 0; if( offsetMe ) { - // Make our own peer stand out from the crowd better so we can see it easier + // Make our own peer stand out from the crowd better so we can see it easier offset = -10; } else { offset = (int) (((double) PEER_CIRCLE_INNER_RADIUS) * (1.0 - strength)); From zothar at freenetproject.org Sun Feb 4 17:05:25 2007 From: zothar at freenetproject.org (zothar at freenetproject.org) Date: Sun, 4 Feb 2007 17:05:25 +0000 (UTC) Subject: [freenet-cvs] r11677 - trunk/freenet/src/freenet/clients/http Message-ID: <20070204170525.E27559BC28@emu.freenetproject.org> Author: zothar Date: 2007-02-04 17:05:24 +0000 (Sun, 04 Feb 2007) New Revision: 11677 Modified: trunk/freenet/src/freenet/clients/http/StatisticsToadlet.java Log: Re-order threads above CPUs in JVM info on /stats/, resolving https://bugs.freenetproject.org/view.php?id=1074 Modified: trunk/freenet/src/freenet/clients/http/StatisticsToadlet.java =================================================================== --- trunk/freenet/src/freenet/clients/http/StatisticsToadlet.java 2007-02-04 17:05:08 UTC (rev 11676) +++ trunk/freenet/src/freenet/clients/http/StatisticsToadlet.java 2007-02-04 17:05:24 UTC (rev 11677) @@ -461,8 +461,8 @@ jvmStatsList.addChild("li", "Used Java memory:\u00a0" + SizeUtil.formatSize(usedJavaMem, true)); jvmStatsList.addChild("li", "Allocated Java memory:\u00a0" + SizeUtil.formatSize(allocatedJavaMem, true)); jvmStatsList.addChild("li", "Maximum Java memory:\u00a0" + SizeUtil.formatSize(maxJavaMem, true)); + jvmStatsList.addChild("li", "Running threads:\u00a0" + thousendPoint.format(threadCount)); jvmStatsList.addChild("li", "Available CPUs:\u00a0" + availableCpus); - jvmStatsList.addChild("li", "Running threads:\u00a0" + thousendPoint.format(threadCount)); jvmStatsList.addChild("li", "JVM Vendor:\u00a0" + System.getProperty("java.vm.vendor")); jvmStatsList.addChild("li", "JVM Version:\u00a0" + System.getProperty("java.vm.version")); jvmStatsList.addChild("li", "OS Name:\u00a0" + System.getProperty("os.name")); From mrogers at freenetproject.org Mon Feb 5 11:52:16 2007 From: mrogers at freenetproject.org (mrogers at freenetproject.org) Date: Mon, 5 Feb 2007 11:52:16 +0000 (UTC) Subject: [freenet-cvs] r11679 - in trunk/apps/load-balancing-sims/phase7/sim: . clients messages Message-ID: <20070205115216.B20789BB8F@emu.freenetproject.org> Author: mrogers Date: 2007-02-05 11:52:15 +0000 (Mon, 05 Feb 2007) New Revision: 11679 Modified: trunk/apps/load-balancing-sims/phase7/sim/Node.java trunk/apps/load-balancing-sims/phase7/sim/Packet.java trunk/apps/load-balancing-sims/phase7/sim/Peer.java trunk/apps/load-balancing-sims/phase7/sim/Sim.java trunk/apps/load-balancing-sims/phase7/sim/clients/Client.java trunk/apps/load-balancing-sims/phase7/sim/clients/SimplePublisher.java trunk/apps/load-balancing-sims/phase7/sim/messages/ChkInsert.java trunk/apps/load-balancing-sims/phase7/sim/messages/Search.java trunk/apps/load-balancing-sims/phase7/sim/messages/SskInsert.java Log: Disabled ack coalescing in preparation for improved RTO/FRTO calculation Modified: trunk/apps/load-balancing-sims/phase7/sim/Node.java =================================================================== --- trunk/apps/load-balancing-sims/phase7/sim/Node.java 2007-02-05 00:36:43 UTC (rev 11678) +++ trunk/apps/load-balancing-sims/phase7/sim/Node.java 2007-02-05 11:52:15 UTC (rev 11679) @@ -1,5 +1,5 @@ package sim; -import sim.generators.Client; +import sim.clients.Client; import sim.handlers.*; import sim.messages.*; import java.util.HashMap; Modified: trunk/apps/load-balancing-sims/phase7/sim/Packet.java =================================================================== --- trunk/apps/load-balancing-sims/phase7/sim/Packet.java 2007-02-05 00:36:43 UTC (rev 11678) +++ trunk/apps/load-balancing-sims/phase7/sim/Packet.java 2007-02-05 11:52:15 UTC (rev 11679) @@ -1,40 +1,32 @@ // A low-level packet (as opposed to a high-level message) package sim; -import sim.messages.Ack; import sim.messages.Message; import java.util.ArrayList; class Packet { - public final static int HEADER_SIZE = 70; // Including IP & UDP headers - public final static int ACK_SIZE = 4; // Size of an ack in bytes + public final static int HEADER_SIZE = 60; // Including IP & UDP headers public final static int MAX_SIZE = 1450; // MTU including headers public final static int SENSIBLE_PAYLOAD = 1000; // Coalescing public final int src, dest; // Network addresses public int size = HEADER_SIZE; // Size in bytes, including headers public int seq = -1; // Data sequence number (-1 if no data) - public ArrayList acks = null; + public int ack = -1; // Ack sequence number (-1 if no ack) public ArrayList messages = null; public double sent; // Time at which the packet was (re) transmitted public double latency; // Link latency, stored here for convenience - public Packet (int src, int dest, double latency) + public Packet (int src, int dest, double latency, int ack) { this.src = src; this.dest = dest; this.latency = latency; + this.ack = ack; } - public void addAck (Ack a) - { - if (acks == null) acks = new ArrayList(); - acks.add (a); - size += a.size(); - } - public void addMessage (Message m) { if (messages == null) messages = new ArrayList(); Modified: trunk/apps/load-balancing-sims/phase7/sim/Peer.java =================================================================== --- trunk/apps/load-balancing-sims/phase7/sim/Peer.java 2007-02-05 00:36:43 UTC (rev 11678) +++ trunk/apps/load-balancing-sims/phase7/sim/Peer.java 2007-02-05 11:52:15 UTC (rev 11679) @@ -36,7 +36,6 @@ private int txSeq = 0; // Sequence number of next outgoing data packet private int txMaxSeq = SEQ_RANGE - 1; // Highest sequence number private LinkedList txBuffer; // Retransmission buffer - private DeadlineQueue ackQueue; // Outgoing acks private DeadlineQueue searchQueue; // Outgoing search messages private DeadlineQueue transferQueue; // Outgoing transfers private CongestionWindow window; // AIMD congestion window @@ -61,7 +60,6 @@ this.location = location; this.latency = latency; txBuffer = new LinkedList(); - ackQueue = new DeadlineQueue(); searchQueue = new DeadlineQueue(); transferQueue = new DeadlineQueue(); window = new CongestionWindow (this); @@ -83,20 +81,9 @@ // Start the coalescing timer startTimer(); // Send as many packets as possible - while (send()); + while (send (-1)); } - // Queue an ack for transmission - private void sendAck (int seq) - { - if (LOG) log ("ack " + seq + " added to ack queue"); - ackQueue.add (new Ack (seq, Event.time() + MAX_DELAY)); - // Start the coalescing timer - startTimer(); - // Send as many packets as possible - while (send()); - } - // Start the coalescing timer private void startTimer() { @@ -107,11 +94,11 @@ } // Try to send a packet, return true if a packet was sent - private boolean send() + private boolean send (int ack) { - int waiting = ackQueue.size+searchQueue.size+transferQueue.size; + int waiting = searchQueue.size + transferQueue.size; if (LOG) log (waiting + " bytes waiting"); - if (waiting == 0) return false; + if (ack == -1 && waiting == 0) return false; // Return to slow start when the link is idle double now = Event.time(); @@ -123,29 +110,30 @@ size = Math.min (size, node.bandwidth.available()); if (LOG) log (size + " bytes available for packet"); - // Urgent acks to send? - if (ackQueue.deadline() <= now) return sendPacket (size); + // Ack to send? + if (ack != -1) return sendPacket (ack, size); // Urgent searches and room to send them? if (searchQueue.deadline() <= now - && searchQueue.headSize() <= size) return sendPacket (size); + && searchQueue.headSize() <= size) + return sendPacket (ack, size); // Urgent transfers and room to send them? if (transferQueue.deadline() <= now - && transferQueue.headSize() <= size) return sendPacket (size); + && transferQueue.headSize() <= size) + return sendPacket (ack, size); // Enough non-urgent messages for a large packet, and room? if (waiting >= Packet.SENSIBLE_PAYLOAD - && size >= Packet.SENSIBLE_PAYLOAD) return sendPacket (size); + && size >= Packet.SENSIBLE_PAYLOAD) + return sendPacket (ack, size); if (LOG) log ("not sending a packet"); return false; } // Try to send a packet up to the specified size, return true if sent - private boolean sendPacket (int maxSize) + private boolean sendPacket (int ack, int maxSize) { // Construct a packet - Packet p = new Packet (node.net.address, address, latency); - // Add all waiting acks to the packet - while (ackQueue.size > 0) p.addAck (ackQueue.pop()); + Packet p = new Packet (node.net.address, address, latency, ack); if (LOG) log ((maxSize - p.size) + " bytes for messages"); // Don't allow more than SEQ_RANGE payloads to be in flight if (txSeq <= txMaxSeq) { @@ -169,7 +157,7 @@ log ("waiting for ack " + (txMaxSeq - SEQ_RANGE + 1)); } // Don't send empty packets - if (p.acks == null && p.messages == null) return false; + if (p.ack == -1 && p.messages == null) return false; // Transmit the packet if (LOG) log ("sending packet " +p.seq+ ", " +p.size+ " bytes"); node.sendPacket (p); @@ -186,7 +174,7 @@ // Called by Node when a packet arrives public void handlePacket (Packet p) { - if (p.acks != null) for (Ack a : p.acks) handleAck (a); + if (p.ack != -1) handleAck (p.ack); if (p.messages != null) handleData (p); } @@ -195,7 +183,7 @@ if (LOG) log ("received packet " +p.seq+ ", expected " +rxSeq); if (p.seq < rxSeq || rxDupe.contains (p.seq)) { if (LOG) log ("duplicate packet"); - sendAck (p.seq); // Original ack may have been lost + send (p.seq); // Original ack may have been lost } else if (p.seq == rxSeq) { // Find the sequence number of the next missing packet @@ -204,7 +192,7 @@ // Deliver the messages to the node for (Message m : p.messages) node.handleMessage (m, this); - sendAck (p.seq); + send (p.seq); } else if (p.seq < rxSeq + SEQ_RANGE) { if (LOG) log ("packet out of order"); @@ -212,37 +200,36 @@ // Deliver the messages to the node for (Message m : p.messages) node.handleMessage (m, this); - sendAck (p.seq); + send (p.seq); } // This indicates a misbehaving sender - discard the packet else if (LOG) log ("WARNING: sequence number out of range"); } - private void handleAck (Ack a) + private void handleAck (int ack) { - int seq = a.id; - if (LOG) log ("received ack " + seq); + if (LOG) log ("received ack " + ack); double now = Event.time(); Iterator i = txBuffer.iterator(); while (i.hasNext()) { Packet p = i.next(); double age = now - p.sent; // Explicit ack - if (p.seq == seq) { + if (p.seq == ack) { i.remove(); // Update the congestion window window.bytesAcked (p.size); // Update the average round-trip time rtt = rtt * RTT_DECAY + age * (1.0 - RTT_DECAY); if (LOG) { - log ("packet " +p.seq+ " acknowledged"); + log ("packet " + ack + " acknowledged"); log ("round-trip time " + age); log ("average round-trip time " + rtt); } break; } // Fast retransmission - if (p.seq < seq && age > FRTO * rtt + MAX_DELAY) { + if (p.seq < ack && age > FRTO * rtt) { p.sent = now; if (LOG) log ("fast retransmitting " + p.seq); node.resendPacket (p); @@ -253,8 +240,8 @@ if (txBuffer.isEmpty()) txMaxSeq = txSeq + SEQ_RANGE - 1; else txMaxSeq = txBuffer.peek().seq + SEQ_RANGE - 1; if (LOG) log ("maximum sequence number " + txMaxSeq); - // Send as many packets a possible - if (timerRunning) while (send()); + // Send as many packets as possible + if (timerRunning) while (send (-1)); else checkDeadlines(); } @@ -326,7 +313,7 @@ double now = Event.time(); for (Packet p : txBuffer) { - if (now - p.sent > RTO * rtt + MAX_DELAY) { + if (now - p.sent > RTO * rtt) { // Retransmission timeout if (LOG) log ("retransmitting " + p.seq); p.sent = now; @@ -341,11 +328,11 @@ private void checkDeadlines() { // Send as many packets as possible - while (send()); + while (send (-1)); // Find the next coalescing deadline - ignore message deadlines // if there isn't room in the congestion window to send them - double dl = ackQueue.deadline(); - int win = window.available() -Packet.HEADER_SIZE -ackQueue.size; + double dl = Double.POSITIVE_INFINITY; + int win = window.available() - Packet.HEADER_SIZE; if (searchQueue.headSize() <= win) dl = Math.min (dl, searchQueue.deadline()); if (transferQueue.headSize() <= win) @@ -370,12 +357,9 @@ // Are we waiting for the bandwidth limiter? private boolean shouldPoll() { - double now = Event.time(); - // Will we need to send an ack before the next polling interval? - if (ackQueue.deadline() < now + node.bandwidth.poll) - return false; double bw = node.bandwidth.available(); double win = window.available(); + double now = Event.time(); // Is there an overdue search that's waiting for bandwidth? if (searchQueue.headSize() > bw && searchQueue.headSize() <= win Modified: trunk/apps/load-balancing-sims/phase7/sim/Sim.java =================================================================== --- trunk/apps/load-balancing-sims/phase7/sim/Sim.java 2007-02-05 00:36:43 UTC (rev 11678) +++ trunk/apps/load-balancing-sims/phase7/sim/Sim.java 2007-02-05 11:52:15 UTC (rev 11679) @@ -1,5 +1,5 @@ package sim; -import sim.generators.SimplePublisher; +import sim.clients.SimplePublisher; class Sim implements EventTarget { Modified: trunk/apps/load-balancing-sims/phase7/sim/clients/Client.java =================================================================== --- trunk/apps/load-balancing-sims/phase7/sim/clients/Client.java 2007-02-05 00:36:43 UTC (rev 11678) +++ trunk/apps/load-balancing-sims/phase7/sim/clients/Client.java 2007-02-05 11:52:15 UTC (rev 11679) @@ -1,4 +1,4 @@ -package sim.generators; +package sim.clients; import sim.messages.Search; public interface Client Modified: trunk/apps/load-b