From 2b3fe33c07b17b6bbc6515f73aef5efac89fd542 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 18 Mar 2013 23:56:48 +0100 Subject: Implement hello and IHU sending and receiving --- src/CMakeLists.txt | 3 +- src/babel.c | 230 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/babel.h | 86 ++++++++++++++++++++ src/core.c | 53 ------------ src/neigh.c | 110 +++++++++++++++++++++++++ src/neigh.h | 57 +++++++++++++ src/tlv.c | 4 +- src/tlv.h | 4 +- src/types.h | 2 + 9 files changed, 491 insertions(+), 58 deletions(-) create mode 100644 src/babel.c create mode 100644 src/babel.h delete mode 100644 src/core.c create mode 100644 src/neigh.c create mode 100644 src/neigh.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5301b14..60bd916 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,8 @@ include_directories(${GMRF_INCLUDE_DIR}) add_library(mmss_proto_babel MODULE - core.c + babel.c + neigh.c tlv.c ) target_link_libraries(mmss_proto_babel ${MMSS_PROTOCOL_LIB}) diff --git a/src/babel.c b/src/babel.c new file mode 100644 index 0000000..39cd6c8 --- /dev/null +++ b/src/babel.c @@ -0,0 +1,230 @@ +/* + Copyright (c) 2013, Matthias Schiffer + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include "babel.h" +#include "neigh.h" +#include "packet.h" +#include "tlv.h" +#include "tlv_types.h" + +#include + +#include + + +const char *gmrf_protocol_name = "babel"; +const char *gmrf_protocol_version = "experimental"; + + +static void add_ihus(gmrf_t *gmrf, gp_babel_packet_t *packet, size_t max_len, const gp_babel_iface_t *iface) { + const gp_babel_neigh_t *neigh; + + for (neigh = iface->neighbours; neigh; neigh = neigh->next) { + gp_babel_tlv_ihu_t *ihu = gp_babel_tlv_add(packet, GP_BABEL_PACKET_MAX, TLV_IHU, sizeof(gp_babel_tlv_ihu_t)+sizeof(gmrf_addr_t)); + if (!ihu) + return; + + ihu->ae = ADDR_ENC_GMRF; + ihu->reserved = 0; + ihu->rxcost = htons(gp_babel_neigh_get_rxcost(gmrf, neigh)); + ihu->interval = htons(GP_BABEL_IHU_INTERVAL); + memcpy(ihu->address, &neigh->addr, sizeof(gmrf_addr_t)); + } +} + +static void send_hellos(gmrf_t *gmrf, gmrf_context_t *ctx, void *arg) { + gmrf_schedule(gmrf, send_hellos, NULL, GP_BABEL_HELLO_INTERVAL*10); + + gmrf_logf(gmrf, LOG_DEBUG, "sending hellos..."); + + gp_babel_packet_t *packet = alloca(sizeof(gp_babel_packet_t)+GP_BABEL_PACKET_MAX); + + packet->version = htons(GP_BABEL_VERSION); + packet->len = 0; + + gp_babel_tlv_hello_t *hello = gp_babel_tlv_add(packet, GP_BABEL_PACKET_MAX, TLV_HELLO, sizeof(gp_babel_tlv_hello_t)); + if (!hello) + return; + + hello->reserved = 0; + hello->interval = htons(GP_BABEL_HELLO_INTERVAL); + + uint16_t len = packet->len; + + gp_babel_iface_t *iface; + for (iface = ctx->interfaces; iface; iface = iface->next) { + hello->seqno = htons(iface->seqno++); + + packet->len = len; + + add_ihus(gmrf, packet, GP_BABEL_PACKET_MAX, iface); + + gmrf_iface_send_bc(gmrf, iface->gmrf_iface, packet, sizeof(gp_babel_packet_t)+ntohs(packet->len)); + } +} + +gmrf_context_t* gmrf_protocol_init(gmrf_t *gmrf) { + gmrf_logf(gmrf, LOG_INFO, "initalizing..."); + + gmrf_schedule(gmrf, send_hellos, NULL, GP_BABEL_HELLO_INTERVAL*10); + + gmrf_context_t *ctx = calloc(1, sizeof(gmrf_context_t)); + gmrf_random_bytes(gmrf, &ctx->self, sizeof(gp_babel_node_id_t)); + + 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)); + + gp_babel_iface_t *b_iface = calloc(1, sizeof(gp_babel_iface_t)); + b_iface->gmrf_iface = iface; + + b_iface->next = ctx->interfaces; + ctx->interfaces = b_iface; +} + +typedef struct handle_tlv_arg { + gp_babel_iface_t *iface; + const gmrf_addr_t *source; + + gp_babel_node_id_t node_id; +} handle_tlv_arg_t; + +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_hello(gmrf_t *gmrf, 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."); + return; + } + + gmrf_logf(gmrf, LOG_DEBUG, "received hello with seqno %u.", 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; + 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."); + neigh->hello_log = 0; + neigh->txcost = 0xffff; + } + else { + int16_t seqdiff = seqno - neigh->last_seqno; + if (seqdiff <= 0) { + gmrf_logf(gmrf, LOG_DEBUG, "seqno already seen, ignoring."); + return; + } + + neigh->hello_log <<= seqdiff; + } + } + else { + gmrf_logf(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); + + 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)); +} + +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) { + if (len < sizeof(gp_babel_tlv_ihu_t)) { + gmrf_logf(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."); + return; + } + + gmrf_addr_t iface_addr = gmrf_iface_get_addr(gmrf, arg->iface->gmrf_iface); + if (memcmp(tlv_ihu->address, &iface_addr, sizeof(gmrf_addr_t)) != 0) + return; + } + else if (tlv_ihu->ae != ADDR_ENC_UNSPEC) { + return; + } + + gp_babel_neigh_t *neigh = get_tlv_neigh(arg); + neigh->ihu_interval = ntohs(tlv_ihu->interval); + neigh->last_ihu = gmrf_now(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)); +} + +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) { + handle_tlv_arg_t *tlv_arg = arg; + + switch (type) { + case TLV_HELLO: + handle_tlv_hello(gmrf, ctx, data, len, tlv_arg); + return; + + case TLV_IHU: + handle_tlv_ihu(gmrf, 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)); + return; + } +} + +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) { + const gp_babel_packet_t *packet = data; + + if (len < sizeof(gp_babel_packet_t) || len < sizeof(gp_babel_packet_t)+ntohs(packet->len)) { + gmrf_logf(gmrf, LOG_DEBUG, "received short packet."); + return; + } + + handle_tlv_arg_t arg = { .iface = gp_babel_get_iface(ctx, iface), .source = source }; + + if (!arg.iface) + return; + + if (!gp_babel_tlv_parse(gmrf, ctx, packet, handle_tlv, &arg)) { + gmrf_logf(gmrf, LOG_DEBUG, "received invalid packet."); + } +} diff --git a/src/babel.h b/src/babel.h new file mode 100644 index 0000000..7ad5450 --- /dev/null +++ b/src/babel.h @@ -0,0 +1,86 @@ +/* + Copyright (c) 2013, Matthias Schiffer + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#ifndef _GMRF_PROTO_BABEL_BABEL_H_ +#define _GMRF_PROTO_BABEL_BABEL_H_ + +#include "types.h" + +#define GP_BABEL_PACKET_MAX 1000 + +#define GP_BABEL_HELLO_INTERVAL 400 +#define GP_BABEL_IHU_INTERVAL (3*GP_BABEL_HELLO_INTERVAL) + +#define GP_BABEL_HELLO_TIMEOUT(interval) (16*(interval)) +#define GP_BABEL_IHU_TIMEOUT(interval) ((interval)*7/2) + +struct gmrf_context { + gp_babel_node_id_t self; + + gp_babel_iface_t *interfaces; + gp_babel_neigh_t *neighbours; +}; + +struct gp_babel_iface { + gp_babel_iface_t *next; + + gmrf_iface_t *gmrf_iface; + + uint16_t seqno; + + gp_babel_neigh_t *neighbours; +}; + +struct gp_babel_neigh { + gp_babel_neigh_t *next; + + unsigned ref; + + gp_babel_iface_t *iface; + gmrf_addr_t addr; + + uint16_t hello_log; + uint16_t hello_interval; + uint16_t last_seqno; + gmrf_time_t last_hello; + + uint16_t ihu_interval; + gmrf_time_t last_ihu; + + uint16_t txcost; +}; + +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; +} + +#endif /* _GMRF_PROTO_BABEL_BABEL_H_ */ diff --git a/src/core.c b/src/core.c deleted file mode 100644 index d8d089f..0000000 --- a/src/core.c +++ /dev/null @@ -1,53 +0,0 @@ -/* - Copyright (c) 2013, Matthias Schiffer - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - - -#include - - -const char *gmrf_protocol_name = "babel"; -const char *gmrf_protocol_version = "experimental"; - - -static void periodic_task(gmrf_t *gmrf, gmrf_context_t *ctx, void *arg) { - gmrf_logf(gmrf, LOG_DEBUG, "still there."); - - gmrf_schedule(gmrf, periodic_task, NULL, 1000); -} - -gmrf_context_t* gmrf_protocol_init(gmrf_t *gmrf) { - gmrf_logf(gmrf, LOG_INFO, "initalizing..."); - - gmrf_schedule(gmrf, periodic_task, NULL, 1000); - - return NULL; -} - -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)); -} - -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) { -} diff --git a/src/neigh.c b/src/neigh.c new file mode 100644 index 0000000..bbef746 --- /dev/null +++ b/src/neigh.c @@ -0,0 +1,110 @@ +/* + Copyright (c) 2012, Matthias Schiffer + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include "neigh.h" + +#include + + +gp_babel_neigh_t* gp_babel_neigh_get(gp_babel_iface_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)); + neigh->next = iface->neighbours; + neigh->iface = iface; + iface->neighbours = neigh; + neigh->last_hello = gmrf_time_unspec; + neigh->last_ihu = gmrf_time_unspec; + neigh->addr = *addr; + + neigh->txcost = 0xffff; + } + + return neigh; +} + +void gp_babel_neigh_unref_list(gp_babel_neigh_t *neigh) { + gp_babel_neigh_t *next; + for (; neigh; neigh = next) { + next = neigh->next; + + neigh->iface = NULL; + neigh->next = NULL; + + if (!neigh->ref) + free(neigh); + } +} + +void gp_babel_neigh_unref(gp_babel_neigh_t *neigh) { + if (!(--neigh->ref) && !neigh->iface) + free(neigh); +} + +uint16_t gp_babel_neigh_get_rxcost(gmrf_t *gmrf, const gp_babel_neigh_t *neigh) { + if (!neigh->hello_log || !neigh->hello_interval || !neigh->iface) + return 0xffff; + + int timediff = (gmrf_now(gmrf) - neigh->last_hello)/10; + int shift = (timediff - neigh->hello_interval/2)/neigh->hello_interval; + + if (shift >= 16) + return 0xffff; + + int received = __builtin_popcount((neigh->hello_log << shift) & 0xffff); + + if (received == 0) + return 0xffff; + else + 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) + return 0xffff; + else + return neigh->txcost; +} + +uint16_t gp_babel_neigh_get_cost(gmrf_t *gmrf, const gp_babel_neigh_t *neigh) { + if (!neigh) /* self */ + return 0; + + uint16_t txcost = gp_babel_neigh_get_txcost(gmrf, neigh); + if (txcost < 256) + txcost = 256; + + uint32_t cost = (txcost * gp_babel_neigh_get_rxcost(gmrf, neigh)) >> 8; + + if (cost > 0xffff) + return 0xffff; + else + return cost; +} + +void gp_babel_neigh_reset(gp_babel_iface_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 new file mode 100644 index 0000000..022cffe --- /dev/null +++ b/src/neigh.h @@ -0,0 +1,57 @@ +/* + Copyright (c) 2012, Matthias Schiffer + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#ifndef _GMRF_PROTO_BABEL_NEIGH_H_ +#define _GMRF_PROTO_BABEL_NEIGH_H_ + +#include "babel.h" + + +static inline gp_babel_neigh_t* gp_babel_neigh_find(const gp_babel_iface_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)) + return neigh; + } + + return NULL; +} + +static inline void gp_babel_neigh_ref(gp_babel_neigh_t *neigh) { + neigh->ref++; +} + + +gp_babel_neigh_t* gp_babel_neigh_get(gp_babel_iface_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); + +#endif /* _GMRF_PROTO_BABEL_NEIGH_H_ */ diff --git a/src/tlv.c b/src/tlv.c index 22d04b3..b43156c 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -32,7 +32,7 @@ #include -bool gp_babel_tlv_parse(const gp_babel_packet_t *packet, gp_babel_tlv_cb cb, 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) { if (packet->version != htons(GP_BABEL_VERSION)) return false; @@ -51,7 +51,7 @@ bool gp_babel_tlv_parse(const gp_babel_packet_t *packet, gp_babel_tlv_cb cb, voi } if (data[-2] != TLV_PADN) - cb(data[-2], data, data[-1], arg); + cb(gmrf, ctx, data[-2], data, data[-1], arg); data += data[-1] + 2; } diff --git a/src/tlv.h b/src/tlv.h index d8a154e..d05a4a8 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -53,10 +53,10 @@ typedef enum gp_babel_addr_enc { } gp_babel_addr_enc_t; -typedef void (*gp_babel_tlv_cb)(gp_babel_tlv_type_t type, const void *data, size_t len, void *arg); +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); -bool gp_babel_tlv_parse(const gp_babel_packet_t *packet, gp_babel_tlv_cb cb, 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); void* gp_babel_tlv_add(gp_babel_packet_t *packet, size_t max_len, gp_babel_tlv_type_t type, size_t len); diff --git a/src/types.h b/src/types.h index 5689d9a..ce3d99f 100644 --- a/src/types.h +++ b/src/types.h @@ -35,5 +35,7 @@ typedef struct __attribute__((packed)) gp_gabel_node_id { } gp_babel_node_id_t; typedef struct gp_babel_packet gp_babel_packet_t; +typedef struct gp_babel_iface gp_babel_iface_t; +typedef struct gp_babel_neigh gp_babel_neigh_t; #endif /* _GMRF_PROTO_BABEL_TYPES_H_ */ -- cgit v1.2.3