[or-cvs] various bugfixes and updates

Roger Dingledine arma at seul.org
Thu Sep 25 10:42:09 UTC 2003


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

Modified Files:
	buffers.c connection.c connection_or.c directory.c main.c or.h 
	routers.c 
Log Message:
various bugfixes and updates

redo all the config files for the new format (we'll redo them again soon)

fix (another! yuck) segfault in log_fn when input is too large
tor_tls_context_new() returns -1 for error, not NULL
fix segfault in check_conn_marked() on conn's that die during tls handshake

make ORs also initialize conn from router when we're the receiving node

make non-dirserver ORs upload descriptor to every dirserver on startup
add our local address to the descriptor
add Content-Length field to POST command
revert the Content-Length search in fetch_from_buf_http() to previous code
fix segfault in memmove in fetch_from_buf_http()
raise maximum allowed headers/body size in directory.c





Index: buffers.c
===================================================================
RCS file: /home/or/cvsroot/src/or/buffers.c,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -d -r1.37 -r1.38
--- buffers.c	25 Sep 2003 05:17:10 -0000	1.37
+++ buffers.c	25 Sep 2003 10:42:06 -0000	1.38
@@ -38,6 +38,10 @@
   return -1;
 }
 
+int find_on_inbuf(char *string, int string_len, buf_t *buf) {
+  return find_str_in_str(string, string_len, buf->buf, buf->datalen);
+}
+
 /* Create and return a new buf of size 'size'
  */
 buf_t *buf_new_with_capacity(size_t size) {
@@ -206,7 +210,7 @@
   return r;
 }
 
-int write_to_buf(char *string, int string_len, buf_t *buf) {
+int write_to_buf(const char *string, int string_len, buf_t *buf) {
 
   /* append string to buf (growing as needed, return -1 if "too big")
    * return total number of bytes on the buf
@@ -285,11 +289,12 @@
   }
 
 #define CONTENT_LENGTH "\r\nContent-Length: "
-  i = find_str_in_str(CONTENT_LENGTH, sizeof(CONTENT_LENGTH), 
+  i = find_str_in_str(CONTENT_LENGTH, strlen(CONTENT_LENGTH), 
                       headers, headerlen);
   if(i > 0) {
     contentlen = atoi(headers+i);
     /* XXX What if content-length is malformed? */
+    log_fn(LOG_DEBUG,"Got a contentlen of %d.",contentlen);
     if(bodylen < contentlen) {
       log_fn(LOG_DEBUG,"body not all here yet.");
       return 0; /* not all there yet */
@@ -307,7 +312,7 @@
     body_out[bodylen] = 0; /* null terminate it */
   }
   buf->datalen -= (headerlen+bodylen);
-  memmove(buf, buf->buf+headerlen+bodylen, buf->datalen);
+  memmove(buf->buf, buf->buf+headerlen+bodylen, buf->datalen);
 
   return 1;
 }
@@ -399,15 +404,6 @@
   memmove(buf->buf, next+1, buf->datalen);
 //  log_fn(LOG_DEBUG,"buf_datalen is now %d:'%s'",*buf_datalen,buf);
   return 1;
-}
-
-int find_on_inbuf(char *string, int string_len, buf_t *buf) {
-  /* find first instance of needle 'string' on haystack 'buf'. return how
-   * many bytes from the beginning of buf to the end of string.
-   * If it's not there, return -1.
-   */
-
-  return find_str_in_str(string, string_len, buf->buf, buf->datalen);
 }
 
 /*

Index: connection.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection.c,v
retrieving revision 1.102
retrieving revision 1.103
diff -u -d -r1.102 -r1.103
--- connection.c	25 Sep 2003 05:17:10 -0000	1.102
+++ connection.c	25 Sep 2003 10:42:06 -0000	1.103
@@ -304,19 +304,14 @@
           return -1;
         }
         log_fn(LOG_DEBUG,"The router's pk matches the one we meant to connect to. Good.");
-        crypto_free_pk_env(pk);
       } else {
         if(connection_exact_get_by_addr_port(router->addr,router->or_port)) {
           log_fn(LOG_INFO,"That router is already connected. Dropping.");
           return -1;
         }
-        conn->link_pkey = pk;
-        conn->bandwidth = router->bandwidth;
-        conn->addr = router->addr, conn->port = router->or_port;
-        if(conn->address)
-          free(conn->address);
-        conn->address = strdup(router->address);
+        connection_or_init_conn_from_router(conn, router);
       }
+      crypto_free_pk_env(pk);
     } else { /* it's an OP */
       conn->bandwidth = DEFAULT_BANDWIDTH_OP;
     }
@@ -615,7 +610,7 @@
   return 0;
 }
 
-int connection_write_to_buf(char *string, int len, connection_t *conn) {
+int connection_write_to_buf(const char *string, int len, connection_t *conn) {
 
   if(!len)
     return 0;

Index: connection_or.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_or.c,v
retrieving revision 1.53
retrieving revision 1.54
diff -u -d -r1.53 -r1.54
--- connection_or.c	25 Sep 2003 05:17:10 -0000	1.53
+++ connection_or.c	25 Sep 2003 10:42:06 -0000	1.54
@@ -73,6 +73,18 @@
 
 /*********************/
 
+void connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router) {
+  conn->addr = router->addr;
+  conn->port = router->or_port;
+  conn->bandwidth = router->bandwidth;
+  conn->onion_pkey = crypto_pk_dup_key(router->onion_pkey);
+  conn->link_pkey = crypto_pk_dup_key(router->link_pkey);
+  conn->identity_pkey = crypto_pk_dup_key(router->identity_pkey);
+  if(conn->address)
+    free(conn->address);
+  conn->address = strdup(router->address);
+}
+
 connection_t *connection_or_connect(routerinfo_t *router) {
   connection_t *conn;
 
@@ -96,12 +108,7 @@
   }
 
   /* set up conn so it's got all the data we need to remember */
-  conn->addr = router->addr;
-  conn->port = router->or_port;
-  conn->bandwidth = router->bandwidth;
-  conn->onion_pkey = crypto_pk_dup_key(router->onion_pkey);
-  conn->link_pkey = crypto_pk_dup_key(router->link_pkey);
-  conn->address = strdup(router->address);
+  connection_or_init_conn_from_router(conn, router);
 
   if(connection_add(conn) < 0) { /* no space, forget it */
     connection_free(conn);

Index: directory.c
===================================================================
RCS file: /home/or/cvsroot/src/or/directory.c,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -d -r1.31 -r1.32
--- directory.c	25 Sep 2003 05:17:11 -0000	1.31
+++ directory.c	25 Sep 2003 10:42:06 -0000	1.32
@@ -19,7 +19,6 @@
 static int directory_dirty=1;
 
 static char fetchstring[] = "GET / HTTP/1.0\r\n\r\n";
-static char uploadstring[] = "POST / HTTP/1.0\r\n\r\n";
 static char answerstring[] = "HTTP/1.0 200 OK\r\n\r\n";
 
 /********* END VARIABLES ************/
@@ -88,7 +87,8 @@
 }
 
 static int directory_send_command(connection_t *conn, int command) {
-  char *s;
+  const char *s;
+  char tmp[8192];
 
   assert(conn && conn->type == CONN_TYPE_DIR);
 
@@ -106,8 +106,9 @@
         log_fn(LOG_DEBUG,"Failed to get my descriptor.");
         return -1;
       }
-      if(connection_write_to_buf(uploadstring, strlen(uploadstring), conn) < 0 ||
-         connection_write_to_buf(s, strlen(s), conn) < 0) {
+      snprintf(tmp, sizeof(tmp), "POST / HTTP/1.0\r\nContent-Length: %d\r\n\r\n%s",
+               strlen(s), s);
+      if(connection_write_to_buf(tmp, strlen(tmp), conn) < 0) {
         log_fn(LOG_DEBUG,"Couldn't write post/descriptor to buffer.");
         return -1;
       }
@@ -192,7 +193,7 @@
 
 static int directory_handle_command(connection_t *conn) {
   char headers[1024];
-  char body[1024];
+  char body[50000]; /* XXX */
 
   assert(conn && conn->type == CONN_TYPE_DIR);
 

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/src/or/main.c,v
retrieving revision 1.101
retrieving revision 1.102
diff -u -d -r1.101 -r1.102
--- main.c	25 Sep 2003 05:17:11 -0000	1.101
+++ main.c	25 Sep 2003 10:42:06 -0000	1.102
@@ -12,7 +12,7 @@
 /********* START VARIABLES **********/
 
 extern char *conn_type_to_string[];
-extern char *conn_state_to_string[][15];
+extern char *conn_state_to_string[][_CONN_TYPE_MAX+1];
 
 or_options_t options; /* command-line and config-file options */
 int global_read_bucket; /* max number of bytes I can read this second */
@@ -320,10 +320,12 @@
     log_fn(LOG_DEBUG,"Cleaning up connection.");
     if(conn->s >= 0) { /* might be an incomplete edge connection */
       /* FIXME there's got to be a better way to check for this -- and make other checks? */
-      if(connection_speaks_cells(conn) && conn->state != OR_CONN_STATE_CONNECTING)
-        flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen);
-      else
+      if(connection_speaks_cells(conn)) {
+        if(conn->state == OR_CONN_STATE_OPEN)
+          flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen);
+      } else {
         flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen);
+      }
       if(connection_wants_to_flush(conn)) /* not done flushing */
         log_fn(LOG_WARNING,"Conn (socket %d) still wants to flush. Losing %d bytes!",conn->s, (int)buf_datalen(conn->inbuf));
     }
@@ -642,7 +644,9 @@
   }
 
   if(options.OnionRouter) {
-    cpu_init(); /* launch cpuworkers. Need to do this *after* we've read the private key. */
+    cpu_init(); /* launch cpuworkers. Need to do this *after* we've read the onion key. */
+    if(options.DirPort == 0) /* not a dirserver; XXX eventually do this for dirservers too */
+      router_upload_desc_to_dirservers(); /* upload our descriptor to all dirservers */
   }
 
   /* start up the necessary connections based on which ports are
@@ -981,13 +985,20 @@
 /* XXX should this replace my_routerinfo? */
 static routerinfo_t *desc_routerinfo; 
 const char *router_get_my_descriptor(void) {
+  log_fn(LOG_DEBUG,"my desc is '%s'",descriptor);	
   return descriptor;
 }
 
 static int init_descriptor(void) {
   routerinfo_t *ri;
+  char localhostname[256];
+
+  if(gethostname(localhostname,sizeof(localhostname)) < 0) {
+    log_fn(LOG_ERR,"Error obtaining local hostname");
+    return -1;
+  }
   ri = tor_malloc(sizeof(routerinfo_t));
-  ri->address = strdup("XXXXXXX"); /*XXX*/
+  ri->address = strdup(localhostname);
   ri->nickname = strdup(options.Nickname);
   /* No need to set addr. ???? */
   ri->or_port = options.ORPort;

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.137
retrieving revision 1.138
diff -u -d -r1.137 -r1.138
--- or.h	25 Sep 2003 05:17:11 -0000	1.137
+++ or.h	25 Sep 2003 10:42:06 -0000	1.138
@@ -434,6 +434,8 @@
 
 /********************************* buffers.c ***************************/
 
+int find_on_inbuf(char *string, int string_len, buf_t *buf);
+
 buf_t *buf_new();
 buf_t *buf_new_with_capacity(size_t size);
 void buf_free(buf_t *buf);
@@ -448,7 +450,7 @@
 int flush_buf(int s, buf_t *buf, int *buf_flushlen);
 int flush_buf_tls(tor_tls *tls, buf_t *buf, int *buf_flushlen);
 
-int write_to_buf(char *string, int string_len, buf_t *buf);
+int write_to_buf(const char *string, int string_len, buf_t *buf);
 int fetch_from_buf(char *string, int string_len, buf_t *buf);
 int fetch_from_buf_http(buf_t *buf,
                         char *headers_out, int max_headerlen,
@@ -456,7 +458,6 @@
 int fetch_from_buf_socks(buf_t *buf,
                          char *addr_out, int max_addrlen,
                          uint16_t *port_out);
-int find_on_inbuf(char *string, int string_len, buf_t *buf);
 
 /********************************* circuit.c ***************************/
 
@@ -529,7 +530,7 @@
 int connection_outbuf_too_full(connection_t *conn);
 int connection_flush_buf(connection_t *conn);
 int connection_handle_write(connection_t *conn);
-int connection_write_to_buf(char *string, int len, connection_t *conn);
+int connection_write_to_buf(const char *string, int len, connection_t *conn);
 
 int connection_receiver_bucket_should_increase(connection_t *conn);
 
@@ -562,6 +563,7 @@
 int connection_or_process_inbuf(connection_t *conn);
 int connection_or_finished_flushing(connection_t *conn);
 
+void connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router);
 connection_t *connection_or_connect(routerinfo_t *router);
 
 int connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn);
@@ -658,6 +660,7 @@
 int learn_my_address(struct sockaddr_in *me);
 void router_retry_connections(void);
 routerinfo_t *router_pick_directory_server(void);
+void router_upload_desc_to_dirservers(void);
 routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
 routerinfo_t *router_get_by_link_pk(crypto_pk_env_t *pk);
 #if 0

Index: routers.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routers.c,v
retrieving revision 1.50
retrieving revision 1.51
diff -u -d -r1.50 -r1.51
--- routers.c	25 Sep 2003 05:17:11 -0000	1.50
+++ routers.c	25 Sep 2003 10:42:07 -0000	1.51
@@ -37,14 +37,14 @@
 
 int learn_my_address(struct sockaddr_in *me) {
   /* local host information */
-  char localhostname[512];
+  char localhostname[256];
   struct hostent *localhost;
   static struct sockaddr_in answer;
   static int already_learned=0;
 
   if(!already_learned) {
     /* obtain local host information */
-    if(gethostname(localhostname,512) < 0) {
+    if(gethostname(localhostname,sizeof(localhostname)) < 0) {
       log_fn(LOG_ERR,"Error obtaining local hostname");
       return -1;
     }
@@ -98,6 +98,20 @@
   }
 
   return NULL;
+}
+
+void router_upload_desc_to_dirservers(void) {
+  int i;
+  routerinfo_t *router;
+
+  if(!directory)
+    return;
+
+  for(i=0;i<directory->n_routers;i++) {
+    router = directory->routers[i];
+    if(router->dir_port > 0)
+      directory_initiate_command(router, DIR_CONN_STATE_CONNECTING_UPLOAD);
+  }
 }
 
 routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {



More information about the tor-commits mailing list