From 2e14d72936d1ccfb6faed4a20dec7072fb6d8232 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 19 Apr 2014 19:07:18 +0200 Subject: Make eth_addrs a vector --- src/fastd.c | 9 ++++++--- src/fastd.h | 5 ++--- src/peer.c | 44 +++++++++++++++----------------------------- 3 files changed, 23 insertions(+), 35 deletions(-) diff --git a/src/fastd.c b/src/fastd.c index a226203..7459d6d 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -379,8 +379,8 @@ static void dump_state(fastd_context_t *ctx) { if (ctx->conf->mode == MODE_TAP) { unsigned int eth_addresses = 0; size_t i; - for (i = 0; i < ctx->n_eth_addr; i++) { - if (ctx->eth_addr[i].peer == peer) + for (i = 0; i < VECTOR_LEN(ctx->eth_addrs); i++) { + if (VECTOR_INDEX(ctx->eth_addrs, i).peer == peer) eth_addresses++; } @@ -965,6 +965,8 @@ int main(int argc, char *argv[]) { set_user(&ctx); fastd_config_load_peer_dirs(&ctx, &conf); + + VECTOR_ALLOC(ctx.eth_addrs, 0); init_peers(&ctx); while (!terminate) { @@ -1011,8 +1013,9 @@ int main(int argc, char *argv[]) { on_post_down(&ctx); + VECTOR_FREE(ctx.eth_addrs); + free(ctx.protocol_state); - free(ctx.eth_addr); free(ctx.ifname); #ifdef ENABLE_OPENSSL diff --git a/src/fastd.h b/src/fastd.h index c2b85de..b7f3532 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -31,6 +31,7 @@ #include "buffer.h" #include "log.h" #include "shell.h" +#include "vector.h" #include #include @@ -261,9 +262,7 @@ struct fastd_context { fastd_stats_t tx_dropped; fastd_stats_t tx_error; - size_t eth_addr_size; - size_t n_eth_addr; - fastd_peer_eth_addr_t *eth_addr; + VECTOR(fastd_peer_eth_addr_t) eth_addrs; unsigned int randseed; diff --git a/src/peer.c b/src/peer.c index 42268b7..1de0911 100644 --- a/src/peer.c +++ b/src/peer.c @@ -154,16 +154,16 @@ static void reset_peer(fastd_context_t *ctx, fastd_peer_t *peer) { ctx->conf->protocol->reset_peer_state(ctx, peer); size_t i, deleted = 0; - for (i = 0; i < ctx->n_eth_addr; i++) { - if (ctx->eth_addr[i].peer == peer) { + for (i = 0; i < VECTOR_LEN(ctx->eth_addrs); i++) { + if (VECTOR_INDEX(ctx->eth_addrs, i).peer == peer) { deleted++; } else if (deleted) { - ctx->eth_addr[i-deleted] = ctx->eth_addr[i]; + VECTOR_INDEX(ctx->eth_addrs, i-deleted) = VECTOR_INDEX(ctx->eth_addrs, i); } } - ctx->n_eth_addr -= deleted; + VECTOR_RESIZE(ctx->eth_addrs, VECTOR_LEN(ctx->eth_addrs)-deleted); fastd_peer_unschedule_handshake(ctx, peer); } @@ -658,7 +658,7 @@ static inline int fastd_eth_addr_cmp(const fastd_eth_addr_t *addr1, const fastd_ } static inline fastd_peer_eth_addr_t* peer_get_by_addr(fastd_context_t *ctx, fastd_eth_addr_t addr) { - fastd_eth_addr_t *ret = bsearch(&addr, &ctx->eth_addr[0].addr, ctx->n_eth_addr, sizeof(fastd_peer_eth_addr_t), + fastd_eth_addr_t *ret = bsearch(&addr, &VECTOR_INDEX(ctx->eth_addrs, 0).addr, VECTOR_LEN(ctx->eth_addrs), sizeof(fastd_peer_eth_addr_t), (int (*)(const void *, const void *))fastd_eth_addr_cmp); if (ret) @@ -668,18 +668,18 @@ static inline fastd_peer_eth_addr_t* peer_get_by_addr(fastd_context_t *ctx, fast } void fastd_peer_eth_addr_add(fastd_context_t *ctx, fastd_peer_t *peer, fastd_eth_addr_t addr) { - int min = 0, max = ctx->n_eth_addr; + int min = 0, max = VECTOR_LEN(ctx->eth_addrs); if (!fastd_peer_is_established(peer)) exit_bug(ctx, "tried to learn ethernet address on non-established peer"); while (max > min) { int cur = (min+max)/2; - int cmp = fastd_eth_addr_cmp(&addr, &ctx->eth_addr[cur].addr); + int cmp = fastd_eth_addr_cmp(&addr, &VECTOR_INDEX(ctx->eth_addrs, cur).addr); if (cmp == 0) { - ctx->eth_addr[cur].peer = peer; - ctx->eth_addr[cur].timeout = fastd_in_seconds(ctx, ctx->conf->eth_addr_stale_time); + VECTOR_INDEX(ctx->eth_addrs, cur).peer = peer; + VECTOR_INDEX(ctx->eth_addrs, cur).timeout = fastd_in_seconds(ctx, ctx->conf->eth_addr_stale_time); return; /* We're done here. */ } else if (cmp < 0) { @@ -690,21 +690,7 @@ void fastd_peer_eth_addr_add(fastd_context_t *ctx, fastd_peer_t *peer, fastd_eth } } - ctx->n_eth_addr++; - if (ctx->n_eth_addr > ctx->eth_addr_size) { - if (ctx->eth_addr_size == 0) - ctx->eth_addr_size = 16; - else - ctx->eth_addr_size *= 2; - - ctx->eth_addr = realloc(ctx->eth_addr, ctx->eth_addr_size*sizeof(fastd_peer_eth_addr_t)); - } - - int i; - for (i = ctx->n_eth_addr-1; i > min; i--) - ctx->eth_addr[i] = ctx->eth_addr[i-1]; - - ctx->eth_addr[min] = (fastd_peer_eth_addr_t){ addr, peer, fastd_in_seconds(ctx, ctx->conf->eth_addr_stale_time) }; + VECTOR_INSERT(ctx->eth_addrs, ((fastd_peer_eth_addr_t) {addr, peer, fastd_in_seconds(ctx, ctx->conf->eth_addr_stale_time)}), min); pr_debug(ctx, "learned new MAC address %E on peer %P", &addr, peer); } @@ -712,18 +698,18 @@ void fastd_peer_eth_addr_add(fastd_context_t *ctx, fastd_peer_t *peer, fastd_eth void fastd_peer_eth_addr_cleanup(fastd_context_t *ctx) { size_t i, deleted = 0; - for (i = 0; i < ctx->n_eth_addr; i++) { - if (fastd_timed_out(ctx, &ctx->eth_addr[i].timeout)) { + for (i = 0; i < VECTOR_LEN(ctx->eth_addrs); i++) { + if (fastd_timed_out(ctx, &VECTOR_INDEX(ctx->eth_addrs, i).timeout)) { deleted++; pr_debug(ctx, "MAC address %E not seen for more than %u seconds, removing", - &ctx->eth_addr[i].addr, ctx->conf->eth_addr_stale_time); + &VECTOR_INDEX(ctx->eth_addrs, i).addr, ctx->conf->eth_addr_stale_time); } else if (deleted) { - ctx->eth_addr[i-deleted] = ctx->eth_addr[i]; + VECTOR_INDEX(ctx->eth_addrs, i-deleted) = VECTOR_INDEX(ctx->eth_addrs, i); } } - ctx->n_eth_addr -= deleted; + VECTOR_RESIZE(ctx->eth_addrs, VECTOR_LEN(ctx->eth_addrs)-deleted); } fastd_peer_t* fastd_peer_find_by_eth_addr(fastd_context_t *ctx, const fastd_eth_addr_t addr) { -- cgit v1.2.3