buffers.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. // log_fn(LOG_DEBUG,"reading at most %d bytes.",at_most);
  39. read_result = read(s, *buf+*buf_datalen, at_most);
  40. if (read_result < 0) {
  41. if(!ERRNO_EAGAIN(errno)) { /* it's a real error */
  42. return -1;
  43. }
  44. #ifdef MS_WINDOWS
  45. e = correct_socket_errno(s);
  46. if(!ERRNO_EAGAIN(e)) { /* no, it *is* a real error! */
  47. return -1;
  48. }
  49. #endif
  50. return 0;
  51. } else if (read_result == 0) {
  52. log_fn(LOG_DEBUG,"Encountered eof");
  53. *reached_eof = 1;
  54. return 0;
  55. } else { /* we read some bytes */
  56. *buf_datalen += read_result;
  57. // log_fn(LOG_DEBUG,"Read %d bytes. %d on inbuf.",read_result, *buf_datalen);
  58. return read_result;
  59. }
  60. }
  61. int read_to_buf_tls(tor_tls *tls, int at_most, char **buf, int *buflen, int *buf_datalen) {
  62. int r;
  63. assert(tls && *buf && buflen && buf_datalen);
  64. if (at_most > *buflen - *buf_datalen)
  65. at_most = *buflen - *buf_datalen;
  66. if (at_most == 0)
  67. return 0;
  68. r = tor_tls_read(tls, *buf+*buf_datalen, at_most);
  69. if (r<0)
  70. return r;
  71. *buf_datalen += r;
  72. return r;
  73. }
  74. int flush_buf(int s, char **buf, int *buflen, int *buf_flushlen, int *buf_datalen) {
  75. /* push from buf onto s
  76. * then memmove to front of buf
  77. * return -1 or how many bytes remain to be flushed */
  78. int write_result;
  79. #ifdef MS_WINDOWS
  80. int e;
  81. #endif
  82. assert(buf && *buf && buflen && buf_flushlen && buf_datalen && (s>=0) && (*buf_flushlen <= *buf_datalen));
  83. if(*buf_flushlen == 0) /* nothing to flush */
  84. return 0;
  85. /* this is the point where you would grow the buffer, if you want to */
  86. write_result = write(s, *buf, *buf_flushlen);
  87. if (write_result < 0) {
  88. if(!ERRNO_EAGAIN(errno)) { /* it's a real error */
  89. return -1;
  90. }
  91. #ifdef MS_WINDOWS
  92. e = correct_socket_errno(s);
  93. if(!ERRNO_EAGAIN(e)) { /* no, it *is* a real error! */
  94. return -1;
  95. }
  96. #endif
  97. log_fn(LOG_DEBUG,"write() would block, returning.");
  98. return 0;
  99. } else {
  100. *buf_datalen -= write_result;
  101. *buf_flushlen -= write_result;
  102. memmove(*buf, *buf+write_result, *buf_datalen);
  103. // log_fn(LOG_DEBUG,"flushed %d bytes, %d ready to flush, %d remain.",
  104. // write_result,*buf_flushlen,*buf_datalen);
  105. return *buf_flushlen;
  106. /* XXX USE_TLS should change to return write_result like any sane function would */
  107. }
  108. }
  109. int flush_buf_tls(tor_tls *tls, char **buf, int *buflen, int *buf_flushlen, int *buf_datalen)
  110. {
  111. int r;
  112. assert(tls && *buf && buflen && buf_datalen);
  113. /* we want to let tls write even if flushlen is zero, because it might
  114. * have a partial record pending */
  115. r = tor_tls_write(tls, *buf, *buf_flushlen);
  116. if (r < 0) {
  117. return r;
  118. }
  119. *buf_datalen -= r;
  120. *buf_flushlen -= r;
  121. memmove(*buf, *buf+r, *buf_datalen);
  122. return r;
  123. }
  124. int write_to_buf(char *string, int string_len,
  125. char **buf, int *buflen, int *buf_datalen) {
  126. /* append string to buf (growing as needed, return -1 if "too big")
  127. * return total number of bytes on the buf
  128. */
  129. assert(string && buf && *buf && buflen && buf_datalen);
  130. /* this is the point where you would grow the buffer, if you want to */
  131. if (string_len + *buf_datalen > *buflen) { /* we're out of luck */
  132. log_fn(LOG_DEBUG, "buflen too small. Time to implement growing dynamic bufs.");
  133. return -1;
  134. }
  135. memcpy(*buf+*buf_datalen, string, string_len);
  136. *buf_datalen += string_len;
  137. // log_fn(LOG_DEBUG,"added %d bytes to buf (now %d total).",string_len, *buf_datalen);
  138. return *buf_datalen;
  139. }
  140. int fetch_from_buf(char *string, int string_len,
  141. char **buf, int *buflen, int *buf_datalen) {
  142. /* There must be string_len bytes in buf; write them onto string,
  143. * then memmove buf back (that is, remove them from buf).
  144. *
  145. * Return the number of bytes still on the buffer. */
  146. assert(string && buf && *buf && buflen && buf_datalen);
  147. assert(string_len <= *buf_datalen); /* make sure we don't ask for too much */
  148. memcpy(string,*buf,string_len);
  149. *buf_datalen -= string_len;
  150. memmove(*buf, *buf+string_len, *buf_datalen);
  151. return *buf_datalen;
  152. }
  153. int find_on_inbuf(char *string, int string_len,
  154. char *buf, int buf_datalen) {
  155. /* find first instance of needle 'string' on haystack 'buf'. return how
  156. * many bytes from the beginning of buf to the end of string.
  157. * If it's not there, return -1.
  158. */
  159. char *location;
  160. char *last_possible = buf + buf_datalen - string_len;
  161. assert(string && string_len > 0 && buf);
  162. if(buf_datalen < string_len)
  163. return -1;
  164. for(location = buf; location <= last_possible; location++)
  165. if((*location == *string) && !memcmp(location+1, string+1, string_len-1))
  166. return location-buf+string_len;
  167. return -1;
  168. }
  169. /*
  170. Local Variables:
  171. mode:c
  172. indent-tabs-mode:nil
  173. c-basic-offset:2
  174. End:
  175. */