[or-cvs] parse exit policy lines

Roger Dingledine arma at seul.org
Mon Apr 7 04:38:21 UTC 2003


Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/home/arma/work/onion/cvs/src/or

Modified Files:
	or.h routers.c 
Log Message:
parse exit policy lines


Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.57
retrieving revision 1.58
diff -u -d -r1.57 -r1.58
--- or.h	7 Apr 2003 02:12:02 -0000	1.57
+++ or.h	7 Apr 2003 04:38:19 -0000	1.58
@@ -290,6 +290,18 @@
 
 typedef struct connection_t connection_t;
 
+#define EXIT_POLICY_ACCEPT 1
+#define EXIT_POLICY_REJECT 2
+
+struct exit_policy_t {
+  char policy_type;
+  char *string;
+  char *address;
+  char *port;
+
+  struct exit_policy_t *next;
+};
+
 /* config stuff we know about the other ORs in the network */
 typedef struct {
   char *address;
@@ -304,14 +316,7 @@
  
   /* link info */
   uint32_t bandwidth;
-
-//  struct timeval  min_interval;
- 
-  /* time when last data was sent to that router */
-//  struct timeval lastsend;
- 
-  /* socket */
-//  int s;
+  struct exit_policy_t *exit_policy;
 
   void *next;
 } routerinfo_t;

Index: routers.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routers.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- routers.c	7 Apr 2003 02:12:02 -0000	1.19
+++ routers.c	7 Apr 2003 04:38:19 -0000	1.20
@@ -2,13 +2,6 @@
 /* See LICENSE for licensing information */
 /* $Id$ */
 
-/**
- * routers.c 
- * Routines for loading the list of routers and their public RSA keys.
- *
- * Matej Pfajfar <mp292 at cam.ac.uk>
- */
-
 #define OR_PUBLICKEY_END_TAG "-----END RSA PUBLIC KEY-----\n"
 
 #include "or.h"
@@ -30,6 +23,8 @@
 static char *eat_whitespace(char *s);
 static char *find_whitespace(char *s);
 static routerinfo_t *router_get_entry_from_string(char **s);
+static void router_add_exit_policy(routerinfo_t *router, char *string);
+static void router_free_exit_policy(routerinfo_t *router);
 
 /****************************************************************************/
 
@@ -49,7 +44,7 @@
     log(LOG_ERR,"Error obtaining local host info.");
     return -1;
   }
-  memset((void *)me,0,sizeof(struct sockaddr_in));
+  memset(me,0,sizeof(struct sockaddr_in));
   me->sin_family = AF_INET;
   memcpy((void *)&me->sin_addr,(void *)localhost->h_addr,sizeof(struct in_addr));
   me->sin_port = htons(options.ORPort);
@@ -418,6 +413,7 @@
   }
   memset(router,0,sizeof(routerinfo_t)); /* zero it out first */
 
+/* Bug: if find_whitespace returns a '#', we'll squish it. */
 #define NEXT_TOKEN(s, next)    \
   *s = eat_whitespace(*s);     \
   next = find_whitespace(*s);  \
@@ -504,19 +500,42 @@
 //  test_write_pkey(router->pkey);  
 
   *s = next+1;
+  while(**s != '\n') {
+    /* pull in a line of exit policy */
+    next = strchr(*s, '\n');
+    if(!next)
+      goto router_read_failed;
+    *next = 0;
+    router_add_exit_policy(router, *s);
+    *s = next+1;
+  }
+
+  return router;
 
-  /* success */
-  return(router);
 
 router_read_failed:
   if(router->address)
     free(router->address);
   if(router->pkey)
     crypto_free_pk_env(router->pkey);
+  router_free_exit_policy(router);
   free(router);
   return NULL;
 }
 
+static void router_free_exit_policy(routerinfo_t *router) {
+  struct exit_policy_t *tmpe;
+
+  while(router->exit_policy) {
+    tmpe = router->exit_policy;
+    router->exit_policy = tmpe->next;
+    free(tmpe->string);
+    free(tmpe->address);
+    free(tmpe->port);
+    free(tmpe);
+  }
+}
+
 #if 0
 void test_write_pkey(crypto_pk_env_t *pkey) {
   char *string;
@@ -532,6 +551,70 @@
 }
 #endif
 
+static void router_add_exit_policy(routerinfo_t *router, char *string) {
+  struct exit_policy_t *tmpe, *newe;
+  char *n;
+
+  string = eat_whitespace(string);
+  if(!*string) /* it was all whitespace or comment */
+    return;
+
+  newe = malloc(sizeof(struct exit_policy_t));
+  memset(newe,0,sizeof(struct exit_policy_t));
+
+  newe->string = strdup(string);
+  if(!strncasecmp(string,"reject ",strlen("reject "))) {
+    newe->policy_type = EXIT_POLICY_REJECT;
+  } else if(!strncasecmp(string,"accept ",strlen("accept "))) {
+    newe->policy_type = EXIT_POLICY_ACCEPT;
+  } else {
+    goto policy_read_failed;
+  }
+
+  string = eat_whitespace(string + strlen("reject "));
+  if(!*string) {
+    goto policy_read_failed;
+  }
+
+  n = strchr(string,':');
+  if(!n)
+    goto policy_read_failed;
+  *n = 0;
+  newe->address = strdup(string);
+  string = n+1;
+  n = find_whitespace(string);
+  *n = 0;
+  newe->port = strdup(string);
+
+  log(LOG_DEBUG,"router_add_exit_policy(): type %d, address '%s', port '%s'.",
+      newe->policy_type, newe->address, newe->port);
+
+  /* now link newe onto the end of exit_policy */
+
+  if(!router->exit_policy) {
+    router->exit_policy = newe;
+    return;
+  }
+
+  for(tmpe=router->exit_policy; tmpe->next; tmpe=tmpe->next) ;
+  tmpe->next = newe;
+
+  return;
+
+policy_read_failed:
+  assert(newe->string);
+  log(LOG_INFO,"router_add_exit_policy(): Couldn't parse line '%s'. Dropping", newe->string);
+  if(newe->string)
+    free(newe->string);
+  if(newe->address)
+    free(newe->address);
+  if(newe->port)
+    free(newe->port);
+  free(newe);
+  return;
+
+}
+
 /*
   Local Variables:
   mode:c
@@ -539,3 +622,4 @@
   c-basic-offset:2
   End:
 */
+



More information about the tor-commits mailing list