diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/announce.c | 48 | ||||
-rw-r--r-- | src/babel.c | 80 | ||||
-rw-r--r-- | src/babel.h | 48 | ||||
-rw-r--r-- | src/neigh.c | 18 | ||||
-rw-r--r-- | src/neigh.h | 12 | ||||
-rw-r--r-- | src/send.c | 60 | ||||
-rw-r--r-- | src/tlv.c | 4 | ||||
-rw-r--r-- | src/tlv.h | 4 | ||||
-rw-r--r-- | src/tlv_types.c | 100 | ||||
-rw-r--r-- | src/types.h | 1 |
10 files changed, 183 insertions, 192 deletions
diff --git a/src/announce.c b/src/announce.c index 4d55f42..9deee18 100644 --- a/src/announce.c +++ b/src/announce.c @@ -30,7 +30,7 @@ #include <stdlib.h> -gp_babel_announce_t* gp_babel_announce_new(gmrf_t *gmrf, gmrf_context_t *ctx) { +gp_babel_announce_t* gp_babel_announce_new(gmrf_context_t *ctx) { gp_babel_announce_t *a = calloc(1, sizeof(gp_babel_announce_t)); a->metric.metric = a->feasibility_distance.metric = a->last_metric = GP_BABEL_INFINITY; @@ -41,7 +41,7 @@ gp_babel_announce_t* gp_babel_announce_new(gmrf_t *gmrf, gmrf_context_t *ctx) { return a; } -gp_babel_announce_t* gp_babel_announce_find(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_node_id_t *node, uint16_t type, uint16_t key) { +gp_babel_announce_t* gp_babel_announce_find(gmrf_context_t *ctx, const gp_babel_node_id_t *node, uint16_t type, uint16_t key) { gp_babel_announce_t *announce; for (announce = ctx->announces; announce; announce = announce->next) { if (gp_babel_node_id_equal(&announce->node, node) @@ -53,11 +53,11 @@ gp_babel_announce_t* gp_babel_announce_find(gmrf_t *gmrf, gmrf_context_t *ctx, c return NULL; } -gp_babel_announce_t* gp_babel_announce_get(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_node_id_t *node, uint16_t type, uint16_t key) { - gp_babel_announce_t *announce = gp_babel_announce_find(gmrf, ctx, node, type, key); +gp_babel_announce_t* gp_babel_announce_get(gmrf_context_t *ctx, const gp_babel_node_id_t *node, uint16_t type, uint16_t key) { + gp_babel_announce_t *announce = gp_babel_announce_find(ctx, node, type, key); if (!announce) { - announce = gp_babel_announce_new(gmrf, ctx); + announce = gp_babel_announce_new(ctx); announce->node = *node; announce->type = type; announce->key = key; @@ -66,7 +66,7 @@ gp_babel_announce_t* gp_babel_announce_get(gmrf_t *gmrf, gmrf_context_t *ctx, co return announce; } -void gp_babel_announce_free(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_announce_t *announce) { +void gp_babel_announce_free(gmrf_context_t *ctx, gp_babel_announce_t *announce) { free(announce); } @@ -92,7 +92,7 @@ gp_babel_nexthop_t* gp_babel_announce_nexthop_new(gp_babel_announce_t *announce, return nexthop; } -static void maintain_nexthops(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_announce_t *announce) { +static void maintain_nexthops(gmrf_context_t *ctx, gp_babel_announce_t *announce) { gp_babel_nexthop_t **cur, **next; for (cur = &announce->nexthops; *cur; cur = next) { gp_babel_nexthop_t *nexthop = *cur; @@ -104,14 +104,14 @@ static void maintain_nexthops(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_announ if (!nexthop->neigh->iface) { if (nexthop->metric_seqno.metric != GP_BABEL_INFINITY) { nexthop->metric_seqno.metric = GP_BABEL_INFINITY; - nexthop->last_update = gmrf_now(gmrf); + nexthop->last_update = gmrf_now(ctx->gmrf); nexthop->last_update += GP_BABEL_UPDATE_TIMEOUT(nexthop->interval)*10; } continue; } - if (gmrf_now(gmrf) > nexthop->last_update+GP_BABEL_UPDATE_TIMEOUT(nexthop->interval)*10) { + if (gmrf_now(ctx->gmrf) > nexthop->last_update+GP_BABEL_UPDATE_TIMEOUT(nexthop->interval)*10) { if (nexthop->metric_seqno.metric == GP_BABEL_INFINITY) { *cur = *next; next = cur; @@ -128,17 +128,17 @@ static void maintain_nexthops(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_announ nexthop->last_update += GP_BABEL_UPDATE_TIMEOUT(nexthop->interval)*10; } } - else if (gmrf_now(gmrf) > nexthop->last_update+GP_BABEL_UPDATE_REQUEST_TIMEOUT(nexthop->interval)*10 && announce->selected == nexthop) { + else if (gmrf_now(ctx->gmrf) > nexthop->last_update+GP_BABEL_UPDATE_REQUEST_TIMEOUT(nexthop->interval)*10 && announce->selected == nexthop) { if (!nexthop->requested_update) { - gmrf_logf(gmrf, LOG_INFO, "announce about to expire, requesting update"); - gp_babel_send_announce_request(gmrf, ctx, NULL, nexthop->neigh, &announce->node, announce->type, announce->key, false); + gmrf_logf(ctx->gmrf, LOG_INFO, "announce about to expire, requesting update"); + gp_babel_send_announce_request(ctx, NULL, nexthop->neigh, &announce->node, announce->type, announce->key, false); nexthop->requested_update = true; } } } } -static gp_babel_nexthop_t* select_nexthop(gmrf_t *gmrf, const gp_babel_announce_t *announce) { +static gp_babel_nexthop_t* select_nexthop(gmrf_context_t *ctx, const gp_babel_announce_t *announce) { uint16_t ret_metric = GP_BABEL_INFINITY; gp_babel_nexthop_t *ret = NULL; @@ -150,7 +150,7 @@ static gp_babel_nexthop_t* select_nexthop(gmrf_t *gmrf, const gp_babel_announce_ if (!gp_babel_is_feasible(announce, nexthop->metric_seqno)) continue; - uint32_t metric = nexthop->metric_seqno.metric + gp_babel_neigh_get_cost(gmrf, nexthop->neigh); + uint32_t metric = nexthop->metric_seqno.metric + gp_babel_neigh_get_cost(ctx, nexthop->neigh); if (metric < ret_metric) { ret = nexthop; @@ -161,9 +161,9 @@ static gp_babel_nexthop_t* select_nexthop(gmrf_t *gmrf, const gp_babel_announce_ return ret; } -gp_babel_metric_seqno_t get_metric(gmrf_t *gmrf, const gp_babel_announce_t *announce) { +gp_babel_metric_seqno_t get_metric(gmrf_context_t *ctx, const gp_babel_announce_t *announce) { if (announce->selected) { - uint32_t metric = announce->selected->metric_seqno.metric + gp_babel_neigh_get_cost(gmrf, announce->selected->neigh); + uint32_t metric = announce->selected->metric_seqno.metric + gp_babel_neigh_get_cost(ctx, announce->selected->neigh); if (metric < GP_BABEL_INFINITY) return (gp_babel_metric_seqno_t){metric, announce->selected->metric_seqno.seqno}; @@ -172,14 +172,14 @@ gp_babel_metric_seqno_t get_metric(gmrf_t *gmrf, const gp_babel_announce_t *anno return (gp_babel_metric_seqno_t){GP_BABEL_INFINITY, 0}; } -void gp_babel_announce_update(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_announce_t *announce) { - maintain_nexthops(gmrf, ctx, announce); +void gp_babel_announce_update(gmrf_context_t *ctx, gp_babel_announce_t *announce) { + maintain_nexthops(ctx, announce); - announce->selected = select_nexthop(gmrf, announce); - announce->metric = get_metric(gmrf, announce); + announce->selected = select_nexthop(ctx, announce); + announce->metric = get_metric(ctx, announce); if (!announce->selected) - gp_babel_send_seqno_request_for(gmrf, ctx, NULL, announce); + gp_babel_send_seqno_request_for(ctx, NULL, announce); /* triggered updates */ /*int diff = announce->metric.metric - announce->last_metric; @@ -191,13 +191,13 @@ void gp_babel_announce_update(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_announ } */ } -void gp_babel_announce_update_nexthop(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_announce_t *announce, gp_babel_nexthop_t *nexthop, gp_babel_metric_seqno_t ms, uint16_t interval) { +void gp_babel_announce_update_nexthop(gmrf_context_t *ctx, gp_babel_announce_t *announce, gp_babel_nexthop_t *nexthop, gp_babel_metric_seqno_t ms, uint16_t interval) { nexthop->metric_seqno = ms; nexthop->interval = interval; nexthop->requested_update = false; if (ms.metric != GP_BABEL_INFINITY) - nexthop->last_update = gmrf_now(gmrf); + nexthop->last_update = gmrf_now(ctx->gmrf); - gp_babel_announce_update(gmrf, ctx, announce); + gp_babel_announce_update(ctx, announce); } diff --git a/src/babel.c b/src/babel.c index 1ca4dd4..7cf52b6 100644 --- a/src/babel.c +++ b/src/babel.c @@ -35,32 +35,32 @@ const char *gmrf_protocol_name = "babel"; const char *gmrf_protocol_version = "experimental"; -static void send_hellos(gmrf_t *gmrf, gmrf_context_t *ctx, void *arg) { - gmrf_schedule(gmrf, send_hellos, NULL, GP_BABEL_HELLO_INTERVAL*10); +static void send_hellos(gmrf_context_t *ctx, void *arg) { + gmrf_schedule(ctx->gmrf, send_hellos, NULL, GP_BABEL_HELLO_INTERVAL*10); - gp_babel_send_hellos(gmrf, ctx); + gp_babel_send_hellos(ctx); } -static void send_updates(gmrf_t *gmrf, gmrf_context_t *ctx, void *arg) { - gmrf_schedule(gmrf, send_updates, NULL, GP_BABEL_UPDATE_INTERVAL*10); +static void send_updates(gmrf_context_t *ctx, void *arg) { + gmrf_schedule(ctx->gmrf, send_updates, NULL, GP_BABEL_UPDATE_INTERVAL*10); - gmrf_logf(gmrf, LOG_DEBUG, "sending periodic updates."); + gmrf_logf(ctx->gmrf, LOG_DEBUG, "sending periodic updates."); - gp_babel_iface_t *iface; + gmrf_iface_state_t *iface; for (iface = ctx->interfaces; iface; iface = iface->next) { - gp_babel_send_update(gmrf, ctx, iface, NULL, NULL, false); + gp_babel_send_update(ctx, iface, NULL, NULL, false); } } -static void maintain_neighbours(gmrf_t *gmrf, gmrf_context_t *ctx) { - gp_babel_iface_t *iface; +static void maintain_neighbours(gmrf_context_t *ctx) { + gmrf_iface_state_t *iface; for (iface = ctx->interfaces; iface; iface = iface->next) { gp_babel_neigh_t **cur, **next; for (cur = &iface->neighbours; *cur; cur = next) { gp_babel_neigh_t *neigh = *cur; next = &neigh->next; - if (gp_babel_neigh_get_rxcost(gmrf, neigh) == GP_BABEL_INFINITY && gp_babel_neigh_get_txcost(gmrf, neigh) == GP_BABEL_INFINITY && !neigh->ref) { + if (gp_babel_neigh_get_rxcost(ctx, neigh) == GP_BABEL_INFINITY && gp_babel_neigh_get_txcost(ctx, neigh) == GP_BABEL_INFINITY && !neigh->ref) { *cur = *next; next = cur; free(neigh); @@ -68,32 +68,32 @@ static void maintain_neighbours(gmrf_t *gmrf, gmrf_context_t *ctx) { continue; } - gmrf_logf(gmrf, LOG_DEBUG, "maintenance: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x[%s]: %u (rx %u/tx %u)", + gmrf_logf(ctx->gmrf, LOG_DEBUG, "maintenance: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x[%s]: %u (rx %u/tx %u)", neigh->addr.d[0], neigh->addr.d[1], neigh->addr.d[2], neigh->addr.d[3], neigh->addr.d[4], neigh->addr.d[5], neigh->addr.d[6], neigh->addr.d[7], - gmrf_iface_get_name(gmrf, iface->gmrf_iface), gp_babel_neigh_get_cost(gmrf, neigh), - gp_babel_neigh_get_rxcost(gmrf, neigh), gp_babel_neigh_get_txcost(gmrf, neigh)); + gmrf_iface_get_name(ctx->gmrf, iface->gmrf_iface), gp_babel_neigh_get_cost(ctx, neigh), + gp_babel_neigh_get_rxcost(ctx, neigh), gp_babel_neigh_get_txcost(ctx, neigh)); } } } -static void maintain_announces(gmrf_t *gmrf, gmrf_context_t *ctx) { +static void maintain_announces(gmrf_context_t *ctx) { gp_babel_announce_t **cur, **next; for (cur = &ctx->announces; *cur; cur = next) { gp_babel_announce_t *announce = *cur; next = &announce->next; - gp_babel_announce_update(gmrf, ctx, announce); + gp_babel_announce_update(ctx, announce); if (!announce->nexthops) { *cur = *next; next = cur; - gp_babel_announce_free(gmrf, ctx, announce); + gp_babel_announce_free(ctx, announce); continue; } - gmrf_logf(gmrf, LOG_DEBUG, "node %04x%04x, type %04x, announce %04x (%u, seqno=%04x):", + gmrf_logf(ctx->gmrf, LOG_DEBUG, "node %04x%04x, type %04x, announce %04x (%u, seqno=%04x):", ntohl(*(uint32_t*)announce->node.id), ntohl(*(uint32_t*)(announce->node.id+4)), announce->type, announce->key, announce->metric.metric, announce->metric.seqno); @@ -102,25 +102,25 @@ static void maintain_announces(gmrf_t *gmrf, gmrf_context_t *ctx) { gp_babel_neigh_t *neigh = nexthop->neigh; if (!neigh) { - gmrf_logf(gmrf, LOG_DEBUG, " local"); + gmrf_logf(ctx->gmrf, LOG_DEBUG, " local"); continue; } - gmrf_logf(gmrf, LOG_DEBUG, " nexthop: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x[%s] (%u, seqno=%04x, cost=%u%s)", + gmrf_logf(ctx->gmrf, LOG_DEBUG, " nexthop: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x[%s] (%u, seqno=%04x, cost=%u%s)", neigh->addr.d[0], neigh->addr.d[1], neigh->addr.d[2], neigh->addr.d[3], neigh->addr.d[4], neigh->addr.d[5], neigh->addr.d[6], neigh->addr.d[7], - neigh->iface ? gmrf_iface_get_name(gmrf, neigh->iface->gmrf_iface) : NULL, - nexthop->metric_seqno.metric, nexthop->metric_seqno.seqno, gp_babel_neigh_get_cost(gmrf, neigh), + neigh->iface ? gmrf_iface_get_name(ctx->gmrf, neigh->iface->gmrf_iface) : NULL, + nexthop->metric_seqno.metric, nexthop->metric_seqno.seqno, gp_babel_neigh_get_cost(ctx, neigh), (nexthop == announce->selected) ? ", selected" : ""); } } } -static void maintenance(gmrf_t *gmrf, gmrf_context_t *ctx, void *arg) { - gmrf_schedule(gmrf, maintenance, NULL, GP_BABEL_MAINTENANCE_INTERVAL*10); +static void maintenance(gmrf_context_t *ctx, void *arg) { + gmrf_schedule(ctx->gmrf, maintenance, NULL, GP_BABEL_MAINTENANCE_INTERVAL*10); - maintain_neighbours(gmrf, ctx); - maintain_announces(gmrf, ctx); + maintain_neighbours(ctx); + maintain_announces(ctx); } gmrf_context_t* gmrf_protocol_init(gmrf_t *gmrf) { @@ -131,9 +131,11 @@ gmrf_context_t* gmrf_protocol_init(gmrf_t *gmrf) { gmrf_schedule(gmrf, maintenance, NULL, 0); gmrf_context_t *ctx = calloc(1, sizeof(gmrf_context_t)); + + ctx->gmrf = gmrf; gmrf_random_bytes(gmrf, &ctx->self, sizeof(gp_babel_node_id_t)); - gp_babel_announce_t *announce = gp_babel_announce_new(gmrf, ctx); + gp_babel_announce_t *announce = gp_babel_announce_new(ctx); announce->node = ctx->self; announce->type = 1; announce->key = 1337; @@ -143,27 +145,25 @@ gmrf_context_t* gmrf_protocol_init(gmrf_t *gmrf) { return ctx; } -void gmrf_protocol_add_iface(gmrf_t *gmrf, gmrf_context_t *ctx, gmrf_iface_t *iface) { - gmrf_logf(gmrf, LOG_INFO, "interface `%s' added.", gmrf_iface_get_name(gmrf, iface)); +gmrf_iface_state_t* gmrf_protocol_add_iface(gmrf_context_t *ctx, gmrf_iface_t *iface) { + gmrf_logf(ctx->gmrf, LOG_INFO, "interface `%s' added.", gmrf_iface_get_name(ctx->gmrf, iface)); - gp_babel_iface_t *b_iface = calloc(1, sizeof(gp_babel_iface_t)); - b_iface->gmrf_iface = iface; + gmrf_iface_state_t *state = calloc(1, sizeof(gmrf_iface_state_t)); + state->gmrf_iface = iface; - b_iface->next = ctx->interfaces; - ctx->interfaces = b_iface; + state->next = ctx->interfaces; + ctx->interfaces = state; + + return state; } -void gmrf_protocol_handle_packet(gmrf_t *gmrf, gmrf_context_t *ctx, gmrf_iface_t *iface, const gmrf_addr_t *source, const void *data, size_t len) { +void gmrf_protocol_handle_packet(gmrf_context_t *ctx, gmrf_iface_state_t *iface, const gmrf_addr_t *source, const void *data, size_t len) { const gp_babel_packet_t *packet = data; if (len < sizeof(gp_babel_packet_t) || len < gp_babel_packet_size(packet)) { - gmrf_logf(gmrf, LOG_DEBUG, "received short packet."); + gmrf_logf(ctx->gmrf, LOG_DEBUG, "received short packet."); return; } - gp_babel_iface_t *b_iface = gp_babel_get_iface(ctx, iface); - if (!b_iface) - return; - - gp_babel_handle_packet(gmrf, ctx, b_iface, source, packet); + gp_babel_handle_packet(ctx, iface, source, packet); } diff --git a/src/babel.h b/src/babel.h index b85438b..0d64e9c 100644 --- a/src/babel.h +++ b/src/babel.h @@ -55,16 +55,18 @@ struct gmrf_context { + gmrf_t *gmrf; + gp_babel_node_id_t self; - gp_babel_iface_t *interfaces; + gmrf_iface_state_t *interfaces; gp_babel_neigh_t *neighbours; gp_babel_announce_t *announces; }; -struct gp_babel_iface { - gp_babel_iface_t *next; +struct gmrf_iface_state { + gmrf_iface_state_t *next; gmrf_iface_t *gmrf_iface; @@ -78,7 +80,7 @@ struct gp_babel_neigh { unsigned ref; - gp_babel_iface_t *iface; + gmrf_iface_state_t *iface; gmrf_addr_t addr; uint16_t hello_log; @@ -123,25 +125,25 @@ struct gp_babel_nexthop { }; -void gp_babel_handle_packet(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_iface_t *iface, const gmrf_addr_t *source, const gp_babel_packet_t *packet); +void gp_babel_handle_packet(gmrf_context_t *ctx, gmrf_iface_state_t *iface, const gmrf_addr_t *source, const gp_babel_packet_t *packet); -void gp_babel_send_ack(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_neigh_t *neigh, uint16_t nonce); -void gp_babel_send_hellos(gmrf_t *gmrf, gmrf_context_t *ctx); +void gp_babel_send_ack(gmrf_context_t *ctx, gp_babel_neigh_t *neigh, uint16_t nonce); +void gp_babel_send_hellos(gmrf_context_t *ctx); -void gp_babel_send_update(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_iface_t *iface, gp_babel_neigh_t *neigh, gp_babel_announce_t *announce, bool with_payload); +void gp_babel_send_update(gmrf_context_t *ctx, gmrf_iface_state_t *iface, gp_babel_neigh_t *neigh, gp_babel_announce_t *announce, bool with_payload); -void gp_babel_send_announce_request(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_iface_t *iface, gp_babel_neigh_t *neigh, const gp_babel_node_id_t *node, uint16_t type, uint16_t key, bool with_payload); -void gp_babel_send_seqno_request(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_neigh_t *neigh, gp_babel_announce_t *announce, uint16_t seqno, uint8_t hop_count); +void gp_babel_send_announce_request(gmrf_context_t *ctx, gmrf_iface_state_t *iface, gp_babel_neigh_t *neigh, const gp_babel_node_id_t *node, uint16_t type, uint16_t key, bool with_payload); +void gp_babel_send_seqno_request(gmrf_context_t *ctx, gp_babel_neigh_t *neigh, gp_babel_announce_t *announce, uint16_t seqno, uint8_t hop_count); -gp_babel_announce_t* gp_babel_announce_new(gmrf_t *gmrf, gmrf_context_t *ctx); -gp_babel_announce_t* gp_babel_announce_find(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_node_id_t *node, uint16_t type, uint16_t key); -gp_babel_announce_t* gp_babel_announce_get(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_node_id_t *node, uint16_t type, uint16_t key); -void gp_babel_announce_update(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_announce_t *announce); -void gp_babel_announce_free(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_announce_t *announce); +gp_babel_announce_t* gp_babel_announce_new(gmrf_context_t *ctx); +gp_babel_announce_t* gp_babel_announce_find(gmrf_context_t *ctx, const gp_babel_node_id_t *node, uint16_t type, uint16_t key); +gp_babel_announce_t* gp_babel_announce_get(gmrf_context_t *ctx, const gp_babel_node_id_t *node, uint16_t type, uint16_t key); +void gp_babel_announce_update(gmrf_context_t *ctx, gp_babel_announce_t *announce); +void gp_babel_announce_free(gmrf_context_t *ctx, gp_babel_announce_t *announce); gp_babel_nexthop_t* gp_babel_announce_nexthop_new(gp_babel_announce_t *announce, gp_babel_neigh_t *neigh); gp_babel_nexthop_t* gp_babel_announce_nexthop_find(const gp_babel_announce_t *announce, gp_babel_neigh_t *neigh); -void gp_babel_announce_update_nexthop(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_announce_t *announce, gp_babel_nexthop_t *nexthop, gp_babel_metric_seqno_t ms, uint16_t interval); +void gp_babel_announce_update_nexthop(gmrf_context_t *ctx, gp_babel_announce_t *announce, gp_babel_nexthop_t *nexthop, gp_babel_metric_seqno_t ms, uint16_t interval); static inline bool gp_babel_node_id_equal(const gp_babel_node_id_t *id1, const gp_babel_node_id_t *id2) { @@ -152,16 +154,6 @@ static inline bool gp_babel_node_id_is_unspec(const gp_babel_node_id_t *id) { return gp_babel_node_id_equal(id, &gp_babel_node_id_unspec); }; -static inline gp_babel_iface_t* gp_babel_get_iface(gmrf_context_t *ctx, gmrf_iface_t *gmrf_iface) { - gp_babel_iface_t *iface; - for (iface = ctx->interfaces; iface; iface = iface->next) { - if (iface->gmrf_iface == gmrf_iface) - return iface; - } - - return NULL; -} - static inline bool gp_babel_is_metric_better(gp_babel_metric_seqno_t ms1, gp_babel_metric_seqno_t ms2) { if (ms1.metric == GP_BABEL_INFINITY) return false; @@ -186,11 +178,11 @@ static inline bool gp_babel_is_feasible(const gp_babel_announce_t *announce, gp_ return gp_babel_is_metric_better(ms, announce->feasibility_distance); } -static inline void gp_babel_send_seqno_request_for(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_neigh_t *neigh, gp_babel_announce_t *announce) { +static inline void gp_babel_send_seqno_request_for(gmrf_context_t *ctx, gp_babel_neigh_t *neigh, gp_babel_announce_t *announce) { if (announce->feasibility_distance.metric == GP_BABEL_INFINITY) return; - gp_babel_send_seqno_request(gmrf, ctx, neigh, announce, announce->feasibility_distance.seqno+1, GP_BABEL_SEQNO_REQ_HOP_LIMIT); + gp_babel_send_seqno_request(ctx, neigh, announce, announce->feasibility_distance.seqno+1, GP_BABEL_SEQNO_REQ_HOP_LIMIT); } #endif /* _GMRF_PROTO_BABEL_BABEL_H_ */ diff --git a/src/neigh.c b/src/neigh.c index b89e284..252713c 100644 --- a/src/neigh.c +++ b/src/neigh.c @@ -29,7 +29,7 @@ #include <stdlib.h> -gp_babel_neigh_t* gp_babel_neigh_get(gp_babel_iface_t *iface, const gmrf_addr_t *addr) { +gp_babel_neigh_t* gp_babel_neigh_get(gmrf_iface_state_t *iface, const gmrf_addr_t *addr) { gp_babel_neigh_t *neigh = gp_babel_neigh_find(iface, addr); if (!neigh) { neigh = calloc(1, sizeof(gp_babel_neigh_t)); @@ -64,11 +64,11 @@ void gp_babel_neigh_unref(gp_babel_neigh_t *neigh) { free(neigh); } -uint16_t gp_babel_neigh_get_rxcost(gmrf_t *gmrf, const gp_babel_neigh_t *neigh) { +uint16_t gp_babel_neigh_get_rxcost(gmrf_context_t *ctx, const gp_babel_neigh_t *neigh) { if (!neigh->hello_log || !neigh->hello_interval || !neigh->iface) return GP_BABEL_INFINITY; - int timediff = (gmrf_now(gmrf) - neigh->last_hello)/10; + int timediff = (gmrf_now(ctx->gmrf) - neigh->last_hello)/10; int shift = (timediff - neigh->hello_interval/2)/neigh->hello_interval; if (shift >= 16) @@ -82,22 +82,22 @@ uint16_t gp_babel_neigh_get_rxcost(gmrf_t *gmrf, const gp_babel_neigh_t *neigh) return (0x1000/received); } -uint16_t gp_babel_neigh_get_txcost(gmrf_t *gmrf, const gp_babel_neigh_t *neigh) { - if ((gmrf_now(gmrf) - neigh->last_ihu)/10 > GP_BABEL_IHU_TIMEOUT(neigh->ihu_interval) || !neigh->iface) +uint16_t gp_babel_neigh_get_txcost(gmrf_context_t *ctx, const gp_babel_neigh_t *neigh) { + if ((gmrf_now(ctx->gmrf) - neigh->last_ihu)/10 > GP_BABEL_IHU_TIMEOUT(neigh->ihu_interval) || !neigh->iface) return GP_BABEL_INFINITY; else return neigh->txcost; } -uint16_t gp_babel_neigh_get_cost(gmrf_t *gmrf, const gp_babel_neigh_t *neigh) { +uint16_t gp_babel_neigh_get_cost(gmrf_context_t *ctx, const gp_babel_neigh_t *neigh) { if (!neigh) /* self */ return 0; - uint16_t txcost = gp_babel_neigh_get_txcost(gmrf, neigh); + uint16_t txcost = gp_babel_neigh_get_txcost(ctx, neigh); if (txcost < 256) txcost = 256; - uint32_t cost = (txcost * gp_babel_neigh_get_rxcost(gmrf, neigh)) >> 8; + uint32_t cost = (txcost * gp_babel_neigh_get_rxcost(ctx, neigh)) >> 8; if (cost > GP_BABEL_INFINITY) return GP_BABEL_INFINITY; @@ -105,6 +105,6 @@ uint16_t gp_babel_neigh_get_cost(gmrf_t *gmrf, const gp_babel_neigh_t *neigh) { return cost; } -void gp_babel_neigh_reset(gp_babel_iface_t *iface, gp_babel_neigh_t *neigh) { +void gp_babel_neigh_reset(gmrf_iface_state_t *iface, gp_babel_neigh_t *neigh) { //gp_babel_send_announce_request(iface, neigh, GMRF_PROTO_BABEL_NODE_ID_UNSPEC, 0, 0, true /* XXX change this later */); } diff --git a/src/neigh.h b/src/neigh.h index 022cffe..54b935e 100644 --- a/src/neigh.h +++ b/src/neigh.h @@ -30,7 +30,7 @@ #include "babel.h" -static inline gp_babel_neigh_t* gp_babel_neigh_find(const gp_babel_iface_t *iface, const gmrf_addr_t *addr) { +static inline gp_babel_neigh_t* gp_babel_neigh_find(const gmrf_iface_state_t *iface, const gmrf_addr_t *addr) { gp_babel_neigh_t *neigh; for (neigh = iface->neighbours; neigh; neigh = neigh->next) { if (gmrf_addr_equal(addr, &neigh->addr)) @@ -45,13 +45,13 @@ static inline void gp_babel_neigh_ref(gp_babel_neigh_t *neigh) { } -gp_babel_neigh_t* gp_babel_neigh_get(gp_babel_iface_t *iface, const gmrf_addr_t *addr); +gp_babel_neigh_t* gp_babel_neigh_get(gmrf_iface_state_t *iface, const gmrf_addr_t *addr); void gp_babel_neigh_unref(gp_babel_neigh_t *neigh); void gp_babel_neigh_unref_list(gp_babel_neigh_t *neigh); -uint16_t gp_babel_neigh_get_rxcost(gmrf_t *gmrf, const gp_babel_neigh_t *neigh); -uint16_t gp_babel_neigh_get_txcost(gmrf_t *gmrf, const gp_babel_neigh_t *neigh); -uint16_t gp_babel_neigh_get_cost(gmrf_t *gmrf, const gp_babel_neigh_t *neigh); -void gp_babel_neigh_reset(gp_babel_iface_t *iface, gp_babel_neigh_t *neigh); +uint16_t gp_babel_neigh_get_rxcost(gmrf_context_t *ctx, const gp_babel_neigh_t *neigh); +uint16_t gp_babel_neigh_get_txcost(gmrf_context_t *ctx, const gp_babel_neigh_t *neigh); +uint16_t gp_babel_neigh_get_cost(gmrf_context_t *ctx, const gp_babel_neigh_t *neigh); +void gp_babel_neigh_reset(gmrf_iface_state_t *iface, gp_babel_neigh_t *neigh); #endif /* _GMRF_PROTO_BABEL_NEIGH_H_ */ @@ -31,34 +31,34 @@ #include "tlv_types.h" -static inline bool send_neigh(gmrf_t *gmrf, const gp_babel_neigh_t *neigh, const gp_babel_packet_t *packet) { +static inline bool send_neigh(gmrf_context_t *ctx, const gp_babel_neigh_t *neigh, const gp_babel_packet_t *packet) { if (!neigh->iface) return false; - if (!gmrf_iface_send(gmrf, neigh->iface->gmrf_iface, packet, gp_babel_packet_size(packet), &neigh->addr)) { - gmrf_logf(gmrf, LOG_WARNING, "gmrf_iface_send: %m"); + if (!gmrf_iface_send(ctx->gmrf, neigh->iface->gmrf_iface, packet, gp_babel_packet_size(packet), &neigh->addr)) { + gmrf_logf(ctx->gmrf, LOG_WARNING, "gmrf_iface_send: %m"); return false; } return true; } -static inline bool send_iface(gmrf_t *gmrf, const gp_babel_iface_t *iface, const gp_babel_packet_t *packet) { - if (!gmrf_iface_send_bc(gmrf, iface->gmrf_iface, packet, gp_babel_packet_size(packet))) { - gmrf_logf(gmrf, LOG_WARNING, "gmrf_iface_send_bc: %m"); +static inline bool send_iface(gmrf_context_t *ctx, const gmrf_iface_state_t *iface, const gp_babel_packet_t *packet) { + if (!gmrf_iface_send_bc(ctx->gmrf, iface->gmrf_iface, packet, gp_babel_packet_size(packet))) { + gmrf_logf(ctx->gmrf, LOG_WARNING, "gmrf_iface_send_bc: %m"); return false; } return true; } -static inline void send_bc(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_packet_t *packet) { - gp_babel_iface_t *iface; +static inline void send_bc(gmrf_context_t *ctx, const gp_babel_packet_t *packet) { + gmrf_iface_state_t *iface; for (iface = ctx->interfaces; iface; iface = iface->next) - send_iface(gmrf, iface, packet); + send_iface(ctx, iface, packet); } -void gp_babel_send_ack(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_neigh_t *neigh, uint16_t nonce) { +void gp_babel_send_ack(gmrf_context_t *ctx, gp_babel_neigh_t *neigh, uint16_t nonce) { gp_babel_packet_buf_t *buf = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX); gp_babel_tlv_ack_t *ack = gp_babel_tlv_add(buf, TLV_ACK, sizeof(gp_babel_tlv_ack_t)); @@ -67,10 +67,10 @@ void gp_babel_send_ack(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_neigh_t *neig ack->nonce = htons(nonce); - send_neigh(gmrf, neigh, &buf->packet); + send_neigh(ctx, neigh, &buf->packet); } -static void add_ihus(gmrf_t *gmrf, gp_babel_packet_buf_t *buf, const gp_babel_iface_t *iface) { +static void add_ihus(gmrf_context_t *ctx, gp_babel_packet_buf_t *buf, const gmrf_iface_state_t *iface) { const gp_babel_neigh_t *neigh; for (neigh = iface->neighbours; neigh; neigh = neigh->next) { @@ -80,14 +80,14 @@ static void add_ihus(gmrf_t *gmrf, gp_babel_packet_buf_t *buf, const gp_babel_if ihu->ae = ADDR_ENC_GMRF; ihu->reserved = 0; - ihu->rxcost = htons(gp_babel_neigh_get_rxcost(gmrf, neigh)); + ihu->rxcost = htons(gp_babel_neigh_get_rxcost(ctx, neigh)); ihu->interval = htons(GP_BABEL_IHU_INTERVAL); memcpy(ihu->address, &neigh->addr, sizeof(gmrf_addr_t)); } } -void gp_babel_send_hellos(gmrf_t *gmrf, gmrf_context_t *ctx) { - gmrf_logf(gmrf, LOG_DEBUG, "sending hellos..."); +void gp_babel_send_hellos(gmrf_context_t *ctx) { + gmrf_logf(ctx->gmrf, LOG_DEBUG, "sending hellos..."); gp_babel_packet_buf_t *buf = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX); @@ -100,14 +100,14 @@ void gp_babel_send_hellos(gmrf_t *gmrf, gmrf_context_t *ctx) { uint16_t len = buf->packet.len; - gp_babel_iface_t *iface; + gmrf_iface_state_t *iface; for (iface = ctx->interfaces; iface; iface = iface->next) { hello->seqno = htons(iface->seqno++); buf->packet.len = len; - add_ihus(gmrf, buf, iface); + add_ihus(ctx, buf, iface); - send_iface(gmrf, iface, &buf->packet); + send_iface(ctx, iface, &buf->packet); } } @@ -169,7 +169,7 @@ static bool add_update(gp_babel_packet_buf_t *buf, gp_babel_node_id_t *node_id, return true; } -void gp_babel_send_update(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_iface_t *iface, gp_babel_neigh_t *neigh, gp_babel_announce_t *announce, bool with_payload) { +void gp_babel_send_update(gmrf_context_t *ctx, gmrf_iface_state_t *iface, gp_babel_neigh_t *neigh, gp_babel_announce_t *announce, bool with_payload) { gp_babel_packet_buf_t *buf = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX); if (announce) { @@ -182,15 +182,15 @@ void gp_babel_send_update(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_iface_t *i for (a = ctx->announces; a; a = a->next) { if (!add_update(buf, &node_id, a, with_payload, neigh)) { if (neigh) - send_neigh(gmrf, neigh, &buf->packet); + send_neigh(ctx, neigh, &buf->packet); else - send_iface(gmrf, iface, &buf->packet); + send_iface(ctx, iface, &buf->packet); node_id = gp_babel_node_id_unspec; buf->packet.len = 0; if (!add_update(buf, &node_id, a, with_payload, neigh)) { - gmrf_logf(gmrf, LOG_ERR, "add_update failed"); + gmrf_logf(ctx->gmrf, LOG_ERR, "add_update failed"); return; } } @@ -199,13 +199,13 @@ void gp_babel_send_update(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_iface_t *i if (buf->packet.len) { if (neigh) - send_neigh(gmrf, neigh, &buf->packet); + send_neigh(ctx, neigh, &buf->packet); else - send_iface(gmrf, iface, &buf->packet); + send_iface(ctx, iface, &buf->packet); } } -void gp_babel_send_announce_request(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_iface_t *iface, gp_babel_neigh_t *neigh, const gp_babel_node_id_t *node, uint16_t type, uint16_t key, bool with_payload) { +void gp_babel_send_announce_request(gmrf_context_t *ctx, gmrf_iface_state_t *iface, gp_babel_neigh_t *neigh, const gp_babel_node_id_t *node, uint16_t type, uint16_t key, bool with_payload) { gp_babel_packet_buf_t *buf = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX); gp_babel_tlv_announce_req_t *req = gp_babel_tlv_add(buf, TLV_ANNOUNCE_REQ, sizeof(gp_babel_tlv_announce_req_t)); @@ -222,12 +222,12 @@ void gp_babel_send_announce_request(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_ req->flags |= GP_BABEL_ANNOUCE_REQ_FLAG_WITH_PAYLOAD; if (neigh) - send_neigh(gmrf, neigh, &buf->packet); + send_neigh(ctx, neigh, &buf->packet); else - send_iface(gmrf, iface, &buf->packet); + send_iface(ctx, iface, &buf->packet); } -void gp_babel_send_seqno_request(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_neigh_t *neigh, gp_babel_announce_t *announce, uint16_t seqno, uint8_t hop_count) { +void gp_babel_send_seqno_request(gmrf_context_t *ctx, gp_babel_neigh_t *neigh, gp_babel_announce_t *announce, uint16_t seqno, uint8_t hop_count) { gp_babel_packet_buf_t *buf = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX); gp_babel_tlv_seqno_req_t *req = gp_babel_tlv_add(buf, TLV_SEQNO_REQ, sizeof(gp_babel_tlv_seqno_req_t)); @@ -241,7 +241,7 @@ void gp_babel_send_seqno_request(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_nei req->key = htons(announce->key); if (neigh) - send_neigh(gmrf, neigh, &buf->packet); + send_neigh(ctx, neigh, &buf->packet); else - send_bc(gmrf, ctx, &buf->packet); + send_bc(ctx, &buf->packet); } @@ -30,7 +30,7 @@ #include <string.h> -bool gp_babel_tlv_parse(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_packet_t *packet, gp_babel_tlv_cb cb, void *arg) { +bool gp_babel_tlv_parse(gmrf_context_t *ctx, const gp_babel_packet_t *packet, gp_babel_tlv_cb cb, void *arg) { if (packet->version != htons(GP_BABEL_VERSION)) return false; @@ -49,7 +49,7 @@ bool gp_babel_tlv_parse(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_packet } if (data[-2] != TLV_PADN) - cb(gmrf, ctx, data[-2], data, data[-1], arg); + cb(ctx, data[-2], data, data[-1], arg); data += data[-1] + 2; } @@ -53,10 +53,10 @@ typedef enum gp_babel_addr_enc { } gp_babel_addr_enc_t; -typedef void (*gp_babel_tlv_cb)(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_tlv_type_t type, const void *data, size_t len, void *arg); +typedef void (*gp_babel_tlv_cb)(gmrf_context_t *ctx, gp_babel_tlv_type_t type, const void *data, size_t len, void *arg); -bool gp_babel_tlv_parse(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_packet_t *packet, gp_babel_tlv_cb cb, void *arg); +bool gp_babel_tlv_parse(gmrf_context_t *ctx, const gp_babel_packet_t *packet, gp_babel_tlv_cb cb, void *arg); void* gp_babel_tlv_add(gp_babel_packet_buf_t *buf, gp_babel_tlv_type_t type, size_t len); diff --git a/src/tlv_types.c b/src/tlv_types.c index cc380ea..6e94d51 100644 --- a/src/tlv_types.c +++ b/src/tlv_types.c @@ -33,7 +33,7 @@ typedef struct handle_tlv_arg { - gp_babel_iface_t *iface; + gmrf_iface_state_t *iface; const gmrf_addr_t *source; gp_babel_node_id_t node_id; @@ -43,53 +43,53 @@ static inline gp_babel_neigh_t* get_tlv_neigh(handle_tlv_arg_t *arg) { return gp_babel_neigh_get(arg->iface, arg->source); } -static void handle_tlv_ack_req(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_tlv_ack_req_t *tlv_req, size_t len, handle_tlv_arg_t *arg) { +static void handle_tlv_ack_req(gmrf_context_t *ctx, const gp_babel_tlv_ack_req_t *tlv_req, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_ack_req_t)) { - gmrf_logf(gmrf, LOG_WARNING, "received short acknowledement request TLV."); + gmrf_logf(ctx->gmrf, LOG_WARNING, "received short acknowledement request TLV."); return; } - gp_babel_send_ack(gmrf, ctx, get_tlv_neigh(arg), ntohs(tlv_req->nonce)); + gp_babel_send_ack(ctx, get_tlv_neigh(arg), ntohs(tlv_req->nonce)); } -static void handle_tlv_ack(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_tlv_ack_t *tlv_ack, size_t len, handle_tlv_arg_t *arg) { +static void handle_tlv_ack(gmrf_context_t *ctx, const gp_babel_tlv_ack_t *tlv_ack, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_ack_t)) { - gmrf_logf(gmrf, LOG_WARNING, "received short acknowledement TLV."); + gmrf_logf(ctx->gmrf, LOG_WARNING, "received short acknowledement TLV."); return; } //gp_babel_ack_handle(ntohs(tlv_ack->nonce)); } -static void handle_tlv_hello(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_tlv_hello_t *tlv_hello, size_t len, handle_tlv_arg_t *arg) { +static void handle_tlv_hello(gmrf_context_t *ctx, const gp_babel_tlv_hello_t *tlv_hello, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_hello_t)) { - gmrf_logf(gmrf, LOG_WARNING, "received short hello TLV."); + gmrf_logf(ctx->gmrf, LOG_WARNING, "received short hello TLV."); return; } - gmrf_logf(gmrf, LOG_DEBUG, "received hello from %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x[%s] with seqno %04x.", + gmrf_logf(ctx->gmrf, LOG_DEBUG, "received hello from %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x[%s] with seqno %04x.", arg->source->d[0], arg->source->d[1], arg->source->d[2], arg->source->d[3], arg->source->d[4], arg->source->d[5], arg->source->d[6], arg->source->d[7], - gmrf_iface_get_name(gmrf, arg->iface->gmrf_iface), ntohs(tlv_hello->seqno)); + gmrf_iface_get_name(ctx->gmrf, arg->iface->gmrf_iface), ntohs(tlv_hello->seqno)); gp_babel_neigh_t *neigh = get_tlv_neigh(arg); uint16_t seqno = ntohs(tlv_hello->seqno); if (neigh->last_hello != gmrf_time_unspec) { - int timediff = (gmrf_now(gmrf) - neigh->last_hello)/10; + int timediff = (gmrf_now(ctx->gmrf) - neigh->last_hello)/10; uint16_t seqexp = neigh->last_seqno + (timediff - neigh->hello_interval/2)/neigh->hello_interval; /* cast to int16_t to ensure correct handling of seqno wrapping */ if (abs((int16_t)(seqno - seqexp)) > 16) { - gmrf_logf(gmrf, LOG_INFO, "neighbour was reset."); + gmrf_logf(ctx->gmrf, LOG_INFO, "neighbour was reset."); neigh->hello_log = 0; neigh->txcost = GP_BABEL_INFINITY; } else { int16_t seqdiff = seqno - neigh->last_seqno; if (seqdiff <= 0) { - gmrf_logf(gmrf, LOG_DEBUG, "seqno already seen, ignoring."); + gmrf_logf(ctx->gmrf, LOG_DEBUG, "seqno already seen, ignoring."); return; } @@ -97,33 +97,33 @@ static void handle_tlv_hello(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_t } } else { - gmrf_logf(gmrf, LOG_DEBUG, "received hello from new neighbour."); + gmrf_logf(ctx->gmrf, LOG_DEBUG, "received hello from new neighbour."); } neigh->hello_log |= 1; neigh->hello_interval = ntohs(tlv_hello->interval); neigh->last_seqno = seqno; - neigh->last_hello = gmrf_now(gmrf); + neigh->last_hello = gmrf_now(ctx->gmrf); if (neigh->hello_log == 1) /* new or reset neighbour */ gp_babel_neigh_reset(arg->iface, neigh); - gmrf_logf(gmrf, LOG_DEBUG, "accepted hello, log %04x, rxcost is %u, cost is %u now.", neigh->hello_log, gp_babel_neigh_get_rxcost(gmrf, neigh), gp_babel_neigh_get_cost(gmrf, neigh)); + gmrf_logf(ctx->gmrf, LOG_DEBUG, "accepted hello, log %04x, rxcost is %u, cost is %u now.", neigh->hello_log, gp_babel_neigh_get_rxcost(ctx, neigh), gp_babel_neigh_get_cost(ctx, neigh)); } -static void handle_tlv_ihu(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_tlv_ihu_t *tlv_ihu, size_t len, handle_tlv_arg_t *arg) { +static void handle_tlv_ihu(gmrf_context_t *ctx, const gp_babel_tlv_ihu_t *tlv_ihu, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_ihu_t)) { - gmrf_logf(gmrf, LOG_WARNING, "received short IHU TLV."); + gmrf_logf(ctx->gmrf, LOG_WARNING, "received short IHU TLV."); return; } if (tlv_ihu->ae == ADDR_ENC_GMRF) { if (len < sizeof(gp_babel_tlv_ihu_t)+sizeof(gmrf_addr_t)) { - gmrf_logf(gmrf, LOG_WARNING, "received short IHU TLV."); + gmrf_logf(ctx->gmrf, LOG_WARNING, "received short IHU TLV."); return; } - gmrf_addr_t iface_addr = gmrf_iface_get_addr(gmrf, arg->iface->gmrf_iface); + gmrf_addr_t iface_addr = gmrf_iface_get_addr(ctx->gmrf, arg->iface->gmrf_iface); if (memcmp(tlv_ihu->address, &iface_addr, sizeof(gmrf_addr_t)) != 0) return; } @@ -133,48 +133,48 @@ static void handle_tlv_ihu(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_tlv gp_babel_neigh_t *neigh = get_tlv_neigh(arg); neigh->ihu_interval = ntohs(tlv_ihu->interval); - neigh->last_ihu = gmrf_now(gmrf); + neigh->last_ihu = gmrf_now(ctx->gmrf); neigh->txcost = ntohs(tlv_ihu->rxcost); - gmrf_logf(gmrf, LOG_DEBUG, "accepted IHU, txcost is %u, cost is %u now.", gp_babel_neigh_get_txcost(gmrf, neigh), gp_babel_neigh_get_cost(gmrf, neigh)); + gmrf_logf(ctx->gmrf, LOG_DEBUG, "accepted IHU, txcost is %u, cost is %u now.", gp_babel_neigh_get_txcost(ctx, neigh), gp_babel_neigh_get_cost(ctx, neigh)); } -static void handle_tlv_node_id(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_tlv_node_id_t *tlv_node_id, size_t len, handle_tlv_arg_t *arg) { +static void handle_tlv_node_id(gmrf_context_t *ctx, const gp_babel_tlv_node_id_t *tlv_node_id, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_node_id_t)) { - gmrf_logf(gmrf, LOG_WARNING, "received short node id TLV."); + gmrf_logf(ctx->gmrf, LOG_WARNING, "received short node id TLV."); return; } arg->node_id = tlv_node_id->id; } -static void handle_tlv_update(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_tlv_update_t *tlv_update, size_t len, handle_tlv_arg_t *arg) { +static void handle_tlv_update(gmrf_context_t *ctx, const gp_babel_tlv_update_t *tlv_update, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_update_t)) { - gmrf_logf(gmrf, LOG_WARNING, "received short update TLV."); + gmrf_logf(ctx->gmrf, LOG_WARNING, "received short update TLV."); return; } if (gp_babel_node_id_is_unspec(&arg->node_id)) { - gmrf_logf(gmrf, LOG_WARNING, "received update TLV without node id TLV."); + gmrf_logf(ctx->gmrf, LOG_WARNING, "received update TLV without node id TLV."); return; } if (gp_babel_node_id_equal(&arg->node_id, &ctx->self)) { - gmrf_logf(gmrf, LOG_DEBUG, "update source is myself."); + gmrf_logf(ctx->gmrf, LOG_DEBUG, "update source is myself."); return; } - gp_babel_announce_t *announce = gp_babel_announce_get(gmrf, ctx, &arg->node_id, ntohs(tlv_update->type), ntohs(tlv_update->key)); + gp_babel_announce_t *announce = gp_babel_announce_get(ctx, &arg->node_id, ntohs(tlv_update->type), ntohs(tlv_update->key)); gp_babel_metric_seqno_t ms = { ntohs(tlv_update->metric), ntohs(tlv_update->seqno) }; bool feasible = gp_babel_is_feasible(announce, ms); gp_babel_neigh_t *neigh = get_tlv_neigh(arg); gp_babel_nexthop_t *nexthop = gp_babel_announce_nexthop_find(announce, neigh); - gmrf_logf(gmrf, LOG_DEBUG, "update received from %04x%04x, metric %u, seqno %04x.", + gmrf_logf(ctx->gmrf, LOG_DEBUG, "update received from %04x%04x, metric %u, seqno %04x.", ntohl(*(uint32_t*)arg->node_id.id), ntohl(*(uint32_t*)(arg->node_id.id+4)), ms.metric, ms.seqno); - gmrf_logf(gmrf, LOG_DEBUG, "the update is %sfeasible and there is %s nexthop.", feasible ? "" : "not ", nexthop ? "a" : "no"); + gmrf_logf(ctx->gmrf, LOG_DEBUG, "the update is %sfeasible and there is %s nexthop.", feasible ? "" : "not ", nexthop ? "a" : "no"); if (!nexthop) { if (feasible && tlv_update->metric != GP_BABEL_INFINITY /* no need to ntohs */) @@ -182,16 +182,16 @@ static void handle_tlv_update(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_ } else { if (!feasible && nexthop == announce->selected) { - gmrf_logf(gmrf, LOG_DEBUG, "requesting new seqno"); - gp_babel_send_seqno_request_for(gmrf, ctx, neigh, announce); + gmrf_logf(ctx->gmrf, LOG_DEBUG, "requesting new seqno"); + gp_babel_send_seqno_request_for(ctx, neigh, announce); nexthop = NULL; } } if (!feasible && nexthop && nexthop != announce->selected) { - if (ms.metric + gp_babel_neigh_get_cost(gmrf, neigh) < announce->metric.metric) { - gmrf_logf(gmrf, LOG_DEBUG, "requesting new seqno"); - gp_babel_send_seqno_request_for(gmrf, ctx, neigh, announce); + if (ms.metric + gp_babel_neigh_get_cost(ctx, neigh) < announce->metric.metric) { + gmrf_logf(ctx->gmrf, LOG_DEBUG, "requesting new seqno"); + gp_babel_send_seqno_request_for(ctx, neigh, announce); } } @@ -206,56 +206,56 @@ static void handle_tlv_update(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_ /* request payload */ if (nexthop) - gp_babel_send_announce_request(gmrf, ctx, NULL, neigh, &announce->node, announce->type, announce->key, true); + gp_babel_send_announce_request(ctx, NULL, neigh, &announce->node, announce->type, announce->key, true); } } if (!nexthop) return; - gmrf_logf(gmrf, LOG_DEBUG, "the update was accepted."); + gmrf_logf(ctx->gmrf, LOG_DEBUG, "the update was accepted."); - gp_babel_announce_update_nexthop(gmrf, ctx, announce, nexthop, ms, ntohs(tlv_update->interval)); + gp_babel_announce_update_nexthop(ctx, announce, nexthop, ms, ntohs(tlv_update->interval)); } -static void handle_tlv(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_tlv_type_t type, const void *data, size_t len, void *arg) { +static void handle_tlv(gmrf_context_t *ctx, gp_babel_tlv_type_t type, const void *data, size_t len, void *arg) { handle_tlv_arg_t *tlv_arg = arg; switch (type) { case TLV_ACK_REQ: - handle_tlv_ack_req(gmrf, ctx, data, len, tlv_arg); + handle_tlv_ack_req(ctx, data, len, tlv_arg); return; case TLV_ACK: - handle_tlv_ack(gmrf, ctx, data, len, tlv_arg); + handle_tlv_ack(ctx, data, len, tlv_arg); return; case TLV_HELLO: - handle_tlv_hello(gmrf, ctx, data, len, tlv_arg); + handle_tlv_hello(ctx, data, len, tlv_arg); return; case TLV_IHU: - handle_tlv_ihu(gmrf, ctx, data, len, tlv_arg); + handle_tlv_ihu(ctx, data, len, tlv_arg); return; case TLV_NODE_ID: - handle_tlv_node_id(gmrf, ctx, data, len, tlv_arg); + handle_tlv_node_id(ctx, data, len, tlv_arg); return; case TLV_UPDATE: - handle_tlv_update(gmrf, ctx, data, len, tlv_arg); + handle_tlv_update(ctx, data, len, tlv_arg); return; default: - gmrf_logf(gmrf, LOG_DEBUG, "received unknown TLV %u on %s.", type, gmrf_iface_get_name(gmrf, tlv_arg->iface->gmrf_iface)); + gmrf_logf(ctx->gmrf, LOG_DEBUG, "received unknown TLV %u on %s.", type, gmrf_iface_get_name(ctx->gmrf, tlv_arg->iface->gmrf_iface)); return; } } -void gp_babel_handle_packet(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_iface_t *iface, const gmrf_addr_t *source, const gp_babel_packet_t *packet) { +void gp_babel_handle_packet(gmrf_context_t *ctx, gmrf_iface_state_t *iface, const gmrf_addr_t *source, const gp_babel_packet_t *packet) { handle_tlv_arg_t arg = { .iface = iface, .source = source }; - if (!gp_babel_tlv_parse(gmrf, ctx, packet, handle_tlv, &arg)) { - gmrf_logf(gmrf, LOG_DEBUG, "received invalid packet."); + if (!gp_babel_tlv_parse(ctx, packet, handle_tlv, &arg)) { + gmrf_logf(ctx->gmrf, LOG_DEBUG, "received invalid packet."); } } diff --git a/src/types.h b/src/types.h index 3e38d91..d7cd604 100644 --- a/src/types.h +++ b/src/types.h @@ -45,7 +45,6 @@ typedef struct gp_babel_metric_seqno { } gp_babel_metric_seqno_t; -typedef struct gp_babel_iface gp_babel_iface_t; typedef struct gp_babel_neigh gp_babel_neigh_t; typedef struct gp_babel_announce gp_babel_announce_t; typedef struct gp_babel_nexthop gp_babel_nexthop_t; |