buffers.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /* buffers.c */
  5. #include "or.h"
  6. extern or_options_t options; /* command-line and config-file options */
  7. int buf_new(char **buf, size_t *buflen, size_t *buf_datalen) {
  8. assert(buf && buflen && buf_datalen);
  9. *buf = (char *)malloc(MAX_BUF_SIZE);
  10. if(!*buf)
  11. return -1;
  12. memset(*buf,0,MAX_BUF_SIZE);
  13. *buflen = MAX_BUF_SIZE;
  14. *buf_datalen = 0;
  15. return 0;
  16. }
  17. void buf_free(char *buf) {
  18. free(buf);
  19. }
  20. int read_to_buf(int s, int at_most, char **buf, size_t *buflen, size_t *buf_datalen, int *reached_eof) {
  21. /* read from socket s, writing onto buf+buf_datalen. If at_most is >= 0 then
  22. * read at most 'at_most' bytes, and in any case don't read more than will fit based on buflen.
  23. * If read() returns 0, set *reached_eof to 1 and return 0. If you want to tear
  24. * down the connection return -1, else return the number of bytes read.
  25. */
  26. int read_result;
  27. assert(buf && *buf && buflen && buf_datalen && reached_eof && (s>=0));
  28. /* this is the point where you would grow the buffer, if you want to */
  29. if(at_most < 0 || *buflen - *buf_datalen < at_most)
  30. at_most = *buflen - *buf_datalen; /* take the min of the two */
  31. /* (note that this only modifies at_most inside this function) */
  32. if(at_most == 0)
  33. return 0; /* we shouldn't read anything */
  34. if(!options.LinkPadding && at_most > 10*sizeof(cell_t)) {
  35. /* if no linkpadding. do a rudimentary round-robin so one
  36. * connection can't hog our receiver bucket
  37. */
  38. at_most = 10*sizeof(cell_t);
  39. }
  40. // log(LOG_DEBUG,"read_to_buf(): reading at most %d bytes.",at_most);
  41. read_result = read(s, *buf+*buf_datalen, at_most);
  42. if (read_result < 0) {
  43. if(errno!=EAGAIN) { /* it's a real error */
  44. return -1;
  45. }
  46. return 0;
  47. } else if (read_result == 0) {
  48. log(LOG_DEBUG,"read_to_buf(): Encountered eof");
  49. *reached_eof = 1;
  50. return 0;
  51. } else { /* we read some bytes */
  52. *buf_datalen += read_result;
  53. // log(LOG_DEBUG,"read_to_buf(): Read %d bytes. %d on inbuf.",read_result, *buf_datalen);
  54. return read_result;
  55. }
  56. }
  57. int flush_buf(int s, char **buf, size_t *buflen, size_t *buf_flushlen, size_t *buf_datalen) {
  58. /* push from buf onto s
  59. * then memmove to front of buf
  60. * return -1 or how many bytes remain to be flushed */
  61. int write_result;
  62. assert(buf && *buf && buflen && buf_flushlen && buf_datalen && (s>=0) && (*buf_flushlen <= *buf_datalen));
  63. if(*buf_flushlen == 0) /* nothing to flush */
  64. return 0;
  65. /* this is the point where you would grow the buffer, if you want to */
  66. write_result = write(s, *buf, *buf_flushlen);
  67. if (write_result < 0) {
  68. if(errno!=EAGAIN) { /* it's a real error */
  69. return -1;
  70. }
  71. log(LOG_DEBUG,"flush_buf(): write() would block, returning.");
  72. return 0;
  73. } else {
  74. *buf_datalen -= write_result;
  75. *buf_flushlen -= write_result;
  76. memmove(*buf, *buf+write_result, *buf_datalen);
  77. // log(LOG_DEBUG,"flush_buf(): flushed %d bytes, %d ready to flush, %d remain.",
  78. // write_result,*buf_flushlen,*buf_datalen);
  79. return *buf_flushlen;
  80. }
  81. }
  82. int write_to_buf(char *string, size_t string_len,
  83. char **buf, size_t *buflen, size_t *buf_datalen) {
  84. /* append string to buf (growing as needed, return -1 if "too big")
  85. * return total number of bytes on the buf
  86. */
  87. assert(string && buf && *buf && buflen && buf_datalen);
  88. /* this is the point where you would grow the buffer, if you want to */
  89. if (string_len + *buf_datalen > *buflen) { /* we're out of luck */
  90. log(LOG_DEBUG, "write_to_buf(): buflen too small. Time to implement growing dynamic bufs.");
  91. return -1;
  92. }
  93. memcpy(*buf+*buf_datalen, string, string_len);
  94. *buf_datalen += string_len;
  95. // log(LOG_DEBUG,"write_to_buf(): added %d bytes to buf (now %d total).",string_len, *buf_datalen);
  96. return *buf_datalen;
  97. }
  98. int fetch_from_buf(char *string, size_t string_len,
  99. char **buf, size_t *buflen, size_t *buf_datalen) {
  100. /* if there is string_len bytes in buf, write them onto string,
  101. * then memmove buf back (that is, remove them from buf) */
  102. assert(string && buf && *buf && buflen && buf_datalen);
  103. /* this is the point where you would grow the buffer, if you want to */
  104. if(string_len > *buf_datalen) /* we want too much. sorry. */
  105. return -1;
  106. memcpy(string,*buf,string_len);
  107. *buf_datalen -= string_len;
  108. memmove(*buf, *buf+string_len, *buf_datalen);
  109. return *buf_datalen;
  110. }