From 570d8d15af3d26155eff217af01ddd4522e9eab2 Mon Sep 17 00:00:00 2001 From: zzz Date: Wed, 7 Apr 2010 23:19:24 +0000 Subject: [PATCH] * Key Manager: Hopefully avoid some races at startup http://forum.i2p/viewtopic.php?t=4424 --- .../java/src/net/i2p/router/KeyManager.java | 60 +++++++++++++------ 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/router/java/src/net/i2p/router/KeyManager.java b/router/java/src/net/i2p/router/KeyManager.java index a145a4f66..4d19228b7 100644 --- a/router/java/src/net/i2p/router/KeyManager.java +++ b/router/java/src/net/i2p/router/KeyManager.java @@ -105,11 +105,15 @@ public class KeyManager { _leaseSetKeys.put(dest.calculateHash(), keys); } + /** + * Wait one second, as this will get called 4 times in quick succession + * There is still a race here though, if a key is set while the sync job is running + */ private void queueWrite() { Clock cl = _context.clock(); JobQueue q = _context.jobQueue(); if ( (cl == null) || (q == null) ) return; - _synchronizeJob.getTiming().setStartAfter(cl.now()); + _synchronizeJob.getTiming().setStartAfter(cl.now() + 1000); q.addJob(_synchronizeJob); } @@ -159,33 +163,55 @@ public class KeyManager { } private synchronized void syncPrivateKey(File keyDir) { - File keyFile = new File(keyDir, KeyManager.KEYFILE_PRIVATE_ENC); + DataStructure ds; + File keyFile = new File(keyDir, KEYFILE_PRIVATE_ENC); boolean exists = (_privateKey != null); - if (!exists) - _privateKey = new PrivateKey(); - _privateKey = (PrivateKey)syncKey(keyFile, _privateKey, exists); + if (exists) + ds = _privateKey; + else + ds = new PrivateKey(); + DataStructure readin = syncKey(keyFile, ds, exists); + if (readin != null && !exists) + _privateKey = (PrivateKey) readin; } + private synchronized void syncPublicKey(File keyDir) { - File keyFile = new File(keyDir, KeyManager.KEYFILE_PUBLIC_ENC); + DataStructure ds; + File keyFile = new File(keyDir, KEYFILE_PUBLIC_ENC); boolean exists = (_publicKey != null); - if (!exists) - _publicKey = new PublicKey(); - _publicKey = (PublicKey)syncKey(keyFile, _publicKey, exists); + if (exists) + ds = _publicKey; + else + ds = new PublicKey(); + DataStructure readin = syncKey(keyFile, ds, exists); + if (readin != null && !exists) + _publicKey = (PublicKey) readin; } private synchronized void syncSigningKey(File keyDir) { - File keyFile = new File(keyDir, KeyManager.KEYFILE_PRIVATE_SIGNING); + DataStructure ds; + File keyFile = new File(keyDir, KEYFILE_PRIVATE_SIGNING); boolean exists = (_signingPrivateKey != null); - if (!exists) - _signingPrivateKey = new SigningPrivateKey(); - _signingPrivateKey = (SigningPrivateKey)syncKey(keyFile, _signingPrivateKey, exists); + if (exists) + ds = _signingPrivateKey; + else + ds = new SigningPrivateKey(); + DataStructure readin = syncKey(keyFile, ds, exists); + if (readin != null && !exists) + _signingPrivateKey = (SigningPrivateKey) readin; } + private synchronized void syncVerificationKey(File keyDir) { - File keyFile = new File(keyDir, KeyManager.KEYFILE_PUBLIC_SIGNING); + DataStructure ds; + File keyFile = new File(keyDir, KEYFILE_PUBLIC_SIGNING); boolean exists = (_signingPublicKey != null); - if (!exists) - _signingPublicKey = new SigningPublicKey(); - _signingPublicKey = (SigningPublicKey)syncKey(keyFile, _signingPublicKey, exists); + if (exists) + ds = _signingPublicKey; + else + ds = new SigningPublicKey(); + DataStructure readin = syncKey(keyFile, ds, exists); + if (readin != null && !exists) + _signingPublicKey = (SigningPublicKey) readin; } private DataStructure syncKey(File keyFile, DataStructure structure, boolean exists) {