Pārlūkot izejas kodu

Add function to check that addr_policy_t is okay; change struct addr_policy_t to addr_policy_t.

svn:r3070
Nick Mathewson 21 gadi atpakaļ
vecāks
revīzija
fc87758ff3
8 mainītis faili ar 57 papildinājumiem un 32 dzēšanām
  1. 5 5
      src/or/config.c
  2. 2 2
      src/or/connection_edge.c
  3. 2 2
      src/or/directory.c
  4. 8 7
      src/or/or.h
  5. 2 2
      src/or/router.c
  6. 5 5
      src/or/routerlist.c
  7. 32 8
      src/or/routerparse.c
  8. 1 1
      src/or/test.c

+ 5 - 5
src/or/config.c

@@ -1130,7 +1130,7 @@ options_validate(or_options_t *options)
   int i;
   int result = 0;
   struct config_line_t *cl;
-  struct addr_policy_t *addr_policy=NULL;
+  addr_policy_t *addr_policy=NULL;
 
   if (options->ORPort < 0 || options->ORPort > 65535) {
     log(LOG_WARN, "ORPort option out of bounds.");
@@ -1975,9 +1975,9 @@ normalize_log_options(or_options_t *options)
  */
 int
 config_parse_addr_policy(struct config_line_t *cfg,
-                         struct addr_policy_t **dest)
+                         addr_policy_t **dest)
 {
-  struct addr_policy_t **nextp;
+  addr_policy_t **nextp;
   smartlist_t *entries;
   int r = 0;
 
@@ -2012,8 +2012,8 @@ config_parse_addr_policy(struct config_line_t *cfg,
 
 /** Release all storage held by <b>p</b> */
 void
-addr_policy_free(struct addr_policy_t *p) {
-  struct addr_policy_t *e;
+addr_policy_free(addr_policy_t *p) {
+  addr_policy_t *e;
 
   while (p) {
     e = p;

+ 2 - 2
src/or/connection_edge.c

@@ -13,7 +13,7 @@ const char connection_edge_c_id[] = "$Id$";
 #include "or.h"
 #include "tree.h"
 
-static struct addr_policy_t *socks_policy = NULL;
+static addr_policy_t *socks_policy = NULL;
 /* List of exit_redirect_t */
 static smartlist_t *redirect_exit_list = NULL;
 
@@ -1045,7 +1045,7 @@ int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit)
 void
 parse_socks_policy(void)
 {
-  struct addr_policy_t *n;
+  addr_policy_t *n;
   if (socks_policy) {
     addr_policy_free(socks_policy);
     socks_policy = NULL;

+ 2 - 2
src/or/directory.c

@@ -51,7 +51,7 @@ static int directory_handle_command(connection_t *conn);
 
 /********* START VARIABLES **********/
 
-static struct addr_policy_t *dir_policy = NULL;
+static addr_policy_t *dir_policy = NULL;
 
 #if 0 /* commented out for now, since for now what clients send is
          different from what servers want to receive */
@@ -74,7 +74,7 @@ char rend_fetch_url[] = "/tor/rendezvous/";
 void
 parse_dir_policy(void)
 {
-  struct addr_policy_t *n;
+  addr_policy_t *n;
   if (dir_policy) {
     addr_policy_free(dir_policy);
     dir_policy = NULL;

+ 8 - 7
src/or/or.h

@@ -582,7 +582,7 @@ typedef struct connection_t connection_t;
 #define ADDR_POLICY_REJECT 2
 
 /** A linked list of policy rules */
-struct addr_policy_t {
+typedef struct addr_policy_t {
   char policy_type; /**< One of ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT. */
   char *string; /**< String representation of this rule. */
   uint32_t addr; /**< Base address to accept or reject. */
@@ -592,7 +592,7 @@ struct addr_policy_t {
   uint16_t prt_max; /**< Highest port number to accept/reject. */
 
   struct addr_policy_t *next; /**< Next rule in list. */
-};
+} addr_policy_t;
 
 /** Information about another onion router in the network. */
 typedef struct {
@@ -619,7 +619,7 @@ typedef struct {
   uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
   /** How many bytes/s is this router known to handle? */
   uint32_t bandwidthcapacity;
-  struct addr_policy_t *exit_policy; /**< What streams will this OR permit
+  addr_policy_t *exit_policy; /**< What streams will this OR permit
                                       * to exit? */
   long uptime; /**< How many seconds the router claims to have been up */
   /* local info */
@@ -1124,8 +1124,8 @@ void options_init(or_options_t *options);
 int init_from_config(int argc, char **argv);
 int config_init_logs(or_options_t *options, int validate_only);
 int config_parse_addr_policy(struct config_line_t *cfg,
-                             struct addr_policy_t **dest);
-void addr_policy_free(struct addr_policy_t *p);
+                             addr_policy_t **dest);
+void addr_policy_free(addr_policy_t *p);
 int config_option_is_recognized(const char *key);
 struct config_line_t *config_get_assigned_option(or_options_t *options,
                                                  const char *key);
@@ -1595,7 +1595,7 @@ void routerlist_remove_old_routers(int age);
 int router_load_routerlist_from_directory(const char *s,crypto_pk_env_t *pkey,
                                           int check_version);
 int router_compare_addr_to_addr_policy(uint32_t addr, uint16_t port,
-                                       struct addr_policy_t *policy);
+                                       addr_policy_t *policy);
 #define ADDR_POLICY_ACCEPTED 0
 #define ADDR_POLICY_REJECTED -1
 #define ADDR_POLICY_UNKNOWN 1
@@ -1682,12 +1682,13 @@ int router_parse_routerlist_from_directory(const char *s,
 running_routers_t *router_parse_runningrouters(const char *str);
 routerinfo_t *router_parse_entry_from_string(const char *s, const char *end);
 int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
-struct addr_policy_t *router_parse_addr_policy_from_string(const char *s);
+addr_policy_t *router_parse_addr_policy_from_string(const char *s);
 int check_software_version_against_directory(const char *directory,
                                              int ignoreversion);
 int tor_version_parse(const char *s, tor_version_t *out);
 int tor_version_as_new_as(const char *platform, const char *cutoff);
 int tor_version_compare(tor_version_t *a, tor_version_t *b);
+void assert_addr_policy_ok(addr_policy_t *t);
 
 #endif
 

+ 2 - 2
src/or/router.c

@@ -454,7 +454,7 @@ void router_upload_dir_desc_to_dirservers(int force) {
  * rule, then append the default exit policy as well.
  */
 static void router_add_exit_policy_from_config(routerinfo_t *router) {
-  struct addr_policy_t *ep;
+  addr_policy_t *ep;
   struct config_line_t default_policy;
   config_parse_addr_policy(get_options()->ExitPolicy, &router->exit_policy);
 
@@ -627,7 +627,7 @@ int router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
   size_t onion_pkeylen, identity_pkeylen;
   size_t written;
   int result=0;
-  struct addr_policy_t *tmpe;
+  addr_policy_t *tmpe;
   char *bandwidth_usage;
   char *family_line;
 #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING

+ 5 - 5
src/or/routerlist.c

@@ -684,7 +684,7 @@ void routerinfo_free(routerinfo_t *router)
 routerinfo_t *routerinfo_copy(const routerinfo_t *router)
 {
   routerinfo_t *r;
-  struct addr_policy_t **e, *tmp;
+  addr_policy_t **e, *tmp;
 
   r = tor_malloc(sizeof(routerinfo_t));
   memcpy(r, router, sizeof(routerinfo_t));
@@ -698,8 +698,8 @@ routerinfo_t *routerinfo_copy(const routerinfo_t *router)
     r->identity_pkey = crypto_pk_dup_key(r->identity_pkey);
   e = &r->exit_policy;
   while (*e) {
-    tmp = tor_malloc(sizeof(struct addr_policy_t));
-    memcpy(tmp,*e,sizeof(struct addr_policy_t));
+    tmp = tor_malloc(sizeof(addr_policy_t));
+    memcpy(tmp,*e,sizeof(addr_policy_t));
     *e = tmp;
     (*e)->string = tor_strdup((*e)->string);
     e = & ((*e)->next);
@@ -942,14 +942,14 @@ router_resolve_routerlist(routerlist_t *rl)
  * unknown).
  */
 int router_compare_addr_to_addr_policy(uint32_t addr, uint16_t port,
-                                       struct addr_policy_t *policy)
+                                       addr_policy_t *policy)
 {
   int maybe_reject = 0;
   int maybe_accept = 0;
   int match = 0;
   int maybe = 0;
   struct in_addr in;
-  struct addr_policy_t *tmpe;
+  addr_policy_t *tmpe;
 
   for (tmpe=policy; tmpe; tmpe=tmpe->next) {
 //    log_fn(LOG_DEBUG,"Considering exit policy %s", tmpe->string);

+ 32 - 8
src/or/routerparse.c

@@ -125,7 +125,7 @@ static struct {
 
 /* static function prototypes */
 static int router_add_exit_policy(routerinfo_t *router,directory_token_t *tok);
-static struct addr_policy_t *router_parse_addr_policy(directory_token_t *tok);
+static addr_policy_t *router_parse_addr_policy(directory_token_t *tok);
 static int router_get_hash_impl(const char *s, char *digest,
                                 const char *start_str, const char *end_str);
 static void token_free(directory_token_t *tok);
@@ -968,13 +968,13 @@ routerinfo_t *router_parse_entry_from_string(const char *s,
 
 /** Parse the exit policy in the string <b>s</b> and return it.
  */
-struct addr_policy_t *
+addr_policy_t *
 router_parse_addr_policy_from_string(const char *s)
 {
   directory_token_t *tok = NULL;
   const char *cp;
   char *tmp;
-  struct addr_policy_t *r;
+  addr_policy_t *r;
   size_t len, idx;
 
   /* *s might not end with \n, so we need to extend it with one. */
@@ -1009,7 +1009,7 @@ router_parse_addr_policy_from_string(const char *s)
 int
 router_add_exit_policy_from_string(routerinfo_t *router, const char *s)
 {
-  struct addr_policy_t *newe, *tmpe;
+  addr_policy_t *newe, *tmpe;
   newe = router_parse_addr_policy_from_string(s);
   if (!newe)
     return -1;
@@ -1023,7 +1023,7 @@ router_add_exit_policy_from_string(routerinfo_t *router, const char *s)
 static int
 router_add_exit_policy(routerinfo_t *router,directory_token_t *tok)
 {
-  struct addr_policy_t *newe, **tmpe;
+  addr_policy_t *newe, **tmpe;
   newe = router_parse_addr_policy(tok);
   if (!newe)
     return -1;
@@ -1036,10 +1036,10 @@ router_add_exit_policy(routerinfo_t *router,directory_token_t *tok)
 
 /** Given a K_ACCEPT or K_REJECT token and a router, create and return
  * a new exit_policy_t corresponding to the token. */
-static struct addr_policy_t *
+static addr_policy_t *
 router_parse_addr_policy(directory_token_t *tok) {
 
-  struct addr_policy_t *newe;
+  addr_policy_t *newe;
   struct in_addr in;
   char *arg, *address;
 
@@ -1049,7 +1049,7 @@ router_parse_addr_policy(directory_token_t *tok) {
     return NULL;
   arg = tok->args[0];
 
-  newe = tor_malloc_zero(sizeof(struct addr_policy_t));
+  newe = tor_malloc_zero(sizeof(addr_policy_t));
 
   newe->string = tor_malloc(8+strlen(arg));
 //  tor_snprintf(newe->string, 8+strlen(arg), "%s %s",
@@ -1079,6 +1079,30 @@ policy_read_failed:
   return NULL;
 }
 
+void
+assert_addr_policy_ok(addr_policy_t *t)
+{
+  addr_policy_t *t2;
+  while (t) {
+    tor_assert(t->policy_type == ADDR_POLICY_REJECT ||
+               t->policy_type == ADDR_POLICY_ACCEPT);
+    tor_assert(t->prt_min <= t->prt_max);
+    t2 = router_parse_addr_policy_from_string(t->string);
+    tor_assert(t2);
+    tor_assert(t2->policy_type == t->policy_type);
+    tor_assert(t2->addr == t->addr);
+    tor_assert(t2->msk == t->msk);
+    tor_assert(t2->prt_min == t->prt_min);
+    tor_assert(t2->prt_max == t->prt_max);
+    tor_assert(!strcmp(t2->string, t->string));
+    tor_assert(t2->next == NULL);
+    addr_policy_free(t2);
+
+    t = t->next;
+  }
+
+}
+
 /*
  * Low-level tokenizer for router descriptors and directories.
  */

+ 1 - 1
src/or/test.c

@@ -940,7 +940,7 @@ test_dir_format(void)
   routerinfo_t r1, r2;
   crypto_pk_env_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL;
   routerinfo_t *rp1 = NULL, *rp2 = NULL;
-  struct addr_policy_t ex1, ex2;
+  addr_policy_t ex1, ex2;
   routerlist_t *dir1 = NULL, *dir2 = NULL;
   tor_version_t ver1;
   char *bw_lines = NULL;