Commit e6c51a05 authored by rl1987's avatar rl1987
Browse files

Make entry_guards_update_primary() shorter

parent 86549c0d
Loading
Loading
Loading
Loading
+15 −29
Original line number Diff line number Diff line
@@ -1883,28 +1883,24 @@ entry_guards_update_primary(guard_selection_t *gs)
    smartlist_add(new_primary_guards, guard);
  } SMARTLIST_FOREACH_END(guard);

  SMARTLIST_FOREACH_BEGIN(old_primary_guards, entry_guard_t *, guard) {
    /* Can we keep any older primary guards? First remove all the ones
     * that we already kept. */
  SMARTLIST_FOREACH_BEGIN(old_primary_guards, entry_guard_t *, guard) {
    if (smartlist_contains(new_primary_guards, guard)) {
      SMARTLIST_DEL_CURRENT_KEEPORDER(old_primary_guards, guard);
      continue;
    }
  } SMARTLIST_FOREACH_END(guard);

    /* Now add any that are still good. */
  SMARTLIST_FOREACH_BEGIN(old_primary_guards, entry_guard_t *, guard) {
    if (smartlist_len(new_primary_guards) >= N_PRIMARY_GUARDS)
      break;
    if (! guard->is_filtered_guard)
      continue;
    if (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS &&
        guard->is_filtered_guard) {
      guard->is_primary = 1;
      smartlist_add(new_primary_guards, guard);
      SMARTLIST_DEL_CURRENT_KEEPORDER(old_primary_guards, guard);
  } SMARTLIST_FOREACH_END(guard);

    } else {
      /* Mark the remaining previous primary guards as non-primary */
  SMARTLIST_FOREACH_BEGIN(old_primary_guards, entry_guard_t *, guard) {
      guard->is_primary = 0;
    }
  } SMARTLIST_FOREACH_END(guard);

  /* Finally, fill out the list with sampled guards. */
@@ -1928,18 +1924,8 @@ entry_guards_update_primary(guard_selection_t *gs)
  });
#endif /* 1 */

  int any_change = 0;
  if (smartlist_len(gs->primary_entry_guards) !=
      smartlist_len(new_primary_guards)) {
    any_change = 1;
  } else {
    SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards, entry_guard_t *, g) {
      if (g != smartlist_get(new_primary_guards, g_sl_idx)) {
        any_change = 1;
      }
    } SMARTLIST_FOREACH_END(g);
  }

  const int any_change = !smartlist_ptrs_eq(gs->primary_entry_guards,
                                            new_primary_guards);
  if (any_change) {
    log_info(LD_GUARD, "Primary entry guards have changed. "
             "New primary guard list is: ");
+27 −0
Original line number Diff line number Diff line
@@ -189,6 +189,33 @@ smartlist_ints_eq(const smartlist_t *sl1, const smartlist_t *sl2)
  return 1;
}

/**
 * Return true if there is shallow equality between smartlists -
 * i.e. all indices correspond to exactly same object (pointer
 * values are matching). Otherwise, return false.
 */
int
smartlist_ptrs_eq(const smartlist_t *s1, const smartlist_t *s2)
{
  if (s1 == s2)
    return 1;

  // Note: pointers cannot both be NULL at this point, because
  // above check.
  if (s1 == NULL || s2 == NULL)
    return 0;

  if (smartlist_len(s1) != smartlist_len(s2))
    return 0;

  for (int i = 0; i < smartlist_len(s1); i++) {
    if (smartlist_get(s1, i) != smartlist_get(s2, i))
      return 0;
  }

  return 1;
}

/** Return true iff <b>sl</b> has some element E such that
 * tor_memeq(E,<b>element</b>,DIGEST_LEN)
 */
+3 −0
Original line number Diff line number Diff line
@@ -37,6 +37,9 @@ int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);

int smartlist_ptrs_eq(const smartlist_t *s1,
                      const smartlist_t *s2);

void smartlist_sort(smartlist_t *sl,
                    int (*compare)(const void **a, const void **b));
void *smartlist_get_most_frequent_(const smartlist_t *sl,