From f29e37138a190c5cb43747433d687c944b7d2efe Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 1 Aug 2013 21:07:59 +0200 Subject: Simplify basic protocol, rename announces to routes, begin implementing route request handling --- src/tlv_types.c | 143 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 76 insertions(+), 67 deletions(-) (limited to 'src/tlv_types.c') diff --git a/src/tlv_types.c b/src/tlv_types.c index 5f077db..bd68642 100644 --- a/src/tlv_types.c +++ b/src/tlv_types.c @@ -43,25 +43,25 @@ 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_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, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_ack_req_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short acknowledement request TLV."); return; } - gp_babel_send_ack(ctx, get_tlv_neigh(arg), ntohs(tlv_req->nonce)); + gp_babel_send_ack(ctx, get_tlv_neigh(arg), ntohs(tlv->nonce)); } -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) { +static void handle_tlv_ack(gmrf_context_t *ctx, const gp_babel_tlv_ack_t *tlv, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_ack_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short acknowledement TLV."); return; } - //gp_babel_ack_handle(ntohs(tlv_ack->nonce)); + //gp_babel_ack_handle(ntohs(tlv->nonce)); } -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) { +static void handle_tlv_hello(gmrf_context_t *ctx, const gp_babel_tlv_hello_t *tlv, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_hello_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short hello TLV."); return; @@ -70,11 +70,11 @@ static void handle_tlv_hello(gmrf_context_t *ctx, const gp_babel_tlv_hello_t *tl 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(ctx->gmrf, arg->iface->gmrf_iface), ntohs(tlv_hello->seqno)); + gmrf_iface_get_name(ctx->gmrf, arg->iface->gmrf_iface), ntohs(tlv->seqno)); gp_babel_neigh_t *neigh = get_tlv_neigh(arg); - uint16_t seqno = ntohs(tlv_hello->seqno); + uint16_t seqno = ntohs(tlv->seqno); if (neigh->last_hello != gmrf_time_unspec) { int timediff = (gmrf_now(ctx->gmrf) - neigh->last_hello)/10; @@ -101,112 +101,92 @@ static void handle_tlv_hello(gmrf_context_t *ctx, const gp_babel_tlv_hello_t *tl } neigh->hello_log |= 1; - neigh->hello_interval = ntohs(tlv_hello->interval); + neigh->hello_interval = ntohs(tlv->interval); neigh->last_seqno = seqno; neigh->last_hello = gmrf_now(ctx->gmrf); if (neigh->hello_log == 1) /* new or reset neighbour */ - gp_babel_neigh_reset(arg->iface, neigh); + gp_babel_neigh_reset(ctx, arg->iface, 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_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, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_ihu_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short IHU TLV."); return; } - if (tlv_ihu->ae == ADDR_ENC_GMRF) { + if (tlv->ae == ADDR_ENC_GMRF) { if (len < sizeof(gp_babel_tlv_ihu_t)+sizeof(gmrf_addr_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short IHU TLV."); return; } 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) + if (memcmp(tlv->address, &iface_addr, sizeof(gmrf_addr_t)) != 0) return; } - else if (tlv_ihu->ae != ADDR_ENC_UNSPEC) { + else if (tlv->ae != ADDR_ENC_UNSPEC) { return; } gp_babel_neigh_t *neigh = get_tlv_neigh(arg); - neigh->ihu_interval = ntohs(tlv_ihu->interval); + neigh->ihu_interval = ntohs(tlv->interval); neigh->last_ihu = gmrf_now(ctx->gmrf); - neigh->txcost = ntohs(tlv_ihu->rxcost); + neigh->txcost = ntohs(tlv->rxcost); 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_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, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_node_id_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short node id TLV."); return; } - arg->node_id = tlv_node_id->id; + arg->node_id = tlv->id; } -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) { +static void handle_tlv_update(gmrf_context_t *ctx, const gp_babel_tlv_update_t *tlv, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_update_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short update TLV."); return; } - if (gp_babel_node_id_is_unspec(&arg->node_id)) { - 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)) { + if (gp_babel_node_id_equal(&tlv->node, &ctx->self)) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "update source is myself."); return; } - 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_route_t *route = gp_babel_route_get(ctx, &tlv->node); + gp_babel_metric_seqno_t ms = { ntohs(tlv->metric), ntohs(tlv->seqno) }; + bool feasible = gp_babel_is_feasible(route, ms); gp_babel_neigh_t *neigh = get_tlv_neigh(arg); - gp_babel_nexthop_t *nexthop = gp_babel_announce_nexthop_find(announce, neigh); + gp_babel_nexthop_t *nexthop = gp_babel_route_nexthop_find(route, neigh); 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)), + ntohl(*(uint32_t*)tlv->node.id), ntohl(*(uint32_t*)(tlv->node.id+4)), ms.metric, ms.seqno); 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 */) - nexthop = gp_babel_announce_nexthop_new(announce, neigh); + if (feasible && tlv->metric != GP_BABEL_INFINITY /* no need to ntohs */) + nexthop = gp_babel_route_nexthop_new(route, neigh); } else { - if (!feasible && nexthop == announce->selected) { + if (!feasible && nexthop == route->selected) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "requesting new seqno"); - gp_babel_send_seqno_request_for(ctx, neigh, announce); + gp_babel_send_seqno_request_for(ctx, neigh, route); nexthop = NULL; } } - if (!feasible && nexthop && nexthop != announce->selected) { - if (ms.metric + gp_babel_neigh_get_cost(ctx, neigh) < announce->metric.metric) { + if (!feasible && nexthop && nexthop != route->selected) { + if (ms.metric + gp_babel_neigh_get_cost(ctx, neigh) < route->metric.metric) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "requesting new seqno"); - gp_babel_send_seqno_request_for(ctx, neigh, announce); - } - } - - if ((tlv_update->flags & GP_BABEL_UPDATE_FLAG_HAS_PAYLOAD) && !announce->payload) { - if (len > sizeof(gp_babel_tlv_update_t)) { - announce->len = len - sizeof(gp_babel_tlv_update_t); - announce->payload = malloc(announce->len); - memcpy(announce->payload, tlv_update->payload, announce->len); - } - else { - announce->len = 0xff; - - /* request payload */ - if (nexthop) - gp_babel_send_announce_request(ctx, NULL, neigh, &announce->node, announce->type, announce->key, true); + gp_babel_send_seqno_request_for(ctx, neigh, route); } } @@ -215,56 +195,81 @@ static void handle_tlv_update(gmrf_context_t *ctx, const gp_babel_tlv_update_t * gmrf_logf(ctx->gmrf, LOG_DEBUG, "the update was accepted."); - gp_babel_announce_update_nexthop(ctx, announce, nexthop, ms, ntohs(tlv_update->interval)); + gp_babel_route_update_nexthop(ctx, route, nexthop, ms, ntohs(tlv->interval)); } -static void handle_tlv_seqno_req(gmrf_context_t *ctx, const gp_babel_tlv_seqno_req_t *tlv_seqno_req, size_t len, handle_tlv_arg_t *arg) { +static void handle_tlv_route_req(gmrf_context_t *ctx, const gp_babel_tlv_route_req_t *tlv, size_t len, handle_tlv_arg_t *arg) { + if (len < sizeof(gp_babel_tlv_route_req_t)) { + gmrf_logf(ctx->gmrf, LOG_WARNING, "received short route request TLV."); + return; + } + + const gp_babel_node_id_t *node = &tlv->node; + gp_babel_route_t *route = NULL; + gp_babel_neigh_t *neigh = get_tlv_neigh(arg); + + if (gp_babel_node_id_is_unspec(node)) + node = NULL; + + if (node) { + route = gp_babel_route_find(ctx, &tlv->node); + if (!route) { + gmrf_logf(ctx->gmrf, LOG_DEBUG, "received route request for unknown route, retracting."); + gp_babel_send_retract(ctx, arg->iface, neigh, node); + return; + } + } + + // TODO +} + +static void handle_tlv_seqno_req(gmrf_context_t *ctx, const gp_babel_tlv_seqno_req_t *tlv, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_seqno_req_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short seqno request TLV."); return; } - gp_babel_announce_t *announce = gp_babel_announce_get(ctx, &tlv_seqno_req->node, ntohs(tlv_seqno_req->type), ntohs(tlv_seqno_req->key)); - if (!announce || !announce->selected) { - gmrf_logf(ctx->gmrf, LOG_DEBUG, "received seqno request for unknown/unreachable announce."); + gp_babel_route_t *route = gp_babel_route_find(ctx, &tlv->node); + if (!route || !route->selected) { + gmrf_logf(ctx->gmrf, LOG_DEBUG, "received seqno request for unknown/unreachable route."); return; } gmrf_logf(ctx->gmrf, LOG_DEBUG, "seqno request received for %04x%04x, seqno %04x.", - ntohl(*(uint32_t*)tlv_seqno_req->node.id), ntohl(*(uint32_t*)(tlv_seqno_req->node.id+4)), - ntohs(tlv_seqno_req->seqno)); + ntohl(*(uint32_t*)tlv->node.id), ntohl(*(uint32_t*)(tlv->node.id+4)), + ntohs(tlv->seqno)); gp_babel_neigh_t *neigh = get_tlv_neigh(arg); - if (!gp_babel_less(announce->metric.seqno, ntohs(tlv_seqno_req->seqno))) { + if (!gp_babel_less(route->metric.seqno, ntohs(tlv->seqno))) { /* local seqno is high enough */ gmrf_logf(ctx->gmrf, LOG_DEBUG, "sending update."); - gp_babel_send_update(ctx, arg->iface, neigh, announce, false); + gp_babel_send_update_for_route(ctx, arg->iface, neigh, route); return; } - if (!announce->selected->neigh) { - /* announce is local, increment seqno */ + if (!route->selected->neigh) { + /* route is local, increment seqno */ gmrf_logf(ctx->gmrf, LOG_DEBUG, "incrementing seqno."); - announce->selected->metric_seqno.seqno++; + route->selected->metric_seqno.seqno++; - gp_babel_send_update(ctx, arg->iface, neigh, announce, false); + gp_babel_send_update_for_route(ctx, arg->iface, neigh, route); return; } - if (tlv_seqno_req->hop_count < 2) { + if (tlv->hop_count < 2) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "hopcount too low, discarding."); return; } - if (announce->selected->neigh == neigh) { + if (route->selected->neigh == neigh) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "not forwarding backwards."); return; } /* forward request */ gmrf_logf(ctx->gmrf, LOG_DEBUG, "forwarding request."); - gp_babel_send_seqno_request(ctx, announce->selected->neigh, announce, ntohs(tlv_seqno_req->seqno), tlv_seqno_req->hop_count-1); + gp_babel_send_seqno_request(ctx, route->selected->neigh, route, ntohs(tlv->seqno), tlv->hop_count-1); } static void handle_tlv(gmrf_context_t *ctx, gp_babel_tlv_type_t type, const void *data, size_t len, void *arg) { @@ -295,6 +300,10 @@ static void handle_tlv(gmrf_context_t *ctx, gp_babel_tlv_type_t type, const void handle_tlv_update(ctx, data, len, tlv_arg); return; + case TLV_ROUTE_REQ: + handle_tlv_route_req(ctx, data, len, tlv_arg); + return; + case TLV_SEQNO_REQ: handle_tlv_seqno_req(ctx, data, len, tlv_arg); return; -- cgit v1.2.3