[or-cvs] Choose correct abstraction for topic_foo. Abstract random-...
Nick Mathewson
nickm at seul.org
Thu Apr 17 17:10:43 UTC 2003
Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/tmp/cvs-serv25669/src/or
Modified Files:
circuit.c connection.c connection_ap.c connection_edge.c
onion.c or.h
Log Message:
Choose correct abstraction for topic_foo. Abstract random-integer code
Index: circuit.c
===================================================================
RCS file: /home/or/cvsroot/src/or/circuit.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -d -r1.34 -r1.35
--- circuit.c 16 Apr 2003 23:21:44 -0000 1.34
+++ circuit.c 17 Apr 2003 17:10:40 -0000 1.35
@@ -118,7 +118,8 @@
try_again:
log(LOG_DEBUG,"get_unique_aci_by_addr_port() trying to get a unique aci");
- crypto_pseudo_rand(2, (unsigned char *)&test_aci);
+ if (CRYPTO_PSEUDO_RAND_INT(test_aci))
+ return -1;
if(aci_type == ACI_TYPE_LOWER && test_aci >= (1<<15))
test_aci -= (1<<15);
Index: connection.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection.c,v
retrieving revision 1.55
retrieving revision 1.56
diff -u -d -r1.55 -r1.56
--- connection.c 16 Apr 2003 17:44:33 -0000 1.55
+++ connection.c 17 Apr 2003 17:10:40 -0000 1.56
@@ -698,8 +698,8 @@
cell.command = CELL_DATA;
- cell.topic_command = TOPIC_COMMAND_DATA;
- cell.topic_id = conn->topic_id;
+ SET_CELL_TOPIC_COMMAND(cell, TOPIC_COMMAND_DATA);
+ SET_CELL_TOPIC_ID(cell, conn->topic_id);
cell.length += TOPIC_HEADER_SIZE;
if(conn->type == CONN_TYPE_EXIT) {
@@ -752,8 +752,8 @@
memset(&cell, 0, sizeof(cell_t));
cell.command = CELL_DATA;
- cell.topic_command = TOPIC_COMMAND_SENDME;
- cell.topic_id = conn->topic_id;
+ SET_CELL_TOPIC_COMMAND(cell, TOPIC_COMMAND_SENDME);
+ SET_CELL_TOPIC_ID(cell, conn->topic_id);
cell.length += TOPIC_HEADER_SIZE;
if(edge_type == EDGE_EXIT) { /* we're at an exit */
@@ -860,14 +860,7 @@
*(uint8_t*)(dest+2) = src->command;
*(uint8_t*)(dest+3) = src->length;
*(uint32_t*)(dest+4) = 0; /* Reserved */
- if (src->command != CELL_DATA) {
- memcpy(dest+8, src->payload, CELL_PAYLOAD_SIZE);
- } else {
- *(uint8_t*)(dest+8) = src->topic_command;
- *(uint8_t*)(dest+9) = 0;
- *(uint16_t*)(dest+10) = htons(src->topic_id);
- memcpy(dest+12, src->payload, CELL_PAYLOAD_SIZE - TOPIC_HEADER_SIZE);
- }
+ memcpy(dest+8, src->payload, CELL_PAYLOAD_SIZE);
}
void
@@ -877,14 +870,7 @@
dest->command = *(uint8_t*)(src+2);
dest->length = *(uint8_t*)(src+3);
dest->seq = ntohl(*(uint32_t*)(src+4));
- if (dest->command != CELL_DATA) {
- memcpy(dest->payload, src+8, CELL_PAYLOAD_SIZE);
- } else {
- dest->topic_command = *(uint8_t*)(src+8);
- /* zero = *(uint8_t*)(src+9); */
- dest->topic_id = ntohs(*(uint16_t*)(src+10));
- memcpy(dest->payload, src+12, CELL_PAYLOAD_SIZE - TOPIC_HEADER_SIZE);
- }
+ memcpy(dest->payload, src+8, CELL_PAYLOAD_SIZE);
}
/*
Index: connection_ap.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_ap.c,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -d -r1.36 -r1.37
--- connection_ap.c 17 Apr 2003 16:46:44 -0000 1.36
+++ connection_ap.c 17 Apr 2003 17:10:40 -0000 1.37
@@ -119,15 +119,18 @@
int ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ) {
cell_t cell;
+ uint16_t topic_id;
memset(&cell, 0, sizeof(cell_t));
/* deliver the dest_addr in a data cell */
cell.command = CELL_DATA;
cell.aci = circ->n_aci;
- cell.topic_command = TOPIC_COMMAND_BEGIN;
- crypto_pseudo_rand(2, (char*)&cell.topic_id);
+ SET_CELL_TOPIC_COMMAND(cell, TOPIC_COMMAND_BEGIN);
+ if (CRYPTO_PSEUDO_RAND_INT(topic_id))
+ return -1;
+ SET_CELL_TOPIC_ID(cell, topic_id);
/* FIXME check for collisions */
- ap_conn->topic_id = cell.topic_id;
+ ap_conn->topic_id = topic_id;
snprintf(cell.payload+4, CELL_PAYLOAD_SIZE-4, "%s:%d", ap_conn->dest_addr, ap_conn->dest_port);
cell.length = strlen(cell.payload+TOPIC_HEADER_SIZE)+1+TOPIC_HEADER_SIZE;
Index: connection_edge.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_edge.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- connection_edge.c 16 Apr 2003 17:44:33 -0000 1.3
+++ connection_edge.c 17 Apr 2003 17:10:40 -0000 1.4
@@ -27,8 +27,8 @@
memset(&cell, 0, sizeof(cell_t));
cell.command = CELL_DATA;
cell.length = TOPIC_HEADER_SIZE;
- cell.topic_command = TOPIC_COMMAND_END;
- cell.topic_id = conn->topic_id;
+ SET_CELL_TOPIC_COMMAND(cell, TOPIC_COMMAND_END);
+ SET_CELL_TOPIC_ID(cell, conn->topic_id);
cell.aci = circ->n_aci;
if (circuit_deliver_data_cell_from_edge(&cell, circ, conn->type) < 0) {
@@ -76,8 +76,8 @@
else
cell.aci = circ->p_aci;
cell.command = CELL_DATA;
- cell.topic_command = topic_command;
- cell.topic_id = conn->topic_id;
+ SET_CELL_TOPIC_COMMAND(cell, topic_command);
+ SET_CELL_TOPIC_ID(cell, conn->topic_id);
cell.length = TOPIC_HEADER_SIZE;
log(LOG_INFO,"connection_edge_send_command(): delivering %d cell %s.", topic_command, conn->type == CONN_TYPE_AP ? "forward" : "backward");
@@ -100,8 +100,8 @@
assert(cell && circ);
- topic_command = cell->topic_command;
- topic_id = cell->topic_id;
+ topic_command = CELL_TOPIC_COMMAND(*cell);
+ topic_id = CELL_TOPIC_ID(*cell);
log(LOG_DEBUG,"connection_edge_process_data_cell(): command %d topic %d", topic_command, topic_id);
num_seen++;
log(LOG_DEBUG,"connection_edge_process_data_cell(): Now seen %d data cells here.", num_seen);
Index: onion.c
===================================================================
RCS file: /home/or/cvsroot/src/or/onion.c,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -d -r1.36 -r1.37
--- onion.c 16 Apr 2003 17:11:56 -0000 1.36
+++ onion.c 17 Apr 2003 17:10:40 -0000 1.37
@@ -311,15 +311,14 @@
{
int len = 2;
int retval = 0;
- unsigned char coin;
+ uint8_t coin;
if ((cw < 0) || (cw >= 1)) /* invalid parameter */
return -1;
while(1)
{
- retval = crypto_pseudo_rand(1, &coin);
- if (retval)
+ if (CRYPTO_PSEUDO_RAND_INT(coin))
return -1;
if (coin > cw*255) /* don't extend */
@@ -378,7 +377,7 @@
oldchoice = rarray_len;
for(i=0;i<*routelen;i++) {
log(LOG_DEBUG,"new_route(): Choosing hop %u.",i);
- if(crypto_pseudo_rand(sizeof(unsigned int),(unsigned char *)&choice)) {
+ if (CRYPTO_PSEUDO_RAND_INT(choice)) {
free((void *)route);
return NULL;
}
Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.67
retrieving revision 1.68
diff -u -d -r1.67 -r1.68
--- or.h 16 Apr 2003 17:44:33 -0000 1.67
+++ or.h 17 Apr 2003 17:10:41 -0000 1.68
@@ -198,12 +198,12 @@
unsigned char length; /* of payload if data cell, else value of sendme */
uint32_t seq; /* sequence number */
- /* The following 2 fields are only set when command is CELL_DATA */
- unsigned char topic_command;
- uint16_t topic_id;
-
unsigned char payload[CELL_PAYLOAD_SIZE];
} cell_t;
+#define CELL_TOPIC_COMMAND(c) (*(uint8_t*)((c).payload))
+#define SET_CELL_TOPIC_COMMAND(c,cmd) (*(uint8_t*)((c).payload) = (cmd))
+#define CELL_TOPIC_ID(c) ntohs(*(uint16_t*)((c).payload+2))
+#define SET_CELL_TOPIC_ID(c,id) (*(uint16_t*)((c).payload+2) = htons(id))
#define SOCKS4_REQUEST_GRANTED 90
#define SOCKS4_REQUEST_REJECT 91
More information about the tor-commits
mailing list