summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2012-02-28 22:40:53 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2012-02-28 22:40:53 +0100
commit6390442c477b603d87680f8b415c1ec0dfaa9ebf (patch)
treec5f6c6a5415add9548ad0c17ce051bcaa3b8eb15
parent35742ca05a8826541207faaa9584708da5485d95 (diff)
downloadfastd-6390442c477b603d87680f8b415c1ec0dfaa9ebf.tar
fastd-6390442c477b603d87680f8b415c1ec0dfaa9ebf.zip
Rework buffer structure
-rw-r--r--src/fastd.c22
-rw-r--r--src/fastd.h15
-rw-r--r--src/handshake.c8
-rw-r--r--src/method_null.c3
4 files changed, 22 insertions, 26 deletions
diff --git a/src/fastd.c b/src/fastd.c
index 3594ffd..d346da4 100644
--- a/src/fastd.c
+++ b/src/fastd.c
@@ -237,8 +237,6 @@ static void init_peers(fastd_context *ctx) {
}
}
-
-
static void* get_source_address(const fastd_context *ctx, void *buffer) {
switch (ctx->conf->protocol) {
case PROTOCOL_ETHERNET:
@@ -280,7 +278,7 @@ static void handle_tasks(fastd_context *ctx) {
struct iovec iov[2] = {
{ .iov_base = &task->send.packet_type, .iov_len = 1 },
- { .iov_base = task->send.buffer.base, .iov_len = task->send.buffer.len }
+ { .iov_base = task->send.buffer.data, .iov_len = task->send.buffer.len }
};
msg.msg_iov = iov;
@@ -294,7 +292,7 @@ static void handle_tasks(fastd_context *ctx) {
case TASK_HANDLE_RECV:
// TODO Handle source address
- write(ctx->tunfd, task->handle_recv.buffer.base, task->handle_recv.buffer.len);
+ write(ctx->tunfd, task->handle_recv.buffer.data, task->handle_recv.buffer.len);
fastd_buffer_free(task->handle_recv.buffer);
break;
@@ -329,14 +327,14 @@ static void handle_input(fastd_context *ctx) {
if (fds[0].revents & POLLIN) {
size_t max_len = fastd_max_packet_size(ctx);
- fastd_buffer buffer = fastd_buffer_alloc(max_len, 0);
+ fastd_buffer buffer = fastd_buffer_alloc(max_len, 0, 0);
- ssize_t len = read(ctx->tunfd, buffer.base, max_len);
+ ssize_t len = read(ctx->tunfd, buffer.data, max_len);
if (len < 0)
exit_errno(ctx, "read");
- uint8_t *src_addr = get_source_address(ctx, buffer.base);
- uint8_t *dest_addr = get_dest_address(ctx, buffer.base);
+ uint8_t *src_addr = get_source_address(ctx, buffer.data);
+ uint8_t *dest_addr = get_dest_address(ctx, buffer.data);
pr_debug(ctx, "A packet with length %u is to be sent from %02x:%02x:%02x:%02x:%02x:%02x to %02x:%02x:%02x:%02x:%02x:%02x",
(unsigned)len, src_addr[0], src_addr[1], src_addr[2], src_addr[3], src_addr[4], src_addr[5],
@@ -347,8 +345,8 @@ static void handle_input(fastd_context *ctx) {
fastd_peer *peer;
for (peer = ctx->peers; peer; peer = peer->next) {
if (peer->state == STATE_ESTABLISHED) {
- fastd_buffer send_buffer = fastd_buffer_alloc(len, 0);
- memcpy(send_buffer.base, buffer.base, len);
+ fastd_buffer send_buffer = fastd_buffer_alloc(len, 0, 0);
+ memcpy(send_buffer.data, buffer.data, len);
ctx->conf->method->method_send(ctx, peer, send_buffer);
}
}
@@ -357,13 +355,13 @@ static void handle_input(fastd_context *ctx) {
}
if (fds[1].revents & POLLIN) {
size_t max_len = ctx->conf->method->method_max_packet_size(ctx);
- fastd_buffer buffer = fastd_buffer_alloc(max_len, 0);
+ fastd_buffer buffer = fastd_buffer_alloc(max_len, 0, 0);
uint8_t packet_type;
struct iovec iov[2] = {
{ .iov_base = &packet_type, .iov_len = 1 },
- { .iov_base = buffer.base, .iov_len = max_len }
+ { .iov_base = buffer.data, .iov_len = max_len }
};
struct sockaddr_in recvaddr;
diff --git a/src/fastd.h b/src/fastd.h
index 59fdf90..90df868 100644
--- a/src/fastd.h
+++ b/src/fastd.h
@@ -50,10 +50,10 @@ typedef enum _fastd_loglevel {
typedef struct _fastd_buffer {
void *base;
- size_t len;
+ size_t base_len;
- void (*free)(void *free_p);
- void *free_p;
+ void *data;
+ size_t len;
} fastd_buffer;
typedef enum _fastd_protocol {
@@ -142,15 +142,14 @@ struct _fastd_context {
#define exit_errno(context, message) exit_error(context, "%s: %s", message, strerror(errno))
-static inline fastd_buffer fastd_buffer_alloc(size_t len, size_t head_space) {
+static inline fastd_buffer fastd_buffer_alloc(size_t len, size_t head_space, size_t tail_space) {
+ size_t base_len = head_space+len+tail_space;
uint8_t *ptr = malloc(head_space+len);
- return (fastd_buffer){ .base = ptr, .len = len, .free = free, .free_p = ptr+head_space };
+ return (fastd_buffer){ .base = ptr, .base_len = base_len, .data = ptr+head_space, .len = len };
}
static inline void fastd_buffer_free(fastd_buffer buffer) {
- if (buffer.free) {
- buffer.free(buffer.free_p);
- }
+ free(buffer.base);
}
diff --git a/src/handshake.c b/src/handshake.c
index 350dc5e..59ebd72 100644
--- a/src/handshake.c
+++ b/src/handshake.c
@@ -35,8 +35,8 @@
void fastd_handshake_send(fastd_context *ctx, fastd_peer *peer) {
size_t method_len = strlen(ctx->conf->method->name);
size_t len = sizeof(fastd_packet_request)+method_len;
- fastd_buffer buffer = fastd_buffer_alloc(len, 0);
- fastd_packet_request *request = buffer.base;
+ fastd_buffer buffer = fastd_buffer_alloc(len, 0, 0);
+ fastd_packet_request *request = buffer.data;
request->reply = 0;
request->cp = 0;
@@ -73,8 +73,8 @@ void fastd_handshake_handle(fastd_context *ctx, fastd_peer *peer, fastd_buffer b
strncmp(packet->request.method_name, ctx->conf->method->name, packet->request.method_len))
goto end_free; // TODO
- fastd_buffer reply_buffer = fastd_buffer_alloc(sizeof(fastd_packet_reply), 0);
- fastd_packet_reply *reply = reply_buffer.base;
+ fastd_buffer reply_buffer = fastd_buffer_alloc(sizeof(fastd_packet_reply), 0, 0);
+ fastd_packet_reply *reply = reply_buffer.data;
reply->reply = 1;
reply->cp = 0;
diff --git a/src/method_null.c b/src/method_null.c
index 75e74de..60e612d 100644
--- a/src/method_null.c
+++ b/src/method_null.c
@@ -33,8 +33,7 @@ static size_t null_max_packet_size(fastd_context *ctx) {
}
static void null_init(fastd_context *ctx, fastd_peer *peer) {
- fastd_buffer buffer = { .base = NULL, .len = 0, .free = NULL, .free_p = NULL };
- fastd_task_put_send(ctx, peer, buffer);
+ fastd_task_put_send(ctx, peer, fastd_buffer_alloc(0, 0, 0));
}
static void null_handle_recv(fastd_context *ctx, fastd_peer *peer, fastd_buffer buffer) {