From b36f93a6711aa3c763d1ea0765862cd2f6939756 Mon Sep 17 00:00:00 2001
From: Matthew Finkel <Matthew.Finkel@gmail.com>
Date: Wed, 2 Oct 2013 02:46:24 +0000
Subject: A Bridge Authority should compute flag thresholds

As a bridge authority, before we create our networkstatus document, we
should compute the thresholds needed for the various status flags
assigned to each bridge based on the status of all other bridges. We
then add these thresholds to the networkstatus document for easy access.
Fixes for #1117 and #9859.
---
 changes/bug9859        |  6 ++++++
 src/or/dirserv.c       | 19 +++++++++++++++++++
 src/or/dirserv.h       |  1 +
 src/or/networkstatus.c | 10 +++++++++-
 4 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 changes/bug9859

diff --git a/changes/bug9859 b/changes/bug9859
new file mode 100644
index 000000000..54ca30e6f
--- /dev/null
+++ b/changes/bug9859
@@ -0,0 +1,6 @@
+  o Minor Feature
+
+    - Assign status flags to bridges based on thresholds calculated
+      over all bridges. Fixes bug 9859.
+    - Add threshold cutoffs to the networkstatus document created by
+      the Bridge Authority. Fixes bug 1117.
diff --git a/src/or/dirserv.c b/src/or/dirserv.c
index d30a47436..8d2da5b50 100644
--- a/src/or/dirserv.c
+++ b/src/or/dirserv.c
@@ -1957,6 +1957,10 @@ dirserv_compute_performance_thresholds(routerlist_t *rl,
 
   /* Now, fill in the arrays. */
   SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), node_t *, node) {
+    if (options->BridgeAuthoritativeDir &&
+        node->ri &&
+        node->ri->purpose != ROUTER_PURPOSE_BRIDGE)
+      continue;
     if (router_counts_toward_thresholds(node, now, omit_as_sybil,
                                         require_mbw)) {
       routerinfo_t *ri = node->ri;
@@ -2071,6 +2075,21 @@ dirserv_compute_performance_thresholds(routerlist_t *rl,
   tor_free(wfus);
 }
 
+/* Use dirserv_compute_performance_thresholds() to compute the thresholds
+ * for the status flags, specifically for bridges.
+ *
+ * This is only called by a Bridge Authority from
+ * networkstatus_getinfo_by_purpose().
+ */
+void
+dirserv_compute_bridge_flag_thresholds(routerlist_t *rl)
+{
+
+  digestmap_t *omit_as_sybil = digestmap_new();
+  dirserv_compute_performance_thresholds(rl, omit_as_sybil);
+  digestmap_free(omit_as_sybil, NULL);
+}
+
 /** Measured bandwidth cache entry */
 typedef struct mbw_cache_entry_s {
   long mbw_kb;
diff --git a/src/or/dirserv.h b/src/or/dirserv.h
index 7221fc995..c2d6131c7 100644
--- a/src/or/dirserv.h
+++ b/src/or/dirserv.h
@@ -51,6 +51,7 @@ int list_server_status_v1(smartlist_t *routers, char **router_status_out,
 int dirserv_dump_directory_to_string(char **dir_out,
                                      crypto_pk_t *private_key);
 char *dirserv_get_flag_thresholds_line(void);
+void dirserv_compute_bridge_flag_thresholds(routerlist_t *rl);
 
 int directory_fetches_from_authorities(const or_options_t *options);
 int directory_fetches_dir_info_early(const or_options_t *options);
diff --git a/src/or/networkstatus.c b/src/or/networkstatus.c
index c950731bb..a935a4672 100644
--- a/src/or/networkstatus.c
+++ b/src/or/networkstatus.c
@@ -2171,9 +2171,17 @@ networkstatus_dump_bridge_status_to_file(time_t now)
   char *status = networkstatus_getinfo_by_purpose("bridge", now);
   const or_options_t *options = get_options();
   char *fname = NULL;
+  char *thresholds = NULL, *thresholds_and_status = NULL;
+  routerlist_t *rl = router_get_routerlist();
+  dirserv_compute_bridge_flag_thresholds(rl);
+  thresholds = dirserv_get_flag_thresholds_line();
+  tor_asprintf(&thresholds_and_status, "flag-thresholds %s\n%s",
+               thresholds, status);
   tor_asprintf(&fname, "%s"PATH_SEPARATOR"networkstatus-bridges",
                options->DataDirectory);
-  write_str_to_file(fname,status,0);
+  write_str_to_file(fname,thresholds_and_status,0);
+  tor_free(thresholds);
+  tor_free(thresholds_and_status);
   tor_free(fname);
   tor_free(status);
 }
-- 
cgit v1.2.3