From 67c70b2566fc9bef4527fb8a0c24ce7d8c4d0647 Mon Sep 17 00:00:00 2001
From: George Kadianakis <desnacked@riseup.net>
Date: Mon, 10 Mar 2014 22:52:07 +0000
Subject: [PATCH] Throw control port warning if we failed to connect to all our
 bridges.

Conflicts:
	src/or/connection.c
	src/or/control.c
	src/or/control.h
	src/test/test_extorport.c
---
 changes/bug11069       |  4 ++++
 src/or/connection.c    | 25 +++++++++++++++++++++++++
 src/or/connection.h    |  2 ++
 src/or/connection_or.c |  8 +++++---
 src/or/control.c       | 13 ++++++++-----
 src/or/control.h       |  3 ++-
 src/or/entrynodes.c    | 21 ---------------------
 src/or/entrynodes.h    |  1 -
 8 files changed, 46 insertions(+), 31 deletions(-)
 create mode 100644 changes/bug11069

diff --git a/changes/bug11069 b/changes/bug11069
new file mode 100644
index 0000000..5aa3085
--- /dev/null
+++ b/changes/bug11069
@@ -0,0 +1,4 @@
+  o Minor bugfixes (clients):
+    - Fix tor so that it raises a control port warning when we fail to
+      connect to all of our bridges. Fixes bug 11069; bugfix on
+      tor-0.2.1.2-alpha.
diff --git a/src/or/connection.c b/src/or/connection.c
index 4f74a1d..ba28d81 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -3846,6 +3846,31 @@ connection_get_by_type_purpose(int type, int purpose)
   return NULL;
 }
 
+/** Return 1 if there are any active OR connections apart from
+ * <b>this_conn</b>.
+ *
+ * We use this to guess if we should tell the controller that we
+ * didn't manage to connect to any of our bridges. */
+int
+any_other_active_or_conns(const or_connection_t *this_conn)
+{
+  smartlist_t *conns = get_connection_array();
+  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
+    if (conn == TO_CONN(this_conn)) { /* don't consider this conn */
+      continue;
+    }
+
+    if (conn->type == CONN_TYPE_OR &&
+        !conn->marked_for_close) {
+      log_debug(LD_DIR, "%s: Found an OR connection: %s",
+                __func__, conn->address);
+      return 1;
+    }
+  } SMARTLIST_FOREACH_END(conn);
+
+  return 0;
+}
+
 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
 int
 connection_is_listener(connection_t *conn)
diff --git a/src/or/connection.h b/src/or/connection.h
index c78fe6e..9bd5f88 100644
--- a/src/or/connection.h
+++ b/src/or/connection.h
@@ -180,6 +180,8 @@ connection_t *connection_get_by_type_state_rendquery(int type, int state,
 dir_connection_t *connection_dir_get_by_purpose_and_resource(
                                            int state, const char *resource);
 
+int any_other_active_or_conns(const or_connection_t *this_conn);
+
 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
 int connection_is_listener(connection_t *conn);
 int connection_state_is_open(connection_t *conn);
diff --git a/src/or/connection_or.c b/src/or/connection_or.c
index 8e7cd9e..8684f18 100644
--- a/src/or/connection_or.c
+++ b/src/or/connection_or.c
@@ -645,7 +645,8 @@ connection_or_about_to_close(or_connection_t *or_conn)
                                      reason);
         if (!authdir_mode_tests_reachability(options))
           control_event_bootstrap_problem(
-                orconn_end_reason_to_control_string(reason), reason);
+                orconn_end_reason_to_control_string(reason),
+                reason, or_conn);
       }
     }
   } else if (conn->hold_open_until_flushed) {
@@ -1008,7 +1009,7 @@ connection_or_connect_failed(or_connection_t *conn,
 {
   control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED, reason);
   if (!authdir_mode_tests_reachability(get_options()))
-    control_event_bootstrap_problem(msg, reason);
+    control_event_bootstrap_problem(msg, reason, conn);
 }
 
 /** <b>conn</b> got an error in connection_handle_read_impl() or
@@ -1638,7 +1639,8 @@ connection_or_client_learned_peer_id(or_connection_t *conn,
     if (!authdir_mode_tests_reachability(options))
       control_event_bootstrap_problem(
                                 "Unexpected identity in router certificate",
-                                END_OR_CONN_REASON_OR_IDENTITY);
+                                END_OR_CONN_REASON_OR_IDENTITY,
+                                conn);
     return -1;
   }
   if (authdir_mode_tests_reachability(options)) {
diff --git a/src/or/control.c b/src/or/control.c
index a88de12..cd2c55c 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -4696,10 +4696,12 @@ control_event_bootstrap(bootstrap_status_t status, int progress)
 
 /** Called when Tor has failed to make bootstrapping progress in a way
  * that indicates a problem. <b>warn</b> gives a hint as to why, and
- * <b>reason</b> provides an "or_conn_end_reason" tag.
+ * <b>reason</b> provides an "or_conn_end_reason" tag.  <b>or_conn</b>
+ * is the connection that caused this problem.
  */
 void
-control_event_bootstrap_problem(const char *warn, int reason)
+control_event_bootstrap_problem(const char *warn, int reason,
+                                const or_connection_t *or_conn)
 {
   int status = bootstrap_percent;
   const char *tag, *summary;
@@ -4721,9 +4723,10 @@ control_event_bootstrap_problem(const char *warn, int reason)
   if (reason == END_OR_CONN_REASON_NO_ROUTE)
     recommendation = "warn";
 
-  if (get_options()->UseBridges &&
-      !any_bridge_descriptors_known() &&
-      !any_pending_bridge_descriptor_fetches())
+  /* If we are using bridges and all our OR connections are now
+     closed, it means that we totally failed to connect to our
+     bridges. Throw a warning. */
+  if (get_options()->UseBridges && !any_other_active_or_conns(or_conn))
     recommendation = "warn";
 
   if (we_are_hibernating())
diff --git a/src/or/control.h b/src/or/control.h
index 61062da..1d90a90 100644
--- a/src/or/control.h
+++ b/src/or/control.h
@@ -85,7 +85,8 @@ void enable_control_logging(void);
 void monitor_owning_controller_process(const char *process_spec);
 
 void control_event_bootstrap(bootstrap_status_t status, int progress);
-void control_event_bootstrap_problem(const char *warn, int reason);
+void control_event_bootstrap_problem(const char *warn, int reason,
+                                     const or_connection_t *or_conn);
 
 void control_event_clients_seen(const char *controller_str);
 
diff --git a/src/or/entrynodes.c b/src/or/entrynodes.c
index 2aa063c..b145c94 100644
--- a/src/or/entrynodes.c
+++ b/src/or/entrynodes.c
@@ -2134,27 +2134,6 @@ any_bridge_descriptors_known(void)
   return choose_random_entry(NULL) != NULL;
 }
 
-/** Return 1 if there are any directory conns fetching bridge descriptors
- * that aren't marked for close. We use this to guess if we should tell
- * the controller that we have a problem. */
-int
-any_pending_bridge_descriptor_fetches(void)
-{
-  smartlist_t *conns = get_connection_array();
-  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
-    if (conn->type == CONN_TYPE_DIR &&
-        conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
-        TO_DIR_CONN(conn)->router_purpose == ROUTER_PURPOSE_BRIDGE &&
-        !conn->marked_for_close &&
-        conn->linked &&
-        conn->linked_conn && !conn->linked_conn->marked_for_close) {
-      log_debug(LD_DIR, "found one: %s", conn->address);
-      return 1;
-    }
-  } SMARTLIST_FOREACH_END(conn);
-  return 0;
-}
-
 /** Return 1 if we have at least one descriptor for an entry guard
  * (bridge or member of EntryNodes) and all descriptors we know are
  * down. Else return 0. If <b>act</b> is 1, then mark the down guards
diff --git a/src/or/entrynodes.h b/src/or/entrynodes.h
index 52b8dc0..f6c07d9 100644
--- a/src/or/entrynodes.h
+++ b/src/or/entrynodes.h
@@ -104,7 +104,6 @@ void retry_bridge_descriptor_fetch_directly(const char *digest);
 void fetch_bridge_descriptors(const or_options_t *options, time_t now);
 void learned_bridge_descriptor(routerinfo_t *ri, int from_cache);
 int any_bridge_descriptors_known(void);
-int any_pending_bridge_descriptor_fetches(void);
 int entries_known_but_down(const or_options_t *options);
 void entries_retry_all(const or_options_t *options);
 
-- 
1.8.1.2

