Never call free() on tor_malloc()d memory. This is unlikely to be our current leak...
[tor.git] / src / or / buffers.c
blobe3b5c75ca20c6ba9234802f5ee92bddb74acf090
1 /* Copyright 2001 Matej Pfajfar.
2 * Copyright 2001-2004 Roger Dingledine.
3 * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
4 /* See LICENSE for licensing information */
5 /* $Id$ */
6 const char buffers_c_id[] = "$Id$";
8 /**
9 * \file buffers.c
10 * \brief Implements a generic buffer interface. Buffers are
11 * fairly opaque string holders that can read to or flush from:
12 * memory, file descriptors, or TLS connections.
13 **/
15 #include "or.h"
17 #define SENTINELS
18 #undef CHECK_AFTER_RESIZE
19 #undef PARANOIA
20 #undef NOINLINE
22 #ifdef SENTINELS
23 /* If SENTINELS is defined, check for attempts to write beyond the
24 * end/before the start of the buffer.
26 #define START_MAGIC 0x70370370u
27 #define END_MAGIC 0xA0B0C0D0u
28 #define RAW_MEM(m) ((void*)(((char*)m)-4))
29 #define GUARDED_MEM(m) ((void*)(((char*)m)+4))
30 #define ALLOC_LEN(ln) ((ln)+8)
31 #define SET_GUARDS(m, ln) \
32 do { set_uint32((m)-4,START_MAGIC); set_uint32((m)+ln,END_MAGIC); } while (0)
33 #else
34 #define RAW_MEM(m) (m)
35 #define GUARDED_MEM(m) (m)
36 #define ALLOC_LEN(ln) (ln)
37 #define SET_GUARDS(m,ln) do {} while (0)
38 #endif
40 #ifdef PARANOIA
41 #define check() do { assert_buf_ok(buf); } while (0)
42 #else
43 #define check() do { } while (0)
44 #endif
46 #ifdef NOINLINE
47 #undef INLINE
48 #define INLINE
49 #endif
51 #define BUFFER_MAGIC 0xB0FFF312u
52 struct buf_t {
53 uint32_t magic; /**< Magic cookie for debugging: Must be set to BUFFER_MAGIC */
54 char *mem; /**< Storage for data in the buffer */
55 char *cur; /**< The first byte used for storing data in the buffer. */
56 size_t highwater; /**< Largest observed datalen since last buf_shrink */
57 size_t len; /**< Maximum amount of data that <b>mem</b> can hold. */
58 size_t datalen; /**< Number of bytes currently in <b>mem</b>. */
61 uint64_t buf_total_used = 0;
62 uint64_t buf_total_alloc = 0;
64 /** Size, in bytes, for newly allocated buffers. Should be a power of 2. */
65 #define INITIAL_BUF_SIZE (4*1024)
66 /** Size, in bytes, for minimum 'shrink' size for buffers. Buffers may start
67 * out smaller than this, but they will never autoshrink to less
68 * than this size. */
69 #define MIN_GREEDY_SHRINK_SIZE (16*1024)
70 #define MIN_LAZY_SHRINK_SIZE (4*1024)
72 static INLINE void peek_from_buf(char *string, size_t string_len, buf_t *buf);
74 /** If the contents of buf wrap around the end of the allocated space,
75 * malloc a new buf and copy the contents in starting at the
76 * beginning. This operation is relatively expensive, so it shouldn't
77 * be used e.g. for every single read or write.
79 static void
80 buf_normalize(buf_t *buf)
82 check();
83 if (buf->cur + buf->datalen <= buf->mem+buf->len) {
84 return;
85 } else {
86 char *newmem, *oldmem;
87 size_t sz = (buf->mem+buf->len)-buf->cur;
88 log_fn(LOG_WARN, "Unexpected non-normalized buffer.");
89 newmem = GUARDED_MEM(tor_malloc(ALLOC_LEN(buf->len)));
90 SET_GUARDS(newmem, buf->len);
91 memcpy(newmem, buf->cur, sz);
92 memcpy(newmem+sz, buf->mem, buf->datalen-sz);
93 oldmem = RAW_MEM(buf->mem);
94 tor_free(oldmem); /* Can't use tor_free directly. */
95 buf->mem = buf->cur = newmem;
96 check();
100 /** Return the point in the buffer where the next byte will get stored. */
101 static INLINE char *
102 _buf_end(buf_t *buf)
104 char *next = buf->cur + buf->datalen;
105 char *end = buf->mem + buf->len;
106 return (next < end) ? next : (next - buf->len);
109 /** If the pointer <b>cp</b> has passed beyond the end of the buffer, wrap it
110 * around. */
111 static INLINE char *
112 _wrap_ptr(buf_t *buf, char *cp)
114 return (cp >= buf->mem + buf->len) ? (cp - buf->len) : cp;
117 /** Return the offset of <b>cp</b> within the buffer. */
118 static INLINE int
119 _buf_offset(buf_t *buf, char *cp)
121 if (cp >= buf->cur)
122 return cp - buf->cur;
123 else
124 /* return (cp - buf->mem) + buf->mem+buf->len - buf->cur */
125 return cp + buf->len - buf->cur;
128 /** If the range of *<b>len</b> bytes starting at <b>at</b> wraps around the
129 * end of the buffer, then set *<b>len</b> to the number of bytes starting
130 * at <b>at</b>, and set *<b>more_len</b> to the number of bytes starting
131 * at <b>buf-&gt;mem</b>. Otherwise, set *<b>more_len</b> to 0.
133 static INLINE void
134 _split_range(buf_t *buf, char *at, size_t *len,
135 size_t *more_len)
137 char *eos = at + *len;
138 check();
139 if (eos >= (buf->mem + buf->len)) {
140 *more_len = eos - (buf->mem + buf->len);
141 *len -= *more_len;
142 } else {
143 *more_len = 0;
147 /** Change a buffer's capacity. <b>new_capacity</b> must be \>= buf->datalen. */
148 static void
149 buf_resize(buf_t *buf, size_t new_capacity)
151 off_t offset;
152 #ifdef CHECK_AFTER_RESIZE
153 char *tmp, *tmp2;
154 #endif
155 tor_assert(buf->datalen <= new_capacity);
156 tor_assert(new_capacity);
158 #ifdef CHECK_AFTER_RESIZE
159 assert_buf_ok(buf);
160 tmp = tor_malloc(buf->datalen);
161 tmp2 = tor_malloc(buf->datalen);
162 peek_from_buf(tmp, buf->datalen, buf);
163 #endif
165 if (buf->len == new_capacity)
166 return;
168 offset = buf->cur - buf->mem;
169 if (offset + buf->datalen > new_capacity) {
170 /* We need to move stuff before we shrink. */
171 if (offset + buf->datalen > buf->len) {
172 /* We have:
174 * mem[0] ... mem[datalen-(len-offset)] (end of data)
175 * mem[offset] ... mem[len-1] (the start of the data)
177 * We're shrinking the buffer by (len-new_capacity) bytes, so we need
178 * to move the start portion back by that many bytes.
180 memmove(buf->cur-(buf->len-new_capacity), buf->cur,
181 buf->len-offset);
182 offset -= (buf->len-new_capacity);
183 } else {
184 /* The data doesn't wrap around, but it does extend beyond the new
185 * buffer length:
186 * mem[offset] ... mem[offset+datalen-1] (the data)
188 memmove(buf->mem, buf->cur, buf->datalen);
189 offset = 0;
193 /* XXX Some play code to throw away old buffers sometimes rather
194 * than constantly reallocing them; just in case this is our memory
195 * problem. It looks for now like it isn't, so disabled. -RD */
196 if (0 && new_capacity == MIN_LAZY_SHRINK_SIZE &&
197 !buf->datalen &&
198 buf->len >= 1<<16) {
199 /* don't realloc; free and malloc */
200 char *oldmem, *newmem = GUARDED_MEM(tor_malloc(ALLOC_LEN(new_capacity)));
201 SET_GUARDS(newmem, new_capacity);
202 oldmem = RAW_MEM(buf->mem);
203 tor_free(oldmem);
204 buf->mem = buf->cur = newmem;
205 } else {
206 buf->mem = GUARDED_MEM(tor_realloc(RAW_MEM(buf->mem),
207 ALLOC_LEN(new_capacity)));
208 SET_GUARDS(buf->mem, new_capacity);
209 buf->cur = buf->mem+offset;
211 buf_total_alloc += new_capacity;
212 buf_total_alloc -= buf->len;
214 if (offset + buf->datalen > buf->len) {
215 /* We need to move data now that we are done growing. The buffer
216 * now contains:
218 * mem[0] ... mem[datalen-(len-offset)] (end of data)
219 * mem[offset] ... mem[len-1] (the start of the data)
220 * mem[len]...mem[new_capacity] (empty space)
222 * We're growing by (new_capacity-len) bytes, so we need to move the
223 * end portion forward by that many bytes.
225 memmove(buf->cur+(new_capacity-buf->len), buf->cur,
226 buf->len-offset);
227 buf->cur += new_capacity-buf->len;
229 buf->len = new_capacity;
231 #ifdef CHECK_AFTER_RESIZE
232 assert_buf_ok(buf);
233 peek_from_buf(tmp2, buf->datalen, buf);
234 if (memcmp(tmp, tmp2, buf->datalen)) {
235 tor_assert(0);
237 tor_free(tmp);
238 tor_free(tmp2);
239 #endif
242 /** If the buffer is not large enough to hold <b>capacity</b> bytes, resize
243 * it so that it can. (The new size will be a power of 2 times the old
244 * size.)
246 static INLINE int
247 buf_ensure_capacity(buf_t *buf, size_t capacity)
249 size_t new_len;
250 if (buf->len >= capacity) /* Don't grow if we're already big enough. */
251 return 0;
252 if (capacity > MAX_BUF_SIZE) /* Don't grow past the maximum. */
253 return -1;
254 /* Find the smallest new_len equal to (2**X)*len for some X; such that
255 * new_len is at least capacity.
257 new_len = buf->len*2;
258 while (new_len < capacity)
259 new_len *= 2;
260 /* Resize the buffer. */
261 log_fn(LOG_DEBUG,"Growing buffer from %d to %d bytes.",
262 (int)buf->len, (int)new_len);
263 buf_resize(buf,new_len);
264 return 0;
267 /** Resize buf so it won't hold extra memory that we haven't been
268 * using lately (that is, since the last time we called buf_shrink).
269 * Try to shrink the buf until it is the largest factor of two that
270 * can contain <b>buf</b>-&gt;highwater, but never smaller than
271 * MIN_LAZY_SHRINK_SIZE.
273 void
274 buf_shrink(buf_t *buf)
276 size_t new_len;
278 new_len = buf->len;
279 while (buf->highwater < (new_len>>2) && new_len > MIN_LAZY_SHRINK_SIZE*2)
280 new_len >>= 1;
282 buf->highwater = buf->datalen;
283 if (new_len == buf->len)
284 return;
286 log_fn(LOG_DEBUG,"Shrinking buffer from %d to %d bytes.",
287 (int)buf->len, (int)new_len);
288 buf_resize(buf, new_len);
291 /** Remove the first <b>n</b> bytes from buf. */
292 static INLINE void
293 buf_remove_from_front(buf_t *buf, size_t n)
295 tor_assert(buf->datalen >= n);
296 buf->datalen -= n;
297 buf_total_used -= n;
298 if (buf->datalen) {
299 buf->cur = _wrap_ptr(buf, buf->cur+n);
300 } else {
301 buf->cur = buf->mem;
303 check();
306 /** Make sure that the memory in buf ends with a zero byte. */
307 static INLINE int
308 buf_nul_terminate(buf_t *buf)
310 if (buf_ensure_capacity(buf,buf->datalen+1)<0)
311 return -1;
312 *_buf_end(buf) = '\0';
313 return 0;
316 /** Create and return a new buf with capacity <b>size</b>. */
317 buf_t *
318 buf_new_with_capacity(size_t size)
320 buf_t *buf;
321 buf = tor_malloc_zero(sizeof(buf_t));
322 buf->magic = BUFFER_MAGIC;
323 buf->cur = buf->mem = GUARDED_MEM(tor_malloc(ALLOC_LEN(size)));
324 SET_GUARDS(buf->mem, size);
325 buf->len = size;
327 buf_total_alloc += size;
328 assert_buf_ok(buf);
329 return buf;
332 /** Allocate and return a new buffer with default capacity. */
333 buf_t *
334 buf_new(void)
336 return buf_new_with_capacity(INITIAL_BUF_SIZE);
339 /** Remove all data from <b>buf</b>. */
340 void
341 buf_clear(buf_t *buf)
343 buf_total_used -= buf->datalen;
344 buf->datalen = 0;
345 buf->cur = buf->mem;
348 /** Return the number of bytes stored in <b>buf</b> */
349 size_t
350 buf_datalen(const buf_t *buf)
352 return buf->datalen;
355 /** Return the maximum bytes that can be stored in <b>buf</b> before buf
356 * needs to resize. */
357 size_t
358 buf_capacity(const buf_t *buf)
360 return buf->len;
363 /** For testing only: Return a pointer to the raw memory stored in
364 * <b>buf</b>. */
365 const char *
366 _buf_peek_raw_buffer(const buf_t *buf)
368 return buf->cur;
371 /** Release storage held by <b>buf</b>. */
372 void
373 buf_free(buf_t *buf)
375 char *oldmem;
376 assert_buf_ok(buf);
377 buf->magic = 0xDEADBEEF;
378 oldmem = RAW_MEM(buf->mem);
379 tor_free(oldmem);
380 buf_total_alloc -= buf->len;
381 buf_total_used -= buf->datalen;
382 tor_free(buf);
385 /** Helper for read_to_buf(): read no more than at_most bytes from
386 * socket s into buffer buf, starting at the position pos. (Does not
387 * check for overflow.) Set *reached_eof to true on EOF. Return
388 * number of bytes read on success, 0 if the read would block, -1 on
389 * failure.
391 static INLINE int
392 read_to_buf_impl(int s, size_t at_most, buf_t *buf,
393 char *pos, int *reached_eof)
395 int read_result;
397 // log_fn(LOG_DEBUG,"reading at most %d bytes.",at_most);
398 read_result = recv(s, pos, at_most, 0);
399 if (read_result < 0) {
400 int e = tor_socket_errno(s);
401 if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
402 return -1;
404 return 0; /* would block. */
405 } else if (read_result == 0) {
406 log_fn(LOG_DEBUG,"Encountered eof");
407 *reached_eof = 1;
408 return 0;
409 } else { /* we read some bytes */
410 buf->datalen += read_result;
411 buf_total_used += read_result;
412 if (buf->datalen > buf->highwater)
413 buf->highwater = buf->datalen;
414 log_fn(LOG_DEBUG,"Read %d bytes. %d on inbuf.",read_result,
415 (int)buf->datalen);
416 return read_result;
420 /** Read from socket <b>s</b>, writing onto end of <b>buf</b>. Read at most
421 * <b>at_most</b> bytes, resizing the buffer as necessary. If recv()
422 * returns 0, set *<b>reached_eof</b> to 1 and return 0. Return -1 on error;
423 * else return the number of bytes read. Return 0 if recv() would
424 * block.
427 read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof)
429 int r;
430 char *next;
431 size_t at_start;
433 assert_buf_ok(buf);
434 tor_assert(reached_eof);
435 tor_assert(s>=0);
437 if (buf_ensure_capacity(buf,buf->datalen+at_most))
438 return -1;
440 if (at_most + buf->datalen > buf->len)
441 at_most = buf->len - buf->datalen; /* take the min of the two */
443 if (at_most == 0)
444 return 0; /* we shouldn't read anything */
446 next = _buf_end(buf);
447 _split_range(buf, next, &at_most, &at_start);
449 r = read_to_buf_impl(s, at_most, buf, next, reached_eof);
450 check();
451 if (r < 0 || (size_t)r < at_most) {
452 return r; /* Either error, eof, block, or no more to read. */
455 if (at_start) {
456 int r2;
457 tor_assert(_buf_end(buf) == buf->mem);
458 r2 = read_to_buf_impl(s, at_start, buf, buf->mem, reached_eof);
459 check();
460 if (r2 < 0) {
461 return r2;
462 } else {
463 r += r2;
466 return r;
469 /** Helper for read_to_buf_tls(): read no more than <b>at_most</b>
470 * bytes from the TLS connection <b>tls</b> into buffer <b>buf</b>,
471 * starting at the position <b>next</b>. (Does not check for overflow.)
472 * Return number of bytes read on success, 0 if the read would block,
473 * -1 on failure.
475 static INLINE int
476 read_to_buf_tls_impl(tor_tls *tls, size_t at_most, buf_t *buf, char *next)
478 int r;
480 log_fn(LOG_DEBUG,"before: %d on buf, %d pending, at_most %d.",
481 (int)buf_datalen(buf), (int)tor_tls_get_pending_bytes(tls),
482 (int)at_most);
483 r = tor_tls_read(tls, next, at_most);
484 if (r<0)
485 return r;
486 buf->datalen += r;
487 buf_total_used += r;
488 if (buf->datalen > buf->highwater)
489 buf->highwater = buf->datalen;
490 log_fn(LOG_DEBUG,"Read %d bytes. %d on inbuf; %d pending",r,
491 (int)buf->datalen,(int)tor_tls_get_pending_bytes(tls));
492 return r;
495 /** As read_to_buf, but reads from a TLS connection.
497 * Using TLS on OR connections complicates matters in two ways.
499 * First, a TLS stream has its own read buffer independent of the
500 * connection's read buffer. (TLS needs to read an entire frame from
501 * the network before it can decrypt any data. Thus, trying to read 1
502 * byte from TLS can require that several KB be read from the network
503 * and decrypted. The extra data is stored in TLS's decrypt buffer.)
504 * Because the data hasn't been read by Tor (it's still inside the TLS),
505 * this means that sometimes a connection "has stuff to read" even when
506 * poll() didn't return POLLIN. The tor_tls_get_pending_bytes function is
507 * used in connection.c to detect TLS objects with non-empty internal
508 * buffers and read from them again.
510 * Second, the TLS stream's events do not correspond directly to network
511 * events: sometimes, before a TLS stream can read, the network must be
512 * ready to write -- or vice versa.
515 read_to_buf_tls(tor_tls *tls, size_t at_most, buf_t *buf)
517 int r;
518 char *next;
519 size_t at_start;
521 tor_assert(tls);
522 assert_buf_ok(buf);
524 log_fn(LOG_DEBUG,"start: %d on buf, %d pending, at_most %d.",
525 (int)buf_datalen(buf), (int)tor_tls_get_pending_bytes(tls),
526 (int)at_most);
528 if (buf_ensure_capacity(buf, at_most+buf->datalen))
529 return TOR_TLS_ERROR;
531 if (at_most + buf->datalen > buf->len)
532 at_most = buf->len - buf->datalen;
534 if (at_most == 0)
535 return 0;
537 next = _buf_end(buf);
538 _split_range(buf, next, &at_most, &at_start);
540 r = read_to_buf_tls_impl(tls, at_most, buf, next);
541 check();
542 if (r < 0 || (size_t)r < at_most)
543 return r; /* Either error, eof, block, or no more to read. */
545 if (at_start) {
546 int r2;
547 tor_assert(_buf_end(buf) == buf->mem);
548 r2 = read_to_buf_tls_impl(tls, at_start, buf, buf->mem);
549 check();
550 if (r2 < 0)
551 return r2;
552 else
553 r += r2;
555 return r;
558 /** Helper for flush_buf(): try to write <b>sz</b> bytes from buffer
559 * <b>buf</b> onto socket <b>s</b>. On success, deduct the bytes written
560 * from *<b>buf_flushlen</b>.
561 * Return the number of bytes written on success, -1 on failure.
563 static INLINE int
564 flush_buf_impl(int s, buf_t *buf, size_t sz, size_t *buf_flushlen)
566 int write_result;
568 write_result = send(s, buf->cur, sz, 0);
569 if (write_result < 0) {
570 int e = tor_socket_errno(s);
571 if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
572 return -1;
574 log_fn(LOG_DEBUG,"write() would block, returning.");
575 return 0;
576 } else {
577 *buf_flushlen -= write_result;
579 buf_remove_from_front(buf, write_result);
581 return write_result;
585 /** Write data from <b>buf</b> to the socket <b>s</b>. Write at most
586 * *<b>buf_flushlen</b> bytes, decrement *<b>buf_flushlen</b> by
587 * the number of bytes actually written, and remove the written bytes
588 * from the buffer. Return the number of bytes written on success,
589 * -1 on failure. Return 0 if write() would block.
592 flush_buf(int s, buf_t *buf, size_t *buf_flushlen)
594 int r;
595 size_t flushed = 0;
596 size_t flushlen0, flushlen1;
598 assert_buf_ok(buf);
599 tor_assert(buf_flushlen);
600 tor_assert(s>=0);
601 tor_assert(*buf_flushlen <= buf->datalen);
603 if (*buf_flushlen == 0) /* nothing to flush */
604 return 0;
606 flushlen0 = *buf_flushlen;
607 _split_range(buf, buf->cur, &flushlen0, &flushlen1);
609 r = flush_buf_impl(s, buf, flushlen0, buf_flushlen);
610 check();
612 log_fn(LOG_DEBUG,"%d: flushed %d bytes, %d ready to flush, %d remain.",
613 s,r,(int)*buf_flushlen,(int)buf->datalen);
614 if (r < 0 || (size_t)r < flushlen0)
615 return r; /* Error, or can't flush any more now. */
616 flushed = r;
618 if (flushlen1) {
619 tor_assert(buf->cur == buf->mem);
620 r = flush_buf_impl(s, buf, flushlen1, buf_flushlen);
621 check();
622 log_fn(LOG_DEBUG,"%d: flushed %d bytes, %d ready to flush, %d remain.",
623 s,r,(int)*buf_flushlen,(int)buf->datalen);
624 if (r<0)
625 return r;
626 flushed += r;
628 return flushed;
631 /** Helper for flush_buf_tls(): try to write <b>sz</b> bytes from buffer
632 * <b>buf</b> onto TLS object <b>tls</b>. On success, deduct the bytes
633 * written from *<b>buf_flushlen</b>.
634 * Return the number of bytes written on success, -1 on failure.
636 static INLINE int
637 flush_buf_tls_impl(tor_tls *tls, buf_t *buf, size_t sz, size_t *buf_flushlen)
639 int r;
641 r = tor_tls_write(tls, buf->cur, sz);
642 if (r < 0) {
643 return r;
645 *buf_flushlen -= r;
646 buf_remove_from_front(buf, r);
647 log_fn(LOG_DEBUG,"flushed %d bytes, %d ready to flush, %d remain.",
648 r,(int)*buf_flushlen,(int)buf->datalen);
649 return r;
652 /** As flush_buf(), but writes data to a TLS connection.
655 flush_buf_tls(tor_tls *tls, buf_t *buf, size_t *buf_flushlen)
657 int r;
658 size_t flushed=0;
659 size_t flushlen0, flushlen1;
660 assert_buf_ok(buf);
661 tor_assert(tls);
662 tor_assert(buf_flushlen);
664 /* we want to let tls write even if flushlen is zero, because it might
665 * have a partial record pending */
666 check_no_tls_errors();
668 flushlen0 = *buf_flushlen;
669 _split_range(buf, buf->cur, &flushlen0, &flushlen1);
671 r = flush_buf_tls_impl(tls, buf, flushlen0, buf_flushlen);
672 check();
673 if (r < 0 || (size_t)r < flushlen0)
674 return r; /* Error, or can't flush any more now. */
675 flushed = r;
677 if (flushlen1) {
678 tor_assert(buf->cur == buf->mem);
679 r = flush_buf_tls_impl(tls, buf, flushlen1, buf_flushlen);
680 check();
681 if (r<0)
682 return r;
683 flushed += r;
685 return flushed;
688 /** Append <b>string_len</b> bytes from <b>string</b> to the end of
689 * <b>buf</b>.
691 * Return the new length of the buffer on success, -1 on failure.
694 write_to_buf(const char *string, size_t string_len, buf_t *buf)
696 char *next;
697 size_t len2;
699 /* append string to buf (growing as needed, return -1 if "too big")
700 * return total number of bytes on the buf
703 tor_assert(string);
704 assert_buf_ok(buf);
706 if (buf_ensure_capacity(buf, buf->datalen+string_len)) {
707 log_fn(LOG_WARN, "buflen too small, can't hold %d bytes.", (int)(buf->datalen+string_len));
708 return -1;
711 next = _buf_end(buf);
712 _split_range(buf, next, &string_len, &len2);
714 memcpy(next, string, string_len);
715 buf->datalen += string_len;
716 buf_total_used += string_len;
718 if (len2) {
719 tor_assert(_buf_end(buf) == buf->mem);
720 memcpy(buf->mem, string+string_len, len2);
721 buf->datalen += len2;
722 buf_total_used += len2;
724 if (buf->datalen > buf->highwater)
725 buf->highwater = buf->datalen;
726 log_fn(LOG_DEBUG,"added %d bytes to buf (now %d total).",
727 (int)string_len, (int)buf->datalen);
728 check();
729 return buf->datalen;
732 /** Helper: copy the first <b>string_len</b> bytes from <b>buf</b>
733 * onto <b>string</b>.
735 static INLINE void
736 peek_from_buf(char *string, size_t string_len, buf_t *buf)
738 size_t len2;
740 /* There must be string_len bytes in buf; write them onto string,
741 * then memmove buf back (that is, remove them from buf).
743 * Return the number of bytes still on the buffer. */
745 tor_assert(string);
746 tor_assert(string_len <= buf->datalen); /* make sure we don't ask for too much */
747 assert_buf_ok(buf);
749 _split_range(buf, buf->cur, &string_len, &len2);
751 memcpy(string, buf->cur, string_len);
752 if (len2) {
753 memcpy(string+string_len,buf->mem,len2);
757 /** Remove <b>string_len</b> bytes from the front of <b>buf</b>, and store them
758 * into <b>string</b>. Return the new buffer size. <b>string_len</b> must be \<=
759 * the number of bytes on the buffer.
762 fetch_from_buf(char *string, size_t string_len, buf_t *buf)
764 /* There must be string_len bytes in buf; write them onto string,
765 * then memmove buf back (that is, remove them from buf).
767 * Return the number of bytes still on the buffer. */
769 check();
770 peek_from_buf(string, string_len, buf);
771 buf_remove_from_front(buf, string_len);
772 check();
773 return buf->datalen;
776 /** There is a (possibly incomplete) http statement on <b>buf</b>, of the
777 * form "\%s\\r\\n\\r\\n\%s", headers, body. (body may contain nuls.)
778 * If a) the headers include a Content-Length field and all bytes in
779 * the body are present, or b) there's no Content-Length field and
780 * all headers are present, then:
782 * - strdup headers into <b>*headers_out</b>, and nul-terminate it.
783 * - memdup body into <b>*body_out</b>, and nul-terminate it.
784 * - Then remove them from <b>buf</b>, and return 1.
786 * - If headers or body is NULL, discard that part of the buf.
787 * - If a headers or body doesn't fit in the arg, return -1.
788 * (We ensure that the headers or body don't exceed max len,
789 * _even if_ we're planning to discard them.)
791 * Else, change nothing and return 0.
794 fetch_from_buf_http(buf_t *buf,
795 char **headers_out, size_t max_headerlen,
796 char **body_out, size_t *body_used, size_t max_bodylen)
798 char *headers, *body, *p;
799 size_t headerlen, bodylen, contentlen;
801 assert_buf_ok(buf);
802 buf_normalize(buf);
804 if (buf_nul_terminate(buf)<0) {
805 log_fn(LOG_WARN,"Couldn't nul-terminate buffer");
806 return -1;
808 headers = buf->cur;
809 body = strstr(headers,"\r\n\r\n");
810 if (!body) {
811 log_fn(LOG_DEBUG,"headers not all here yet.");
812 return 0;
814 body += 4; /* Skip the the CRLFCRLF */
815 headerlen = body-headers; /* includes the CRLFCRLF */
816 bodylen = buf->datalen - headerlen;
817 log_fn(LOG_DEBUG,"headerlen %d, bodylen %d.", (int)headerlen, (int)bodylen);
819 if (max_headerlen <= headerlen) {
820 log_fn(LOG_WARN,"headerlen %d larger than %d. Failing.", (int)headerlen,
821 (int)max_headerlen-1);
822 return -1;
824 if (max_bodylen <= bodylen) {
825 log_fn(LOG_WARN,"bodylen %d larger than %d. Failing.", (int)bodylen, (int)max_bodylen-1);
826 return -1;
829 #define CONTENT_LENGTH "\r\nContent-Length: "
830 p = strstr(headers, CONTENT_LENGTH);
831 if (p) {
832 int i;
833 i = atoi(p+strlen(CONTENT_LENGTH));
834 if (i < 0) {
835 log_fn(LOG_WARN, "Content-Length is less than zero; it looks like someone is trying to crash us.");
836 return -1;
838 contentlen = i;
839 /* if content-length is malformed, then our body length is 0. fine. */
840 log_fn(LOG_DEBUG,"Got a contentlen of %d.",(int)contentlen);
841 if (bodylen < contentlen) {
842 log_fn(LOG_DEBUG,"body not all here yet.");
843 return 0; /* not all there yet */
845 if (bodylen > contentlen) {
846 bodylen = contentlen;
847 log_fn(LOG_DEBUG,"bodylen reduced to %d.",(int)bodylen);
850 /* all happy. copy into the appropriate places, and return 1 */
851 if (headers_out) {
852 *headers_out = tor_malloc(headerlen+1);
853 memcpy(*headers_out,buf->cur,headerlen);
854 (*headers_out)[headerlen] = 0; /* null terminate it */
856 if (body_out) {
857 tor_assert(body_used);
858 *body_used = bodylen;
859 *body_out = tor_malloc(bodylen+1);
860 memcpy(*body_out,buf->cur+headerlen,bodylen);
861 (*body_out)[bodylen] = 0; /* null terminate it */
863 buf_remove_from_front(buf, headerlen+bodylen);
864 return 1;
867 /** There is a (possibly incomplete) socks handshake on <b>buf</b>, of one
868 * of the forms
869 * - socks4: "socksheader username\\0"
870 * - socks4a: "socksheader username\\0 destaddr\\0"
871 * - socks5 phase one: "version #methods methods"
872 * - socks5 phase two: "version command 0 addresstype..."
873 * If it's a complete and valid handshake, and destaddr fits in
874 * MAX_SOCKS_ADDR_LEN bytes, then pull the handshake off the buf,
875 * assign to <b>req</b>, and return 1.
877 * If it's invalid or too big, return -1.
879 * Else it's not all there yet, leave buf alone and return 0.
881 * If you want to specify the socks reply, write it into <b>req->reply</b>
882 * and set <b>req->replylen</b>, else leave <b>req->replylen</b> alone.
884 * If returning 0 or -1, <b>req->address</b> and <b>req->port</b> are undefined.
887 fetch_from_buf_socks(buf_t *buf, socks_request_t *req)
889 unsigned char len;
890 char tmpbuf[INET_NTOA_BUF_LEN];
891 uint32_t destip;
892 enum {socks4, socks4a} socks4_prot = socks4a;
893 char *next, *startaddr;
894 struct in_addr in;
896 /* If the user connects with socks4 or the wrong variant of socks5,
897 * then log a warning to let him know that it might be unwise. */
898 static int have_warned_about_unsafe_socks = 0;
900 if (buf->datalen < 2) /* version and another byte */
901 return 0;
902 buf_normalize(buf);
904 switch (*(buf->cur)) { /* which version of socks? */
906 case 5: /* socks5 */
908 if (req->socks_version != 5) { /* we need to negotiate a method */
909 unsigned char nummethods = (unsigned char)*(buf->cur+1);
910 tor_assert(!req->socks_version);
911 if (buf->datalen < 2u+nummethods)
912 return 0;
913 if (!nummethods || !memchr(buf->cur+2, 0, nummethods)) {
914 log_fn(LOG_WARN,"socks5: offered methods don't include 'no auth'. Rejecting.");
915 req->replylen = 2; /* 2 bytes of response */
916 req->reply[0] = 5;
917 req->reply[1] = '\xFF'; /* reject all methods */
918 return -1;
920 buf_remove_from_front(buf,2+nummethods);/* remove packet from buf */
922 req->replylen = 2; /* 2 bytes of response */
923 req->reply[0] = 5; /* socks5 reply */
924 req->reply[1] = SOCKS5_SUCCEEDED;
925 req->socks_version = 5; /* remember that we've already negotiated auth */
926 log_fn(LOG_DEBUG,"socks5: accepted method 0");
927 return 0;
929 /* we know the method; read in the request */
930 log_fn(LOG_DEBUG,"socks5: checking request");
931 if (buf->datalen < 8) /* basic info plus >=2 for addr plus 2 for port */
932 return 0; /* not yet */
933 req->command = (unsigned char) *(buf->cur+1);
934 if (req->command != SOCKS_COMMAND_CONNECT &&
935 req->command != SOCKS_COMMAND_RESOLVE) {
936 /* not a connect or resolve? we don't support it. */
937 log_fn(LOG_WARN,"socks5: command %d not recognized. Rejecting.",
938 req->command);
939 return -1;
941 switch (*(buf->cur+3)) { /* address type */
942 case 1: /* IPv4 address */
943 log_fn(LOG_DEBUG,"socks5: ipv4 address type");
944 if (buf->datalen < 10) /* ip/port there? */
945 return 0; /* not yet */
947 destip = ntohl(*(uint32_t*)(buf->cur+4));
948 in.s_addr = htonl(destip);
949 tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
950 if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
951 log_fn(LOG_WARN,"socks5 IP takes %d bytes, which doesn't fit in %d. Rejecting.",
952 (int)strlen(tmpbuf)+1,(int)MAX_SOCKS_ADDR_LEN);
953 return -1;
955 strlcpy(req->address,tmpbuf,sizeof(req->address));
956 req->port = ntohs(*(uint16_t*)(buf->cur+8));
957 buf_remove_from_front(buf, 10);
958 if (!address_is_in_virtual_range(req->address) &&
959 !have_warned_about_unsafe_socks) {
960 log_fn(LOG_WARN,"Your application (using socks5 on port %d) is giving Tor only an IP address. Applications that do DNS resolves themselves may leak information. Consider using Socks4A (e.g. via privoxy or socat) instead. For more information, please see http://wiki.noreply.org/noreply/TheOnionRouter/TorFAQ#SOCKSAndDNS", req->port);
961 // have_warned_about_unsafe_socks = 1; // (for now, warn every time)
963 return 1;
964 case 3: /* fqdn */
965 log_fn(LOG_DEBUG,"socks5: fqdn address type");
966 len = (unsigned char)*(buf->cur+4);
967 if (buf->datalen < 7u+len) /* addr/port there? */
968 return 0; /* not yet */
969 if (len+1 > MAX_SOCKS_ADDR_LEN) {
970 log_fn(LOG_WARN,"socks5 hostname is %d bytes, which doesn't fit in %d. Rejecting.",
971 len+1,MAX_SOCKS_ADDR_LEN);
972 return -1;
974 memcpy(req->address,buf->cur+5,len);
975 req->address[len] = 0;
976 req->port = ntohs(get_uint16(buf->cur+5+len));
977 buf_remove_from_front(buf, 5+len+2);
978 return 1;
979 default: /* unsupported */
980 log_fn(LOG_WARN,"socks5: unsupported address type %d. Rejecting.",*(buf->cur+3));
981 return -1;
983 tor_assert(0);
984 case 4: /* socks4 */
985 /* http://archive.socks.permeo.com/protocol/socks4.protocol */
986 /* http://archive.socks.permeo.com/protocol/socks4a.protocol */
988 req->socks_version = 4;
989 if (buf->datalen < SOCKS4_NETWORK_LEN) /* basic info available? */
990 return 0; /* not yet */
992 req->command = (unsigned char) *(buf->cur+1);
993 if (req->command != SOCKS_COMMAND_CONNECT &&
994 req->command != SOCKS_COMMAND_RESOLVE) {
995 /* not a connect or resolve? we don't support it. */
996 log_fn(LOG_WARN,"socks4: command %d not recognized. Rejecting.",
997 req->command);
998 return -1;
1001 req->port = ntohs(*(uint16_t*)(buf->cur+2));
1002 destip = ntohl(*(uint32_t*)(buf->mem+4));
1003 if ((!req->port && req->command!=SOCKS_COMMAND_RESOLVE) || !destip) {
1004 log_fn(LOG_WARN,"socks4: Port or DestIP is zero. Rejecting.");
1005 return -1;
1007 if (destip >> 8) {
1008 log_fn(LOG_DEBUG,"socks4: destip not in form 0.0.0.x.");
1009 in.s_addr = htonl(destip);
1010 tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
1011 if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
1012 log_fn(LOG_WARN,"socks4 addr (%d bytes) too long. Rejecting.",
1013 (int)strlen(tmpbuf));
1014 return -1;
1016 log_fn(LOG_DEBUG,"socks4: successfully read destip (%s)", safe_str(tmpbuf));
1017 socks4_prot = socks4;
1020 next = memchr(buf->cur+SOCKS4_NETWORK_LEN, 0,
1021 buf->datalen-SOCKS4_NETWORK_LEN);
1022 if (!next) {
1023 log_fn(LOG_DEBUG,"socks4: Username not here yet.");
1024 return 0;
1026 tor_assert(next < buf->cur+buf->datalen);
1028 startaddr = NULL;
1029 if (socks4_prot != socks4a &&
1030 !address_is_in_virtual_range(tmpbuf) &&
1031 !have_warned_about_unsafe_socks) {
1032 log_fn(LOG_WARN,"Your application (using socks4 on port %d) is giving Tor only an IP address. Applications that do DNS resolves themselves may leak information. Consider using Socks4A (e.g. via privoxy or socat) instead.", req->port);
1033 // have_warned_about_unsafe_socks = 1; // (for now, warn every time)
1035 if (socks4_prot == socks4a) {
1036 if (next+1 == buf->cur+buf->datalen) {
1037 log_fn(LOG_DEBUG,"socks4: No part of destaddr here yet.");
1038 return 0;
1040 startaddr = next+1;
1041 next = memchr(startaddr, 0, buf->cur+buf->datalen-startaddr);
1042 if (!next) {
1043 log_fn(LOG_DEBUG,"socks4: Destaddr not all here yet.");
1044 return 0;
1046 if (MAX_SOCKS_ADDR_LEN <= next-startaddr) {
1047 log_fn(LOG_WARN,"socks4: Destaddr too long. Rejecting.");
1048 return -1;
1050 tor_assert(next < buf->cur+buf->datalen);
1052 log_fn(LOG_DEBUG,"socks4: Everything is here. Success.");
1053 strlcpy(req->address, startaddr ? startaddr : tmpbuf,
1054 sizeof(req->address));
1055 buf_remove_from_front(buf, next-buf->cur+1); /* next points to the final \0 on inbuf */
1056 return 1;
1058 case 'G': /* get */
1059 case 'H': /* head */
1060 case 'P': /* put/post */
1061 case 'C': /* connect */
1062 strlcpy(req->reply,
1063 "HTTP/1.0 501 Tor is not an HTTP Proxy\r\n"
1064 "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
1065 "<html>\n"
1066 "<head>\n"
1067 "<title>Tor is not an HTTP Proxy</title>\n"
1068 "</head>\n"
1069 "<body>\n"
1070 "<h1>Tor is not an HTTP Proxy</h1>\n"
1071 "<p>\n"
1072 "It appears you have configured your web browser to use Tor as an HTTP Proxy.\n"
1073 "This is not correct: Tor provides a SOCKS proxy. Please configure your\n"
1074 "client accordingly.\n"
1075 "</p>\n"
1076 "<p>\n"
1077 "See <a href=\"http://tor.eff.org/documentation.html\">http://tor.eff.org/documentation.html</a> for more information.\n"
1078 "<!-- Plus this comment, to make the body response more than 512 bytes, so IE will be willing to display it. Comment comment comment comment comment comment comment comment comment comment comment comment.-->\n"
1079 "</p>\n"
1080 "</body>\n"
1081 "</html>\n"
1082 , MAX_SOCKS_REPLY_LEN);
1083 req->replylen = strlen(req->reply)+1;
1084 /* fall through */
1085 default: /* version is not socks4 or socks5 */
1086 log_fn(LOG_WARN,"Socks version %d not recognized. (Tor is not an http proxy.)",
1087 *(buf->cur));
1088 return -1;
1092 #define CONTROL_CMD_FRAGMENTHEADER 0x0010
1093 #define CONTROL_CMD_FRAGMENT 0x0011
1094 /** If there is a complete version 0 control message waiting on buf, then store
1095 * its contents into *<b>type_out</b>, store its body's length into
1096 * *<b>len_out</b>, allocate and store a string for its body into
1097 * *<b>body_out</b>, and return 1. (body_out will always be NUL-terminated,
1098 * even if the control message body doesn't end with NUL.)
1100 * If there is not a complete control message waiting, return 0.
1102 * Return -1 on error; return -2 on "seems to be control protocol v1."
1105 fetch_from_buf_control0(buf_t *buf, uint32_t *len_out, uint16_t *type_out,
1106 char **body_out, int check_for_v1)
1108 uint32_t msglen;
1109 uint16_t type;
1110 char tmp[4];
1112 tor_assert(buf);
1113 tor_assert(len_out);
1114 tor_assert(type_out);
1115 tor_assert(body_out);
1117 *len_out = 0;
1118 *body_out = NULL;
1120 if (buf->datalen < 4)
1121 return 0;
1123 peek_from_buf(tmp, 4, buf);
1125 msglen = ntohs(get_uint16(tmp));
1126 type = ntohs(get_uint16(tmp+2));
1127 if (type > 255 && check_for_v1)
1128 return -2;
1130 if (buf->datalen < 4 + (unsigned)msglen)
1131 return 0;
1133 *len_out = msglen;
1134 *type_out = type;
1135 buf_remove_from_front(buf, 4);
1136 if (msglen) {
1137 *body_out = tor_malloc(msglen+1);
1138 fetch_from_buf(*body_out, msglen, buf);
1139 (*body_out)[msglen] = '\0';
1141 return 1;
1144 /** Helper: return a pointer to the first instance of <b>c</b> in the
1145 * <b>len</b>characters after <b>start</b> on <b>buf</b>. Return NULL if the
1146 * character isn't found. */
1147 static char *
1148 find_char_on_buf(buf_t *buf, char *start, size_t len, char c)
1150 size_t len_rest;
1151 char *cp;
1152 _split_range(buf, start, &len, &len_rest);
1153 cp = memchr(buf->cur, c, len);
1154 if (cp || !len_rest)
1155 return cp;
1156 return memchr(buf->mem, c, len_rest);
1159 /** Helper: return a pointer to the first CRLF after cp on <b>buf</b>. Return
1160 * NULL if no CRLF is found. */
1161 static char *
1162 find_crlf_on_buf(buf_t *buf, char *cp)
1164 char *next;
1165 while (1) {
1166 size_t remaining = buf->datalen - _buf_offset(buf,cp);
1167 cp = find_char_on_buf(buf, cp, remaining, '\r');
1168 if (!cp)
1169 return NULL;
1170 next = _wrap_ptr(buf, cp+1);
1171 if (next == _buf_end(buf))
1172 return NULL;
1173 if (*next == '\n')
1174 return cp;
1175 cp = next;
1179 /** Try to read a single CRLF-terminated line from <b>buf</b>, and write it,
1180 * NUL-terminated, into the *<b>data_len</b> byte buffer at <b>data_out</b>.
1181 * Set *<b>data_len</b> to the number of bytes in the line, not counting the
1182 * terminating NUL. Return 1 if we read a whole line, return 0 if we don't
1183 * have a whole line yet, and return -1 if we we need to grow the buffer.
1186 fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len)
1188 char *eol;
1189 size_t sz;
1190 /* Look for a CRLF. */
1191 if (!(eol = find_crlf_on_buf(buf, buf->cur))) {
1192 return 0;
1194 sz = _buf_offset(buf, eol);
1195 if (sz+3 > *data_len) {
1196 *data_len = sz+3;
1197 return -1;
1199 fetch_from_buf(data_out, sz+2, buf);
1200 data_out[sz+2] = '\0';
1201 *data_len = sz+2;
1202 return 1;
1205 /** Log an error and exit if <b>buf</b> is corrupted.
1207 void
1208 assert_buf_ok(buf_t *buf)
1210 tor_assert(buf);
1211 tor_assert(buf->magic == BUFFER_MAGIC);
1212 tor_assert(buf->mem);
1213 tor_assert(buf->highwater <= buf->len);
1214 tor_assert(buf->datalen <= buf->highwater);
1215 #ifdef SENTINELS
1217 uint32_t u32 = get_uint32(buf->mem - 4);
1218 tor_assert(u32 == START_MAGIC);
1219 u32 = get_uint32(buf->mem + buf->len);
1220 tor_assert(u32 == END_MAGIC);
1222 #endif