2000-11-01 23:11:19 +00:00
|
|
|
#include "tunala.h"
|
|
|
|
|
|
|
|
#ifndef NO_BUFFER
|
|
|
|
|
|
|
|
void buffer_init(buffer_t *buf)
|
|
|
|
{
|
|
|
|
buf->used = 0;
|
2000-12-20 19:30:19 +00:00
|
|
|
buf->total_in = buf->total_out = 0;
|
2000-11-01 23:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void buffer_close(buffer_t *buf)
|
|
|
|
{
|
2000-12-20 19:30:19 +00:00
|
|
|
/* Our data is static - nothing needs "release", just reset it */
|
|
|
|
buf->used = 0;
|
2000-11-01 23:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Code these simple ones in compact form */
|
|
|
|
unsigned int buffer_used(buffer_t *buf) {
|
|
|
|
return buf->used; }
|
|
|
|
unsigned int buffer_unused(buffer_t *buf) {
|
|
|
|
return (MAX_DATA_SIZE - buf->used); }
|
|
|
|
int buffer_full(buffer_t *buf) {
|
|
|
|
return (buf->used == MAX_DATA_SIZE ? 1 : 0); }
|
|
|
|
int buffer_notfull(buffer_t *buf) {
|
|
|
|
return (buf->used < MAX_DATA_SIZE ? 1 : 0); }
|
|
|
|
int buffer_empty(buffer_t *buf) {
|
|
|
|
return (buf->used == 0 ? 1 : 0); }
|
|
|
|
int buffer_notempty(buffer_t *buf) {
|
|
|
|
return (buf->used > 0 ? 1 : 0); }
|
2000-12-20 19:30:19 +00:00
|
|
|
unsigned long buffer_total_in(buffer_t *buf) {
|
|
|
|
return buf->total_in; }
|
|
|
|
unsigned long buffer_total_out(buffer_t *buf) {
|
|
|
|
return buf->total_out; }
|
|
|
|
|
|
|
|
/* These 3 static (internal) functions don't adjust the "total" variables as
|
|
|
|
* it's not sure when they're called how it should be interpreted. Only the
|
|
|
|
* higher-level "buffer_[to|from]_[fd|SSL|BIO]" functions should alter these
|
|
|
|
* values. */
|
2001-02-12 02:30:19 +00:00
|
|
|
#if 0 /* To avoid "unused" warnings */
|
2000-12-20 19:30:19 +00:00
|
|
|
static unsigned int buffer_adddata(buffer_t *buf, const unsigned char *ptr,
|
2000-11-01 23:11:19 +00:00
|
|
|
unsigned int size)
|
|
|
|
{
|
|
|
|
unsigned int added = MAX_DATA_SIZE - buf->used;
|
|
|
|
if(added > size)
|
|
|
|
added = size;
|
|
|
|
if(added == 0)
|
|
|
|
return 0;
|
|
|
|
memcpy(buf->data + buf->used, ptr, added);
|
|
|
|
buf->used += added;
|
2000-12-20 19:30:19 +00:00
|
|
|
buf->total_in += added;
|
2000-11-01 23:11:19 +00:00
|
|
|
return added;
|
|
|
|
}
|
|
|
|
|
2001-02-12 02:30:19 +00:00
|
|
|
static unsigned int buffer_tobuffer(buffer_t *to, buffer_t *from, int cap)
|
|
|
|
{
|
|
|
|
unsigned int moved, tomove = from->used;
|
|
|
|
if((int)tomove > cap)
|
|
|
|
tomove = cap;
|
|
|
|
if(tomove == 0)
|
|
|
|
return 0;
|
|
|
|
moved = buffer_adddata(to, from->data, tomove);
|
|
|
|
if(moved == 0)
|
|
|
|
return 0;
|
|
|
|
buffer_takedata(from, NULL, moved);
|
|
|
|
return moved;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-12-20 19:30:19 +00:00
|
|
|
static unsigned int buffer_takedata(buffer_t *buf, unsigned char *ptr,
|
2000-11-01 23:11:19 +00:00
|
|
|
unsigned int size)
|
|
|
|
{
|
|
|
|
unsigned int taken = buf->used;
|
|
|
|
if(taken > size)
|
|
|
|
taken = size;
|
|
|
|
if(taken == 0)
|
|
|
|
return 0;
|
|
|
|
if(ptr)
|
|
|
|
memcpy(ptr, buf->data, taken);
|
|
|
|
buf->used -= taken;
|
|
|
|
/* Do we have to scroll? */
|
|
|
|
if(buf->used > 0)
|
|
|
|
memmove(buf->data, buf->data + taken, buf->used);
|
|
|
|
return taken;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NO_IP
|
|
|
|
|
|
|
|
int buffer_from_fd(buffer_t *buf, int fd)
|
|
|
|
{
|
2002-01-10 06:03:12 +00:00
|
|
|
int toread = buffer_unused(buf);
|
2000-11-01 23:11:19 +00:00
|
|
|
if(toread == 0)
|
|
|
|
/* Shouldn't be called in this case! */
|
|
|
|
abort();
|
|
|
|
toread = read(fd, buf->data + buf->used, toread);
|
2000-12-20 19:30:19 +00:00
|
|
|
if(toread > 0) {
|
2000-11-01 23:11:19 +00:00
|
|
|
buf->used += toread;
|
2000-12-20 19:30:19 +00:00
|
|
|
buf->total_in += toread;
|
|
|
|
}
|
2000-11-01 23:11:19 +00:00
|
|
|
return toread;
|
|
|
|
}
|
|
|
|
|
|
|
|
int buffer_to_fd(buffer_t *buf, int fd)
|
|
|
|
{
|
2002-01-10 06:03:12 +00:00
|
|
|
int towrite = buffer_used(buf);
|
2000-11-01 23:11:19 +00:00
|
|
|
if(towrite == 0)
|
|
|
|
/* Shouldn't be called in this case! */
|
|
|
|
abort();
|
|
|
|
towrite = write(fd, buf->data, towrite);
|
2000-12-20 19:30:19 +00:00
|
|
|
if(towrite > 0) {
|
2000-11-01 23:11:19 +00:00
|
|
|
buffer_takedata(buf, NULL, towrite);
|
2000-12-20 19:30:19 +00:00
|
|
|
buf->total_out += towrite;
|
|
|
|
}
|
2000-11-01 23:11:19 +00:00
|
|
|
return towrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !defined(NO_IP) */
|
|
|
|
|
|
|
|
#ifndef NO_OPENSSL
|
|
|
|
|
2000-11-28 23:27:23 +00:00
|
|
|
static void int_ssl_check(SSL *s, int ret)
|
|
|
|
{
|
|
|
|
int e = SSL_get_error(s, ret);
|
|
|
|
switch(e) {
|
|
|
|
/* These seem to be harmless and already "dealt with" by our
|
|
|
|
* non-blocking environment. NB: "ZERO_RETURN" is the clean
|
|
|
|
* "error" indicating a successfully closed SSL tunnel. We let
|
|
|
|
* this happen because our IO loop should not appear to have
|
|
|
|
* broken on this condition - and outside the IO loop, the
|
|
|
|
* "shutdown" state is checked. */
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
case SSL_ERROR_WANT_X509_LOOKUP:
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
|
|
|
return;
|
|
|
|
/* These seem to be indications of a genuine error that should
|
|
|
|
* result in the SSL tunnel being regarded as "dead". */
|
|
|
|
case SSL_ERROR_SYSCALL:
|
|
|
|
case SSL_ERROR_SSL:
|
|
|
|
SSL_set_app_data(s, (char *)1);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* For any other errors that (a) exist, and (b) crop up - we need to
|
|
|
|
* interpret what to do with them - so "politely inform" the caller that
|
|
|
|
* the code needs updating here. */
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2000-11-01 23:11:19 +00:00
|
|
|
void buffer_from_SSL(buffer_t *buf, SSL *ssl)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if(!ssl || buffer_full(buf))
|
|
|
|
return;
|
|
|
|
ret = SSL_read(ssl, buf->data + buf->used, buffer_unused(buf));
|
2000-12-20 19:30:19 +00:00
|
|
|
if(ret > 0) {
|
2000-11-01 23:11:19 +00:00
|
|
|
buf->used += ret;
|
2000-12-20 19:30:19 +00:00
|
|
|
buf->total_in += ret;
|
|
|
|
}
|
2000-11-28 23:27:23 +00:00
|
|
|
if(ret < 0)
|
|
|
|
int_ssl_check(ssl, ret);
|
2000-11-01 23:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void buffer_to_SSL(buffer_t *buf, SSL *ssl)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if(!ssl || buffer_empty(buf))
|
|
|
|
return;
|
|
|
|
ret = SSL_write(ssl, buf->data, buf->used);
|
2000-12-20 19:30:19 +00:00
|
|
|
if(ret > 0) {
|
2000-11-01 23:11:19 +00:00
|
|
|
buffer_takedata(buf, NULL, ret);
|
2000-12-20 19:30:19 +00:00
|
|
|
buf->total_out += ret;
|
|
|
|
}
|
2000-11-28 23:27:23 +00:00
|
|
|
if(ret < 0)
|
|
|
|
int_ssl_check(ssl, ret);
|
2000-11-01 23:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void buffer_from_BIO(buffer_t *buf, BIO *bio)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if(!bio || buffer_full(buf))
|
|
|
|
return;
|
|
|
|
ret = BIO_read(bio, buf->data + buf->used, buffer_unused(buf));
|
2000-12-20 19:30:19 +00:00
|
|
|
if(ret > 0) {
|
2000-11-01 23:11:19 +00:00
|
|
|
buf->used += ret;
|
2000-12-20 19:30:19 +00:00
|
|
|
buf->total_in += ret;
|
|
|
|
}
|
2000-11-01 23:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void buffer_to_BIO(buffer_t *buf, BIO *bio)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if(!bio || buffer_empty(buf))
|
|
|
|
return;
|
|
|
|
ret = BIO_write(bio, buf->data, buf->used);
|
2000-12-20 19:30:19 +00:00
|
|
|
if(ret > 0) {
|
2000-11-01 23:11:19 +00:00
|
|
|
buffer_takedata(buf, NULL, ret);
|
2000-12-20 19:30:19 +00:00
|
|
|
buf->total_out += ret;
|
|
|
|
}
|
2000-11-01 23:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !defined(NO_OPENSSL) */
|
|
|
|
|
|
|
|
#endif /* !defined(NO_BUFFER) */
|