[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Resolve a FIXME: use identity comparison, not nickname comp...



Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/tmp/cvs-serv19477/src/or

Modified Files:
	circuitbuild.c command.c connection_or.c or.h 
Log Message:
Resolve a FIXME: use identity comparison, not nickname comparison, to
choose circuit ID types.  This is important because our view of "the
nickname of the router on the other side of this connection" is
skewed, and depends on whether we think the other rotuer is
verified--and there's no way to know whether another router thinks you
are verified.

For backward compatibility, we notice when the other router chooses
the same circuit ID type as us (because it's running an old version),
and switch our type to be polite.


Index: circuitbuild.c
===================================================================
RCS file: /home/or/cvsroot/src/or/circuitbuild.c,v
retrieving revision 1.53
retrieving revision 1.54
diff -u -d -r1.53 -r1.54
--- circuitbuild.c	10 Nov 2004 14:27:26 -0000	1.53
+++ circuitbuild.c	10 Nov 2004 20:14:37 -0000	1.54
@@ -35,14 +35,14 @@
  *
  * Return it, or 0 if can't get a unique circ_id.
  */
-static uint16_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type) {
+static uint16_t get_unique_circ_id_by_conn(connection_t *conn) {
   uint16_t test_circ_id;
   int attempts=0;
   uint16_t high_bit;
 
   tor_assert(conn);
   tor_assert(conn->type == CONN_TYPE_OR);
-  high_bit = (circ_id_type == CIRC_ID_TYPE_HIGHER) ? 1<<15 : 0;
+  high_bit = (conn->circ_id_type == CIRC_ID_TYPE_HIGHER) ? 1<<15 : 0;
   do {
     /* Sequentially iterate over test_circ_id=1...1<<15-1 until we find a
      * circID such that (high_bit|test_circ_id) is not already used. */
@@ -359,9 +359,7 @@
    * Solution: switch to identity-based comparison, but if we get
    * any circuits in the wrong half of the space, switch.
    */
-  circ_id_type = decide_circ_id_type(get_options()->Nickname,
-                                     circ->n_conn->nickname);
-  circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
+  circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn);
   if(!circ->n_circ_id) {
     log_fn(LOG_WARN,"failed to get unique circID.");
     return -1;
@@ -697,27 +695,6 @@
 #endif
 }
 
-/** Decide whether the first bit of the circuit ID will be
- * 0 or 1, to avoid conflicts where each side randomly chooses
- * the same circuit ID.
- *
- * Return CIRC_ID_TYPE_LOWER if local_nick is NULL, or if
- * local_nick is lexographically smaller than remote_nick.
- * Else return CIRC_ID_TYPE_HIGHER.
- */
-static int decide_circ_id_type(char *local_nick, char *remote_nick) {
-  int result;
-
-  tor_assert(remote_nick);
-  if(!local_nick)
-    return CIRC_ID_TYPE_LOWER;
-  result = strcasecmp(local_nick, remote_nick);
-  tor_assert(result);
-  if(result < 0)
-    return CIRC_ID_TYPE_LOWER;
-  return CIRC_ID_TYPE_HIGHER;
-}
-
 /** Given a response payload and keys, initialize, then send a created
  * cell back.
  */

Index: command.c
===================================================================
RCS file: /home/or/cvsroot/src/or/command.c,v
retrieving revision 1.71
retrieving revision 1.72
diff -u -d -r1.71 -r1.72
--- command.c	7 Nov 2004 01:33:05 -0000	1.71
+++ command.c	10 Nov 2004 20:14:37 -0000	1.72
@@ -139,6 +139,22 @@
     return;
   }
 
+  /* If the high bit of the circuit ID is not as expected, then switch
+   * which half of the space we'll use for our own CREATE cells.
+   *
+   * This can happen because Tor 0.0.9pre5 and earlier decide which
+   * half to use based on nickname, and we now use identity keys.
+   */
+  if ((cell->circ_id & (1<<15)) && conn->circ_id_type == CIRC_ID_TYPE_HIGHER) {
+    log_fn(LOG_INFO, "Got a high circuit ID from %s (%d); switching to low circuit IDs.",
+           conn->nickname, conn->s);
+    conn->circ_id_type = CIRC_ID_TYPE_LOWER;
+  } else if (!(cell->circ_id & (1<<15)) && conn->circ_id_type == CIRC_ID_TYPE_LOWER) {
+    log_fn(LOG_INFO, "Got a low circuit ID from %s (%d); switching to high circuit IDs.",
+           conn->nickname, conn->s);
+    conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
+  }
+
   circ = circuit_new(cell->circ_id, conn);
   circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
   circ->purpose = CIRCUIT_PURPOSE_OR;

Index: connection_or.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_or.c,v
retrieving revision 1.135
retrieving revision 1.136
diff -u -d -r1.135 -r1.136
--- connection_or.c	7 Nov 2004 01:33:06 -0000	1.135
+++ connection_or.c	10 Nov 2004 20:14:37 -0000	1.136
@@ -339,7 +339,7 @@
   conn->state = OR_CONN_STATE_OPEN;
   connection_watch_events(conn, POLLIN);
   log_fn(LOG_DEBUG,"tls handshake done. verifying.");
-  if (! tor_tls_peer_has_cert(conn->tls)) { /* It's an OP. */
+  if (! tor_tls_peer_has_cert(conn->tls)) { /* It's an old OP. */
     if (server_mode(options)) { /* I'm an OR; good. */
       conn->receiver_bucket = conn->bandwidth = DEFAULT_BANDWIDTH_OP;
       return 0;
@@ -348,7 +348,7 @@
       return -1;
     }
   }
-  /* Okay; the other side is an OR. */
+  /* Okay; the other side is an OR or a post-0.0.8 OP (with a cert). */
   if (tor_tls_get_peer_cert_nickname(conn->tls, nickname, MAX_NICKNAME_LEN)) {
     log_fn(LOG_WARN,"Other side (%s:%d) has a cert without a valid nickname. Closing.",
            conn->address, conn->port);
@@ -366,6 +366,12 @@
   crypto_pk_get_digest(identity_rcvd, digest_rcvd);
   crypto_free_pk_env(identity_rcvd);
 
+  if (crypto_pk_cmp_keys(get_identity_key(), identity_rcvd)<0) {
+    conn->circ_id_type = CIRC_ID_TYPE_LOWER;
+  } else {
+    conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
+  }
+
   router = router_get_by_nickname(nickname);
   if(router && /* we know this nickname */
      router->is_verified && /* make sure it's the right guy */
@@ -394,6 +400,7 @@
   if (!server_mode(options)) { /* If I'm an OP... */
     conn->receiver_bucket = conn->bandwidth = DEFAULT_BANDWIDTH_OP;
   }
+
   directory_set_dirty();
   circuit_n_conn_done(conn, 1); /* send the pending creates, if any. */
   /* Note the success */

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.476
retrieving revision 1.477
diff -u -d -r1.476 -r1.477
--- or.h	10 Nov 2004 14:28:04 -0000	1.476
+++ or.h	10 Nov 2004 20:14:37 -0000	1.477
@@ -150,8 +150,10 @@
  * In seconds. */
 #define ROUTER_MAX_AGE (60*60*24)
 
-#define CIRC_ID_TYPE_LOWER 0
-#define CIRC_ID_TYPE_HIGHER 1
+typedef enum {
+  CIRC_ID_TYPE_LOWER=0,
+  CIRC_ID_TYPE_HIGHER=1
+} circ_id_type_t;
 
 #define _CONN_TYPE_MIN 3
 /** Type for sockets listening for OR connections. */
@@ -534,6 +536,9 @@
                         * add 'bandwidth' to this, capping it at 10*bandwidth.
                         * (OPEN ORs only)
                         */
+  circ_id_type_t circ_id_type; /**< When we send CREATE cells along this
+                                * connection, which half of the space should
+                                * we use? */
 
 /* Used only by DIR and AP connections: */
   char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we