[or-cvs] wrap strdup; prefer time() to gettimeofday()

Roger Dingledine arma at seul.org
Sat Oct 4 03:29:13 UTC 2003


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

Modified Files:
	circuit.c command.c config.c connection.c connection_edge.c 
	connection_or.c cpuworker.c directory.c dirserv.c dns.c main.c 
	routers.c 
Log Message:
wrap strdup; prefer time() to gettimeofday()


Index: circuit.c
===================================================================
RCS file: /home/or/cvsroot/src/or/circuit.c,v
retrieving revision 1.73
retrieving revision 1.74
diff -u -d -r1.73 -r1.74
--- circuit.c	4 Oct 2003 02:38:17 -0000	1.73
+++ circuit.c	4 Oct 2003 03:29:09 -0000	1.74
@@ -58,14 +58,11 @@
 
 circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn) {
   circuit_t *circ; 
-  struct timeval now;
-
-  my_gettimeofday(&now);
 
   circ = (circuit_t *)tor_malloc(sizeof(circuit_t));
   memset(circ,0,sizeof(circuit_t)); /* zero it out */
 
-  circ->timestamp_created = now.tv_sec;
+  circ->timestamp_created = time(NULL);
 
   circ->p_aci = p_aci;
   circ->p_conn = p_conn;

Index: command.c
===================================================================
RCS file: /home/or/cvsroot/src/or/command.c,v
retrieving revision 1.41
retrieving revision 1.42
diff -u -d -r1.41 -r1.42
--- command.c	2 Oct 2003 20:00:38 -0000	1.41
+++ command.c	4 Oct 2003 03:29:09 -0000	1.42
@@ -25,11 +25,11 @@
 
   *num += 1;
 
-  my_gettimeofday(&start);
+  tor_gettimeofday(&start);
 
   (*func)(cell, conn);
 
-  my_gettimeofday(&end);
+  tor_gettimeofday(&end);
   time_passed = tv_udiff(&start, &end) ;
 
   if (time_passed > 5000) { /* more than 5ms */
@@ -38,17 +38,13 @@
   *time += time_passed;
 }
 
-
-
 void command_process_cell(cell_t *cell, connection_t *conn) {
   static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
   static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
-  static long current_second = 0; /* from previous calls to gettimeofday */
-  struct timeval now;
-
-  my_gettimeofday(&now);
+  static time_t current_second = 0; /* from previous calls to time */
+  time_t now = time(NULL);
 
-  if(now.tv_sec > current_second) { /* the second has rolled over */
+  if(now > current_second) { /* the second has rolled over */
     /* print stats */
     log(LOG_INFO,"At end of second:"); 
     log(LOG_INFO,"Create:    %d (%d ms)", num_create, create_time/1000);
@@ -61,7 +57,7 @@
     create_time = created_time = relay_time = destroy_time = 0;
 
     /* remember which second it is, for next time */
-    current_second = now.tv_sec; 
+    current_second = now;
   }
 
   switch(cell->command) {

Index: config.c
===================================================================
RCS file: /home/or/cvsroot/src/or/config.c,v
retrieving revision 1.50
retrieving revision 1.51
diff -u -d -r1.50 -r1.51
--- config.c	1 Oct 2003 01:49:53 -0000	1.50
+++ config.c	4 Oct 2003 03:29:09 -0000	1.51
@@ -61,8 +61,8 @@
     s = argv[i];
     while(*s == '-')
       s++;
-    new->key = strdup(s);
-    new->value = strdup(argv[i+1]);
+    new->key = tor_strdup(s);
+    new->value = tor_strdup(argv[i+1]);
 
     log(LOG_DEBUG,"Commandline: parsed keyword '%s', value '%s'",
       new->key, new->value);
@@ -85,8 +85,8 @@
 
   while( (result=parse_line_from_file(line,sizeof(line),f,&key,&value)) > 0) {
     new = tor_malloc(sizeof(struct config_line));
-    new->key = strdup(key);
-    new->value = strdup(value);
+    new->key = tor_strdup(key);
+    new->value = tor_strdup(value);
 
     new->next = front;
     front = new;
@@ -131,7 +131,7 @@
       *(int *)arg = i;
       break;
     case CONFIG_TYPE_STRING:
-      *(char **)arg = strdup(c->value);
+      *(char **)arg = tor_strdup(c->value);
       break;
     case CONFIG_TYPE_DOUBLE:
       *(double *)arg = atof(c->value);

Index: connection.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection.c,v
retrieving revision 1.114
retrieving revision 1.115
diff -u -d -r1.114 -r1.115
--- connection.c	4 Oct 2003 02:38:17 -0000	1.114
+++ connection.c	4 Oct 2003 03:29:09 -0000	1.115
@@ -73,9 +73,7 @@
 
 connection_t *connection_new(int type) {
   connection_t *conn;
-  struct timeval now;
-
-  my_gettimeofday(&now);
+  time_t now = time(NULL);
 
   conn = (connection_t *)tor_malloc(sizeof(connection_t));
   memset(conn,0,sizeof(connection_t)); /* zero it out to start */
@@ -84,9 +82,9 @@
   conn->inbuf = buf_new();
   conn->outbuf = buf_new();
 
-  conn->timestamp_created = now.tv_sec;
-  conn->timestamp_lastread = now.tv_sec;
-  conn->timestamp_lastwritten = now.tv_sec;
+  conn->timestamp_created = now;
+  conn->timestamp_lastread = now;
+  conn->timestamp_lastwritten = now;
 
   return conn;
 }
@@ -195,7 +193,7 @@
   newconn = connection_new(new_type);
   newconn->s = news;
 
-  newconn->address = strdup(inet_ntoa(remote.sin_addr)); /* remember the remote address */
+  newconn->address = tor_strdup(inet_ntoa(remote.sin_addr)); /* remember the remote address */
   newconn->addr = ntohl(remote.sin_addr.s_addr);
   newconn->port = ntohs(remote.sin_port);
 
@@ -309,10 +307,8 @@
 }
 
 int connection_handle_read(connection_t *conn) {
-  struct timeval now;
 
-  my_gettimeofday(&now);
-  conn->timestamp_lastread = now.tv_sec;
+  conn->timestamp_lastread = time(NULL);
 
   switch(conn->type) {
     case CONN_TYPE_OR_LISTENER:
@@ -433,15 +429,13 @@
 
 /* return -1 if you want to break the conn, else return 0 */
 int connection_handle_write(connection_t *conn) {
-  struct timeval now;
 
   if(connection_is_listener(conn)) {
     log_fn(LOG_WARNING,"Got a listener socket. Can't happen!");
     return -1;
   }
 
-  my_gettimeofday(&now);
-  conn->timestamp_lastwritten = now.tv_sec;
+  conn->timestamp_lastwritten = time(NULL);
 
   if(connection_speaks_cells(conn) && conn->state != OR_CONN_STATE_CONNECTING) {
     if(conn->state == OR_CONN_STATE_HANDSHAKING) {

Index: connection_edge.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_edge.c,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -d -r1.31 -r1.32
--- connection_edge.c	4 Oct 2003 02:38:17 -0000	1.31
+++ connection_edge.c	4 Oct 2003 03:29:09 -0000	1.32
@@ -577,7 +577,7 @@
   n_stream = connection_new(CONN_TYPE_EXIT);
 
   memcpy(n_stream->stream_id, cell->payload + RELAY_HEADER_SIZE, STREAM_ID_SIZE);
-  n_stream->address = strdup(cell->payload + RELAY_HEADER_SIZE + STREAM_ID_SIZE);
+  n_stream->address = tor_strdup(cell->payload + RELAY_HEADER_SIZE + STREAM_ID_SIZE);
   n_stream->port = atoi(colon+1);
   n_stream->state = EXIT_CONN_STATE_RESOLVING;
   n_stream->s = -1; /* not yet valid */

Index: connection_or.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_or.c,v
retrieving revision 1.62
retrieving revision 1.63
diff -u -d -r1.62 -r1.63
--- connection_or.c	4 Oct 2003 02:38:17 -0000	1.62
+++ connection_or.c	4 Oct 2003 03:29:09 -0000	1.63
@@ -83,10 +83,10 @@
   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);
-  conn->nickname = strdup(router->nickname);
+  conn->nickname = tor_strdup(router->nickname);
   if(conn->address)
     free(conn->address);
-  conn->address = strdup(router->address);
+  conn->address = tor_strdup(router->address);
 }
 
 connection_t *connection_or_connect(routerinfo_t *router) {

Index: cpuworker.c
===================================================================
RCS file: /home/or/cvsroot/src/or/cpuworker.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- cpuworker.c	4 Oct 2003 02:38:17 -0000	1.10
+++ cpuworker.c	4 Oct 2003 03:29:09 -0000	1.11
@@ -184,7 +184,7 @@
 
   /* set up conn so it's got all the data we need to remember */
   conn->s = fd[0];
-  conn->address = strdup("localhost");
+  conn->address = tor_strdup("localhost");
 
   if(connection_add(conn) < 0) { /* no space, forget it */
     log_fn(LOG_WARNING,"connection_add failed. Giving up.");

Index: directory.c
===================================================================
RCS file: /home/or/cvsroot/src/or/directory.c,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -d -r1.42 -r1.43
--- directory.c	4 Oct 2003 02:38:17 -0000	1.42
+++ directory.c	4 Oct 2003 03:29:09 -0000	1.43
@@ -45,8 +45,8 @@
   /* set up conn so it's got all the data we need to remember */
   conn->addr = router->addr;
   conn->port = router->dir_port;
-  conn->address = strdup(router->address);
-  conn->nickname = strdup(router->nickname);
+  conn->address = tor_strdup(router->address);
+  conn->nickname = tor_strdup(router->nickname);
   if (router->identity_pkey)
     conn->identity_pkey = crypto_pk_dup_key(router->identity_pkey);
   else {

Index: dirserv.c
===================================================================
RCS file: /home/or/cvsroot/src/or/dirserv.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- dirserv.c	1 Oct 2003 00:43:34 -0000	1.8
+++ dirserv.c	4 Oct 2003 03:29:09 -0000	1.9
@@ -29,12 +29,12 @@
   for (i = 0; i < n_fingerprints; ++i) {
     if (!strcasecmp(fingerprint_list[i].nickname,nickname)) {
       free(fingerprint_list[i].fingerprint);
-      fingerprint_list[i].fingerprint = strdup(fp);
+      fingerprint_list[i].fingerprint = tor_strdup(fp);
       return;
     }
   }
-  fingerprint_list[n_fingerprints].nickname = strdup(nickname);
-  fingerprint_list[n_fingerprints].fingerprint = strdup(fp);
+  fingerprint_list[n_fingerprints].nickname = tor_strdup(nickname);
+  fingerprint_list[n_fingerprints].fingerprint = tor_strdup(fp);
   ++n_fingerprints;
 }
 
@@ -83,8 +83,8 @@
       }
     }
     if(i == n_fingerprints_tmp) { /* not a duplicate */
-      fingerprint_list_tmp[n_fingerprints_tmp].nickname = strdup(nickname);
-      fingerprint_list_tmp[n_fingerprints_tmp].fingerprint = strdup(fingerprint);
+      fingerprint_list_tmp[n_fingerprints_tmp].nickname = tor_strdup(nickname);
+      fingerprint_list_tmp[n_fingerprints_tmp].fingerprint = tor_strdup(fingerprint);
       ++n_fingerprints_tmp;
     }
   }
@@ -427,7 +427,7 @@
     /* Now read the directory we just made in order to update our own
      * router lists.  This does more signature checking than is strictly
      * necessary, but safe is better than sorry. */
-    new_directory = strdup(the_directory);
+    new_directory = tor_strdup(the_directory);
     /* use a new copy of the dir, since get_dir_from_string scribbles on it */
     if (router_get_dir_from_string(new_directory, get_identity_key())) {
       log_fn(LOG_ERR, "We just generated a directory we can't parse. Dying.");

Index: dns.c
===================================================================
RCS file: /home/or/cvsroot/src/or/dns.c,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- dns.c	4 Oct 2003 02:38:17 -0000	1.30
+++ dns.c	4 Oct 2003 03:29:09 -0000	1.31
@@ -165,7 +165,7 @@
     return -1;
   }
 
-  dnsconn->address = strdup(exitconn->address);
+  dnsconn->address = tor_strdup(exitconn->address);
   dnsconn->state = DNSWORKER_STATE_BUSY;
   num_dnsworkers_busy++;
 
@@ -381,7 +381,7 @@
 
   /* set up conn so it's got all the data we need to remember */
   conn->s = fd[0];
-  conn->address = strdup("localhost");
+  conn->address = tor_strdup("localhost");
 
   if(connection_add(conn) < 0) { /* no space, forget it */
     log_fn(LOG_WARNING,"connection_add failed. Giving up.");

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/src/or/main.c,v
retrieving revision 1.124
retrieving revision 1.125
diff -u -d -r1.124 -r1.125
--- main.c	4 Oct 2003 02:38:17 -0000	1.124
+++ main.c	4 Oct 2003 03:29:09 -0000	1.125
@@ -270,7 +270,7 @@
   cell_t cell;
   circuit_t *circ;
 
-  my_gettimeofday(&now);
+  tor_gettimeofday(&now);
 
   if(now.tv_sec > current_second) { /* the second has rolled over. check more stuff. */
 
@@ -656,23 +656,22 @@
 static void dumpstats(void) { /* dump stats to stdout */
   int i;
   connection_t *conn;
-  struct timeval now;
+  time_t now = time(NULL);
 
   printf("Dumping stats:\n");
-  my_gettimeofday(&now);
 
   for(i=0;i<nfds;i++) {
     conn = connection_array[i];
     printf("Conn %d (socket %d) type %d (%s), state %d (%s), created %ld secs ago\n",
       i, conn->s, conn->type, conn_type_to_string[conn->type],
-      conn->state, conn_state_to_string[conn->type][conn->state], now.tv_sec - conn->timestamp_created);
+      conn->state, conn_state_to_string[conn->type][conn->state], now - conn->timestamp_created);
     if(!connection_is_listener(conn)) {
       printf("Conn %d is to '%s:%d'.\n",i,conn->address, conn->port);
       printf("Conn %d: %d bytes waiting on inbuf (last read %ld secs ago)\n",i,
              (int)buf_datalen(conn->inbuf),
-             now.tv_sec - conn->timestamp_lastread);
-      printf("Conn %d: %d bytes waiting on outbuf (last written %ld secs ago)\n",i,(int)buf_datalen(conn->outbuf),
-        now.tv_sec - conn->timestamp_lastwritten);
+             now - conn->timestamp_lastread);
+      printf("Conn %d: %d bytes waiting on outbuf (last written %ld secs ago)\n",i,
+             (int)buf_datalen(conn->outbuf), now - conn->timestamp_lastwritten);
     }
     circuit_dump_by_conn(conn); /* dump info about all the circuits using this conn */
     printf("\n");
@@ -703,7 +702,6 @@
   if (stats_n_seconds_reading)
     printf("Average bandwidth used: %d bytes/sec\n",
            (int) (stats_n_bytes_read/stats_n_seconds_reading));
-
 }
 
 void daemonize(void) {

Index: routers.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routers.c,v
retrieving revision 1.68
retrieving revision 1.69
diff -u -d -r1.68 -r1.69
--- routers.c	1 Oct 2003 22:31:12 -0000	1.68
+++ routers.c	4 Oct 2003 03:29:09 -0000	1.69
@@ -606,7 +606,7 @@
     log_fn(LOG_WARNING, "Invalid recommended-software line");
     goto err;
   }
-  versions = strdup(tok.val.cmd.args[0]);
+  versions = tor_strdup(tok.val.cmd.args[0]);
   
   NEXT_TOK();
   TOK_IS(K_RUNNING_ROUTERS, "running-routers");
@@ -801,8 +801,7 @@
     log_fn(LOG_WARNING,"Wrong # of arguments to \"router\"");
     goto err;
   }
-  if (!(router->nickname = strdup(ARGS[0])))
-    goto err;
+  router->nickname = tor_strdup(ARGS[0]);
   if (strlen(router->nickname) > MAX_NICKNAME_LEN) {
     log_fn(LOG_WARNING,"Router nickname too long.");
     goto err;
@@ -814,8 +813,7 @@
   }
   
   /* read router.address */
-  if (!(router->address = strdup(ARGS[1])))
-    goto err;
+  router->address = tor_strdup(ARGS[1]);
   router->addr = 0;
 
   /* Read router->or_port */
@@ -975,8 +973,8 @@
   if(!colon)
     goto policy_read_failed;
   *colon = 0;
-  newe->address = strdup(arg);
-  newe->port = strdup(colon+1);
+  newe->address = tor_strdup(arg);
+  newe->port = tor_strdup(colon+1);
 
   log_fn(LOG_DEBUG,"%s %s:%s",
       newe->policy_type == EXIT_POLICY_REJECT ? "reject" : "accept",
@@ -1064,8 +1062,8 @@
     address = localhostname;
   }
   ri = tor_malloc(sizeof(routerinfo_t));
-  ri->address = strdup(address);
-  ri->nickname = strdup(options.Nickname);
+  ri->address = tor_strdup(address);
+  ri->nickname = tor_strdup(options.Nickname);
   /* No need to set addr. */
   ri->or_port = options.ORPort;
   ri->ap_port = options.APPort;
@@ -1205,7 +1203,7 @@
   s[written+1] = 0;
 
 #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
-  s_tmp = s_dup = strdup(s);
+  s_tmp = s_dup = tor_strdup(s);
   ri_tmp = router_get_entry_from_string(&s_tmp);
   if (!ri_tmp) {
     log_fn(LOG_ERR, "We just generated a router descriptor we can't parse: <<%s>>", 



More information about the tor-commits mailing list