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 */
6 const char buffers_c_id
[] = "$Id$";
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.
18 #undef CHECK_AFTER_RESIZE
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)
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)
41 #define check() do { assert_buf_ok(buf); } while (0)
43 #define check() do { } while (0)
51 #define BUFFER_MAGIC 0xB0FFF312u
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
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.
80 buf_normalize(buf_t
*buf
)
83 if (buf
->cur
+ buf
->datalen
<= buf
->mem
+buf
->len
) {
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
;
100 /** Return the point in the buffer where the next byte will get stored. */
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
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. */
119 _buf_offset(buf_t
*buf
, char *cp
)
122 return cp
- buf
->cur
;
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->mem</b>. Otherwise, set *<b>more_len</b> to 0.
134 _split_range(buf_t
*buf
, char *at
, size_t *len
,
137 char *eos
= at
+ *len
;
139 if (eos
>= (buf
->mem
+ buf
->len
)) {
140 *more_len
= eos
- (buf
->mem
+ buf
->len
);
147 /** Change a buffer's capacity. <b>new_capacity</b> must be \>= buf->datalen. */
149 buf_resize(buf_t
*buf
, size_t new_capacity
)
152 #ifdef CHECK_AFTER_RESIZE
155 tor_assert(buf
->datalen
<= new_capacity
);
156 tor_assert(new_capacity
);
158 #ifdef CHECK_AFTER_RESIZE
160 tmp
= tor_malloc(buf
->datalen
);
161 tmp2
= tor_malloc(buf
->datalen
);
162 peek_from_buf(tmp
, buf
->datalen
, buf
);
165 if (buf
->len
== new_capacity
)
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
) {
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
,
182 offset
-= (buf
->len
-new_capacity
);
184 /* The data doesn't wrap around, but it does extend beyond the new
186 * mem[offset] ... mem[offset+datalen-1] (the data)
188 memmove(buf
->mem
, buf
->cur
, buf
->datalen
);
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
&&
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
);
204 buf
->mem
= buf
->cur
= newmem
;
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
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
,
227 buf
->cur
+= new_capacity
-buf
->len
;
229 buf
->len
= new_capacity
;
231 #ifdef CHECK_AFTER_RESIZE
233 peek_from_buf(tmp2
, buf
->datalen
, buf
);
234 if (memcmp(tmp
, tmp2
, buf
->datalen
)) {
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
247 buf_ensure_capacity(buf_t
*buf
, size_t capacity
)
250 if (buf
->len
>= capacity
) /* Don't grow if we're already big enough. */
252 if (capacity
> MAX_BUF_SIZE
) /* Don't grow past the maximum. */
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
)
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
);
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>->highwater, but never smaller than
271 * MIN_LAZY_SHRINK_SIZE.
274 buf_shrink(buf_t
*buf
)
279 while (buf
->highwater
< (new_len
>>2) && new_len
> MIN_LAZY_SHRINK_SIZE
*2)
282 buf
->highwater
= buf
->datalen
;
283 if (new_len
== buf
->len
)
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. */
293 buf_remove_from_front(buf_t
*buf
, size_t n
)
295 tor_assert(buf
->datalen
>= n
);
299 buf
->cur
= _wrap_ptr(buf
, buf
->cur
+n
);
306 /** Make sure that the memory in buf ends with a zero byte. */
308 buf_nul_terminate(buf_t
*buf
)
310 if (buf_ensure_capacity(buf
,buf
->datalen
+1)<0)
312 *_buf_end(buf
) = '\0';
316 /** Create and return a new buf with capacity <b>size</b>. */
318 buf_new_with_capacity(size_t size
)
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
);
327 buf_total_alloc
+= size
;
332 /** Allocate and return a new buffer with default capacity. */
336 return buf_new_with_capacity(INITIAL_BUF_SIZE
);
339 /** Remove all data from <b>buf</b>. */
341 buf_clear(buf_t
*buf
)
343 buf_total_used
-= buf
->datalen
;
348 /** Return the number of bytes stored in <b>buf</b> */
350 buf_datalen(const buf_t
*buf
)
355 /** Return the maximum bytes that can be stored in <b>buf</b> before buf
356 * needs to resize. */
358 buf_capacity(const buf_t
*buf
)
363 /** For testing only: Return a pointer to the raw memory stored in
366 _buf_peek_raw_buffer(const buf_t
*buf
)
371 /** Release storage held by <b>buf</b>. */
377 buf
->magic
= 0xDEADBEEF;
378 oldmem
= RAW_MEM(buf
->mem
);
380 buf_total_alloc
-= buf
->len
;
381 buf_total_used
-= buf
->datalen
;
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
392 read_to_buf_impl(int s
, size_t at_most
, buf_t
*buf
,
393 char *pos
, int *reached_eof
)
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 */
404 return 0; /* would block. */
405 } else if (read_result
== 0) {
406 log_fn(LOG_DEBUG
,"Encountered eof");
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
,
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
427 read_to_buf(int s
, size_t at_most
, buf_t
*buf
, int *reached_eof
)
434 tor_assert(reached_eof
);
437 if (buf_ensure_capacity(buf
,buf
->datalen
+at_most
))
440 if (at_most
+ buf
->datalen
> buf
->len
)
441 at_most
= buf
->len
- buf
->datalen
; /* take the min of the two */
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
);
451 if (r
< 0 || (size_t)r
< at_most
) {
452 return r
; /* Either error, eof, block, or no more to read. */
457 tor_assert(_buf_end(buf
) == buf
->mem
);
458 r2
= read_to_buf_impl(s
, at_start
, buf
, buf
->mem
, reached_eof
);
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,
476 read_to_buf_tls_impl(tor_tls
*tls
, size_t at_most
, buf_t
*buf
, char *next
)
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
),
483 r
= tor_tls_read(tls
, next
, at_most
);
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
));
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
)
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
),
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
;
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
);
542 if (r
< 0 || (size_t)r
< at_most
)
543 return r
; /* Either error, eof, block, or no more to read. */
547 tor_assert(_buf_end(buf
) == buf
->mem
);
548 r2
= read_to_buf_tls_impl(tls
, at_start
, buf
, buf
->mem
);
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.
564 flush_buf_impl(int s
, buf_t
*buf
, size_t sz
, size_t *buf_flushlen
)
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 */
574 log_fn(LOG_DEBUG
,"write() would block, returning.");
577 *buf_flushlen
-= write_result
;
579 buf_remove_from_front(buf
, 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
)
596 size_t flushlen0
, flushlen1
;
599 tor_assert(buf_flushlen
);
601 tor_assert(*buf_flushlen
<= buf
->datalen
);
603 if (*buf_flushlen
== 0) /* nothing to flush */
606 flushlen0
= *buf_flushlen
;
607 _split_range(buf
, buf
->cur
, &flushlen0
, &flushlen1
);
609 r
= flush_buf_impl(s
, buf
, flushlen0
, buf_flushlen
);
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. */
619 tor_assert(buf
->cur
== buf
->mem
);
620 r
= flush_buf_impl(s
, buf
, flushlen1
, buf_flushlen
);
622 log_fn(LOG_DEBUG
,"%d: flushed %d bytes, %d ready to flush, %d remain.",
623 s
,r
,(int)*buf_flushlen
,(int)buf
->datalen
);
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.
637 flush_buf_tls_impl(tor_tls
*tls
, buf_t
*buf
, size_t sz
, size_t *buf_flushlen
)
641 r
= tor_tls_write(tls
, buf
->cur
, sz
);
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
);
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
)
659 size_t flushlen0
, flushlen1
;
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
);
673 if (r
< 0 || (size_t)r
< flushlen0
)
674 return r
; /* Error, or can't flush any more now. */
678 tor_assert(buf
->cur
== buf
->mem
);
679 r
= flush_buf_tls_impl(tls
, buf
, flushlen1
, buf_flushlen
);
688 /** Append <b>string_len</b> bytes from <b>string</b> to the end of
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
)
699 /* append string to buf (growing as needed, return -1 if "too big")
700 * return total number of bytes on the 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
));
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
;
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
);
732 /** Helper: copy the first <b>string_len</b> bytes from <b>buf</b>
733 * onto <b>string</b>.
736 peek_from_buf(char *string
, size_t string_len
, buf_t
*buf
)
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. */
746 tor_assert(string_len
<= buf
->datalen
); /* make sure we don't ask for too much */
749 _split_range(buf
, buf
->cur
, &string_len
, &len2
);
751 memcpy(string
, buf
->cur
, string_len
);
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. */
770 peek_from_buf(string
, string_len
, buf
);
771 buf_remove_from_front(buf
, string_len
);
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
;
804 if (buf_nul_terminate(buf
)<0) {
805 log_fn(LOG_WARN
,"Couldn't nul-terminate buffer");
809 body
= strstr(headers
,"\r\n\r\n");
811 log_fn(LOG_DEBUG
,"headers not all here yet.");
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);
824 if (max_bodylen
<= bodylen
) {
825 log_fn(LOG_WARN
,"bodylen %d larger than %d. Failing.", (int)bodylen
, (int)max_bodylen
-1);
829 #define CONTENT_LENGTH "\r\nContent-Length: "
830 p
= strstr(headers
, CONTENT_LENGTH
);
833 i
= atoi(p
+strlen(CONTENT_LENGTH
));
835 log_fn(LOG_WARN
, "Content-Length is less than zero; it looks like someone is trying to crash us.");
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 */
852 *headers_out
= tor_malloc(headerlen
+1);
853 memcpy(*headers_out
,buf
->cur
,headerlen
);
854 (*headers_out
)[headerlen
] = 0; /* null terminate it */
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
);
867 /** There is a (possibly incomplete) socks handshake on <b>buf</b>, of one
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
)
890 char tmpbuf
[INET_NTOA_BUF_LEN
];
892 enum {socks4
, socks4a
} socks4_prot
= socks4a
;
893 char *next
, *startaddr
;
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 */
904 switch (*(buf
->cur
)) { /* which version of socks? */
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
)
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 */
917 req
->reply
[1] = '\xFF'; /* reject all methods */
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");
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.",
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
);
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)
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
);
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);
979 default: /* unsupported */
980 log_fn(LOG_WARN
,"socks5: unsupported address type %d. Rejecting.",*(buf
->cur
+3));
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.",
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.");
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
));
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
);
1023 log_fn(LOG_DEBUG
,"socks4: Username not here yet.");
1026 tor_assert(next
< buf
->cur
+buf
->datalen
);
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.");
1041 next
= memchr(startaddr
, 0, buf
->cur
+buf
->datalen
-startaddr
);
1043 log_fn(LOG_DEBUG
,"socks4: Destaddr not all here yet.");
1046 if (MAX_SOCKS_ADDR_LEN
<= next
-startaddr
) {
1047 log_fn(LOG_WARN
,"socks4: Destaddr too long. Rejecting.");
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 */
1059 case 'H': /* head */
1060 case 'P': /* put/post */
1061 case 'C': /* connect */
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"
1067 "<title>Tor is not an HTTP Proxy</title>\n"
1070 "<h1>Tor is not an HTTP Proxy</h1>\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"
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"
1082 , MAX_SOCKS_REPLY_LEN
);
1083 req
->replylen
= strlen(req
->reply
)+1;
1085 default: /* version is not socks4 or socks5 */
1086 log_fn(LOG_WARN
,"Socks version %d not recognized. (Tor is not an http proxy.)",
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
)
1113 tor_assert(len_out
);
1114 tor_assert(type_out
);
1115 tor_assert(body_out
);
1120 if (buf
->datalen
< 4)
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
)
1130 if (buf
->datalen
< 4 + (unsigned)msglen
)
1135 buf_remove_from_front(buf
, 4);
1137 *body_out
= tor_malloc(msglen
+1);
1138 fetch_from_buf(*body_out
, msglen
, buf
);
1139 (*body_out
)[msglen
] = '\0';
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. */
1148 find_char_on_buf(buf_t
*buf
, char *start
, size_t len
, char c
)
1152 _split_range(buf
, start
, &len
, &len_rest
);
1153 cp
= memchr(buf
->cur
, c
, len
);
1154 if (cp
|| !len_rest
)
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. */
1162 find_crlf_on_buf(buf_t
*buf
, char *cp
)
1166 size_t remaining
= buf
->datalen
- _buf_offset(buf
,cp
);
1167 cp
= find_char_on_buf(buf
, cp
, remaining
, '\r');
1170 next
= _wrap_ptr(buf
, cp
+1);
1171 if (next
== _buf_end(buf
))
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
)
1190 /* Look for a CRLF. */
1191 if (!(eol
= find_crlf_on_buf(buf
, buf
->cur
))) {
1194 sz
= _buf_offset(buf
, eol
);
1195 if (sz
+3 > *data_len
) {
1199 fetch_from_buf(data_out
, sz
+2, buf
);
1200 data_out
[sz
+2] = '\0';
1205 /** Log an error and exit if <b>buf</b> is corrupted.
1208 assert_buf_ok(buf_t
*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
);
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
);