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

[or-cvs] r10056: Let the controller specify HOP=%d as an argument to ATTACHST (in tor/trunk: . doc/spec src/or)



Author: arma
Date: 2007-04-30 07:10:45 -0400 (Mon, 30 Apr 2007)
New Revision: 10056

Modified:
   tor/trunk/ChangeLog
   tor/trunk/doc/spec/control-spec.txt
   tor/trunk/src/or/circuitlist.c
   tor/trunk/src/or/circuituse.c
   tor/trunk/src/or/connection_edge.c
   tor/trunk/src/or/control.c
   tor/trunk/src/or/or.h
Log:
Let the controller specify HOP=%d as an argument to ATTACHSTREAM,
so we can exit from the middle of the circuit.


Modified: tor/trunk/ChangeLog
===================================================================
--- tor/trunk/ChangeLog	2007-04-30 11:10:03 UTC (rev 10055)
+++ tor/trunk/ChangeLog	2007-04-30 11:10:45 UTC (rev 10056)
@@ -60,13 +60,17 @@
       documents; authorities do not yet cache them.)  [Partially implements
       proposal 104.]
 
-  o Minor features (other):
-    - Correctly report Windows 95 OSR2 and Windows 98 SE.
-    - More unit tests.
+  o Minor features (controller):
     - Add a new config option __DisablePredictedCircuits designed for
       use by the controller, when we don't want Tor to build any circuits
       preemptively.
+    - Let the controller specify HOP=%d as an argument to ATTACHSTREAM,
+      so we can exit from the middle of the circuit.
 
+  o Minor features (other):
+    - Correctly report Windows 95 OSR2 and Windows 98 SE.
+    - More unit tests.
+
   o Removed features:
     - Removed support for the old binary "version 0" controller protocol.
       This has been deprecated since 0.1.1, and warnings have been issued

Modified: tor/trunk/doc/spec/control-spec.txt
===================================================================
--- tor/trunk/doc/spec/control-spec.txt	2007-04-30 11:10:03 UTC (rev 10055)
+++ tor/trunk/doc/spec/control-spec.txt	2007-04-30 11:10:45 UTC (rev 10056)
@@ -535,7 +535,7 @@
 3.13. ATTACHSTREAM
 
   Sent from the client to the server.  The syntax is:
-     "ATTACHSTREAM" SP StreamID SP CircuitID CRLF
+     "ATTACHSTREAM" SP StreamID SP CircuitID ["HOP=" HopNum] CRLF
 
   This message informs the server that the specified stream should be
   associated with the specified circuit.  Each stream may be associated with
@@ -547,6 +547,9 @@
   If the circuit ID is 0, responsibility for attaching the given stream is
   returned to Tor.
 
+  If HOP=HopNum is specified, Tor will choose the HopNumth hop in the
+  circuit as the exit node, rather than the last node in the circuit.
+
   Tor responds with "250 OK" if it can attach the stream, 552 if the circuit
   or stream didn't exist, or 551 if the stream couldn't be attached for
   another reason.

Modified: tor/trunk/src/or/circuitlist.c
===================================================================
--- tor/trunk/src/or/circuitlist.c	2007-04-30 11:10:03 UTC (rev 10055)
+++ tor/trunk/src/or/circuitlist.c	2007-04-30 11:10:45 UTC (rev 10056)
@@ -844,6 +844,22 @@
   return n;
 }
 
+/** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
+ * aren't that many hops in the list. */
+crypt_path_t *
+circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
+{
+  if (circ && circ->cpath) {
+    crypt_path_t *cpath, *cpath_next = NULL;
+    for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
+      cpath_next = cpath->next;
+      if (--hopnum <= 0)
+        return cpath;
+    }
+  }
+  return NULL;
+}
+
 /** Go through the circuitlist; mark-for-close each circuit that starts
  *  at us but has not yet been used. */
 void

Modified: tor/trunk/src/or/circuituse.c
===================================================================
--- tor/trunk/src/or/circuituse.c	2007-04-30 11:10:03 UTC (rev 10055)
+++ tor/trunk/src/or/circuituse.c	2007-04-30 11:10:45 UTC (rev 10056)
@@ -1101,7 +1101,8 @@
  * circ's cpath.
  */
 static void
-link_apconn_to_circ(edge_connection_t *apconn, origin_circuit_t *circ)
+link_apconn_to_circ(edge_connection_t *apconn, origin_circuit_t *circ,
+                    crypt_path_t *cpath)
 {
   /* add it into the linked list of streams on this circuit */
   log_debug(LD_APP|LD_CIRC, "attaching new conn to circ. n_circ_id %d.",
@@ -1113,10 +1114,14 @@
   /* assert_connection_ok(conn, time(NULL)); */
   circ->p_streams = apconn;
 
-  tor_assert(circ->cpath);
-  tor_assert(circ->cpath->prev);
-  tor_assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
-  apconn->cpath_layer = circ->cpath->prev;
+  if (cpath) { /* we were given one; use it */
+    apconn->cpath_layer = cpath;
+  } else { /* use the last hop in the circuit */
+    tor_assert(circ->cpath);
+    tor_assert(circ->cpath->prev);
+    tor_assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
+    apconn->cpath_layer = circ->cpath->prev;
+  }
 }
 
 /** If an exit wasn't specifically chosen, save the history for future
@@ -1172,7 +1177,8 @@
  * for connection_ap_handshake_attach_circuit. */
 int
 connection_ap_handshake_attach_chosen_circuit(edge_connection_t *conn,
-                                              origin_circuit_t *circ)
+                                              origin_circuit_t *circ,
+                                              crypt_path_t *cpath)
 {
   tor_assert(conn);
   tor_assert(conn->_base.state == AP_CONN_STATE_CIRCUIT_WAIT ||
@@ -1186,7 +1192,7 @@
   if (!circ->_base.timestamp_dirty)
     circ->_base.timestamp_dirty = time(NULL);
 
-  link_apconn_to_circ(conn, circ);
+  link_apconn_to_circ(conn, circ, cpath);
   tor_assert(conn->socks_request);
   switch (conn->socks_request->command) {
     case SOCKS_COMMAND_CONNECT:
@@ -1269,7 +1275,7 @@
     circuit_log_path(LOG_INFO,LD_APP|LD_CIRC,circ);
 
     /* We have found a suitable circuit for our conn. Hurray. */
-    return connection_ap_handshake_attach_chosen_circuit(conn, circ);
+    return connection_ap_handshake_attach_chosen_circuit(conn, circ, NULL);
 
   } else { /* we're a rendezvous conn */
     origin_circuit_t *rendcirc=NULL, *introcirc=NULL;
@@ -1295,7 +1301,7 @@
        * feasibility, at this point.
        */
       rendcirc->_base.timestamp_dirty = time(NULL);
-      link_apconn_to_circ(conn, rendcirc);
+      link_apconn_to_circ(conn, rendcirc, NULL);
       if (connection_ap_handshake_send_begin(conn) < 0)
         return 0; /* already marked, let them fade away */
       return 1;

Modified: tor/trunk/src/or/connection_edge.c
===================================================================
--- tor/trunk/src/or/connection_edge.c	2007-04-30 11:10:03 UTC (rev 10055)
+++ tor/trunk/src/or/connection_edge.c	2007-04-30 11:10:45 UTC (rev 10056)
@@ -1181,7 +1181,8 @@
  */
 int
 connection_ap_handshake_rewrite_and_attach(edge_connection_t *conn,
-                                           origin_circuit_t *circ)
+                                           origin_circuit_t *circ,
+                                           crypt_path_t *cpath)
 {
   socks_request_t *socks = conn->socks_request;
   hostname_type_t addresstype;
@@ -1337,8 +1338,8 @@
       tor_fragile_assert();
     }
     conn->_base.state = AP_CONN_STATE_CIRCUIT_WAIT;
-    if ((circ &&
-         connection_ap_handshake_attach_chosen_circuit(conn, circ) < 0) ||
+    if ((circ && connection_ap_handshake_attach_chosen_circuit(
+                   conn, circ, cpath) < 0) ||
         (!circ &&
          connection_ap_handshake_attach_circuit(conn) < 0)) {
       connection_mark_unattached_ap(conn, END_STREAM_REASON_CANT_ATTACH);
@@ -1583,7 +1584,7 @@
     conn->_base.state = AP_CONN_STATE_CONTROLLER_WAIT;
     return 0;
   }
-  return connection_ap_handshake_rewrite_and_attach(conn, NULL);
+  return connection_ap_handshake_rewrite_and_attach(conn, NULL, NULL);
 }
 
 /** connection_init_accepted_conn() found a new trans AP conn.
@@ -1625,7 +1626,7 @@
     conn->_base.state = AP_CONN_STATE_CONTROLLER_WAIT;
     return 0;
   }
-  return connection_ap_handshake_rewrite_and_attach(conn, NULL);
+  return connection_ap_handshake_rewrite_and_attach(conn, NULL, NULL);
 }
 
 /** connection_edge_process_inbuf() found a conn in state natd_wait. See if
@@ -1704,7 +1705,7 @@
   }
   conn->_base.state = AP_CONN_STATE_CIRCUIT_WAIT;
 
-  return connection_ap_handshake_rewrite_and_attach(conn, NULL);
+  return connection_ap_handshake_rewrite_and_attach(conn, NULL, NULL);
 }
 
 /** Iterate over the two bytes of stream_id until we get one that is not

Modified: tor/trunk/src/or/control.c
===================================================================
--- tor/trunk/src/or/control.c	2007-04-30 11:10:03 UTC (rev 10055)
+++ tor/trunk/src/or/control.c	2007-04-30 11:10:45 UTC (rev 10056)
@@ -1897,6 +1897,8 @@
   origin_circuit_t *circ = NULL;
   int zero_circ;
   smartlist_t *args;
+  crypt_path_t *cpath=NULL;
+  int hop=0, hop_line_ok=1;
   (void) len;
 
   args = getargs_helper("ATTACHSTREAM", conn, body, 2, -1);
@@ -1911,10 +1913,20 @@
   } else if (!zero_circ && !(circ = get_circ(smartlist_get(args, 1)))) {
     connection_printf_to_buf(conn, "552 Unknown circuit \"%s\"\r\n",
                              (char*)smartlist_get(args, 1));
+  } else if (circ && smartlist_len(args) > 2) {
+    char *hopstring = smartlist_get(args, 2);
+    if (!strcasecmpstart(hopstring, "HOP=")) {
+      hopstring += strlen("HOP=");
+      hop = tor_parse_ulong(hopstring, 10, 0, ULONG_MAX,
+                            &hop_line_ok, NULL);
+      if (!hop_line_ok) { /* broken hop line */
+        connection_printf_to_buf(conn, "552 Bad value hop=%s\r\n", hopstring);
+      }
+    }
   }
   SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
   smartlist_free(args);
-  if (!ap_conn || (!zero_circ && !circ))
+  if (!ap_conn || (!zero_circ && !circ) || !hop_line_ok)
     return 0;
 
   if (ap_conn->_base.state != AP_CONN_STATE_CONTROLLER_WAIT &&
@@ -1940,16 +1952,25 @@
 
   if (circ && (circ->_base.state != CIRCUIT_STATE_OPEN)) {
     connection_write_str_to_buf(
-                    "551 Can't attach stream to non-open, origin circuit\r\n",
+                    "551 Can't attach stream to non-open origin circuit\r\n",
                     conn);
     return 0;
   }
-  if (circ && circuit_get_cpath_len(circ) < 2) {
+  if (circ && (circuit_get_cpath_len(circ)<2 || hop==1)) {
     connection_write_str_to_buf(
                     "551 Can't attach stream to one-hop circuit.\r\n", conn);
     return 0;
   }
-  if (connection_ap_handshake_rewrite_and_attach(ap_conn, circ) < 0) {
+  if (circ && hop>0) {
+    /* find this hop in the circuit, and set cpath */
+    cpath = circuit_get_cpath_hop(circ, hop);
+    if (!cpath) {
+      connection_printf_to_buf(conn,
+                               "551 Circuit doesn't have %d hops.\r\n", hop);
+      return 0;
+    }
+  }
+  if (connection_ap_handshake_rewrite_and_attach(ap_conn, circ, cpath) < 0) {
     connection_write_str_to_buf("551 Unable to attach stream\r\n", conn);
     return 0;
   }

Modified: tor/trunk/src/or/or.h
===================================================================
--- tor/trunk/src/or/or.h	2007-04-30 11:10:03 UTC (rev 10055)
+++ tor/trunk/src/or/or.h	2007-04-30 11:10:45 UTC (rev 10056)
@@ -2085,6 +2085,7 @@
 void _circuit_mark_for_close(circuit_t *circ, int reason,
                              int line, const char *file);
 int circuit_get_cpath_len(origin_circuit_t *circ);
+crypt_path_t *circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum);
 void circuit_get_all_pending_on_or_conn(smartlist_t *out,
                                         or_connection_t *or_conn);
 int circuit_count_pending_on_or_conn(or_connection_t *or_conn);
@@ -2127,7 +2128,8 @@
                                     int is_internal);
 void circuit_reset_failure_count(int timeout);
 int connection_ap_handshake_attach_chosen_circuit(edge_connection_t *conn,
-                                                  origin_circuit_t *circ);
+                                                  origin_circuit_t *circ,
+                                                  crypt_path_t *cpath);
 int connection_ap_handshake_attach_circuit(edge_connection_t *conn);
 
 /********************************* command.c ***************************/
@@ -2322,7 +2324,8 @@
 void addressmap_get_mappings(smartlist_t *sl, time_t min_expires,
                              time_t max_expires);
 int connection_ap_handshake_rewrite_and_attach(edge_connection_t *conn,
-                                               origin_circuit_t *circ);
+                                               origin_circuit_t *circ,
+                                               crypt_path_t *cpath);
 
 void set_exit_redirects(smartlist_t *lst);
 typedef enum hostname_type_t {