2004-05-09 18:47:25 +02:00
/* Copyright 2001 Matej Pfajfar, 2001-2004 Roger Dingledine. */
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$ */
2002-06-27 00:45:49 +02:00
2004-05-07 19:04:12 +02:00
/**
* \ file buffers . c
* \ brief Abstractions for buffered IO .
* */
2002-06-27 00:45:49 +02:00
# include "or.h"
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 */
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
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
/** Maximum size, in bytes, for resized buffers. */
2004-04-08 04:19:35 +02:00
# define MAX_BUF_SIZE (1024*1024*10)
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 . */
2003-10-19 03:10:38 +02:00
# define MIN_BUF_SHRINK_SIZE (16*1024)
2003-09-25 07:17:11 +02:00
2004-05-09 18:47:25 +02:00
/** Change a buffer's capacity. <b>new_capacity</b> must be \<= buf->datalen. */
2003-10-14 03:34:31 +02:00
static INLINE void buf_resize ( buf_t * buf , size_t new_capacity )
{
2004-04-25 22:37:37 +02:00
tor_assert ( buf - > datalen < = new_capacity ) ;
tor_assert ( new_capacity ) ;
2003-10-15 21:17:21 +02:00
buf - > mem = tor_realloc ( buf - > mem , new_capacity ) ;
2003-10-14 03:34:31 +02:00
buf - > len = new_capacity ;
}
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 . )
*/
static INLINE int buf_ensure_capacity ( buf_t * buf , size_t capacity )
{
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 ;
}
2004-05-07 19:04:12 +02:00
/** If the buffer is at least 2*MIN_BUF_SHRINK_SIZE bytes in capacity,
2003-10-14 03:34:31 +02:00
* and if the buffer is less than 1 / 4 full , shrink the buffer until
* one of the above no longer holds . ( We shrink the buffer by
* dividing by powers of 2. )
*/
static INLINE void buf_shrink_if_underfull ( buf_t * buf ) {
size_t new_len ;
2003-10-15 21:07:07 +02:00
/* If the buffer is at least .25 full, or if shrinking the buffer would
* put it onder MIN_BUF_SHRINK_SIZE , don ' t do it . */
if ( buf - > datalen > = buf - > len / 4 | | buf - > len < 2 * MIN_BUF_SHRINK_SIZE )
2003-10-14 03:34:31 +02:00
return ;
2003-10-15 21:07:07 +02:00
/* Shrink new_len by powers of 2 until: datalen is at least 1/4 of
* new_len , OR shrinking new_len more would put it under
* MIN_BUF_SHRINK_SIZE .
*/
2003-10-14 03:34:31 +02:00
new_len = buf - > len / 2 ;
2003-12-17 22:09:31 +01:00
while ( buf - > datalen < new_len / 4 & & new_len / 2 > MIN_BUF_SHRINK_SIZE )
2003-10-14 03:34:31 +02:00
new_len / = 2 ;
2003-10-14 05:06:48 +02:00
log_fn ( LOG_DEBUG , " Shrinking buffer from %d to %d bytes. " ,
( int ) buf - > len , ( int ) new_len ) ;
buf_resize ( buf , new_len ) ;
2003-10-14 03:34:31 +02:00
}
2004-05-09 18:47:25 +02:00
/** Remove the first <b>n</b> bytes from buf.
2003-10-14 03:34:31 +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 ;
2003-10-15 21:17:21 +02:00
memmove ( buf - > mem , buf - > mem + n , buf - > datalen ) ;
2003-10-14 03:34:31 +02:00
buf_shrink_if_underfull ( buf ) ;
}
2004-05-18 17:35:21 +02:00
/** Make sure that the memory in buf ends with a zero byte. */
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 ;
2004-05-19 21:28:24 +02:00
buf - > mem [ buf - > datalen ] = ' \0 ' ;
2004-05-18 17:35:21 +02:00
return 0 ;
2003-09-25 07:17:11 +02:00
}
2004-05-09 18:47:25 +02:00
/** Create and return a new buf with capacity <b>size</b>.
2003-03-04 05:36:37 +01:00
*/
2003-09-27 09:33:07 +02:00
buf_t * buf_new_with_capacity ( size_t size ) {
2003-09-25 07:17:11 +02:00
buf_t * buf ;
2004-04-07 22:11:28 +02:00
buf = tor_malloc ( sizeof ( buf_t ) ) ;
2004-03-03 23:49:15 +01:00
buf - > magic = BUFFER_MAGIC ;
2004-04-07 22:11:28 +02:00
buf - > mem = tor_malloc ( size ) ;
2003-09-25 07:17:11 +02:00
buf - > len = size ;
buf - > datalen = 0 ;
2003-10-15 21:17:21 +02:00
// memset(buf->mem,0,size);
2003-09-25 07:17:11 +02:00
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. */
2003-09-25 07:17:11 +02:00
buf_t * buf_new ( )
{
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
2004-05-09 18:47:25 +02:00
/** Remove all data from <b>buf</b> */
2004-02-28 20:14:11 +01:00
void buf_clear ( buf_t * buf )
{
buf - > datalen = 0 ;
}
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> */
2003-09-25 07:17:11 +02:00
size_t buf_datalen ( const buf_t * buf )
{
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 . */
2003-09-25 07:17:11 +02:00
size_t buf_capacity ( const buf_t * buf )
{
return buf - > len ;
}
2004-05-09 18:47:25 +02:00
/** For testing only: Return a pointer to the raw memory stored in <b>buf</b>.
2004-05-02 00:08:43 +02:00
*/
2003-09-25 07:17:11 +02:00
const char * _buf_peek_raw_buffer ( const buf_t * buf )
{
2003-10-15 21:17:21 +02:00
return buf - > mem ;
2003-09-25 07:17:11 +02:00
}
2004-05-09 18:47:25 +02:00
/** Release storage held by <b>buf</b>.
2004-05-02 00:08:43 +02:00
*/
2003-09-25 07:17:11 +02:00
void buf_free ( buf_t * buf ) {
2004-03-03 23:49:15 +01:00
assert_buf_ok ( buf ) ;
buf - > magic = 0xDEADBEEF ;
tor_free ( buf - > mem ) ;
tor_free ( buf ) ;
2002-06-27 00:45:49 +02:00
}
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-05-09 18:47:25 +02:00
* < b > at_most < / b > bytes , resizing the buffer as necessary . If read ( )
* returns 0 , set < b > * reached_eof < / b > to 1 and return 0. Return - 1 on error ;
2004-05-02 00:08:43 +02:00
* else return the number of bytes read . Return 0 if read ( ) would
* block .
2003-03-04 05:36:37 +01:00
*/
2004-04-28 22:13:21 +02:00
int read_to_buf ( int s , size_t at_most , buf_t * buf , int * reached_eof ) {
2002-06-27 00:45:49 +02:00
int read_result ;
2004-04-25 22:37:37 +02:00
assert_buf_ok ( buf ) ;
tor_assert ( reached_eof & & ( 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-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 ; /* 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
if ( at_most = = 0 )
return 0 ; /* we shouldn't read anything */
2003-06-18 00:18:26 +02:00
// log_fn(LOG_DEBUG,"reading at most %d bytes.",at_most);
2004-03-11 07:19:08 +01:00
read_result = recv ( s , buf - > mem + buf - > datalen , at_most , 0 ) ;
2002-06-27 00:45:49 +02:00
if ( read_result < 0 ) {
2004-05-01 22:46:28 +02:00
if ( ! ERRNO_IS_EAGAIN ( tor_socket_errno ( s ) ) ) { /* it's a real error */
2002-06-27 00:45:49 +02:00
return - 1 ;
}
2004-05-02 00:08:43 +02:00
return 0 ; /* would block. */
2002-06-27 00:45:49 +02:00
} else if ( read_result = = 0 ) {
2003-06-18 00:18:26 +02:00
log_fn ( LOG_DEBUG , " Encountered eof " ) ;
2002-06-30 09:37:49 +02:00
* reached_eof = 1 ;
2002-06-27 00:45:49 +02:00
return 0 ;
} else { /* we read some bytes */
2003-09-25 07:17:11 +02:00
buf - > datalen + = read_result ;
log_fn ( LOG_DEBUG , " Read %d bytes. %d on inbuf. " , read_result ,
( int ) buf - > datalen ) ;
2002-06-27 00:45:49 +02:00
return read_result ;
}
}
2004-05-07 19:04:12 +02:00
/** As read_to_buf, but reads from a TLS connection.
2004-05-02 00:08:43 +02:00
*/
2004-04-28 22:13:21 +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 ;
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
2004-05-05 23:32:43 +02:00
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 ) ;
2004-04-27 01:00:07 +02:00
assert_no_tls_errors ( ) ;
2003-10-15 21:17:21 +02:00
r = tor_tls_read ( tls , buf - > mem + buf - > datalen , at_most ) ;
2003-12-17 22:09:31 +01:00
if ( r < 0 )
2003-09-04 18:05:08 +02:00
return r ;
2003-09-25 07:17:11 +02:00
buf - > datalen + = r ;
2004-04-27 01:05:58 +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 ) ) ;
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
2004-05-09 18:47:25 +02:00
/** Write data from <b>buf</b> to the socket <b>s</b>. Write at most
* < b > * buf_flushlen < / b > bytes , decrement < b > * buf_flushlen < / b > by
* 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
*/
2003-12-17 22:09:31 +01:00
int flush_buf ( int s , buf_t * buf , int * buf_flushlen )
2003-09-25 07:17:11 +02:00
{
2002-06-27 00:45:49 +02:00
int write_result ;
2004-04-25 22:37:37 +02:00
assert_buf_ok ( buf ) ;
2004-04-28 22:13:21 +02:00
tor_assert ( buf_flushlen & & ( s > = 0 ) & & ( ( unsigned ) * buf_flushlen < = buf - > datalen ) ) ;
2002-06-27 00:45:49 +02:00
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
if ( * buf_flushlen = = 0 ) /* nothing to flush */
2002-06-27 00:45:49 +02:00
return 0 ;
2004-03-11 07:19:08 +01:00
write_result = send ( s , buf - > mem , * buf_flushlen , 0 ) ;
2002-06-27 00:45:49 +02:00
if ( write_result < 0 ) {
2004-05-01 22:46:28 +02:00
if ( ! ERRNO_IS_EAGAIN ( tor_socket_errno ( s ) ) ) { /* it's a real error */
2003-08-14 19:13:52 +02:00
return - 1 ;
}
2003-06-18 00:18:26 +02:00
log_fn ( LOG_DEBUG , " write() would block, returning. " ) ;
2002-06-27 00:45:49 +02:00
return 0 ;
} else {
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
* buf_flushlen - = write_result ;
2003-10-14 03:34:31 +02:00
buf_remove_from_front ( buf , write_result ) ;
2003-09-18 10:11:31 +02:00
log_fn ( LOG_DEBUG , " %d: flushed %d bytes, %d ready to flush, %d remain. " ,
2003-09-25 07:17:11 +02:00
s , write_result , * buf_flushlen , ( int ) buf - > datalen ) ;
2003-10-14 03:34:31 +02:00
2004-02-28 06:22:07 +01:00
return write_result ;
2002-06-27 00:45:49 +02:00
}
}
2004-05-07 19:04:12 +02:00
/** As flush_buf, but writes data to a TLS connection.
2004-05-02 00:08:43 +02:00
*/
2003-12-17 22:09:31 +01:00
int flush_buf_tls ( tor_tls * tls , buf_t * buf , int * buf_flushlen )
2003-09-04 18:05:08 +02:00
{
int r ;
2004-04-25 22:37:37 +02:00
assert_buf_ok ( buf ) ;
tor_assert ( tls & & 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 */
2003-10-15 21:17:21 +02:00
r = tor_tls_write ( tls , buf - > mem , * buf_flushlen ) ;
2003-09-04 18:05:08 +02:00
if ( r < 0 ) {
return r ;
}
* buf_flushlen - = r ;
2003-10-14 03:34:31 +02:00
buf_remove_from_front ( buf , r ) ;
cleanups, bugfixes, more verbose logs
Fixed up the assert_*_ok funcs some (more work remains)
Changed config so it reads either /etc/torrc or the -f arg, never both
Finally tracked down a nasty bug with our use of tls:
It turns out that if you ask SSL_read() for no more than n bytes, it
will read the entire record from the network (and maybe part of the next
record, I'm not sure), give you n bytes of it, and keep the remaining
bytes internally. This is fine, except our poll-for-read looks at the
network, and there are no bytes pending on the network, so we never know
to ask SSL_read() for more bytes. Currently I've hacked it so if we ask
for n bytes and it returns n bytes, then it reads again right then. This
will interact poorly with our rate limiting; we need a cleaner solution.
svn:r481
2003-09-24 23:24:52 +02:00
log_fn ( LOG_DEBUG , " flushed %d bytes, %d ready to flush, %d remain. " ,
2003-09-25 07:17:11 +02:00
r , * buf_flushlen , ( int ) buf - > datalen ) ;
2003-09-04 18:05:08 +02:00
return r ;
}
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 .
*/
2003-09-25 12:42:07 +02:00
int write_to_buf ( const char * string , int string_len , buf_t * buf ) {
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 ) ) {
2003-12-16 06:33:11 +01: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
2003-10-15 21:17:21 +02:00
memcpy ( buf - > mem + buf - > datalen , string , string_len ) ;
2003-09-25 07:17:11 +02:00
buf - > datalen + = string_len ;
log_fn ( LOG_DEBUG , " added %d bytes to buf (now %d total). " , string_len , ( int ) buf - > datalen ) ;
return buf - > datalen ;
2003-03-17 03:42:45 +01:00
}
2004-05-09 18:47:25 +02:00
/** 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 \ < =
2004-05-02 00:08:43 +02:00
* the number of bytes on the buffer .
*/
2004-04-28 22:13:21 +02:00
int fetch_from_buf ( char * string , size_t string_len , buf_t * buf ) {
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
2003-10-15 21:17:21 +02:00
memcpy ( string , buf - > mem , string_len ) ;
2003-10-14 03:34:31 +02:00
buf_remove_from_front ( buf , string_len ) ;
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.
2003-12-17 22:09:31 +01:00
*
2003-09-17 22:09:06 +02:00
* Else , change nothing and return 0.
*/
2003-09-25 07:17:11 +02:00
int fetch_from_buf_http ( buf_t * buf ,
2003-12-17 10:42:28 +01:00
char * * headers_out , int max_headerlen ,
2004-03-31 07:01:30 +02:00
char * * body_out , int * body_used , int max_bodylen ) {
2004-05-18 17:35:21 +02:00
char * headers , * body , * p ;
2003-09-17 22:09:06 +02:00
int headerlen , bodylen , contentlen ;
2004-04-25 22:37:37 +02:00
assert_buf_ok ( buf ) ;
2003-09-17 22:09:06 +02:00
2003-10-15 21:17:21 +02:00
headers = buf - > mem ;
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 ;
}
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 ;
2003-12-17 10:42:28 +01:00
log_fn ( LOG_DEBUG , " headerlen %d, bodylen %d. " , headerlen , bodylen ) ;
2003-09-17 22:09:06 +02:00
if ( headers_out & & max_headerlen < = headerlen ) {
2003-10-10 03:48:32 +02:00
log_fn ( LOG_WARN , " headerlen %d larger than %d. Failing. " , headerlen , max_headerlen - 1 ) ;
2003-09-17 22:09:06 +02:00
return - 1 ;
}
if ( body_out & & max_bodylen < = bodylen ) {
2003-10-10 03:48:32 +02:00
log_fn ( LOG_WARN , " bodylen %d larger than %d. Failing. " , bodylen , 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 ) {
contentlen = atoi ( p + strlen ( CONTENT_LENGTH ) ) ;
2004-03-31 07:01:30 +02:00
/* if content-length is malformed, then our body length is 0. fine. */
2003-09-25 12:42:07 +02:00
log_fn ( LOG_DEBUG , " Got a contentlen of %d. " , contentlen ) ;
2003-09-17 22:09:06 +02:00
if ( bodylen < contentlen ) {
log_fn ( LOG_DEBUG , " body not all here yet. " ) ;
return 0 ; /* not all there yet */
}
2004-04-26 23:15:06 +02:00
if ( bodylen > contentlen ) {
bodylen = contentlen ;
log_fn ( LOG_DEBUG , " bodylen reduced to %d. " , bodylen ) ;
}
2003-09-17 22:09:06 +02:00
}
/* all happy. copy into the appropriate places, and return 1 */
if ( headers_out ) {
2003-12-17 10:42:28 +01:00
* headers_out = tor_malloc ( headerlen + 1 ) ;
memcpy ( * headers_out , buf - > mem , headerlen ) ;
2003-12-19 06:09:51 +01:00
( * headers_out ) [ headerlen ] = 0 ; /* null terminate it */
2003-09-17 22:09:06 +02: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 ) ;
memcpy ( * body_out , buf - > mem + 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
*/
2003-11-11 03:41:31 +01:00
int fetch_from_buf_socks ( buf_t * buf , socks_request_t * req ) {
2003-10-04 03:37:01 +02:00
unsigned char len ;
2003-09-21 08:15:43 +02:00
char * tmpbuf = NULL ;
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 ;
2003-10-04 03:37:01 +02:00
if ( buf - > datalen < 2 ) /* version and another byte */
2003-09-18 10:11:31 +02:00
return 0 ;
2003-10-15 21:17:21 +02:00
switch ( * ( buf - > mem ) ) { /* which version of socks? */
2003-10-04 03:37:01 +02:00
case 5 : /* socks5 */
2003-11-11 03:41:31 +01:00
if ( req - > socks_version ! = 5 ) { /* we need to negotiate a method */
2003-10-15 21:17:21 +02:00
unsigned char nummethods = ( unsigned char ) * ( buf - > mem + 1 ) ;
2004-04-25 22:37:37 +02:00
tor_assert ( ! req - > socks_version ) ;
2004-04-28 22:13:21 +02:00
if ( buf - > datalen < 2u + nummethods )
2003-10-04 03:37:01 +02:00
return 0 ;
2003-10-15 21:17:21 +02:00
if ( ! nummethods | | ! memchr ( buf - > mem + 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 */
req - > reply [ 0 ] = 5 ; /* socks5 reply */
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 */
req - > reply [ 1 ] = 0 ; /* choose the 'no auth' method */
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 " ) ;
if ( buf - > datalen < 8 ) /* basic info plus >=2 for addr plus 2 for port */
return 0 ; /* not yet */
2004-06-17 20:13:09 +02:00
req - > command = ( unsigned char ) * ( buf - > mem + 1 ) ;
if ( req - > command ! = SOCKS_COMMAND_CONNECT & &
req - > command ! = SOCKS_COMMAND_RESOLVE ) {
/* 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 ;
}
2003-10-15 21:17:21 +02:00
switch ( * ( buf - > mem + 3 ) ) { /* address type */
2003-10-04 03:37:01 +02:00
case 1 : /* IPv4 address */
log_fn ( LOG_DEBUG , " socks5: ipv4 address type " ) ;
if ( buf - > datalen < 10 ) /* ip/port there? */
return 0 ; /* not yet */
2004-08-07 06:03:01 +02:00
2003-10-15 21:17:21 +02:00
destip = ntohl ( * ( uint32_t * ) ( buf - > mem + 4 ) ) ;
2003-10-04 03:37:01 +02:00
in . s_addr = htonl ( destip ) ;
tmpbuf = inet_ntoa ( in ) ;
2003-11-11 03:41:31 +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 ;
}
2003-11-16 18:00:02 +01:00
strcpy ( req - > address , tmpbuf ) ;
2003-11-11 03:41:31 +01:00
req - > port = ntohs ( * ( uint16_t * ) ( buf - > mem + 8 ) ) ;
2003-10-14 03:34:31 +02:00
buf_remove_from_front ( buf , 10 ) ;
2004-08-07 06:03:01 +02: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 " ) ;
2003-10-15 21:17:21 +02:00
len = ( unsigned char ) * ( buf - > mem + 4 ) ;
2004-04-28 22:13:21 +02:00
if ( buf - > datalen < 7u + len ) /* addr/port there? */
2003-10-04 03:37:01 +02:00
return 0 ; /* not yet */
2003-11-11 03:41:31 +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 ;
}
2003-11-16 18:00:02 +01:00
memcpy ( req - > address , buf - > mem + 5 , len ) ;
req - > address [ len ] = 0 ;
2004-04-03 05:07:25 +02:00
req - > port = ntohs ( get_uint16 ( buf - > mem + 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 */
2004-02-28 08:01:22 +01:00
log_fn ( LOG_WARN , " socks5: unsupported address type %d. Rejecting. " , * ( buf - > mem + 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 ;
2003-10-04 03:37:01 +02:00
if ( buf - > datalen < SOCKS4_NETWORK_LEN ) /* basic info available? */
return 0 ; /* not yet */
2004-06-17 20:13:09 +02:00
req - > command = ( unsigned char ) * ( buf - > mem + 1 ) ;
if ( req - > command ! = SOCKS_COMMAND_CONNECT & &
req - > command ! = SOCKS_COMMAND_RESOLVE ) {
/* 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 ;
}
2003-11-11 03:41:31 +01:00
req - > port = ntohs ( * ( uint16_t * ) ( buf - > mem + 2 ) ) ;
2003-10-15 21:17:21 +02:00
destip = ntohl ( * ( uint32_t * ) ( buf - > mem + 4 ) ) ;
2004-06-17 20:13:09 +02: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 ;
}
if ( destip > > 8 ) {
log_fn ( LOG_DEBUG , " socks4: destip not in form 0.0.0.x. " ) ;
in . s_addr = htonl ( destip ) ;
tmpbuf = inet_ntoa ( in ) ;
2003-11-11 03:41:31 +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 ;
}
log_fn ( LOG_DEBUG , " socks4: successfully read destip (%s) " , tmpbuf ) ;
socks4_prot = socks4 ;
}
2004-07-12 18:51:05 +02:00
next = memchr ( buf - > mem + SOCKS4_NETWORK_LEN , 0 ,
buf - > datalen - SOCKS4_NETWORK_LEN ) ;
2003-10-04 03:37:01 +02: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 ;
}
startaddr = next + 1 ;
2004-08-04 01:42:33 +02: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)
}
2003-10-04 03:37:01 +02:00
if ( socks4_prot = = socks4a ) {
2003-10-15 21:17:21 +02:00
next = memchr ( startaddr , 0 , buf - > mem + buf - > datalen - startaddr ) ;
2003-10-04 03:37:01 +02:00
if ( ! next ) {
2004-02-28 08:01:22 +01:00
log_fn ( LOG_DEBUG , " socks4: Destaddr not here yet. " ) ;
2003-10-04 03:37:01 +02:00
return 0 ;
}
2003-11-11 03:41:31 +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 ;
}
}
2004-02-28 08:01:22 +01:00
log_fn ( LOG_DEBUG , " socks4: Everything is here. Success. " ) ;
2003-11-16 18:00:02 +01:00
strcpy ( req - > address , socks4_prot = = socks4 ? tmpbuf : startaddr ) ;
2004-01-30 16:15:11 +01:00
/* XXX on very old netscapes (socks4) the next line triggers an
* assert , because next - buf - > mem + 1 is greater than buf - > datalen .
*/
2003-10-15 21:17:21 +02:00
buf_remove_from_front ( buf , next - buf - > mem + 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 */
strcpy ( req - > reply ,
" 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 "
2004-08-06 21:44:17 +02:00
" See <a href= \" http://freehaven.net/tor/doc/tor-doc.html \" >http://freehaven.net/tor/doc/tor-doc.html</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 "
) ;
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.) " ,
2004-02-26 23:02:22 +01:00
* ( buf - > mem ) ) ;
2003-09-18 10:11:31 +02:00
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 ) ;
tor_assert ( buf - > datalen < = buf - > len ) ;
2004-03-03 23:49:15 +01:00
}
2003-04-07 04:12:02 +02:00
/*
Local Variables :
mode : c
indent - tabs - mode : nil
c - basic - offset : 2
End :
*/