Author: toad
Date: 2007-10-24 16:05:17 +0000 (Wed, 24 Oct 2007)
New Revision: 15527

Modified:
   trunk/freenet/src/freenet/node/FNPPacketMangler.java
Log:
More robust and slightly simpler.

Modified: trunk/freenet/src/freenet/node/FNPPacketMangler.java
===================================================================
--- trunk/freenet/src/freenet/node/FNPPacketMangler.java        2007-10-24 
16:00:32 UTC (rev 15526)
+++ trunk/freenet/src/freenet/node/FNPPacketMangler.java        2007-10-24 
16:05:17 UTC (rev 15527)
@@ -104,9 +104,7 @@
        /** The Runnable in charge of rekeying on a regular basis */
        private final Runnable transientKeyRekeyer = new Runnable() {
                public void run() {
-                       resetTransientKey();
-                       
-                       node.getTicker().queueTimedJob(transientKeyRekeyer, 
TRANSIENT_KEY_REKEYING_MIN_INTERVAL);
+                       maybeResetTransientKey();
                }
        };
        /** Minimum headers overhead */
@@ -1117,9 +1115,7 @@

                // cache the message
                synchronized (authenticatorCache) {
-                       if(authenticatorCache.size() > AUTHENTICATOR_CACHE_SIZE)
-                               resetTransientKey();
-                       else
+                       if(!maybeResetTransientKey())
                                authenticatorCache.put(authenticator,message3);
                }               
                sendAuthPacket(1, 2, 2, message3, pn, replyTo);
@@ -1197,9 +1193,7 @@

                // cache the message
                synchronized (authenticatorCache) {
-                       if(authenticatorCache.size() > AUTHENTICATOR_CACHE_SIZE)
-                               resetTransientKey();
-                       else
+                       if(!maybeResetTransientKey())
                                authenticatorCache.put(authenticator, message4);
                }

@@ -2650,19 +2644,30 @@
                return mac.mac(exponential.toByteArray(), toHash, HASH_LENGTH);
        }

+       private long timeLastReset = -1;
+       
        /**
         * Change the transient key used by JFK.
         * 
         * It will determine the PFS interval, hence we call it at least once 
every 30mins.
+        * 
+        * @return True if we reset the transient key and therefore the 
authenticator cache.
         */
-       private void resetTransientKey() {
-               Logger.normal(this, "JFK's TransientKey has been changed and 
the message cache flushed.");
+       private boolean maybeResetTransientKey() {
                synchronized (authenticatorCache) {
+                       if(authenticatorCache.size() < 
AUTHENTICATOR_CACHE_SIZE) {
+                               long now = System.currentTimeMillis();
+                               if(now - timeLastReset < 
TRANSIENT_KEY_REKEYING_MIN_INTERVAL)
+                                       return false;
+                       }
                        node.random.nextBytes(transientKey);

                        // reset the authenticator cache
                        authenticatorCache.clear();
                }
+               node.getTicker().queueTimedJob(transientKeyRekeyer, 
TRANSIENT_KEY_REKEYING_MIN_INTERVAL);
+               Logger.normal(this, "JFK's TransientKey has been changed and 
the message cache flushed.");
+               return true;
        }

        private byte[] stripBigIntegerToNetworkFormat(BigInteger exponential) {


Reply via email to