buffers.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. /* Create a new buf of size MAX_BUF_SIZE. Write a pointer to it
  8. * into *buf, write MAX_BUF_SIZE into *buflen, and initialize
  9. * *buf_datalen to 0. Return 0.
  10. */
  11. int buf_new(char **buf, int *buflen, int *buf_datalen) {
  12. assert(buf && buflen && buf_datalen);
  13. *buf = (char *)tor_malloc(MAX_BUF_SIZE);
  14. // memset(*buf,0,MAX_BUF_SIZE);
  15. *buflen = MAX_BUF_SIZE;
  16. *buf_datalen = 0;
  17. return 0;
  18. }
  19. void buf_free(char *buf) {
  20. free(buf);
  21. }
  22. /* read from socket s, writing onto buf+buf_datalen. If at_most is >= 0 then
  23. * read at most 'at_most' bytes, and in any case don't read more than will fit based on buflen.
  24. * If read() returns 0, set *reached_eof to 1 and return 0. If you want to tear
  25. * down the connection return -1, else return the number of bytes read.
  26. */
  27. int read_to_buf(int s, int at_most, char **buf, int *buflen, int *buf_datalen, int *reached_eof) {
  28. int read_result;
  29. assert(buf && *buf && buflen && buf_datalen && reached_eof && (s>=0));
  30. /* this is the point where you would grow the buffer, if you want to */
  31. if(at_most < 0 || *buflen - *buf_datalen < at_most)
  32. at_most = *buflen - *buf_datalen; /* take the min of the two */
  33. /* (note that this only modifies at_most inside this function) */
  34. if(at_most == 0)
  35. return 0; /* we shouldn't read anything */
  36. if(!options.LinkPadding && at_most > 10*sizeof(cell_t)) {
  37. /* if no linkpadding: do a rudimentary round-robin so one
  38. * connection can't hog a thickpipe
  39. */
  40. at_most = 10*(CELL_PAYLOAD_SIZE - RELAY_HEADER_SIZE);
  41. /* XXX this still isn't perfect. now we read 10 relay data payloads per read --
  42. * but if we're reading from a connection that speaks cells, we always
  43. * read a partial cell from the network and can't process it yet. Good
  44. * enough for now though. (And maybe best, to stress our code more.)
  45. */
  46. }
  47. // log(LOG_DEBUG,"read_to_buf(): reading at most %d bytes.",at_most);
  48. read_result = read(s, *buf+*buf_datalen, at_most);
  49. if (read_result < 0) {
  50. if(errno!=EAGAIN) { /* it's a real error */
  51. return -1;
  52. }
  53. return 0;
  54. } else if (read_result == 0) {
  55. log(LOG_DEBUG,"read_to_buf(): Encountered eof");
  56. *reached_eof = 1;
  57. return 0;
  58. } else { /* we read some bytes */
  59. *buf_datalen += read_result;
  60. // log(LOG_DEBUG,"read_to_buf(): Read %d bytes. %d on inbuf.",read_result, *buf_datalen);
  61. return read_result;
  62. }
  63. }
  64. int flush_buf(int s, char **buf, int *buflen, int *buf_flushlen, int *buf_datalen) {
  65. /* push from buf onto s
  66. * then memmove to front of buf
  67. * return -1 or how many bytes remain to be flushed */
  68. int write_result;
  69. assert(buf && *buf && buflen && buf_flushlen && buf_datalen && (s>=0) && (*buf_flushlen <= *buf_datalen));
  70. if(*buf_flushlen == 0) /* nothing to flush */
  71. return 0;
  72. /* this is the point where you would grow the buffer, if you want to */
  73. write_result = write(s, *buf, *buf_flushlen);
  74. if (write_result < 0) {
  75. if(errno!=EAGAIN) { /* it's a real error */
  76. return -1;
  77. }
  78. log(LOG_DEBUG,"flush_buf(): write() would block, returning.");
  79. return 0;
  80. } else {
  81. *buf_datalen -= write_result;
  82. *buf_flushlen -= write_result;
  83. memmove(*buf, *buf+write_result, *buf_datalen);
  84. // log(LOG_DEBUG,"flush_buf(): flushed %d bytes, %d ready to flush, %d remain.",
  85. // write_result,*buf_flushlen,*buf_datalen);
  86. return *buf_flushlen;
  87. }
  88. }
  89. int write_to_buf(char *string, int string_len,
  90. char **buf, int *buflen, int *buf_datalen) {
  91. /* append string to buf (growing as needed, return -1 if "too big")
  92. * return total number of bytes on the buf
  93. */
  94. assert(string && buf && *buf && buflen && buf_datalen);
  95. /* this is the point where you would grow the buffer, if you want to */
  96. if (string_len + *buf_datalen > *buflen) { /* we're out of luck */
  97. log(LOG_DEBUG, "write_to_buf(): buflen too small. Time to implement growing dynamic bufs.");
  98. return -1;
  99. }
  100. memcpy(*buf+*buf_datalen, string, string_len);
  101. *buf_datalen += string_len;
  102. // log(LOG_DEBUG,"write_to_buf(): added %d bytes to buf (now %d total).",string_len, *buf_datalen);
  103. return *buf_datalen;
  104. }
  105. int fetch_from_buf(char *string, int string_len,
  106. char **buf, int *buflen, int *buf_datalen) {
  107. /* if there are string_len bytes in buf, write them onto string,
  108. * then memmove buf back (that is, remove them from buf).
  109. *
  110. * If there are not enough bytes on the buffer to fill string, return -1.
  111. *
  112. * Return the number of bytes still on the buffer. */
  113. assert(string && buf && *buf && buflen && buf_datalen);
  114. /* this is the point where you would grow the buffer, if you want to */
  115. if(string_len > *buf_datalen) /* we want too much. sorry. */
  116. return -1;
  117. memcpy(string,*buf,string_len);
  118. *buf_datalen -= string_len;
  119. memmove(*buf, *buf+string_len, *buf_datalen);
  120. return *buf_datalen;
  121. }
  122. int find_on_inbuf(char *string, int string_len,
  123. char *buf, int buf_datalen) {
  124. /* find first instance of needle 'string' on haystack 'buf'. return how
  125. * many bytes from the beginning of buf to the end of string.
  126. * If it's not there, return -1.
  127. */
  128. char *location;
  129. char *last_possible = buf + buf_datalen - string_len;
  130. assert(string && string_len > 0 && buf);
  131. if(buf_datalen < string_len)
  132. return -1;
  133. for(location = buf; location <= last_possible; location++)
  134. if((*location == *string) && !memcmp(location+1, string+1, string_len-1))
  135. return location-buf+string_len;
  136. return -1;
  137. }
  138. /*
  139. Local Variables:
  140. mode:c
  141. indent-tabs-mode:nil
  142. c-basic-offset:2
  143. End:
  144. */