commit 71eff4394b01cf19c08b7d504574d425199bcd71 Author: George Kadianakis desnacked@gmail.com Date: Thu Mar 17 16:41:47 2011 +0100
* Abstracted the protocol interface more by adding proto_<operation>() functions. --- src/network.c | 17 ++++++++--------- src/protocol.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- src/protocol.h | 15 ++++++++++++--- 3 files changed, 66 insertions(+), 14 deletions(-)
diff --git a/src/network.c b/src/network.c index b6dadd7..9350a61 100644 --- a/src/network.c +++ b/src/network.c @@ -122,10 +122,9 @@ simple_listener_cb(struct evconnlistener *evcl, conn->mode = lsn->mode; conn->proto = lsn->proto;
- /* ASN Is this actually modular. Will all protocols need to init here? - I don't think so. I don't know. */ + /* Will all protocols need to _init() here? Don't think so! */ int is_initiator = (conn->mode != LSN_SIMPLE_SERVER) ? 1 : 0; - conn->proto->state = lsn->proto->init(&is_initiator); + conn->proto->state = proto_init(conn->proto, &is_initiator);
if (!conn->proto->state) goto err; @@ -178,9 +177,9 @@ simple_listener_cb(struct evconnlistener *evcl, struct bufferevent *encrypted = conn->mode == LSN_SIMPLE_SERVER ? conn->input : conn->output;
- /* ASN Send handshake */ - if (lsn->proto->handshake(conn->proto->state, - bufferevent_get_output(encrypted))<0) + /* ASN Will all protocols need to handshake here? Don't think so. */ + if (proto_handshake(conn->proto, + bufferevent_get_output(encrypted))<0) goto err;
if (conn->mode == LSN_SIMPLE_SERVER || conn->mode == LSN_SIMPLE_CLIENT) { @@ -205,7 +204,7 @@ static void conn_free(conn_t *conn) { if (conn->proto->state) - conn->proto->destroy((void *)conn->proto->state); + proto_destroy(conn->proto->state); if (conn->socks_state) socks_state_free(conn->socks_state); if (conn->input) @@ -280,7 +279,7 @@ plaintext_read_cb(struct bufferevent *bev, void *arg) other = (bev == conn->input) ? conn->output : conn->input;
dbg(("Got data on plaintext side\n")); - if (conn->proto->send(conn->proto->state, + if (proto_send(conn->proto, bufferevent_get_input(bev), bufferevent_get_output(other)) < 0) conn_free(conn); @@ -294,7 +293,7 @@ obfsucated_read_cb(struct bufferevent *bev, void *arg) other = (bev == conn->input) ? conn->output : conn->input;
dbg(("Got data on encrypted side\n")); - if (conn->proto->recv(conn->proto->state, + if (proto_recv(conn->proto, bufferevent_get_input(bev), bufferevent_get_output(other)) < 0) conn_free(conn); diff --git a/src/protocol.c b/src/protocol.c index c186857..9fda4b0 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -1,5 +1,6 @@ -#include "stdlib.h" -#include "stdio.h" +#include <stdlib.h> +#include <stdio.h> +#include <assert.h>
#include "protocol.h" #include "crypt.h" @@ -29,3 +30,46 @@ set_up_protocol(int protocol) {
return proto; } + +void * +proto_init(struct protocol_t *proto, void *arg) { + assert(proto); + if (proto->init) + return proto->init(arg); + else + return NULL; +} + +int +proto_handshake(struct protocol_t *proto, void *buf) { + assert(proto); + if (proto->handshake) + return proto->handshake(proto->state, buf); + else + return -1; +} + +int +proto_send(struct protocol_t *proto, void *source, void *dest) { + assert(proto); + if (proto->send) + return proto->send(proto->state, source, dest); + else + return -1; +} + +int +proto_recv(struct protocol_t *proto, void *source, void *dest) { + assert(proto); + if (proto->recv) + return proto->recv(proto->state, source, dest); + else + return -1; +} + +void proto_destroy(struct protocol_t *proto) { + assert(proto); + + if (proto->destroy) + proto->destroy(proto->state); +} diff --git a/src/protocol.h b/src/protocol.h index 159f3e3..4de5c70 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -5,22 +5,31 @@ #define BRL_PROTOCOL 1
struct protocol_t *set_up_protocol(int protocol); +void *proto_init(struct protocol_t *proto, void *arg); +void proto_destroy(struct protocol_t *proto); +int proto_handshake(struct protocol_t *proto, void *buf); +int proto_send(struct protocol_t *proto, void *source, void *dest); +int proto_recv(struct protocol_t *proto, void *source, void *dest);
-/* ASN */ + + +/* ASN Why the hell do half of them return int? FIXME */ struct protocol_t { /* Constructor: creates the protocol; sets up functions etc. */ void *(*new)(struct protocol_t *self); /* Destructor */ - void (*destroy)(void *arg); + void (*destroy)(void *state);
/* does nessesary initiation steps; like build a proto state etc. */ void *(*init)(void *arg);
/* does handshake. Supposedly all protocols have a handshake. */ - void *(*handshake)(void *state, void *buf); + int (*handshake)(void *state, void *buf); + /* send data function */ int (*send)(void *state, void *source, void *dest); + /* receive data function */ int (*recv)(void *state, void *source, void *dest);
tor-commits@lists.torproject.org