buffers.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.
  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. #ifdef MS_WINDOWS
  30. int e;
  31. #endif
  32. assert(buf && *buf && buflen && buf_datalen && reached_eof && (s>=0));
  33. /* this is the point where you would grow the buffer, if you want to */
  34. if(at_most > *buflen - *buf_datalen)
  35. at_most = *buflen - *buf_datalen; /* take the min of the two */
  36. if(at_most == 0)
  37. return 0; /* we shouldn't read anything */
  38. if(!options.LinkPadding && at_most > 10*sizeof(cell_t)) {
  39. /* if no linkpadding: do a rudimentary round-robin so one
  40. * connection can't hog a thickpipe
  41. */
  42. at_most = 10*(CELL_PAYLOAD_SIZE - RELAY_HEADER_SIZE);
  43. /* XXX this still isn't perfect. now we read 10 relay data payloads per read --
  44. * but if we're reading from a connection that speaks cells, we always
  45. * read a partial cell from the network and can't process it yet. Good
  46. * enough for now though. (And maybe best, to stress our code more.)
  47. */
  48. }
  49. // log_fn(LOG_DEBUG,"reading at most %d bytes.",at_most);
  50. read_result = read(s, *buf+*buf_datalen, at_most);
  51. if (read_result < 0) {
  52. if(!ERRNO_EAGAIN(errno)) { /* it's a real error */
  53. return -1;
  54. }
  55. #ifdef MS_WINDOWS
  56. e = correct_socket_errno(s);
  57. if(!ERRNO_EAGAIN(errno)) { /* no, it *is* a real error! */
  58. return -1;
  59. }
  60. #endif
  61. return 0;
  62. } else if (read_result == 0) {
  63. log_fn(LOG_DEBUG,"Encountered eof");
  64. *reached_eof = 1;
  65. return 0;
  66. } else { /* we read some bytes */
  67. *buf_datalen += read_result;
  68. // log_fn(LOG_DEBUG,"Read %d bytes. %d on inbuf.",read_result, *buf_datalen);
  69. return read_result;
  70. }
  71. }
  72. int flush_buf(int s, char **buf, int *buflen, int *buf_flushlen, int *buf_datalen) {
  73. /* push from buf onto s
  74. * then memmove to front of buf
  75. * return -1 or how many bytes remain to be flushed */
  76. int write_result;
  77. #ifdef MS_WINDOWS
  78. int e;
  79. #endif
  80. assert(buf && *buf && buflen && buf_flushlen && buf_datalen && (s>=0) && (*buf_flushlen <= *buf_datalen));
  81. if(*buf_flushlen == 0) /* nothing to flush */
  82. return 0;
  83. /* this is the point where you would grow the buffer, if you want to */
  84. write_result = write(s, *buf, *buf_flushlen);
  85. if (write_result < 0) {
  86. if(!ERRNO_EAGAIN(errno)) { /* it's a real error */
  87. return -1;
  88. }
  89. #ifdef MS_WINDOWS
  90. e = correct_socket_errno(s);
  91. if(!ERRNO_EAGAIN(errno)) { /* no, it *is* a real error! */
  92. return -1;
  93. }
  94. #endif
  95. log_fn(LOG_DEBUG,"write() would block, returning.");
  96. return 0;
  97. } else {
  98. *buf_datalen -= write_result;
  99. *buf_flushlen -= write_result;
  100. memmove(*buf, *buf+write_result, *buf_datalen);
  101. // log_fn(LOG_DEBUG,"flushed %d bytes, %d ready to flush, %d remain.",
  102. // write_result,*buf_flushlen,*buf_datalen);
  103. return *buf_flushlen;
  104. }
  105. }
  106. int write_to_buf(char *string, int string_len,
  107. char **buf, int *buflen, int *buf_datalen) {
  108. /* append string to buf (growing as needed, return -1 if "too big")
  109. * return total number of bytes on the buf
  110. */
  111. assert(string && buf && *buf && buflen && buf_datalen);
  112. /* this is the point where you would grow the buffer, if you want to */
  113. if (string_len + *buf_datalen > *buflen) { /* we're out of luck */
  114. log_fn(LOG_DEBUG, "buflen too small. Time to implement growing dynamic bufs.");
  115. return -1;
  116. }
  117. memcpy(*buf+*buf_datalen, string, string_len);
  118. *buf_datalen += string_len;
  119. // log_fn(LOG_DEBUG,"added %d bytes to buf (now %d total).",string_len, *buf_datalen);
  120. return *buf_datalen;
  121. }
  122. int fetch_from_buf(char *string, int string_len,
  123. char **buf, int *buflen, int *buf_datalen) {
  124. /* There must be string_len bytes in buf; write them onto string,
  125. * then memmove buf back (that is, remove them from buf).
  126. *
  127. * Return the number of bytes still on the buffer. */
  128. assert(string && buf && *buf && buflen && buf_datalen);
  129. assert(string_len <= *buf_datalen); /* make sure we don't ask for too much */
  130. memcpy(string,*buf,string_len);
  131. *buf_datalen -= string_len;
  132. memmove(*buf, *buf+string_len, *buf_datalen);
  133. return *buf_datalen;
  134. }
  135. int find_on_inbuf(char *string, int string_len,
  136. char *buf, int buf_datalen) {
  137. /* find first instance of needle 'string' on haystack 'buf'. return how
  138. * many bytes from the beginning of buf to the end of string.
  139. * If it's not there, return -1.
  140. */
  141. char *location;
  142. char *last_possible = buf + buf_datalen - string_len;
  143. assert(string && string_len > 0 && buf);
  144. if(buf_datalen < string_len)
  145. return -1;
  146. for(location = buf; location <= last_possible; location++)
  147. if((*location == *string) && !memcmp(location+1, string+1, string_len-1))
  148. return location-buf+string_len;
  149. return -1;
  150. }
  151. /*
  152. Local Variables:
  153. mode:c
  154. indent-tabs-mode:nil
  155. c-basic-offset:2
  156. End:
  157. */