[freenet-cvs] r15468 - trunk/freenet/src/freenet/node

nextgens at freenetproject.org nextgens at freenetproject.org
Sun Oct 21 14:19:15 UTC 2007


Author: nextgens
Date: 2007-10-21 14:19:15 +0000 (Sun, 21 Oct 2007)
New Revision: 15468

Modified:
   trunk/freenet/src/freenet/node/FNPPacketMangler.java
Log:
Reintroduce the "generate and sign the DH exponents off-thread" patch.

The code is much simpler that way; not as ambitious as the previous one though.
We will renew the DH exponents at most once every 30sec using the ticker.

That should work even for opennet :p

Modified: trunk/freenet/src/freenet/node/FNPPacketMangler.java
===================================================================
--- trunk/freenet/src/freenet/node/FNPPacketMangler.java	2007-10-21 13:07:09 UTC (rev 15467)
+++ trunk/freenet/src/freenet/node/FNPPacketMangler.java	2007-10-21 14:19:15 UTC (rev 15468)
@@ -81,7 +81,7 @@
 		JFK_PREFIX_RESPONDER = R;
 	}
 	
-	/** We renew it every 30mins (the spec. says "once a while") - access is synchronized! */
+	/** We renew it every 30sec (the spec. says "once a while") - access is synchronized! */
 	private DiffieHellmanLightContext currentDHContext = null;
 	private long currentDHContextLifetime = 0;
 	
@@ -2431,7 +2431,7 @@
 	}
 
 	public int[] supportedNegTypes() {
-		return new int[] { 2, 1 };
+		return new int[] { 1, 2 };
 	}
 
 	public int fullHeadersLengthOneMessage() {
@@ -2454,16 +2454,48 @@
 		return crypto.config.alwaysAllowLocalAddresses();
 	}
 
+	private DiffieHellmanLightContext _genLightDiffieHellmanContext() {
+		DiffieHellmanLightContext ctx = DiffieHellman.generateLightContext();
+		ctx.setSignature(crypto.sign(SHA256.digest(assembleDHParams(ctx.myExponential, crypto.getCryptoGroup()))));
+		
+		return ctx;
+	}
+	
+	/**
+	 * Change the DH Exponents on a regular basis but at most once every 30sec
+	 * 
+	 * @return {@link DiffieHellmanLightContext}
+	 */
 	private DiffieHellmanLightContext getLightDiffieHellmanContext() {
 		final long now = System.currentTimeMillis();
 		
+		boolean changeDHExponents = false;
+		
 		synchronized (this) {
-			if((currentDHContext == null) || (currentDHContextLifetime + 1800000 /*30mins*/) < now) {
+			if((currentDHContext == null) || (currentDHContextLifetime + 30000 /*30sec*/) < now) {
+				changeDHExponents = true;
 				currentDHContextLifetime = now;
-				currentDHContext = DiffieHellman.generateLightContext();
-				currentDHContext.setSignature(crypto.sign(SHA256.digest(assembleDHParams(currentDHContext.myExponential, crypto.getCryptoGroup()))));
 			}
 		}
+		
+		if(changeDHExponents) {
+			if(currentDHContext == null) {
+				Logger.minor(this, "No DH exponent have been created; generate the context on-thread!");
+				// No need to synchronize here as we are on-thread
+				currentDHContext = _genLightDiffieHellmanContext();
+			} else {
+				// Use the ticket to do it off-thread
+				node.getTicker().queueTimedJob(new Runnable() {
+					public void run() {
+						synchronized (this) {
+							currentDHContext = _genLightDiffieHellmanContext();
+						}
+					}
+				}, 0);
+				Logger.minor(this, "The DH exponents will been renewed soonish");
+			}
+		}
+		
 		return currentDHContext;
 	}
 




More information about the cvs mailing list