[or-cvs] Use (set|get)_uint(16|32) in lieu of memcpy where reasonable.

Nick Mathewson nickm at seul.org
Sat Apr 3 03:07:35 UTC 2004


Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/tmp/cvs-serv20900/src/or

Modified Files:
	buffers.c circuit.c connection_edge.c rendservice.c 
Log Message:
Use (set|get)_uint(16|32) in lieu of memcpy where reasonable.

Index: buffers.c
===================================================================
RCS file: /home/or/cvsroot/src/or/buffers.c,v
retrieving revision 1.75
retrieving revision 1.76
diff -u -d -r1.75 -r1.76
--- buffers.c	31 Mar 2004 05:10:34 -0000	1.75
+++ buffers.c	3 Apr 2004 03:07:25 -0000	1.76
@@ -487,8 +487,7 @@
           }
           memcpy(req->address,buf->mem+5,len);
           req->address[len] = 0;
-          memcpy(&req->port, buf->mem+5+len, 2);
-          req->port = ntohs(req->port);
+          req->port = ntohs(get_uint16(buf->mem+5+len));
           buf_remove_from_front(buf, 5+len+2);
           return 1;
         default: /* unsupported */

Index: circuit.c
===================================================================
RCS file: /home/or/cvsroot/src/or/circuit.c,v
retrieving revision 1.179
retrieving revision 1.180
diff -u -d -r1.179 -r1.180
--- circuit.c	3 Apr 2004 03:06:06 -0000	1.179
+++ circuit.c	3 Apr 2004 03:07:25 -0000	1.180
@@ -1315,13 +1315,8 @@
     return -1;
   }
 
-  memcpy(&circ->n_addr, cell->payload+RELAY_HEADER_SIZE, 4);
-  circ->n_addr = ntohl(circ->n_addr);
-  memcpy(&circ->n_port, cell->payload+RELAY_HEADER_SIZE+4, 2);
-  circ->n_port = ntohs(circ->n_port);
-
-//  circ->n_addr = ntohl(*(uint32_t*)(cell->payload+RELAY_HEADER_SIZE));
-//  circ->n_port = ntohs(*(uint16_t*)(cell->payload+RELAY_HEADER_SIZE+4));
+  circ->n_addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
+  circ->n_port = ntohs(get_uint16(cell->payload+RELAY_HEADER_SIZE+4));
 
   n_conn = connection_twin_get_by_addr_port(circ->n_addr,circ->n_port);
   if(!n_conn || n_conn->type != CONN_TYPE_OR) {

Index: connection_edge.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_edge.c,v
retrieving revision 1.132
retrieving revision 1.133
diff -u -d -r1.132 -r1.133
--- connection_edge.c	3 Apr 2004 03:06:06 -0000	1.132
+++ connection_edge.c	3 Apr 2004 03:07:25 -0000	1.133
@@ -18,56 +18,21 @@
 static void client_dns_set_entry(const char *address, uint32_t val);
 
 void relay_header_pack(char *dest, const relay_header_t *src) {
-  uint16_t tmp;
-
-  /* we have to do slow memcpy's here, because we screwed up
-   * and made our cell payload not word-aligned. we should fix
-   * this someday.
-   */
-
   *(uint8_t*)(dest) = src->command;
 
-  tmp = htons(src->recognized);
-  memcpy(dest+1, &tmp, 2);
-
-  tmp = htons(src->stream_id);
-  memcpy(dest+3, &tmp, 2);
-
-  memcpy(dest+5, src->integrity, 4);
-
-  tmp = htons(src->length);
-  memcpy(dest+9, &tmp, 2);
-
-#if 0
-  *(uint8_t*)(dest)    = src->command;
-  *(uint16_t*)(dest+1) = htons(src->recognized);
-  *(uint16_t*)(dest+3) = htons(src->stream_id);
+  set_uint16(dest+1, htons(src->recognized));
+  set_uint16(dest+3, htons(src->stream_id));
   memcpy(dest+5, src->integrity, 4);
-  *(uint16_t*)(dest+9) = htons(src->length);
-#endif
+  set_uint16(dest+9, htons(src->length));
 }
 
 void relay_header_unpack(relay_header_t *dest, const char *src) {
   dest->command = *(uint8_t*)(src);
 
-  memcpy(&dest->recognized, src+1, 2);
-  dest->recognized = ntohs(dest->recognized);
-
-  memcpy(&dest->stream_id, src+3, 2);
-  dest->stream_id = ntohs(dest->stream_id);
-
-  memcpy(dest->integrity, src+5, 4);
-
-  memcpy(&dest->length, src+9, 2);
-  dest->length = ntohs(dest->length);
-
-#if 0
-  dest->command    = *(uint8_t*)(src);
-  dest->recognized = ntohs(*(uint16_t*)(src+1));
-  dest->stream_id  = ntohs(*(uint16_t*)(src+3));
+  dest->recognized = ntohs(get_uint16(src+1));
+  dest->stream_id = ntohs(get_uint16(src+3));
   memcpy(dest->integrity, src+5, 4);
-  dest->length     = ntohs(*(uint16_t*)(src+9));
-#endif
+  dest->length = ntohs(get_uint16(src+9));
 }
 
 /* mark and return -1 if there was an unexpected error with the conn,
@@ -178,8 +143,7 @@
 
   payload[0] = reason;
   if(reason == END_STREAM_REASON_EXITPOLICY) {
-    uint32_t tmp = htonl(conn->addr);
-    memcpy(payload+1, &tmp, 4);
+    set_uint32(payload+1, htonl(conn->addr));
 //    *(uint32_t *)(payload+1) = htonl(conn->addr);
     payload_len += 4;
   }
@@ -288,8 +252,7 @@
 //      log_fn(LOG_INFO,"Connected! Notifying application.");
       conn->state = AP_CONN_STATE_OPEN;
       if (rh.length >= 4) {
-        memcpy(&addr, cell->payload + RELAY_HEADER_SIZE, 4);
-        addr = ntohl(addr);
+        addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
         client_dns_set_entry(conn->socks_request->address, addr);
       }
       log_fn(LOG_INFO,"'connected' received after %d seconds.",
@@ -370,8 +333,7 @@
          * we try a new exit node.
          * cell->payload+RELAY_HEADER_SIZE+1 holds the destination addr.
          */
-        memcpy(&addr, cell->payload+RELAY_HEADER_SIZE+1, 4);
-        addr = ntohl(addr);
+        addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
         client_dns_set_entry(conn->socks_request->address, addr);
         /* conn->purpose is still set to general */
         conn->state = AP_CONN_STATE_CIRCUIT_WAIT;

Index: rendservice.c
===================================================================
RCS file: /home/or/cvsroot/src/or/rendservice.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- rendservice.c	3 Apr 2004 02:40:30 -0000	1.17
+++ rendservice.c	3 Apr 2004 03:07:25 -0000	1.18
@@ -29,7 +29,7 @@
   /* Other fields */
   crypto_pk_env_t *private_key;
   char service_id[REND_SERVICE_ID_LEN+1];
-  char pk_digest[20];
+  char pk_digest[DIGEST_LEN];
   smartlist_t *intro_nodes; /* list of nicknames */
   rend_service_descriptor_t *desc;
 } rend_service_t;
@@ -600,8 +600,8 @@
       circ = NULL;
       found = 1;
       while ((circ = circuit_get_next_by_pk_and_purpose(
-                                                circ,service->pk_digest,
-                                                CIRCUIT_PURPOSE_S_ESTABLISH_INTRO))) {
+                                        circ,service->pk_digest,
+                                        CIRCUIT_PURPOSE_S_ESTABLISH_INTRO))) {
         assert(circ->cpath);
         if (circ->cpath->prev->addr == router->addr &&
             circ->cpath->prev->port == router->or_port) {



More information about the tor-commits mailing list