diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2012-02-29 22:44:40 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2012-02-29 22:44:40 +0100 |
commit | 0f2e170ea6edee4df0f90a3821d5c054db6bd6b7 (patch) | |
tree | 6d90aea5c094eec471e6aa2f25f9f080741d1320 /src/fastd.c | |
parent | b5892c34186707a50b2afb713e13cbfc3e859920 (diff) | |
download | fastd-0f2e170ea6edee4df0f90a3821d5c054db6bd6b7.tar fastd-0f2e170ea6edee4df0f90a3821d5c054db6bd6b7.zip |
Only send packets to the right host in ethernet mode
Diffstat (limited to 'src/fastd.c')
-rw-r--r-- | src/fastd.c | 80 |
1 files changed, 34 insertions, 46 deletions
diff --git a/src/fastd.c b/src/fastd.c index 9161239..53b8488 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -27,6 +27,7 @@ #include "fastd.h" #include "handshake.h" +#include "peer.h" #include "task.h" #include <arpa/inet.h> @@ -234,43 +235,12 @@ static void init_peers(fastd_context *ctx) { fastd_peer **current_peer = &ctx->peers; fastd_peer_config *peer_conf; for (peer_conf = ctx->conf->peers; peer_conf; peer_conf = peer_conf->next) { - *current_peer = malloc(sizeof(fastd_peer)); - (*current_peer)->next = NULL; - (*current_peer)->config = peer_conf; - (*current_peer)->address = peer_conf->address; - (*current_peer)->port = peer_conf->port; - (*current_peer)->state = STATE_WAIT; - (*current_peer)->last_req_id = 0; - (*current_peer)->addresses = NULL; - - fastd_task_schedule_handshake(ctx, *current_peer, 0); + *current_peer = fastd_peer_init(ctx, peer_conf); current_peer = &(*current_peer)->next; } } -static void* get_source_address(const fastd_context *ctx, void *buffer) { - switch (ctx->conf->protocol) { - case PROTOCOL_ETHERNET: - return &((struct ethhdr*)buffer)->h_source; - case PROTOCOL_IP: - return NULL; - default: - exit_bug(ctx, "invalid protocol"); - } -} - -static void* get_dest_address(const fastd_context *ctx, void *buffer) { - switch (ctx->conf->protocol) { - case PROTOCOL_ETHERNET: - return &((struct ethhdr*)buffer)->h_dest; - case PROTOCOL_IP: - return NULL; - default: - exit_bug(ctx, "invalid protocol"); - } -} - static void handle_tasks(fastd_context *ctx) { fastd_task *task; while ((task = fastd_task_get(ctx)) != NULL) { @@ -303,7 +273,13 @@ static void handle_tasks(fastd_context *ctx) { break; case TASK_HANDLE_RECV: - // TODO Handle source address + if (ctx->conf->protocol == PROTOCOL_ETHERNET) { + const fastd_eth_addr *src_addr = fastd_get_source_address(ctx, task->handle_recv.buffer); + + if (fastd_eth_addr_is_unicast(src_addr)) + fastd_peer_add_eth_addr(ctx, task->handle_recv.peer, src_addr); + } + write(ctx->tunfd, task->handle_recv.buffer.data, task->handle_recv.buffer.len); fastd_buffer_free(task->handle_recv.buffer); break; @@ -345,25 +321,37 @@ static void handle_input(fastd_context *ctx) { if (len < 0) exit_errno(ctx, "read"); - /*uint8_t *src_addr = get_source_address(ctx, buffer.data); - uint8_t *dest_addr = get_dest_address(ctx, buffer.data); + fastd_peer *peer = NULL; - 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], - dest_addr[0], dest_addr[1], dest_addr[2], dest_addr[3], dest_addr[4], dest_addr[5]);*/ + if (ctx->conf->protocol == PROTOCOL_ETHERNET) { + const fastd_eth_addr *dest_addr = fastd_get_dest_address(ctx, buffer); + if (fastd_eth_addr_is_unicast(dest_addr)) { + peer = fastd_peer_find_by_eth_addr(ctx, dest_addr); - // TODO find correct peer + if (peer == NULL) { + fastd_buffer_free(buffer); + return; + } - 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, 0); - memcpy(send_buffer.data, buffer.data, len); - ctx->conf->method->method_send(ctx, peer, send_buffer); + if (peer->state == STATE_ESTABLISHED) { + ctx->conf->method->method_send(ctx, peer, buffer); + } + else { + fastd_buffer_free(buffer); + } } } + if (peer == NULL) { + for (peer = ctx->peers; peer; peer = peer->next) { + if (peer->state == STATE_ESTABLISHED) { + 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); + } + } - fastd_buffer_free(buffer); + fastd_buffer_free(buffer); + } } if (fds[1].revents & POLLIN) { size_t max_len = ctx->conf->method->method_max_packet_size(ctx); |