cell now has a network appearance and an internal (struct) appearance

svn:r130
This commit is contained in:
Roger Dingledine 2002-10-02 20:12:44 +00:00
parent e3fd91755d
commit 7032d16e78
2 changed files with 33 additions and 19 deletions

View File

@ -561,16 +561,25 @@ int connection_send_connected(aci_t aci, connection_t *conn) {
} }
int connection_write_cell_to_buf(cell_t *cellp, connection_t *conn) { int connection_write_cell_to_buf(cell_t *cellp, connection_t *conn) {
char networkcell[CELL_NETWORK_SIZE];
char *n = networkcell;
if(connection_encrypt_cell(cellp,conn)<0) { memset(n,0,CELL_NETWORK_SIZE); /* zero it out to start */
*(aci_t *)n = htons(cellp->aci);
*(n+2) = cellp->command;
*(n+3) = cellp->length;
/* seq is reserved, leave zero */
memcpy(n+8,cellp->payload,CELL_PAYLOAD_SIZE);
if(connection_encrypt_cell(n,conn)<0) {
return -1; return -1;
} }
return connection_write_to_buf((char *)cellp, sizeof(cell_t), conn); return connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
} }
int connection_encrypt_cell(cell_t *cellp, connection_t *conn) { int connection_encrypt_cell(char *cellp, connection_t *conn) {
cell_t newcell; char cryptcell[CELL_NETWORK_SIZE];
#if 0 #if 0
int x; int x;
char *px; char *px;
@ -583,7 +592,7 @@ int connection_encrypt_cell(cell_t *cellp, connection_t *conn) {
printf("\n"); printf("\n");
#endif #endif
if(crypto_cipher_encrypt(conn->f_crypto, (char *)cellp, sizeof(cell_t), (char *)&newcell)) { if(crypto_cipher_encrypt(conn->f_crypto, cellp, CELL_NETWORK_SIZE, cryptcell)) {
log(LOG_ERR,"Could not encrypt cell for connection %s:%u.",conn->address,conn->port); log(LOG_ERR,"Could not encrypt cell for connection %s:%u.",conn->address,conn->port);
return -1; return -1;
} }
@ -596,7 +605,7 @@ int connection_encrypt_cell(cell_t *cellp, connection_t *conn) {
printf("\n"); printf("\n");
#endif #endif
memcpy(cellp,&newcell,sizeof(cell_t)); memcpy(cellp,cryptcell,CELL_NETWORK_SIZE);
return 0; return 0;
} }
@ -750,15 +759,15 @@ int connection_process_cell_from_inbuf(connection_t *conn) {
/* check if there's a whole cell there. /* check if there's a whole cell there.
* if yes, pull it off, decrypt it, and process it. * if yes, pull it off, decrypt it, and process it.
*/ */
char crypted[128]; char crypted[CELL_NETWORK_SIZE];
char outbuf[1024]; char outbuf[1024];
// int x; // int x;
cell_t *cellp; cell_t cell;
if(conn->inbuf_datalen < 128) /* entire response available? */ if(conn->inbuf_datalen < CELL_NETWORK_SIZE) /* entire response available? */
return 0; /* not yet */ return 0; /* not yet */
if(connection_fetch_from_buf(crypted,128,conn) < 0) { if(connection_fetch_from_buf(crypted,CELL_NETWORK_SIZE,conn) < 0) {
return -1; return -1;
} }
@ -770,7 +779,7 @@ int connection_process_cell_from_inbuf(connection_t *conn) {
printf("\n"); printf("\n");
#endif #endif
/* decrypt */ /* decrypt */
if(crypto_cipher_decrypt(conn->b_crypto,crypted,sizeof(cell_t),(unsigned char *)outbuf)) { if(crypto_cipher_decrypt(conn->b_crypto,crypted,CELL_NETWORK_SIZE,outbuf)) {
log(LOG_ERR,"connection_process_cell_from_inbuf(): Decryption failed, dropping."); log(LOG_ERR,"connection_process_cell_from_inbuf(): Decryption failed, dropping.");
return connection_process_inbuf(conn); /* process the remainder of the buffer */ return connection_process_inbuf(conn); /* process the remainder of the buffer */
} }
@ -783,11 +792,15 @@ int connection_process_cell_from_inbuf(connection_t *conn) {
printf("\n"); printf("\n");
#endif #endif
/* copy the rest of the cell */ /* retrieve cell info from outbuf (create the struct from the string) */
// memcpy((char *)outbuf+8, (char *)crypted+8, sizeof(cell_t)-8); memset(&cell,0,sizeof(cell_t)); /* zero it out to start */
cellp = (cell_t *)outbuf; cell.aci = ntohs(*(aci_t *)outbuf);
cell.command = *(outbuf+2);
cell.length = *(outbuf+3);
memcpy(cell.payload, outbuf+8, CELL_PAYLOAD_SIZE);
// log(LOG_DEBUG,"connection_process_cell_from_inbuf(): Decrypted cell is of type %u (ACI %u).",cellp->command,cellp->aci); // log(LOG_DEBUG,"connection_process_cell_from_inbuf(): Decrypted cell is of type %u (ACI %u).",cellp->command,cellp->aci);
command_process_cell(cellp, conn); command_process_cell(&cell, conn);
return connection_process_inbuf(conn); /* process the remainder of the buffer */ return connection_process_inbuf(conn); /* process the remainder of the buffer */
} }

View File

@ -145,6 +145,7 @@
#define CELL_CONNECTED 7 #define CELL_CONNECTED 7
#define CELL_PAYLOAD_SIZE 120 #define CELL_PAYLOAD_SIZE 120
#define CELL_NETWORK_SIZE 128
typedef uint16_t aci_t; typedef uint16_t aci_t;
@ -334,13 +335,13 @@ typedef struct
int forwf:4; int forwf:4;
uint16_t port; uint16_t port;
uint32_t addr; uint32_t addr;
time_t expire; uint32_t expire;
unsigned char keyseed[16]; unsigned char keyseed[16];
} onion_layer_t; } onion_layer_t;
typedef struct typedef struct
{ {
time_t expire; uint32_t expire;
char digest[20]; /* SHA digest of the onion */ char digest[20]; /* SHA digest of the onion */
void *prev; void *prev;
void *next; void *next;
@ -495,7 +496,7 @@ int connection_state_is_open(connection_t *conn);
int connection_send_destroy(aci_t aci, connection_t *conn); int connection_send_destroy(aci_t aci, connection_t *conn);
int connection_send_connected(aci_t aci, connection_t *conn); int connection_send_connected(aci_t aci, connection_t *conn);
int connection_encrypt_cell(cell_t *cellp, connection_t *conn); int connection_encrypt_cell(char *cellp, connection_t *conn);
int connection_write_cell_to_buf(cell_t *cellp, connection_t *conn); int connection_write_cell_to_buf(cell_t *cellp, connection_t *conn);
int connection_process_inbuf(connection_t *conn); int connection_process_inbuf(connection_t *conn);