Commit 58544e96 authored by Niels Möller's avatar Niels Möller
Browse files

More paranoid allocation.

Rev: src/packet_dispatch.c:1.5
Rev: src/packet_dispatch.h:1.4
Rev: src/pad.c:1.7
Rev: src/pad.h:1.6
Rev: src/read_data.c:1.2
Rev: src/read_line.c:1.4
Rev: src/server.c:1.2
Rev: src/unpad.c:1.6
Rev: src/unpad.h:1.6
Rev: src/write_buffer.c:1.5
Rev: src/xalloc.c:1.3
Rev: src/xalloc.h:1.3
parent 404a5250
......@@ -6,11 +6,10 @@
#include "xalloc.h"
#include "werror.h"
static int do_dispatch(struct abstract_write **c,,
static int do_dispatch(struct abstract_write **w,
struct lsh_string *packet)
{
struct dispatch_processor *closure
= (struct dispatch_processor *) *c;
struct packet_dispatch *closure = (struct packet_dispatch *) *w;
unsigned start;
unsigned end;
......@@ -34,8 +33,8 @@ static int do_dispatch(struct abstract_write **c,,
if (middle_msg == msg)
{
/* Found right method */
return apply_processor(closure->dispatch_table[middle].f,
packet);
return A_WRITE(closure->dispatch_table[middle].f,
packet);
}
if (middle == start)
/* Not found */
......@@ -48,7 +47,7 @@ static int do_dispatch(struct abstract_write **c,,
}
if (closure->other)
return apply_processor(closure->other, packet);
return A_WRITE(closure->other, packet);
else
return 0;
}
......@@ -58,8 +57,8 @@ make_dispatch_processor(unsigned size,
struct dispatch_assoc *table,
struct abstract_write *other)
{
struct dispatch_processor *closure
= xalloc(sizeof(struct dispatch_processor));
struct packet_dispatch *closure
= xalloc(sizeof(struct packet_dispatch));
unsigned i;
/* Check that message numbers are increasing */
......@@ -67,7 +66,7 @@ make_dispatch_processor(unsigned size,
if (table[i].msg >= table[i+1].msg)
fatal("make_dispatch_processor: Table out of order");
closure->p.f = (abstract_write_f) do_dispatch;
closure->super.write = do_dispatch;
closure->other = other;
closure->table_size = size;
closure->dispatch_table = table;
......
......@@ -6,7 +6,7 @@
#ifndef LSH_PACKET_DISPATH_H_INCLUDED
#define LSH_PACKET_DISPATH_H_INCLUDED
#include "transport.h"
#include "abstract_io.h"
struct dispatch_assoc
{
......@@ -16,7 +16,7 @@ struct dispatch_assoc
struct packet_dispatch
{
struct abstract_write p;
struct abstract_write super;
struct abstract_write *other;
unsigned table_size;
/* Should be sorted by message number */
......
......@@ -6,11 +6,11 @@
#include "xalloc.h"
#include <assert.h>
static int do_pad(struct abstract_write **c,
static int do_pad(struct abstract_write **w,
struct lsh_string *packet)
{
struct pad_processor *closure
= (struct pad_processor *) *c;
struct packet_pad *closure
= (struct packet_pad *) *w;
UINT32 new_size;
UINT8 padding;
......@@ -38,20 +38,19 @@ static int do_pad(struct abstract_write **c,
lsh_string_free(packet);
return apply_processor(closure->c.next, new);
return A_WRITE(closure->super.next, new);
}
struct abstract_write *
make_pad_processor(struct abstract_write *continuation,
unsigned block_size,
random_function random,
void *state)
make_packet_pad(struct abstract_write *continuation,
unsigned block_size,
random_function random,
void *state)
{
struct pad_processor *closure = xalloc(sizeof(struct pad_processor));
struct packet_pad *closure = xalloc(sizeof(struct packet_pad));
closure->super.super = do_pad;
closure->c.next = continuation;
closure->super.super.write = do_pad;
closure->super.next = continuation;
closure->block_size = block_size;
closure->random = random;
closure->state = state;
......
......@@ -6,7 +6,7 @@
#ifndef LSH_PAD_H_INCLUDED
#define LSH_PAD_H_INCLUDED
#include "transport.h"
#include "abstract_io.h"
typedef void (*random_function)(void *state, UINT32 length, UINT8 *dst);
......@@ -15,7 +15,7 @@ typedef void (*random_function)(void *state, UINT32 length, UINT8 *dst);
* etc). */
struct packet_pad
{
struct abstract_write_pipe c;
struct abstract_write_pipe super;
unsigned block_size; /* At least 8, even for stream ciphers */
......
......@@ -3,13 +3,17 @@
*/
#include "read_data.h"
#include "werror.h"
#include "xalloc.h"
static struct read_handler *do_read_data(struct read_data *closure,
struct abstract_read *read)
static int do_read_data(struct read_handler **h,
struct abstract_read *read)
{
struct read_data *closure = (struct read_data *) *h;
while(1)
{
struct lsh_string packet = lsh_string_alloc(closure->block_size);
struct lsh_string *packet = lsh_string_alloc(closure->block_size);
int n = A_READ(read, packet->data, packet->length);
switch(n)
......@@ -18,17 +22,30 @@ static struct read_handler *do_read_data(struct read_data *closure,
lsh_string_free(packet);
break;
case A_FAIL:
werror("do_read_data: read() failed, %s\n", strerror(errno));
/* Fall through */
case A_EOF:
CALLBACK(closure->close_callback)
CALLBACK(closure->close_callback);
return 0;
default:
packet->length = n;
/* FIXME: Use returned value */
A_WRITE(closure->handler, packet);
break;
}
}
}
struct read_handler *make_read_data(struct abstract_write *handler,
struct callback *close_callback,
UINT32 block_size)
{
struct read_data *closure = xalloc(sizeof(struct read_data));
closure->super.handler = do_read_data;
closure->block_size = block_size;
closure->handler = handler;
closure->close_callback = close_callback;
return &closure->super;
}
......@@ -63,7 +63,17 @@ static int do_read_line(struct read_handler **h,
length = end - closure->buffer;
next = PROCESS_LINE(closure->handler, length, closure->buffer);
{
/* Remove line from buffer */
/* Number of characters that have been processed */
UINT32 done = eol - closure->buffer + 1;
UINT32 left = closure->pos - done;
memcpy(closure->buffer, closure->buffer + done, left);
closure->pos = left;
}
if (next)
{
/* Read no more lines. Instead, pass remaining data to next,
......@@ -79,30 +89,18 @@ static int do_read_line(struct read_handler **h,
return 0;
}
/* No data left */
free(closure);
lsh_free(closure);
*h = next;
return 1;
}
else
{
if (closure->handler)
{
/* Read another line */
/* Number of characters that have been processed */
UINT32 done = eol - closure->buffer + 1;
UINT32 left = closure->pos - done;
memcpy(closure->buffer, closure->buffer + done, left);
closure->pos = left;
}
else
{
/* Fail */
return 0;
}
}
if (!closure->handler)
{
/* Fail */
return 0;
}
}
/* Partial line */
if (closure->pos == MAX_LINE)
{
......
......@@ -18,9 +18,11 @@
struct read_handler *make_server_read_line();
struct callback *make_server_close_handler();
static int server_initiate(struct server_callback *closure,
static int server_initiate(struct fd_callback **c,
int fd)
{
struct server_callback *closure = (struct server_callback *) *c;
struct ssh_connection *connection = ssh_connection_alloc();
struct abstract_write *write =
io_read_write(closure->backend, fd,
......@@ -29,12 +31,12 @@ static int server_initiate(struct server_callback *closure,
make_server_close_handler());
connection->server_version
= ssh_format("SSH-%z-%z %z\r\n",
= ssh_format("SSH-%lz-%lz %lz",
PROTOCOL_VERSION,
SOFTWARE_SERVER_VERSION,
closure->id_comment);
/* Copies the version string, so that it is isn't freed */
return A_WRITE(write, ssh_format("%lS", connection->server_version));
return A_WRITE(write, ssh_format("%lS\r\n", connection->server_version));
}
struct server_line_handler
......@@ -43,25 +45,32 @@ struct server_line_handler
struct ssh_connection *connection;
};
static struct read_handler *do_line(struct server_line_handler *closure,
static struct read_handler *do_line(struct line_handler **h,
UINT32 length,
UINT8 *line)
{
struct server_line_handler *closure = (struct server_line_handler *) *h;
if ( (length >= 4) && !memcmp(line, "SSH-", 4))
{
/* Parse and remember format string */
if ((length >= 8) && !memcmp(line + 4, "2.0-", 4))
{
struct read_handler *new
= make_read_packet(make_debug_processor(make_packet_void(),
stderr),
= make_read_packet(make_packet_debug(make_packet_void(),
stderr),
closure->connection->max_packet);
closure->connection->client_version
= ssh_format("%s", length, line);
= ssh_format("%ls", length, line);
verbose("Client version: ");
verbose_safe(closure->connection->client_version->length,
closure->connection->client_version->data);
verbose("\n");
/* FIXME: Cleanup properly. */
free(closure);
lsh_free(closure);
return new;
}
......@@ -71,8 +80,10 @@ static struct read_handler *do_line(struct server_line_handler *closure,
werror_safe(length, line);
werror("\n");
fatal("server.c: do_line: Unsupported version.\n");
/* FIXME: What could be returned here? */
/* FIXME: Clean up properly */
lsh_free(closure);
*h = 0;
return 0;
}
}
......@@ -85,3 +96,46 @@ static struct read_handler *do_line(struct server_line_handler *closure,
return 0;
}
}
struct read_handler *make_server_read_line(struct ssh_connection *s)
{
struct server_line_handler *closure
= xalloc(sizeof(struct server_line_handler));
closure->super.handler = do_line;
closure->connection = s;
return make_read_line(&closure->super);
}
struct fd_callback *make_server_callback(struct io_backend *b,
char *comment,
UINT32 block_size)
{
struct server_callback *connected = xalloc(sizeof(struct server_callback));
connected->super.f = server_initiate;
connected->backend = b;
connected->block_size = block_size;
connected->id_comment = comment;
return &connected->super;
}
static int server_die(struct callback *closure)
{
werror("Connection died.\n");
/* FIXME: Cleanup properly. */
return 0; /* Ignored */
}
struct callback *make_server_close_handler()
{
struct callback *c = xalloc(sizeof(struct callback));
c->f = server_die;
return c;
}
......@@ -5,10 +5,10 @@
#include "unpad.h"
#include "xalloc.h"
static int do_unpad(struct abstract_write **c,
static int do_unpad(struct abstract_write **w,
struct lsh_string *packet)
{
struct unpad_processor *closure = (struct unpad_processor *) *c;
struct packet_unpad *closure = (struct packet_unpad *) *w;
UINT8 padding_length;
UINT32 payload_length;
......@@ -32,16 +32,16 @@ static int do_unpad(struct abstract_write **c,
lsh_string_free(packet);
return apply_processor(closure->c.next, new);
return A_WRITE(closure->super.next, new);
}
struct abstract_write *
make_packet_unpad(struct abstract_write *continuation)
{
struct unpad_processor *closure = xalloc(sizeof(struct unpad_processor));
struct packet_unpad *closure = xalloc(sizeof(struct packet_unpad));
closure->super.super.write = do_unpad;
closure->c.next = continuation;
closure->super.next = continuation;
return &closure->super.super;
}
......@@ -6,12 +6,12 @@
#ifndef LSH_UNPAD_H_INCLUDED
#define LSH_UNPAD_H_INCLUDED
#include "transport.h"
#include "abstract_io.h"
/* Input to the processor is a padded payload. */
struct packet_unpad
{
struct abstract_write_pipe c;
struct abstract_write_pipe super;
};
struct abstract_write *
......
......@@ -112,7 +112,7 @@ int write_buffer_pre_write(struct write_buffer *buffer)
buffer->head->prev = 0;
else
buffer->tail = 0;
free(n);
lsh_free(n);
}
else
break;
......
......@@ -5,9 +5,45 @@
#include "xalloc.h"
#include "werror.h"
#ifdef DEBUG_ALLOC
void *debug_malloc(size_t size)
{
static int count = 4711;
int *res;
/* Count size in ints, and round up */
size = (size + sizeof(int)-1) / sizeof(int);
res = malloc((size + 3)*sizeof(int));
if (!res)
return NULL;
res[0] = count;
res[1] = size;
res[size+2] = ~count;
count++;
return (void *) (res + 2);
}
void debug_free(void *m)
{
int *p = (int *) m;
size_t size = p[-1];
if (~p[-2] != p[size])
fatal("Memory currupted!\n");
free(p-2);
}
#endif
void *xalloc(size_t size)
{
void *res = malloc(size);
void *res = lsh_malloc(size);
if (!res)
fatal("Virtual memory exhausted");
return res;
......@@ -23,5 +59,5 @@ struct lsh_string *lsh_string_alloc(UINT32 length)
void lsh_string_free(struct lsh_string *packet)
{
free(packet);
lsh_free(packet);
}
......@@ -28,4 +28,15 @@ void *xalloc(size_t size);
struct lsh_string *lsh_string_alloc(UINT32 size);
void lsh_string_free(struct lsh_string *packet);
#ifdef DEBUG_ALLOC
void *debug_malloc(size_t size);
void debug_free(void *m);
#define lsh_free debug_free
#define lsh_malloc debug_malloc
#else
#define lsh_free free
#define lsh_malloc malloc
#endif
#endif /* LSH_XALLOC_H_INCLUDED */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment