[or-cvs] r9350: Expire socks connections if they spend too long waiting for (in tor/trunk: . doc src/or)

arma at seul.org arma at seul.org
Mon Jan 15 09:10:31 UTC 2007


Author: arma
Date: 2007-01-15 04:09:03 -0500 (Mon, 15 Jan 2007)
New Revision: 9350

Modified:
   tor/trunk/ChangeLog
   tor/trunk/doc/tor.1.in
   tor/trunk/src/or/circuituse.c
   tor/trunk/src/or/connection_edge.c
   tor/trunk/src/or/or.h
Log:
Expire socks connections if they spend too long waiting for the
handshake to finish. Previously we would let them sit around for
days, if the connecting application didn't close them either.

Also take this opportunity to refactor a duplicate bit of circuituse.c.

And change the semantics of SocksTimeout slightly, but I think it'll
be ok.


Modified: tor/trunk/ChangeLog
===================================================================
--- tor/trunk/ChangeLog	2007-01-15 08:52:01 UTC (rev 9349)
+++ tor/trunk/ChangeLog	2007-01-15 09:09:03 UTC (rev 9350)
@@ -20,6 +20,9 @@
       indefinitely, if they came from nontrusted authorities.  Now we
       discard them if they are more than 10 days old.
     - Fix crash with "tor --list-fingerprint" (reported by seeess).
+    - Expire socks connections if they spend too long waiting for the
+      handshake to finish. Previously we would let them sit around for
+      days, if the connecting application didn't close them either.
 
 
 Changes in version 0.1.2.6-alpha - 2007-01-09

Modified: tor/trunk/doc/tor.1.in
===================================================================
--- tor/trunk/doc/tor.1.in	2007-01-15 08:52:01 UTC (rev 9349)
+++ tor/trunk/doc/tor.1.in	2007-01-15 09:09:03 UTC (rev 9350)
@@ -461,7 +461,8 @@
 .LP
 .TP
 \fBSocksTimeout \fR\fINUM\fP
-Let a socks connection wait NUM seconds unattached before we fail it.
+Let a socks connection wait NUM seconds handshaking, and NUM seconds
+unattached waiting for an appropriate circuit, before we fail it.
 (Default: 2 minutes.)
 .LP
 .TP

Modified: tor/trunk/src/or/circuituse.c
===================================================================
--- tor/trunk/src/or/circuituse.c	2007-01-15 08:52:01 UTC (rev 9349)
+++ tor/trunk/src/or/circuituse.c	2007-01-15 09:09:03 UTC (rev 9350)
@@ -532,7 +532,7 @@
 }
 
 /** Find each circuit that has been unused for too long, or dirty
- * for too long and has no streax=ms on it: mark it for close.
+ * for too long and has no streams on it: mark it for close.
  */
 static void
 circuit_expire_old_circuits(time_t now)
@@ -1180,21 +1180,12 @@
 {
   int retval;
   int conn_age;
-  int severity;
 
   tor_assert(conn);
   tor_assert(conn->_base.state == AP_CONN_STATE_CIRCUIT_WAIT);
   tor_assert(conn->socks_request);
 
   conn_age = time(NULL) - conn->_base.timestamp_created;
-  severity = (!conn->_base.addr && !conn->_base.port) ? LOG_INFO : LOG_NOTICE;
-  if (conn_age > get_options()->SocksTimeout) {
-    log_fn(severity, LD_APP,
-           "Tried for %d seconds to get a connection to %s:%d. Giving up.",
-           conn_age, safe_str(conn->socks_request->address),
-           conn->socks_request->port);
-    return -1;
-  }
 
   if (!connection_edge_is_rendezvous_stream(conn)) { /* we're a general conn */
     origin_circuit_t *circ=NULL;

Modified: tor/trunk/src/or/connection_edge.c
===================================================================
--- tor/trunk/src/or/connection_edge.c	2007-01-15 08:52:01 UTC (rev 9349)
+++ tor/trunk/src/or/connection_edge.c	2007-01-15 09:09:03 UTC (rev 9350)
@@ -353,6 +353,7 @@
   or_options_t *options = get_options();
   int severity;
   int cutoff;
+  int seconds_idle;
 
   get_connection_array(&carray, &n);
 
@@ -363,19 +364,28 @@
     /* if it's an internal bridge connection, don't yell its status. */
     severity = (!conn->_base.addr && !conn->_base.port)
       ? LOG_INFO : LOG_NOTICE;
-    if (conn->_base.state == AP_CONN_STATE_CONTROLLER_WAIT) {
-      if (now - conn->_base.timestamp_lastread >= options->SocksTimeout) {
-        log_fn(severity, LD_APP, "Closing unattached stream.");
+    seconds_idle = now - conn->_base.timestamp_lastread;
+
+    if (AP_CONN_STATE_IS_UNATTACHED(conn->_base.state)) {
+      if (seconds_idle >= options->SocksTimeout) {
+        if (conn->_base.state == AP_CONN_STATE_CIRCUIT_WAIT) {
+          log_fn(severity, LD_APP,
+            "Tried for %d seconds to get a connection to %s:%d. Giving up.",
+            seconds_idle, safe_str(conn->socks_request->address),
+            conn->socks_request->port);
+        } else {
+          log_fn(severity, LD_APP, "Closing unattached stream.");
+        }
         connection_mark_unattached_ap(conn, END_STREAM_REASON_TIMEOUT);
       }
       continue;
     }
 
-    if (conn->_base.state != AP_CONN_STATE_RESOLVE_WAIT &&
-        conn->_base.state != AP_CONN_STATE_CONNECT_WAIT)
+    if (conn->_base.state == AP_CONN_STATE_OPEN)
       continue;
+
     cutoff = compute_socks_timeout(conn);
-    if (now - conn->_base.timestamp_lastread < cutoff)
+    if (seconds_idle < cutoff)
       continue;
     circ = circuit_get_by_edge_conn(conn);
     if (!circ) { /* it's vanished? */
@@ -385,11 +395,11 @@
       continue;
     }
     if (circ->purpose == CIRCUIT_PURPOSE_C_REND_JOINED) {
-      if (now - conn->_base.timestamp_lastread > options->SocksTimeout) {
+      if (seconds_idle > options->SocksTimeout) {
         log_fn(severity, LD_REND,
                "Rend stream is %d seconds late. Giving up on address"
                " '%s.onion'.",
-               (int)(now - conn->_base.timestamp_lastread),
+               seconds_idle,
                safe_str(conn->socks_request->address));
         connection_edge_end(conn, END_STREAM_REASON_TIMEOUT,
                             conn->cpath_layer);
@@ -403,8 +413,7 @@
     log_fn(cutoff < 15 ? LOG_INFO : severity, LD_APP,
            "We tried for %d seconds to connect to '%s' using exit '%s'."
            " Retrying on a new circuit.",
-           (int)(now - conn->_base.timestamp_lastread),
-           safe_str(conn->socks_request->address),
+           seconds_idle, safe_str(conn->socks_request->address),
            nickname ? nickname : "*unnamed*");
     /* send an end down the circuit */
     connection_edge_end(conn, END_STREAM_REASON_TIMEOUT, conn->cpath_layer);

Modified: tor/trunk/src/or/or.h
===================================================================
--- tor/trunk/src/or/or.h	2007-01-15 08:52:01 UTC (rev 9349)
+++ tor/trunk/src/or/or.h	2007-01-15 09:09:03 UTC (rev 9350)
@@ -294,7 +294,7 @@
 #define AP_CONN_STATE_CIRCUIT_WAIT 8
 /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
 #define AP_CONN_STATE_CONNECT_WAIT 9
-/** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
+/** State for a SOCKS connection: sent RESOLVE, waiting for RESOLVED. */
 #define AP_CONN_STATE_RESOLVE_WAIT 10
 /** State for a SOCKS connection: ready to send and receive. */
 #define AP_CONN_STATE_OPEN 11
@@ -303,6 +303,9 @@
 #define AP_CONN_STATE_NATD_WAIT 12
 #define _AP_CONN_STATE_MAX 12
 
+#define AP_CONN_STATE_IS_UNATTACHED(s) \
+  ((s) <= AP_CONN_STATE_CIRCUIT_WAIT || (s) == AP_CONN_STATE_NATD_WAIT)
+
 #define _DIR_CONN_STATE_MIN 1
 /** State for connection to directory server: waiting for connect(). */
 #define DIR_CONN_STATE_CONNECTING 1
@@ -501,8 +504,8 @@
 #define END_STREAM_REASON_CANT_FETCH_ORIG_DEST 260
 #define END_STREAM_REASON_INVALID_NATD_DEST 261
 
-/* OR this with the argument to control_event_stream_status to indicate that
- * the reason came from an END cell. */
+/** Bitwise-or this with the argument to control_event_stream_status
+ * to indicate that the reason came from an END cell. */
 #define END_STREAM_REASON_FLAG_REMOTE     512
 
 #define RESOLVED_TYPE_HOSTNAME 0



More information about the tor-commits mailing list