r14602@catbus: nickm | 2007-08-16 13:33:27 -0400
[tor.git] / src / or / buffers.c
blob33118c2b46bc3ae18e04541afe7d168454a6e62c
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 */
5 /* $Id$ */
6 const char buffers_c_id[] =
7 "$Id$";
9 /**
10 * \file buffers.c
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.
14 **/
16 #include "or.h"
18 #undef SENTINELS
19 #undef CHECK_AFTER_RESIZE
20 #undef PARANOIA
21 #undef NOINLINE
23 /* If SENTINELS is defined, check for attempts to write beyond the
24 * end/before the start of the buffer.
26 #ifdef SENTINELS
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
38 * of data? */
39 #define ALLOC_LEN(ln) ((ln)+8)
40 /** Initialize the sentinel values on <b>m</b> (a value of buf-&gt;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)
44 #else
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)
49 #endif
51 #ifdef PARANOIA
52 #define check() do { assert_buf_ok(buf); } while (0)
53 #else
54 #define check() do { } while (0)
55 #endif
57 #ifdef NOINLINE
58 #undef INLINE
59 #define INLINE
60 #endif
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. */
65 struct buf_t {
66 uint32_t magic; /**< Magic cookie for debugging: Must be set to
67 * BUFFER_MAGIC. */
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
87 * than this size. */
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.
97 static void
98 buf_normalize(buf_t *buf)
100 check();
101 if (buf->cur + buf->datalen <= buf->mem+buf->len) {
102 return;
103 } else {
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;
115 check();
119 /** Return the point in the buffer where the next byte will get stored. */
120 static INLINE char *
121 _buf_end(buf_t *buf)
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
129 * around. */
130 static INLINE char *
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. */
137 static INLINE int
138 _buf_offset(buf_t *buf, char *cp)
140 if (cp >= buf->cur)
141 return cp - buf->cur;
142 else
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-&gt;mem</b>. Otherwise, set *<b>more_len</b> to 0.
152 static INLINE void
153 _split_range(buf_t *buf, char *at, size_t *len,
154 size_t *more_len)
156 char *eos = at + *len;
157 check();
158 if (eos >= (buf->mem + buf->len)) {
159 *more_len = eos - (buf->mem + buf->len);
160 *len -= *more_len;
161 } else {
162 *more_len = 0;
166 /** Change a buffer's capacity. <b>new_capacity</b> must be \>=
167 * buf->datalen. */
168 static void
169 buf_resize(buf_t *buf, size_t new_capacity)
171 off_t offset;
172 #ifdef CHECK_AFTER_RESIZE
173 char *tmp, *tmp2;
174 #endif
175 tor_assert(buf->datalen <= new_capacity);
176 tor_assert(new_capacity);
178 #ifdef CHECK_AFTER_RESIZE
179 assert_buf_ok(buf);
180 tmp = tor_malloc(buf->datalen);
181 tmp2 = tor_malloc(buf->datalen);
182 peek_from_buf(tmp, buf->datalen, buf);
183 #endif
185 if (buf->len == new_capacity)
186 return;
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) {
192 /* We have:
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);
203 } else {
204 /* The data doesn't wrap around, but it does extend beyond the new
205 * buffer length:
206 * mem[offset] ... mem[offset+datalen-1] (the data)
208 memmove(buf->mem, buf->cur, buf->datalen);
209 offset = 0;
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 &&
217 !buf->datalen &&
218 buf->len >= 1<<16) {
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);
223 tor_free(oldmem);
224 buf->mem = buf->cur = newmem;
225 } else {
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
236 * now contains:
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
252 assert_buf_ok(buf);
253 peek_from_buf(tmp2, buf->datalen, buf);
254 if (memcmp(tmp, tmp2, buf->datalen)) {
255 tor_assert(0);
257 tor_free(tmp);
258 tor_free(tmp2);
259 #endif
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
264 * size.)
266 static INLINE int
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. */
271 return 0;
272 if (capacity > MAX_BUF_SIZE) /* Don't grow past the maximum. */
273 return -1;
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;
278 new_len = 16;
279 while (new_len < min_len)
280 new_len *= 2;
281 while (new_len < capacity)
282 new_len *= 2;
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);
287 return 0;
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>-&gt;highwater, but never smaller than
294 * MIN_LAZY_SHRINK_SIZE.
296 void
297 buf_shrink(buf_t *buf)
299 size_t new_len;
301 new_len = buf->len;
302 while (buf->highwater < (new_len>>2) && new_len > MIN_LAZY_SHRINK_SIZE*2)
303 new_len >>= 1;
305 buf->highwater = buf->datalen;
306 if (new_len == buf->len)
307 return;
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. */
315 static INLINE void
316 buf_remove_from_front(buf_t *buf, size_t n)
318 tor_assert(buf->datalen >= n);
319 buf->datalen -= n;
320 buf_total_used -= n;
321 if (buf->datalen) {
322 buf->cur = _wrap_ptr(buf, buf->cur+n);
323 } else {
324 buf->cur = buf->mem;
326 check();
329 /** Make sure that the memory in buf ends with a zero byte. */
330 static INLINE int
331 buf_nul_terminate(buf_t *buf)
333 if (buf_ensure_capacity(buf,buf->datalen+1)<0)
334 return -1;
335 *_buf_end(buf) = '\0';
336 return 0;
339 /** Create and return a new buf with capacity <b>size</b>. */
340 buf_t *
341 buf_new_with_capacity(size_t size)
343 buf_t *buf;
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;
351 assert_buf_ok(buf);
352 return buf;
355 /** Allocate and return a new buffer with default capacity. */
356 buf_t *
357 buf_new(void)
359 return buf_new_with_capacity(INITIAL_BUF_SIZE);
362 /** Remove all data from <b>buf</b>. */
363 void
364 buf_clear(buf_t *buf)
366 buf_total_used -= buf->datalen;
367 buf->datalen = 0;
368 buf->cur = buf->mem;
369 buf->len = buf->memsize;
372 /** Return the number of bytes stored in <b>buf</b> */
373 size_t
374 buf_datalen(const buf_t *buf)
376 return buf->datalen;
379 /** Return the maximum bytes that can be stored in <b>buf</b> before buf
380 * needs to resize. */
381 size_t
382 buf_capacity(const buf_t *buf)
384 return buf->len;
387 /** For testing only: Return a pointer to the raw memory stored in
388 * <b>buf</b>. */
389 const char *
390 _buf_peek_raw_buffer(const buf_t *buf)
392 return buf->cur;
395 /** Release storage held by <b>buf</b>. */
396 void
397 buf_free(buf_t *buf)
399 char *oldmem;
400 assert_buf_ok(buf);
401 buf->magic = 0xDEADBEEF;
402 oldmem = RAW_MEM(buf->mem);
403 tor_free(oldmem);
404 buf_total_alloc -= buf->len;
405 buf_total_used -= buf->datalen;
406 tor_free(buf);
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
413 * failure.
415 static INLINE int
416 read_to_buf_impl(int s, size_t at_most, buf_t *buf,
417 char *pos, int *reached_eof)
419 int read_result;
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 */
426 #ifdef MS_WINDOWS
427 if (e == WSAENOBUFS)
428 log_warn(LD_NET,"recv() failed: WSAENOBUFS. Not enough ram?");
429 #endif
430 return -1;
432 return 0; /* would block. */
433 } else if (read_result == 0) {
434 log_debug(LD_NET,"Encountered eof");
435 *reached_eof = 1;
436 return 0;
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,
443 (int)buf->datalen);
444 return 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
452 * block.
455 read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof)
457 int r;
458 char *next;
459 size_t at_start;
461 /* assert_buf_ok(buf); */
462 tor_assert(reached_eof);
463 tor_assert(s>=0);
465 if (buf_ensure_capacity(buf,buf->datalen+at_most))
466 return -1;
468 if (at_most + buf->datalen > buf->len)
469 at_most = buf->len - buf->datalen; /* take the min of the two */
471 if (at_most == 0)
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);
478 check();
479 if (r < 0 || (size_t)r < at_most) {
480 return r; /* Either error, eof, block, or no more to read. */
483 if (at_start) {
484 int r2;
485 tor_assert(_buf_end(buf) == buf->mem);
486 r2 = read_to_buf_impl(s, at_start, buf, buf->mem, reached_eof);
487 check();
488 if (r2 < 0) {
489 return r2;
490 } else {
491 r += r2;
494 return r;
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,
501 * -1 on failure.
503 static INLINE int
504 read_to_buf_tls_impl(tor_tls_t *tls, size_t at_most, buf_t *buf, char *next)
506 int r;
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),
510 (int)at_most);
511 r = tor_tls_read(tls, next, at_most);
512 if (r<0)
513 return r;
514 buf->datalen += r;
515 buf_total_used += r;
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));
520 return r;
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)
545 int r;
546 char *next;
547 size_t at_start;
549 tor_assert(tls);
550 assert_buf_ok(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),
554 (int)at_most);
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;
562 if (at_most == 0)
563 return 0;
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);
569 check();
570 if (r < 0 || (size_t)r < at_most)
571 return r; /* Either error, eof, block, or no more to read. */
573 if (at_start) {
574 int r2;
575 tor_assert(_buf_end(buf) == buf->mem);
576 r2 = read_to_buf_tls_impl(tls, at_start, buf, buf->mem);
577 check();
578 if (r2 < 0)
579 return r2;
580 else
581 r += r2;
583 return r;
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.
591 static INLINE int
592 flush_buf_impl(int s, buf_t *buf, size_t sz, size_t *buf_flushlen)
594 int write_result;
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 */
600 #ifdef MS_WINDOWS
601 if (e == WSAENOBUFS)
602 log_warn(LD_NET,"write() failed: WSAENOBUFS. Not enough ram?");
603 #endif
604 return -1;
606 log_debug(LD_NET,"write() would block, returning.");
607 return 0;
608 } else {
609 *buf_flushlen -= write_result;
610 buf_remove_from_front(buf, write_result);
611 return 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)
624 int r;
625 size_t flushed = 0;
626 size_t flushlen0, flushlen1;
628 /* assert_buf_ok(buf); */
629 tor_assert(buf_flushlen);
630 tor_assert(s>=0);
631 tor_assert(*buf_flushlen <= buf->datalen);
632 tor_assert(sz <= *buf_flushlen);
634 if (sz == 0) /* nothing to flush */
635 return 0;
637 flushlen0 = sz;
638 _split_range(buf, buf->cur, &flushlen0, &flushlen1);
640 r = flush_buf_impl(s, buf, flushlen0, buf_flushlen);
641 check();
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. */
647 flushed = r;
649 if (flushlen1) {
650 tor_assert(buf->cur == buf->mem);
651 r = flush_buf_impl(s, buf, flushlen1, buf_flushlen);
652 check();
653 log_debug(LD_NET,"%d: flushed %d bytes, %d ready to flush, %d remain.",
654 s,r,(int)*buf_flushlen,(int)buf->datalen);
655 if (r<0)
656 return r;
657 flushed += r;
659 return flushed;
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
666 * failure.
668 static INLINE int
669 flush_buf_tls_impl(tor_tls_t *tls, buf_t *buf, size_t sz, size_t *buf_flushlen)
671 int r;
672 size_t forced;
674 forced = tor_tls_get_forced_write_size(tls);
675 if (forced > sz)
676 sz = forced;
677 r = tor_tls_write(tls, buf->cur, sz);
678 if (r < 0) {
679 return r;
681 *buf_flushlen -= r;
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);
685 return r;
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)
693 int r;
694 size_t flushed=0;
695 size_t flushlen0, flushlen1;
696 /* assert_buf_ok(buf); */
697 tor_assert(tls);
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();
706 flushlen0 = sz;
707 _split_range(buf, buf->cur, &flushlen0, &flushlen1);
708 if (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);
714 check();
715 if (r < 0 || (size_t)r < flushlen0)
716 return r; /* Error, or can't flush any more now. */
717 flushed = r;
719 if (flushlen1) {
720 tor_assert(buf->cur == buf->mem);
721 r = flush_buf_tls_impl(tls, buf, flushlen1, buf_flushlen);
722 check();
723 if (r<0)
724 return r;
725 flushed += r;
727 return flushed;
730 /** Append <b>string_len</b> bytes from <b>string</b> to the end of
731 * <b>buf</b>.
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)
738 char *next;
739 size_t len2;
741 /* append string to buf (growing as needed, return -1 if "too big")
742 * return total number of bytes on the buf
745 tor_assert(string);
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));
751 return -1;
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;
761 if (len2) {
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);
771 check();
772 return buf->datalen;
775 /** Helper: copy the first <b>string_len</b> bytes from <b>buf</b>
776 * onto <b>string</b>.
778 static INLINE void
779 peek_from_buf(char *string, size_t string_len, buf_t *buf)
781 size_t len2;
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. */
788 tor_assert(string);
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);
796 if (len2) {
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. */
813 check();
814 peek_from_buf(string, string_len, buf);
815 buf_remove_from_front(buf, string_len);
816 check();
817 return buf->datalen;
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,
843 int force_complete)
845 char *headers, *body, *p;
846 size_t headerlen, bodylen, contentlen;
848 /* assert_buf_ok(buf); */
849 buf_normalize(buf);
851 if (buf_nul_terminate(buf)<0) {
852 log_warn(LD_BUG,"Couldn't nul-terminate buffer");
853 return -1;
855 headers = buf->cur;
856 body = strstr(headers,"\r\n\r\n");
857 if (!body) {
858 log_debug(LD_HTTP,"headers not all here yet.");
859 return 0;
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);
869 return -1;
871 if (max_bodylen <= bodylen) {
872 log_warn(LD_HTTP,"bodylen %d larger than %d. Failing.",
873 (int)bodylen, (int)max_bodylen-1);
874 return -1;
877 #define CONTENT_LENGTH "\r\nContent-Length: "
878 p = strstr(headers, CONTENT_LENGTH);
879 if (p) {
880 int i;
881 i = atoi(p+strlen(CONTENT_LENGTH));
882 if (i < 0) {
883 log_warn(LD_PROTOCOL, "Content-Length is less than zero; it looks like "
884 "someone is trying to crash us.");
885 return -1;
887 contentlen = i;
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 */
902 if (headers_out) {
903 *headers_out = tor_malloc(headerlen+1);
904 memcpy(*headers_out,buf->cur,headerlen);
905 (*headers_out)[headerlen] = 0; /* nul terminate it */
907 if (body_out) {
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);
915 return 1;
918 /** There is a (possibly incomplete) socks handshake on <b>buf</b>, of one
919 * of the forms
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
941 * undefined.
944 fetch_from_buf_socks(buf_t *buf, socks_request_t *req,
945 int log_sockstype, int safe_socks)
947 unsigned char len;
948 char tmpbuf[INET_NTOA_BUF_LEN];
949 uint32_t destip;
950 enum {socks4, socks4a} socks4_prot = socks4a;
951 char *next, *startaddr;
952 struct in_addr in;
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 */
959 return 0;
960 buf_normalize(buf);
962 switch (*(buf->cur)) { /* which version of socks? */
964 case 5: /* socks5 */
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)
970 return 0;
971 if (!nummethods || !memchr(buf->cur+2, 0, nummethods)) {
972 log_warn(LD_APP,
973 "socks5: offered methods don't include 'no auth'. "
974 "Rejecting.");
975 req->replylen = 2; /* 2 bytes of response */
976 req->reply[0] = 5;
977 req->reply[1] = '\xFF'; /* reject all methods */
978 return -1;
980 /* remove packet from buf. also remove any other extraneous
981 * bytes, to support broken socks clients. */
982 buf_clear(buf);
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");
989 return 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.",
1002 req->command);
1003 return -1;
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) {
1015 log_warn(LD_APP,
1016 "socks5 IP takes %d bytes, which doesn't fit in %d. "
1017 "Rejecting.",
1018 (int)strlen(tmpbuf)+1,(int)MAX_SOCKS_ADDR_LEN);
1019 return -1;
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) {
1027 log_warn(LD_APP,
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);
1039 if (safe_socks)
1040 return -1;
1042 return 1;
1043 case 3: /* fqdn */
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) {
1049 log_warn(LD_APP,
1050 "socks5 hostname is %d bytes, which doesn't fit in "
1051 "%d. Rejecting.", len+1,MAX_SOCKS_ADDR_LEN);
1052 return -1;
1054 if (req->command == SOCKS_COMMAND_RESOLVE_PTR) {
1055 log_warn(LD_APP, "socks5 received RESOLVE_PTR command with "
1056 "hostname type. Rejecting.");
1057 return -1;
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));
1068 return -1;
1070 if (log_sockstype)
1071 log_notice(LD_APP,
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);
1075 return 1;
1076 default: /* unsupported */
1077 log_warn(LD_APP,"socks5: unsupported address type %d. Rejecting.",
1078 *(buf->cur+3));
1079 return -1;
1081 tor_assert(0);
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
1095 * socks4.) */
1096 log_warn(LD_APP,"socks4: command %d not recognized. Rejecting.",
1097 req->command);
1098 return -1;
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.");
1105 return -1;
1107 if (destip >> 8) {
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));
1114 return -1;
1116 log_debug(LD_APP,
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);
1123 if (!next) {
1124 log_debug(LD_APP,"socks4: Username not here yet.");
1125 return 0;
1127 tor_assert(next < buf->cur+buf->datalen);
1129 startaddr = NULL;
1130 if (socks4_prot != socks4a &&
1131 !addressmap_have_mapping(tmpbuf) &&
1132 !have_warned_about_unsafe_socks) {
1133 log_warn(LD_APP,
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",
1144 tmpbuf, req->port);
1145 if (safe_socks)
1146 return -1;
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.");
1151 return 0;
1153 startaddr = next+1;
1154 next = memchr(startaddr, 0, buf->cur+buf->datalen-startaddr);
1155 if (!next) {
1156 log_debug(LD_APP,"socks4: Destaddr not all here yet.");
1157 return 0;
1159 if (MAX_SOCKS_ADDR_LEN <= next-startaddr) {
1160 log_warn(LD_APP,"socks4: Destaddr too long. Rejecting.");
1161 return -1;
1163 tor_assert(next < buf->cur+buf->datalen);
1165 if (log_sockstype)
1166 log_notice(LD_APP,
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));
1179 return -1;
1181 /* next points to the final \0 on inbuf */
1182 buf_remove_from_front(buf, next-buf->cur+1);
1183 return 1;
1185 case 'G': /* get */
1186 case 'H': /* head */
1187 case 'P': /* put/post */
1188 case 'C': /* connect */
1189 strlcpy(req->reply,
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"
1192 "<html>\n"
1193 "<head>\n"
1194 "<title>Tor is not an HTTP Proxy</title>\n"
1195 "</head>\n"
1196 "<body>\n"
1197 "<h1>Tor is not an HTTP Proxy</h1>\n"
1198 "<p>\n"
1199 "It appears you have configured your web browser to use Tor as an HTTP proxy."
1200 "\n"
1201 "This is not correct: Tor is a SOCKS proxy, not an HTTP proxy.\n"
1202 "Please configure your client accordingly.\n"
1203 "</p>\n"
1204 "<p>\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"
1210 "</p>\n"
1211 "</body>\n"
1212 "</html>\n"
1213 , MAX_SOCKS_REPLY_LEN);
1214 req->replylen = strlen(req->reply)+1;
1215 /* fall through */
1216 default: /* version is not socks4 or socks5 */
1217 log_warn(LD_APP,
1218 "Socks version %d not recognized. (Tor is not an http proxy.)",
1219 *(buf->cur));
1221 char *tmp = tor_strndup(buf->cur, 8);
1222 control_event_client_status(LOG_WARN,
1223 "SOCKS_UNKNOWN_PROTOCOL DATA=\"%s\"",
1224 escaped(tmp));
1225 tor_free(tmp);
1227 return -1;
1231 /** Return 1 iff buf looks more like it has an (obsolete) v0 controller
1232 * command on it than any valid v1 controller command. */
1234 peek_buf_has_control0_command(buf_t *buf)
1236 if (buf->datalen >= 4) {
1237 char header[4];
1238 uint16_t cmd;
1239 peek_from_buf(header, sizeof(header), buf);
1240 cmd = ntohs(get_uint16(header+2));
1241 if (cmd <= 0x14)
1242 return 1; /* This is definitely not a v1 control command. */
1244 return 0;
1247 /** Helper: return a pointer to the first instance of <b>c</b> in the
1248 * <b>len</b>characters after <b>start</b> on <b>buf</b>. Return NULL if the
1249 * character isn't found. */
1250 static char *
1251 find_char_on_buf(buf_t *buf, char *start, size_t len, char c)
1253 size_t len_rest;
1254 char *cp;
1255 _split_range(buf, start, &len, &len_rest);
1256 cp = memchr(start, c, len);
1257 if (cp || !len_rest)
1258 return cp;
1259 return memchr(buf->mem, c, len_rest);
1262 /** Helper: return a pointer to the first CRLF after cp on <b>buf</b>. Return
1263 * NULL if no CRLF is found. */
1264 static char *
1265 find_crlf_on_buf(buf_t *buf, char *cp)
1267 char *next;
1268 while (1) {
1269 size_t remaining = buf->datalen - _buf_offset(buf,cp);
1270 cp = find_char_on_buf(buf, cp, remaining, '\r');
1271 if (!cp)
1272 return NULL;
1273 next = _wrap_ptr(buf, cp+1);
1274 if (next == _buf_end(buf))
1275 return NULL;
1276 if (*next == '\n')
1277 return cp;
1278 cp = next;
1282 /** Try to read a single CRLF-terminated line from <b>buf</b>, and write it,
1283 * NUL-terminated, into the *<b>data_len</b> byte buffer at <b>data_out</b>.
1284 * Set *<b>data_len</b> to the number of bytes in the line, not counting the
1285 * terminating NUL. Return 1 if we read a whole line, return 0 if we don't
1286 * have a whole line yet, and return -1 if we we need to grow the buffer.
1289 fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len)
1291 char *eol;
1292 size_t sz;
1293 /* Look for a CRLF. */
1294 if (!(eol = find_crlf_on_buf(buf, buf->cur))) {
1295 return 0;
1297 sz = _buf_offset(buf, eol);
1298 if (sz+3 > *data_len) {
1299 *data_len = sz+3;
1300 return -1;
1302 fetch_from_buf(data_out, sz+2, buf);
1303 data_out[sz+2] = '\0';
1304 *data_len = sz+2;
1305 return 1;
1308 /** Try to read a single LF-terminated line from <b>buf</b>, and write it,
1309 * NUL-terminated, into the *<b>data_len</b> byte buffer at <b>data_out</b>.
1310 * Set *<b>data_len</b> to the number of bytes in the line, not counting the
1311 * terminating NUL. Return 1 if we read a whole line, return 0 if we don't
1312 * have a whole line yet, and return -1 if the line length exceeds
1313 *<b>data_len</b>.
1316 fetch_from_buf_line_lf(buf_t *buf, char *data_out, size_t *data_len)
1318 char *cp;
1319 size_t sz;
1321 size_t remaining = buf->datalen - _buf_offset(buf,buf->cur);
1322 cp = find_char_on_buf(buf, buf->cur, remaining, '\n');
1323 if (!cp)
1324 return 0;
1325 sz = _buf_offset(buf, cp);
1326 if (sz+2 > *data_len) {
1327 *data_len = sz+2;
1328 return -1;
1330 fetch_from_buf(data_out, sz+1, buf);
1331 data_out[sz+1] = '\0';
1332 *data_len = sz+1;
1333 return 1;
1336 /** Compress on uncompress the <b>data_len</b> bytes in <b>data</b> using the
1337 * zlib state <b>state</b>, appending the result to <b>buf</b>. If
1338 * <b>done</b> is true, flush the data in the state and finish the
1339 * compression/uncompression. Return -1 on failure, 0 on success. */
1341 write_to_buf_zlib(buf_t *buf, tor_zlib_state_t *state,
1342 const char *data, size_t data_len,
1343 int done)
1345 char *next;
1346 size_t old_avail, avail;
1347 int over = 0;
1348 do {
1349 buf_ensure_capacity(buf, buf->datalen + 1024);
1350 next = _buf_end(buf);
1351 if (next < buf->cur)
1352 old_avail = avail = buf->cur - next;
1353 else
1354 old_avail = avail = (buf->mem + buf->len) - next;
1355 switch (tor_zlib_process(state, &next, &avail, &data, &data_len, done)) {
1356 case TOR_ZLIB_DONE:
1357 over = 1;
1358 break;
1359 case TOR_ZLIB_ERR:
1360 return -1;
1361 case TOR_ZLIB_OK:
1362 if (data_len == 0)
1363 over = 1;
1364 break;
1365 case TOR_ZLIB_BUF_FULL:
1366 if (avail && buf->len >= 1024 + buf->datalen) {
1367 /* Zlib says we need more room (ZLIB_BUF_FULL), and we're not about
1368 * to wrap around (avail != 0), and resizing won't actually make us
1369 * un-full: we're at the end of the buffer, and zlib refuses to
1370 * append more here, but there's a pile of free space at the start
1371 * of the buffer (about 1K). So chop a few characters off the
1372 * end of the buffer. This feels silly; anybody got a better hack?
1374 * (We don't just want to expand the buffer nevertheless. Consider a
1375 * 1/3 full buffer with a single byte free at the end. zlib will
1376 * often refuse to append to that, and so we want to use the
1377 * beginning, not double the buffer to be just 1/6 full.)
1379 tor_assert(next >= buf->cur);
1380 buf->len -= avail;
1382 break;
1384 buf->datalen += old_avail - avail;
1385 if (buf->datalen > buf->highwater)
1386 buf->highwater = buf->datalen;
1387 buf_total_used += old_avail - avail;
1388 } while (!over);
1389 return 0;
1392 /** Log an error and exit if <b>buf</b> is corrupted.
1394 void
1395 assert_buf_ok(buf_t *buf)
1397 tor_assert(buf);
1398 tor_assert(buf->magic == BUFFER_MAGIC);
1399 tor_assert(buf->mem);
1400 tor_assert(buf->highwater <= buf->len);
1401 tor_assert(buf->datalen <= buf->highwater);
1402 #ifdef SENTINELS
1404 uint32_t u32 = get_uint32(buf->mem - 4);
1405 tor_assert(u32 == START_MAGIC);
1406 u32 = get_uint32(buf->mem + buf->memsize);
1407 tor_assert(u32 == END_MAGIC);
1409 #endif