[or-cvs] [tor/master] Merge remote branch 'origin/maint-0.2.2'

nickm at torproject.org nickm at torproject.org
Thu Jan 20 19:58:00 UTC 2011


commit c496229b0a8d90a327674a03aa5be455014b4d5c
Merge: f6a25a9 13e9a2b
Author: Nick Mathewson <nickm at torproject.org>
Date:   Thu Jan 20 15:00:42 2011 -0500

    Merge remote branch 'origin/maint-0.2.2'

 changes/policy_summarize-assert |    6 ++++++
 src/or/policies.c               |    4 ++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --combined src/or/policies.c
index e5af788,62e048c..d0406d3
--- a/src/or/policies.c
+++ b/src/or/policies.c
@@@ -11,7 -11,6 +11,7 @@@
  #include "or.h"
  #include "config.h"
  #include "dirserv.h"
 +#include "nodelist.h"
  #include "policies.h"
  #include "routerparse.h"
  #include "ht.h"
@@@ -262,7 -261,7 +262,7 @@@ fascist_firewall_allows_address_or(cons
  /** Return true iff we think our firewall will let us make an OR connection to
   * <b>ri</b>. */
  int
 -fascist_firewall_allows_or(routerinfo_t *ri)
 +fascist_firewall_allows_or(const routerinfo_t *ri)
  {
    /* XXXX proposal 118 */
    tor_addr_t addr;
@@@ -270,22 -269,6 +270,22 @@@
    return fascist_firewall_allows_address_or(&addr, ri->or_port);
  }
  
 +/** Return true iff we think our firewall will let us make an OR connection to
 + * <b>node</b>. */
 +int
 +fascist_firewall_allows_node(const node_t *node)
 +{
 +  if (node->ri) {
 +    return fascist_firewall_allows_or(node->ri);
 +  } else if (node->rs) {
 +    tor_addr_t addr;
 +    tor_addr_from_ipv4h(&addr, node->rs->addr);
 +    return fascist_firewall_allows_address_or(&addr, node->rs->or_port);
 +  } else {
 +    return 1;
 +  }
 +}
 +
  /** Return true iff we think our firewall will let us make a directory
   * connection to addr:port. */
  int
@@@ -875,11 -858,15 +875,11 @@@ policies_parse_exit_policy(config_line_
    return 0;
  }
  
 -/** Replace the exit policy of <b>r</b> with reject *:*. */
 +/** Replace the exit policy of <b>node</b> with reject *:* */
  void
 -policies_set_router_exitpolicy_to_reject_all(routerinfo_t *r)
 +policies_set_node_exitpolicy_to_reject_all(node_t *node)
  {
 -  addr_policy_t *item;
 -  addr_policy_list_free(r->exit_policy);
 -  r->exit_policy = smartlist_create();
 -  item = router_parse_addr_policy_item_from_string("reject *:*", -1);
 -  smartlist_add(r->exit_policy, item);
 +  node->rejects_all = 1;
  }
  
  /** Return 1 if there is at least one /8 subnet in <b>policy</b> that
@@@ -1088,7 -1075,7 +1088,7 @@@ policy_summary_split(smartlist_t *summa
    int start_at_index;
  
    int i = 0;
 -  /* XXXX Do a binary search if run time matters */
 +
    while (AT(i)->prt_max < prt_min)
      i++;
    if (AT(i)->prt_min != prt_min) {
@@@ -1256,8 -1243,8 +1256,8 @@@ policy_summarize(smartlist_t *policy
    accepts_str = smartlist_join_strings(accepts, ",", 0, &accepts_len);
    rejects_str = smartlist_join_strings(rejects, ",", 0, &rejects_len);
  
-   if (rejects_len > MAX_EXITPOLICY_SUMMARY_LEN &&
-       accepts_len > MAX_EXITPOLICY_SUMMARY_LEN) {
+   if (rejects_len > MAX_EXITPOLICY_SUMMARY_LEN-strlen("reject")-1 &&
+       accepts_len > MAX_EXITPOLICY_SUMMARY_LEN-strlen("accept")-1) {
      char *c;
      shorter_str = accepts_str;
      prefix = "accept";
@@@ -1301,195 -1288,6 +1301,195 @@@
    return result;
  }
  
 +/** Convert a summarized policy string into a short_policy_t.  Return NULL
 + * if the string is not well-formed. */
 +short_policy_t *
 +parse_short_policy(const char *summary)
 +{
 +  const char *orig_summary = summary;
 +  short_policy_t *result;
 +  int is_accept;
 +  int n_entries;
 +  short_policy_entry_t entries[MAX_EXITPOLICY_SUMMARY_LEN]; /* overkill */
 +  const char *next;
 +
 +  if (!strcmpstart(summary, "accept ")) {
 +    is_accept = 1;
 +    summary += strlen("accept ");
 +  } else if (!strcmpstart(summary, "reject ")) {
 +    is_accept = 0;
 +    summary += strlen("reject ");
 +  } else {
 +    log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Unrecognized policy summary keyword");
 +    return NULL;
 +  }
 +
 +  n_entries = 0;
 +  for ( ; *summary; summary = next) {
 +    const char *comma = strchr(summary, ',');
 +    unsigned low, high;
 +    char dummy;
 +    char ent_buf[32];
 +
 +    next = comma ? comma+1 : strchr(summary, '\0');
 +
 +    if (n_entries == MAX_EXITPOLICY_SUMMARY_LEN) {
 +      log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Impossibly long policy summary %s",
 +             escaped(orig_summary));
 +      return NULL;
 +    }
 +
 +    if (! TOR_ISDIGIT(*summary) || next-summary > (int)(sizeof(ent_buf)-1)) {
 +      /* unrecognized entry format. skip it. */
 +      continue;
 +    }
 +    if (next-summary < 2) {
 +      /* empty; skip it. */
 +      continue;
 +    }
 +
 +    memcpy(ent_buf, summary, next-summary-1);
 +    ent_buf[next-summary-1] = '\0';
 +
 +    if (tor_sscanf(ent_buf, "%u-%u%c", &low, &high, &dummy) == 2) {
 +      if (low<1 || low>65535 || high<1 || high>65535) {
 +        log_fn(LOG_PROTOCOL_WARN, LD_DIR,
 +               "Found bad entry in policy summary %s", escaped(orig_summary));
 +        return NULL;
 +      }
 +    } else if (tor_sscanf(ent_buf, "%u%c", &low, &dummy) == 1) {
 +      if (low<1 || low>65535) {
 +        log_fn(LOG_PROTOCOL_WARN, LD_DIR,
 +               "Found bad entry in policy summary %s", escaped(orig_summary));
 +        return NULL;
 +      }
 +      high = low;
 +    } else {
 +      log_fn(LOG_PROTOCOL_WARN, LD_DIR,"Found bad entry in policy summary %s",
 +             escaped(orig_summary));
 +      return NULL;
 +    }
 +
 +    entries[n_entries].min_port = low;
 +    entries[n_entries].max_port = high;
 +    n_entries++;
 +  }
 +
 +  if (n_entries == 0) {
 +    log_fn(LOG_PROTOCOL_WARN, LD_DIR,
 +           "Found no port-range entries in summary %s", escaped(orig_summary));
 +    return NULL;
 +  }
 +
 +  {
 +    size_t size = STRUCT_OFFSET(short_policy_t, entries) +
 +      sizeof(short_policy_entry_t)*(n_entries);
 +    result = tor_malloc_zero(size);
 +
 +    tor_assert( (char*)&result->entries[n_entries-1] < ((char*)result)+size);
 +  }
 +
 +  result->is_accept = is_accept;
 +  result->n_entries = n_entries;
 +  memcpy(result->entries, entries, sizeof(short_policy_entry_t)*n_entries);
 +  return result;
 +}
 +
 +/** Release all storage held in <b>policy</b>. */
 +void
 +short_policy_free(short_policy_t *policy)
 +{
 +  tor_free(policy);
 +}
 +
 +/** See whether the <b>addr</b>:<b>port</b> address is likely to be accepted
 + * or rejected by the summarized policy <b>policy</b>.  Return values are as
 + * for compare_tor_addr_to_addr_policy.  Unlike the regular addr_policy
 + * functions, requires the <b>port</b> be specified. */
 +addr_policy_result_t
 +compare_tor_addr_to_short_policy(const tor_addr_t *addr, uint16_t port,
 +                                 const short_policy_t *policy)
 +{
 +  int i;
 +  int found_match = 0;
 +  int accept;
 +  (void)addr;
 +
 +  tor_assert(port != 0);
 +
 +  if (addr && (tor_addr_is_internal(addr, 0) ||
 +               tor_addr_is_null(addr) ||
 +               tor_addr_is_loopback(addr)))
 +    return ADDR_POLICY_REJECTED;
 +
 +  for (i=0; i < policy->n_entries; ++i) {
 +    const short_policy_entry_t *e = &policy->entries[i];
 +    if (e->min_port <= port && port <= e->max_port) {
 +      found_match = 1;
 +      break;
 +    }
 +  }
 +
 +  if (found_match)
 +    accept = policy->is_accept;
 +  else
 +    accept = ! policy->is_accept;
 +
 +  /* ???? are these right? */
 +  if (accept)
 +    return ADDR_POLICY_PROBABLY_ACCEPTED;
 +  else
 +    return ADDR_POLICY_REJECTED;
 +}
 +
 +/** Return true iff <b>policy</b> seems reject all ports */
 +int
 +short_policy_is_reject_star(const short_policy_t *policy)
 +{
 +  /* This doesn't need to be as much on the lookout as policy_is_reject_star,
 +   * since policy summaries are from the consensus or from consensus
 +   * microdescs.
 +   */
 +  tor_assert(policy);
 +  /* Check for an exact match of "reject 1-65535". */
 +  return (policy->is_accept == 0 && policy->n_entries == 1 &&
 +          policy->entries[0].min_port == 1 &&
 +          policy->entries[0].max_port == 65535);
 +}
 +
 +/** Decides whether addr:port is probably or definitely accepted or rejcted by
 + * <b>node</b>.  See compare_tor_addr_to_addr_policy for details on addr/port
 + * interpretation. */
 +addr_policy_result_t
 +compare_addr_to_node_policy(uint32_t addr, uint16_t port, const node_t *node)
 +{
 +  tor_addr_t a;
 +  tor_addr_from_ipv4h(&a, addr);
 +  return compare_tor_addr_to_node_policy(&a, port, node);
 +}
 +
 +/** Decides whether addr:port is probably or definitely accepted or rejcted by
 + * <b>node</b>.  See compare_tor_addr_to_addr_policy for details on addr/port
 + * interpretation. */
 +addr_policy_result_t
 +compare_tor_addr_to_node_policy(const tor_addr_t *addr, uint16_t port,
 +                                const node_t *node)
 +{
 +  if (node->rejects_all)
 +    return ADDR_POLICY_REJECTED;
 +
 +  if (node->ri)
 +    return compare_tor_addr_to_addr_policy(addr, port, node->ri->exit_policy);
 +  else if (node->md && node->md) {
 +    if (node->md->exit_policy == NULL)
 +      return ADDR_POLICY_REJECTED;
 +    else
 +      return compare_tor_addr_to_short_policy(addr, port,
 +                                              node->md->exit_policy);
 +  } else
 +    return ADDR_POLICY_PROBABLY_REJECTED;
 +}
 +
  /** Implementation for GETINFO control command: knows the answer for questions
   * about "exit-policy/..." */
  int



More information about the tor-commits mailing list