1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2007, Roger Dingledine, Nick Mathewson. */
4 /* See LICENSE for licensing information */
6 const char buffers_c_id
[] =
11 * \brief Implements a generic buffer interface. Buffers are
12 * fairly opaque string holders that can read to or flush from:
13 * memory, file descriptors, or TLS connections.
19 #undef CHECK_AFTER_RESIZE
23 /* If SENTINELS is defined, check for attempts to write beyond the
24 * end/before the start of the buffer.
27 /** 4-byte value to write at the start of each buffer memory region. */
28 #define START_MAGIC 0x70370370u
29 /** 4-byte value to write at the end of each buffer memory region. */
30 #define END_MAGIC 0xA0B0C0D0u
31 /** Given buf->mem, yield a pointer to the raw memory region (for free(),
32 * realloc(), and so on). */
33 #define RAW_MEM(m) ((void*)(((char*)m)-4))
34 /** Given a pointer to the raw memory region (from malloc() or realloc()),
35 * yield the correct value for buf->mem (just past the first sentinel). */
36 #define GUARDED_MEM(m) ((void*)(((char*)m)+4))
37 /** How much memory do we need to allocate for a buffer to hold <b>ln</b> bytes
39 #define ALLOC_LEN(ln) ((ln)+8)
40 /** Initialize the sentinel values on <b>m</b> (a value of buf->mem), which
41 * has <b>ln</b> useful bytes. */
42 #define SET_GUARDS(m, ln) \
43 do { set_uint32((m)-4,START_MAGIC); set_uint32((m)+ln,END_MAGIC); } while (0)
45 #define RAW_MEM(m) (m)
46 #define GUARDED_MEM(m) (m)
47 #define ALLOC_LEN(ln) (ln)
48 #define SET_GUARDS(m,ln) do {} while (0)
52 #define check() do { assert_buf_ok(buf); } while (0)
54 #define check() do { } while (0)
62 /** Magic value for buf_t.magic, to catch pointer errors. */
63 #define BUFFER_MAGIC 0xB0FFF312u
64 /** A resizeable buffer, optimized for reading and writing. */
66 uint32_t magic
; /**< Magic cookie for debugging: Must be set to
68 char *mem
; /**< Storage for data in the buffer. */
69 char *cur
; /**< The first byte used for storing data in the buffer. */
70 size_t highwater
; /**< Largest observed datalen since last buf_shrink. */
71 size_t len
; /**< Maximum amount of data that <b>mem</b> can hold. */
72 size_t memsize
; /**< How many bytes did we actually allocate? Can be less
73 * than 'len' if we shortened 'len' by a few bytes to make
74 * zlib wrap around more easily. */
75 size_t datalen
; /**< Number of bytes currently in <b>mem</b>. */
78 /** How many bytes, total, are used in all buffers? */
79 uint64_t buf_total_used
= 0;
80 /** How many bytes, total, are allocated in all buffers? */
81 uint64_t buf_total_alloc
= 0;
83 /** Size, in bytes, for newly allocated buffers. Should be a power of 2. */
84 #define INITIAL_BUF_SIZE (4*1024)
85 /** Size, in bytes, for minimum 'shrink' size for buffers. Buffers may start
86 * out smaller than this, but they will never autoshrink to less
88 #define MIN_LAZY_SHRINK_SIZE (4*1024)
90 static INLINE
void peek_from_buf(char *string
, size_t string_len
, buf_t
*buf
);
92 /** If the contents of buf wrap around the end of the allocated space,
93 * malloc a new buf and copy the contents in starting at the
94 * beginning. This operation is relatively expensive, so it shouldn't
95 * be used e.g. for every single read or write.
98 buf_normalize(buf_t
*buf
)
101 if (buf
->cur
+ buf
->datalen
<= buf
->mem
+buf
->len
) {
104 char *newmem
, *oldmem
;
105 size_t sz
= (buf
->mem
+buf
->len
)-buf
->cur
;
106 log_warn(LD_BUG
, "Unexpected non-normalized buffer.");
107 newmem
= GUARDED_MEM(tor_malloc(ALLOC_LEN(buf
->memsize
)));
108 SET_GUARDS(newmem
, buf
->memsize
);
109 memcpy(newmem
, buf
->cur
, sz
);
110 memcpy(newmem
+sz
, buf
->mem
, buf
->datalen
-sz
);
111 oldmem
= RAW_MEM(buf
->mem
);
112 tor_free(oldmem
); /* Can't use tor_free directly. */
113 buf
->mem
= buf
->cur
= newmem
;
114 buf
->len
= buf
->memsize
;
119 /** Return the point in the buffer where the next byte will get stored. */
123 char *next
= buf
->cur
+ buf
->datalen
;
124 char *end
= buf
->mem
+ buf
->len
;
125 return (next
< end
) ? next
: (next
- buf
->len
);
128 /** If the pointer <b>cp</b> has passed beyond the end of the buffer, wrap it
131 _wrap_ptr(buf_t
*buf
, char *cp
)
133 return (cp
>= buf
->mem
+ buf
->len
) ? (cp
- buf
->len
) : cp
;
136 /** Return the offset of <b>cp</b> within the buffer. */
138 _buf_offset(buf_t
*buf
, char *cp
)
141 return cp
- buf
->cur
;
143 /* return (cp - buf->mem) + buf->mem+buf->len - buf->cur */
144 return cp
+ buf
->len
- buf
->cur
;
147 /** If the range of *<b>len</b> bytes starting at <b>at</b> wraps around the
148 * end of the buffer, then set *<b>len</b> to the number of bytes starting
149 * at <b>at</b>, and set *<b>more_len</b> to the number of bytes starting
150 * at <b>buf->mem</b>. Otherwise, set *<b>more_len</b> to 0.
153 _split_range(buf_t
*buf
, char *at
, size_t *len
,
156 char *eos
= at
+ *len
;
158 if (eos
>= (buf
->mem
+ buf
->len
)) {
159 *more_len
= eos
- (buf
->mem
+ buf
->len
);
166 /** Change a buffer's capacity. <b>new_capacity</b> must be \>=
169 buf_resize(buf_t
*buf
, size_t new_capacity
)
172 #ifdef CHECK_AFTER_RESIZE
175 tor_assert(buf
->datalen
<= new_capacity
);
176 tor_assert(new_capacity
);
178 #ifdef CHECK_AFTER_RESIZE
180 tmp
= tor_malloc(buf
->datalen
);
181 tmp2
= tor_malloc(buf
->datalen
);
182 peek_from_buf(tmp
, buf
->datalen
, buf
);
185 if (buf
->len
== new_capacity
)
188 offset
= buf
->cur
- buf
->mem
;
189 if (offset
+ buf
->datalen
> new_capacity
) {
190 /* We need to move stuff before we shrink. */
191 if (offset
+ buf
->datalen
> buf
->len
) {
194 * mem[0] ... mem[datalen-(len-offset)] (end of data)
195 * mem[offset] ... mem[len-1] (the start of the data)
197 * We're shrinking the buffer by (len-new_capacity) bytes, so we need
198 * to move the start portion back by that many bytes.
200 memmove(buf
->cur
-(buf
->len
-new_capacity
), buf
->cur
,
201 (size_t)(buf
->len
-offset
));
202 offset
-= (buf
->len
-new_capacity
);
204 /* The data doesn't wrap around, but it does extend beyond the new
206 * mem[offset] ... mem[offset+datalen-1] (the data)
208 memmove(buf
->mem
, buf
->cur
, buf
->datalen
);
213 /* XXX Some play code to throw away old buffers sometimes rather
214 * than constantly reallocing them; just in case this is our memory
215 * problem. It looks for now like it isn't, so disabled. -RD */
216 if (0 && new_capacity
== MIN_LAZY_SHRINK_SIZE
&&
219 /* don't realloc; free and malloc */
220 char *oldmem
, *newmem
= GUARDED_MEM(tor_malloc(ALLOC_LEN(new_capacity
)));
221 SET_GUARDS(newmem
, new_capacity
);
222 oldmem
= RAW_MEM(buf
->mem
);
224 buf
->mem
= buf
->cur
= newmem
;
226 buf
->mem
= GUARDED_MEM(tor_realloc(RAW_MEM(buf
->mem
),
227 ALLOC_LEN(new_capacity
)));
228 SET_GUARDS(buf
->mem
, new_capacity
);
229 buf
->cur
= buf
->mem
+offset
;
231 buf_total_alloc
+= new_capacity
;
232 buf_total_alloc
-= buf
->len
;
234 if (offset
+ buf
->datalen
> buf
->len
) {
235 /* We need to move data now that we are done growing. The buffer
238 * mem[0] ... mem[datalen-(len-offset)] (end of data)
239 * mem[offset] ... mem[len-1] (the start of the data)
240 * mem[len]...mem[new_capacity] (empty space)
242 * We're growing by (new_capacity-len) bytes, so we need to move the
243 * end portion forward by that many bytes.
245 memmove(buf
->cur
+(new_capacity
-buf
->len
), buf
->cur
,
246 (size_t)(buf
->len
-offset
));
247 buf
->cur
+= new_capacity
-buf
->len
;
249 buf
->memsize
= buf
->len
= new_capacity
;
251 #ifdef CHECK_AFTER_RESIZE
253 peek_from_buf(tmp2
, buf
->datalen
, buf
);
254 if (memcmp(tmp
, tmp2
, buf
->datalen
)) {
262 /** If the buffer is not large enough to hold <b>capacity</b> bytes, resize
263 * it so that it can. (The new size will be a power of 2 times the old
267 buf_ensure_capacity(buf_t
*buf
, size_t capacity
)
269 size_t new_len
, min_len
;
270 if (buf
->len
>= capacity
) /* Don't grow if we're already big enough. */
272 if (capacity
> MAX_BUF_SIZE
) /* Don't grow past the maximum. */
274 /* Find the smallest new_len equal to (2**X) for some X; such that
275 * new_len is at least capacity, and at least 2*buf->len.
277 min_len
= buf
->len
*2;
279 while (new_len
< min_len
)
281 while (new_len
< capacity
)
283 /* Resize the buffer. */
284 log_debug(LD_MM
,"Growing buffer from %d to %d bytes.",
285 (int)buf
->len
, (int)new_len
);
286 buf_resize(buf
,new_len
);
290 /** Resize buf so it won't hold extra memory that we haven't been
291 * using lately (that is, since the last time we called buf_shrink).
292 * Try to shrink the buf until it is the largest factor of two that
293 * can contain <b>buf</b>->highwater, but never smaller than
294 * MIN_LAZY_SHRINK_SIZE.
297 buf_shrink(buf_t
*buf
)
302 while (buf
->highwater
< (new_len
>>2) && new_len
> MIN_LAZY_SHRINK_SIZE
*2)
305 buf
->highwater
= buf
->datalen
;
306 if (new_len
== buf
->len
)
309 log_debug(LD_MM
,"Shrinking buffer from %d to %d bytes.",
310 (int)buf
->len
, (int)new_len
);
311 buf_resize(buf
, new_len
);
314 /** Remove the first <b>n</b> bytes from buf. */
316 buf_remove_from_front(buf_t
*buf
, size_t n
)
318 tor_assert(buf
->datalen
>= n
);
322 buf
->cur
= _wrap_ptr(buf
, buf
->cur
+n
);
329 /** Make sure that the memory in buf ends with a zero byte. */
331 buf_nul_terminate(buf_t
*buf
)
333 if (buf_ensure_capacity(buf
,buf
->datalen
+1)<0)
335 *_buf_end(buf
) = '\0';
339 /** Create and return a new buf with capacity <b>size</b>. */
341 buf_new_with_capacity(size_t size
)
344 buf
= tor_malloc_zero(sizeof(buf_t
));
345 buf
->magic
= BUFFER_MAGIC
;
346 buf
->cur
= buf
->mem
= GUARDED_MEM(tor_malloc(ALLOC_LEN(size
)));
347 SET_GUARDS(buf
->mem
, size
);
348 buf
->len
= buf
->memsize
= size
;
350 buf_total_alloc
+= size
;
355 /** Allocate and return a new buffer with default capacity. */
359 return buf_new_with_capacity(INITIAL_BUF_SIZE
);
362 /** Remove all data from <b>buf</b>. */
364 buf_clear(buf_t
*buf
)
366 buf_total_used
-= buf
->datalen
;
369 buf
->len
= buf
->memsize
;
372 /** Return the number of bytes stored in <b>buf</b> */
374 buf_datalen(const buf_t
*buf
)
379 /** Return the maximum bytes that can be stored in <b>buf</b> before buf
380 * needs to resize. */
382 buf_capacity(const buf_t
*buf
)
387 /** For testing only: Return a pointer to the raw memory stored in
390 _buf_peek_raw_buffer(const buf_t
*buf
)
395 /** Release storage held by <b>buf</b>. */
401 buf
->magic
= 0xDEADBEEF;
402 oldmem
= RAW_MEM(buf
->mem
);
404 buf_total_alloc
-= buf
->len
;
405 buf_total_used
-= buf
->datalen
;
409 /** Helper for read_to_buf(): read no more than at_most bytes from
410 * socket s into buffer buf, starting at the position pos. (Does not
411 * check for overflow.) Set *reached_eof to true on EOF. Return
412 * number of bytes read on success, 0 if the read would block, -1 on
416 read_to_buf_impl(int s
, size_t at_most
, buf_t
*buf
,
417 char *pos
, int *reached_eof
)
421 // log_fn(LOG_DEBUG,"reading at most %d bytes.",at_most);
422 read_result
= tor_socket_recv(s
, pos
, at_most
, 0);
423 if (read_result
< 0) {
424 int e
= tor_socket_errno(s
);
425 if (!ERRNO_IS_EAGAIN(e
)) { /* it's a real error */
428 log_warn(LD_NET
,"recv() failed: WSAENOBUFS. Not enough ram?");
432 return 0; /* would block. */
433 } else if (read_result
== 0) {
434 log_debug(LD_NET
,"Encountered eof");
437 } else { /* we read some bytes */
438 buf
->datalen
+= read_result
;
439 buf_total_used
+= read_result
;
440 if (buf
->datalen
> buf
->highwater
)
441 buf
->highwater
= buf
->datalen
;
442 log_debug(LD_NET
,"Read %d bytes. %d on inbuf.",read_result
,
448 /** Read from socket <b>s</b>, writing onto end of <b>buf</b>. Read at most
449 * <b>at_most</b> bytes, resizing the buffer as necessary. If recv()
450 * returns 0, set *<b>reached_eof</b> to 1 and return 0. Return -1 on error;
451 * else return the number of bytes read. Return 0 if recv() would
455 read_to_buf(int s
, size_t at_most
, buf_t
*buf
, int *reached_eof
)
461 /* assert_buf_ok(buf); */
462 tor_assert(reached_eof
);
465 if (buf_ensure_capacity(buf
,buf
->datalen
+at_most
))
468 if (at_most
+ buf
->datalen
> buf
->len
)
469 at_most
= buf
->len
- buf
->datalen
; /* take the min of the two */
472 return 0; /* we shouldn't read anything */
474 next
= _buf_end(buf
);
475 _split_range(buf
, next
, &at_most
, &at_start
);
477 r
= read_to_buf_impl(s
, at_most
, buf
, next
, reached_eof
);
479 if (r
< 0 || (size_t)r
< at_most
) {
480 return r
; /* Either error, eof, block, or no more to read. */
485 tor_assert(_buf_end(buf
) == buf
->mem
);
486 r2
= read_to_buf_impl(s
, at_start
, buf
, buf
->mem
, reached_eof
);
497 /** Helper for read_to_buf_tls(): read no more than <b>at_most</b>
498 * bytes from the TLS connection <b>tls</b> into buffer <b>buf</b>,
499 * starting at the position <b>next</b>. (Does not check for overflow.)
500 * Return number of bytes read on success, 0 if the read would block,
504 read_to_buf_tls_impl(tor_tls_t
*tls
, size_t at_most
, buf_t
*buf
, char *next
)
508 log_debug(LD_NET
,"before: %d on buf, %d pending, at_most %d.",
509 (int)buf_datalen(buf
), (int)tor_tls_get_pending_bytes(tls
),
511 r
= tor_tls_read(tls
, next
, at_most
);
516 if (buf
->datalen
> buf
->highwater
)
517 buf
->highwater
= buf
->datalen
;
518 log_debug(LD_NET
,"Read %d bytes. %d on inbuf; %d pending",r
,
519 (int)buf
->datalen
,(int)tor_tls_get_pending_bytes(tls
));
523 /** As read_to_buf, but reads from a TLS connection.
525 * Using TLS on OR connections complicates matters in two ways.
527 * First, a TLS stream has its own read buffer independent of the
528 * connection's read buffer. (TLS needs to read an entire frame from
529 * the network before it can decrypt any data. Thus, trying to read 1
530 * byte from TLS can require that several KB be read from the network
531 * and decrypted. The extra data is stored in TLS's decrypt buffer.)
532 * Because the data hasn't been read by Tor (it's still inside the TLS),
533 * this means that sometimes a connection "has stuff to read" even when
534 * poll() didn't return POLLIN. The tor_tls_get_pending_bytes function is
535 * used in connection.c to detect TLS objects with non-empty internal
536 * buffers and read from them again.
538 * Second, the TLS stream's events do not correspond directly to network
539 * events: sometimes, before a TLS stream can read, the network must be
540 * ready to write -- or vice versa.
543 read_to_buf_tls(tor_tls_t
*tls
, size_t at_most
, buf_t
*buf
)
552 log_debug(LD_NET
,"start: %d on buf, %d pending, at_most %d.",
553 (int)buf_datalen(buf
), (int)tor_tls_get_pending_bytes(tls
),
556 if (buf_ensure_capacity(buf
, at_most
+buf
->datalen
))
557 return TOR_TLS_ERROR_MISC
;
559 if (at_most
+ buf
->datalen
> buf
->len
)
560 at_most
= buf
->len
- buf
->datalen
;
565 next
= _buf_end(buf
);
566 _split_range(buf
, next
, &at_most
, &at_start
);
568 r
= read_to_buf_tls_impl(tls
, at_most
, buf
, next
);
570 if (r
< 0 || (size_t)r
< at_most
)
571 return r
; /* Either error, eof, block, or no more to read. */
575 tor_assert(_buf_end(buf
) == buf
->mem
);
576 r2
= read_to_buf_tls_impl(tls
, at_start
, buf
, buf
->mem
);
586 /** Helper for flush_buf(): try to write <b>sz</b> bytes from buffer
587 * <b>buf</b> onto socket <b>s</b>. On success, deduct the bytes written
588 * from *<b>buf_flushlen</b>.
589 * Return the number of bytes written on success, -1 on failure.
592 flush_buf_impl(int s
, buf_t
*buf
, size_t sz
, size_t *buf_flushlen
)
596 write_result
= tor_socket_send(s
, buf
->cur
, sz
, 0);
597 if (write_result
< 0) {
598 int e
= tor_socket_errno(s
);
599 if (!ERRNO_IS_EAGAIN(e
)) { /* it's a real error */
602 log_warn(LD_NET
,"write() failed: WSAENOBUFS. Not enough ram?");
606 log_debug(LD_NET
,"write() would block, returning.");
609 *buf_flushlen
-= write_result
;
610 buf_remove_from_front(buf
, write_result
);
615 /** Write data from <b>buf</b> to the socket <b>s</b>. Write at most
616 * <b>sz</b> bytes, decrement *<b>buf_flushlen</b> by
617 * the number of bytes actually written, and remove the written bytes
618 * from the buffer. Return the number of bytes written on success,
619 * -1 on failure. Return 0 if write() would block.
622 flush_buf(int s
, buf_t
*buf
, size_t sz
, size_t *buf_flushlen
)
626 size_t flushlen0
, flushlen1
;
628 /* assert_buf_ok(buf); */
629 tor_assert(buf_flushlen
);
631 tor_assert(*buf_flushlen
<= buf
->datalen
);
632 tor_assert(sz
<= *buf_flushlen
);
634 if (sz
== 0) /* nothing to flush */
638 _split_range(buf
, buf
->cur
, &flushlen0
, &flushlen1
);
640 r
= flush_buf_impl(s
, buf
, flushlen0
, buf_flushlen
);
643 log_debug(LD_NET
,"%d: flushed %d bytes, %d ready to flush, %d remain.",
644 s
,r
,(int)*buf_flushlen
,(int)buf
->datalen
);
645 if (r
< 0 || (size_t)r
< flushlen0
)
646 return r
; /* Error, or can't flush any more now. */
650 tor_assert(buf
->cur
== buf
->mem
);
651 r
= flush_buf_impl(s
, buf
, flushlen1
, buf_flushlen
);
653 log_debug(LD_NET
,"%d: flushed %d bytes, %d ready to flush, %d remain.",
654 s
,r
,(int)*buf_flushlen
,(int)buf
->datalen
);
662 /** Helper for flush_buf_tls(): try to write <b>sz</b> bytes (or more if
663 * required by a previous write) from buffer <b>buf</b> onto TLS object
664 * <b>tls</b>. On success, deduct the bytes written from
665 * *<b>buf_flushlen</b>. Return the number of bytes written on success, -1 on
669 flush_buf_tls_impl(tor_tls_t
*tls
, buf_t
*buf
, size_t sz
, size_t *buf_flushlen
)
674 forced
= tor_tls_get_forced_write_size(tls
);
677 r
= tor_tls_write(tls
, buf
->cur
, sz
);
682 buf_remove_from_front(buf
, r
);
683 log_debug(LD_NET
,"flushed %d bytes, %d ready to flush, %d remain.",
684 r
,(int)*buf_flushlen
,(int)buf
->datalen
);
688 /** As flush_buf(), but writes data to a TLS connection.
691 flush_buf_tls(tor_tls_t
*tls
, buf_t
*buf
, size_t sz
, size_t *buf_flushlen
)
695 size_t flushlen0
, flushlen1
;
696 /* assert_buf_ok(buf); */
698 tor_assert(buf_flushlen
);
699 tor_assert(*buf_flushlen
<= buf
->datalen
);
700 tor_assert(sz
<= *buf_flushlen
);
702 /* we want to let tls write even if flushlen is zero, because it might
703 * have a partial record pending */
704 check_no_tls_errors();
707 _split_range(buf
, buf
->cur
, &flushlen0
, &flushlen1
);
709 size_t forced
= tor_tls_get_forced_write_size(tls
);
710 tor_assert(forced
<= flushlen0
);
713 r
= flush_buf_tls_impl(tls
, buf
, flushlen0
, buf_flushlen
);
715 if (r
< 0 || (size_t)r
< flushlen0
)
716 return r
; /* Error, or can't flush any more now. */
720 tor_assert(buf
->cur
== buf
->mem
);
721 r
= flush_buf_tls_impl(tls
, buf
, flushlen1
, buf_flushlen
);
730 /** Append <b>string_len</b> bytes from <b>string</b> to the end of
733 * Return the new length of the buffer on success, -1 on failure.
736 write_to_buf(const char *string
, size_t string_len
, buf_t
*buf
)
741 /* append string to buf (growing as needed, return -1 if "too big")
742 * return total number of bytes on the buf
746 /* assert_buf_ok(buf); */
748 if (buf_ensure_capacity(buf
, buf
->datalen
+string_len
)) {
749 log_warn(LD_MM
, "buflen too small, can't hold %d bytes.",
750 (int)(buf
->datalen
+string_len
));
754 next
= _buf_end(buf
);
755 _split_range(buf
, next
, &string_len
, &len2
);
757 memcpy(next
, string
, string_len
);
758 buf
->datalen
+= string_len
;
759 buf_total_used
+= string_len
;
762 tor_assert(_buf_end(buf
) == buf
->mem
);
763 memcpy(buf
->mem
, string
+string_len
, len2
);
764 buf
->datalen
+= len2
;
765 buf_total_used
+= len2
;
767 if (buf
->datalen
> buf
->highwater
)
768 buf
->highwater
= buf
->datalen
;
769 log_debug(LD_NET
,"added %d bytes to buf (now %d total).",
770 (int)string_len
, (int)buf
->datalen
);
775 /** Helper: copy the first <b>string_len</b> bytes from <b>buf</b>
776 * onto <b>string</b>.
779 peek_from_buf(char *string
, size_t string_len
, buf_t
*buf
)
783 /* There must be string_len bytes in buf; write them onto string,
784 * then memmove buf back (that is, remove them from buf).
786 * Return the number of bytes still on the buffer. */
789 /* make sure we don't ask for too much */
790 tor_assert(string_len
<= buf
->datalen
);
791 /* assert_buf_ok(buf); */
793 _split_range(buf
, buf
->cur
, &string_len
, &len2
);
795 memcpy(string
, buf
->cur
, string_len
);
797 memcpy(string
+string_len
,buf
->mem
,len2
);
801 /** Remove <b>string_len</b> bytes from the front of <b>buf</b>, and store
802 * them into <b>string</b>. Return the new buffer size. <b>string_len</b>
803 * must be \<= the number of bytes on the buffer.
806 fetch_from_buf(char *string
, size_t string_len
, buf_t
*buf
)
808 /* There must be string_len bytes in buf; write them onto string,
809 * then memmove buf back (that is, remove them from buf).
811 * Return the number of bytes still on the buffer. */
814 peek_from_buf(string
, string_len
, buf
);
815 buf_remove_from_front(buf
, string_len
);
820 /** There is a (possibly incomplete) http statement on <b>buf</b>, of the
821 * form "\%s\\r\\n\\r\\n\%s", headers, body. (body may contain nuls.)
822 * If a) the headers include a Content-Length field and all bytes in
823 * the body are present, or b) there's no Content-Length field and
824 * all headers are present, then:
826 * - strdup headers into <b>*headers_out</b>, and nul-terminate it.
827 * - memdup body into <b>*body_out</b>, and nul-terminate it.
828 * - Then remove them from <b>buf</b>, and return 1.
830 * - If headers or body is NULL, discard that part of the buf.
831 * - If a headers or body doesn't fit in the arg, return -1.
832 * (We ensure that the headers or body don't exceed max len,
833 * _even if_ we're planning to discard them.)
834 * - If force_complete is true, then succeed even if not all of the
835 * content has arrived.
837 * Else, change nothing and return 0.
840 fetch_from_buf_http(buf_t
*buf
,
841 char **headers_out
, size_t max_headerlen
,
842 char **body_out
, size_t *body_used
, size_t max_bodylen
,
845 char *headers
, *body
, *p
;
846 size_t headerlen
, bodylen
, contentlen
;
848 /* assert_buf_ok(buf); */
851 if (buf_nul_terminate(buf
)<0) {
852 log_warn(LD_BUG
,"Couldn't nul-terminate buffer");
856 body
= strstr(headers
,"\r\n\r\n");
858 log_debug(LD_HTTP
,"headers not all here yet.");
861 body
+= 4; /* Skip the the CRLFCRLF */
862 headerlen
= body
-headers
; /* includes the CRLFCRLF */
863 bodylen
= buf
->datalen
- headerlen
;
864 log_debug(LD_HTTP
,"headerlen %d, bodylen %d.", (int)headerlen
, (int)bodylen
);
866 if (max_headerlen
<= headerlen
) {
867 log_warn(LD_HTTP
,"headerlen %d larger than %d. Failing.",
868 (int)headerlen
, (int)max_headerlen
-1);
871 if (max_bodylen
<= bodylen
) {
872 log_warn(LD_HTTP
,"bodylen %d larger than %d. Failing.",
873 (int)bodylen
, (int)max_bodylen
-1);
877 #define CONTENT_LENGTH "\r\nContent-Length: "
878 p
= strstr(headers
, CONTENT_LENGTH
);
881 i
= atoi(p
+strlen(CONTENT_LENGTH
));
883 log_warn(LD_PROTOCOL
, "Content-Length is less than zero; it looks like "
884 "someone is trying to crash us.");
888 /* if content-length is malformed, then our body length is 0. fine. */
889 log_debug(LD_HTTP
,"Got a contentlen of %d.",(int)contentlen
);
890 if (bodylen
< contentlen
) {
891 if (!force_complete
) {
892 log_debug(LD_HTTP
,"body not all here yet.");
893 return 0; /* not all there yet */
896 if (bodylen
> contentlen
) {
897 bodylen
= contentlen
;
898 log_debug(LD_HTTP
,"bodylen reduced to %d.",(int)bodylen
);
901 /* all happy. copy into the appropriate places, and return 1 */
903 *headers_out
= tor_malloc(headerlen
+1);
904 memcpy(*headers_out
,buf
->cur
,headerlen
);
905 (*headers_out
)[headerlen
] = 0; /* nul terminate it */
908 tor_assert(body_used
);
909 *body_used
= bodylen
;
910 *body_out
= tor_malloc(bodylen
+1);
911 memcpy(*body_out
,buf
->cur
+headerlen
,bodylen
);
912 (*body_out
)[bodylen
] = 0; /* nul terminate it */
914 buf_remove_from_front(buf
, headerlen
+bodylen
);
918 /** There is a (possibly incomplete) socks handshake on <b>buf</b>, of one
920 * - socks4: "socksheader username\\0"
921 * - socks4a: "socksheader username\\0 destaddr\\0"
922 * - socks5 phase one: "version #methods methods"
923 * - socks5 phase two: "version command 0 addresstype..."
924 * If it's a complete and valid handshake, and destaddr fits in
925 * MAX_SOCKS_ADDR_LEN bytes, then pull the handshake off the buf,
926 * assign to <b>req</b>, and return 1.
928 * If it's invalid or too big, return -1.
930 * Else it's not all there yet, leave buf alone and return 0.
932 * If you want to specify the socks reply, write it into <b>req->reply</b>
933 * and set <b>req->replylen</b>, else leave <b>req->replylen</b> alone.
935 * If <b>log_sockstype</b> is non-zero, then do a notice-level log of whether
936 * the connection is possibly leaking DNS requests locally or not.
938 * If <b>safe_socks</b> is true, then reject unsafe socks protocols.
940 * If returning 0 or -1, <b>req->address</b> and <b>req->port</b> are
944 fetch_from_buf_socks(buf_t
*buf
, socks_request_t
*req
,
945 int log_sockstype
, int safe_socks
)
948 char tmpbuf
[INET_NTOA_BUF_LEN
];
950 enum {socks4
, socks4a
} socks4_prot
= socks4a
;
951 char *next
, *startaddr
;
954 /* If the user connects with socks4 or the wrong variant of socks5,
955 * then log a warning to let him know that it might be unwise. */
956 static int have_warned_about_unsafe_socks
= 0;
958 if (buf
->datalen
< 2) /* version and another byte */
962 switch (*(buf
->cur
)) { /* which version of socks? */
966 if (req
->socks_version
!= 5) { /* we need to negotiate a method */
967 unsigned char nummethods
= (unsigned char)*(buf
->cur
+1);
968 tor_assert(!req
->socks_version
);
969 if (buf
->datalen
< 2u+nummethods
)
971 if (!nummethods
|| !memchr(buf
->cur
+2, 0, nummethods
)) {
973 "socks5: offered methods don't include 'no auth'. "
975 req
->replylen
= 2; /* 2 bytes of response */
977 req
->reply
[1] = '\xFF'; /* reject all methods */
980 /* remove packet from buf. also remove any other extraneous
981 * bytes, to support broken socks clients. */
984 req
->replylen
= 2; /* 2 bytes of response */
985 req
->reply
[0] = 5; /* socks5 reply */
986 req
->reply
[1] = SOCKS5_SUCCEEDED
;
987 req
->socks_version
= 5; /* remember we've already negotiated auth */
988 log_debug(LD_APP
,"socks5: accepted method 0");
991 /* we know the method; read in the request */
992 log_debug(LD_APP
,"socks5: checking request");
993 if (buf
->datalen
< 8) /* basic info plus >=2 for addr plus 2 for port */
994 return 0; /* not yet */
995 req
->command
= (unsigned char) *(buf
->cur
+1);
996 if (req
->command
!= SOCKS_COMMAND_CONNECT
&&
997 req
->command
!= SOCKS_COMMAND_CONNECT_DIR
&&
998 req
->command
!= SOCKS_COMMAND_RESOLVE
&&
999 req
->command
!= SOCKS_COMMAND_RESOLVE_PTR
) {
1000 /* not a connect or resolve or a resolve_ptr? we don't support it. */
1001 log_warn(LD_APP
,"socks5: command %d not recognized. Rejecting.",
1005 switch (*(buf
->cur
+3)) { /* address type */
1006 case 1: /* IPv4 address */
1007 log_debug(LD_APP
,"socks5: ipv4 address type");
1008 if (buf
->datalen
< 10) /* ip/port there? */
1009 return 0; /* not yet */
1011 destip
= ntohl(*(uint32_t*)(buf
->cur
+4));
1012 in
.s_addr
= htonl(destip
);
1013 tor_inet_ntoa(&in
,tmpbuf
,sizeof(tmpbuf
));
1014 if (strlen(tmpbuf
)+1 > MAX_SOCKS_ADDR_LEN
) {
1016 "socks5 IP takes %d bytes, which doesn't fit in %d. "
1018 (int)strlen(tmpbuf
)+1,(int)MAX_SOCKS_ADDR_LEN
);
1021 strlcpy(req
->address
,tmpbuf
,sizeof(req
->address
));
1022 req
->port
= ntohs(*(uint16_t*)(buf
->cur
+8));
1023 buf_remove_from_front(buf
, 10);
1024 if (req
->command
!= SOCKS_COMMAND_RESOLVE_PTR
&&
1025 !addressmap_have_mapping(req
->address
) &&
1026 !have_warned_about_unsafe_socks
) {
1028 "Your application (using socks5 on port %d) is giving "
1029 "Tor only an IP address. Applications that do DNS resolves "
1030 "themselves may leak information. Consider using Socks4A "
1031 "(e.g. via privoxy or socat) instead. For more information, "
1032 "please see http://wiki.noreply.org/noreply/TheOnionRouter/"
1033 "TorFAQ#SOCKSAndDNS.%s", req
->port
,
1034 safe_socks
? " Rejecting." : "");
1035 // have_warned_about_unsafe_socks = 1; // (for now, warn every time)
1036 control_event_client_status(LOG_WARN
,
1037 "DANGEROUS_SOCKS PROTOCOL=SOCKS5 ADDRESS=%s:%d",
1038 req
->address
, req
->port
);
1044 log_debug(LD_APP
,"socks5: fqdn address type");
1045 len
= (unsigned char)*(buf
->cur
+4);
1046 if (buf
->datalen
< 7u+len
) /* addr/port there? */
1047 return 0; /* not yet */
1048 if (len
+1 > MAX_SOCKS_ADDR_LEN
) {
1050 "socks5 hostname is %d bytes, which doesn't fit in "
1051 "%d. Rejecting.", len
+1,MAX_SOCKS_ADDR_LEN
);
1054 if (req
->command
== SOCKS_COMMAND_RESOLVE_PTR
) {
1055 log_warn(LD_APP
, "socks5 received RESOLVE_PTR command with "
1056 "hostname type. Rejecting.");
1059 memcpy(req
->address
,buf
->cur
+5,len
);
1060 req
->address
[len
] = 0;
1061 req
->port
= ntohs(get_uint16(buf
->cur
+5+len
));
1062 buf_remove_from_front(buf
, 5+len
+2);
1063 if (!tor_strisprint(req
->address
) || strchr(req
->address
,'\"')) {
1064 log_warn(LD_PROTOCOL
,
1065 "Your application (using socks5 on port %d) gave Tor "
1066 "a malformed hostname: %s. Rejecting the connection.",
1067 req
->port
, escaped(req
->address
));
1072 "Your application (using socks5 on port %d) gave "
1073 "Tor a hostname, which means Tor will do the DNS resolve "
1074 "for you. This is good.", req
->port
);
1076 default: /* unsupported */
1077 log_warn(LD_APP
,"socks5: unsupported address type %d. Rejecting.",
1082 case 4: /* socks4 */
1083 /* http://archive.socks.permeo.com/protocol/socks4.protocol */
1084 /* http://archive.socks.permeo.com/protocol/socks4a.protocol */
1086 req
->socks_version
= 4;
1087 if (buf
->datalen
< SOCKS4_NETWORK_LEN
) /* basic info available? */
1088 return 0; /* not yet */
1090 req
->command
= (unsigned char) *(buf
->cur
+1);
1091 if (req
->command
!= SOCKS_COMMAND_CONNECT
&&
1092 req
->command
!= SOCKS_COMMAND_CONNECT_DIR
&&
1093 req
->command
!= SOCKS_COMMAND_RESOLVE
) {
1094 /* not a connect or resolve? we don't support it. (No resolve_ptr with
1096 log_warn(LD_APP
,"socks4: command %d not recognized. Rejecting.",
1101 req
->port
= ntohs(*(uint16_t*)(buf
->cur
+2));
1102 destip
= ntohl(*(uint32_t*)(buf
->mem
+4));
1103 if ((!req
->port
&& req
->command
!=SOCKS_COMMAND_RESOLVE
) || !destip
) {
1104 log_warn(LD_APP
,"socks4: Port or DestIP is zero. Rejecting.");
1108 log_debug(LD_APP
,"socks4: destip not in form 0.0.0.x.");
1109 in
.s_addr
= htonl(destip
);
1110 tor_inet_ntoa(&in
,tmpbuf
,sizeof(tmpbuf
));
1111 if (strlen(tmpbuf
)+1 > MAX_SOCKS_ADDR_LEN
) {
1112 log_debug(LD_APP
,"socks4 addr (%d bytes) too long. Rejecting.",
1113 (int)strlen(tmpbuf
));
1117 "socks4: successfully read destip (%s)", safe_str(tmpbuf
));
1118 socks4_prot
= socks4
;
1121 next
= memchr(buf
->cur
+SOCKS4_NETWORK_LEN
, 0,
1122 buf
->datalen
-SOCKS4_NETWORK_LEN
);
1124 log_debug(LD_APP
,"socks4: Username not here yet.");
1127 tor_assert(next
< buf
->cur
+buf
->datalen
);
1130 if (socks4_prot
!= socks4a
&&
1131 !addressmap_have_mapping(tmpbuf
) &&
1132 !have_warned_about_unsafe_socks
) {
1134 "Your application (using socks4 on port %d) is giving Tor "
1135 "only an IP address. Applications that do DNS resolves "
1136 "themselves may leak information. Consider using Socks4A "
1137 "(e.g. via privoxy or socat) instead. For more information, "
1138 "please see http://wiki.noreply.org/noreply/TheOnionRouter/"
1139 "TorFAQ#SOCKSAndDNS.%s", req
->port
,
1140 safe_socks
? " Rejecting." : "");
1141 // have_warned_about_unsafe_socks = 1; // (for now, warn every time)
1142 control_event_client_status(LOG_WARN
,
1143 "DANGEROUS_SOCKS PROTOCOL=SOCKS4 ADDRESS=%s:%d",
1148 if (socks4_prot
== socks4a
) {
1149 if (next
+1 == buf
->cur
+buf
->datalen
) {
1150 log_debug(LD_APP
,"socks4: No part of destaddr here yet.");
1154 next
= memchr(startaddr
, 0, buf
->cur
+buf
->datalen
-startaddr
);
1156 log_debug(LD_APP
,"socks4: Destaddr not all here yet.");
1159 if (MAX_SOCKS_ADDR_LEN
<= next
-startaddr
) {
1160 log_warn(LD_APP
,"socks4: Destaddr too long. Rejecting.");
1163 tor_assert(next
< buf
->cur
+buf
->datalen
);
1167 "Your application (using socks4a on port %d) gave "
1168 "Tor a hostname, which means Tor will do the DNS resolve "
1169 "for you. This is good.", req
->port
);
1171 log_debug(LD_APP
,"socks4: Everything is here. Success.");
1172 strlcpy(req
->address
, startaddr
? startaddr
: tmpbuf
,
1173 sizeof(req
->address
));
1174 if (!tor_strisprint(req
->address
) || strchr(req
->address
,'\"')) {
1175 log_warn(LD_PROTOCOL
,
1176 "Your application (using socks4 on port %d) gave Tor "
1177 "a malformed hostname: %s. Rejecting the connection.",
1178 req
->port
, escaped(req
->address
));
1181 /* next points to the final \0 on inbuf */
1182 buf_remove_from_front(buf
, next
-buf
->cur
+1);
1186 case 'H': /* head */
1187 case 'P': /* put/post */
1188 case 'C': /* connect */
1190 "HTTP/1.0 501 Tor is not an HTTP Proxy\r\n"
1191 "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
1194 "<title>Tor is not an HTTP Proxy</title>\n"
1197 "<h1>Tor is not an HTTP Proxy</h1>\n"
1199 "It appears you have configured your web browser to use Tor as an HTTP proxy."
1201 "This is not correct: Tor is a SOCKS proxy, not an HTTP proxy.\n"
1202 "Please configure your client accordingly.\n"
1205 "See <a href=\"http://tor.eff.org/documentation.html\">"
1206 "http://tor.eff.org/documentation.html</a> for more information.\n"
1207 "<!-- Plus this comment, to make the body response more than 512 bytes, so "
1208 " IE will be willing to display it. Comment comment comment comment "
1209 " comment comment comment comment comment comment comment comment.-->\n"
1213 , MAX_SOCKS_REPLY_LEN
);
1214 req
->replylen
= strlen(req
->reply
)+1;
1216 default: /* version is not socks4 or socks5 */
1218 "Socks version %d not recognized. (Tor is not an http proxy.)",
1221 char *tmp
= tor_strndup(buf
->cur
, 8);
1222 control_event_client_status(LOG_WARN
,
1223 "SOCKS_UNKNOWN_PROTOCOL DATA=\"%s\"",
1231 /** If there is a complete version 0 control message waiting on buf, then store
1232 * its contents into *<b>type_out</b>, store its body's length into
1233 * *<b>len_out</b>, allocate and store a string for its body into
1234 * *<b>body_out</b>, and return 1. (body_out will always be NUL-terminated,
1235 * even if the control message body doesn't end with NUL.)
1237 * If there is not a complete control message waiting, return 0.
1239 * Return -1 on error; return -2 on "seems to be control protocol v1."
1242 fetch_from_buf_control0(buf_t
*buf
, uint32_t *len_out
, uint16_t *type_out
,
1243 char **body_out
, int check_for_v1
)
1250 tor_assert(len_out
);
1251 tor_assert(type_out
);
1252 tor_assert(body_out
);
1257 if (buf
->datalen
< 4)
1260 peek_from_buf(tmp
, 4, buf
);
1262 msglen
= ntohs(get_uint16(tmp
));
1263 type
= ntohs(get_uint16(tmp
+2));
1264 if (type
> 255 && check_for_v1
)
1267 if (buf
->datalen
< 4 + (unsigned)msglen
)
1272 buf_remove_from_front(buf
, 4);
1274 *body_out
= tor_malloc(msglen
+1);
1275 fetch_from_buf(*body_out
, msglen
, buf
);
1276 (*body_out
)[msglen
] = '\0';
1281 /** Helper: return a pointer to the first instance of <b>c</b> in the
1282 * <b>len</b>characters after <b>start</b> on <b>buf</b>. Return NULL if the
1283 * character isn't found. */
1285 find_char_on_buf(buf_t
*buf
, char *start
, size_t len
, char c
)
1289 _split_range(buf
, start
, &len
, &len_rest
);
1290 cp
= memchr(start
, c
, len
);
1291 if (cp
|| !len_rest
)
1293 return memchr(buf
->mem
, c
, len_rest
);
1296 /** Helper: return a pointer to the first CRLF after cp on <b>buf</b>. Return
1297 * NULL if no CRLF is found. */
1299 find_crlf_on_buf(buf_t
*buf
, char *cp
)
1303 size_t remaining
= buf
->datalen
- _buf_offset(buf
,cp
);
1304 cp
= find_char_on_buf(buf
, cp
, remaining
, '\r');
1307 next
= _wrap_ptr(buf
, cp
+1);
1308 if (next
== _buf_end(buf
))
1316 /** Try to read a single CRLF-terminated line from <b>buf</b>, and write it,
1317 * NUL-terminated, into the *<b>data_len</b> byte buffer at <b>data_out</b>.
1318 * Set *<b>data_len</b> to the number of bytes in the line, not counting the
1319 * terminating NUL. Return 1 if we read a whole line, return 0 if we don't
1320 * have a whole line yet, and return -1 if we we need to grow the buffer.
1323 fetch_from_buf_line(buf_t
*buf
, char *data_out
, size_t *data_len
)
1327 /* Look for a CRLF. */
1328 if (!(eol
= find_crlf_on_buf(buf
, buf
->cur
))) {
1331 sz
= _buf_offset(buf
, eol
);
1332 if (sz
+3 > *data_len
) {
1336 fetch_from_buf(data_out
, sz
+2, buf
);
1337 data_out
[sz
+2] = '\0';
1342 /** Try to read a single LF-terminated line from <b>buf</b>, and write it,
1343 * NUL-terminated, into the *<b>data_len</b> byte buffer at <b>data_out</b>.
1344 * Set *<b>data_len</b> to the number of bytes in the line, not counting the
1345 * terminating NUL. Return 1 if we read a whole line, return 0 if we don't
1346 * have a whole line yet, and return -1 if the line length exceeds
1350 fetch_from_buf_line_lf(buf_t
*buf
, char *data_out
, size_t *data_len
)
1355 size_t remaining
= buf
->datalen
- _buf_offset(buf
,buf
->cur
);
1356 cp
= find_char_on_buf(buf
, buf
->cur
, remaining
, '\n');
1359 sz
= _buf_offset(buf
, cp
);
1360 if (sz
+2 > *data_len
) {
1364 fetch_from_buf(data_out
, sz
+1, buf
);
1365 data_out
[sz
+1] = '\0';
1370 /** Compress on uncompress the <b>data_len</b> bytes in <b>data</b> using the
1371 * zlib state <b>state</b>, appending the result to <b>buf</b>. If
1372 * <b>done</b> is true, flush the data in the state and finish the
1373 * compression/uncompression. Return -1 on failure, 0 on success. */
1375 write_to_buf_zlib(buf_t
*buf
, tor_zlib_state_t
*state
,
1376 const char *data
, size_t data_len
,
1380 size_t old_avail
, avail
;
1383 buf_ensure_capacity(buf
, buf
->datalen
+ 1024);
1384 next
= _buf_end(buf
);
1385 if (next
< buf
->cur
)
1386 old_avail
= avail
= buf
->cur
- next
;
1388 old_avail
= avail
= (buf
->mem
+ buf
->len
) - next
;
1389 switch (tor_zlib_process(state
, &next
, &avail
, &data
, &data_len
, done
)) {
1399 case TOR_ZLIB_BUF_FULL
:
1400 if (avail
&& buf
->len
>= 1024 + buf
->datalen
) {
1401 /* Zlib says we need more room (ZLIB_BUF_FULL), and we're not about
1402 * to wrap around (avail != 0), and resizing won't actually make us
1403 * un-full: we're at the end of the buffer, and zlib refuses to
1404 * append more here, but there's a pile of free space at the start
1405 * of the buffer (about 1K). So chop a few characters off the
1406 * end of the buffer. This feels silly; anybody got a better hack?
1408 * (We don't just want to expand the buffer nevertheless. Consider a
1409 * 1/3 full buffer with a single byte free at the end. zlib will
1410 * often refuse to append to that, and so we want to use the
1411 * beginning, not double the buffer to be just 1/6 full.)
1413 tor_assert(next
>= buf
->cur
);
1418 buf
->datalen
+= old_avail
- avail
;
1419 if (buf
->datalen
> buf
->highwater
)
1420 buf
->highwater
= buf
->datalen
;
1421 buf_total_used
+= old_avail
- avail
;
1426 /** Log an error and exit if <b>buf</b> is corrupted.
1429 assert_buf_ok(buf_t
*buf
)
1432 tor_assert(buf
->magic
== BUFFER_MAGIC
);
1433 tor_assert(buf
->mem
);
1434 tor_assert(buf
->highwater
<= buf
->len
);
1435 tor_assert(buf
->datalen
<= buf
->highwater
);
1438 uint32_t u32
= get_uint32(buf
->mem
- 4);
1439 tor_assert(u32
== START_MAGIC
);
1440 u32
= get_uint32(buf
->mem
+ buf
->memsize
);
1441 tor_assert(u32
== END_MAGIC
);