2006-02-09 06:46:49 +01:00
|
|
|
/* Copyright (c) 2001 Matej Pfajfar.
|
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
2007-12-12 22:09:01 +01:00
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2012-06-05 02:58:17 +02:00
|
|
|
* Copyright (c) 2007-2012, The Tor Project, Inc. */
|
2004-05-13 09:24:49 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file circuituse.c
|
2005-06-11 07:31:17 +02:00
|
|
|
* \brief Launch the right sort of circuits and attach streams to them.
|
2004-05-13 09:24:49 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
#include "or.h"
|
2012-10-28 21:13:58 +01:00
|
|
|
#include "addressmap.h"
|
2012-08-08 15:02:47 +02:00
|
|
|
#include "channel.h"
|
2010-07-22 01:21:00 +02:00
|
|
|
#include "circuitbuild.h"
|
2010-07-22 09:46:23 +02:00
|
|
|
#include "circuitlist.h"
|
2012-10-15 20:48:34 +02:00
|
|
|
#include "circuitstats.h"
|
2010-07-22 10:03:40 +02:00
|
|
|
#include "circuituse.h"
|
2010-07-22 10:22:51 +02:00
|
|
|
#include "config.h"
|
2010-07-22 10:32:52 +02:00
|
|
|
#include "connection.h"
|
2010-07-22 10:43:02 +02:00
|
|
|
#include "connection_edge.h"
|
2010-07-22 11:35:09 +02:00
|
|
|
#include "control.h"
|
2012-10-15 20:48:34 +02:00
|
|
|
#include "entrynodes.h"
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
#include "nodelist.h"
|
2011-07-20 16:38:00 +02:00
|
|
|
#include "networkstatus.h"
|
2010-07-23 20:51:25 +02:00
|
|
|
#include "policies.h"
|
2010-07-22 00:13:51 +02:00
|
|
|
#include "rendclient.h"
|
2010-07-21 17:52:54 +02:00
|
|
|
#include "rendcommon.h"
|
2010-07-22 00:30:17 +02:00
|
|
|
#include "rendservice.h"
|
2010-07-23 22:57:20 +02:00
|
|
|
#include "rephist.h"
|
2010-07-21 16:17:10 +02:00
|
|
|
#include "router.h"
|
2010-07-21 17:08:11 +02:00
|
|
|
#include "routerlist.h"
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/********* START VARIABLES **********/
|
|
|
|
|
|
|
|
extern circuit_t *global_circuitlist; /* from circuitlist.c */
|
|
|
|
|
|
|
|
/********* END VARIABLES ************/
|
|
|
|
|
2011-03-26 06:34:42 +01:00
|
|
|
static void circuit_expire_old_circuits_clientside(void);
|
2004-05-13 09:24:49 +02:00
|
|
|
static void circuit_increment_failure_count(void);
|
|
|
|
|
2007-02-02 21:06:43 +01:00
|
|
|
/** Return 1 if <b>circ</b> could be returned by circuit_get_best().
|
2004-05-13 09:24:49 +02:00
|
|
|
* Else return 0.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
2011-07-06 22:30:02 +02:00
|
|
|
circuit_is_acceptable(const origin_circuit_t *origin_circ,
|
2011-07-20 18:55:42 +02:00
|
|
|
const entry_connection_t *conn,
|
2005-11-25 09:08:56 +01:00
|
|
|
int must_be_open, uint8_t purpose,
|
|
|
|
int need_uptime, int need_internal,
|
2005-06-11 20:52:12 +02:00
|
|
|
time_t now)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
2011-07-06 22:30:02 +02:00
|
|
|
const circuit_t *circ = TO_CIRCUIT(origin_circ);
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const node_t *exitnode;
|
2006-07-23 09:37:35 +02:00
|
|
|
cpath_build_state_t *build_state;
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(conn);
|
|
|
|
tor_assert(conn->socks_request);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
if (must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_chan))
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0; /* ignore non-open circs */
|
|
|
|
if (circ->marked_for_close)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* if this circ isn't our purpose, skip. */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (purpose == CIRCUIT_PURPOSE_C_REND_JOINED && !must_be_open) {
|
|
|
|
if (circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
|
2005-01-19 18:13:11 +01:00
|
|
|
circ->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
|
|
|
|
circ->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED &&
|
|
|
|
circ->purpose != CIRCUIT_PURPOSE_C_REND_JOINED)
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
2005-12-14 21:40:40 +01:00
|
|
|
} else if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT &&
|
|
|
|
!must_be_open) {
|
2004-05-13 09:24:49 +02:00
|
|
|
if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCING &&
|
|
|
|
circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
|
|
|
|
return 0;
|
|
|
|
} else {
|
2004-11-28 10:05:49 +01:00
|
|
|
if (purpose != circ->purpose)
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-23 14:25:17 +01:00
|
|
|
/* If this is a timed-out hidden service circuit, skip it. */
|
|
|
|
if (origin_circ->hs_circ_has_timed_out) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-10 04:56:40 +02:00
|
|
|
if (purpose == CIRCUIT_PURPOSE_C_GENERAL ||
|
|
|
|
purpose == CIRCUIT_PURPOSE_C_REND_JOINED)
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->timestamp_dirty &&
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
circ->timestamp_dirty+get_options()->MaxCircuitDirtiness <= now)
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
/* decide if this circ is suitable for this conn */
|
2004-05-13 09:24:49 +02:00
|
|
|
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
/* for rend circs, circ->cpath->prev is not the last router in the
|
|
|
|
* circuit, it's the magical extra bob hop. so just check the nickname
|
|
|
|
* of the one we meant to finish at.
|
|
|
|
*/
|
2011-07-06 22:30:02 +02:00
|
|
|
build_state = origin_circ->build_state;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
exitnode = build_state_get_exit_node(build_state);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if (need_uptime && !build_state->need_uptime)
|
2005-11-25 09:08:56 +01:00
|
|
|
return 0;
|
2006-07-23 09:37:35 +02:00
|
|
|
if (need_internal != build_state->is_internal)
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
return 0;
|
2005-01-12 05:58:23 +01:00
|
|
|
|
2005-02-02 07:26:52 +01:00
|
|
|
if (purpose == CIRCUIT_PURPOSE_C_GENERAL) {
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
if (!exitnode && !build_state->onehop_tunnel) {
|
2008-04-16 02:12:44 +02:00
|
|
|
log_debug(LD_CIRC,"Not considering circuit with unknown router.");
|
2005-06-29 23:46:55 +02:00
|
|
|
return 0; /* this circuit is screwed and doesn't know it yet,
|
|
|
|
* or is a rendezvous circuit. */
|
|
|
|
}
|
2006-12-13 03:49:45 +01:00
|
|
|
if (build_state->onehop_tunnel) {
|
2007-10-28 09:16:19 +01:00
|
|
|
if (!conn->want_onehop) {
|
2006-12-13 03:49:45 +01:00
|
|
|
log_debug(LD_CIRC,"Skipping one-hop circuit.");
|
|
|
|
return 0;
|
|
|
|
}
|
2007-04-27 12:26:09 +02:00
|
|
|
tor_assert(conn->chosen_exit_name);
|
|
|
|
if (build_state->chosen_exit) {
|
|
|
|
char digest[DIGEST_LEN];
|
2008-06-20 05:13:16 +02:00
|
|
|
if (hexdigest_to_digest(conn->chosen_exit_name, digest) < 0)
|
|
|
|
return 0; /* broken digest, we don't want it */
|
2011-05-10 22:58:38 +02:00
|
|
|
if (tor_memneq(digest, build_state->chosen_exit->identity_digest,
|
2008-06-20 05:13:16 +02:00
|
|
|
DIGEST_LEN))
|
2007-04-27 12:26:09 +02:00
|
|
|
return 0; /* this is a circuit to somewhere else */
|
2008-06-20 05:13:16 +02:00
|
|
|
if (tor_digest_is_zero(digest)) {
|
|
|
|
/* we don't know the digest; have to compare addr:port */
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_t addr;
|
2011-10-11 17:21:31 +02:00
|
|
|
int r = tor_addr_parse(&addr, conn->socks_request->address);
|
2008-08-05 22:08:19 +02:00
|
|
|
if (r < 0 ||
|
|
|
|
!tor_addr_eq(&build_state->chosen_exit->addr, &addr) ||
|
2008-06-20 05:13:16 +02:00
|
|
|
build_state->chosen_exit->port != conn->socks_request->port)
|
|
|
|
return 0;
|
|
|
|
}
|
2007-04-27 12:26:09 +02:00
|
|
|
}
|
2006-12-13 03:49:45 +01:00
|
|
|
} else {
|
2007-10-28 09:16:19 +01:00
|
|
|
if (conn->want_onehop) {
|
2006-12-13 03:49:45 +01:00
|
|
|
/* don't use three-hop circuits -- that could hurt our anonymity. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2011-04-27 20:36:30 +02:00
|
|
|
if (exitnode && !connection_ap_can_use_exit(conn, exitnode)) {
|
2005-02-02 07:26:52 +01:00
|
|
|
/* can't exit from this router */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else { /* not general */
|
2011-07-20 18:55:42 +02:00
|
|
|
const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(conn);
|
|
|
|
if ((edge_conn->rend_data && !origin_circ->rend_data) ||
|
|
|
|
(!edge_conn->rend_data && origin_circ->rend_data) ||
|
|
|
|
(edge_conn->rend_data && origin_circ->rend_data &&
|
|
|
|
rend_cmp_service_ids(edge_conn->rend_data->onion_address,
|
2011-07-06 22:30:02 +02:00
|
|
|
origin_circ->rend_data->onion_address))) {
|
2005-02-02 07:26:52 +01:00
|
|
|
/* this circ is not for this conn */
|
|
|
|
return 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
2011-07-06 23:08:24 +02:00
|
|
|
|
|
|
|
if (!connection_edge_compatible_with_circuit(conn, origin_circ)) {
|
|
|
|
/* conn needs to be isolated from other conns that have already used
|
|
|
|
* origin_circ */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-02-02 21:06:43 +01:00
|
|
|
/** Return 1 if circuit <b>a</b> is better than circuit <b>b</b> for
|
2011-07-06 23:08:24 +02:00
|
|
|
* <b>conn</b>, and return 0 otherwise. Used by circuit_get_best.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
2011-07-06 22:30:02 +02:00
|
|
|
circuit_is_better(const origin_circuit_t *oa, const origin_circuit_t *ob,
|
2011-07-20 18:55:42 +02:00
|
|
|
const entry_connection_t *conn)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
2011-07-06 22:30:02 +02:00
|
|
|
const circuit_t *a = TO_CIRCUIT(oa);
|
|
|
|
const circuit_t *b = TO_CIRCUIT(ob);
|
2011-07-20 18:55:42 +02:00
|
|
|
const uint8_t purpose = ENTRY_TO_CONN(conn)->purpose;
|
2011-07-06 23:08:24 +02:00
|
|
|
int a_bits, b_bits;
|
2011-07-06 22:30:02 +02:00
|
|
|
|
2012-10-26 02:43:10 +02:00
|
|
|
/* If one of the circuits was allowed to live due to relaxing its timeout,
|
|
|
|
* it is definitely worse (it's probably a much slower path). */
|
|
|
|
if (oa->relaxed_timeout && !ob->relaxed_timeout)
|
|
|
|
return 0; /* ob is better. It's not relaxed. */
|
|
|
|
if (!oa->relaxed_timeout && ob->relaxed_timeout)
|
|
|
|
return 1; /* oa is better. It's not relaxed. */
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
switch (purpose) {
|
2004-05-13 09:24:49 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
|
|
|
/* if it's used but less dirty it's best;
|
|
|
|
* else if it's more recently created it's best
|
|
|
|
*/
|
2004-11-28 10:05:49 +01:00
|
|
|
if (b->timestamp_dirty) {
|
|
|
|
if (a->timestamp_dirty &&
|
2004-11-28 12:39:53 +01:00
|
|
|
a->timestamp_dirty > b->timestamp_dirty)
|
2004-05-13 09:24:49 +02:00
|
|
|
return 1;
|
|
|
|
} else {
|
2004-11-28 10:05:49 +01:00
|
|
|
if (a->timestamp_dirty ||
|
2012-10-26 02:43:10 +02:00
|
|
|
timercmp(&a->timestamp_began, &b->timestamp_began, >))
|
2004-05-13 09:24:49 +02:00
|
|
|
return 1;
|
2011-07-06 22:30:02 +02:00
|
|
|
if (ob->build_state->is_internal)
|
2011-04-27 01:55:34 +02:00
|
|
|
/* XXX023 what the heck is this internal thing doing here. I
|
|
|
|
* think we can get rid of it. circuit_is_acceptable() already
|
|
|
|
* makes sure that is_internal is exactly what we need it to
|
|
|
|
* be. -RD */
|
2006-07-23 09:37:35 +02:00
|
|
|
return 1;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
|
|
|
|
/* the closer it is to ack_wait the better it is */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (a->purpose > b->purpose)
|
2004-05-13 09:24:49 +02:00
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_JOINED:
|
|
|
|
/* the closer it is to rend_joined the better it is */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (a->purpose > b->purpose)
|
2004-05-13 09:24:49 +02:00
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
2011-07-06 23:08:24 +02:00
|
|
|
|
|
|
|
/* XXXX023 Maybe this check should get a higher priority to avoid
|
|
|
|
* using up circuits too rapidly. */
|
|
|
|
|
|
|
|
a_bits = connection_edge_update_circuit_isolation(conn,
|
|
|
|
(origin_circuit_t*)oa, 1);
|
|
|
|
b_bits = connection_edge_update_circuit_isolation(conn,
|
|
|
|
(origin_circuit_t*)ob, 1);
|
|
|
|
/* if x_bits < 0, then we have not used x for anything; better not to dirty
|
|
|
|
* a connection if we can help it. */
|
|
|
|
if (a_bits < 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (b_bits < 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
a_bits &= ~ oa->isolation_flags_mixed;
|
|
|
|
a_bits &= ~ ob->isolation_flags_mixed;
|
|
|
|
if (n_bits_set_u8(a_bits) < n_bits_set_u8(b_bits)) {
|
|
|
|
/* The fewer new restrictions we need to make on a circuit for stream
|
|
|
|
* isolation, the better. */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Find the best circ that conn can use, preferably one which is
|
|
|
|
* dirty. Circ must not be too old.
|
|
|
|
*
|
|
|
|
* Conn must be defined.
|
|
|
|
*
|
|
|
|
* If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
|
|
|
|
*
|
|
|
|
* circ_purpose specifies what sort of circuit we must have.
|
|
|
|
* It can be C_GENERAL, C_INTRODUCE_ACK_WAIT, or C_REND_JOINED.
|
|
|
|
*
|
|
|
|
* If it's REND_JOINED and must_be_open==0, then return the closest
|
|
|
|
* rendezvous-purposed circuit that you can find.
|
|
|
|
*
|
|
|
|
* If it's INTRODUCE_ACK_WAIT and must_be_open==0, then return the
|
|
|
|
* closest introduce-purposed circuit that you can find.
|
|
|
|
*/
|
2006-07-23 09:37:35 +02:00
|
|
|
static origin_circuit_t *
|
2011-07-20 18:55:42 +02:00
|
|
|
circuit_get_best(const entry_connection_t *conn,
|
2011-07-06 22:30:02 +02:00
|
|
|
int must_be_open, uint8_t purpose,
|
2005-11-25 09:08:56 +01:00
|
|
|
int need_uptime, int need_internal)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
2011-07-06 22:30:02 +02:00
|
|
|
circuit_t *circ;
|
|
|
|
origin_circuit_t *best=NULL;
|
2011-03-26 06:39:11 +01:00
|
|
|
struct timeval now;
|
2008-10-15 20:52:06 +02:00
|
|
|
int intro_going_on_but_too_old = 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
tor_assert(conn);
|
|
|
|
|
|
|
|
tor_assert(purpose == CIRCUIT_PURPOSE_C_GENERAL ||
|
|
|
|
purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT ||
|
|
|
|
purpose == CIRCUIT_PURPOSE_C_REND_JOINED);
|
|
|
|
|
2011-03-26 06:39:11 +01:00
|
|
|
tor_gettimeofday(&now);
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
for (circ=global_circuitlist;circ;circ = circ->next) {
|
2011-07-06 22:30:02 +02:00
|
|
|
origin_circuit_t *origin_circ;
|
|
|
|
if (!CIRCUIT_IS_ORIGIN(circ))
|
|
|
|
continue;
|
|
|
|
origin_circ = TO_ORIGIN_CIRCUIT(circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2012-12-08 03:57:51 +01:00
|
|
|
/* Log an info message if we're going to launch a new intro circ in
|
|
|
|
* parallel */
|
2008-10-15 20:52:06 +02:00
|
|
|
if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT &&
|
2012-12-08 03:57:51 +01:00
|
|
|
!must_be_open && origin_circ->hs_circ_has_timed_out) {
|
|
|
|
intro_going_on_but_too_old = 1;
|
|
|
|
continue;
|
2008-10-15 20:52:06 +02:00
|
|
|
}
|
|
|
|
|
2012-12-08 03:57:51 +01:00
|
|
|
if (!circuit_is_acceptable(origin_circ,conn,must_be_open,purpose,
|
|
|
|
need_uptime,need_internal,now.tv_sec))
|
|
|
|
continue;
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/* now this is an acceptable circ to hand back. but that doesn't
|
|
|
|
* mean it's the *best* circ to hand back. try to decide.
|
|
|
|
*/
|
2011-07-06 23:08:24 +02:00
|
|
|
if (!best || circuit_is_better(origin_circ,best,conn))
|
2011-07-06 22:30:02 +02:00
|
|
|
best = origin_circ;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2008-10-15 20:52:06 +02:00
|
|
|
if (!best && intro_going_on_but_too_old)
|
|
|
|
log_info(LD_REND|LD_CIRC, "There is an intro circuit being created "
|
|
|
|
"right now, but it has already taken quite a while. Starting "
|
|
|
|
"one in parallel.");
|
|
|
|
|
2011-07-06 22:30:02 +02:00
|
|
|
return best;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2011-07-07 20:54:54 +02:00
|
|
|
/** Return the number of not-yet-open general-purpose origin circuits. */
|
|
|
|
static int
|
|
|
|
count_pending_general_client_circuits(void)
|
|
|
|
{
|
|
|
|
const circuit_t *circ;
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
for (circ = global_circuitlist; circ; circ = circ->next) {
|
|
|
|
if (circ->marked_for_close ||
|
|
|
|
circ->state == CIRCUIT_STATE_OPEN ||
|
|
|
|
circ->purpose != CIRCUIT_PURPOSE_C_GENERAL ||
|
|
|
|
!CIRCUIT_IS_ORIGIN(circ))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2011-03-11 09:22:53 +01:00
|
|
|
#if 0
|
2008-09-16 00:29:47 +02:00
|
|
|
/** Check whether, according to the policies in <b>options</b>, the
|
2008-12-22 15:56:16 +01:00
|
|
|
* circuit <b>circ</b> makes sense. */
|
2011-03-11 09:22:53 +01:00
|
|
|
/* XXXX currently only checks Exclude{Exit}Nodes; it should check more.
|
|
|
|
* Also, it doesn't have the right definition of an exit circuit. Also,
|
|
|
|
* it's never called. */
|
2008-09-16 00:29:47 +02:00
|
|
|
int
|
|
|
|
circuit_conforms_to_options(const origin_circuit_t *circ,
|
|
|
|
const or_options_t *options)
|
|
|
|
{
|
|
|
|
const crypt_path_t *cpath, *cpath_next = NULL;
|
|
|
|
|
2011-03-11 09:22:53 +01:00
|
|
|
/* first check if it includes any excluded nodes */
|
|
|
|
for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
|
2008-09-16 00:29:47 +02:00
|
|
|
cpath_next = cpath->next;
|
|
|
|
if (routerset_contains_extendinfo(options->ExcludeNodes,
|
|
|
|
cpath->extend_info))
|
|
|
|
return 0;
|
2011-03-11 09:22:53 +01:00
|
|
|
}
|
2008-09-16 00:29:47 +02:00
|
|
|
|
2011-03-11 09:22:53 +01:00
|
|
|
/* then consider the final hop */
|
|
|
|
if (routerset_contains_extendinfo(options->ExcludeExitNodes,
|
|
|
|
circ->cpath->prev->extend_info))
|
|
|
|
return 0;
|
2008-09-16 00:29:47 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2011-03-11 09:22:53 +01:00
|
|
|
#endif
|
2008-09-16 00:29:47 +02:00
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Close all circuits that start at us, aren't open, and were born
|
2006-03-22 01:52:37 +01:00
|
|
|
* at least CircuitBuildTimeout seconds ago.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2011-03-26 06:34:42 +01:00
|
|
|
circuit_expire_building(void)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2009-09-21 01:50:44 +02:00
|
|
|
circuit_t *victim, *next_circ = global_circuitlist;
|
2010-09-30 00:01:22 +02:00
|
|
|
/* circ_times.timeout_ms and circ_times.close_ms are from
|
|
|
|
* circuit_build_times_get_initial_timeout() if we haven't computed
|
|
|
|
* custom timeouts yet */
|
2011-03-26 06:34:42 +01:00
|
|
|
struct timeval general_cutoff, begindir_cutoff, fourhop_cutoff,
|
2012-12-08 03:57:51 +01:00
|
|
|
close_cutoff, extremely_old_cutoff, hs_extremely_old_cutoff,
|
|
|
|
cannibalized_cutoff, c_intro_cutoff, s_intro_cutoff, stream_cutoff;
|
2011-12-23 14:25:17 +01:00
|
|
|
const or_options_t *options = get_options();
|
2011-03-26 06:34:42 +01:00
|
|
|
struct timeval now;
|
2008-03-24 04:50:21 +01:00
|
|
|
cpath_build_state_t *build_state;
|
2012-10-26 02:43:10 +02:00
|
|
|
int any_opened_circs = 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2011-03-26 06:34:42 +01:00
|
|
|
tor_gettimeofday(&now);
|
2012-10-26 02:43:10 +02:00
|
|
|
|
|
|
|
/* Check to see if we have any opened circuits. If we don't,
|
|
|
|
* we want to be more lenient with timeouts, in case the
|
|
|
|
* user has relocated and/or changed network connections.
|
|
|
|
* See bug #3443. */
|
|
|
|
while (next_circ) {
|
|
|
|
if (!CIRCUIT_IS_ORIGIN(next_circ) || /* didn't originate here */
|
2012-12-07 20:08:07 +01:00
|
|
|
next_circ->marked_for_close) { /* don't mess with marked circs */
|
|
|
|
next_circ = next_circ->next;
|
2012-10-26 02:43:10 +02:00
|
|
|
continue;
|
2012-12-07 20:08:07 +01:00
|
|
|
}
|
2012-10-26 02:43:10 +02:00
|
|
|
|
|
|
|
if (TO_ORIGIN_CIRCUIT(next_circ)->has_opened &&
|
|
|
|
next_circ->state == CIRCUIT_STATE_OPEN &&
|
|
|
|
TO_ORIGIN_CIRCUIT(next_circ)->build_state &&
|
|
|
|
TO_ORIGIN_CIRCUIT(next_circ)->build_state->desired_path_len
|
|
|
|
== DEFAULT_ROUTE_LEN) {
|
|
|
|
any_opened_circs = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
next_circ = next_circ->next;
|
|
|
|
}
|
|
|
|
next_circ = global_circuitlist;
|
|
|
|
|
2011-03-26 06:34:42 +01:00
|
|
|
#define SET_CUTOFF(target, msec) do { \
|
|
|
|
long ms = tor_lround(msec); \
|
|
|
|
struct timeval diff; \
|
|
|
|
diff.tv_sec = ms / 1000; \
|
2011-04-07 05:30:49 +02:00
|
|
|
diff.tv_usec = (int)((ms % 1000) * 1000); \
|
2011-03-26 06:34:42 +01:00
|
|
|
timersub(&now, &diff, &target); \
|
|
|
|
} while (0)
|
|
|
|
|
2012-12-08 03:57:51 +01:00
|
|
|
/**
|
2013-01-09 02:19:15 +01:00
|
|
|
* Because circuit build timeout is calculated only based on 3 hop
|
|
|
|
* general purpose circuit construction, we need to scale the timeout
|
|
|
|
* to make it properly apply to longer circuits, and circuits of
|
|
|
|
* certain usage types. The following diagram illustrates how we
|
|
|
|
* derive the scaling below. In short, we calculate the number
|
|
|
|
* of times our telescoping-based circuit construction causes cells
|
|
|
|
* to traverse each link for the circuit purpose types in question,
|
|
|
|
* and then assume each link is equivalent.
|
|
|
|
*
|
2012-12-08 03:57:51 +01:00
|
|
|
* OP --a--> A --b--> B --c--> C
|
|
|
|
* OP --a--> A --b--> B --c--> C --d--> D
|
|
|
|
*
|
|
|
|
* Let h = a = b = c = d
|
|
|
|
*
|
|
|
|
* Three hops (general_cutoff)
|
|
|
|
* RTTs = 3a + 2b + c
|
|
|
|
* RTTs = 6h
|
|
|
|
* Cannibalized:
|
|
|
|
* RTTs = a+b+c+d
|
|
|
|
* RTTs = 4h
|
|
|
|
* Four hops:
|
|
|
|
* RTTs = 4a + 3b + 2c + d
|
|
|
|
* RTTs = 10h
|
|
|
|
* Client INTRODUCE1+ACK: // XXX: correct?
|
|
|
|
* RTTs = 5a + 4b + 3c + 2d
|
|
|
|
* RTTs = 14h
|
|
|
|
* Server intro:
|
|
|
|
* RTTs = 4a + 3b + 2c
|
|
|
|
* RTTs = 9h
|
|
|
|
*/
|
2011-03-26 06:34:42 +01:00
|
|
|
SET_CUTOFF(general_cutoff, circ_times.timeout_ms);
|
2012-09-01 07:25:17 +02:00
|
|
|
SET_CUTOFF(begindir_cutoff, circ_times.timeout_ms);
|
2012-12-08 03:57:51 +01:00
|
|
|
|
|
|
|
/* > 3hop circs seem to have a 1.0 second delay on their cannibalized
|
|
|
|
* 4th hop. */
|
|
|
|
SET_CUTOFF(fourhop_cutoff, circ_times.timeout_ms * (10/6.0) + 1000);
|
|
|
|
|
|
|
|
/* CIRCUIT_PURPOSE_C_ESTABLISH_REND behaves more like a RELAY cell.
|
|
|
|
* Use the stream cutoff (more or less). */
|
|
|
|
SET_CUTOFF(stream_cutoff, MAX(options->CircuitStreamTimeout,15)*1000 + 1000);
|
|
|
|
|
|
|
|
/* Be lenient with cannibalized circs. They already survived the official
|
2013-01-15 21:40:17 +01:00
|
|
|
* CBT, and they're usually not performance-critical. */
|
2012-12-08 03:57:51 +01:00
|
|
|
SET_CUTOFF(cannibalized_cutoff,
|
|
|
|
MAX(circ_times.close_ms*(4/6.0),
|
|
|
|
options->CircuitStreamTimeout * 1000) + 1000);
|
|
|
|
|
2013-01-15 21:40:17 +01:00
|
|
|
/* Intro circs have an extra round trip (and are also 4 hops long) */
|
2012-12-08 03:57:51 +01:00
|
|
|
SET_CUTOFF(c_intro_cutoff, circ_times.timeout_ms * (14/6.0) + 1000);
|
|
|
|
|
2013-01-15 21:40:17 +01:00
|
|
|
/* Server intro circs have an extra round trip */
|
2012-12-08 03:57:51 +01:00
|
|
|
SET_CUTOFF(s_intro_cutoff, circ_times.timeout_ms * (9/6.0) + 1000);
|
|
|
|
|
2011-03-26 06:34:42 +01:00
|
|
|
SET_CUTOFF(close_cutoff, circ_times.close_ms);
|
|
|
|
SET_CUTOFF(extremely_old_cutoff, circ_times.close_ms*2 + 1000);
|
|
|
|
|
2011-12-23 14:25:17 +01:00
|
|
|
SET_CUTOFF(hs_extremely_old_cutoff,
|
|
|
|
MAX(circ_times.close_ms*2 + 1000,
|
|
|
|
options->SocksTimeout * 1000));
|
|
|
|
|
2009-09-21 01:50:44 +02:00
|
|
|
while (next_circ) {
|
2011-03-26 06:34:42 +01:00
|
|
|
struct timeval cutoff;
|
2009-09-21 01:50:44 +02:00
|
|
|
victim = next_circ;
|
|
|
|
next_circ = next_circ->next;
|
2005-12-03 06:29:05 +01:00
|
|
|
if (!CIRCUIT_IS_ORIGIN(victim) || /* didn't originate here */
|
2012-10-26 02:43:10 +02:00
|
|
|
victim->marked_for_close) /* don't mess with marked circs */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* If we haven't yet started the first hop, it means we don't have
|
|
|
|
* any orconns available, and thus have not started counting time yet
|
|
|
|
* for this circuit. See circuit_deliver_create_cell() and uses of
|
|
|
|
* timestamp_began.
|
|
|
|
*
|
|
|
|
* Continue to wait in this case. The ORConn should timeout
|
|
|
|
* independently and kill us then.
|
|
|
|
*/
|
|
|
|
if (TO_ORIGIN_CIRCUIT(victim)->cpath->state == CPATH_STATE_CLOSED) {
|
2005-12-03 06:29:05 +01:00
|
|
|
continue;
|
2012-10-26 02:43:10 +02:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2008-03-24 04:50:21 +01:00
|
|
|
build_state = TO_ORIGIN_CIRCUIT(victim)->build_state;
|
2008-10-15 02:36:09 +02:00
|
|
|
if (build_state && build_state->onehop_tunnel)
|
|
|
|
cutoff = begindir_cutoff;
|
2010-06-08 10:01:47 +02:00
|
|
|
else if (victim->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT)
|
|
|
|
cutoff = close_cutoff;
|
2012-12-08 03:57:51 +01:00
|
|
|
else if (victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCING ||
|
|
|
|
victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
|
|
|
|
cutoff = c_intro_cutoff;
|
|
|
|
else if (victim->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)
|
|
|
|
cutoff = s_intro_cutoff;
|
|
|
|
else if (victim->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND)
|
|
|
|
cutoff = stream_cutoff;
|
2012-12-12 20:53:18 +01:00
|
|
|
else if (victim->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
|
|
|
|
cutoff = close_cutoff;
|
2012-12-08 03:57:51 +01:00
|
|
|
else if (TO_ORIGIN_CIRCUIT(victim)->has_opened &&
|
|
|
|
victim->state != CIRCUIT_STATE_OPEN)
|
|
|
|
cutoff = cannibalized_cutoff;
|
|
|
|
else if (build_state && build_state->desired_path_len >= 4)
|
|
|
|
cutoff = fourhop_cutoff;
|
2008-10-15 02:36:09 +02:00
|
|
|
else
|
|
|
|
cutoff = general_cutoff;
|
2010-06-08 10:01:47 +02:00
|
|
|
|
2011-12-23 14:25:17 +01:00
|
|
|
if (TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out)
|
|
|
|
cutoff = hs_extremely_old_cutoff;
|
|
|
|
|
2012-10-26 02:43:10 +02:00
|
|
|
if (timercmp(&victim->timestamp_began, &cutoff, >))
|
2008-03-24 04:50:21 +01:00
|
|
|
continue; /* it's still young, leave it alone */
|
|
|
|
|
2012-10-26 02:43:10 +02:00
|
|
|
if (!any_opened_circs) {
|
|
|
|
/* It's still young enough that we wouldn't close it, right? */
|
|
|
|
if (timercmp(&victim->timestamp_began, &close_cutoff, >)) {
|
|
|
|
if (!TO_ORIGIN_CIRCUIT(victim)->relaxed_timeout) {
|
|
|
|
int first_hop_succeeded = TO_ORIGIN_CIRCUIT(victim)->cpath->state
|
|
|
|
== CPATH_STATE_OPEN;
|
|
|
|
log_info(LD_CIRC,
|
|
|
|
"No circuits are opened. Relaxing timeout for "
|
|
|
|
"a circuit with channel state %s. %d guards are live.",
|
|
|
|
channel_state_to_string(victim->n_chan->state),
|
2012-12-11 18:44:18 +01:00
|
|
|
num_live_entry_guards(0));
|
2012-10-26 02:43:10 +02:00
|
|
|
|
|
|
|
/* We count the timeout here for CBT, because technically this
|
|
|
|
* was a timeout, and the timeout value needs to reset if we
|
|
|
|
* see enough of them. Note this means we also need to avoid
|
|
|
|
* double-counting below, too. */
|
|
|
|
circuit_build_times_count_timeout(&circ_times, first_hop_succeeded);
|
|
|
|
TO_ORIGIN_CIRCUIT(victim)->relaxed_timeout = 1;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
2012-12-26 16:05:45 +01:00
|
|
|
static ratelim_t relax_timeout_limit = RATELIM_INIT(3600);
|
|
|
|
char *m;
|
|
|
|
if ((m = rate_limit_log(&relax_timeout_limit, approx_time()))) {
|
|
|
|
log_notice(LD_CIRC,
|
2012-10-26 02:43:10 +02:00
|
|
|
"No circuits are opened. Relaxed timeout for "
|
|
|
|
"a circuit with channel state %s to %ldms. "
|
|
|
|
"However, it appears the circuit has timed out anyway. "
|
2012-12-26 16:05:45 +01:00
|
|
|
"%d guards are live. %s",
|
2012-10-26 02:43:10 +02:00
|
|
|
channel_state_to_string(victim->n_chan->state),
|
2012-12-26 16:05:45 +01:00
|
|
|
(long)circ_times.close_ms, num_live_entry_guards(0), m);
|
|
|
|
tor_free(m);
|
|
|
|
}
|
2012-10-26 02:43:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-17 19:49:13 +01:00
|
|
|
#if 0
|
2004-05-13 09:24:49 +02:00
|
|
|
/* some debug logs, to help track bugs */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (victim->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
|
2004-11-28 12:39:53 +01:00
|
|
|
victim->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!victim->timestamp_dirty)
|
2009-05-27 23:55:51 +02:00
|
|
|
log_fn(LOG_DEBUG,"Considering %sopen purpose %d to %s (circid %d)."
|
2005-12-14 21:40:40 +01:00
|
|
|
"(clean).",
|
2004-05-13 09:24:49 +02:00
|
|
|
victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
|
2004-07-03 01:40:03 +02:00
|
|
|
victim->purpose, victim->build_state->chosen_exit_name,
|
2004-05-13 09:24:49 +02:00
|
|
|
victim->n_circ_id);
|
|
|
|
else
|
2009-05-27 23:55:51 +02:00
|
|
|
log_fn(LOG_DEBUG,"Considering %sopen purpose %d to %s (circid %d). "
|
2005-12-14 21:40:40 +01:00
|
|
|
"%d secs since dirty.",
|
2004-05-13 09:24:49 +02:00
|
|
|
victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
|
2004-07-03 01:40:03 +02:00
|
|
|
victim->purpose, victim->build_state->chosen_exit_name,
|
2004-05-13 09:24:49 +02:00
|
|
|
victim->n_circ_id,
|
|
|
|
(int)(now - victim->timestamp_dirty));
|
|
|
|
}
|
2005-01-17 19:49:13 +01:00
|
|
|
#endif
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/* if circ is !open, or if it's open but purpose is a non-finished
|
|
|
|
* intro or rend, then mark it for close */
|
2005-12-03 06:29:05 +01:00
|
|
|
if (victim->state == CIRCUIT_STATE_OPEN) {
|
|
|
|
switch (victim->purpose) {
|
2005-12-04 23:19:04 +01:00
|
|
|
default: /* most open circuits can be left alone. */
|
2005-12-03 17:32:29 +01:00
|
|
|
continue; /* yes, continue inside a switch refers to the nearest
|
|
|
|
* enclosing loop. C is smart. */
|
2005-12-03 06:29:05 +01:00
|
|
|
case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
|
2005-12-04 23:19:04 +01:00
|
|
|
break; /* too old, need to die */
|
2005-12-03 06:29:05 +01:00
|
|
|
case CIRCUIT_PURPOSE_C_REND_READY:
|
|
|
|
/* it's a rend_ready circ -- has it already picked a query? */
|
2005-12-04 22:51:50 +01:00
|
|
|
/* c_rend_ready circs measure age since timestamp_dirty,
|
|
|
|
* because that's set when they switch purposes
|
|
|
|
*/
|
2008-09-24 16:44:29 +02:00
|
|
|
if (TO_ORIGIN_CIRCUIT(victim)->rend_data ||
|
2011-03-26 06:34:42 +01:00
|
|
|
victim->timestamp_dirty > cutoff.tv_sec)
|
2005-12-03 06:29:05 +01:00
|
|
|
continue;
|
|
|
|
break;
|
2012-12-12 20:53:18 +01:00
|
|
|
case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
|
|
|
|
/* Open path bias testing circuits are given a long
|
|
|
|
* time to complete the test, but not forever */
|
|
|
|
TO_ORIGIN_CIRCUIT(victim)->path_state = PATH_STATE_USE_FAILED;
|
|
|
|
break;
|
2012-12-08 03:57:51 +01:00
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCING:
|
|
|
|
/* We keep old introducing circuits around for
|
|
|
|
* a while in parallel, and they can end up "opened".
|
|
|
|
* We decide below if we're going to mark them timed
|
|
|
|
* out and eventually close them.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
|
2005-12-03 06:29:05 +01:00
|
|
|
case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
|
|
|
|
/* rend and intro circs become dirty each time they
|
|
|
|
* make an introduction attempt. so timestamp_dirty
|
|
|
|
* will reflect the time since the last attempt.
|
|
|
|
*/
|
2011-03-26 06:34:42 +01:00
|
|
|
if (victim->timestamp_dirty > cutoff.tv_sec)
|
2005-12-03 06:29:05 +01:00
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-21 01:50:44 +02:00
|
|
|
} else { /* circuit not open, consider recording failure as timeout */
|
|
|
|
int first_hop_succeeded = TO_ORIGIN_CIRCUIT(victim)->cpath &&
|
|
|
|
TO_ORIGIN_CIRCUIT(victim)->cpath->state == CPATH_STATE_OPEN;
|
2010-06-08 10:01:47 +02:00
|
|
|
|
|
|
|
if (TO_ORIGIN_CIRCUIT(victim)->p_streams != NULL) {
|
2011-04-08 19:27:25 +02:00
|
|
|
log_warn(LD_BUG, "Circuit %d (purpose %d, %s) has timed out, "
|
2010-06-08 10:01:47 +02:00
|
|
|
"yet has attached streams!",
|
|
|
|
TO_ORIGIN_CIRCUIT(victim)->global_identifier,
|
2011-04-08 19:27:25 +02:00
|
|
|
victim->purpose,
|
|
|
|
circuit_purpose_to_string(victim->purpose));
|
2010-06-08 10:01:47 +02:00
|
|
|
tor_fragile_assert();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-09-30 00:05:10 +02:00
|
|
|
if (circuit_timeout_want_to_count_circ(TO_ORIGIN_CIRCUIT(victim)) &&
|
|
|
|
circuit_build_times_enough_to_compute(&circ_times)) {
|
2010-09-29 20:41:27 +02:00
|
|
|
/* Circuits are allowed to last longer for measurement.
|
|
|
|
* Switch their purpose and wait. */
|
|
|
|
if (victim->purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
|
2010-07-24 18:19:20 +02:00
|
|
|
control_event_circuit_status(TO_ORIGIN_CIRCUIT(victim),
|
|
|
|
CIRC_EVENT_FAILED,
|
|
|
|
END_CIRC_REASON_TIMEOUT);
|
2011-11-24 15:52:38 +01:00
|
|
|
circuit_change_purpose(victim, CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT);
|
2010-09-29 20:41:27 +02:00
|
|
|
/* Record this failure to check for too many timeouts
|
|
|
|
* in a row. This function does not record a time value yet
|
|
|
|
* (we do that later); it only counts the fact that we did
|
2012-10-26 02:43:10 +02:00
|
|
|
* have a timeout. We also want to avoid double-counting
|
|
|
|
* already "relaxed" circuits, which are counted above. */
|
|
|
|
if (!TO_ORIGIN_CIRCUIT(victim)->relaxed_timeout) {
|
|
|
|
circuit_build_times_count_timeout(&circ_times,
|
|
|
|
first_hop_succeeded);
|
|
|
|
}
|
2010-09-29 20:41:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
2010-06-08 10:01:47 +02:00
|
|
|
|
2010-09-29 20:41:27 +02:00
|
|
|
/*
|
|
|
|
* If the circuit build time is much greater than we would have cut
|
|
|
|
* it off at, we probably had a suspend event along this codepath,
|
|
|
|
* and we should discard the value.
|
|
|
|
*/
|
2012-10-26 02:43:10 +02:00
|
|
|
if (timercmp(&victim->timestamp_began, &extremely_old_cutoff, <)) {
|
2010-09-29 20:41:27 +02:00
|
|
|
log_notice(LD_CIRC,
|
|
|
|
"Extremely large value for circuit build timeout: %lds. "
|
2011-04-08 19:27:25 +02:00
|
|
|
"Assuming clock jump. Purpose %d (%s)",
|
2012-10-26 02:43:10 +02:00
|
|
|
(long)(now.tv_sec - victim->timestamp_began.tv_sec),
|
2011-04-27 21:18:34 +02:00
|
|
|
victim->purpose,
|
2011-04-08 19:27:25 +02:00
|
|
|
circuit_purpose_to_string(victim->purpose));
|
2010-09-29 20:41:27 +02:00
|
|
|
} else if (circuit_build_times_count_close(&circ_times,
|
2011-04-27 21:18:34 +02:00
|
|
|
first_hop_succeeded,
|
2012-10-26 02:43:10 +02:00
|
|
|
victim->timestamp_began.tv_sec)) {
|
2010-09-29 20:41:27 +02:00
|
|
|
circuit_build_times_set_timeout(&circ_times);
|
|
|
|
}
|
2010-05-08 00:42:57 +02:00
|
|
|
}
|
2012-12-12 20:53:18 +01:00
|
|
|
|
|
|
|
if (TO_ORIGIN_CIRCUIT(victim)->has_opened &&
|
|
|
|
victim->purpose != CIRCUIT_PURPOSE_PATH_BIAS_TESTING) {
|
|
|
|
/* For path bias: we want to let these guys live for a while
|
|
|
|
* so we get a chance to test them. */
|
|
|
|
log_info(LD_CIRC,
|
2013-01-09 02:19:15 +01:00
|
|
|
"Allowing cannibalized circuit %d time to finish building "
|
|
|
|
"as a pathbias testing circ.",
|
2012-12-12 20:53:18 +01:00
|
|
|
TO_ORIGIN_CIRCUIT(victim)->global_identifier);
|
|
|
|
circuit_change_purpose(victim, CIRCUIT_PURPOSE_PATH_BIAS_TESTING);
|
|
|
|
continue; /* It now should have a longer timeout next time */
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2005-12-03 06:29:05 +01:00
|
|
|
|
2011-12-24 09:46:37 +01:00
|
|
|
/* If this is a hidden service client circuit which is far enough
|
|
|
|
* along in connecting to its destination, and we haven't already
|
|
|
|
* flagged it as 'timed out', and the user has not told us to
|
|
|
|
* close such circs immediately on timeout, flag it as 'timed out'
|
|
|
|
* so we'll launch another intro or rend circ, but don't mark it
|
|
|
|
* for close yet.
|
2011-12-23 14:25:17 +01:00
|
|
|
*
|
|
|
|
* (Circs flagged as 'timed out' are given a much longer timeout
|
|
|
|
* period above, so we won't close them in the next call to
|
|
|
|
* circuit_expire_building.) */
|
2011-12-24 09:46:37 +01:00
|
|
|
if (!(options->CloseHSClientCircuitsImmediatelyOnTimeout) &&
|
|
|
|
!(TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out)) {
|
2011-12-23 14:25:17 +01:00
|
|
|
switch (victim->purpose) {
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_READY:
|
|
|
|
/* We only want to spare a rend circ if it has been specified in
|
|
|
|
* an INTRODUCE1 cell sent to a hidden service. A circ's
|
|
|
|
* pending_final_cpath field is non-NULL iff it is a rend circ
|
|
|
|
* and we have tried to send an INTRODUCE1 cell specifying it.
|
|
|
|
* Thus, if the pending_final_cpath field *is* NULL, then we
|
|
|
|
* want to not spare it. */
|
|
|
|
if (TO_ORIGIN_CIRCUIT(victim)->build_state->pending_final_cpath ==
|
|
|
|
NULL)
|
|
|
|
break;
|
2012-12-08 03:57:51 +01:00
|
|
|
/* fallthrough! */
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCING:
|
|
|
|
/* connection_ap_handshake_attach_circuit() will relaunch for us */
|
2011-12-23 14:25:17 +01:00
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
|
|
|
|
/* If we have reached this line, we want to spare the circ for now. */
|
2012-01-13 04:20:52 +01:00
|
|
|
log_info(LD_CIRC,"Marking circ %d (state %d:%s, purpose %d) "
|
2011-12-23 14:25:17 +01:00
|
|
|
"as timed-out HS circ",
|
|
|
|
victim->n_circ_id,
|
|
|
|
victim->state, circuit_state_to_string(victim->state),
|
|
|
|
victim->purpose);
|
|
|
|
TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out = 1;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-24 13:47:30 +01:00
|
|
|
/* If this is a service-side rendezvous circuit which is far
|
|
|
|
* enough along in connecting to its destination, consider sparing
|
|
|
|
* it. */
|
2011-12-24 13:55:20 +01:00
|
|
|
if (!(options->CloseHSServiceRendCircuitsImmediatelyOnTimeout) &&
|
|
|
|
!(TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out) &&
|
2011-12-24 13:47:30 +01:00
|
|
|
victim->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND) {
|
2012-01-13 04:20:52 +01:00
|
|
|
log_info(LD_CIRC,"Marking circ %d (state %d:%s, purpose %d) "
|
2011-12-24 13:47:30 +01:00
|
|
|
"as timed-out HS circ; relaunching rendezvous attempt.",
|
|
|
|
victim->n_circ_id,
|
|
|
|
victim->state, circuit_state_to_string(victim->state),
|
|
|
|
victim->purpose);
|
|
|
|
TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out = 1;
|
|
|
|
rend_service_relaunch_rendezvous(TO_ORIGIN_CIRCUIT(victim));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-08 15:02:47 +02:00
|
|
|
if (victim->n_chan)
|
2012-12-08 03:57:51 +01:00
|
|
|
log_info(LD_CIRC,
|
2013-01-09 02:19:15 +01:00
|
|
|
"Abandoning circ %u %s:%d (state %d,%d:%s, purpose %d, "
|
2012-12-08 03:57:51 +01:00
|
|
|
"len %d)", TO_ORIGIN_CIRCUIT(victim)->global_identifier,
|
2012-08-08 15:02:47 +02:00
|
|
|
channel_get_canonical_remote_descr(victim->n_chan),
|
2006-07-26 21:07:37 +02:00
|
|
|
victim->n_circ_id,
|
2012-12-08 03:57:51 +01:00
|
|
|
TO_ORIGIN_CIRCUIT(victim)->has_opened,
|
2006-02-13 09:28:42 +01:00
|
|
|
victim->state, circuit_state_to_string(victim->state),
|
2012-12-08 03:57:51 +01:00
|
|
|
victim->purpose,
|
|
|
|
TO_ORIGIN_CIRCUIT(victim)->build_state->desired_path_len);
|
2005-12-03 06:29:05 +01:00
|
|
|
else
|
2012-12-08 03:57:51 +01:00
|
|
|
log_info(LD_CIRC,
|
2013-01-09 02:19:15 +01:00
|
|
|
"Abandoning circ %u %d (state %d,%d:%s, purpose %d, len %d)",
|
2012-12-08 03:57:51 +01:00
|
|
|
TO_ORIGIN_CIRCUIT(victim)->global_identifier,
|
|
|
|
victim->n_circ_id, TO_ORIGIN_CIRCUIT(victim)->has_opened,
|
|
|
|
victim->state,
|
|
|
|
circuit_state_to_string(victim->state), victim->purpose,
|
|
|
|
TO_ORIGIN_CIRCUIT(victim)->build_state->desired_path_len);
|
2005-12-03 06:29:05 +01:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_log_path(LOG_INFO,LD_CIRC,TO_ORIGIN_CIRCUIT(victim));
|
2010-07-24 18:19:20 +02:00
|
|
|
if (victim->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT)
|
|
|
|
circuit_mark_for_close(victim, END_CIRC_REASON_MEASUREMENT_EXPIRED);
|
|
|
|
else
|
|
|
|
circuit_mark_for_close(victim, END_CIRC_REASON_TIMEOUT);
|
2012-10-25 02:34:18 +02:00
|
|
|
|
|
|
|
pathbias_count_timeout(TO_ORIGIN_CIRCUIT(victim));
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-05 08:10:08 +01:00
|
|
|
/** Remove any elements in <b>needed_ports</b> that are handled by an
|
|
|
|
* open or in-progress circuit.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2004-12-05 08:10:08 +01:00
|
|
|
void
|
2005-06-11 20:52:12 +02:00
|
|
|
circuit_remove_handled_ports(smartlist_t *needed_ports)
|
|
|
|
{
|
2004-12-05 08:10:08 +01:00
|
|
|
int i;
|
2004-12-07 16:29:54 +01:00
|
|
|
uint16_t *port;
|
2004-12-05 08:10:08 +01:00
|
|
|
|
|
|
|
for (i = 0; i < smartlist_len(needed_ports); ++i) {
|
2004-12-07 16:29:54 +01:00
|
|
|
port = smartlist_get(needed_ports, i);
|
|
|
|
tor_assert(*port);
|
2006-12-10 09:04:50 +01:00
|
|
|
if (circuit_stream_is_being_handled(NULL, *port,
|
|
|
|
MIN_CIRCUITS_HANDLING_STREAM)) {
|
2006-02-13 09:28:42 +01:00
|
|
|
// log_debug(LD_CIRC,"Port %d is already being handled; removing.", port);
|
2004-12-05 08:10:08 +01:00
|
|
|
smartlist_del(needed_ports, i--);
|
2004-12-07 16:29:54 +01:00
|
|
|
tor_free(port);
|
2004-12-05 08:10:08 +01:00
|
|
|
} else {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,"Port %d is not handled.", *port);
|
2004-12-05 08:10:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2005-11-25 09:08:56 +01:00
|
|
|
/** Return 1 if at least <b>min</b> general-purpose non-internal circuits
|
|
|
|
* will have an acceptable exit node for exit stream <b>conn</b> if it
|
|
|
|
* is defined, else for "*:port".
|
2004-12-05 08:10:08 +01:00
|
|
|
* Else return 0.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2011-07-20 18:55:42 +02:00
|
|
|
circuit_stream_is_being_handled(entry_connection_t *conn,
|
2006-07-26 21:07:37 +02:00
|
|
|
uint16_t port, int min)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_t *circ;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const node_t *exitnode;
|
2004-05-13 09:24:49 +02:00
|
|
|
int num=0;
|
|
|
|
time_t now = time(NULL);
|
2005-01-13 08:23:19 +01:00
|
|
|
int need_uptime = smartlist_string_num_isin(get_options()->LongLivedPorts,
|
|
|
|
conn ? conn->socks_request->port : port);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
for (circ=global_circuitlist;circ;circ = circ->next) {
|
2004-11-28 12:39:53 +01:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
!circ->marked_for_close &&
|
|
|
|
circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
|
|
|
|
(!circ->timestamp_dirty ||
|
2005-12-04 23:23:24 +01:00
|
|
|
circ->timestamp_dirty + get_options()->MaxCircuitDirtiness > now)) {
|
2006-07-23 09:37:35 +02:00
|
|
|
cpath_build_state_t *build_state = TO_ORIGIN_CIRCUIT(circ)->build_state;
|
2006-12-13 03:49:45 +01:00
|
|
|
if (build_state->is_internal || build_state->onehop_tunnel)
|
2006-07-23 09:37:35 +02:00
|
|
|
continue;
|
|
|
|
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
exitnode = build_state_get_exit_node(build_state);
|
|
|
|
if (exitnode && (!need_uptime || build_state->need_uptime)) {
|
2005-03-19 07:57:16 +01:00
|
|
|
int ok;
|
|
|
|
if (conn) {
|
2011-04-27 20:36:30 +02:00
|
|
|
ok = connection_ap_can_use_exit(conn, exitnode);
|
2005-03-19 07:57:16 +01:00
|
|
|
} else {
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
addr_policy_result_t r;
|
2011-07-15 18:58:13 +02:00
|
|
|
r = compare_tor_addr_to_node_policy(NULL, port, exitnode);
|
2005-03-19 07:57:16 +01:00
|
|
|
ok = r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED;
|
|
|
|
}
|
|
|
|
if (ok) {
|
|
|
|
if (++num >= min)
|
|
|
|
return 1;
|
|
|
|
}
|
2004-12-05 08:10:08 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-12 23:48:18 +01:00
|
|
|
/** Don't keep more than this many unused open circuits around. */
|
2009-09-17 07:41:47 +02:00
|
|
|
#define MAX_UNUSED_OPEN_CIRCUITS 14
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
|
|
|
|
/** Figure out how many circuits we have open that are clean. Make
|
|
|
|
* sure it's enough for all the upcoming behaviors we predict we'll have.
|
2009-09-17 07:41:47 +02:00
|
|
|
* But put an upper bound on the total number of circuits.
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
circuit_predict_and_launch_new(void)
|
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
int num=0, num_internal=0, num_uptime_internal=0;
|
|
|
|
int hidserv_needs_uptime=0, hidserv_needs_capacity=1;
|
|
|
|
int port_needs_uptime=0, port_needs_capacity=1;
|
|
|
|
time_t now = time(NULL);
|
2007-12-23 20:15:22 +01:00
|
|
|
int flags = 0;
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
|
2005-08-13 02:22:07 +02:00
|
|
|
/* First, count how many of each type of circuit we have already. */
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
for (circ=global_circuitlist;circ;circ = circ->next) {
|
2006-07-23 09:37:35 +02:00
|
|
|
cpath_build_state_t *build_state;
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
if (!CIRCUIT_IS_ORIGIN(circ))
|
|
|
|
continue;
|
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue; /* don't mess with marked circs */
|
|
|
|
if (circ->timestamp_dirty)
|
|
|
|
continue; /* only count clean circs */
|
|
|
|
if (circ->purpose != CIRCUIT_PURPOSE_C_GENERAL)
|
|
|
|
continue; /* only pay attention to general-purpose circs */
|
2006-07-23 09:37:35 +02:00
|
|
|
build_state = TO_ORIGIN_CIRCUIT(circ)->build_state;
|
2006-12-13 03:49:45 +01:00
|
|
|
if (build_state->onehop_tunnel)
|
|
|
|
continue;
|
|
|
|
num++;
|
2006-07-23 09:37:35 +02:00
|
|
|
if (build_state->is_internal)
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
num_internal++;
|
2006-07-23 09:37:35 +02:00
|
|
|
if (build_state->need_uptime && build_state->is_internal)
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
num_uptime_internal++;
|
|
|
|
}
|
|
|
|
|
2005-08-13 02:22:07 +02:00
|
|
|
/* If that's enough, then stop now. */
|
|
|
|
if (num >= MAX_UNUSED_OPEN_CIRCUITS)
|
|
|
|
return; /* we already have many, making more probably will hurt */
|
|
|
|
|
|
|
|
/* Second, see if we need any more exit circuits. */
|
|
|
|
/* check if we know of a port that's been requested recently
|
|
|
|
* and no circuit is currently available that can handle it. */
|
|
|
|
if (!circuit_all_predicted_ports_handled(now, &port_needs_uptime,
|
|
|
|
&port_needs_capacity)) {
|
2007-12-23 20:15:22 +01:00
|
|
|
if (port_needs_uptime)
|
|
|
|
flags |= CIRCLAUNCH_NEED_UPTIME;
|
|
|
|
if (port_needs_capacity)
|
|
|
|
flags |= CIRCLAUNCH_NEED_CAPACITY;
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,
|
|
|
|
"Have %d clean circs (%d internal), need another exit circ.",
|
|
|
|
num, num_internal);
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, flags);
|
2005-08-13 02:22:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Third, see if we need any more hidden service (server) circuits. */
|
|
|
|
if (num_rend_services() && num_uptime_internal < 3) {
|
2007-12-23 20:15:22 +01:00
|
|
|
flags = (CIRCLAUNCH_NEED_CAPACITY | CIRCLAUNCH_NEED_UPTIME |
|
|
|
|
CIRCLAUNCH_IS_INTERNAL);
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,
|
|
|
|
"Have %d clean circs (%d internal), need another internal "
|
|
|
|
"circ for my hidden service.",
|
|
|
|
num, num_internal);
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, flags);
|
2005-08-13 02:22:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fourth, see if we need any more hidden service (client) circuits. */
|
2005-11-25 09:08:56 +01:00
|
|
|
if (rep_hist_get_predicted_internal(now, &hidserv_needs_uptime,
|
|
|
|
&hidserv_needs_capacity) &&
|
2005-08-13 02:22:07 +02:00
|
|
|
((num_uptime_internal<2 && hidserv_needs_uptime) ||
|
|
|
|
num_internal<2)) {
|
2007-12-23 20:15:22 +01:00
|
|
|
if (hidserv_needs_uptime)
|
|
|
|
flags |= CIRCLAUNCH_NEED_UPTIME;
|
|
|
|
if (hidserv_needs_capacity)
|
|
|
|
flags |= CIRCLAUNCH_NEED_CAPACITY;
|
|
|
|
flags |= CIRCLAUNCH_IS_INTERNAL;
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,
|
|
|
|
"Have %d clean circs (%d uptime-internal, %d internal), need"
|
2009-05-27 23:55:51 +02:00
|
|
|
" another hidden service circ.",
|
2006-02-13 09:28:42 +01:00
|
|
|
num, num_uptime_internal, num_internal);
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, flags);
|
2005-08-13 02:22:07 +02:00
|
|
|
return;
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
}
|
2009-09-01 03:10:27 +02:00
|
|
|
|
|
|
|
/* Finally, check to see if we still need more circuits to learn
|
2009-09-17 07:41:47 +02:00
|
|
|
* a good build timeout. But if we're close to our max number we
|
|
|
|
* want, don't do another -- we want to leave a few slots open so
|
|
|
|
* we can still build circuits preemptively as needed. */
|
|
|
|
if (num < MAX_UNUSED_OPEN_CIRCUITS-2 &&
|
2012-06-12 21:12:06 +02:00
|
|
|
get_options()->LearnCircuitBuildTimeout &&
|
2009-09-17 07:41:47 +02:00
|
|
|
circuit_build_times_needs_circuits_now(&circ_times)) {
|
2009-09-01 03:10:27 +02:00
|
|
|
flags = CIRCLAUNCH_NEED_CAPACITY;
|
|
|
|
log_info(LD_CIRC,
|
|
|
|
"Have %d clean circs need another buildtime test circ.", num);
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, flags);
|
2009-09-01 08:09:54 +02:00
|
|
|
return;
|
2009-09-01 03:10:27 +02:00
|
|
|
}
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Build a new test circuit every 5 minutes */
|
|
|
|
#define TESTING_CIRCUIT_INTERVAL 300
|
|
|
|
|
2007-02-22 08:41:14 +01:00
|
|
|
/** This function is called once a second, if router_have_min_dir_info() is
|
|
|
|
* true. Its job is to make sure all services we offer have enough circuits
|
|
|
|
* available. Some services just want enough circuits for current tasks,
|
|
|
|
* whereas others want a minimum set of idle circuits hanging around.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
circuit_build_needed_circs(time_t now)
|
|
|
|
{
|
2009-01-22 17:28:12 +01:00
|
|
|
static time_t time_to_new_circuit = 0;
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/* launch a new circ for any pending streams that need one */
|
|
|
|
connection_ap_attach_pending();
|
|
|
|
|
|
|
|
/* make sure any hidden services have enough intro points */
|
2007-02-22 08:41:14 +01:00
|
|
|
rend_services_introduce();
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (time_to_new_circuit < now) {
|
2004-08-18 10:51:04 +02:00
|
|
|
circuit_reset_failure_count(1);
|
2007-04-30 11:18:48 +02:00
|
|
|
time_to_new_circuit = now + options->NewCircuitPeriod;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (proxy_mode(get_options()))
|
2005-02-22 01:53:08 +01:00
|
|
|
addressmap_clean(now);
|
2011-03-26 06:34:42 +01:00
|
|
|
circuit_expire_old_circuits_clientside();
|
2004-05-13 09:24:49 +02:00
|
|
|
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
#if 0 /* disable for now, until predict-and-launch-new can cull leftovers */
|
|
|
|
circ = circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL);
|
2004-11-28 12:39:53 +01:00
|
|
|
if (get_options()->RunTesting &&
|
|
|
|
circ &&
|
2012-10-26 02:43:10 +02:00
|
|
|
circ->timestamp_began.tv_sec + TESTING_CIRCUIT_INTERVAL < now) {
|
2004-05-13 09:24:49 +02:00
|
|
|
log_fn(LOG_INFO,"Creating a new testing circuit.");
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, 0);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2004-12-05 08:10:08 +01:00
|
|
|
#endif
|
|
|
|
}
|
2007-04-30 11:18:48 +02:00
|
|
|
if (!options->DisablePredictedCircuits)
|
|
|
|
circuit_predict_and_launch_new();
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** If the stream <b>conn</b> is a member of any of the linked
|
|
|
|
* lists of <b>circ</b>, then remove it from the list.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2006-07-26 21:07:26 +02:00
|
|
|
circuit_detach_stream(circuit_t *circ, edge_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *prevconn;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(conn);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
if (conn->base_.type == CONN_TYPE_AP) {
|
2011-07-20 18:55:42 +02:00
|
|
|
entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
|
|
|
|
entry_conn->may_use_optimistic_data = 0;
|
|
|
|
}
|
|
|
|
conn->cpath_layer = NULL; /* don't keep a stale pointer */
|
2005-04-06 08:13:49 +02:00
|
|
|
conn->on_circuit = NULL;
|
2004-10-11 03:17:42 +02:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
|
|
|
|
if (conn == origin_circ->p_streams) {
|
|
|
|
origin_circ->p_streams = conn->next_stream;
|
|
|
|
return;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
for (prevconn = origin_circ->p_streams;
|
|
|
|
prevconn && prevconn->next_stream && prevconn->next_stream != conn;
|
|
|
|
prevconn = prevconn->next_stream)
|
|
|
|
;
|
|
|
|
if (prevconn && prevconn->next_stream) {
|
|
|
|
prevconn->next_stream = conn->next_stream;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
|
|
|
|
if (conn == or_circ->n_streams) {
|
|
|
|
or_circ->n_streams = conn->next_stream;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (conn == or_circ->resolving_streams) {
|
|
|
|
or_circ->resolving_streams = conn->next_stream;
|
|
|
|
return;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
for (prevconn = or_circ->n_streams;
|
|
|
|
prevconn && prevconn->next_stream && prevconn->next_stream != conn;
|
|
|
|
prevconn = prevconn->next_stream)
|
|
|
|
;
|
|
|
|
if (prevconn && prevconn->next_stream) {
|
|
|
|
prevconn->next_stream = conn->next_stream;
|
|
|
|
return;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
for (prevconn = or_circ->resolving_streams;
|
|
|
|
prevconn && prevconn->next_stream && prevconn->next_stream != conn;
|
|
|
|
prevconn = prevconn->next_stream)
|
|
|
|
;
|
|
|
|
if (prevconn && prevconn->next_stream) {
|
|
|
|
prevconn->next_stream = conn->next_stream;
|
|
|
|
return;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 21:54:51 +02:00
|
|
|
log_warn(LD_BUG,"Edge connection not in circuit's list.");
|
|
|
|
/* Don't give an error here; it's harmless. */
|
2007-06-01 01:57:46 +02:00
|
|
|
tor_fragile_assert();
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2009-09-17 07:41:47 +02:00
|
|
|
/** If we haven't yet decided on a good timeout value for circuit
|
|
|
|
* building, we close idles circuits aggressively so we can get more
|
|
|
|
* data points. */
|
|
|
|
#define IDLE_TIMEOUT_WHILE_LEARNING (10*60)
|
|
|
|
|
2006-05-30 08:19:06 +02:00
|
|
|
/** Find each circuit that has been unused for too long, or dirty
|
2007-01-15 10:09:03 +01:00
|
|
|
* for too long and has no streams on it: mark it for close.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
|
|
|
static void
|
2011-03-26 06:34:42 +01:00
|
|
|
circuit_expire_old_circuits_clientside(void)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
|
|
|
circuit_t *circ;
|
2011-03-26 06:34:42 +01:00
|
|
|
struct timeval cutoff, now;
|
|
|
|
|
|
|
|
tor_gettimeofday(&now);
|
|
|
|
cutoff = now;
|
2009-09-01 03:10:27 +02:00
|
|
|
|
2012-06-12 21:12:06 +02:00
|
|
|
if (get_options()->LearnCircuitBuildTimeout &&
|
|
|
|
circuit_build_times_needs_circuits(&circ_times)) {
|
2009-09-17 07:41:47 +02:00
|
|
|
/* Circuits should be shorter lived if we need more of them
|
|
|
|
* for learning a good build timeout */
|
2011-03-26 06:34:42 +01:00
|
|
|
cutoff.tv_sec -= IDLE_TIMEOUT_WHILE_LEARNING;
|
2009-09-01 03:10:27 +02:00
|
|
|
} else {
|
2011-03-26 06:34:42 +01:00
|
|
|
cutoff.tv_sec -= get_options()->CircuitIdleTimeout;
|
2009-09-01 03:10:27 +02:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
for (circ = global_circuitlist; circ; circ = circ->next) {
|
2010-08-10 00:50:49 +02:00
|
|
|
if (circ->marked_for_close || !CIRCUIT_IS_ORIGIN(circ))
|
2004-05-13 09:24:49 +02:00
|
|
|
continue;
|
|
|
|
/* If the circuit has been dirty for too long, and there are no streams
|
|
|
|
* on it, mark it for close.
|
|
|
|
*/
|
|
|
|
if (circ->timestamp_dirty &&
|
2011-04-27 21:18:34 +02:00
|
|
|
circ->timestamp_dirty + get_options()->MaxCircuitDirtiness <
|
|
|
|
now.tv_sec &&
|
2006-07-23 09:37:35 +02:00
|
|
|
!TO_ORIGIN_CIRCUIT(circ)->p_streams /* nothing attached */ ) {
|
2011-03-26 06:34:42 +01:00
|
|
|
log_debug(LD_CIRC, "Closing n_circ_id %d (dirty %ld sec ago, "
|
2009-05-28 17:54:56 +02:00
|
|
|
"purpose %d)",
|
2011-03-26 06:34:42 +01:00
|
|
|
circ->n_circ_id, (long)(now.tv_sec - circ->timestamp_dirty),
|
2006-02-13 09:28:42 +01:00
|
|
|
circ->purpose);
|
2006-10-17 17:20:00 +02:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
|
2010-06-15 23:46:01 +02:00
|
|
|
} else if (!circ->timestamp_dirty && circ->state == CIRCUIT_STATE_OPEN) {
|
2012-10-26 02:43:10 +02:00
|
|
|
if (timercmp(&circ->timestamp_began, &cutoff, <)) {
|
2010-06-15 23:46:01 +02:00
|
|
|
if (circ->purpose == CIRCUIT_PURPOSE_C_GENERAL ||
|
|
|
|
circ->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT ||
|
|
|
|
circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
|
|
|
|
circ->purpose == CIRCUIT_PURPOSE_TESTING ||
|
|
|
|
(circ->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
|
|
|
|
circ->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) ||
|
|
|
|
circ->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND) {
|
|
|
|
log_debug(LD_CIRC,
|
2011-03-26 06:34:42 +01:00
|
|
|
"Closing circuit that has been unused for %ld msec.",
|
2012-10-26 02:43:10 +02:00
|
|
|
tv_mdiff(&circ->timestamp_began, &now));
|
2010-06-15 23:46:01 +02:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
|
|
|
|
} else if (!TO_ORIGIN_CIRCUIT(circ)->is_ancient) {
|
2010-08-14 10:22:08 +02:00
|
|
|
/* Server-side rend joined circuits can end up really old, because
|
2010-07-27 17:45:23 +02:00
|
|
|
* they are reused by clients for longer than normal. The client
|
2010-08-14 02:50:51 +02:00
|
|
|
* controls their lifespan. (They never become dirty, because
|
2010-08-14 10:22:08 +02:00
|
|
|
* connection_exit_begin_conn() never marks anything as dirty.)
|
|
|
|
* Similarly, server-side intro circuits last a long time. */
|
|
|
|
if (circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED &&
|
|
|
|
circ->purpose != CIRCUIT_PURPOSE_S_INTRO) {
|
2010-07-27 17:45:23 +02:00
|
|
|
log_notice(LD_CIRC,
|
|
|
|
"Ancient non-dirty circuit %d is still around after "
|
2011-04-08 19:27:25 +02:00
|
|
|
"%ld milliseconds. Purpose: %d (%s)",
|
2010-07-27 17:45:23 +02:00
|
|
|
TO_ORIGIN_CIRCUIT(circ)->global_identifier,
|
2012-10-26 02:43:10 +02:00
|
|
|
tv_mdiff(&circ->timestamp_began, &now),
|
2011-04-08 19:27:25 +02:00
|
|
|
circ->purpose,
|
|
|
|
circuit_purpose_to_string(circ->purpose));
|
2010-07-27 17:45:23 +02:00
|
|
|
TO_ORIGIN_CIRCUIT(circ)->is_ancient = 1;
|
|
|
|
}
|
2010-06-15 23:46:01 +02:00
|
|
|
}
|
2004-12-05 08:10:08 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-24 00:35:11 +02:00
|
|
|
/** How long do we wait before killing circuits with the properties
|
|
|
|
* described below?
|
|
|
|
*
|
|
|
|
* Probably we could choose a number here as low as 5 to 10 seconds,
|
|
|
|
* since these circs are used for begindir, and a) generally you either
|
|
|
|
* ask another begindir question right after or you don't for a long time,
|
|
|
|
* b) clients at least through 0.2.1.x choose from the whole set of
|
|
|
|
* directory mirrors at each choice, and c) re-establishing a one-hop
|
|
|
|
* circuit via create-fast is a light operation assuming the TLS conn is
|
|
|
|
* still there.
|
|
|
|
*
|
|
|
|
* I expect "b" to go away one day when we move to using directory
|
|
|
|
* guards, but I think "a" and "c" are good enough reasons that a low
|
|
|
|
* number is safe even then.
|
|
|
|
*/
|
|
|
|
#define IDLE_ONE_HOP_CIRC_TIMEOUT 60
|
|
|
|
|
|
|
|
/** Find each non-origin circuit that has been unused for too long,
|
|
|
|
* has no streams on it, used a create_fast, and ends here: mark it
|
|
|
|
* for close.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
circuit_expire_old_circuits_serverside(time_t now)
|
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
or_circuit_t *or_circ;
|
|
|
|
time_t cutoff = now - IDLE_ONE_HOP_CIRC_TIMEOUT;
|
|
|
|
|
|
|
|
for (circ = global_circuitlist; circ; circ = circ->next) {
|
|
|
|
if (circ->marked_for_close || CIRCUIT_IS_ORIGIN(circ))
|
|
|
|
continue;
|
|
|
|
or_circ = TO_OR_CIRCUIT(circ);
|
|
|
|
/* If the circuit has been idle for too long, and there are no streams
|
|
|
|
* on it, and it ends here, and it used a create_fast, mark it for close.
|
|
|
|
*/
|
2012-08-08 15:02:47 +02:00
|
|
|
if (or_circ->is_first_hop && !circ->n_chan &&
|
2010-04-24 00:35:11 +02:00
|
|
|
!or_circ->n_streams && !or_circ->resolving_streams &&
|
2012-08-08 15:02:47 +02:00
|
|
|
or_circ->p_chan &&
|
|
|
|
channel_when_last_xmit(or_circ->p_chan) <= cutoff) {
|
2010-04-24 00:35:11 +02:00
|
|
|
log_info(LD_CIRC, "Closing circ_id %d (empty %d secs ago)",
|
|
|
|
or_circ->p_circ_id,
|
2012-08-08 15:02:47 +02:00
|
|
|
(int)(now - channel_when_last_xmit(or_circ->p_chan)));
|
2010-04-24 00:35:11 +02:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-24 08:50:38 +01:00
|
|
|
/** Number of testing circuits we want open before testing our bandwidth. */
|
2006-09-15 07:30:25 +02:00
|
|
|
#define NUM_PARALLEL_TESTING_CIRCS 4
|
|
|
|
|
2007-02-24 08:50:38 +01:00
|
|
|
/** True iff we've ever had enough testing circuits open to test our
|
2007-02-16 21:39:37 +01:00
|
|
|
* bandwidth. */
|
2006-09-15 07:30:25 +02:00
|
|
|
static int have_performed_bandwidth_test = 0;
|
|
|
|
|
|
|
|
/** Reset have_performed_bandwidth_test, so we'll start building
|
|
|
|
* testing circuits again so we can exercise our bandwidth. */
|
|
|
|
void
|
|
|
|
reset_bandwidth_test(void)
|
|
|
|
{
|
|
|
|
have_performed_bandwidth_test = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return 1 if we've already exercised our bandwidth, or if we
|
|
|
|
* have fewer than NUM_PARALLEL_TESTING_CIRCS testing circuits
|
|
|
|
* established or on the way. Else return 0.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
circuit_enough_testing_circs(void)
|
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
int num = 0;
|
|
|
|
|
|
|
|
if (have_performed_bandwidth_test)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (circ = global_circuitlist; circ; circ = circ->next) {
|
|
|
|
if (!circ->marked_for_close && CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
circ->purpose == CIRCUIT_PURPOSE_TESTING &&
|
|
|
|
circ->state == CIRCUIT_STATE_OPEN)
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
return num >= NUM_PARALLEL_TESTING_CIRCS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A testing circuit has completed. Take whatever stats we want.
|
|
|
|
* Noticing reachability is taken care of in onionskin_answer(),
|
|
|
|
* so there's no need to record anything here. But if we still want
|
|
|
|
* to do the bandwidth test, and we now have enough testing circuits
|
|
|
|
* open, do it.
|
|
|
|
*/
|
2005-02-27 10:47:01 +01:00
|
|
|
static void
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_testing_opened(origin_circuit_t *circ)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2008-09-29 11:36:42 +02:00
|
|
|
if (have_performed_bandwidth_test ||
|
|
|
|
!check_whether_orport_reachable()) {
|
|
|
|
/* either we've already done everything we want with testing circuits,
|
|
|
|
* or this testing circuit became open due to a fluke, e.g. we picked
|
|
|
|
* a last hop where we already had the connection open due to an
|
|
|
|
* outgoing local circuit. */
|
2006-09-15 07:30:25 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_AT_ORIGIN);
|
|
|
|
} else if (circuit_enough_testing_circs()) {
|
|
|
|
router_perform_bandwidth_test(NUM_PARALLEL_TESTING_CIRCS, time(NULL));
|
|
|
|
have_performed_bandwidth_test = 1;
|
2006-09-15 07:53:00 +02:00
|
|
|
} else
|
|
|
|
consider_testing_reachability(1, 0);
|
2005-02-27 10:47:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** A testing circuit has failed to build. Take whatever stats we want. */
|
|
|
|
static void
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_testing_failed(origin_circuit_t *circ, int at_last_hop)
|
2005-09-30 03:09:52 +02:00
|
|
|
{
|
2006-09-18 06:59:15 +02:00
|
|
|
if (server_mode(get_options()) && check_whether_orport_reachable())
|
|
|
|
return;
|
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_GENERAL,
|
|
|
|
"Our testing circuit (to see if your ORPort is reachable) "
|
2006-09-18 06:59:15 +02:00
|
|
|
"has failed. I'll try again later.");
|
2006-06-05 00:42:13 +02:00
|
|
|
|
|
|
|
/* These aren't used yet. */
|
|
|
|
(void)circ;
|
|
|
|
(void)at_last_hop;
|
2005-02-27 10:47:01 +01:00
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** The circuit <b>circ</b> has just become open. Take the next
|
|
|
|
* step: for rendezvous circuits, we pass circ to the appropriate
|
|
|
|
* function in rendclient or rendservice. For general circuits, we
|
|
|
|
* call connection_ap_attach_pending, which looks for pending streams
|
|
|
|
* that could use circ.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_has_opened(origin_circuit_t *circ)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-10-09 17:47:27 +02:00
|
|
|
control_event_circuit_status(circ, CIRC_EVENT_BUILT, 0);
|
2004-11-03 19:33:07 +01:00
|
|
|
|
2010-09-29 20:41:27 +02:00
|
|
|
/* Remember that this circuit has finished building. Now if we start
|
|
|
|
* it building again later (e.g. by extending it), we will know not
|
|
|
|
* to consider its build time. */
|
|
|
|
circ->has_opened = 1;
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
switch (TO_CIRCUIT(circ)->purpose) {
|
2004-05-13 09:24:49 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
|
|
|
|
rend_client_rendcirc_has_opened(circ);
|
2011-12-06 12:46:02 +01:00
|
|
|
/* Start building an intro circ if we don't have one yet. */
|
2005-01-19 18:13:11 +01:00
|
|
|
connection_ap_attach_pending();
|
2011-12-06 12:46:02 +01:00
|
|
|
/* This isn't a call to circuit_try_attaching_streams because a
|
|
|
|
* circuit in _C_ESTABLISH_REND state isn't connected to its
|
|
|
|
* hidden service yet, thus we can't attach streams to it yet,
|
|
|
|
* thus circuit_try_attaching_streams would always clear the
|
|
|
|
* circuit's isolation state. circuit_try_attaching_streams is
|
|
|
|
* called later, when the rend circ enters _C_REND_JOINED
|
|
|
|
* state. */
|
2004-05-13 09:24:49 +02:00
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCING:
|
|
|
|
rend_client_introcirc_has_opened(circ);
|
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
|
|
|
/* Tell any AP connections that have been waiting for a new
|
|
|
|
* circuit that one is ready. */
|
2011-12-06 12:46:02 +01:00
|
|
|
circuit_try_attaching_streams(circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
|
|
|
|
/* at Bob, waiting for introductions */
|
|
|
|
rend_service_intro_has_opened(circ);
|
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_S_CONNECT_REND:
|
|
|
|
/* at Bob, connecting to rend point */
|
|
|
|
rend_service_rendezvous_has_opened(circ);
|
|
|
|
break;
|
2005-02-27 10:47:01 +01:00
|
|
|
case CIRCUIT_PURPOSE_TESTING:
|
|
|
|
circuit_testing_opened(circ);
|
|
|
|
break;
|
2006-10-10 00:26:21 +02:00
|
|
|
/* default:
|
|
|
|
* This won't happen in normal operation, but might happen if the
|
|
|
|
* controller did it. Just let it slide. */
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2011-12-06 12:46:02 +01:00
|
|
|
}
|
2011-07-19 19:51:43 +02:00
|
|
|
|
2011-12-06 12:46:02 +01:00
|
|
|
/** If the stream-isolation state of <b>circ</b> can be cleared, clear
|
|
|
|
* it. Return non-zero iff <b>circ</b>'s isolation state was cleared. */
|
|
|
|
static int
|
|
|
|
circuit_try_clearing_isolation_state(origin_circuit_t *circ)
|
|
|
|
{
|
2011-07-21 02:27:36 +02:00
|
|
|
if (/* The circuit may have become non-open if it was cannibalized.*/
|
2012-10-12 18:22:13 +02:00
|
|
|
circ->base_.state == CIRCUIT_STATE_OPEN &&
|
2011-07-21 02:27:36 +02:00
|
|
|
/* If !isolation_values_set, there is nothing to clear. */
|
2011-07-19 19:51:43 +02:00
|
|
|
circ->isolation_values_set &&
|
2011-07-21 02:27:36 +02:00
|
|
|
/* It's not legal to clear a circuit's isolation info if it's ever had
|
|
|
|
* streams attached */
|
2011-07-19 19:51:43 +02:00
|
|
|
!circ->isolation_any_streams_attached) {
|
|
|
|
/* If we have any isolation information set on this circuit, and
|
|
|
|
* we didn't manage to attach any streams to it, then we can
|
|
|
|
* and should clear it and try again. */
|
|
|
|
circuit_clear_isolation(circ);
|
2011-12-06 12:46:02 +01:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Called when a circuit becomes ready for streams to be attached to
|
|
|
|
* it. */
|
|
|
|
void
|
|
|
|
circuit_try_attaching_streams(origin_circuit_t *circ)
|
|
|
|
{
|
|
|
|
/* Attach streams to this circuit if we can. */
|
|
|
|
connection_ap_attach_pending();
|
|
|
|
|
|
|
|
/* The call to circuit_try_clearing_isolation_state here will do
|
|
|
|
* nothing and return 0 if we didn't attach any streams to circ
|
|
|
|
* above. */
|
|
|
|
if (circuit_try_clearing_isolation_state(circ)) {
|
|
|
|
/* Maybe *now* we can attach some streams to this circuit. */
|
|
|
|
connection_ap_attach_pending();
|
2011-07-19 19:51:43 +02:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Called whenever a circuit could not be successfully built.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_build_failed(origin_circuit_t *circ)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2012-08-08 15:02:47 +02:00
|
|
|
channel_t *n_chan = NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
/* we should examine circ and see if it failed because of
|
|
|
|
* the last hop or an earlier hop. then use this info below.
|
|
|
|
*/
|
|
|
|
int failed_at_last_hop = 0;
|
|
|
|
/* If the last hop isn't open, and the second-to-last is, we failed
|
|
|
|
* at the last hop. */
|
|
|
|
if (circ->cpath &&
|
|
|
|
circ->cpath->prev->state != CPATH_STATE_OPEN &&
|
|
|
|
circ->cpath->prev->prev->state == CPATH_STATE_OPEN) {
|
2005-11-19 07:57:44 +01:00
|
|
|
failed_at_last_hop = 1;
|
|
|
|
}
|
|
|
|
if (circ->cpath &&
|
|
|
|
circ->cpath->state != CPATH_STATE_OPEN) {
|
|
|
|
/* We failed at the first hop. If there's an OR connection
|
2009-01-07 01:30:12 +01:00
|
|
|
* to blame, blame it. Also, avoid this relay for a while, and
|
|
|
|
* fail any one-hop directory fetches destined for it. */
|
2012-08-08 15:02:47 +02:00
|
|
|
const char *n_chan_id = circ->cpath->extend_info->identity_digest;
|
2010-09-29 04:32:38 +02:00
|
|
|
int already_marked = 0;
|
2012-10-12 18:22:13 +02:00
|
|
|
if (circ->base_.n_chan) {
|
|
|
|
n_chan = circ->base_.n_chan;
|
2012-08-08 15:02:47 +02:00
|
|
|
|
2012-10-09 09:51:33 +02:00
|
|
|
if (n_chan->is_bad_for_new_circs) {
|
2010-09-29 05:27:00 +02:00
|
|
|
/* We only want to blame this router when a fresh healthy
|
|
|
|
* connection fails. So don't mark this router as newly failed,
|
|
|
|
* since maybe this was just an old circuit attempt that's
|
|
|
|
* finally timing out now. Also, there's no need to blow away
|
|
|
|
* circuits/streams/etc, since the failure of an unhealthy conn
|
|
|
|
* doesn't tell us much about whether a healthy conn would
|
|
|
|
* succeed. */
|
2010-09-29 04:32:38 +02:00
|
|
|
already_marked = 1;
|
|
|
|
}
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_OR,
|
|
|
|
"Our circuit failed to get a response from the first hop "
|
2012-08-08 15:02:47 +02:00
|
|
|
"(%s). I'm going to try to rotate to a better connection.",
|
|
|
|
channel_get_canonical_remote_descr(n_chan));
|
2012-10-09 09:51:33 +02:00
|
|
|
n_chan->is_bad_for_new_circs = 1;
|
2009-09-18 11:01:39 +02:00
|
|
|
} else {
|
|
|
|
log_info(LD_OR,
|
|
|
|
"Our circuit died before the first hop with no connection");
|
2005-11-19 07:57:44 +01:00
|
|
|
}
|
2012-08-08 15:02:47 +02:00
|
|
|
if (n_chan_id && !already_marked) {
|
|
|
|
entry_guard_register_connect_status(n_chan_id, 0, 1, time(NULL));
|
2009-01-06 22:42:05 +01:00
|
|
|
/* if there are any one-hop streams waiting on this circuit, fail
|
|
|
|
* them now so they can retry elsewhere. */
|
2012-08-08 15:02:47 +02:00
|
|
|
connection_ap_fail_onehop(n_chan_id, circ->build_state);
|
2009-01-06 22:42:05 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
switch (circ->base_.purpose) {
|
2004-05-13 09:24:49 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
2006-06-05 10:25:02 +02:00
|
|
|
/* If we never built the circuit, note it as a failure. */
|
|
|
|
circuit_increment_failure_count();
|
2006-10-01 08:41:13 +02:00
|
|
|
if (failed_at_last_hop) {
|
|
|
|
/* Make sure any streams that demand our last hop as their exit
|
|
|
|
* know that it's unlikely to happen. */
|
|
|
|
circuit_discard_optional_exit_enclaves(circ->cpath->prev->extend_info);
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
break;
|
2005-02-27 10:47:01 +01:00
|
|
|
case CIRCUIT_PURPOSE_TESTING:
|
|
|
|
circuit_testing_failed(circ, failed_at_last_hop);
|
|
|
|
break;
|
2004-05-13 09:24:49 +02:00
|
|
|
case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
|
|
|
|
/* at Bob, waiting for introductions */
|
2012-10-12 18:22:13 +02:00
|
|
|
if (circ->base_.state != CIRCUIT_STATE_OPEN) {
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_increment_failure_count();
|
|
|
|
}
|
|
|
|
/* no need to care here, because bob will rebuild intro
|
|
|
|
* points periodically. */
|
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCING:
|
|
|
|
/* at Alice, connecting to intro point */
|
|
|
|
/* Don't increment failure count, since Bob may have picked
|
|
|
|
* the introduction point maliciously */
|
|
|
|
/* Alice will pick a new intro point when this one dies, if
|
|
|
|
* the stream in question still cares. No need to act here. */
|
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
|
|
|
|
/* at Alice, waiting for Bob */
|
2006-06-05 10:25:02 +02:00
|
|
|
circuit_increment_failure_count();
|
2004-05-13 09:24:49 +02:00
|
|
|
/* Alice will pick a new rend point when this one dies, if
|
|
|
|
* the stream in question still cares. No need to act here. */
|
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_S_CONNECT_REND:
|
|
|
|
/* at Bob, connecting to rend point */
|
|
|
|
/* Don't increment failure count, since Alice may have picked
|
|
|
|
* the rendezvous point maliciously */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_REND,
|
|
|
|
"Couldn't connect to Alice's chosen rend point %s "
|
|
|
|
"(%s hop failed).",
|
2006-03-05 10:50:26 +01:00
|
|
|
escaped(build_state_get_exit_nickname(circ->build_state)),
|
2006-02-13 09:28:42 +01:00
|
|
|
failed_at_last_hop?"last":"non-last");
|
2005-03-19 05:38:59 +01:00
|
|
|
rend_service_relaunch_rendezvous(circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
break;
|
2006-10-10 00:26:21 +02:00
|
|
|
/* default:
|
|
|
|
* This won't happen in normal operation, but might happen if the
|
|
|
|
* controller did it. Just let it slide. */
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Number of consecutive failures so far; should only be touched by
|
|
|
|
* circuit_launch_new and circuit_*_failure_count.
|
|
|
|
*/
|
|
|
|
static int n_circuit_failures = 0;
|
2007-02-16 21:39:37 +01:00
|
|
|
/** Before the last time we called circuit_reset_failure_count(), were
|
|
|
|
* there a lot of failures? */
|
2004-08-18 10:51:04 +02:00
|
|
|
static int did_circs_fail_last_period = 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/** Don't retry launching a new circuit if we try this many times with no
|
|
|
|
* success. */
|
|
|
|
#define MAX_CIRCUIT_FAILURES 5
|
|
|
|
|
2005-07-14 10:43:19 +02:00
|
|
|
/** Launch a new circuit; see circuit_launch_by_extend_info() for
|
|
|
|
* details on arguments. */
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
circuit_launch(uint8_t purpose, int flags)
|
2004-07-03 01:40:03 +02:00
|
|
|
{
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
return circuit_launch_by_extend_info(purpose, NULL, flags);
|
2005-06-29 23:46:55 +02:00
|
|
|
}
|
|
|
|
|
2007-07-29 06:38:21 +02:00
|
|
|
/** Launch a new circuit with purpose <b>purpose</b> and exit node
|
2007-12-23 20:15:22 +01:00
|
|
|
* <b>extend_info</b> (or NULL to select a random exit node). If flags
|
|
|
|
* contains CIRCLAUNCH_NEED_UPTIME, choose among routers with high uptime. If
|
|
|
|
* CIRCLAUNCH_NEED_CAPACITY is set, choose among routers with high bandwidth.
|
|
|
|
* If CIRCLAUNCH_IS_INTERNAL is true, the last hop need not be an exit node.
|
|
|
|
* If CIRCLAUNCH_ONEHOP_TUNNEL is set, the circuit will have only one hop.
|
|
|
|
* Return the newly allocated circuit on success, or NULL on failure. */
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *
|
2007-12-23 20:15:22 +01:00
|
|
|
circuit_launch_by_extend_info(uint8_t purpose,
|
|
|
|
extend_info_t *extend_info,
|
|
|
|
int flags)
|
2005-06-29 23:46:55 +02:00
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *circ;
|
2008-07-01 01:27:11 +02:00
|
|
|
int onehop_tunnel = (flags & CIRCLAUNCH_ONEHOP_TUNNEL) != 0;
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
|
2006-12-13 03:49:45 +01:00
|
|
|
if (!onehop_tunnel && !router_have_minimum_dir_info()) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,"Haven't fetched enough directory info yet; canceling "
|
|
|
|
"circuit launch.");
|
2004-07-13 03:25:39 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-11-15 04:05:23 +01:00
|
|
|
if ((extend_info || purpose != CIRCUIT_PURPOSE_C_GENERAL) &&
|
2006-12-13 03:49:45 +01:00
|
|
|
purpose != CIRCUIT_PURPOSE_TESTING && !onehop_tunnel) {
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
/* see if there are appropriate circs available to cannibalize. */
|
2008-12-18 17:11:03 +01:00
|
|
|
/* XXX if we're planning to add a hop, perhaps we want to look for
|
2007-10-28 09:16:19 +01:00
|
|
|
* internal circs rather than exit circs? -RD */
|
2007-12-23 20:15:22 +01:00
|
|
|
circ = circuit_find_to_cannibalize(purpose, extend_info, flags);
|
2005-11-11 20:25:30 +01:00
|
|
|
if (circ) {
|
2012-10-12 18:22:13 +02:00
|
|
|
uint8_t old_purpose = circ->base_.purpose;
|
2012-10-26 02:43:10 +02:00
|
|
|
struct timeval old_timestamp_began;
|
2011-06-25 22:46:20 +02:00
|
|
|
|
2011-04-08 19:27:25 +02:00
|
|
|
log_info(LD_CIRC,"Cannibalizing circ '%s' for purpose %d (%s)",
|
|
|
|
build_state_get_exit_nickname(circ->build_state), purpose,
|
|
|
|
circuit_purpose_to_string(purpose));
|
2011-06-25 22:46:20 +02:00
|
|
|
|
2012-12-08 23:16:29 +01:00
|
|
|
if ((purpose == CIRCUIT_PURPOSE_S_CONNECT_REND ||
|
|
|
|
purpose == CIRCUIT_PURPOSE_C_INTRODUCING) &&
|
2012-12-08 21:07:58 +01:00
|
|
|
circ->path_state == PATH_STATE_BUILD_SUCCEEDED) {
|
|
|
|
/* Path bias: Cannibalized rends pre-emptively count as a
|
|
|
|
* successfully used circ. We don't wait until the extend,
|
2012-12-10 08:47:04 +01:00
|
|
|
* because the rend point could be malicious.
|
2012-12-08 23:16:29 +01:00
|
|
|
*
|
|
|
|
* Same deal goes for client side introductions. Clients
|
|
|
|
* can be manipulated to connect repeatedly to them
|
2012-12-10 04:18:04 +01:00
|
|
|
* (especially web clients).
|
|
|
|
*
|
|
|
|
* If we decide to probe the initial portion of these circs,
|
|
|
|
* (up to the adversaries final hop), we need to remove this.
|
|
|
|
*/
|
2012-12-08 21:07:58 +01:00
|
|
|
circ->path_state = PATH_STATE_USE_SUCCEEDED;
|
|
|
|
/* This must be called before the purpose change */
|
2012-12-10 05:02:08 +01:00
|
|
|
pathbias_check_close(circ, END_CIRC_REASON_FINISHED);
|
2012-12-08 21:07:58 +01:00
|
|
|
}
|
|
|
|
|
2011-11-24 15:52:38 +01:00
|
|
|
circuit_change_purpose(TO_CIRCUIT(circ), purpose);
|
2012-10-26 02:43:10 +02:00
|
|
|
/* Reset the start date of this circ, else expire_building
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
* will see it and think it's been trying to build since it
|
2012-10-26 02:43:10 +02:00
|
|
|
* began.
|
|
|
|
*
|
|
|
|
* Technically, the code should reset this when the
|
|
|
|
* create cell is finally sent, but we're close enough
|
|
|
|
* here. */
|
|
|
|
tor_gettimeofday(&circ->base_.timestamp_began);
|
2011-06-25 22:46:20 +02:00
|
|
|
|
2012-01-11 16:28:20 +01:00
|
|
|
control_event_circuit_cannibalized(circ, old_purpose,
|
2012-10-26 02:43:10 +02:00
|
|
|
&old_timestamp_began);
|
2011-06-25 22:46:20 +02:00
|
|
|
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
switch (purpose) {
|
|
|
|
case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
|
|
|
|
case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
|
|
|
|
/* it's ready right now */
|
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCING:
|
|
|
|
case CIRCUIT_PURPOSE_S_CONNECT_REND:
|
2005-08-15 05:25:40 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
/* need to add a new hop */
|
2005-11-15 04:05:23 +01:00
|
|
|
tor_assert(extend_info);
|
|
|
|
if (circuit_extend_to_new_exit(circ, extend_info) < 0)
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
default:
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_BUG,
|
2007-03-04 21:11:46 +01:00
|
|
|
"unexpected purpose %d when cannibalizing a circ.",
|
2006-02-13 09:28:42 +01:00
|
|
|
purpose);
|
2005-04-26 20:52:16 +02:00
|
|
|
tor_fragile_assert();
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-18 10:51:04 +02:00
|
|
|
if (did_circs_fail_last_period &&
|
|
|
|
n_circuit_failures > MAX_CIRCUIT_FAILURES) {
|
2004-05-13 09:24:49 +02:00
|
|
|
/* too many failed circs in a row. don't try. */
|
|
|
|
// log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
/* try a circ. if it fails, circuit_mark_for_close will increment
|
|
|
|
* n_circuit_failures */
|
2007-12-23 20:15:22 +01:00
|
|
|
return circuit_establish_circuit(purpose, extend_info, flags);
|
2004-07-03 01:40:03 +02:00
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Record another failure at opening a general circuit. When we have
|
|
|
|
* too many, we'll stop trying for the remainder of this minute.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
circuit_increment_failure_count(void)
|
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
++n_circuit_failures;
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,"n_circuit_failures now %d.",n_circuit_failures);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Reset the failure count for opening general circuits. This means
|
|
|
|
* we will try MAX_CIRCUIT_FAILURES times more (if necessary) before
|
|
|
|
* stopping again.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
circuit_reset_failure_count(int timeout)
|
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
if (timeout && n_circuit_failures > MAX_CIRCUIT_FAILURES)
|
2004-08-18 10:51:04 +02:00
|
|
|
did_circs_fail_last_period = 1;
|
|
|
|
else
|
|
|
|
did_circs_fail_last_period = 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
n_circuit_failures = 0;
|
|
|
|
}
|
|
|
|
|
2006-09-21 23:48:42 +02:00
|
|
|
/** Find an open circ that we're happy to use for <b>conn</b> and return 1. If
|
|
|
|
* there isn't one, and there isn't one on the way, launch one and return
|
|
|
|
* 0. If it will never work, return -1.
|
2004-05-13 09:24:49 +02:00
|
|
|
*
|
|
|
|
* Write the found or in-progress or launched circ into *circp.
|
|
|
|
*/
|
|
|
|
static int
|
2011-07-20 18:55:42 +02:00
|
|
|
circuit_get_open_circ_or_launch(entry_connection_t *conn,
|
2004-05-13 09:24:49 +02:00
|
|
|
uint8_t desired_circuit_purpose,
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t **circp)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *circ;
|
2006-12-13 01:28:56 +01:00
|
|
|
int check_exit_policy;
|
2005-11-25 09:08:56 +01:00
|
|
|
int need_uptime, need_internal;
|
2006-12-13 03:49:45 +01:00
|
|
|
int want_onehop;
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
tor_assert(conn);
|
|
|
|
tor_assert(circp);
|
2011-07-20 18:55:42 +02:00
|
|
|
tor_assert(ENTRY_TO_CONN(conn)->state == AP_CONN_STATE_CIRCUIT_WAIT);
|
2006-12-13 01:28:56 +01:00
|
|
|
check_exit_policy =
|
2007-10-28 09:16:19 +01:00
|
|
|
conn->socks_request->command == SOCKS_COMMAND_CONNECT &&
|
|
|
|
!conn->use_begindir &&
|
2011-07-20 18:55:42 +02:00
|
|
|
!connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(conn));
|
2007-10-28 09:16:19 +01:00
|
|
|
want_onehop = conn->want_onehop;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2007-10-28 09:16:19 +01:00
|
|
|
need_uptime = !conn->want_onehop && !conn->use_begindir &&
|
2007-07-29 06:38:21 +02:00
|
|
|
smartlist_string_num_isin(options->LongLivedPorts,
|
2005-11-25 09:08:56 +01:00
|
|
|
conn->socks_request->port);
|
2012-05-15 15:41:45 +02:00
|
|
|
|
|
|
|
if (desired_circuit_purpose != CIRCUIT_PURPOSE_C_GENERAL)
|
|
|
|
need_internal = 1;
|
|
|
|
else if (conn->use_begindir || conn->want_onehop)
|
|
|
|
need_internal = 1;
|
|
|
|
else
|
|
|
|
need_internal = 0;
|
2005-11-25 09:08:56 +01:00
|
|
|
|
|
|
|
circ = circuit_get_best(conn, 1, desired_circuit_purpose,
|
|
|
|
need_uptime, need_internal);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ) {
|
2004-05-13 09:24:49 +02:00
|
|
|
*circp = circ;
|
|
|
|
return 1; /* we're happy */
|
|
|
|
}
|
|
|
|
|
2006-12-13 03:49:45 +01:00
|
|
|
if (!want_onehop && !router_have_minimum_dir_info()) {
|
2005-01-07 16:57:57 +01:00
|
|
|
if (!connection_get_by_type(CONN_TYPE_DIR)) {
|
2008-08-03 07:35:18 +02:00
|
|
|
int severity = LOG_NOTICE;
|
2009-05-27 23:55:51 +02:00
|
|
|
/* FFFF if this is a tunneled directory fetch, don't yell
|
2008-08-03 07:35:18 +02:00
|
|
|
* as loudly. the user doesn't even know it's happening. */
|
2010-09-24 04:41:01 +02:00
|
|
|
if (entry_list_is_constrained(options) &&
|
|
|
|
entries_known_but_down(options)) {
|
2008-08-03 07:35:18 +02:00
|
|
|
log_fn(severity, LD_APP|LD_DIR,
|
2010-07-31 22:20:21 +02:00
|
|
|
"Application request when we haven't used client functionality "
|
2010-09-24 04:41:01 +02:00
|
|
|
"lately. Optimistically trying known %s again.",
|
|
|
|
options->UseBridges ? "bridges" : "entrynodes");
|
|
|
|
entries_retry_all(options);
|
2007-07-29 06:38:21 +02:00
|
|
|
} else if (!options->UseBridges || any_bridge_descriptors_known()) {
|
2008-08-03 07:35:18 +02:00
|
|
|
log_fn(severity, LD_APP|LD_DIR,
|
2010-07-31 22:20:21 +02:00
|
|
|
"Application request when we haven't used client functionality "
|
|
|
|
"lately. Optimistically trying directory fetches again.");
|
2007-07-29 06:38:21 +02:00
|
|
|
routerlist_retry_directory_downloads(time(NULL));
|
|
|
|
}
|
2005-01-07 16:57:57 +01:00
|
|
|
}
|
2005-12-15 21:44:15 +01:00
|
|
|
/* the stream will be dealt with when router_have_minimum_dir_info becomes
|
2005-01-07 16:57:57 +01:00
|
|
|
* 1, or when all directory attempts fail and directory_all_unreachable()
|
|
|
|
* kills it.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-17 20:13:09 +02:00
|
|
|
/* Do we need to check exit policy? */
|
2006-12-13 01:28:56 +01:00
|
|
|
if (check_exit_policy) {
|
2008-10-21 19:09:04 +02:00
|
|
|
if (!conn->chosen_exit_name) {
|
|
|
|
struct in_addr in;
|
2011-07-15 18:58:13 +02:00
|
|
|
tor_addr_t addr, *addrp=NULL;
|
|
|
|
if (tor_inet_aton(conn->socks_request->address, &in)) {
|
|
|
|
tor_addr_from_in(&addr, &in);
|
|
|
|
addrp = &addr;
|
|
|
|
}
|
|
|
|
if (router_exit_policy_all_nodes_reject(addrp,
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
conn->socks_request->port,
|
|
|
|
need_uptime)) {
|
2008-10-21 19:09:04 +02:00
|
|
|
log_notice(LD_APP,
|
2008-11-07 02:48:55 +01:00
|
|
|
"No Tor server allows exit to %s:%d. Rejecting.",
|
2009-12-15 23:23:36 +01:00
|
|
|
safe_str_client(conn->socks_request->address),
|
2008-10-21 19:09:04 +02:00
|
|
|
conn->socks_request->port);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2012-06-15 15:37:40 +02:00
|
|
|
/* XXXX024 Duplicates checks in connection_ap_handshake_attach_circuit:
|
2011-03-25 21:01:16 +01:00
|
|
|
* refactor into a single function? */
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const node_t *node = node_get_by_nickname(conn->chosen_exit_name, 1);
|
2008-12-17 15:59:28 +01:00
|
|
|
int opt = conn->chosen_exit_optional;
|
2011-04-27 20:36:30 +02:00
|
|
|
if (node && !connection_ap_can_use_exit(conn, node)) {
|
2008-10-21 19:09:04 +02:00
|
|
|
log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
|
2011-04-27 01:55:34 +02:00
|
|
|
"Requested exit point '%s' is excluded or "
|
|
|
|
"would refuse request. %s.",
|
2008-10-21 19:09:04 +02:00
|
|
|
conn->chosen_exit_name, opt ? "Trying others" : "Closing");
|
|
|
|
if (opt) {
|
2008-12-17 15:59:28 +01:00
|
|
|
conn->chosen_exit_optional = 0;
|
2008-10-21 19:09:04 +02:00
|
|
|
tor_free(conn->chosen_exit_name);
|
|
|
|
/* Try again. */
|
|
|
|
return circuit_get_open_circ_or_launch(conn,
|
|
|
|
desired_circuit_purpose,
|
|
|
|
circp);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* is one already on the way? */
|
2005-11-25 09:08:56 +01:00
|
|
|
circ = circuit_get_best(conn, 0, desired_circuit_purpose,
|
|
|
|
need_uptime, need_internal);
|
2007-10-28 09:16:19 +01:00
|
|
|
if (circ)
|
|
|
|
log_debug(LD_CIRC, "one on the way!");
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!circ) {
|
2005-06-29 23:46:55 +02:00
|
|
|
extend_info_t *extend_info=NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
uint8_t new_circ_purpose;
|
2011-07-07 20:54:54 +02:00
|
|
|
const int n_pending = count_pending_general_client_circuits();
|
|
|
|
|
|
|
|
if (n_pending >= options->MaxClientCircuitsPending) {
|
|
|
|
static ratelim_t delay_limit = RATELIM_INIT(10*60);
|
|
|
|
char *m;
|
|
|
|
if ((m = rate_limit_log(&delay_limit, approx_time()))) {
|
|
|
|
log_notice(LD_APP, "We'd like to launch a circuit to handle a "
|
|
|
|
"connection, but we already have %d general-purpose client "
|
2012-10-03 19:03:09 +02:00
|
|
|
"circuits pending. Waiting until some finish.%s",
|
|
|
|
n_pending, m);
|
2011-07-07 20:54:54 +02:00
|
|
|
tor_free(m);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
|
2004-05-13 09:24:49 +02:00
|
|
|
/* need to pick an intro point */
|
2011-07-20 18:55:42 +02:00
|
|
|
rend_data_t *rend_data = ENTRY_TO_EDGE_CONN(conn)->rend_data;
|
|
|
|
tor_assert(rend_data);
|
|
|
|
extend_info = rend_client_get_random_intro(rend_data);
|
2005-06-29 23:46:55 +02:00
|
|
|
if (!extend_info) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_REND,
|
2009-05-27 23:55:51 +02:00
|
|
|
"No intro points for '%s': re-fetching service descriptor.",
|
2011-07-20 18:55:42 +02:00
|
|
|
safe_str_client(rend_data->onion_address));
|
|
|
|
rend_client_refetch_v2_renddesc(rend_data);
|
|
|
|
ENTRY_TO_CONN(conn)->state = AP_CONN_STATE_RENDDESC_WAIT;
|
2005-01-20 00:15:59 +01:00
|
|
|
return 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2011-05-16 03:58:46 +02:00
|
|
|
log_info(LD_REND,"Chose %s as intro point for '%s'.",
|
|
|
|
extend_info_describe(extend_info),
|
2011-07-20 18:55:42 +02:00
|
|
|
safe_str_client(rend_data->onion_address));
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2004-11-29 09:34:54 +01:00
|
|
|
/* If we have specified a particular exit node for our
|
|
|
|
* connection, then be sure to open a circuit to that exit node.
|
|
|
|
*/
|
2004-11-30 03:26:41 +01:00
|
|
|
if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_GENERAL) {
|
2004-11-29 09:34:54 +01:00
|
|
|
if (conn->chosen_exit_name) {
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const node_t *r;
|
2008-12-17 15:59:28 +01:00
|
|
|
int opt = conn->chosen_exit_optional;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
r = node_get_by_nickname(conn->chosen_exit_name, 1);
|
2010-10-05 05:51:30 +02:00
|
|
|
if (r && node_has_descriptor(r)) {
|
2011-11-24 18:29:56 +01:00
|
|
|
/* We might want to connect to an IPv6 bridge for loading
|
|
|
|
descriptors so we use the preferred address rather than
|
|
|
|
the primary. */
|
|
|
|
extend_info = extend_info_from_node(r, conn->want_onehop ? 1 : 0);
|
2007-01-27 20:29:16 +01:00
|
|
|
} else {
|
2007-03-01 04:56:21 +01:00
|
|
|
log_debug(LD_DIR, "considering %d, %s",
|
|
|
|
want_onehop, conn->chosen_exit_name);
|
2007-01-27 20:29:16 +01:00
|
|
|
if (want_onehop && conn->chosen_exit_name[0] == '$') {
|
|
|
|
/* We're asking for a one-hop circuit to a router that
|
2007-06-12 11:17:23 +02:00
|
|
|
* we don't have a routerinfo about. Make up an extend_info. */
|
|
|
|
char digest[DIGEST_LEN];
|
|
|
|
char *hexdigest = conn->chosen_exit_name+1;
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_t addr;
|
2007-06-12 11:17:23 +02:00
|
|
|
if (strlen(hexdigest) < HEX_DIGEST_LEN ||
|
|
|
|
base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN)<0) {
|
|
|
|
log_info(LD_DIR, "Broken exit digest on tunnel conn. Closing.");
|
2007-01-27 20:29:16 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2011-10-11 17:21:31 +02:00
|
|
|
if (tor_addr_parse(&addr, conn->socks_request->address) < 0) {
|
2008-08-05 22:08:19 +02:00
|
|
|
log_info(LD_DIR, "Broken address %s on tunnel conn. Closing.",
|
2009-12-15 23:23:36 +01:00
|
|
|
escaped_safe_str_client(conn->socks_request->address));
|
2007-06-12 11:17:23 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2012-09-06 17:38:32 +02:00
|
|
|
extend_info = extend_info_new(conn->chosen_exit_name+1,
|
2012-12-06 07:53:29 +01:00
|
|
|
digest, NULL, NULL, &addr,
|
|
|
|
conn->socks_request->port);
|
2007-01-27 20:29:16 +01:00
|
|
|
} else {
|
|
|
|
/* We will need an onion key for the router, and we
|
|
|
|
* don't have one. Refuse or relax requirements. */
|
|
|
|
log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
|
|
|
|
"Requested exit point '%s' is not known. %s.",
|
|
|
|
conn->chosen_exit_name, opt ? "Trying others" : "Closing");
|
|
|
|
if (opt) {
|
2008-12-17 15:59:28 +01:00
|
|
|
conn->chosen_exit_optional = 0;
|
2007-01-27 20:29:16 +01:00
|
|
|
tor_free(conn->chosen_exit_name);
|
2009-02-09 17:55:48 +01:00
|
|
|
/* Try again with no requested exit */
|
|
|
|
return circuit_get_open_circ_or_launch(conn,
|
|
|
|
desired_circuit_purpose,
|
|
|
|
circp);
|
2007-01-27 20:29:16 +01:00
|
|
|
}
|
|
|
|
return -1;
|
2006-07-18 02:59:46 +02:00
|
|
|
}
|
2004-11-29 09:34:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_REND_JOINED)
|
2004-05-13 09:24:49 +02:00
|
|
|
new_circ_purpose = CIRCUIT_PURPOSE_C_ESTABLISH_REND;
|
2004-11-28 10:05:49 +01:00
|
|
|
else if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
|
2004-05-13 09:24:49 +02:00
|
|
|
new_circ_purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
|
|
|
|
else
|
|
|
|
new_circ_purpose = desired_circuit_purpose;
|
|
|
|
|
2011-06-01 11:00:59 +02:00
|
|
|
if (options->Tor2webMode &&
|
|
|
|
(new_circ_purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND ||
|
|
|
|
new_circ_purpose == CIRCUIT_PURPOSE_C_INTRODUCING)) {
|
|
|
|
want_onehop = 1;
|
|
|
|
}
|
|
|
|
|
2007-12-23 20:15:22 +01:00
|
|
|
{
|
|
|
|
int flags = CIRCLAUNCH_NEED_CAPACITY;
|
|
|
|
if (want_onehop) flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
|
|
|
|
if (need_uptime) flags |= CIRCLAUNCH_NEED_UPTIME;
|
|
|
|
if (need_internal) flags |= CIRCLAUNCH_IS_INTERNAL;
|
|
|
|
circ = circuit_launch_by_extend_info(new_circ_purpose, extend_info,
|
|
|
|
flags);
|
|
|
|
}
|
|
|
|
|
2009-12-12 08:07:59 +01:00
|
|
|
extend_info_free(extend_info);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2011-04-27 04:18:01 +02:00
|
|
|
if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_GENERAL) {
|
|
|
|
/* We just caused a circuit to get built because of this stream.
|
|
|
|
* If this stream has caused a _lot_ of circuits to be built, that's
|
|
|
|
* a bad sign: we should tell the user. */
|
|
|
|
if (conn->num_circuits_launched < NUM_CIRCUITS_LAUNCHED_THRESHOLD &&
|
|
|
|
++conn->num_circuits_launched == NUM_CIRCUITS_LAUNCHED_THRESHOLD)
|
2011-05-13 01:41:08 +02:00
|
|
|
log_info(LD_CIRC, "The application request to %s:%d has launched "
|
2011-04-27 04:18:01 +02:00
|
|
|
"%d circuits without finding one it likes.",
|
|
|
|
escaped_safe_str_client(conn->socks_request->address),
|
|
|
|
conn->socks_request->port,
|
|
|
|
conn->num_circuits_launched);
|
|
|
|
} else {
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
/* help predict this next time */
|
2005-11-25 09:08:56 +01:00
|
|
|
rep_hist_note_used_internal(time(NULL), need_uptime, 1);
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
if (circ) {
|
|
|
|
/* write the service_id into circ */
|
2011-07-20 18:55:42 +02:00
|
|
|
circ->rend_data = rend_data_dup(ENTRY_TO_EDGE_CONN(conn)->rend_data);
|
2012-10-12 18:22:13 +02:00
|
|
|
if (circ->base_.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
|
|
|
|
circ->base_.state == CIRCUIT_STATE_OPEN)
|
2005-08-12 03:26:21 +02:00
|
|
|
rend_client_rendcirc_has_opened(circ);
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
Launch sufficient circuits to satisfy pending isolated streams
Our old "do we need to launch a circuit for stream S" logic was,
more or less, that if we had a pending circuit that could handle S,
we didn't need to launch a new one.
But now that we have streams isolated from one another, we need
something stronger here: It's possible that some pending C can
handle either S1 or S2, but not both.
This patch reuses the existing isolation logic for a simple
solution: when we decide during circuit launching that some pending
C would satisfy stream S1, we "hypothetically" mark C as though S1
had been connected to it. Now if S2 is incompatible with S1, it
won't be something that can attach to C, and so we'll launch a new
stream.
When the circuit becomes OPEN for the first time (with no streams
attached to it), we reset the circuit's isolation status. I'm not
too sure about this part: I wanted some way to be sure that, if all
streams that would have used a circuit die before the circuit is
done, the circuit can still get used. But I worry that this
approach could also lead to us launching too many circuits. Careful
thought needed here.
2011-07-07 16:40:23 +02:00
|
|
|
} /* endif (!circ) */
|
|
|
|
if (circ) {
|
|
|
|
/* Mark the circuit with the isolation fields for this connection.
|
|
|
|
* When the circuit arrives, we'll clear these flags: this is
|
|
|
|
* just some internal bookkeeping to make sure that we have
|
|
|
|
* launched enough circuits.
|
|
|
|
*/
|
|
|
|
connection_edge_update_circuit_isolation(conn, circ, 0);
|
|
|
|
} else {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_APP,
|
|
|
|
"No safe circuit (purpose %d) ready for edge "
|
|
|
|
"connection; delaying.",
|
|
|
|
desired_circuit_purpose);
|
Launch sufficient circuits to satisfy pending isolated streams
Our old "do we need to launch a circuit for stream S" logic was,
more or less, that if we had a pending circuit that could handle S,
we didn't need to launch a new one.
But now that we have streams isolated from one another, we need
something stronger here: It's possible that some pending C can
handle either S1 or S2, but not both.
This patch reuses the existing isolation logic for a simple
solution: when we decide during circuit launching that some pending
C would satisfy stream S1, we "hypothetically" mark C as though S1
had been connected to it. Now if S2 is incompatible with S1, it
won't be something that can attach to C, and so we'll launch a new
stream.
When the circuit becomes OPEN for the first time (with no streams
attached to it), we reset the circuit's isolation status. I'm not
too sure about this part: I wanted some way to be sure that, if all
streams that would have used a circuit die before the circuit is
done, the circuit can still get used. But I worry that this
approach could also lead to us launching too many circuits. Careful
thought needed here.
2011-07-07 16:40:23 +02:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
*circp = circ;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-30 16:09:11 +02:00
|
|
|
/** Return true iff <b>crypt_path</b> is one of the crypt_paths for
|
2007-05-02 11:12:04 +02:00
|
|
|
* <b>circ</b>. */
|
2007-04-30 16:09:11 +02:00
|
|
|
static int
|
|
|
|
cpath_is_on_circuit(origin_circuit_t *circ, crypt_path_t *crypt_path)
|
|
|
|
{
|
|
|
|
crypt_path_t *cpath, *cpath_next = NULL;
|
|
|
|
for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
|
|
|
|
cpath_next = cpath->next;
|
|
|
|
if (crypt_path == cpath)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-20 16:38:00 +02:00
|
|
|
/** Return true iff client-side optimistic data is supported. */
|
|
|
|
static int
|
|
|
|
optimistic_data_enabled(void)
|
|
|
|
{
|
|
|
|
const or_options_t *options = get_options();
|
|
|
|
if (options->OptimisticData < 0) {
|
2011-07-21 06:34:58 +02:00
|
|
|
/* XXX023 consider having auto default to 1 rather than 0 before
|
|
|
|
* the 0.2.3 branch goes stable. See bug 3617. -RD */
|
2011-07-20 16:38:00 +02:00
|
|
|
const int32_t enabled =
|
|
|
|
networkstatus_get_param(NULL, "UseOptimisticData", 0, 0, 1);
|
|
|
|
return (int)enabled;
|
|
|
|
}
|
|
|
|
return options->OptimisticData;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Attach the AP stream <b>apconn</b> to circ's linked list of
|
2007-04-30 16:09:11 +02:00
|
|
|
* p_streams. Also set apconn's cpath_layer to <b>cpath</b>, or to the last
|
|
|
|
* hop in circ's cpath if <b>cpath</b> is NULL.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
2011-07-20 18:55:42 +02:00
|
|
|
link_apconn_to_circ(entry_connection_t *apconn, origin_circuit_t *circ,
|
2007-04-30 13:10:45 +02:00
|
|
|
crypt_path_t *cpath)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2011-07-18 19:56:22 +02:00
|
|
|
const node_t *exitnode;
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/* add it into the linked list of streams on this circuit */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_APP|LD_CIRC, "attaching new conn to circ. n_circ_id %d.",
|
2012-10-12 18:22:13 +02:00
|
|
|
circ->base_.n_circ_id);
|
2005-12-14 21:40:40 +01:00
|
|
|
/* reset it, so we can measure circ timeouts */
|
2011-07-20 18:55:42 +02:00
|
|
|
ENTRY_TO_CONN(apconn)->timestamp_lastread = time(NULL);
|
|
|
|
ENTRY_TO_EDGE_CONN(apconn)->next_stream = circ->p_streams;
|
|
|
|
ENTRY_TO_EDGE_CONN(apconn)->on_circuit = TO_CIRCUIT(circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
/* assert_connection_ok(conn, time(NULL)); */
|
2011-07-20 18:55:42 +02:00
|
|
|
circ->p_streams = ENTRY_TO_EDGE_CONN(apconn);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2011-10-03 21:13:38 +02:00
|
|
|
if (connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(apconn))) {
|
2011-09-17 12:23:26 +02:00
|
|
|
/* We are attaching a stream to a rendezvous circuit. That means
|
|
|
|
* that an attempt to connect to a hidden service just
|
|
|
|
* succeeded. Tell rendclient.c. */
|
2011-10-03 21:13:38 +02:00
|
|
|
rend_client_note_connection_attempt_ended(
|
|
|
|
ENTRY_TO_EDGE_CONN(apconn)->rend_data->onion_address);
|
2011-09-17 12:23:26 +02:00
|
|
|
}
|
|
|
|
|
2007-04-30 13:10:45 +02:00
|
|
|
if (cpath) { /* we were given one; use it */
|
2007-04-30 16:09:11 +02:00
|
|
|
tor_assert(cpath_is_on_circuit(circ, cpath));
|
2011-07-20 18:55:42 +02:00
|
|
|
} else {
|
|
|
|
/* use the last hop in the circuit */
|
2007-04-30 13:10:45 +02:00
|
|
|
tor_assert(circ->cpath);
|
|
|
|
tor_assert(circ->cpath->prev);
|
|
|
|
tor_assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
|
2011-07-20 18:55:42 +02:00
|
|
|
cpath = circ->cpath->prev;
|
2007-04-30 13:10:45 +02:00
|
|
|
}
|
2011-07-20 18:55:42 +02:00
|
|
|
ENTRY_TO_EDGE_CONN(apconn)->cpath_layer = cpath;
|
2011-07-06 23:08:24 +02:00
|
|
|
|
Launch sufficient circuits to satisfy pending isolated streams
Our old "do we need to launch a circuit for stream S" logic was,
more or less, that if we had a pending circuit that could handle S,
we didn't need to launch a new one.
But now that we have streams isolated from one another, we need
something stronger here: It's possible that some pending C can
handle either S1 or S2, but not both.
This patch reuses the existing isolation logic for a simple
solution: when we decide during circuit launching that some pending
C would satisfy stream S1, we "hypothetically" mark C as though S1
had been connected to it. Now if S2 is incompatible with S1, it
won't be something that can attach to C, and so we'll launch a new
stream.
When the circuit becomes OPEN for the first time (with no streams
attached to it), we reset the circuit's isolation status. I'm not
too sure about this part: I wanted some way to be sure that, if all
streams that would have used a circuit die before the circuit is
done, the circuit can still get used. But I worry that this
approach could also lead to us launching too many circuits. Careful
thought needed here.
2011-07-07 16:40:23 +02:00
|
|
|
circ->isolation_any_streams_attached = 1;
|
2011-07-06 23:08:24 +02:00
|
|
|
connection_edge_update_circuit_isolation(apconn, circ, 0);
|
2011-07-20 15:50:53 +02:00
|
|
|
|
2011-07-18 19:56:22 +02:00
|
|
|
/* See if we can use optimistic data on this circuit */
|
2011-07-20 18:55:42 +02:00
|
|
|
if (cpath->extend_info &&
|
|
|
|
(exitnode = node_get_by_id(cpath->extend_info->identity_digest)) &&
|
2011-07-18 19:56:22 +02:00
|
|
|
exitnode->rs) {
|
|
|
|
/* Okay; we know what exit node this is. */
|
2011-07-20 16:38:00 +02:00
|
|
|
if (optimistic_data_enabled() &&
|
2012-10-12 18:22:13 +02:00
|
|
|
circ->base_.purpose == CIRCUIT_PURPOSE_C_GENERAL &&
|
2011-07-18 19:56:22 +02:00
|
|
|
exitnode->rs->version_supports_optimistic_data)
|
2011-07-20 17:02:39 +02:00
|
|
|
apconn->may_use_optimistic_data = 1;
|
2011-07-18 19:56:22 +02:00
|
|
|
else
|
2011-07-20 17:02:39 +02:00
|
|
|
apconn->may_use_optimistic_data = 0;
|
2011-07-18 19:56:22 +02:00
|
|
|
log_info(LD_APP, "Looks like completed circuit to %s %s allow "
|
|
|
|
"optimistic data for connection to %s",
|
|
|
|
safe_str_client(node_describe(exitnode)),
|
2011-07-20 17:02:39 +02:00
|
|
|
apconn->may_use_optimistic_data ? "does" : "doesn't",
|
2011-07-18 19:56:22 +02:00
|
|
|
safe_str_client(apconn->socks_request->address));
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2011-05-13 22:22:10 +02:00
|
|
|
/** Return true iff <b>address</b> is matched by one of the entries in
|
|
|
|
* TrackHostExits. */
|
|
|
|
int
|
2011-06-14 19:01:38 +02:00
|
|
|
hostname_in_track_host_exits(const or_options_t *options, const char *address)
|
2011-05-13 22:22:10 +02:00
|
|
|
{
|
|
|
|
if (!options->TrackHostExits)
|
|
|
|
return 0;
|
|
|
|
SMARTLIST_FOREACH_BEGIN(options->TrackHostExits, const char *, cp) {
|
|
|
|
if (cp[0] == '.') { /* match end */
|
|
|
|
if (cp[1] == '\0' ||
|
|
|
|
!strcasecmpend(address, cp) ||
|
|
|
|
!strcasecmp(address, &cp[1]))
|
|
|
|
return 1;
|
|
|
|
} else if (strcasecmp(cp, address) == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(cp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-15 17:37:33 +02:00
|
|
|
/** If an exit wasn't explicitly specified for <b>conn</b>, consider saving
|
|
|
|
* the exit that we *did* choose for use by future connections to
|
|
|
|
* <b>conn</b>'s destination.
|
|
|
|
*/
|
2005-02-22 01:53:08 +01:00
|
|
|
static void
|
2011-07-20 18:55:42 +02:00
|
|
|
consider_recording_trackhost(const entry_connection_t *conn,
|
2011-07-06 22:30:02 +02:00
|
|
|
const origin_circuit_t *circ)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2011-05-15 17:41:49 +02:00
|
|
|
char *new_address = NULL;
|
2005-12-05 01:15:42 +01:00
|
|
|
char fp[HEX_DIGEST_LEN+1];
|
2005-02-22 01:53:08 +01:00
|
|
|
|
|
|
|
/* Search the addressmap for this conn's destination. */
|
|
|
|
/* If he's not in the address map.. */
|
|
|
|
if (!options->TrackHostExits ||
|
2008-09-29 16:53:53 +02:00
|
|
|
addressmap_have_mapping(conn->socks_request->address,
|
|
|
|
options->TrackHostExitsExpire))
|
2005-02-22 01:53:08 +01:00
|
|
|
return; /* nothing to track, or already mapped */
|
|
|
|
|
2011-05-13 22:22:10 +02:00
|
|
|
if (!hostname_in_track_host_exits(options, conn->socks_request->address) ||
|
|
|
|
!circ->build_state->chosen_exit)
|
2005-02-22 01:53:08 +01:00
|
|
|
return;
|
|
|
|
|
2005-12-05 01:15:42 +01:00
|
|
|
/* write down the fingerprint of the chosen exit, not the nickname,
|
2006-03-19 02:21:59 +01:00
|
|
|
* because the chosen exit might not be named. */
|
2005-12-05 01:15:42 +01:00
|
|
|
base16_encode(fp, sizeof(fp),
|
|
|
|
circ->build_state->chosen_exit->identity_digest, DIGEST_LEN);
|
|
|
|
|
2005-02-22 01:53:08 +01:00
|
|
|
/* Add this exit/hostname pair to the addressmap. */
|
2011-05-15 17:41:49 +02:00
|
|
|
tor_asprintf(&new_address, "%s.%s.exit",
|
2005-12-05 01:15:42 +01:00
|
|
|
conn->socks_request->address, fp);
|
2005-02-22 01:53:08 +01:00
|
|
|
|
|
|
|
addressmap_register(conn->socks_request->address, new_address,
|
2008-02-21 19:45:11 +01:00
|
|
|
time(NULL) + options->TrackHostExitsExpire,
|
2011-11-26 01:46:11 +01:00
|
|
|
ADDRMAPSRC_TRACKEXIT, 0, 0);
|
2005-02-22 01:53:08 +01:00
|
|
|
}
|
|
|
|
|
2007-04-30 16:09:11 +02:00
|
|
|
/** Attempt to attach the connection <b>conn</b> to <b>circ</b>, and send a
|
|
|
|
* begin or resolve cell as appropriate. Return values are as for
|
|
|
|
* connection_ap_handshake_attach_circuit. The stream will exit from the hop
|
2007-05-01 01:24:38 +02:00
|
|
|
* indicated by <b>cpath</b>, or from the last hop in circ's cpath if
|
2007-04-30 16:09:11 +02:00
|
|
|
* <b>cpath</b> is NULL. */
|
2005-03-12 05:22:01 +01:00
|
|
|
int
|
2011-07-20 18:55:42 +02:00
|
|
|
connection_ap_handshake_attach_chosen_circuit(entry_connection_t *conn,
|
2007-04-30 13:10:45 +02:00
|
|
|
origin_circuit_t *circ,
|
|
|
|
crypt_path_t *cpath)
|
2005-03-12 05:22:01 +01:00
|
|
|
{
|
2011-07-20 18:55:42 +02:00
|
|
|
connection_t *base_conn = ENTRY_TO_CONN(conn);
|
2005-03-12 05:22:01 +01:00
|
|
|
tor_assert(conn);
|
2011-07-20 18:55:42 +02:00
|
|
|
tor_assert(base_conn->state == AP_CONN_STATE_CIRCUIT_WAIT ||
|
|
|
|
base_conn->state == AP_CONN_STATE_CONTROLLER_WAIT);
|
2005-03-12 05:22:01 +01:00
|
|
|
tor_assert(conn->socks_request);
|
|
|
|
tor_assert(circ);
|
2012-10-12 18:22:13 +02:00
|
|
|
tor_assert(circ->base_.state == CIRCUIT_STATE_OPEN);
|
2005-03-24 07:05:14 +01:00
|
|
|
|
2011-07-20 18:55:42 +02:00
|
|
|
base_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
|
2005-03-12 05:22:01 +01:00
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
if (!circ->base_.timestamp_dirty)
|
|
|
|
circ->base_.timestamp_dirty = time(NULL);
|
2005-03-12 05:22:01 +01:00
|
|
|
|
2007-04-30 13:10:45 +02:00
|
|
|
link_apconn_to_circ(conn, circ, cpath);
|
2005-03-12 05:22:01 +01:00
|
|
|
tor_assert(conn->socks_request);
|
2007-10-28 09:16:19 +01:00
|
|
|
if (conn->socks_request->command == SOCKS_COMMAND_CONNECT) {
|
|
|
|
if (!conn->use_begindir)
|
2006-12-13 01:28:56 +01:00
|
|
|
consider_recording_trackhost(conn, circ);
|
2007-10-28 09:16:19 +01:00
|
|
|
if (connection_ap_handshake_send_begin(conn) < 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if (connection_ap_handshake_send_resolve(conn) < 0)
|
|
|
|
return -1;
|
2005-03-12 05:22:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Try to find a safe live circuit for CONN_TYPE_AP connection conn. If
|
|
|
|
* we don't find one: if conn cannot be handled by any known nodes,
|
2008-03-17 04:37:54 +01:00
|
|
|
* warn and return -1 (conn needs to die, and is maybe already marked);
|
2004-05-13 09:24:49 +02:00
|
|
|
* else launch new circuit (if necessary) and return 0.
|
|
|
|
* Otherwise, associate conn with a safe live circuit, do the
|
|
|
|
* right next step, and return 1.
|
|
|
|
*/
|
2008-12-18 17:11:24 +01:00
|
|
|
/* XXXX this function should mark for close whenever it returns -1;
|
2008-03-17 04:37:54 +01:00
|
|
|
* its callers shouldn't have to worry about that. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2011-07-20 18:55:42 +02:00
|
|
|
connection_ap_handshake_attach_circuit(entry_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2011-07-20 18:55:42 +02:00
|
|
|
connection_t *base_conn = ENTRY_TO_CONN(conn);
|
2004-05-13 09:24:49 +02:00
|
|
|
int retval;
|
|
|
|
int conn_age;
|
2007-03-01 04:56:21 +01:00
|
|
|
int want_onehop;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
tor_assert(conn);
|
2011-07-20 18:55:42 +02:00
|
|
|
tor_assert(base_conn->state == AP_CONN_STATE_CIRCUIT_WAIT);
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(conn->socks_request);
|
2007-10-28 09:16:19 +01:00
|
|
|
want_onehop = conn->want_onehop;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2011-07-20 18:55:42 +02:00
|
|
|
conn_age = (int)(time(NULL) - base_conn->timestamp_created);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2007-08-19 09:17:40 +02:00
|
|
|
if (conn_age >= get_options()->SocksTimeout) {
|
2011-07-20 18:55:42 +02:00
|
|
|
int severity = (tor_addr_is_null(&base_conn->addr) && !base_conn->port) ?
|
2008-08-05 22:08:19 +02:00
|
|
|
LOG_INFO : LOG_NOTICE;
|
2007-08-19 09:17:40 +02:00
|
|
|
log_fn(severity, LD_APP,
|
|
|
|
"Tried for %d seconds to get a connection to %s:%d. Giving up.",
|
2009-12-15 23:23:36 +01:00
|
|
|
conn_age, safe_str_client(conn->socks_request->address),
|
2007-08-19 09:17:40 +02:00
|
|
|
conn->socks_request->port);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-20 18:55:42 +02:00
|
|
|
if (!connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(conn))) {
|
|
|
|
/* we're a general conn */
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *circ=NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-30 09:39:14 +01:00
|
|
|
if (conn->chosen_exit_name) {
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const node_t *node = node_get_by_nickname(conn->chosen_exit_name, 1);
|
2008-12-17 15:59:28 +01:00
|
|
|
int opt = conn->chosen_exit_optional;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
if (!node && !want_onehop) {
|
2008-09-09 10:41:58 +02:00
|
|
|
/* We ran into this warning when trying to extend a circuit to a
|
|
|
|
* hidden service directory for which we didn't have a router
|
|
|
|
* descriptor. See flyspray task 767 for more details. We should
|
|
|
|
* keep this in mind when deciding to use BEGIN_DIR cells for other
|
|
|
|
* directory requests as well. -KL*/
|
2006-07-18 02:59:46 +02:00
|
|
|
log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
|
|
|
|
"Requested exit point '%s' is not known. %s.",
|
|
|
|
conn->chosen_exit_name, opt ? "Trying others" : "Closing");
|
|
|
|
if (opt) {
|
2008-12-17 15:59:28 +01:00
|
|
|
conn->chosen_exit_optional = 0;
|
2006-07-18 02:59:46 +02:00
|
|
|
tor_free(conn->chosen_exit_name);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-30 09:39:14 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2011-04-27 20:36:30 +02:00
|
|
|
if (node && !connection_ap_can_use_exit(conn, node)) {
|
2006-07-18 02:59:46 +02:00
|
|
|
log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
|
2011-04-27 01:55:34 +02:00
|
|
|
"Requested exit point '%s' is excluded or "
|
|
|
|
"would refuse request. %s.",
|
2006-07-18 02:59:46 +02:00
|
|
|
conn->chosen_exit_name, opt ? "Trying others" : "Closing");
|
|
|
|
if (opt) {
|
2008-12-17 15:59:28 +01:00
|
|
|
conn->chosen_exit_optional = 0;
|
2006-07-18 02:59:46 +02:00
|
|
|
tor_free(conn->chosen_exit_name);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-30 09:39:14 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/* find the circuit that we should use, if there is one. */
|
2005-12-14 21:40:40 +01:00
|
|
|
retval = circuit_get_open_circ_or_launch(
|
|
|
|
conn, CIRCUIT_PURPOSE_C_GENERAL, &circ);
|
2012-06-15 15:37:40 +02:00
|
|
|
if (retval < 1) // XXX023 if we totally fail, this still returns 0 -RD
|
2004-05-13 09:24:49 +02:00
|
|
|
return retval;
|
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_APP|LD_CIRC,
|
|
|
|
"Attaching apconn to circ %d (stream %d sec old).",
|
2012-10-12 18:22:13 +02:00
|
|
|
circ->base_.n_circ_id, conn_age);
|
2007-10-28 09:16:19 +01:00
|
|
|
/* print the circ's path, so people can figure out which circs are
|
2005-12-14 21:40:40 +01:00
|
|
|
* sucking. */
|
2005-10-25 09:04:36 +02:00
|
|
|
circuit_log_path(LOG_INFO,LD_APP|LD_CIRC,circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2005-03-12 05:22:01 +01:00
|
|
|
/* We have found a suitable circuit for our conn. Hurray. */
|
2007-04-30 13:10:45 +02:00
|
|
|
return connection_ap_handshake_attach_chosen_circuit(conn, circ, NULL);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
} else { /* we're a rendezvous conn */
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *rendcirc=NULL, *introcirc=NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2011-07-20 18:55:42 +02:00
|
|
|
tor_assert(!ENTRY_TO_EDGE_CONN(conn)->cpath_layer);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/* start by finding a rendezvous circuit for us */
|
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
retval = circuit_get_open_circ_or_launch(
|
|
|
|
conn, CIRCUIT_PURPOSE_C_REND_JOINED, &rendcirc);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (retval < 0) return -1; /* failed */
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (retval > 0) {
|
2004-10-12 00:19:12 +02:00
|
|
|
tor_assert(rendcirc);
|
2004-05-13 09:24:49 +02:00
|
|
|
/* one is already established, attach */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_REND,
|
|
|
|
"rend joined circ %d already here. attaching. "
|
|
|
|
"(stream %d sec old)",
|
2012-10-12 18:22:13 +02:00
|
|
|
rendcirc->base_.n_circ_id, conn_age);
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
/* Mark rendezvous circuits as 'newly dirty' every time you use
|
|
|
|
* them, since the process of rebuilding a rendezvous circ is so
|
2009-05-27 23:55:51 +02:00
|
|
|
* expensive. There is a tradeoff between linkability and
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
* feasibility, at this point.
|
|
|
|
*/
|
2012-10-12 18:22:13 +02:00
|
|
|
rendcirc->base_.timestamp_dirty = time(NULL);
|
2007-04-30 13:10:45 +02:00
|
|
|
link_apconn_to_circ(conn, rendcirc, NULL);
|
2007-03-24 16:58:11 +01:00
|
|
|
if (connection_ap_handshake_send_begin(conn) < 0)
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0; /* already marked, let them fade away */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
if (rendcirc && (rendcirc->base_.purpose ==
|
2005-12-14 21:40:40 +01:00
|
|
|
CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED)) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_REND,
|
|
|
|
"pending-join circ %d already here, with intro ack. "
|
|
|
|
"Stalling. (stream %d sec old)",
|
2012-10-12 18:22:13 +02:00
|
|
|
rendcirc->base_.n_circ_id, conn_age);
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it's on its way. find an intro circ. */
|
2005-12-14 21:40:40 +01:00
|
|
|
retval = circuit_get_open_circ_or_launch(
|
|
|
|
conn, CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT, &introcirc);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (retval < 0) return -1; /* failed */
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (retval > 0) {
|
2004-05-13 09:24:49 +02:00
|
|
|
/* one has already sent the intro. keep waiting. */
|
2004-10-12 00:19:12 +02:00
|
|
|
tor_assert(introcirc);
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_REND, "Intro circ %d present and awaiting ack (rend %d). "
|
|
|
|
"Stalling. (stream %d sec old)",
|
2012-10-12 18:22:13 +02:00
|
|
|
introcirc->base_.n_circ_id,
|
|
|
|
rendcirc ? rendcirc->base_.n_circ_id : 0,
|
2006-02-13 09:28:42 +01:00
|
|
|
conn_age);
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-12 00:19:12 +02:00
|
|
|
/* now rendcirc and introcirc are each either undefined or not finished */
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
if (rendcirc && introcirc &&
|
2012-10-12 18:22:13 +02:00
|
|
|
rendcirc->base_.purpose == CIRCUIT_PURPOSE_C_REND_READY) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_REND,
|
|
|
|
"ready rend circ %d already here (no intro-ack yet on "
|
|
|
|
"intro %d). (stream %d sec old)",
|
2012-10-12 18:22:13 +02:00
|
|
|
rendcirc->base_.n_circ_id,
|
|
|
|
introcirc->base_.n_circ_id, conn_age);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
tor_assert(introcirc->base_.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
|
|
|
|
if (introcirc->base_.state == CIRCUIT_STATE_OPEN) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_REND,"found open intro circ %d (rend %d); sending "
|
|
|
|
"introduction. (stream %d sec old)",
|
2012-10-12 18:22:13 +02:00
|
|
|
introcirc->base_.n_circ_id, rendcirc->base_.n_circ_id,
|
2006-07-23 09:37:35 +02:00
|
|
|
conn_age);
|
2011-04-25 17:36:02 +02:00
|
|
|
switch (rend_client_send_introduction(introcirc, rendcirc)) {
|
|
|
|
case 0: /* success */
|
2012-10-12 18:22:13 +02:00
|
|
|
rendcirc->base_.timestamp_dirty = time(NULL);
|
|
|
|
introcirc->base_.timestamp_dirty = time(NULL);
|
2011-04-25 17:36:02 +02:00
|
|
|
assert_circuit_ok(TO_CIRCUIT(rendcirc));
|
|
|
|
assert_circuit_ok(TO_CIRCUIT(introcirc));
|
|
|
|
return 0;
|
|
|
|
case -1: /* transient error */
|
|
|
|
return 0;
|
|
|
|
case -2: /* permanent error */
|
2004-05-13 09:24:49 +02:00
|
|
|
return -1;
|
2011-04-25 17:36:02 +02:00
|
|
|
default: /* oops */
|
|
|
|
tor_fragile_assert();
|
2004-05-13 09:24:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_REND, "Intro (%d) and rend (%d) circs are not both ready. "
|
|
|
|
"Stalling conn. (%d sec old)",
|
2012-10-12 18:22:13 +02:00
|
|
|
introcirc ? introcirc->base_.n_circ_id : 0,
|
|
|
|
rendcirc ? rendcirc->base_.n_circ_id : 0, conn_age);
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2011-11-24 15:52:38 +01:00
|
|
|
/** Change <b>circ</b>'s purpose to <b>new_purpose</b>. */
|
|
|
|
void
|
|
|
|
circuit_change_purpose(circuit_t *circ, uint8_t new_purpose)
|
|
|
|
{
|
2011-11-24 15:54:55 +01:00
|
|
|
uint8_t old_purpose;
|
2011-11-24 15:52:38 +01:00
|
|
|
/* Don't allow an OR circ to become an origin circ or vice versa. */
|
|
|
|
tor_assert(!!(CIRCUIT_IS_ORIGIN(circ)) ==
|
|
|
|
!!(CIRCUIT_PURPOSE_IS_ORIGIN(new_purpose)));
|
|
|
|
|
|
|
|
if (circ->purpose == new_purpose) return;
|
|
|
|
|
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
2011-11-24 15:54:55 +01:00
|
|
|
char old_purpose_desc[80] = "";
|
2011-11-24 15:52:38 +01:00
|
|
|
|
2011-11-24 15:54:55 +01:00
|
|
|
strncpy(old_purpose_desc, circuit_purpose_to_string(circ->purpose), 80-1);
|
|
|
|
old_purpose_desc[80-1] = '\0';
|
2011-11-24 15:52:38 +01:00
|
|
|
|
|
|
|
log_debug(LD_CIRC,
|
|
|
|
"changing purpose of origin circ %d "
|
|
|
|
"from \"%s\" (%d) to \"%s\" (%d)",
|
|
|
|
TO_ORIGIN_CIRCUIT(circ)->global_identifier,
|
2011-11-24 15:54:55 +01:00
|
|
|
old_purpose_desc,
|
2011-11-24 15:52:38 +01:00
|
|
|
circ->purpose,
|
|
|
|
circuit_purpose_to_string(new_purpose),
|
|
|
|
new_purpose);
|
|
|
|
}
|
|
|
|
|
2011-11-24 15:54:55 +01:00
|
|
|
old_purpose = circ->purpose;
|
2011-11-24 15:52:38 +01:00
|
|
|
circ->purpose = new_purpose;
|
2011-11-24 15:54:55 +01:00
|
|
|
|
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
2012-01-11 16:28:20 +01:00
|
|
|
control_event_circuit_purpose_changed(TO_ORIGIN_CIRCUIT(circ),
|
|
|
|
old_purpose);
|
2011-11-24 15:54:55 +01:00
|
|
|
}
|
2011-11-24 15:52:38 +01:00
|
|
|
}
|
|
|
|
|