Choose correct abstraction for topic_foo. Abstract random-integer code

svn:r249
This commit is contained in:
Nick Mathewson 2003-04-17 17:10:41 +00:00
parent fdf4d4aa85
commit 32a3e704b4
8 changed files with 30 additions and 39 deletions

View File

@ -228,8 +228,8 @@ which reveals the downstream node.
ACI (anonymous circuit identifier) [2 bytes]
Command [1 byte]
Length [1 byte]
Sequence number (unused, set to 0) [4 bytes]
Length [1 byte]
Payload (padded with 0 bytes) [248 bytes]
[Total size: 256 bytes]

View File

@ -85,6 +85,8 @@ int crypto_SHA_digest(unsigned char *m, int len, unsigned char *digest);
int crypto_rand(unsigned int n, unsigned char *to);
int crypto_pseudo_rand(unsigned int n, unsigned char *to);
#define CRYPTO_PSEUDO_RAND_INT(v) crypto_pseudo_rand(sizeof(v),(char*)&(v))
/* errors */
char *crypto_perror();
#endif

View File

@ -118,7 +118,8 @@ aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type) {
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);

View File

@ -698,8 +698,8 @@ repeat_connection_package_raw_inbuf:
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 @@ int connection_consider_sending_sendme(connection_t *conn, int edge_type) {
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 @@ cell_pack(char *dest, const cell_t *src)
*(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 @@ cell_unpack(cell_t *dest, const char *src)
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);
}
/*

View File

@ -119,15 +119,18 @@ int ap_handshake_process_socks(connection_t *conn) {
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;

View File

@ -27,8 +27,8 @@ int connection_edge_process_inbuf(connection_t *conn) {
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 @@ int connection_edge_send_command(connection_t *conn, circuit_t *circ, int topic_
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 @@ int connection_edge_process_data_cell(cell_t *cell, circuit_t *circ, int edge_ty
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);

View File

@ -311,15 +311,14 @@ int chooselen(double cw)
{
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 @@ unsigned int *new_route(double cw, routerinfo_t **rarray, int rarray_len, int *r
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;
}

View File

@ -198,12 +198,12 @@ typedef struct {
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