2003-10-08 04:04:08 +02:00
/* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
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
/* buffers.c */
# 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-03-03 23:49:15 +01:00
uint32_t magic ; /* for debugging */
2003-10-15 21:17:21 +02:00
char * mem ;
2003-09-25 07:17:11 +02:00
size_t len ;
size_t datalen ;
} ;
2003-10-19 03:10:38 +02:00
2003-10-14 03:34:31 +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)
2003-10-14 03:34:31 +02:00
/* Maximum size, in bytes, for resized buffers. */
2004-04-08 04:19:35 +02:00
# define MAX_BUF_SIZE (1024*1024*10)
2003-10-14 03:34:31 +02:00
/* Size, in bytes, for minimum 'shrink' size for buffers. Buffers may start
* 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
2003-10-14 03:34:31 +02:00
/* Change a buffer's capacity. Must only be called when */
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 ;
}
/* If the buffer is not large enough to hold "capacity" bytes, resize
* 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 ;
}
/* If the buffer is at least 2*MIN_BUF_SHRINK_SIZE bytes in capacity,
* 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
}
/* Remove the first 'n' bytes from buf.
*/
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 ) ;
}
2003-09-25 07:17:11 +02:00
/* Find the first instance of str on buf. If none exists, return -1.
* Otherwise , return index of the first character in buf _after_ the
* first instance of str .
*/
2003-12-17 22:09:31 +01:00
static int find_str_in_str ( const char * str , int str_len ,
2003-09-25 07:17:11 +02:00
const char * buf , int buf_len )
{
const char * location ;
const char * last_possible = buf + buf_len - str_len ;
2004-04-25 22:37:37 +02:00
tor_assert ( str & & str_len > 0 & & buf ) ;
2003-09-25 07:17:11 +02:00
if ( buf_len < str_len )
return - 1 ;
for ( location = buf ; location < = last_possible ; location + + )
if ( ( * location = = * str ) & & ! memcmp ( location + 1 , str + 1 , str_len - 1 ) )
return location - buf + str_len ;
return - 1 ;
}
2003-09-25 12:42:07 +02:00
int find_on_inbuf ( char * string , int string_len , buf_t * buf ) {
2003-10-15 21:17:21 +02:00
return find_str_in_str ( string , string_len , buf - > mem , buf - > datalen ) ;
2003-09-25 12:42:07 +02:00
}
2003-09-25 07:17:11 +02:00
/* Create and return a new buf of size 'size'
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
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-02-28 20:14:11 +01:00
void buf_clear ( buf_t * buf )
{
buf - > datalen = 0 ;
}
2002-06-27 00:45:49 +02:00
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
}
2003-09-25 07:17:11 +02:00
size_t buf_capacity ( const buf_t * buf )
{
return buf - > len ;
}
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
}
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
}
2003-09-25 07:17:11 +02:00
/* read from socket s, writing onto end of buf.
2004-02-28 06:22:07 +01:00
* read at most ' at_most ' bytes , and in any case don ' t read more than
* will fit based on buflen .
* If read ( ) returns 0 , set * reached_eof to 1 and return 0. If you want
* to tear down the connection return - 1 , else return the number of
* bytes read .
2003-03-04 05:36:37 +01:00
*/
2003-09-25 07:17:11 +02:00
int read_to_buf ( int s , int at_most , buf_t * buf , int * reached_eof ) {
2002-06-27 00:45:49 +02:00
int read_result ;
2003-08-14 19:13:52 +02:00
# ifdef MS_WINDOWS
int e ;
# endif
2002-06-27 00:45:49 +02:00
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
2003-09-25 07:17:11 +02:00
if ( at_most > buf - > len - buf - > datalen )
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 ) {
2003-08-14 19:13:52 +02:00
if ( ! ERRNO_EAGAIN ( errno ) ) { /* it's a real error */
2002-06-27 00:45:49 +02:00
return - 1 ;
}
2003-08-14 19:13:52 +02:00
# ifdef MS_WINDOWS
e = correct_socket_errno ( s ) ;
2003-09-09 01:10:24 +02:00
if ( ! ERRNO_EAGAIN ( e ) ) { /* no, it *is* a real error! */
2003-08-14 19:13:52 +02:00
return - 1 ;
}
# endif
2002-06-27 00:45:49 +02:00
return 0 ;
} 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 ;
}
}
2003-09-25 07:17:11 +02:00
int read_to_buf_tls ( tor_tls * tls , int 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-04-27 00:22:18 +02:00
log_fn ( LOG_DEBUG , " start: %d on buf, %d pending, at_most %d. " , ( int ) buf_datalen ( buf ) ,
tor_tls_get_pending_bytes ( tls ) , at_most ) ;
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
2003-09-25 07:17:11 +02:00
if ( at_most > buf - > len - buf - > datalen )
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-04-27 00:22:18 +02:00
log_fn ( LOG_DEBUG , " before: %d on buf, %d pending, at_most %d. " , ( int ) buf_datalen ( buf ) ,
tor_tls_get_pending_bytes ( tls ) , 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
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
/* push from buf onto s
* then memmove to front of buf
2004-02-28 06:22:07 +01:00
* return - 1 or how many bytes you just flushed */
2002-06-27 00:45:49 +02:00
int write_result ;
2003-08-14 19:13:52 +02:00
# ifdef MS_WINDOWS
int e ;
# endif
2002-06-27 00:45:49 +02:00
2004-04-25 22:37:37 +02:00
assert_buf_ok ( buf ) ;
tor_assert ( buf_flushlen & & ( s > = 0 ) & & ( * 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 ) {
2003-08-14 19:13:52 +02:00
if ( ! ERRNO_EAGAIN ( errno ) ) { /* it's a real error */
2004-04-25 22:37:37 +02:00
tor_assert ( errno ! = EPIPE ) ; /* get a stack trace to find epipe bugs */
2003-08-14 19:13:52 +02:00
return - 1 ;
}
# ifdef MS_WINDOWS
e = correct_socket_errno ( s ) ;
2003-09-09 01:10:24 +02:00
if ( ! ERRNO_EAGAIN ( e ) ) { /* no, it *is* a real error! */
2002-06-27 00:45:49 +02:00
return - 1 ;
}
2003-08-14 19:13:52 +02:00
# endif
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
}
}
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 ;
}
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
}
2003-09-25 07:17:11 +02:00
int fetch_from_buf ( char * string , int 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
}
2003-09-17 22:09:06 +02:00
/* There is a (possibly incomplete) http statement on *buf, of the
2004-03-31 07:01:30 +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
* strdup headers into * headers_out , and nul - terminate it .
2004-03-31 07:10:34 +02:00
* memdup body into * body_out , and nul - terminate it .
2004-03-31 07:01:30 +02:00
* Then remove them from buf , and return 1.
*
* If headers or body is NULL , discard that part of the buf .
2003-09-17 22:09:06 +02:00
* 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 ) {
2003-09-17 22:09:06 +02:00
char * headers , * body ;
int i ;
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 ;
2003-09-25 07:17:11 +02:00
i = find_on_inbuf ( " \r \n \r \n " , 4 , buf ) ;
2003-09-17 22:09:06 +02:00
if ( i < 0 ) {
log_fn ( LOG_DEBUG , " headers not all here yet. " ) ;
return 0 ;
}
2003-10-15 21:17:21 +02:00
body = buf - > mem + i ;
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: "
2003-12-17 22:09:31 +01:00
i = find_str_in_str ( CONTENT_LENGTH , strlen ( CONTENT_LENGTH ) ,
2003-09-25 07:17:11 +02:00
headers , headerlen ) ;
2003-09-17 22:09:06 +02:00
if ( i > 0 ) {
contentlen = atoi ( headers + i ) ;
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 ;
}
2003-10-04 03:37:01 +02:00
/* There is a (possibly incomplete) socks handshake on buf, of one
* of the forms
* 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-02-26 23:02:22 +01:00
* assign to req , and return 1.
2003-09-18 10:11:31 +02:00
* If it ' s invalid or too big , return - 1.
2003-10-04 03:37:01 +02:00
* Else it ' s not all there yet , leave buf alone and return 0.
2004-02-26 23:02:22 +01:00
* If you want to specify the socks reply , write it into req - > reply
* and set req - > replylen , else leave req - > replylen alone .
* If returning 0 or - 1 , req - > address and req - > port 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
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 ) ;
2003-10-04 03:37:01 +02:00
if ( buf - > datalen < 2 + nummethods )
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 */
2003-10-15 21:17:21 +02:00
if ( * ( buf - > mem + 1 ) ! = 1 ) { /* not a connect? we don't support it. */
2004-02-28 08:01:22 +01:00
log_fn ( LOG_WARN , " socks5: command %d not '1'. Rejecting. " , * ( buf - > mem + 1 ) ) ;
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 */
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 ) ;
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 ) ;
2003-10-04 03:37:01 +02:00
if ( buf - > datalen < 7 + len ) /* addr/port there? */
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 */
2003-10-15 21:17:21 +02:00
if ( * ( buf - > mem + 1 ) ! = 1 ) { /* not a connect? we don't support it. */
2004-02-28 08:01:22 +01:00
log_fn ( LOG_WARN , " socks4: command %d not '1'. Rejecting. " , * ( buf - > mem + 1 ) ) ;
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 ) ) ;
2003-11-11 03:41:31 +01:00
if ( ! req - > port | | ! 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 ;
}
2003-10-15 21:17:21 +02:00
next = memchr ( buf - > mem + SOCKS4_NETWORK_LEN , 0 , buf - > datalen ) ;
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 ;
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-02-26 23:10:55 +01:00
" See <a href= \" http://freehaven.net/tor/cvs/INSTALL \" >http://freehaven.net/tor/cvs/INSTALL</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-04-25 22:37:37 +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
}
2004-04-25 22:37:37 +02:00
2003-04-07 04:12:02 +02:00
/*
Local Variables :
mode : c
indent - tabs - mode : nil
c - basic - offset : 2
End :
*/