[or-cvs] r11267: In new code, let's try to prefer named flags to mazes of twi (in tor/trunk: . src/or)

nickm at seul.org nickm at seul.org
Fri Aug 24 12:33:53 UTC 2007


Author: nickm
Date: 2007-08-24 08:33:53 -0400 (Fri, 24 Aug 2007)
New Revision: 11267

Modified:
   tor/trunk/
   tor/trunk/src/or/circuitbuild.c
   tor/trunk/src/or/or.h
   tor/trunk/src/or/routerlist.c
Log:
 r14200 at kushana:  nickm | 2007-08-24 08:33:41 -0400
 In new code, let's try to prefer named flags to mazes of twisted boolean arguments, all alike.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r14200] on c95137ef-5f19-0410-b913-86e773d04f59

Modified: tor/trunk/src/or/circuitbuild.c
===================================================================
--- tor/trunk/src/or/circuitbuild.c	2007-08-24 10:50:48 UTC (rev 11266)
+++ tor/trunk/src/or/circuitbuild.c	2007-08-24 12:33:53 UTC (rev 11267)
@@ -1264,7 +1264,7 @@
     smartlist_subtract(sl,excludedexits);
     if (options->StrictExitNodes || smartlist_overlap(sl,preferredexits))
       smartlist_intersect(sl,preferredexits);
-    router = routerlist_sl_choose_by_bandwidth(sl, 1, 0);
+    router = routerlist_sl_choose_by_bandwidth(sl, WEIGHT_FOR_EXIT);
   } else {
     /* Either there are no pending connections, or no routers even seem to
      * possibly support any of them.  Choose a router at random that satisfies
@@ -1308,7 +1308,7 @@
         smartlist_intersect(sl,preferredexits);
         /* XXX sometimes the above results in null, when the requested
          * exit node is down. we should pick it anyway. */
-      router = routerlist_sl_choose_by_bandwidth(sl, 1, 0);
+      router = routerlist_sl_choose_by_bandwidth(sl, WEIGHT_FOR_EXIT);
       if (router)
         break;
     }

Modified: tor/trunk/src/or/or.h
===================================================================
--- tor/trunk/src/or/or.h	2007-08-24 10:50:48 UTC (rev 11266)
+++ tor/trunk/src/or/or.h	2007-08-24 12:33:53 UTC (rev 11267)
@@ -3409,8 +3409,11 @@
 int router_is_unreliable(routerinfo_t *router, int need_uptime,
                          int need_capacity, int need_guard);
 uint32_t router_get_advertised_bandwidth(routerinfo_t *router);
-routerinfo_t *routerlist_sl_choose_by_bandwidth(smartlist_t *sl, int for_exit,
-                                                int for_guard);
+typedef enum {
+  NO_WEIGHTING, WEIGHT_FOR_EXIT, WEIGHT_FOR_GUARD
+} bandwidth_weight_rule_t;
+routerinfo_t *routerlist_sl_choose_by_bandwidth(smartlist_t *sl,
+                                                bandwidth_weight_rule_t rule);
 routerstatus_t *routerstatus_sl_choose_by_bandwidth(smartlist_t *sl);
 
 routerinfo_t *router_choose_random_node(const char *preferred,

Modified: tor/trunk/src/or/routerlist.c
===================================================================
--- tor/trunk/src/or/routerlist.c	2007-08-24 10:50:48 UTC (rev 11266)
+++ tor/trunk/src/or/routerlist.c	2007-08-24 12:33:53 UTC (rev 11267)
@@ -1293,7 +1293,7 @@
  *
  */
 static void *
-smartlist_choose_by_bandwidth(smartlist_t *sl, int for_exit, int for_guard,
+smartlist_choose_by_bandwidth(smartlist_t *sl, bandwidth_weight_rule_t rule,
                               int statuses)
 {
   unsigned int i;
@@ -1313,7 +1313,9 @@
   uint32_t max_believable_bw = get_max_believable_bandwidth();
 
   /* Can't choose exit and guard at same time */
-  tor_assert(!(for_exit && for_guard));
+  tor_assert(rule == NO_WEIGHTING ||
+             rule == WEIGHT_FOR_EXIT ||
+             rule == WEIGHT_FOR_GUARD);
 
   /* First count the total bandwidth weight, and make a list
    * of each value.  <0 means "unknown; no routerinfo."  We use the
@@ -1430,12 +1432,12 @@
      * For detailed derivation of this formula, see
      *   http://archives.seul.org/or/dev/Jul-2007/msg00056.html
      */
-    if (for_exit)
+    if (rule == WEIGHT_FOR_EXIT)
       exit_weight = 1.0;
     else
       exit_weight = 1.0 - all_bw/(3.0*exit_bw);
 
-    if (for_guard)
+    if (rule == WEIGHT_FOR_GUARD)
       guard_weight = 1.0;
     else
       guard_weight = 1.0 - all_bw/(3.0*guard_bw);
@@ -1469,9 +1471,9 @@
             "(for guard == %d)",
             U64_PRINTF_ARG(total_bw),
             U64_PRINTF_ARG(total_exit_bw), U64_PRINTF_ARG(total_nonexit_bw),
-            exit_weight, for_exit,
+            exit_weight, (int)(rule == WEIGHT_FOR_EXIT),
             U64_PRINTF_ARG(total_guard_bw), U64_PRINTF_ARG(total_nonguard_bw),
-            guard_weight, for_guard);
+            guard_weight, (int)(rule == WEIGHT_FOR_GUARD));
 
   /* Almost done: choose a random value from the bandwidth weights. */
   rand_bw = crypto_rand_uint64(total_bw);
@@ -1515,9 +1517,10 @@
  * the advertised bandwidth of each router.
  */
 routerinfo_t *
-routerlist_sl_choose_by_bandwidth(smartlist_t *sl, int for_exit, int for_guard)
+routerlist_sl_choose_by_bandwidth(smartlist_t *sl,
+                                  bandwidth_weight_rule_t rule)
 {
-  return smartlist_choose_by_bandwidth(sl, for_exit, for_guard, 0);
+  return smartlist_choose_by_bandwidth(sl, rule, 0);
 }
 
 /** Choose a random element of status list <b>sl</b>, weighted by
@@ -1527,7 +1530,7 @@
 routerstatus_sl_choose_by_bandwidth(smartlist_t *sl)
 {
   /* We are choosing neither exit nor guard here. Weight accordingly. */
-  return smartlist_choose_by_bandwidth(sl, 0, 0, 1);
+  return smartlist_choose_by_bandwidth(sl, NO_WEIGHTING, 1);
 }
 
 /** Return a random running router from the routerlist.  If any node
@@ -1557,7 +1560,12 @@
 {
   smartlist_t *sl, *excludednodes;
   routerinfo_t *choice = NULL;
+  bandwidth_weight_rule_t rule;
 
+  tor_assert(!(weight_for_exit && need_guard));
+  rule = weight_for_exit ? WEIGHT_FOR_EXIT :
+    (need_guard ? WEIGHT_FOR_GUARD : NO_WEIGHTING);
+
   excludednodes = smartlist_create();
   add_nickname_list_to_smartlist(excludednodes,excluded,0);
 
@@ -1584,8 +1592,7 @@
       smartlist_subtract(sl,excludedsmartlist);
 
     if (need_capacity || need_guard)
-      choice = routerlist_sl_choose_by_bandwidth(sl, weight_for_exit,
-                                                 need_guard);
+      choice = routerlist_sl_choose_by_bandwidth(sl, rule);
     else
       choice = smartlist_choose(sl);
 



More information about the tor-commits mailing list