2006-02-09 06:46:49 +01:00
|
|
|
/* Copyright (c) 2001 Matej Pfajfar.
|
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
2007-02-12 22:39:53 +01:00
|
|
|
* Copyright (c) 2004-2007, 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$ */
|
2005-12-14 21:40:40 +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"
|
|
|
|
|
2006-06-18 17:53:54 +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
|
|
|
/* If SENTINELS is defined, check for attempts to write beyond the
|
2007-02-16 21:01:02 +01:00
|
|
|
* end/before the start of the buffer.
|
2005-04-26 22:53:22 +02:00
|
|
|
*/
|
2007-02-16 21:01:02 +01:00
|
|
|
#ifdef SENTINELS
|
2007-02-24 08:50:38 +01:00
|
|
|
/** 4-byte value to write at the start of each buffer memory region. */
|
2005-04-26 22:53:22 +02:00
|
|
|
#define START_MAGIC 0x70370370u
|
2007-02-24 08:50:38 +01:00
|
|
|
/** 4-byte value to write at the end of each buffer memory region. */
|
2005-04-26 22:53:22 +02:00
|
|
|
#define END_MAGIC 0xA0B0C0D0u
|
2007-02-24 08:50:38 +01:00
|
|
|
/** Given buf->mem, yield a pointer to the raw memory region (for free(),
|
|
|
|
* realloc(), and so on). */
|
2005-04-27 22:02:52 +02:00
|
|
|
#define RAW_MEM(m) ((void*)(((char*)m)-4))
|
2007-02-24 08:50:38 +01:00
|
|
|
/** Given a pointer to the raw memory region (from malloc() or realloc()),
|
2007-02-16 21:01:02 +01:00
|
|
|
* yield the correct value for buf->mem (just past the first sentinel). */
|
2005-04-27 22:02:52 +02:00
|
|
|
#define GUARDED_MEM(m) ((void*)(((char*)m)+4))
|
2007-02-24 08:50:38 +01:00
|
|
|
/** How much memory do we need to allocate for a buffer to hold <b>ln</b> bytes
|
2007-02-16 21:01:02 +01:00
|
|
|
* of data? */
|
2005-04-26 22:53:22 +02:00
|
|
|
#define ALLOC_LEN(ln) ((ln)+8)
|
2007-02-24 08:50:38 +01:00
|
|
|
/** Initialize the sentinel values on <b>m</b> (a value of buf->mem), which
|
2007-02-16 21:01:02 +01:00
|
|
|
* has <b>ln</b> useful bytes. */
|
2005-04-26 22:53:22 +02:00
|
|
|
#define SET_GUARDS(m, ln) \
|
2007-06-17 20:22:39 +02:00
|
|
|
STMT_BEGIN \
|
|
|
|
set_uint32((m)-4,START_MAGIC); \
|
|
|
|
set_uint32((m)+ln,END_MAGIC); \
|
|
|
|
STMT_END
|
2005-04-26 22:53:22 +02:00
|
|
|
#else
|
|
|
|
#define RAW_MEM(m) (m)
|
|
|
|
#define GUARDED_MEM(m) (m)
|
|
|
|
#define ALLOC_LEN(ln) (ln)
|
2007-06-17 20:22:39 +02:00
|
|
|
#define SET_GUARDS(m,ln) STMT_NIL
|
2005-04-26 22:53:22 +02:00
|
|
|
#endif
|
|
|
|
|
2005-04-27 02:53:44 +02:00
|
|
|
#ifdef PARANOIA
|
2007-06-17 20:22:39 +02:00
|
|
|
#define check() STMT_BEGIN assert_buf_ok(buf); STMT_END
|
2005-04-27 02:53:44 +02:00
|
|
|
#else
|
2007-06-17 20:22:39 +02:00
|
|
|
#define check() STMT_NIL
|
2005-04-27 02:53:44 +02:00
|
|
|
#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
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Magic value for buf_t.magic, to catch pointer errors. */
|
2004-03-03 23:49:15 +01:00
|
|
|
#define BUFFER_MAGIC 0xB0FFF312u
|
2005-10-06 06:33:40 +02:00
|
|
|
/** A resizeable buffer, optimized for reading and writing. */
|
2003-09-25 07:17:11 +02:00
|
|
|
struct buf_t {
|
2005-12-14 21:40:40 +01:00
|
|
|
uint32_t magic; /**< Magic cookie for debugging: Must be set to
|
2007-02-24 08:50:38 +01:00
|
|
|
* 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. */
|
2007-02-24 08:50:38 +01: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. */
|
2006-08-28 05:16:02 +02:00
|
|
|
size_t memsize; /**< How many bytes did we actually allocate? Can be less
|
|
|
|
* than 'len' if we shortened 'len' by a few bytes to make
|
|
|
|
* zlib wrap around more easily. */
|
2004-05-09 18:47:25 +02:00
|
|
|
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
|
|
|
/** 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_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 {
|
2005-09-30 22:47:58 +02:00
|
|
|
char *newmem, *oldmem;
|
2005-04-27 04:55:21 +02:00
|
|
|
size_t sz = (buf->mem+buf->len)-buf->cur;
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_BUG, "Unexpected non-normalized buffer.");
|
2006-06-18 17:53:54 +02:00
|
|
|
newmem = GUARDED_MEM(tor_malloc(ALLOC_LEN(buf->memsize)));
|
|
|
|
SET_GUARDS(newmem, buf->memsize);
|
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);
|
2005-09-30 22:47:58 +02:00
|
|
|
oldmem = RAW_MEM(buf->mem);
|
|
|
|
tor_free(oldmem); /* Can't use tor_free directly. */
|
2005-04-27 04:55:21 +02:00
|
|
|
buf->mem = buf->cur = newmem;
|
2006-06-18 17:53:54 +02:00
|
|
|
buf->len = buf->memsize;
|
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->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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-10 00:39:49 +02:00
|
|
|
/** A freelist of buffer RAM chunks. */
|
|
|
|
typedef struct free_mem_list_t {
|
|
|
|
char *list; /**< The first item on the list; begins with pointer to the
|
|
|
|
* next item. */
|
|
|
|
int len; /**< How many entries in <b>list</b>. */
|
|
|
|
int lowwater; /**< The smallest that list has gotten since the last call to
|
|
|
|
* buf_shrink_freelists(). */
|
|
|
|
const size_t chunksize; /**< How big are the items on the list? */
|
|
|
|
const int slack; /**< We always keep at least this many items on the list
|
|
|
|
* when shrinking it. */
|
|
|
|
const int max; /**< How many elements are we willing to throw onto the list?
|
|
|
|
*/
|
|
|
|
} free_mem_list_t;
|
|
|
|
|
|
|
|
/** Freelists to hold 4k and 16k memory chunks. This seems to be what
|
|
|
|
* we use most. */
|
|
|
|
static free_mem_list_t free_mem_list_4k = { NULL, 0, 0, 4096, 16, INT_MAX };
|
|
|
|
static free_mem_list_t free_mem_list_16k = { NULL, 0, 0, 16384, 4, 128 };
|
|
|
|
|
|
|
|
/** Macro: True iff the size is one for which we keep a freelist. */
|
|
|
|
#define IS_FREELIST_SIZE(sz) ((sz) == 4096 || (sz) == 16384)
|
|
|
|
|
|
|
|
/** Return the proper freelist for chunks of size <b>sz</b>, or fail
|
|
|
|
* with an assertion. */
|
|
|
|
static INLINE free_mem_list_t *
|
|
|
|
get_free_mem_list(size_t sz)
|
|
|
|
{
|
|
|
|
if (sz == 4096) {
|
|
|
|
return &free_mem_list_4k;
|
|
|
|
} else {
|
|
|
|
tor_assert(sz == 16384);
|
|
|
|
return &free_mem_list_16k;
|
|
|
|
}
|
|
|
|
}
|
2007-04-23 16:42:27 +02:00
|
|
|
|
2007-05-10 00:39:49 +02:00
|
|
|
/** Throw the memory from <b>buf</b> onto the appropriate freelist.
|
|
|
|
* Return true if we added the memory, 0 if the freelist was full. */
|
|
|
|
static int
|
2007-04-23 16:42:27 +02:00
|
|
|
add_buf_mem_to_freelist(buf_t *buf)
|
|
|
|
{
|
|
|
|
char *mem;
|
2007-05-10 00:39:49 +02:00
|
|
|
free_mem_list_t *list;
|
2007-04-23 16:42:27 +02:00
|
|
|
|
|
|
|
tor_assert(buf->datalen == 0);
|
|
|
|
tor_assert(buf->mem);
|
2007-05-10 00:39:49 +02:00
|
|
|
list = get_free_mem_list(buf->len);
|
|
|
|
|
|
|
|
if (list->len >= list->max)
|
|
|
|
return 0;
|
2007-04-23 16:42:27 +02:00
|
|
|
|
|
|
|
mem = RAW_MEM(buf->mem);
|
|
|
|
buf->len = buf->memsize = 0;
|
|
|
|
buf->mem = buf->cur = NULL;
|
|
|
|
|
2007-05-10 00:39:49 +02:00
|
|
|
*(char**)mem = list->list;
|
|
|
|
list->list = mem;
|
|
|
|
++list->len;
|
|
|
|
log_debug(LD_GENERAL, "Add buf mem to %d-byte freelist. Freelist has "
|
|
|
|
"%d entries.", (int)list->chunksize, list->len);
|
|
|
|
|
|
|
|
return 1;
|
2007-04-23 16:42:27 +02:00
|
|
|
}
|
|
|
|
|
2007-05-10 00:39:49 +02:00
|
|
|
/** Pull memory of size <b>sz</b> from the appropriate freelist for use by
|
|
|
|
* <b>buf</b>, or allocate it as needed. */
|
2007-04-23 16:42:27 +02:00
|
|
|
static void
|
2007-05-10 00:39:49 +02:00
|
|
|
buf_get_initial_mem(buf_t *buf, size_t sz)
|
2007-04-23 16:42:27 +02:00
|
|
|
{
|
|
|
|
char *mem;
|
2007-05-10 00:39:49 +02:00
|
|
|
free_mem_list_t *list = get_free_mem_list(sz);
|
2007-04-23 16:42:27 +02:00
|
|
|
tor_assert(!buf->mem);
|
|
|
|
|
2007-05-10 00:39:49 +02:00
|
|
|
if (list->list) {
|
|
|
|
mem = list->list;
|
|
|
|
list->list = *(char**)mem;
|
|
|
|
if (--list->len < list->lowwater)
|
|
|
|
list->lowwater = list->len;
|
|
|
|
log_debug(LD_GENERAL, "Got buf mem from %d-byte freelist. Freelist has "
|
|
|
|
"%d entries.", (int)list->chunksize, list->len);
|
2007-04-23 16:42:27 +02:00
|
|
|
} else {
|
2007-05-10 00:39:49 +02:00
|
|
|
log_debug(LD_GENERAL, "%d-byte freelist empty; allocating another chunk.",
|
|
|
|
(int)list->chunksize);
|
|
|
|
tor_assert(list->len == 0);
|
|
|
|
mem = tor_malloc(ALLOC_LEN(sz));
|
2007-04-23 16:42:27 +02:00
|
|
|
}
|
|
|
|
buf->mem = GUARDED_MEM(mem);
|
2007-05-10 00:39:49 +02:00
|
|
|
SET_GUARDS(buf->mem, sz);
|
|
|
|
buf->len = sz;
|
|
|
|
buf->memsize = ALLOC_LEN(sz);
|
2007-04-23 16:42:27 +02:00
|
|
|
buf->cur = buf->mem;
|
|
|
|
}
|
|
|
|
|
2007-05-10 00:39:49 +02:00
|
|
|
/** Remove elements from the freelists that haven't been needed since the
|
2007-07-17 11:26:45 +02:00
|
|
|
* last call to this function. If <b>free_all</b>, we're exiting and we
|
|
|
|
* should clear the whole lists. */
|
2007-05-09 23:43:41 +02:00
|
|
|
void
|
2007-07-17 11:26:45 +02:00
|
|
|
buf_shrink_freelists(int free_all)
|
2007-05-09 23:43:41 +02:00
|
|
|
{
|
2007-05-10 00:39:49 +02:00
|
|
|
int j;
|
|
|
|
for (j = 0; j < 2; ++j) {
|
|
|
|
free_mem_list_t *list = j ? &free_mem_list_16k : &free_mem_list_4k;
|
|
|
|
if (list->lowwater > list->slack) {
|
2007-05-24 20:12:41 +02:00
|
|
|
int i, n_to_skip, n_to_free;
|
|
|
|
char **ptr;
|
2007-05-10 00:39:49 +02:00
|
|
|
log_info(LD_GENERAL, "We haven't used %d/%d allocated %d-byte buffer "
|
|
|
|
"memory chunks since the last call; freeing all but %d of them",
|
2007-05-10 06:04:44 +02:00
|
|
|
list->lowwater, list->len, (int)list->chunksize, list->slack);
|
2007-07-17 11:26:45 +02:00
|
|
|
if (free_all) /* Free every one of them */
|
|
|
|
n_to_free = list->len;
|
|
|
|
else /* Skip over the slack and non-lowwater entries */
|
|
|
|
n_to_free = list->lowwater - list->slack;
|
2007-05-24 20:12:41 +02:00
|
|
|
n_to_skip = list->len - n_to_free;
|
|
|
|
for (ptr = &list->list, i = 0; i < n_to_skip; ++i) {
|
|
|
|
char *mem = *ptr;
|
|
|
|
tor_assert(mem);
|
|
|
|
ptr = (char**)mem;
|
|
|
|
}
|
|
|
|
/* And free the remaining entries. */
|
|
|
|
for (i = 0; i < n_to_free; ++i) {
|
|
|
|
char *mem = *ptr;
|
|
|
|
tor_assert(mem);
|
|
|
|
*ptr = *(char**)mem;
|
|
|
|
tor_free(mem);
|
|
|
|
--list->len;
|
|
|
|
}
|
2007-05-09 23:43:41 +02:00
|
|
|
}
|
2007-05-10 00:39:49 +02:00
|
|
|
list->lowwater = list->len;
|
2007-05-09 23:43:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
/** 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,
|
2006-11-14 04:45:48 +01:00
|
|
|
(size_t)(buf->len-offset));
|
2005-04-26 22:53:22 +02:00
|
|
|
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
|
|
|
|
2007-05-10 17:25:40 +02:00
|
|
|
if (buf->len == 0 && new_capacity < MIN_LAZY_SHRINK_SIZE)
|
2007-04-23 16:42:27 +02:00
|
|
|
new_capacity = MIN_LAZY_SHRINK_SIZE;
|
2007-05-10 00:39:49 +02:00
|
|
|
|
|
|
|
if (buf->len == 0 && IS_FREELIST_SIZE(new_capacity)) {
|
2007-04-23 16:42:27 +02:00
|
|
|
tor_assert(!buf->mem);
|
2007-05-10 00:39:49 +02:00
|
|
|
buf_get_initial_mem(buf, new_capacity);
|
2005-06-08 19:27:11 +02:00
|
|
|
} else {
|
2007-04-23 16:42:27 +02:00
|
|
|
char *raw;
|
|
|
|
if (buf->mem)
|
|
|
|
raw = tor_realloc(RAW_MEM(buf->mem), ALLOC_LEN(new_capacity));
|
|
|
|
else {
|
|
|
|
log_info(LD_GENERAL, "Jumping straight from 0 bytes to %d",
|
|
|
|
(int)new_capacity);
|
|
|
|
raw = tor_malloc(ALLOC_LEN(new_capacity));
|
|
|
|
}
|
|
|
|
buf->mem = GUARDED_MEM(raw);
|
2005-06-08 19:27:11 +02:00
|
|
|
SET_GUARDS(buf->mem, new_capacity);
|
|
|
|
buf->cur = buf->mem+offset;
|
|
|
|
}
|
|
|
|
|
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,
|
2006-11-14 04:45:48 +01:00
|
|
|
(size_t)(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
|
|
|
}
|
2007-04-23 16:42:27 +02:00
|
|
|
buf->len = new_capacity;
|
|
|
|
buf->memsize = ALLOC_LEN(buf->len);
|
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
|
|
|
{
|
2006-06-18 09:27:47 +02:00
|
|
|
size_t new_len, min_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;
|
2006-06-18 09:27:47 +02:00
|
|
|
/* Find the smallest new_len equal to (2**X) for some X; such that
|
|
|
|
* new_len is at least capacity, and at least 2*buf->len.
|
2003-10-15 21:07:07 +02:00
|
|
|
*/
|
2006-06-18 09:27:47 +02:00
|
|
|
min_len = buf->len*2;
|
|
|
|
new_len = 16;
|
|
|
|
while (new_len < min_len)
|
|
|
|
new_len *= 2;
|
2003-10-14 03:34:31 +02:00
|
|
|
while (new_len < capacity)
|
|
|
|
new_len *= 2;
|
2003-10-15 21:07:07 +02:00
|
|
|
/* Resize the buffer. */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_MM,"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>->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;
|
2007-04-23 16:42:27 +02:00
|
|
|
if (buf->datalen == 0 && buf->highwater == 0 &&
|
2007-05-10 00:39:49 +02:00
|
|
|
IS_FREELIST_SIZE(buf->len)) {
|
|
|
|
if (add_buf_mem_to_freelist(buf))
|
|
|
|
return;
|
2007-04-23 16:42:27 +02:00
|
|
|
}
|
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;
|
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_MM,"Shrinking buffer from %d to %d bytes.",
|
|
|
|
(int)buf->len, (int)new_len);
|
2005-05-03 00:49:24 +02:00
|
|
|
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
|
2005-09-30 03:09:52 +02:00
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2007-04-23 16:42:27 +02:00
|
|
|
/** Create and return a new buf with capacity <b>size</b>.
|
|
|
|
* (Used for testing). */
|
2005-06-11 20:52:12 +02:00
|
|
|
buf_t *
|
2005-09-30 03:09:52 +02:00
|
|
|
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;
|
2007-05-10 00:39:49 +02:00
|
|
|
if (IS_FREELIST_SIZE(size)) {
|
|
|
|
buf_get_initial_mem(buf, size);
|
2007-04-23 16:42:27 +02:00
|
|
|
} else {
|
|
|
|
buf->cur = buf->mem = GUARDED_MEM(tor_malloc(ALLOC_LEN(size)));
|
|
|
|
SET_GUARDS(buf->mem, size);
|
|
|
|
buf->len = size;
|
|
|
|
buf->memsize = ALLOC_LEN(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. */
|
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
|
|
|
{
|
|
|
|
buf->datalen = 0;
|
2005-04-27 04:55:21 +02:00
|
|
|
buf->cur = buf->mem;
|
2007-04-23 16:42:27 +02:00
|
|
|
/* buf->len = buf->memsize; bad. */
|
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)
|
|
|
|
{
|
2005-09-30 22:47:58 +02:00
|
|
|
char *oldmem;
|
2004-03-03 23:49:15 +01:00
|
|
|
assert_buf_ok(buf);
|
|
|
|
buf->magic = 0xDEADBEEF;
|
2007-05-10 00:39:49 +02:00
|
|
|
if (IS_FREELIST_SIZE(buf->len)) {
|
2007-04-24 01:24:53 +02:00
|
|
|
buf->datalen = 0; /* Avoid assert in add_buf_mem_to_freelist. */
|
2007-04-23 16:42:27 +02:00
|
|
|
add_buf_mem_to_freelist(buf);
|
2007-05-10 00:39:49 +02:00
|
|
|
}
|
|
|
|
if (buf->mem) {
|
|
|
|
/* The freelist didn't want the RAM. */
|
2007-04-23 16:42:27 +02:00
|
|
|
oldmem = RAW_MEM(buf->mem);
|
|
|
|
tor_free(oldmem);
|
|
|
|
}
|
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);
|
2006-09-19 22:41:31 +02:00
|
|
|
read_result = tor_socket_recv(s, pos, at_most, 0);
|
2005-04-26 22:53:22 +02:00
|
|
|
if (read_result < 0) {
|
|
|
|
int e = tor_socket_errno(s);
|
|
|
|
if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
|
2006-04-08 09:54:11 +02:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
if (e == WSAENOBUFS)
|
|
|
|
log_warn(LD_NET,"recv() failed: WSAENOBUFS. Not enough ram?");
|
|
|
|
#endif
|
2005-04-26 22:53:22 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0; /* would block. */
|
|
|
|
} else if (read_result == 0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_NET,"Encountered eof");
|
2005-04-26 22:53:22 +02:00
|
|
|
*reached_eof = 1;
|
|
|
|
return 0;
|
|
|
|
} else { /* we read some bytes */
|
|
|
|
buf->datalen += read_result;
|
2005-05-03 00:49:24 +02:00
|
|
|
if (buf->datalen > buf->highwater)
|
|
|
|
buf->highwater = buf->datalen;
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_NET,"Read %d bytes. %d on inbuf.",read_result,
|
|
|
|
(int)buf->datalen);
|
2005-04-26 22:53:22 +02:00
|
|
|
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
|
|
|
|
2005-11-30 23:48:58 +01: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
|
2005-10-06 06:33:40 +02:00
|
|
|
read_to_buf_tls_impl(tor_tls_t *tls, size_t at_most, buf_t *buf, char *next)
|
2005-04-26 22:53:22 +02:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_NET,"before: %d on buf, %d pending, at_most %d.",
|
|
|
|
(int)buf_datalen(buf), (int)tor_tls_get_pending_bytes(tls),
|
|
|
|
(int)at_most);
|
2005-04-26 22:53:22 +02:00
|
|
|
r = tor_tls_read(tls, next, at_most);
|
|
|
|
if (r<0)
|
|
|
|
return r;
|
|
|
|
buf->datalen += r;
|
2005-05-03 00:49:24 +02:00
|
|
|
if (buf->datalen > buf->highwater)
|
|
|
|
buf->highwater = buf->datalen;
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_NET,"Read %d bytes. %d on inbuf; %d pending",r,
|
|
|
|
(int)buf->datalen,(int)tor_tls_get_pending_bytes(tls));
|
2005-04-26 22:53:22 +02:00
|
|
|
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
|
2005-10-06 06:33:40 +02:00
|
|
|
read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
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
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_NET,"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))
|
2007-01-15 22:13:37 +01:00
|
|
|
return TOR_TLS_ERROR_MISC;
|
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;
|
|
|
|
|
2006-09-19 22:41:31 +02:00
|
|
|
write_result = tor_socket_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 */
|
2006-04-08 09:54:11 +02:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
if (e == WSAENOBUFS)
|
|
|
|
log_warn(LD_NET,"write() failed: WSAENOBUFS. Not enough ram?");
|
|
|
|
#endif
|
2005-04-26 22:53:22 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_NET,"write() would block, returning.");
|
2005-04-26 22:53:22 +02:00
|
|
|
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-10-29 20:19:37 +02:00
|
|
|
* <b>sz</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
|
2005-10-29 20:19:37 +02:00
|
|
|
flush_buf(int s, buf_t *buf, size_t sz, 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
|
|
|
|
2005-11-30 23:48:58 +01: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);
|
2005-10-29 20:19:37 +02:00
|
|
|
tor_assert(sz <= *buf_flushlen);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2005-10-29 20:19:37 +02:00
|
|
|
if (sz == 0) /* nothing to flush */
|
2002-06-27 00:45:49 +02:00
|
|
|
return 0;
|
|
|
|
|
2005-10-29 20:19:37 +02:00
|
|
|
flushlen0 = sz;
|
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
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_NET,"%d: flushed %d bytes, %d ready to flush, %d remain.",
|
|
|
|
s,r,(int)*buf_flushlen,(int)buf->datalen);
|
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_impl(s, buf, flushlen1, buf_flushlen);
|
2005-04-27 02:53:44 +02:00
|
|
|
check();
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_NET,"%d: flushed %d bytes, %d ready to flush, %d remain.",
|
|
|
|
s,r,(int)*buf_flushlen,(int)buf->datalen);
|
2005-04-26 22:53:22 +02:00
|
|
|
if (r<0)
|
|
|
|
return r;
|
|
|
|
flushed += r;
|
|
|
|
}
|
|
|
|
return flushed;
|
|
|
|
}
|
2003-10-14 03:34:31 +02:00
|
|
|
|
2006-12-13 23:46:42 +01:00
|
|
|
/** Helper for flush_buf_tls(): try to write <b>sz</b> bytes (or more if
|
|
|
|
* required by a previous write) from buffer <b>buf</b> onto TLS object
|
|
|
|
* <b>tls</b>. On success, deduct the bytes written from
|
|
|
|
* *<b>buf_flushlen</b>. Return the number of bytes written on success, -1 on
|
|
|
|
* failure.
|
2005-06-11 20:52:12 +02:00
|
|
|
*/
|
2005-04-26 22:53:22 +02:00
|
|
|
static INLINE int
|
2005-10-06 06:33:40 +02:00
|
|
|
flush_buf_tls_impl(tor_tls_t *tls, buf_t *buf, size_t sz, size_t *buf_flushlen)
|
2005-04-26 22:53:22 +02:00
|
|
|
{
|
|
|
|
int r;
|
2006-12-13 23:46:42 +01:00
|
|
|
size_t forced;
|
2005-04-26 22:53:22 +02:00
|
|
|
|
2006-12-13 23:46:42 +01:00
|
|
|
forced = tor_tls_get_forced_write_size(tls);
|
2006-12-14 00:00:05 +01:00
|
|
|
if (forced > sz)
|
2006-12-13 23:46:42 +01:00
|
|
|
sz = forced;
|
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);
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_NET,"flushed %d bytes, %d ready to flush, %d remain.",
|
|
|
|
r,(int)*buf_flushlen,(int)buf->datalen);
|
2005-04-26 22:53:22 +02:00
|
|
|
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
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2005-10-29 20:19:37 +02:00
|
|
|
flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t sz, 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;
|
2005-11-30 23:48:58 +01:00
|
|
|
/* assert_buf_ok(buf); */
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(tls);
|
|
|
|
tor_assert(buf_flushlen);
|
2005-10-29 20:19:37 +02:00
|
|
|
tor_assert(*buf_flushlen <= buf->datalen);
|
|
|
|
tor_assert(sz <= *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
|
|
|
|
2005-10-29 20:19:37 +02:00
|
|
|
flushlen0 = sz;
|
2005-04-27 04:55:21 +02:00
|
|
|
_split_range(buf, buf->cur, &flushlen0, &flushlen1);
|
2006-12-14 04:26:42 +01:00
|
|
|
if (flushlen1) {
|
|
|
|
size_t forced = tor_tls_get_forced_write_size(tls);
|
|
|
|
tor_assert(forced <= flushlen0);
|
|
|
|
}
|
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);
|
2005-11-30 23:48:58 +01:00
|
|
|
/* 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)) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_MM, "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-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-05-03 00:49:24 +02:00
|
|
|
if (buf->datalen > buf->highwater)
|
|
|
|
buf->highwater = buf->datalen;
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_NET,"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);
|
2005-12-14 21:40:40 +01:00
|
|
|
/* make sure we don't ask for too much */
|
|
|
|
tor_assert(string_len <= buf->datalen);
|
2005-11-30 23:48:58 +01:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-14 21:40:40 +01: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 \<= the number of bytes on the buffer.
|
2005-04-26 22:53:22 +02:00
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2007-04-25 09:20:04 +02:00
|
|
|
/** Move up to *<b>buf_flushlen</b> bytes from <b>buf_in</b> to
|
|
|
|
* <b>buf_out</b>, and modify *<b>buf_flushlen</b> appropriately.
|
2007-04-21 19:26:12 +02:00
|
|
|
* Return the number of bytes actually copied.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen)
|
|
|
|
{
|
|
|
|
char b[4096];
|
|
|
|
size_t cp, len;
|
|
|
|
len = *buf_flushlen;
|
|
|
|
if (len > buf_in->datalen)
|
|
|
|
len = buf_in->datalen;
|
|
|
|
|
|
|
|
cp = len; /* Remember the number of bytes we intend to copy. */
|
|
|
|
while (len) {
|
|
|
|
/* This isn't the most efficient implementation one could imagine, since
|
|
|
|
* it does two copies instead of 1, but I kinda doubt that this will be
|
|
|
|
* critical path. */
|
|
|
|
size_t n = len > sizeof(b) ? sizeof(b) : len;
|
|
|
|
fetch_from_buf(b, n, buf_in);
|
|
|
|
write_to_buf(b, n, buf_out);
|
|
|
|
len -= n;
|
|
|
|
}
|
|
|
|
*buf_flushlen -= cp;
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
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.)
|
2005-10-14 04:26:13 +02:00
|
|
|
* - If force_complete is true, then succeed even if not all of the
|
|
|
|
* content has arrived.
|
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,
|
2005-10-14 04:26:13 +02:00
|
|
|
char **body_out, size_t *body_used, size_t max_bodylen,
|
|
|
|
int force_complete)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
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
|
|
|
|
2005-11-30 23:48:58 +01: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) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_BUG,"Couldn't nul-terminate buffer");
|
2004-05-18 17:35:21 +02:00
|
|
|
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) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_HTTP,"headers not all here yet.");
|
2003-09-17 22:09:06 +02:00
|
|
|
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;
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_HTTP,"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) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_HTTP,"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) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_HTTP,"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) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_PROTOCOL, "Content-Length is less than zero; it looks like "
|
|
|
|
"someone is trying to crash us.");
|
2004-10-12 20:38:36 +02:00
|
|
|
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. */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_HTTP,"Got a contentlen of %d.",(int)contentlen);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (bodylen < contentlen) {
|
2005-10-14 04:26:13 +02:00
|
|
|
if (!force_complete) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_HTTP,"body not all here yet.");
|
2005-10-14 04:26:13 +02:00
|
|
|
return 0; /* not all there yet */
|
|
|
|
}
|
2003-09-17 22:09:06 +02:00
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (bodylen > contentlen) {
|
2004-04-26 23:15:06 +02:00
|
|
|
bodylen = contentlen;
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_HTTP,"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);
|
2006-07-15 21:21:30 +02:00
|
|
|
(*headers_out)[headerlen] = 0; /* nul 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);
|
2006-07-15 21:21:30 +02:00
|
|
|
(*body_out)[bodylen] = 0; /* nul 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
|
|
|
*
|
2005-11-17 00:37:35 +01:00
|
|
|
* If <b>log_sockstype</b> is non-zero, then do a notice-level log of whether
|
|
|
|
* the connection is possibly leaking DNS requests locally or not.
|
|
|
|
*
|
2006-03-19 02:44:53 +01:00
|
|
|
* If <b>safe_socks</b> is true, then reject unsafe socks protocols.
|
|
|
|
*
|
2005-12-14 21:40:40 +01: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
|
2006-03-19 02:44:53 +01:00
|
|
|
fetch_from_buf_socks(buf_t *buf, socks_request_t *req,
|
|
|
|
int log_sockstype, int safe_socks)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2007-05-15 23:17:48 +02:00
|
|
|
unsigned int 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)) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_APP,
|
|
|
|
"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
|
|
|
}
|
2006-08-24 06:51:55 +02:00
|
|
|
/* remove packet from buf. also remove any other extraneous
|
|
|
|
* bytes, to support broken socks clients. */
|
|
|
|
buf_clear(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;
|
2005-12-14 21:40:40 +01:00
|
|
|
req->socks_version = 5; /* remember we've already negotiated auth */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_APP,"socks5: accepted method 0");
|
2003-10-04 03:37:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* we know the method; read in the request */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_APP,"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 &&
|
2006-12-13 01:28:56 +01:00
|
|
|
req->command != SOCKS_COMMAND_CONNECT_DIR &&
|
2006-09-22 02:43:55 +02:00
|
|
|
req->command != SOCKS_COMMAND_RESOLVE &&
|
|
|
|
req->command != SOCKS_COMMAND_RESOLVE_PTR) {
|
|
|
|
/* not a connect or resolve or a resolve_ptr? we don't support it. */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_APP,"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 */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_APP,"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) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_APP,
|
|
|
|
"socks5 IP takes %d bytes, which doesn't fit in %d. "
|
|
|
|
"Rejecting.",
|
|
|
|
(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);
|
2006-09-22 02:43:55 +02:00
|
|
|
if (req->command != SOCKS_COMMAND_RESOLVE_PTR &&
|
|
|
|
!addressmap_have_mapping(req->address) &&
|
2005-09-24 23:56:04 +02:00
|
|
|
!have_warned_about_unsafe_socks) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_APP,
|
|
|
|
"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 "
|
2006-10-15 09:42:51 +02:00
|
|
|
"(e.g. via privoxy or socat) instead. For more information, "
|
2006-02-13 09:28:42 +01:00
|
|
|
"please see http://wiki.noreply.org/noreply/TheOnionRouter/"
|
2006-03-19 02:44:53 +01:00
|
|
|
"TorFAQ#SOCKSAndDNS.%s", req->port,
|
|
|
|
safe_socks ? " Rejecting." : "");
|
2004-08-07 06:03:01 +02:00
|
|
|
// have_warned_about_unsafe_socks = 1; // (for now, warn every time)
|
2007-01-06 06:42:31 +01:00
|
|
|
control_event_client_status(LOG_WARN,
|
|
|
|
"DANGEROUS_SOCKS PROTOCOL=SOCKS5 ADDRESS=%s:%d",
|
|
|
|
req->address, req->port);
|
2006-03-19 02:44:53 +01:00
|
|
|
if (safe_socks)
|
|
|
|
return -1;
|
2004-08-07 06:03:01 +02:00
|
|
|
}
|
2003-10-04 03:37:01 +02:00
|
|
|
return 1;
|
|
|
|
case 3: /* fqdn */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_APP,"socks5: fqdn address type");
|
2007-05-15 23:17:48 +02:00
|
|
|
if (req->command == SOCKS_COMMAND_RESOLVE_PTR) {
|
|
|
|
log_warn(LD_APP, "socks5 received RESOLVE_PTR command with "
|
|
|
|
"hostname type. Rejecting.");
|
|
|
|
return -1;
|
|
|
|
}
|
2005-04-27 04:55:21 +02:00
|
|
|
len = (unsigned char)*(buf->cur+4);
|
2007-05-15 23:17:48 +02:00
|
|
|
if (buf->datalen < 7+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) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_APP,
|
|
|
|
"socks5 hostname is %d bytes, which doesn't fit in "
|
|
|
|
"%d. Rejecting.", 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);
|
2006-03-16 00:36:57 +01:00
|
|
|
if (!tor_strisprint(req->address) || strchr(req->address,'\"')) {
|
|
|
|
log_warn(LD_PROTOCOL,
|
|
|
|
"Your application (using socks5 on port %d) gave Tor "
|
|
|
|
"a malformed hostname: %s. Rejecting the connection.",
|
|
|
|
req->port, escaped(req->address));
|
|
|
|
return -1;
|
|
|
|
}
|
2005-11-17 00:37:35 +01:00
|
|
|
if (log_sockstype)
|
2006-02-13 09:28:42 +01:00
|
|
|
log_notice(LD_APP,
|
|
|
|
"Your application (using socks5 on port %d) gave "
|
|
|
|
"Tor a hostname, which means Tor will do the DNS resolve "
|
|
|
|
"for you. This is good.", req->port);
|
2003-10-04 03:37:01 +02:00
|
|
|
return 1;
|
|
|
|
default: /* unsupported */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_APP,"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 &&
|
2006-12-13 01:28:56 +01:00
|
|
|
req->command != SOCKS_COMMAND_CONNECT_DIR &&
|
2004-11-28 12:39:53 +01:00
|
|
|
req->command != SOCKS_COMMAND_RESOLVE) {
|
2006-09-22 02:43:55 +02:00
|
|
|
/* not a connect or resolve? we don't support it. (No resolve_ptr with
|
|
|
|
* socks4.) */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_APP,"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) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_APP,"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) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_APP,"socks4: destip not in form 0.0.0.x.");
|
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) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_APP,"socks4 addr (%d bytes) too long. Rejecting.",
|
|
|
|
(int)strlen(tmpbuf));
|
2003-10-04 03:37:01 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_APP,
|
2006-03-05 10:50:26 +01:00
|
|
|
"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) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_APP,"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;
|
2005-09-24 23:56:04 +02:00
|
|
|
if (socks4_prot != socks4a &&
|
2006-08-25 23:16:22 +02:00
|
|
|
!addressmap_have_mapping(tmpbuf) &&
|
2005-09-24 23:56:04 +02:00
|
|
|
!have_warned_about_unsafe_socks) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_APP,
|
|
|
|
"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 "
|
2006-03-19 02:44:53 +01:00
|
|
|
"(e.g. via privoxy or socat) instead. For more information, "
|
|
|
|
"please see http://wiki.noreply.org/noreply/TheOnionRouter/"
|
|
|
|
"TorFAQ#SOCKSAndDNS.%s", req->port,
|
|
|
|
safe_socks ? " Rejecting." : "");
|
2004-08-04 01:42:33 +02:00
|
|
|
// have_warned_about_unsafe_socks = 1; // (for now, warn every time)
|
2007-01-06 06:42:31 +01:00
|
|
|
control_event_client_status(LOG_WARN,
|
|
|
|
"DANGEROUS_SOCKS PROTOCOL=SOCKS4 ADDRESS=%s:%d",
|
|
|
|
tmpbuf, req->port);
|
2006-03-19 02:44:53 +01:00
|
|
|
if (safe_socks)
|
|
|
|
return -1;
|
2004-08-04 01:42:33 +02:00
|
|
|
}
|
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) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_APP,"socks4: No part of destaddr here yet.");
|
2004-12-22 11:04:50 +01:00
|
|
|
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) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_APP,"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) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_APP,"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);
|
2006-03-05 10:50:26 +01:00
|
|
|
|
2005-11-17 00:37:35 +01:00
|
|
|
if (log_sockstype)
|
2006-02-13 09:28:42 +01:00
|
|
|
log_notice(LD_APP,
|
|
|
|
"Your application (using socks4a on port %d) gave "
|
|
|
|
"Tor a hostname, which means Tor will do the DNS resolve "
|
|
|
|
"for you. This is good.", req->port);
|
2003-10-04 03:37:01 +02:00
|
|
|
}
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_APP,"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));
|
2006-03-16 00:36:57 +01:00
|
|
|
if (!tor_strisprint(req->address) || strchr(req->address,'\"')) {
|
|
|
|
log_warn(LD_PROTOCOL,
|
|
|
|
"Your application (using socks4 on port %d) gave Tor "
|
|
|
|
"a malformed hostname: %s. Rejecting the connection.",
|
|
|
|
req->port, escaped(req->address));
|
|
|
|
return -1;
|
|
|
|
}
|
2005-12-14 21:40:40 +01:00
|
|
|
/* next points to the final \0 on inbuf */
|
|
|
|
buf_remove_from_front(buf, next-buf->cur+1);
|
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"
|
2005-12-14 21:40:40 +01:00
|
|
|
"It appears you have configured your web browser to use Tor as an HTTP proxy."
|
|
|
|
"\n"
|
|
|
|
"This is not correct: Tor is a SOCKS proxy, not an HTTP proxy.\n"
|
|
|
|
"Please configure your client accordingly.\n"
|
2004-02-27 02:33:02 +01:00
|
|
|
"</p>\n"
|
|
|
|
"<p>\n"
|
2005-12-14 21:40:40 +01:00
|
|
|
"See <a href=\"http://tor.eff.org/documentation.html\">"
|
|
|
|
"http://tor.eff.org/documentation.html</a> for more information.\n"
|
2005-12-09 06:37:26 +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 */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_APP,
|
|
|
|
"Socks version %d not recognized. (Tor is not an http proxy.)",
|
|
|
|
*(buf->cur));
|
2007-01-06 06:42:31 +01:00
|
|
|
{
|
|
|
|
char *tmp = tor_strndup(buf->cur, 8);
|
|
|
|
control_event_client_status(LOG_WARN,
|
|
|
|
"SOCKS_UNKNOWN_PROTOCOL DATA=\"%s\"",
|
|
|
|
escaped(tmp));
|
|
|
|
tor_free(tmp);
|
|
|
|
}
|
2003-09-18 10:11:31 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-04 22:08:28 +01:00
|
|
|
/** Return 1 iff buf looks more like it has an (obsolete) v0 controller
|
|
|
|
* command on it than any valid v1 controller command. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2007-03-04 22:08:28 +01:00
|
|
|
peek_buf_has_control0_command(buf_t *buf)
|
2004-11-03 02:32:26 +01:00
|
|
|
{
|
2007-03-04 22:08:28 +01:00
|
|
|
if (buf->datalen >= 4) {
|
|
|
|
char header[4];
|
|
|
|
uint16_t cmd;
|
|
|
|
peek_from_buf(header, sizeof(header), buf);
|
|
|
|
cmd = ntohs(get_uint16(header+2));
|
|
|
|
if (cmd <= 0x14)
|
|
|
|
return 1; /* This is definitely not a v1 control command. */
|
2005-03-02 21:22:10 +01:00
|
|
|
}
|
2007-03-04 22:08:28 +01:00
|
|
|
return 0;
|
2004-11-03 02:32:26 +01:00
|
|
|
}
|
|
|
|
|
2005-07-12 20:19:30 +02:00
|
|
|
/** Helper: return a pointer to the first instance of <b>c</b> in the
|
|
|
|
* <b>len</b>characters after <b>start</b> on <b>buf</b>. Return NULL if the
|
|
|
|
* character isn't found. */
|
2005-06-17 20:49:55 +02:00
|
|
|
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);
|
2006-10-28 10:17:02 +02:00
|
|
|
cp = memchr(start, c, len);
|
2005-06-17 20:49:55 +02:00
|
|
|
if (cp || !len_rest)
|
|
|
|
return cp;
|
|
|
|
return memchr(buf->mem, c, len_rest);
|
|
|
|
}
|
|
|
|
|
2005-07-12 20:19:30 +02:00
|
|
|
/** Helper: return a pointer to the first CRLF after cp on <b>buf</b>. Return
|
|
|
|
* NULL if no CRLF is found. */
|
2005-06-17 20:49:55 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-12 20:19:30 +02:00
|
|
|
/** Try to read a single CRLF-terminated line from <b>buf</b>, and write it,
|
|
|
|
* NUL-terminated, into the *<b>data_len</b> byte buffer at <b>data_out</b>.
|
|
|
|
* Set *<b>data_len</b> to the number of bytes in the line, not counting the
|
|
|
|
* terminating NUL. Return 1 if we read a whole line, return 0 if we don't
|
|
|
|
* have a whole line yet, and return -1 if we we need to grow the buffer.
|
|
|
|
*/
|
2005-06-17 20:49:55 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-11-14 01:06:31 +01:00
|
|
|
/** Try to read a single LF-terminated line from <b>buf</b>, and write it,
|
|
|
|
* NUL-terminated, into the *<b>data_len</b> byte buffer at <b>data_out</b>.
|
|
|
|
* Set *<b>data_len</b> to the number of bytes in the line, not counting the
|
|
|
|
* terminating NUL. Return 1 if we read a whole line, return 0 if we don't
|
|
|
|
* have a whole line yet, and return -1 if the line length exceeds
|
|
|
|
*<b>data_len</b>.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
fetch_from_buf_line_lf(buf_t *buf, char *data_out, size_t *data_len)
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
size_t remaining = buf->datalen - _buf_offset(buf,buf->cur);
|
|
|
|
cp = find_char_on_buf(buf, buf->cur, remaining, '\n');
|
|
|
|
if (!cp)
|
|
|
|
return 0;
|
|
|
|
sz = _buf_offset(buf, cp);
|
|
|
|
if (sz+2 > *data_len) {
|
|
|
|
*data_len = sz+2;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fetch_from_buf(data_out, sz+1, buf);
|
|
|
|
data_out[sz+1] = '\0';
|
|
|
|
*data_len = sz+1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-08-28 05:16:02 +02:00
|
|
|
/** Compress on uncompress the <b>data_len</b> bytes in <b>data</b> using the
|
|
|
|
* zlib state <b>state</b>, appending the result to <b>buf</b>. If
|
|
|
|
* <b>done</b> is true, flush the data in the state and finish the
|
|
|
|
* compression/uncompression. Return -1 on failure, 0 on success. */
|
2006-06-18 09:27:47 +02:00
|
|
|
int
|
|
|
|
write_to_buf_zlib(buf_t *buf, tor_zlib_state_t *state,
|
|
|
|
const char *data, size_t data_len,
|
|
|
|
int done)
|
|
|
|
{
|
|
|
|
char *next;
|
|
|
|
size_t old_avail, avail;
|
2006-06-18 11:03:48 +02:00
|
|
|
int over = 0;
|
2006-06-24 04:10:21 +02:00
|
|
|
do {
|
2006-06-18 09:27:47 +02:00
|
|
|
buf_ensure_capacity(buf, buf->datalen + 1024);
|
|
|
|
next = _buf_end(buf);
|
|
|
|
if (next < buf->cur)
|
|
|
|
old_avail = avail = buf->cur - next;
|
|
|
|
else
|
2006-06-18 18:39:26 +02:00
|
|
|
old_avail = avail = (buf->mem + buf->len) - next;
|
2006-06-18 09:27:47 +02:00
|
|
|
switch (tor_zlib_process(state, &next, &avail, &data, &data_len, done)) {
|
|
|
|
case TOR_ZLIB_DONE:
|
2006-06-18 11:03:48 +02:00
|
|
|
over = 1;
|
|
|
|
break;
|
2006-06-18 09:27:47 +02:00
|
|
|
case TOR_ZLIB_ERR:
|
|
|
|
return -1;
|
|
|
|
case TOR_ZLIB_OK:
|
|
|
|
if (data_len == 0)
|
2006-06-18 11:03:48 +02:00
|
|
|
over = 1;
|
2006-06-18 09:27:47 +02:00
|
|
|
break;
|
|
|
|
case TOR_ZLIB_BUF_FULL:
|
|
|
|
if (avail && buf->len >= 1024 + buf->datalen) {
|
|
|
|
/* Zlib says we need more room (ZLIB_BUF_FULL), and we're not about
|
|
|
|
* to wrap around (avail != 0), and resizing won't actually make us
|
|
|
|
* un-full: we're at the end of the buffer, and zlib refuses to
|
|
|
|
* append more here, but there's a pile of free space at the start
|
|
|
|
* of the buffer (about 1K). So chop a few characters off the
|
|
|
|
* end of the buffer. This feels silly; anybody got a better hack?
|
|
|
|
*
|
|
|
|
* (We don't just want to expand the buffer nevertheless. Consider a
|
|
|
|
* 1/3 full buffer with a single byte free at the end. zlib will
|
|
|
|
* often refuse to append to that, and so we want to use the
|
|
|
|
* beginning, not double the buffer to be just 1/6 full.)
|
|
|
|
*/
|
|
|
|
tor_assert(next >= buf->cur);
|
|
|
|
buf->len -= avail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf->datalen += old_avail - avail;
|
2006-06-18 10:21:27 +02:00
|
|
|
if (buf->datalen > buf->highwater)
|
|
|
|
buf->highwater = buf->datalen;
|
2006-06-24 04:10:21 +02:00
|
|
|
} while (!over);
|
2006-06-18 22:39:46 +02:00
|
|
|
return 0;
|
2006-06-18 09:27:47 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
void
|
|
|
|
assert_buf_ok(buf_t *buf)
|
2004-03-03 23:49:15 +01:00
|
|
|
{
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(buf);
|
|
|
|
tor_assert(buf->magic == BUFFER_MAGIC);
|
2005-05-03 00:49:24 +02:00
|
|
|
tor_assert(buf->highwater <= buf->len);
|
|
|
|
tor_assert(buf->datalen <= buf->highwater);
|
2007-04-23 16:42:27 +02:00
|
|
|
|
|
|
|
if (buf->mem) {
|
|
|
|
tor_assert(buf->cur >= buf->mem);
|
|
|
|
tor_assert(buf->cur < buf->mem+buf->len);
|
|
|
|
tor_assert(buf->memsize == ALLOC_LEN(buf->len));
|
|
|
|
} else {
|
|
|
|
tor_assert(!buf->cur);
|
|
|
|
tor_assert(!buf->len);
|
|
|
|
tor_assert(!buf->memsize);
|
|
|
|
}
|
|
|
|
|
2005-04-26 23:29:20 +02:00
|
|
|
#ifdef SENTINELS
|
2007-04-23 16:42:27 +02:00
|
|
|
if (buf->mem) {
|
2005-04-27 02:53:44 +02:00
|
|
|
uint32_t u32 = get_uint32(buf->mem - 4);
|
|
|
|
tor_assert(u32 == START_MAGIC);
|
2007-04-23 16:42:27 +02:00
|
|
|
u32 = get_uint32(buf->mem + buf->memsize - 8);
|
2005-04-27 02:53:44 +02:00
|
|
|
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
|
|
|
|