[or-cvs] Make tor build on win32 again; handle locking for server
Nick Mathewson
nickm at seul.org
Sat Jun 5 01:50:38 UTC 2004
Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/tmp/cvs-serv12160/src/or
Modified Files:
connection.c cpuworker.c or.h router.c
Log Message:
Make tor build on win32 again; handle locking for server
Index: connection.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection.c,v
retrieving revision 1.230
retrieving revision 1.231
diff -u -d -r1.230 -r1.231
--- connection.c 2 Jun 2004 18:32:24 -0000 1.230
+++ connection.c 5 Jun 2004 01:50:35 -0000 1.231
@@ -333,7 +333,7 @@
memset(&bindaddr,0,sizeof(struct sockaddr_in));
bindaddr.sin_family = AF_INET;
- bindaddr.sin_port = htons(usePort);
+ bindaddr.sin_port = htons((uint16_t) usePort);
if(tor_lookup_hostname(hostname, &(bindaddr.sin_addr.s_addr)) != 0) {
log_fn(LOG_WARN,"Can't resolve BindAddress %s",hostname);
tor_free(hostname);
Index: cpuworker.c
===================================================================
RCS file: /home/or/cvsroot/src/or/cpuworker.c,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -d -r1.42 -r1.43
--- cpuworker.c 3 Jun 2004 05:57:27 -0000 1.42
+++ cpuworker.c 5 Jun 2004 01:50:35 -0000 1.43
@@ -201,11 +201,8 @@
#ifndef MS_WINDOWS
connection_free_all(); /* so the child doesn't hold the parent's fd's open */
#endif
-
- /* XXXX WINDOWS lock here. */
- onion_key = crypto_pk_dup_key(get_onion_key());
- if (get_previous_onion_key())
- last_onion_key = crypto_pk_dup_key(get_previous_onion_key());
+
+ dup_onion_keys(&onion_key, &last_onion_key);
for(;;) {
Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.363
retrieving revision 1.364
diff -u -d -r1.363 -r1.364
--- or.h 2 Jun 2004 20:00:57 -0000 1.363
+++ or.h 5 Jun 2004 01:50:35 -0000 1.364
@@ -1278,7 +1278,8 @@
crypto_pk_env_t *get_previous_onion_key(void);
time_t get_onion_key_set_at(void);
void set_identity_key(crypto_pk_env_t *k);
-crypto_pk_env_t *get_identity_key(void);
+crypto_pk_env_t *get_identity_key(void);
+void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last);
int init_keys(void);
crypto_pk_env_t *init_key_from_file(const char *fname);
void rotate_onion_key(void);
Index: router.c
===================================================================
RCS file: /home/or/cvsroot/src/or/router.c,v
retrieving revision 1.45
retrieving revision 1.46
diff -u -d -r1.45 -r1.46
--- router.c 2 Jun 2004 20:15:35 -0000 1.45
+++ router.c 5 Jun 2004 01:50:35 -0000 1.46
@@ -21,7 +21,8 @@
*****/
/** Private keys for this OR. There is also an SSL key managed by tortls.c.
- */
+ */
+static tor_mutex_t *key_lock=NULL;
static time_t onionkey_set_at=0; /* When was onionkey last changed? */
static crypto_pk_env_t *onionkey=NULL;
static crypto_pk_env_t *lastonionkey=NULL;
@@ -30,9 +31,11 @@
/** Replace the current onion key with <b>k</b>. Does not affect lastonionkey;
* to update onionkey correctly, call rotate_onion_key().
*/
-void set_onion_key(crypto_pk_env_t *k) {
+void set_onion_key(crypto_pk_env_t *k) {
+ tor_mutex_acquire(key_lock);
onionkey = k;
- onionkey_set_at = time(NULL);
+ onionkey_set_at = time(NULL);
+ tor_mutex_release(key_lock);
}
/** Return the current onion key. Requires that the onion key has been
@@ -40,7 +43,7 @@
crypto_pk_env_t *get_onion_key(void) {
tor_assert(onionkey);
return onionkey;
-}
+}
/** Return the onion key that was current before the most recent onion
* key rotation. If no rotation has been performed since this process
@@ -48,6 +51,18 @@
*/
crypto_pk_env_t *get_previous_onion_key(void) {
return lastonionkey;
+}
+
+void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last)
+{
+ tor_assert(key && last);
+ tor_mutex_acquire(key_lock);
+ *key = crypto_pk_dup_key(onionkey);
+ if (lastonionkey)
+ *last = crypto_pk_dup_key(lastonionkey);
+ else
+ *last = NULL;
+ tor_mutex_release(key_lock);
}
/** Return the time when the onion key was last set. This is either the time
@@ -95,14 +110,14 @@
if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
log(LOG_ERR, "Couldn't write generated key to %s.", fname);
goto error;
- }
+ }
+ tor_mutex_acquire(key_lock);
if (lastonionkey)
crypto_free_pk_env(lastonionkey);
- /* XXXX WINDOWS on windows, we need to protect this next bit with a lock.
- */
log_fn(LOG_INFO, "Rotating onion key");
lastonionkey = onionkey;
- set_onion_key(prkey);
+ set_onion_key(prkey);
+ tor_mutex_release(key_lock);
return;
error:
log_fn(LOG_WARN, "Couldn't rotate onion key.");
@@ -170,6 +185,9 @@
char *cp;
const char *tmp, *mydesc;
crypto_pk_env_t *prkey;
+
+ if (!key_lock)
+ key_lock = tor_mutex_new();
/* OP's don't need keys. Just initialize the TLS context.*/
if (!options.ORPort) {
@@ -418,7 +436,7 @@
ri->socks_port = options.SocksPort;
ri->dir_port = options.DirPort;
ri->published_on = time(NULL);
- ri->onion_pkey = crypto_pk_dup_key(get_onion_key());
+ ri->onion_pkey = crypto_pk_dup_key(get_onion_key()); /* must invoke from main thread */
ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
get_platform_str(platform, sizeof(platform));
ri->platform = tor_strdup(platform);
More information about the tor-commits
mailing list