cell.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. static cell_t *new_create_cell(uint16_t aci, unsigned char length, unsigned char *buf)
  6. {
  7. cell_t *c = NULL;
  8. int retval;
  9. if ((aci) && (buf) && (length <= CELL_PAYLOAD_SIZE)) /* valid parameters */
  10. {
  11. c = (cell_t *)malloc(sizeof(cell_t));
  12. if (!c) /* malloc() error */
  13. return NULL;
  14. c->command = CELL_CREATE;
  15. c->aci = aci;
  16. c->length = length;
  17. c->seq = 0;
  18. memcpy((void *)c->payload, (void *)buf, length);
  19. retval = crypto_pseudo_rand(CELL_PAYLOAD_SIZE-length, (unsigned char *)(c->payload+length));
  20. if (retval) /* RAND_pseudo_bytes() error */
  21. {
  22. free((void *)c);
  23. return NULL;
  24. } /* RAND_pseudo_bytes() error */
  25. return c;
  26. } /* valid parameters */
  27. else /* invalid parameters */
  28. return NULL;
  29. }
  30. int pack_create(uint16_t aci, unsigned char *onion, uint32_t onionlen, unsigned char **cellbuf, unsigned int *cellbuflen)
  31. {
  32. cell_t *c;
  33. unsigned char *buf;
  34. unsigned int buflen;
  35. unsigned int cells;
  36. unsigned int dataleft;
  37. unsigned int i;
  38. assert(aci && onion && onionlen && cellbuf && cellbuflen);
  39. /* copy the onion into a buffer, prepend with onion length */
  40. buflen = onionlen+4;
  41. buf = (unsigned char *)malloc(buflen);
  42. if (!buf) /* malloc() error */
  43. return -1;
  44. log(LOG_DEBUG,"pack_create() : Setting onion length to %u.",onionlen);
  45. *(uint32_t*)buf = htonl(onionlen);
  46. memcpy((void *)(buf+4),(void *)onion,onionlen);
  47. /* calculate number of cells required */
  48. if (buflen%CELL_PAYLOAD_SIZE == 0)
  49. cells = buflen/CELL_PAYLOAD_SIZE;
  50. else
  51. cells = buflen/CELL_PAYLOAD_SIZE+1;
  52. /* allocate memory for the cells */
  53. *cellbuflen = cells * sizeof(cell_t);
  54. *cellbuf = malloc(*cellbuflen);
  55. if (!*cellbuf) /* malloc() error */
  56. return -1;
  57. log(LOG_DEBUG,"pack_create() : Allocated memory for %u cells.",cells);
  58. /* create cells one by one */
  59. dataleft = buflen;
  60. for(i=0; i<cells; i++)
  61. {
  62. log(LOG_DEBUG,"pack_create() : Packing %u bytes of data.",dataleft);
  63. if (dataleft >= CELL_PAYLOAD_SIZE)
  64. {
  65. c = new_create_cell(aci,CELL_PAYLOAD_SIZE,buf+i*CELL_PAYLOAD_SIZE);
  66. dataleft -= CELL_PAYLOAD_SIZE;
  67. }
  68. else
  69. c = new_create_cell(aci,dataleft,buf+i*CELL_PAYLOAD_SIZE);
  70. if (!c) /* cell creation failed */
  71. {
  72. free((void *)*cellbuf);
  73. return -1;
  74. } /* cell creation failed */
  75. log(LOG_DEBUG,"pack_create() : new_create_cell succeeded; copying the cell into output buffer");
  76. /* cell has been created, now copy into buffer */
  77. memcpy((void *)(*cellbuf+i*sizeof(cell_t)),(void *)c,sizeof(cell_t));
  78. free((void *)c);
  79. }
  80. free(buf);
  81. return 0;
  82. }