2004-11-07 02:33:06 +01:00
/* Copyright 2001 Matej Pfajfar.
* Copyright 2001 - 2004 Roger Dingledine .
2005-04-01 22:15:56 +02:00
* Copyright 2004 - 2005 Roger Dingledine , Nick Mathewson . */
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
/* See LICENSE for licensing information */
/* $Id$ */
2004-11-29 23:25:31 +01:00
const char buffers_c_id [ ] = " $Id$ " ;
2002-06-27 00:45:49 +02:00
2004-05-07 19:04:12 +02:00
/**
* \ file buffers . c
2005-06-11 07:31:17 +02:00
* \ brief Implements a generic buffer interface . Buffers are
* fairly opaque string holders that can read to or flush from :
* memory , file descriptors , or TLS connections .
2004-05-07 19:04:12 +02:00
* */
2002-06-27 00:45:49 +02:00
# include "or.h"
2005-04-27 21:40:31 +02:00
# define SENTINELS
2005-04-27 19:21:42 +02:00
# undef CHECK_AFTER_RESIZE
# undef PARANOIA
2005-04-27 21:40:31 +02:00
# undef NOINLINE
2005-04-26 22:53:22 +02:00
2005-04-26 23:29:20 +02:00
# ifdef SENTINELS
/* If SENTINELS is defined, check for attempts to write beyond the
2005-04-26 22:53:22 +02:00
* end / before the start of the buffer .
*/
# define START_MAGIC 0x70370370u
# define END_MAGIC 0xA0B0C0D0u
2005-04-27 22:02:52 +02:00
# define RAW_MEM(m) ((void*)(((char*)m)-4))
# define GUARDED_MEM(m) ((void*)(((char*)m)+4))
2005-04-26 22:53:22 +02:00
# define ALLOC_LEN(ln) ((ln)+8)
# define SET_GUARDS(m, ln) \
2005-04-26 22:55:32 +02:00
do { set_uint32 ( ( m ) - 4 , START_MAGIC ) ; set_uint32 ( ( m ) + ln , END_MAGIC ) ; } while ( 0 )
2005-04-26 22:53:22 +02:00
# else
# define RAW_MEM(m) (m)
# define GUARDED_MEM(m) (m)
# define ALLOC_LEN(ln) (ln)
2005-04-26 22:55:32 +02:00
# define SET_GUARDS(m,ln) do {} while (0)
2005-04-26 22:53:22 +02:00
# endif
2005-04-27 02:53:44 +02:00
# ifdef PARANOIA
# define check() do { assert_buf_ok(buf); } while (0)
# else
# define check() do { } while (0)
# endif
2005-04-27 21:40:31 +02:00
# ifdef NOINLINE
2005-04-27 02:53:44 +02:00
# undef INLINE
# define INLINE
2005-04-27 21:40:31 +02:00
# endif
2005-04-27 02:53:44 +02:00
2004-03-03 23:49:15 +01:00
# define BUFFER_MAGIC 0xB0FFF312u
2003-09-25 07:17:11 +02:00
struct buf_t {
2004-05-07 19:04:12 +02:00
uint32_t magic ; /**< Magic cookie for debugging: Must be set to BUFFER_MAGIC */
char * mem ; /**< Storage for data in the buffer */
2005-04-27 04:55:21 +02:00
char * cur ; /**< The first byte used for storing data in the buffer. */
2005-05-03 00:49:24 +02:00
size_t highwater ; /**< Largest observed datalen since last buf_shrink */
2004-05-09 18:47:25 +02:00
size_t len ; /**< Maximum amount of data that <b>mem</b> can hold. */
size_t datalen ; /**< Number of bytes currently in <b>mem</b>. */
2003-09-25 07:17:11 +02:00
} ;
2003-10-19 03:10:38 +02:00
2005-06-06 19:03:21 +02:00
uint64_t buf_total_used = 0 ;
uint64_t buf_total_alloc = 0 ;
2004-05-07 19:04:12 +02:00
/** Size, in bytes, for newly allocated buffers. Should be a power of 2. */
2003-10-19 03:10:38 +02:00
# define INITIAL_BUF_SIZE (4*1024)
2004-05-07 19:04:12 +02:00
/** Size, in bytes, for minimum 'shrink' size for buffers. Buffers may start
2003-10-14 03:34:31 +02:00
* out smaller than this , but they will never autoshrink to less
* than this size . */
2005-05-03 00:49:24 +02:00
# define MIN_GREEDY_SHRINK_SIZE (16*1024)
# define MIN_LAZY_SHRINK_SIZE (4*1024)
2003-09-25 07:17:11 +02:00
2005-04-26 23:29:20 +02:00
static INLINE void peek_from_buf ( char * string , size_t string_len , buf_t * buf ) ;
2005-04-26 22:53:22 +02:00
2005-06-08 19:27:11 +02:00
/** If the contents of buf wrap around the end of the allocated space,
* malloc a new buf and copy the contents in starting at the
* beginning . This operation is relatively expensive , so it shouldn ' t
* be used e . g . for every single read or write .
*/
2005-06-11 20:52:12 +02:00
static void
buf_normalize ( buf_t * buf )
2005-04-26 22:53:22 +02:00
{
2005-04-27 02:53:44 +02:00
check ( ) ;
2005-04-27 04:55:21 +02:00
if ( buf - > cur + buf - > datalen < = buf - > mem + buf - > len ) {
2005-04-26 22:53:22 +02:00
return ;
} else {
char * newmem ;
2005-04-27 04:55:21 +02:00
size_t sz = ( buf - > mem + buf - > len ) - buf - > cur ;
2005-04-26 22:53:22 +02:00
log_fn ( LOG_WARN , " Unexpected non-normalized buffer. " ) ;
newmem = GUARDED_MEM ( tor_malloc ( ALLOC_LEN ( buf - > len ) ) ) ;
SET_GUARDS ( newmem , buf - > len ) ;
2005-04-27 04:55:21 +02:00
memcpy ( newmem , buf - > cur , sz ) ;
2005-04-26 22:53:22 +02:00
memcpy ( newmem + sz , buf - > mem , buf - > datalen - sz ) ;
free ( RAW_MEM ( buf - > mem ) ) ;
2005-04-27 04:55:21 +02:00
buf - > mem = buf - > cur = newmem ;
2005-04-27 02:53:44 +02:00
check ( ) ;
2005-04-26 22:53:22 +02:00
}
}
/** Return the point in the buffer where the next byte will get stored. */
2005-06-11 20:52:12 +02:00
static INLINE char *
_buf_end ( buf_t * buf )
2005-04-26 22:53:22 +02:00
{
2005-04-27 04:55:21 +02:00
char * next = buf - > cur + buf - > datalen ;
2005-04-26 22:53:22 +02:00
char * end = buf - > mem + buf - > len ;
return ( next < end ) ? next : ( next - buf - > len ) ;
}
/** If the pointer <b>cp</b> has passed beyond the end of the buffer, wrap it
* around . */
2005-06-11 20:52:12 +02:00
static INLINE char *
2005-06-17 20:49:55 +02:00
_wrap_ptr ( buf_t * buf , char * cp )
{
2005-04-26 22:53:22 +02:00
return ( cp > = buf - > mem + buf - > len ) ? ( cp - buf - > len ) : cp ;
}
2005-06-17 20:49:55 +02:00
/** Return the offset of <b>cp</b> within the buffer. */
static INLINE int
_buf_offset ( buf_t * buf , char * cp )
{
if ( cp > = buf - > cur )
return cp - buf - > cur ;
else
/* return (cp - buf->mem) + buf->mem+buf->len - buf->cur */
return cp + buf - > len - buf - > cur ;
}
2005-04-26 22:53:22 +02:00
/** If the range of *<b>len</b> bytes starting at <b>at</b> wraps around the
* end of the buffer , then set * < b > len < / b > to the number of bytes starting
* at < b > at < / b > , and set * < b > more_len < / b > to the number of bytes starting
* at < b > buf - & gt ; mem < / b > . Otherwise , set * < b > more_len < / b > to 0.
*/
2005-06-11 20:52:12 +02:00
static INLINE void
_split_range ( buf_t * buf , char * at , size_t * len ,
2005-04-26 22:53:22 +02:00
size_t * more_len )
{
char * eos = at + * len ;
2005-04-27 02:53:44 +02:00
check ( ) ;
2005-04-26 22:53:22 +02:00
if ( eos > = ( buf - > mem + buf - > len ) ) {
* more_len = eos - ( buf - > mem + buf - > len ) ;
* len - = * more_len ;
} else {
* more_len = 0 ;
}
}
/** Change a buffer's capacity. <b>new_capacity</b> must be \>= buf->datalen. */
2005-06-11 20:52:12 +02:00
static void
buf_resize ( buf_t * buf , size_t new_capacity )
2003-10-14 03:34:31 +02:00
{
2005-04-26 22:53:22 +02:00
off_t offset ;
# ifdef CHECK_AFTER_RESIZE
char * tmp , * tmp2 ;
# endif
2004-04-25 22:37:37 +02:00
tor_assert ( buf - > datalen < = new_capacity ) ;
tor_assert ( new_capacity ) ;
2005-04-26 22:53:22 +02:00
# ifdef CHECK_AFTER_RESIZE
assert_buf_ok ( buf ) ;
tmp = tor_malloc ( buf - > datalen ) ;
tmp2 = tor_malloc ( buf - > datalen ) ;
peek_from_buf ( tmp , buf - > datalen , buf ) ;
# endif
2005-05-03 01:17:08 +02:00
if ( buf - > len = = new_capacity )
return ;
2005-04-27 04:55:21 +02:00
offset = buf - > cur - buf - > mem ;
2005-05-03 05:25:04 +02:00
if ( offset + buf - > datalen > new_capacity ) {
2005-04-26 22:53:22 +02:00
/* We need to move stuff before we shrink. */
2005-05-03 05:25:04 +02:00
if ( offset + buf - > datalen > buf - > len ) {
2005-04-26 22:53:22 +02:00
/* We have:
*
* mem [ 0 ] . . . mem [ datalen - ( len - offset ) ] ( end of data )
* mem [ offset ] . . . mem [ len - 1 ] ( the start of the data )
*
* We ' re shrinking the buffer by ( len - new_capacity ) bytes , so we need
* to move the start portion back by that many bytes .
*/
2005-04-27 04:55:21 +02:00
memmove ( buf - > cur - ( buf - > len - new_capacity ) , buf - > cur ,
2005-04-26 22:53:22 +02:00
buf - > len - offset ) ;
offset - = ( buf - > len - new_capacity ) ;
} else {
2005-06-08 19:27:11 +02:00
/* The data doesn't wrap around, but it does extend beyond the new
2005-04-26 22:53:22 +02:00
* buffer length :
* mem [ offset ] . . . mem [ offset + datalen - 1 ] ( the data )
*/
2005-04-27 04:55:21 +02:00
memmove ( buf - > mem , buf - > cur , buf - > datalen ) ;
2005-04-26 22:53:22 +02:00
offset = 0 ;
}
}
2005-06-08 19:27:11 +02:00
/* XXX Some play code to throw away old buffers sometimes rather
* than constantly reallocing them ; just in case this is our memory
* problem . It looks for now like it isn ' t , so disabled . - RD */
if ( 0 & & new_capacity = = MIN_LAZY_SHRINK_SIZE & &
! buf - > datalen & &
buf - > len > = 1 < < 16 ) {
/* don't realloc; free and malloc */
char * newmem = GUARDED_MEM ( tor_malloc ( ALLOC_LEN ( new_capacity ) ) ) ;
SET_GUARDS ( newmem , new_capacity ) ;
free ( RAW_MEM ( buf - > mem ) ) ;
buf - > mem = buf - > cur = newmem ;
} else {
buf - > mem = GUARDED_MEM ( tor_realloc ( RAW_MEM ( buf - > mem ) ,
ALLOC_LEN ( new_capacity ) ) ) ;
SET_GUARDS ( buf - > mem , new_capacity ) ;
buf - > cur = buf - > mem + offset ;
}
2005-06-09 05:57:54 +02:00
buf_total_alloc + = new_capacity ;
buf_total_alloc - = buf - > len ;
2005-06-08 19:27:11 +02:00
2005-05-03 05:25:04 +02:00
if ( offset + buf - > datalen > buf - > len ) {
2005-04-26 22:53:22 +02:00
/* We need to move data now that we are done growing. The buffer
* now contains :
*
* mem [ 0 ] . . . mem [ datalen - ( len - offset ) ] ( end of data )
* mem [ offset ] . . . mem [ len - 1 ] ( the start of the data )
* mem [ len ] . . . mem [ new_capacity ] ( empty space )
*
* We ' re growing by ( new_capacity - len ) bytes , so we need to move the
* end portion forward by that many bytes .
*/
2005-04-27 04:55:21 +02:00
memmove ( buf - > cur + ( new_capacity - buf - > len ) , buf - > cur ,
2005-04-26 22:53:22 +02:00
buf - > len - offset ) ;
2005-04-27 04:55:21 +02:00
buf - > cur + = new_capacity - buf - > len ;
2005-04-26 22:53:22 +02:00
}
2003-10-14 03:34:31 +02:00
buf - > len = new_capacity ;
2005-04-26 22:53:22 +02:00
# ifdef CHECK_AFTER_RESIZE
assert_buf_ok ( buf ) ;
peek_from_buf ( tmp2 , buf - > datalen , buf ) ;
if ( memcmp ( tmp , tmp2 , buf - > datalen ) ) {
tor_assert ( 0 ) ;
}
tor_free ( tmp ) ;
tor_free ( tmp2 ) ;
# endif
2003-10-14 03:34:31 +02:00
}
2004-05-09 18:47:25 +02:00
/** If the buffer is not large enough to hold <b>capacity</b> bytes, resize
2003-10-14 03:34:31 +02:00
* it so that it can . ( The new size will be a power of 2 times the old
* size . )
*/
2005-06-11 20:52:12 +02:00
static INLINE int
buf_ensure_capacity ( buf_t * buf , size_t capacity )
2003-10-14 03:34:31 +02:00
{
size_t new_len ;
2003-10-15 21:07:07 +02:00
if ( buf - > len > = capacity ) /* Don't grow if we're already big enough. */
2003-10-14 03:34:31 +02:00
return 0 ;
2003-10-15 21:07:07 +02:00
if ( capacity > MAX_BUF_SIZE ) /* Don't grow past the maximum. */
2003-10-14 03:34:31 +02:00
return - 1 ;
2003-10-15 21:07:07 +02:00
/* Find the smallest new_len equal to (2**X)*len for some X; such that
* new_len is at least capacity .
*/
2003-10-14 03:34:31 +02:00
new_len = buf - > len * 2 ;
while ( new_len < capacity )
new_len * = 2 ;
2003-10-15 21:07:07 +02:00
/* Resize the buffer. */
2003-10-14 05:06:48 +02:00
log_fn ( LOG_DEBUG , " Growing buffer from %d to %d bytes. " ,
( int ) buf - > len , ( int ) new_len ) ;
2003-10-14 03:34:31 +02:00
buf_resize ( buf , new_len ) ;
return 0 ;
}
2005-05-03 01:41:39 +02:00
/** Resize buf so it won't hold extra memory that we haven't been
* using lately ( that is , since the last time we called buf_shrink ) .
* Try to shrink the buf until it is the largest factor of two that
* can contain < b > buf < / b > - & gt ; highwater , but never smaller than
* MIN_LAZY_SHRINK_SIZE .
2005-05-03 01:39:09 +02:00
*/
2005-05-03 00:49:24 +02:00
void
buf_shrink ( buf_t * buf )
{
size_t new_len ;
2005-05-03 01:36:13 +02:00
new_len = buf - > len ;
2005-05-03 00:49:24 +02:00
while ( buf - > highwater < ( new_len > > 2 ) & & new_len > MIN_LAZY_SHRINK_SIZE * 2 )
new_len > > = 1 ;
2005-06-08 22:17:32 +02:00
buf - > highwater = buf - > datalen ;
2005-05-03 01:36:13 +02:00
if ( new_len = = buf - > len )
return ;
2005-05-03 00:49:24 +02:00
log_fn ( LOG_DEBUG , " Shrinking buffer from %d to %d bytes. " ,
( int ) buf - > len , ( int ) new_len ) ;
buf_resize ( buf , new_len ) ;
}
2005-06-11 23:17:38 +02:00
/** Remove the first <b>n</b> bytes from buf. */
2005-06-11 20:52:12 +02:00
static INLINE void
buf_remove_from_front ( buf_t * buf , size_t n ) {
2004-04-25 22:37:37 +02:00
tor_assert ( buf - > datalen > = n ) ;
2003-10-14 03:34:31 +02:00
buf - > datalen - = n ;
2005-06-06 19:03:21 +02:00
buf_total_used - = n ;
2005-05-03 01:32:23 +02:00
if ( buf - > datalen ) {
buf - > cur = _wrap_ptr ( buf , buf - > cur + n ) ;
} else {
buf - > cur = buf - > mem ;
}
2005-04-27 02:53:44 +02:00
check ( ) ;
2003-10-14 03:34:31 +02:00
}
2004-05-18 17:35:21 +02:00
/** Make sure that the memory in buf ends with a zero byte. */
2005-06-11 20:52:12 +02:00
static INLINE int
buf_nul_terminate ( buf_t * buf )
2003-09-25 07:17:11 +02:00
{
2004-05-19 21:28:24 +02:00
if ( buf_ensure_capacity ( buf , buf - > datalen + 1 ) < 0 )
2003-09-25 07:17:11 +02:00
return - 1 ;
2005-04-26 22:53:22 +02:00
* _buf_end ( buf ) = ' \0 ' ;
2004-05-18 17:35:21 +02:00
return 0 ;
2003-09-25 07:17:11 +02:00
}
2005-06-11 23:17:38 +02:00
/** Create and return a new buf with capacity <b>size</b>. */
2005-06-11 20:52:12 +02:00
buf_t *
buf_new_with_capacity ( size_t size ) {
2003-09-25 07:17:11 +02:00
buf_t * buf ;
2005-05-03 01:17:08 +02:00
buf = tor_malloc_zero ( sizeof ( buf_t ) ) ;
2004-03-03 23:49:15 +01:00
buf - > magic = BUFFER_MAGIC ;
2005-04-27 04:55:21 +02:00
buf - > cur = buf - > mem = GUARDED_MEM ( tor_malloc ( ALLOC_LEN ( size ) ) ) ;
2005-04-26 22:53:22 +02:00
SET_GUARDS ( buf - > mem , size ) ;
2003-09-25 07:17:11 +02:00
buf - > len = size ;
2005-06-06 19:03:21 +02:00
buf_total_alloc + = size ;
2004-04-25 22:37:37 +02:00
assert_buf_ok ( buf ) ;
2003-09-25 07:17:11 +02:00
return buf ;
}
2002-06-27 00:45:49 +02:00
2004-05-07 19:04:12 +02:00
/** Allocate and return a new buffer with default capacity. */
2005-06-11 20:52:12 +02:00
buf_t *
buf_new ( void )
2003-09-25 07:17:11 +02:00
{
2003-10-14 03:34:31 +02:00
return buf_new_with_capacity ( INITIAL_BUF_SIZE ) ;
2003-09-25 07:17:11 +02:00
}
2002-06-27 00:45:49 +02:00
2005-06-11 23:17:38 +02:00
/** Remove all data from <b>buf</b>. */
2005-06-11 20:52:12 +02:00
void
buf_clear ( buf_t * buf )
2004-02-28 20:14:11 +01:00
{
2005-06-06 19:03:21 +02:00
buf_total_used - = buf - > datalen ;
2004-02-28 20:14:11 +01:00
buf - > datalen = 0 ;
2005-04-27 04:55:21 +02:00
buf - > cur = buf - > mem ;
2004-02-28 20:14:11 +01:00
}
2002-06-27 00:45:49 +02:00
2004-05-09 18:47:25 +02:00
/** Return the number of bytes stored in <b>buf</b> */
2005-06-11 20:52:12 +02:00
size_t
buf_datalen ( const buf_t * buf )
2003-09-25 07:17:11 +02:00
{
return buf - > datalen ;
2002-06-27 00:45:49 +02:00
}
2004-05-09 18:47:25 +02:00
/** Return the maximum bytes that can be stored in <b>buf</b> before buf
2004-05-02 00:08:43 +02:00
* needs to resize . */
2005-06-11 20:52:12 +02:00
size_t
buf_capacity ( const buf_t * buf )
2003-09-25 07:17:11 +02:00
{
return buf - > len ;
}
2005-06-11 23:17:38 +02:00
/** For testing only: Return a pointer to the raw memory stored in
* < b > buf < / b > . */
2005-06-11 20:52:12 +02:00
const char *
_buf_peek_raw_buffer ( const buf_t * buf )
2003-09-25 07:17:11 +02:00
{
2005-04-27 04:55:21 +02:00
return buf - > cur ;
2003-09-25 07:17:11 +02:00
}
2005-06-11 23:17:38 +02:00
/** Release storage held by <b>buf</b>. */
2005-06-11 20:52:12 +02:00
void
buf_free ( buf_t * buf )
{
2004-03-03 23:49:15 +01:00
assert_buf_ok ( buf ) ;
buf - > magic = 0xDEADBEEF ;
2005-04-26 22:53:22 +02:00
free ( RAW_MEM ( buf - > mem ) ) ;
2005-06-06 19:03:21 +02:00
buf_total_alloc - = buf - > len ;
buf_total_used - = buf - > datalen ;
2004-03-03 23:49:15 +01:00
tor_free ( buf ) ;
2002-06-27 00:45:49 +02:00
}
2005-06-11 23:17:38 +02:00
/** Helper for read_to_buf(): read no more than at_most bytes from
2005-06-11 20:52:12 +02:00
* socket s into buffer buf , starting at the position pos . ( Does not
* check for overflow . ) Set * reached_eof to true on EOF . Return
* number of bytes read on success , 0 if the read would block , - 1 on
* failure .
*/
static INLINE int
read_to_buf_impl ( int s , size_t at_most , buf_t * buf ,
char * pos , int * reached_eof )
2005-04-26 22:53:22 +02:00
{
int read_result ;
// log_fn(LOG_DEBUG,"reading at most %d bytes.",at_most);
read_result = recv ( s , pos , at_most , 0 ) ;
if ( read_result < 0 ) {
int e = tor_socket_errno ( s ) ;
if ( ! ERRNO_IS_EAGAIN ( e ) ) { /* it's a real error */
return - 1 ;
}
return 0 ; /* would block. */
} else if ( read_result = = 0 ) {
log_fn ( LOG_DEBUG , " Encountered eof " ) ;
* reached_eof = 1 ;
return 0 ;
} else { /* we read some bytes */
buf - > datalen + = read_result ;
2005-06-06 19:03:21 +02:00
buf_total_used + = read_result ;
2005-05-03 00:49:24 +02:00
if ( buf - > datalen > buf - > highwater )
buf - > highwater = buf - > datalen ;
2005-04-26 22:53:22 +02:00
log_fn ( LOG_DEBUG , " Read %d bytes. %d on inbuf. " , read_result ,
( int ) buf - > datalen ) ;
return read_result ;
}
}
2004-05-10 09:27:29 +02:00
/** Read from socket <b>s</b>, writing onto end of <b>buf</b>. Read at most
2004-11-21 12:20:28 +01:00
* < b > at_most < / b > bytes , resizing the buffer as necessary . If recv ( )
2005-06-11 23:17:38 +02:00
* returns 0 , set * < b > reached_eof < / b > to 1 and return 0. Return - 1 on error ;
2004-11-21 12:20:28 +01:00
* else return the number of bytes read . Return 0 if recv ( ) would
2004-05-02 00:08:43 +02:00
* block .
2003-03-04 05:36:37 +01:00
*/
2005-06-11 20:52:12 +02:00
int
read_to_buf ( int s , size_t at_most , buf_t * buf , int * reached_eof )
2005-04-26 22:53:22 +02:00
{
int r ;
char * next ;
size_t at_start ;
2002-06-27 00:45:49 +02:00
2004-04-25 22:37:37 +02:00
assert_buf_ok ( buf ) ;
2004-10-17 00:14:52 +02:00
tor_assert ( reached_eof ) ;
tor_assert ( s > = 0 ) ;
2002-06-27 00:45:49 +02:00
2003-10-14 03:34:31 +02:00
if ( buf_ensure_capacity ( buf , buf - > datalen + at_most ) )
return - 1 ;
2002-06-27 00:45:49 +02:00
2004-11-28 10:05:49 +01:00
if ( at_most + buf - > datalen > buf - > len )
2003-09-25 07:17:11 +02:00
at_most = buf - > len - buf - > datalen ; /* take the min of the two */
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
2004-11-28 10:05:49 +01:00
if ( at_most = = 0 )
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
return 0 ; /* we shouldn't read anything */
2005-04-26 22:53:22 +02:00
next = _buf_end ( buf ) ;
_split_range ( buf , next , & at_most , & at_start ) ;
r = read_to_buf_impl ( s , at_most , buf , next , reached_eof ) ;
2005-04-27 02:53:44 +02:00
check ( ) ;
2005-04-26 22:53:22 +02:00
if ( r < 0 | | ( size_t ) r < at_most ) {
return r ; /* Either error, eof, block, or no more to read. */
}
if ( at_start ) {
int r2 ;
tor_assert ( _buf_end ( buf ) = = buf - > mem ) ;
2005-04-27 04:55:21 +02:00
r2 = read_to_buf_impl ( s , at_start , buf , buf - > mem , reached_eof ) ;
2005-04-27 02:53:44 +02:00
check ( ) ;
2005-04-26 22:53:22 +02:00
if ( r2 < 0 ) {
return r2 ;
} else {
r + = r2 ;
2002-06-27 00:45:49 +02:00
}
}
2005-04-26 22:53:22 +02:00
return r ;
}
2005-06-11 23:17:38 +02:00
/** Helper for read_to_buf_tls(): read no more than <b>at_most</b>
* bytes from the TLS connection < b > tls < / b > into buffer < b > buf < / b > ,
* starting at the position < b > next < / b > . ( Does not check for overflow . )
* Return number of bytes read on success , 0 if the read would block ,
* - 1 on failure .
2005-06-11 20:52:12 +02:00
*/
2005-04-26 22:53:22 +02:00
static INLINE int
read_to_buf_tls_impl ( tor_tls * tls , size_t at_most , buf_t * buf , char * next )
{
int r ;
log_fn ( LOG_DEBUG , " before: %d on buf, %d pending, at_most %d. " ,
( int ) buf_datalen ( buf ) , ( int ) tor_tls_get_pending_bytes ( tls ) ,
( int ) at_most ) ;
r = tor_tls_read ( tls , next , at_most ) ;
if ( r < 0 )
return r ;
buf - > datalen + = r ;
2005-06-06 19:03:21 +02:00
buf_total_used + = r ;
2005-05-03 00:49:24 +02:00
if ( buf - > datalen > buf - > highwater )
buf - > highwater = buf - > datalen ;
2005-04-26 22:53:22 +02:00
log_fn ( LOG_DEBUG , " Read %d bytes. %d on inbuf; %d pending " , r ,
( int ) buf - > datalen , ( int ) tor_tls_get_pending_bytes ( tls ) ) ;
return r ;
2002-06-27 00:45:49 +02:00
}
2004-05-07 19:04:12 +02:00
/** As read_to_buf, but reads from a TLS connection.
2005-06-11 08:07:22 +02:00
*
* Using TLS on OR connections complicates matters in two ways .
*
* First , a TLS stream has its own read buffer independent of the
* connection ' s read buffer . ( TLS needs to read an entire frame from
* the network before it can decrypt any data . Thus , trying to read 1
* byte from TLS can require that several KB be read from the network
* and decrypted . The extra data is stored in TLS ' s decrypt buffer . )
* Because the data hasn ' t been read by Tor ( it ' s still inside the TLS ) ,
* this means that sometimes a connection " has stuff to read " even when
* poll ( ) didn ' t return POLLIN . The tor_tls_get_pending_bytes function is
* used in connection . c to detect TLS objects with non - empty internal
* buffers and read from them again .
*
* Second , the TLS stream ' s events do not correspond directly to network
* events : sometimes , before a TLS stream can read , the network must be
* ready to write - - or vice versa .
2004-05-02 00:08:43 +02:00
*/
2005-06-11 20:52:12 +02:00
int
read_to_buf_tls ( tor_tls * tls , size_t at_most , buf_t * buf )
{
2003-09-04 18:05:08 +02:00
int r ;
2005-04-26 22:53:22 +02:00
char * next ;
size_t at_start ;
2004-04-25 22:37:37 +02:00
tor_assert ( tls ) ;
assert_buf_ok ( buf ) ;
2003-10-14 03:34:31 +02:00
2004-05-05 23:32:43 +02:00
log_fn ( LOG_DEBUG , " start: %d on buf, %d pending, at_most %d. " ,
( int ) buf_datalen ( buf ) , ( int ) tor_tls_get_pending_bytes ( tls ) ,
( int ) at_most ) ;
2004-04-27 00:22:18 +02:00
2003-10-14 03:34:31 +02:00
if ( buf_ensure_capacity ( buf , at_most + buf - > datalen ) )
2004-04-27 01:00:07 +02:00
return TOR_TLS_ERROR ;
2003-12-14 05:18:43 +01:00
2004-04-28 22:13:21 +02:00
if ( at_most + buf - > datalen > buf - > len )
2003-09-25 07:17:11 +02:00
at_most = buf - > len - buf - > datalen ;
2003-09-04 18:05:08 +02:00
if ( at_most = = 0 )
return 0 ;
2003-12-14 05:18:43 +01:00
2005-04-26 22:53:22 +02:00
next = _buf_end ( buf ) ;
_split_range ( buf , next , & at_most , & at_start ) ;
r = read_to_buf_tls_impl ( tls , at_most , buf , next ) ;
2005-04-27 02:53:44 +02:00
check ( ) ;
2005-04-26 22:53:22 +02:00
if ( r < 0 | | ( size_t ) r < at_most )
return r ; /* Either error, eof, block, or no more to read. */
if ( at_start ) {
int r2 ;
tor_assert ( _buf_end ( buf ) = = buf - > mem ) ;
2005-04-26 23:20:22 +02:00
r2 = read_to_buf_tls_impl ( tls , at_start , buf , buf - > mem ) ;
2005-04-27 02:53:44 +02:00
check ( ) ;
2005-04-26 22:53:22 +02:00
if ( r2 < 0 )
return r2 ;
else
r + = r2 ;
}
2003-09-04 18:05:08 +02:00
return r ;
2003-12-17 22:09:31 +01:00
}
2003-09-04 18:05:08 +02:00
2005-06-11 23:17:38 +02:00
/** Helper for flush_buf(): try to write <b>sz</b> bytes from buffer
* < b > buf < / b > onto socket < b > s < / b > . On success , deduct the bytes written
* from * < b > buf_flushlen < / b > .
2005-06-11 20:52:12 +02:00
* Return the number of bytes written on success , - 1 on failure .
*/
2005-04-26 22:53:22 +02:00
static INLINE int
flush_buf_impl ( int s , buf_t * buf , size_t sz , size_t * buf_flushlen )
{
int write_result ;
2005-04-27 04:55:21 +02:00
write_result = send ( s , buf - > cur , sz , 0 ) ;
2005-04-26 22:53:22 +02:00
if ( write_result < 0 ) {
int e = tor_socket_errno ( s ) ;
if ( ! ERRNO_IS_EAGAIN ( e ) ) { /* it's a real error */
return - 1 ;
}
log_fn ( LOG_DEBUG , " write() would block, returning. " ) ;
return 0 ;
} else {
* buf_flushlen - = write_result ;
buf_remove_from_front ( buf , write_result ) ;
return write_result ;
}
}
2004-05-09 18:47:25 +02:00
/** Write data from <b>buf</b> to the socket <b>s</b>. Write at most
2005-06-11 23:17:38 +02:00
* * < b > buf_flushlen < / b > bytes , decrement * < b > buf_flushlen < / b > by
2004-05-09 18:47:25 +02:00
* the number of bytes actually written , and remove the written bytes
* from the buffer . Return the number of bytes written on success ,
* - 1 on failure . Return 0 if write ( ) would block .
2004-05-02 00:08:43 +02:00
*/
2005-06-11 20:52:12 +02:00
int
flush_buf ( int s , buf_t * buf , size_t * buf_flushlen )
2003-09-25 07:17:11 +02:00
{
2005-04-26 22:53:22 +02:00
int r ;
size_t flushed = 0 ;
size_t flushlen0 , flushlen1 ;
2002-06-27 00:45:49 +02:00
2004-04-25 22:37:37 +02:00
assert_buf_ok ( buf ) ;
2004-10-17 00:14:52 +02:00
tor_assert ( buf_flushlen ) ;
tor_assert ( s > = 0 ) ;
tor_assert ( * buf_flushlen < = buf - > datalen ) ;
2002-06-27 00:45:49 +02:00
2004-11-28 10:05:49 +01:00
if ( * buf_flushlen = = 0 ) /* nothing to flush */
2002-06-27 00:45:49 +02:00
return 0 ;
2005-04-26 22:53:22 +02:00
flushlen0 = * buf_flushlen ;
2005-04-27 04:55:21 +02:00
_split_range ( buf , buf - > cur , & flushlen0 , & flushlen1 ) ;
2005-04-26 22:53:22 +02:00
r = flush_buf_impl ( s , buf , flushlen0 , buf_flushlen ) ;
2005-04-27 02:53:44 +02:00
check ( ) ;
2005-04-26 22:53:22 +02:00
log_fn ( LOG_DEBUG , " %d: flushed %d bytes, %d ready to flush, %d remain. " ,
s , r , ( int ) * buf_flushlen , ( int ) buf - > datalen ) ;
if ( r < 0 | | ( size_t ) r < flushlen0 )
return r ; /* Error, or can't flush any more now. */
flushed = r ;
if ( flushlen1 ) {
2005-04-27 04:55:21 +02:00
tor_assert ( buf - > cur = = buf - > mem ) ;
2005-04-26 22:53:22 +02:00
r = flush_buf_impl ( s , buf , flushlen1 , buf_flushlen ) ;
2005-04-27 02:53:44 +02:00
check ( ) ;
2003-09-18 10:11:31 +02:00
log_fn ( LOG_DEBUG , " %d: flushed %d bytes, %d ready to flush, %d remain. " ,
2005-04-26 22:53:22 +02:00
s , r , ( int ) * buf_flushlen , ( int ) buf - > datalen ) ;
if ( r < 0 )
return r ;
flushed + = r ;
}
return flushed ;
}
2003-10-14 03:34:31 +02:00
2005-06-11 23:17:38 +02:00
/** Helper for flush_buf_tls(): try to write <b>sz</b> bytes from buffer
* < b > buf < / b > onto TLS object < b > tls < / b > . On success , deduct the bytes
* written from * < b > buf_flushlen < / b > .
2005-06-11 20:52:12 +02:00
* Return the number of bytes written on success , - 1 on failure .
*/
2005-04-26 22:53:22 +02:00
static INLINE int
flush_buf_tls_impl ( tor_tls * tls , buf_t * buf , size_t sz , size_t * buf_flushlen )
{
int r ;
2005-04-27 04:55:21 +02:00
r = tor_tls_write ( tls , buf - > cur , sz ) ;
2005-04-26 22:53:22 +02:00
if ( r < 0 ) {
return r ;
2002-06-27 00:45:49 +02:00
}
2005-04-26 22:53:22 +02:00
* buf_flushlen - = r ;
buf_remove_from_front ( buf , r ) ;
log_fn ( LOG_DEBUG , " flushed %d bytes, %d ready to flush, %d remain. " ,
r , ( int ) * buf_flushlen , ( int ) buf - > datalen ) ;
return r ;
2002-06-27 00:45:49 +02:00
}
2005-06-11 23:17:38 +02:00
/** As flush_buf(), but writes data to a TLS connection.
2004-05-02 00:08:43 +02:00
*/
2004-10-14 04:47:09 +02:00
int flush_buf_tls ( tor_tls * tls , buf_t * buf , size_t * buf_flushlen )
2003-09-04 18:05:08 +02:00
{
int r ;
2005-04-26 22:53:22 +02:00
size_t flushed = 0 ;
size_t flushlen0 , flushlen1 ;
2004-04-25 22:37:37 +02:00
assert_buf_ok ( buf ) ;
2004-10-17 00:14:52 +02:00
tor_assert ( tls ) ;
tor_assert ( buf_flushlen ) ;
2003-09-07 12:24:40 +02:00
/* we want to let tls write even if flushlen is zero, because it might
* have a partial record pending */
2005-04-23 16:26:02 +02:00
check_no_tls_errors ( ) ;
2005-04-26 22:53:22 +02:00
flushlen0 = * buf_flushlen ;
2005-04-27 04:55:21 +02:00
_split_range ( buf , buf - > cur , & flushlen0 , & flushlen1 ) ;
2005-04-26 22:53:22 +02:00
r = flush_buf_tls_impl ( tls , buf , flushlen0 , buf_flushlen ) ;
2005-04-27 02:53:44 +02:00
check ( ) ;
2005-04-26 22:53:22 +02:00
if ( r < 0 | | ( size_t ) r < flushlen0 )
return r ; /* Error, or can't flush any more now. */
flushed = r ;
if ( flushlen1 ) {
2005-04-27 04:55:21 +02:00
tor_assert ( buf - > cur = = buf - > mem ) ;
2005-04-26 22:53:22 +02:00
r = flush_buf_tls_impl ( tls , buf , flushlen1 , buf_flushlen ) ;
2005-04-27 02:53:44 +02:00
check ( ) ;
2005-04-26 22:53:22 +02:00
if ( r < 0 )
return r ;
flushed + = r ;
2003-09-04 18:05:08 +02:00
}
2005-04-26 22:53:22 +02:00
return flushed ;
2003-09-04 18:05:08 +02:00
}
2004-05-09 18:47:25 +02:00
/** Append <b>string_len</b> bytes from <b>string</b> to the end of
* < b > buf < / b > .
*
2004-05-02 00:08:43 +02:00
* Return the new length of the buffer on success , - 1 on failure .
*/
2005-04-26 22:53:22 +02:00
int
write_to_buf ( const char * string , size_t string_len , buf_t * buf )
{
char * next ;
size_t len2 ;
2002-06-27 00:45:49 +02:00
/* append string to buf (growing as needed, return -1 if "too big")
* return total number of bytes on the buf
*/
2004-04-25 22:37:37 +02:00
tor_assert ( string ) ;
assert_buf_ok ( buf ) ;
2002-06-27 00:45:49 +02:00
2003-12-14 09:15:41 +01:00
if ( buf_ensure_capacity ( buf , buf - > datalen + string_len ) ) {
2004-10-14 05:18:14 +02:00
log_fn ( LOG_WARN , " buflen too small, can't hold %d bytes. " , ( int ) ( buf - > datalen + string_len ) ) ;
2003-10-14 03:34:31 +02:00
return - 1 ;
2003-12-14 09:15:41 +01:00
}
2003-10-14 03:34:31 +02:00
2005-04-26 22:53:22 +02:00
next = _buf_end ( buf ) ;
_split_range ( buf , next , & string_len , & len2 ) ;
memcpy ( next , string , string_len ) ;
2003-09-25 07:17:11 +02:00
buf - > datalen + = string_len ;
2005-06-06 19:03:21 +02:00
buf_total_used + = string_len ;
2005-04-26 22:53:22 +02:00
if ( len2 ) {
tor_assert ( _buf_end ( buf ) = = buf - > mem ) ;
memcpy ( buf - > mem , string + string_len , len2 ) ;
buf - > datalen + = len2 ;
2005-06-08 23:55:02 +02:00
buf_total_used + = len2 ;
2005-04-26 22:53:22 +02:00
}
2005-05-03 00:49:24 +02:00
if ( buf - > datalen > buf - > highwater )
buf - > highwater = buf - > datalen ;
2005-06-09 05:57:54 +02:00
log_fn ( LOG_DEBUG , " added %d bytes to buf (now %d total). " ,
( int ) string_len , ( int ) buf - > datalen ) ;
2005-04-27 02:53:44 +02:00
check ( ) ;
2003-09-25 07:17:11 +02:00
return buf - > datalen ;
2003-03-17 03:42:45 +01:00
}
2005-06-11 23:17:38 +02:00
/** Helper: copy the first <b>string_len</b> bytes from <b>buf</b>
* onto < b > string < / b > .
2005-06-11 20:52:12 +02:00
*/
static INLINE void
peek_from_buf ( char * string , size_t string_len , buf_t * buf )
2005-04-26 22:53:22 +02:00
{
size_t len2 ;
2002-06-27 00:45:49 +02:00
2003-06-25 02:31:41 +02:00
/* There must be string_len bytes in buf; write them onto string,
2003-04-15 21:10:18 +02:00
* then memmove buf back ( that is , remove them from buf ) .
*
* Return the number of bytes still on the buffer . */
2002-06-27 00:45:49 +02:00
2004-04-25 22:37:37 +02:00
tor_assert ( string ) ;
tor_assert ( string_len < = buf - > datalen ) ; /* make sure we don't ask for too much */
assert_buf_ok ( buf ) ;
2002-06-27 00:45:49 +02:00
2005-04-27 04:55:21 +02:00
_split_range ( buf , buf - > cur , & string_len , & len2 ) ;
2005-04-26 22:53:22 +02:00
2005-04-27 04:55:21 +02:00
memcpy ( string , buf - > cur , string_len ) ;
2005-04-26 22:53:22 +02:00
if ( len2 ) {
memcpy ( string + string_len , buf - > mem , len2 ) ;
}
}
/** Remove <b>string_len</b> bytes from the front of <b>buf</b>, and store them
* into < b > string < / b > . Return the new buffer size . < b > string_len < / b > must be \ < =
* the number of bytes on the buffer .
*/
2005-06-11 20:52:12 +02:00
int
fetch_from_buf ( char * string , size_t string_len , buf_t * buf )
2005-04-26 22:53:22 +02:00
{
/* There must be string_len bytes in buf; write them onto string,
* then memmove buf back ( that is , remove them from buf ) .
*
* Return the number of bytes still on the buffer . */
2005-04-27 02:53:44 +02:00
check ( ) ;
2005-04-26 22:53:22 +02:00
peek_from_buf ( string , string_len , buf ) ;
2003-10-14 03:34:31 +02:00
buf_remove_from_front ( buf , string_len ) ;
2005-04-27 02:53:44 +02:00
check ( ) ;
2003-09-25 07:17:11 +02:00
return buf - > datalen ;
2002-06-27 00:45:49 +02:00
}
2004-05-09 18:47:25 +02:00
/** There is a (possibly incomplete) http statement on <b>buf</b>, of the
2004-05-07 19:04:12 +02:00
* form " \ %s \\ r \\ n \\ r \\ n \ %s " , headers , body . ( body may contain nuls . )
2003-09-17 22:09:06 +02:00
* If a ) the headers include a Content - Length field and all bytes in
* the body are present , or b ) there ' s no Content - Length field and
* all headers are present , then :
2004-03-31 07:01:30 +02:00
*
2004-05-09 18:47:25 +02:00
* - strdup headers into < b > * headers_out < / b > , and nul - terminate it .
* - memdup body into < b > * body_out < / b > , and nul - terminate it .
* - Then remove them from < b > buf < / b > , and return 1.
2004-05-07 19:04:12 +02:00
*
* - If headers or body is NULL , discard that part of the buf .
* - If a headers or body doesn ' t fit in the arg , return - 1.
2004-10-12 22:22:09 +02:00
* ( We ensure that the headers or body don ' t exceed max len ,
* _even if_ we ' re planning to discard them . )
2003-12-17 22:09:31 +01:00
*
2003-09-17 22:09:06 +02:00
* Else , change nothing and return 0.
*/
2005-06-11 20:52:12 +02:00
int
fetch_from_buf_http ( buf_t * buf ,
char * * headers_out , size_t max_headerlen ,
char * * body_out , size_t * body_used , size_t max_bodylen )
{
2004-05-18 17:35:21 +02:00
char * headers , * body , * p ;
2004-10-14 04:47:09 +02:00
size_t headerlen , bodylen , contentlen ;
2003-09-17 22:09:06 +02:00
2004-04-25 22:37:37 +02:00
assert_buf_ok ( buf ) ;
2005-04-26 22:53:22 +02:00
buf_normalize ( buf ) ;
2003-09-17 22:09:06 +02:00
2004-05-18 17:35:21 +02:00
if ( buf_nul_terminate ( buf ) < 0 ) {
log_fn ( LOG_WARN , " Couldn't nul-terminate buffer " ) ;
return - 1 ;
}
2005-04-27 04:55:21 +02:00
headers = buf - > cur ;
2004-05-18 17:35:21 +02:00
body = strstr ( headers , " \r \n \r \n " ) ;
if ( ! body ) {
2003-09-17 22:09:06 +02:00
log_fn ( LOG_DEBUG , " headers not all here yet. " ) ;
return 0 ;
}
2004-05-18 17:35:21 +02:00
body + = 4 ; /* Skip the the CRLFCRLF */
2003-09-17 22:09:06 +02:00
headerlen = body - headers ; /* includes the CRLFCRLF */
2003-09-25 07:17:11 +02:00
bodylen = buf - > datalen - headerlen ;
2004-10-14 05:18:14 +02:00
log_fn ( LOG_DEBUG , " headerlen %d, bodylen %d. " , ( int ) headerlen , ( int ) bodylen ) ;
2003-09-17 22:09:06 +02:00
2004-11-28 10:05:49 +01:00
if ( max_headerlen < = headerlen ) {
2004-10-14 05:18:14 +02:00
log_fn ( LOG_WARN , " headerlen %d larger than %d. Failing. " , ( int ) headerlen ,
( int ) max_headerlen - 1 ) ;
2003-09-17 22:09:06 +02:00
return - 1 ;
}
2004-11-28 10:05:49 +01:00
if ( max_bodylen < = bodylen ) {
2004-10-14 05:18:14 +02:00
log_fn ( LOG_WARN , " bodylen %d larger than %d. Failing. " , ( int ) bodylen , ( int ) max_bodylen - 1 ) ;
2003-09-17 22:09:06 +02:00
return - 1 ;
}
2003-09-21 08:15:43 +02:00
# define CONTENT_LENGTH "\r\nContent-Length: "
2004-05-18 17:35:21 +02:00
p = strstr ( headers , CONTENT_LENGTH ) ;
if ( p ) {
2004-10-14 04:47:09 +02:00
int i ;
i = atoi ( p + strlen ( CONTENT_LENGTH ) ) ;
if ( i < 0 ) {
2004-10-12 20:38:36 +02:00
log_fn ( LOG_WARN , " Content-Length is less than zero; it looks like someone is trying to crash us. " ) ;
return - 1 ;
}
2004-10-14 04:47:09 +02:00
contentlen = i ;
2004-03-31 07:01:30 +02:00
/* if content-length is malformed, then our body length is 0. fine. */
2004-10-14 05:18:14 +02:00
log_fn ( LOG_DEBUG , " Got a contentlen of %d. " , ( int ) contentlen ) ;
2004-11-28 10:05:49 +01:00
if ( bodylen < contentlen ) {
2003-09-17 22:09:06 +02:00
log_fn ( LOG_DEBUG , " body not all here yet. " ) ;
return 0 ; /* not all there yet */
}
2004-11-28 10:05:49 +01:00
if ( bodylen > contentlen ) {
2004-04-26 23:15:06 +02:00
bodylen = contentlen ;
2004-10-14 05:18:14 +02:00
log_fn ( LOG_DEBUG , " bodylen reduced to %d. " , ( int ) bodylen ) ;
2004-04-26 23:15:06 +02:00
}
2003-09-17 22:09:06 +02:00
}
/* all happy. copy into the appropriate places, and return 1 */
2004-11-28 10:05:49 +01:00
if ( headers_out ) {
2003-12-17 10:42:28 +01:00
* headers_out = tor_malloc ( headerlen + 1 ) ;
2005-04-27 04:55:21 +02:00
memcpy ( * headers_out , buf - > cur , headerlen ) ;
2003-12-19 06:09:51 +01:00
( * headers_out ) [ headerlen ] = 0 ; /* null terminate it */
2003-09-17 22:09:06 +02:00
}
2004-11-28 10:05:49 +01:00
if ( body_out ) {
2004-04-25 22:37:37 +02:00
tor_assert ( body_used ) ;
2004-03-31 07:01:30 +02:00
* body_used = bodylen ;
2003-12-17 10:42:28 +01:00
* body_out = tor_malloc ( bodylen + 1 ) ;
2005-04-27 04:55:21 +02:00
memcpy ( * body_out , buf - > cur + headerlen , bodylen ) ;
2004-03-31 07:10:34 +02:00
( * body_out ) [ bodylen ] = 0 ; /* null terminate it */
2003-09-17 22:09:06 +02:00
}
2003-10-14 03:34:31 +02:00
buf_remove_from_front ( buf , headerlen + bodylen ) ;
2003-09-17 22:09:06 +02:00
return 1 ;
}
2004-05-09 18:47:25 +02:00
/** There is a (possibly incomplete) socks handshake on <b>buf</b>, of one
2003-10-04 03:37:01 +02:00
* of the forms
2004-05-07 19:04:12 +02:00
* - socks4 : " socksheader username \\ 0 "
* - socks4a : " socksheader username \\ 0 destaddr \\ 0 "
* - socks5 phase one : " version #methods methods "
* - socks5 phase two : " version command 0 addresstype... "
2003-11-11 03:41:31 +01:00
* If it ' s a complete and valid handshake , and destaddr fits in
* MAX_SOCKS_ADDR_LEN bytes , then pull the handshake off the buf ,
2004-05-09 18:47:25 +02:00
* assign to < b > req < / b > , and return 1.
2004-05-07 19:04:12 +02:00
*
2003-09-18 10:11:31 +02:00
* If it ' s invalid or too big , return - 1.
2004-05-07 19:04:12 +02:00
*
2003-10-04 03:37:01 +02:00
* Else it ' s not all there yet , leave buf alone and return 0.
2004-05-07 19:04:12 +02:00
*
2004-05-09 18:47:25 +02:00
* If you want to specify the socks reply , write it into < b > req - > reply < / b >
* and set < b > req - > replylen < / b > , else leave < b > req - > replylen < / b > alone .
2004-05-07 19:04:12 +02:00
*
2004-05-09 18:47:25 +02:00
* If returning 0 or - 1 , < b > req - > address < / b > and < b > req - > port < / b > are undefined .
2003-09-18 10:11:31 +02:00
*/
2005-06-11 20:52:12 +02:00
int
fetch_from_buf_socks ( buf_t * buf , socks_request_t * req )
{
2003-10-04 03:37:01 +02:00
unsigned char len ;
2005-02-22 09:18:36 +01:00
char tmpbuf [ INET_NTOA_BUF_LEN ] ;
2003-10-04 03:37:01 +02:00
uint32_t destip ;
enum { socks4 , socks4a } socks4_prot = socks4a ;
2003-09-18 10:11:31 +02:00
char * next , * startaddr ;
2003-10-04 03:37:01 +02:00
struct in_addr in ;
2003-09-18 10:11:31 +02:00
2004-08-04 03:08:10 +02:00
/* If the user connects with socks4 or the wrong variant of socks5,
* then log a warning to let him know that it might be unwise . */
static int have_warned_about_unsafe_socks = 0 ;
2004-11-28 10:05:49 +01:00
if ( buf - > datalen < 2 ) /* version and another byte */
2003-09-18 10:11:31 +02:00
return 0 ;
2005-04-26 22:53:22 +02:00
buf_normalize ( buf ) ;
2005-04-27 04:55:21 +02:00
switch ( * ( buf - > cur ) ) { /* which version of socks? */
2003-10-04 03:37:01 +02:00
case 5 : /* socks5 */
2004-11-28 10:05:49 +01:00
if ( req - > socks_version ! = 5 ) { /* we need to negotiate a method */
2005-04-27 04:55:21 +02:00
unsigned char nummethods = ( unsigned char ) * ( buf - > cur + 1 ) ;
2004-04-25 22:37:37 +02:00
tor_assert ( ! req - > socks_version ) ;
2004-11-28 10:05:49 +01:00
if ( buf - > datalen < 2u + nummethods )
2003-10-04 03:37:01 +02:00
return 0 ;
2005-04-27 04:55:21 +02:00
if ( ! nummethods | | ! memchr ( buf - > cur + 2 , 0 , nummethods ) ) {
2003-10-10 03:48:32 +02:00
log_fn ( LOG_WARN , " socks5: offered methods don't include 'no auth'. Rejecting. " ) ;
2003-11-11 03:41:31 +01:00
req - > replylen = 2 ; /* 2 bytes of response */
2005-03-01 23:16:15 +01:00
req - > reply [ 0 ] = 5 ;
2004-03-09 23:01:17 +01:00
req - > reply [ 1 ] = ' \xFF ' ; /* reject all methods */
2003-10-04 03:37:01 +02:00
return - 1 ;
2003-11-16 18:00:02 +01:00
}
2003-10-14 03:34:31 +02:00
buf_remove_from_front ( buf , 2 + nummethods ) ; /* remove packet from buf */
2003-10-04 03:37:01 +02:00
2003-11-11 03:41:31 +01:00
req - > replylen = 2 ; /* 2 bytes of response */
req - > reply [ 0 ] = 5 ; /* socks5 reply */
2005-03-01 23:16:15 +01:00
req - > reply [ 1 ] = SOCKS5_SUCCEEDED ;
2003-11-11 03:41:31 +01:00
req - > socks_version = 5 ; /* remember that we've already negotiated auth */
2003-10-04 03:37:01 +02:00
log_fn ( LOG_DEBUG , " socks5: accepted method 0 " ) ;
return 0 ;
}
/* we know the method; read in the request */
log_fn ( LOG_DEBUG , " socks5: checking request " ) ;
2004-11-28 10:05:49 +01:00
if ( buf - > datalen < 8 ) /* basic info plus >=2 for addr plus 2 for port */
2003-10-04 03:37:01 +02:00
return 0 ; /* not yet */
2005-04-27 04:55:21 +02:00
req - > command = ( unsigned char ) * ( buf - > cur + 1 ) ;
2004-11-28 10:05:49 +01:00
if ( req - > command ! = SOCKS_COMMAND_CONNECT & &
2004-11-28 12:39:53 +01:00
req - > command ! = SOCKS_COMMAND_RESOLVE ) {
2004-06-17 20:13:09 +02:00
/* not a connect or resolve? we don't support it. */
log_fn ( LOG_WARN , " socks5: command %d not recognized. Rejecting. " ,
req - > command ) ;
2003-10-04 03:37:01 +02:00
return - 1 ;
}
2005-04-27 04:55:21 +02:00
switch ( * ( buf - > cur + 3 ) ) { /* address type */
2003-10-04 03:37:01 +02:00
case 1 : /* IPv4 address */
log_fn ( LOG_DEBUG , " socks5: ipv4 address type " ) ;
2004-11-28 10:05:49 +01:00
if ( buf - > datalen < 10 ) /* ip/port there? */
2003-10-04 03:37:01 +02:00
return 0 ; /* not yet */
2004-08-07 06:03:01 +02:00
2005-04-27 04:55:21 +02:00
destip = ntohl ( * ( uint32_t * ) ( buf - > cur + 4 ) ) ;
2003-10-04 03:37:01 +02:00
in . s_addr = htonl ( destip ) ;
2005-02-22 09:18:36 +01:00
tor_inet_ntoa ( & in , tmpbuf , sizeof ( tmpbuf ) ) ;
2004-11-28 10:05:49 +01:00
if ( strlen ( tmpbuf ) + 1 > MAX_SOCKS_ADDR_LEN ) {
2004-02-28 08:01:22 +01:00
log_fn ( LOG_WARN , " socks5 IP takes %d bytes, which doesn't fit in %d. Rejecting. " ,
2003-12-16 06:33:11 +01:00
( int ) strlen ( tmpbuf ) + 1 , ( int ) MAX_SOCKS_ADDR_LEN ) ;
2003-10-04 03:37:01 +02:00
return - 1 ;
}
2004-10-27 08:48:16 +02:00
strlcpy ( req - > address , tmpbuf , sizeof ( req - > address ) ) ;
2005-04-27 04:55:21 +02:00
req - > port = ntohs ( * ( uint16_t * ) ( buf - > cur + 8 ) ) ;
2003-10-14 03:34:31 +02:00
buf_remove_from_front ( buf , 10 ) ;
2004-11-28 10:05:49 +01:00
if ( ! have_warned_about_unsafe_socks ) {
2004-08-07 11:01:04 +02:00
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. " , req - > port ) ;
2004-08-07 06:03:01 +02:00
// have_warned_about_unsafe_socks = 1; // (for now, warn every time)
}
2003-10-04 03:37:01 +02:00
return 1 ;
case 3 : /* fqdn */
log_fn ( LOG_DEBUG , " socks5: fqdn address type " ) ;
2005-04-27 04:55:21 +02:00
len = ( unsigned char ) * ( buf - > cur + 4 ) ;
2004-11-28 10:05:49 +01:00
if ( buf - > datalen < 7u + len ) /* addr/port there? */
2003-10-04 03:37:01 +02:00
return 0 ; /* not yet */
2004-11-28 10:05:49 +01:00
if ( len + 1 > MAX_SOCKS_ADDR_LEN ) {
2004-02-28 08:01:22 +01:00
log_fn ( LOG_WARN , " socks5 hostname is %d bytes, which doesn't fit in %d. Rejecting. " ,
2003-11-11 03:41:31 +01:00
len + 1 , MAX_SOCKS_ADDR_LEN ) ;
2003-10-04 03:37:01 +02:00
return - 1 ;
}
2005-04-27 04:55:21 +02:00
memcpy ( req - > address , buf - > cur + 5 , len ) ;
2003-11-16 18:00:02 +01:00
req - > address [ len ] = 0 ;
2005-04-27 04:55:21 +02:00
req - > port = ntohs ( get_uint16 ( buf - > cur + 5 + len ) ) ;
2003-10-14 03:34:31 +02:00
buf_remove_from_front ( buf , 5 + len + 2 ) ;
2003-10-04 03:37:01 +02:00
return 1 ;
default : /* unsupported */
2005-04-27 04:55:21 +02:00
log_fn ( LOG_WARN , " socks5: unsupported address type %d. Rejecting. " , * ( buf - > cur + 3 ) ) ;
2003-10-04 03:37:01 +02:00
return - 1 ;
}
2004-04-25 22:37:37 +02:00
tor_assert ( 0 ) ;
2003-10-04 03:37:01 +02:00
case 4 : /* socks4 */
2004-03-08 00:50:15 +01:00
/* http://archive.socks.permeo.com/protocol/socks4.protocol */
/* http://archive.socks.permeo.com/protocol/socks4a.protocol */
2003-10-04 03:37:01 +02:00
2003-11-11 03:41:31 +01:00
req - > socks_version = 4 ;
2004-11-28 10:05:49 +01:00
if ( buf - > datalen < SOCKS4_NETWORK_LEN ) /* basic info available? */
2003-10-04 03:37:01 +02:00
return 0 ; /* not yet */
2005-04-27 04:55:21 +02:00
req - > command = ( unsigned char ) * ( buf - > cur + 1 ) ;
2004-11-28 10:05:49 +01:00
if ( req - > command ! = SOCKS_COMMAND_CONNECT & &
2004-11-28 12:39:53 +01:00
req - > command ! = SOCKS_COMMAND_RESOLVE ) {
2004-06-17 20:13:09 +02:00
/* not a connect or resolve? we don't support it. */
log_fn ( LOG_WARN , " socks4: command %d not recognized. Rejecting. " ,
req - > command ) ;
2003-10-04 03:37:01 +02:00
return - 1 ;
}
2005-04-27 04:55:21 +02:00
req - > port = ntohs ( * ( uint16_t * ) ( buf - > cur + 2 ) ) ;
2003-10-15 21:17:21 +02:00
destip = ntohl ( * ( uint32_t * ) ( buf - > mem + 4 ) ) ;
2004-11-28 10:05:49 +01:00
if ( ( ! req - > port & & req - > command ! = SOCKS_COMMAND_RESOLVE ) | | ! destip ) {
2004-02-28 08:01:22 +01:00
log_fn ( LOG_WARN , " socks4: Port or DestIP is zero. Rejecting. " ) ;
2003-10-04 03:37:01 +02:00
return - 1 ;
}
2004-11-28 10:05:49 +01:00
if ( destip > > 8 ) {
2003-10-04 03:37:01 +02:00
log_fn ( LOG_DEBUG , " socks4: destip not in form 0.0.0.x. " ) ;
in . s_addr = htonl ( destip ) ;
2005-02-22 09:18:36 +01:00
tor_inet_ntoa ( & in , tmpbuf , sizeof ( tmpbuf ) ) ;
2004-11-28 10:05:49 +01:00
if ( strlen ( tmpbuf ) + 1 > MAX_SOCKS_ADDR_LEN ) {
2004-02-28 08:01:22 +01:00
log_fn ( LOG_WARN , " socks4 addr (%d bytes) too long. Rejecting. " ,
2003-12-16 06:33:11 +01:00
( int ) strlen ( tmpbuf ) ) ;
2003-10-04 03:37:01 +02:00
return - 1 ;
}
2005-05-03 12:04:08 +02:00
log_fn ( LOG_DEBUG , " socks4: successfully read destip (%s) " , safe_str ( tmpbuf ) ) ;
2003-10-04 03:37:01 +02:00
socks4_prot = socks4 ;
}
2005-04-27 04:55:21 +02:00
next = memchr ( buf - > cur + SOCKS4_NETWORK_LEN , 0 ,
2004-07-12 18:51:05 +02:00
buf - > datalen - SOCKS4_NETWORK_LEN ) ;
2004-11-28 10:05:49 +01:00
if ( ! next ) {
2004-02-28 08:01:22 +01:00
log_fn ( LOG_DEBUG , " socks4: Username not here yet. " ) ;
2003-10-04 03:37:01 +02:00
return 0 ;
}
2005-04-27 04:55:21 +02:00
tor_assert ( next < buf - > cur + buf - > datalen ) ;
2003-10-04 03:37:01 +02:00
2004-11-10 15:26:34 +01:00
startaddr = NULL ;
2004-11-28 10:05:49 +01:00
if ( socks4_prot ! = socks4a & & ! have_warned_about_unsafe_socks ) {
2004-08-07 11:01:04 +02:00
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 ) ;
2004-08-04 01:42:33 +02:00
// have_warned_about_unsafe_socks = 1; // (for now, warn every time)
}
2004-12-22 11:04:50 +01:00
if ( socks4_prot = = socks4a ) {
2005-04-27 04:55:21 +02:00
if ( next + 1 = = buf - > cur + buf - > datalen ) {
2004-12-22 11:04:50 +01:00
log_fn ( LOG_DEBUG , " socks4: No part of destaddr here yet. " ) ;
return 0 ;
}
2004-11-10 15:26:34 +01:00
startaddr = next + 1 ;
2005-04-27 04:55:21 +02:00
next = memchr ( startaddr , 0 , buf - > cur + buf - > datalen - startaddr ) ;
2004-11-28 10:05:49 +01:00
if ( ! next ) {
2004-12-22 11:04:50 +01:00
log_fn ( LOG_DEBUG , " socks4: Destaddr not all here yet. " ) ;
2003-10-04 03:37:01 +02:00
return 0 ;
}
2004-11-28 10:05:49 +01:00
if ( MAX_SOCKS_ADDR_LEN < = next - startaddr ) {
2004-02-28 08:01:22 +01:00
log_fn ( LOG_WARN , " socks4: Destaddr too long. Rejecting. " ) ;
2003-10-04 03:37:01 +02:00
return - 1 ;
}
2005-04-27 04:55:21 +02:00
tor_assert ( next < buf - > cur + buf - > datalen ) ;
2003-10-04 03:37:01 +02:00
}
2004-02-28 08:01:22 +01:00
log_fn ( LOG_DEBUG , " socks4: Everything is here. Success. " ) ;
2004-11-10 15:26:34 +01:00
strlcpy ( req - > address , startaddr ? startaddr : tmpbuf ,
2004-10-27 08:48:16 +02:00
sizeof ( req - > address ) ) ;
2005-04-27 04:55:21 +02:00
buf_remove_from_front ( buf , next - buf - > cur + 1 ) ; /* next points to the final \0 on inbuf */
2003-10-04 03:37:01 +02:00
return 1 ;
2004-02-26 23:02:22 +01:00
case ' G ' : /* get */
case ' H ' : /* head */
case ' P ' : /* put/post */
case ' C ' : /* connect */
2004-10-27 08:48:16 +02:00
strlcpy ( req - > reply ,
2004-02-26 23:02:22 +01:00
" HTTP/1.0 501 Tor is not an HTTP Proxy \r \n "
2004-02-26 23:10:55 +01:00
" Content-Type: text/html; charset=iso-8859-1 \r \n \r \n "
2004-02-26 23:02:22 +01:00
" <html> \n "
" <head> \n "
" <title>Tor is not an HTTP Proxy</title> \n "
" </head> \n "
" <body> \n "
2004-02-27 02:33:02 +01:00
" <h1>Tor is not an HTTP Proxy</h1> \n "
" <p> \n "
2004-02-26 23:02:22 +01:00
" It appears you have configured your web browser to use Tor as an HTTP Proxy. \n "
" This is not correct: Tor provides a SOCKS proxy. Please configure your \n "
" client accordingly. \n "
2004-02-27 02:33:02 +01:00
" </p> \n "
" <p> \n "
2005-01-04 00:19:15 +01:00
" See <a href= \" http://tor.eff.org/doc/tor-doc.html#installing \" >http://tor.eff.org/doc/tor-doc.html#installing</a> for more information. \n "
2004-02-26 23:02:22 +01:00
" <!-- 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 "
2004-02-27 02:33:02 +01:00
" </p> \n "
2004-02-26 23:02:22 +01:00
" </body> \n "
" </html> \n "
2004-10-27 08:48:16 +02:00
, MAX_SOCKS_REPLY_LEN ) ;
2004-02-26 23:02:22 +01:00
req - > replylen = strlen ( req - > reply ) + 1 ;
/* fall through */
2003-10-04 03:37:01 +02:00
default : /* version is not socks4 or socks5 */
2004-02-26 23:10:55 +01:00
log_fn ( LOG_WARN , " Socks version %d not recognized. (Tor is not an http proxy.) " ,
2005-04-27 04:55:21 +02:00
* ( buf - > cur ) ) ;
2003-09-18 10:11:31 +02:00
return - 1 ;
}
}
2005-03-02 21:22:10 +01:00
# define CONTROL_CMD_FRAGMENTHEADER 0x0010
# define CONTROL_CMD_FRAGMENT 0x0011
2005-06-17 20:49:55 +02:00
/** If there is a complete version 0control message waiting on buf, then store
2004-11-04 23:30:49 +01:00
* its contents into * < b > type_out < / b > , store its body ' s length into
* * < b > len_out < / b > , allocate and store a string for its body into
2005-03-02 21:22:10 +01:00
* * < b > body_out < / b > , and return 1. ( body_out will always be NUL - terminated ,
2004-11-04 23:30:49 +01:00
* even if the control message body doesn ' t end with NUL . )
*
* If there is not a complete control message waiting , return 0.
*
2005-06-17 20:49:55 +02:00
* Return - 1 on error ; return - 2 on " seems to be control protocol v1. "
2004-11-04 23:30:49 +01:00
*/
2005-06-11 20:52:12 +02:00
int
2005-06-17 20:49:55 +02:00
fetch_from_buf_control0 ( buf_t * buf , uint32_t * len_out , uint16_t * type_out ,
char * * body_out , int check_for_v1 )
2004-11-03 02:32:26 +01:00
{
2005-03-02 21:22:10 +01:00
uint32_t msglen ;
uint16_t type ;
2005-04-28 00:01:34 +02:00
char tmp [ 4 ] ;
2004-11-03 02:32:26 +01:00
tor_assert ( buf ) ;
tor_assert ( len_out ) ;
tor_assert ( type_out ) ;
tor_assert ( body_out ) ;
if ( buf - > datalen < 4 )
return 0 ;
2005-04-26 22:53:22 +02:00
peek_from_buf ( tmp , 4 , buf ) ;
msglen = ntohs ( get_uint16 ( tmp ) ) ;
2005-06-17 20:49:55 +02:00
type = ntohs ( get_uint16 ( tmp + 2 ) ) ;
if ( type > 255 & & check_for_v1 )
return - 2 ;
2005-03-02 21:22:10 +01:00
if ( buf - > datalen < 4 + ( unsigned ) msglen )
2004-11-03 02:32:26 +01:00
return 0 ;
2005-04-28 00:01:34 +02:00
* len_out = msglen ;
* type_out = type ;
buf_remove_from_front ( buf , 4 ) ;
if ( msglen ) {
* body_out = tor_malloc ( msglen + 1 ) ;
fetch_from_buf ( * body_out , msglen , buf ) ;
( * body_out ) [ msglen ] = ' \0 ' ;
2004-11-03 02:32:26 +01:00
} else {
2005-04-28 00:01:34 +02:00
* body_out = NULL ;
2005-03-02 21:22:10 +01:00
}
2005-04-28 00:01:34 +02:00
return 1 ;
2004-11-03 02:32:26 +01:00
}
2005-06-17 20:49:55 +02:00
/** DOCDOC */
static char *
find_char_on_buf ( buf_t * buf , char * start , size_t len , char c )
{
size_t len_rest ;
char * cp ;
_split_range ( buf , start , & len , & len_rest ) ;
cp = memchr ( buf - > cur , c , len ) ;
if ( cp | | ! len_rest )
return cp ;
return memchr ( buf - > mem , c , len_rest ) ;
}
static char *
find_crlf_on_buf ( buf_t * buf , char * cp )
{
char * next ;
while ( 1 ) {
size_t remaining = buf - > datalen - _buf_offset ( buf , cp ) ;
cp = find_char_on_buf ( buf , cp , remaining , ' \r ' ) ;
if ( ! cp )
return NULL ;
next = _wrap_ptr ( buf , cp + 1 ) ;
if ( next = = _buf_end ( buf ) )
return NULL ;
if ( * next = = ' \n ' )
return cp ;
cp = next ;
}
}
/* DOCDOC : 0 means 'need to read more'. means done, -1 means "grow buffer." */
int
fetch_from_buf_line ( buf_t * buf , char * data_out , size_t * data_len )
{
char * eol ;
size_t sz ;
/* Look for a CRLF. */
2005-06-17 22:37:21 +02:00
if ( ! ( eol = find_crlf_on_buf ( buf , buf - > cur ) ) ) {
2005-06-17 20:49:55 +02:00
return 0 ;
2005-06-17 22:37:21 +02:00
}
2005-06-17 20:49:55 +02:00
sz = _buf_offset ( buf , eol ) ;
2005-06-17 22:37:21 +02:00
if ( sz + 3 > * data_len ) {
* data_len = sz + 3 ;
2005-06-17 20:49:55 +02:00
return - 1 ;
}
fetch_from_buf ( data_out , sz + 2 , buf ) ;
2005-06-17 22:37:21 +02:00
data_out [ sz + 2 ] = ' \0 ' ;
2005-06-17 20:49:55 +02:00
* data_len = sz + 2 ;
return 1 ;
}
2004-05-09 18:47:25 +02:00
/** Log an error and exit if <b>buf</b> is corrupted.
2004-05-02 00:08:43 +02:00
*/
2004-03-03 23:49:15 +01:00
void assert_buf_ok ( buf_t * buf )
{
2004-04-25 22:37:37 +02:00
tor_assert ( buf ) ;
tor_assert ( buf - > magic = = BUFFER_MAGIC ) ;
tor_assert ( buf - > mem ) ;
2005-05-03 00:49:24 +02:00
tor_assert ( buf - > highwater < = buf - > len ) ;
tor_assert ( buf - > datalen < = buf - > highwater ) ;
2005-04-26 23:29:20 +02:00
# ifdef SENTINELS
2005-04-27 02:53:44 +02:00
{
uint32_t u32 = get_uint32 ( buf - > mem - 4 ) ;
tor_assert ( u32 = = START_MAGIC ) ;
u32 = get_uint32 ( buf - > mem + buf - > len ) ;
tor_assert ( u32 = = END_MAGIC ) ;
}
2005-04-26 22:53:22 +02:00
# endif
2004-03-03 23:49:15 +01:00
}
2005-06-09 21:03:31 +02:00