[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[or-cvs] Folded cell.? into src/or



Update of /home/or/cvsroot/src/or
In directory moria.seul.org:/home/arma/work/onion/cvs/src/or

Modified Files:
	cell.c command.c connection.c or.h 
Log Message:
Folded cell.? into src/or



Index: cell.c
===================================================================
RCS file: /home/or/cvsroot/src/or/cell.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- cell.c	16 Jul 2002 01:12:15 -0000	1.3
+++ cell.c	19 Jul 2002 18:48:28 -0000	1.4
@@ -4,24 +4,97 @@
 
 #include "or.h"
 
-int check_sane_cell(cell_t *cell) {
+static cell_t *new_create_cell(uint16_t aci, unsigned char length, unsigned char *buf)
+{
+  cell_t *c = NULL;
+  int retval;
 
-  assert(cell);
+  if ((aci) && (buf) && (length <= CELL_PAYLOAD_SIZE)) /* valid parameters */
+  {
+    c = (cell_t *)malloc(sizeof(cell_t));
+    if (!c) /* malloc() error */
+      return NULL;
+    
+    c->command = CELL_CREATE;
+    c->aci = aci;
+    c->length = length;
+    c->seq = 0;
+    
+    memcpy((void *)c->payload, (void *)buf, length);
+    retval = RAND_pseudo_bytes((unsigned char *)(c->payload+length),CELL_PAYLOAD_SIZE-length);
+    if (retval == -1) /* RAND_pseudo_bytes() error */
+    {
+      free((void *)c);
+      return NULL;
+    } /* RAND_pseudo_bytes() error */
+    
+    return c;
+  } /* valid parameters */
+  else /* invalid parameters */
+    return NULL;
+}
 
-#if 0 /* actually, the aci is 0 for padding cells */
-  if(cell->aci == 0) {
-    log(LOG_DEBUG,"check_sane_cell(): Cell has aci=0. Dropping.");
-    return -1;
-  }
-#endif
+int pack_create(uint16_t aci, unsigned char *onion, uint32_t onionlen, unsigned char **cellbuf, unsigned int *cellbuflen)
+{
+  cell_t *c;
+  unsigned char *buf;
+  unsigned int buflen;
+  unsigned int cells;
+  unsigned int dataleft;
+  unsigned int i;
+  
+  assert(aci && onion && onionlen && cellbuf && cellbuflen);
 
-#if 0 /* actually, the length is sometimes encrypted. so it's ok. */
-  if(cell->length > 120) {
-    log(LOG_DEBUG,"check_sane_cell(): Cell claims to have payload length %d. Dropping.",cell->length);
+  /* copy the onion into a buffer, prepend with onion length */
+  buflen = onionlen+4;
+  buf = (unsigned char *)malloc(buflen);
+  if (!buf) /* malloc() error */
     return -1;
+  
+  log(LOG_DEBUG,"pack_create() : Setting onion length to %u.",onionlen);
+  *(uint32_t*)buf = htonl(onionlen);
+  memcpy((void *)(buf+4),(void *)onion,onionlen);
+  
+  /* calculate number of cells required */
+  if (buflen%CELL_PAYLOAD_SIZE == 0)
+    cells = buflen/CELL_PAYLOAD_SIZE;
+  else
+    cells = buflen/CELL_PAYLOAD_SIZE+1;
+  
+  /* allocate memory for the cells */
+  *cellbuflen = cells * sizeof(cell_t);
+  *cellbuf = malloc(*cellbuflen);
+  if (!*cellbuf) /* malloc() error */
+    return -1;
+    
+  log(LOG_DEBUG,"pack_create() : Allocated memory for %u cells.",cells);
+  
+  /* create cells one by one */
+  dataleft = buflen;
+  for(i=0; i<cells; i++)
+  {
+    log(LOG_DEBUG,"pack_create() : Packing %u bytes of data.",dataleft);
+    if (dataleft >= CELL_PAYLOAD_SIZE)
+    {
+    c = new_create_cell(aci,CELL_PAYLOAD_SIZE,buf+i*CELL_PAYLOAD_SIZE);
+    dataleft -= CELL_PAYLOAD_SIZE;
+    }
+    else
+    c = new_create_cell(aci,dataleft,buf+i*CELL_PAYLOAD_SIZE);
+      
+    if (!c) /* cell creation failed */
+    {
+      free((void *)*cellbuf);
+      return -1;
+    } /* cell creation failed */
+      
+    log(LOG_DEBUG,"pack_create() : new_create_cell succeeded; copying the cell into output buffer");
+    /* cell has been created, now copy into buffer */
+    memcpy((void *)(*cellbuf+i*sizeof(cell_t)),(void *)c,sizeof(cell_t));
+    free((void *)c);
   }
-#endif
-
-  return 0; /* looks good */
+  
+  free(buf);
+  return 0;
 }
 

Index: command.c
===================================================================
RCS file: /home/or/cvsroot/src/or/command.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- command.c	18 Jul 2002 23:44:57 -0000	1.8
+++ command.c	19 Jul 2002 18:48:28 -0000	1.9
@@ -6,9 +6,6 @@
 
 void command_process_cell(cell_t *cell, connection_t *conn) {
 
-  if(check_sane_cell(cell) < 0)
-    return;
-
   switch(cell->command) {
     case CELL_PADDING:
       /* do nothing */

Index: connection.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- connection.c	18 Jul 2002 06:37:58 -0000	1.9
+++ connection.c	19 Jul 2002 18:48:28 -0000	1.10
@@ -621,14 +621,14 @@
 
   if(circ->n_conn == conn) { /* we're at an exit */
     if(circ->p_receive_window < RECEIVE_WINDOW_START-RECEIVE_WINDOW_INCREMENT) {
-      log(LOG_DEBUG,"connection_consider_sending_sendme(): Queueing sendme back.");
+      log(LOG_DEBUG,"connection_consider_sending_sendme(): Outbuf %d, Queueing sendme back.", conn->outbuf_flushlen);
       circ->p_receive_window += RECEIVE_WINDOW_INCREMENT;
       sendme.aci = circ->p_aci;
       return connection_write_cell_to_buf(&sendme, circ->p_conn); /* (clobbers sendme) */
     }
   } else { /* we're at an AP */
     if(circ->n_receive_window < RECEIVE_WINDOW_START-RECEIVE_WINDOW_INCREMENT) {
-      log(LOG_DEBUG,"connection_consider_sending_sendme(): Queueing sendme forward.");
+      log(LOG_DEBUG,"connection_consider_sending_sendme(): Outbuf %d, Queueing sendme forward.", conn->outbuf_flushlen);
       circ->n_receive_window += RECEIVE_WINDOW_INCREMENT;
       sendme.aci = circ->n_aci;
       return connection_write_cell_to_buf(&sendme, circ->n_conn); /* (clobbers sendme) */

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- or.h	18 Jul 2002 23:44:57 -0000	1.12
+++ or.h	19 Jul 2002 18:48:28 -0000	1.13
@@ -29,7 +29,6 @@
 #include <openssl/evp.h>
 #include <openssl/rand.h>
 
-#include "../common/cell.h"
 #include "../common/config.h"
 #include "../common/key.h"
 #include "../common/log.h"
@@ -115,8 +114,29 @@
 #define RECEIVE_WINDOW_START 100
 #define RECEIVE_WINDOW_INCREMENT 10
 
+/* cell commands */
+#define CELL_PADDING 0
+#define CELL_CREATE 1
+#define CELL_DATA 2
+#define CELL_DESTROY 3
+#define CELL_ACK 4
+#define CELL_NACK 5
+#define CELL_SENDME 6
+
+#define CELL_PAYLOAD_SIZE 120
+
 typedef uint16_t aci_t;
 
+/* cell definition */
+typedef struct
+{ 
+  aci_t aci; /* Anonymous Connection Identifier */
+  unsigned char command;
+  unsigned char length; /* of payload if data cell, else value of sendme */
+  uint32_t seq; /* sequence number */
+  unsigned char payload[120];
+} cell_t;
+
 typedef struct
 { 
 
@@ -342,7 +362,7 @@
 
 /********************************* cell.c ***************************/
 
-int check_sane_cell(cell_t *cell);
+int pack_create(uint16_t aci, unsigned char *onion, uint32_t onionlen, unsigned char **cellbuf, unsigned int *cellbuflen);
 
 /********************************* circuit.c ***************************/