Ticket #15482: 0001-Bug-15482-Don-t-abandon-circuits-that-are-still-in-u.patch

File 0001-Bug-15482-Don-t-abandon-circuits-that-are-still-in-u.patch, 3.4 KB (added by mikeperry, 4 years ago)

This version also extends the lifetime upon stream detach.

  • src/or/circuituse.c

    From 4375db5e815f2e343215d90d521a1f7ef9995b3e Mon Sep 17 00:00:00 2001
    From: Mike Perry <mikeperry-git@torproject.org>
    Date: Fri, 27 Mar 2015 12:57:37 -0700
    Subject: [PATCH] Bug 15482: Don't abandon circuits that are still in use for
     browsing.
    
    This patch extends the circuit lifespan while circuits are still in active
    use.
    
    Only applies to connections with SOCKS auth set, so that non-web Tor
    activity is not affected.
    ---
     src/or/circuituse.c | 41 ++++++++++++++++++++++++++++++++++++++---
     1 file changed, 38 insertions(+), 3 deletions(-)
    
    diff --git a/src/or/circuituse.c b/src/or/circuituse.c
    index d0d31ad..5aaedaa 100644
    a b void 
    11761176circuit_detach_stream(circuit_t *circ, edge_connection_t *conn)
    11771177{
    11781178  edge_connection_t *prevconn;
     1179  entry_connection_t *entry_conn = NULL;
    11791180
    11801181  tor_assert(circ);
    11811182  tor_assert(conn);
    11821183
    11831184  if (conn->base_.type == CONN_TYPE_AP) {
    1184     entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
     1185    entry_conn = EDGE_TO_ENTRY_CONN(conn);
    11851186    entry_conn->may_use_optimistic_data = 0;
    11861187  }
    11871188  conn->cpath_layer = NULL; /* don't keep a stale pointer */
    circuit_detach_stream(circuit_t *circ, edge_connection_t *conn) 
    11891190
    11901191  if (CIRCUIT_IS_ORIGIN(circ)) {
    11911192    origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
     1193
     1194    if (entry_conn &&
     1195            (entry_conn->entry_cfg.isolation_flags & ISO_SOCKSAUTH) &&
     1196            (entry_conn->socks_request->usernamelen ||
     1197             entry_conn->socks_request->passwordlen)) {
     1198        /* When stream isolation is in use and controlled by an application,
     1199         * we are willing to keep using the circuit for new connections for a
     1200         * bit longer in case new streams arrive with the same SOCKS u+p */
     1201        time_t extra_time = approx_time() -
     1202            crypto_rand_int((int)get_options()->MaxCircuitDirtiness/2);
     1203        circ->timestamp_dirty = MAX(circ->timestamp_dirty, extra_time);
     1204        log_debug(LD_CIRC,
     1205                "SOCKSAUTH stream %lu detached. "
     1206                "Circ %u will live for ~%d more secs",
     1207                 conn->base_.global_identifier,
     1208                 origin_circ->global_identifier,
     1209                 (int)(get_options()->MaxCircuitDirtiness +
     1210                 circ->timestamp_dirty - approx_time()));
     1211    }
     1212
    11921213    if (conn == origin_circ->p_streams) {
    11931214      origin_circ->p_streams = conn->next_stream;
    11941215      return;
    connection_ap_handshake_attach_chosen_circuit(entry_connection_t *conn, 
    22642285
    22652286  base_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
    22662287
    2267   if (!circ->base_.timestamp_dirty)
    2268     circ->base_.timestamp_dirty = time(NULL);
     2288  if (!circ->base_.timestamp_dirty) {
     2289    circ->base_.timestamp_dirty = approx_time();
     2290  } else if ((conn->entry_cfg.isolation_flags & ISO_SOCKSAUTH) &&
     2291             (conn->socks_request->usernamelen ||
     2292              conn->socks_request->passwordlen)) {
     2293    /* When stream isolation is in use and controlled by an application
     2294     * we are willing to keep using the circuit. */
     2295    circ->base_.timestamp_dirty = approx_time();
     2296    log_debug(LD_CIRC,
     2297            "New SOCKSAUTH stream %lu attached. "
     2298            "Circ %u will live for ~%d more secs",
     2299            base_conn->global_identifier,
     2300            circ->global_identifier,
     2301            (int)(get_options()->MaxCircuitDirtiness +
     2302            approx_time() - circ->base_.timestamp_dirty));
     2303  }
    22692304
    22702305  pathbias_count_use_attempt(circ);
    22712306