From e16168401d61c20bf4d66d5636c30ad10874fc52 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 24 Sep 2012 22:44:42 +0200 Subject: Change lots of data structures for the new TLV-based protocool --- ffd/CMakeLists.txt | 1 + ffd/ffd.c | 40 ++++++++++++------------------ ffd/packet.h | 29 +++++++--------------- ffd/tlv.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ffd/tlv.h | 46 ++++++++++++++++++++++++++++++++++ ffd/types.h | 4 ++- 6 files changed, 148 insertions(+), 45 deletions(-) create mode 100644 ffd/tlv.c create mode 100644 ffd/tlv.h diff --git a/ffd/CMakeLists.txt b/ffd/CMakeLists.txt index d2d2a52..d04e69f 100644 --- a/ffd/CMakeLists.txt +++ b/ffd/CMakeLists.txt @@ -3,6 +3,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${FFD_BINARY_DIR} ${CMAKE_CURREN add_executable(ffd ffd.c netif.c + tlv.c util.c ) target_link_libraries(ffd rt) diff --git a/ffd/ffd.c b/ffd/ffd.c index ce71d4b..fcf41e3 100644 --- a/ffd/ffd.c +++ b/ffd/ffd.c @@ -42,11 +42,10 @@ #define FFD_PROTO 0xffd -#define FFD_VERSION 0 static const eth_addr_t ffd_addr = {{0x03, 0x00, 0x00, 0x00, 0x0f, 0xfd}}; -#define ANNOUNCE_INTERVAL 10 +#define HELLO_INTERVAL 10 static char *mesh = "bat0"; @@ -60,7 +59,7 @@ static ffd_orig_t own_data; /* neighs and origs that have been changed must be moved to front */ //static ffd_neigh_t *neigh_data = NULL; -static ffd_orig_t *orig_data = NULL; +//static ffd_orig_t *orig_data = NULL; static inline bool use_netif(const char *ifname) { @@ -156,33 +155,26 @@ static bool send_eth(const eth_addr_t *addr, unsigned ifindex, void *buf, size_t return true; } -static void send_announce(const char *ifname, unsigned ifindex, void *arg) { +static void send_hello(const char *ifname, unsigned ifindex, void *arg) { if (!use_netif(ifname)) return; - ffd_packet_announce_t *announce = arg; + ffd_packet_t *packet = arg; - if (!send_eth(&ffd_addr, ifindex, announce, PACKET_ANNOUNCE_SIZE(announce->n_origs))) + if (!send_eth(&ffd_addr, ifindex, packet, sizeof(ffd_packet_t)+packet->len)) fprintf(stderr, "send_eth: %m\n"); } -static void send_announces() { - if (!orig_data) - return; - - ffd_packet_announce_t *announce = alloca(PACKET_ANNOUNCE_SIZE(PACKET_ANNOUNCE_MAX_ORIGS)); - - memset(announce, 0, PACKET_ANNOUNCE_SIZE(PACKET_ANNOUNCE_MAX_ORIGS)); +static void send_hellos() { + /*if (!orig_data) + return;*/ - announce->version = FFD_VERSION; - announce->type = PACKET_ANNOUNCE; - announce->self_rev = self.rev; + ffd_packet_t *packet = alloca(sizeof(ffd_packet_t)+1000); - ffd_orig_t *orig; - for (orig = orig_data; orig && (announce->n_origs < PACKET_ANNOUNCE_MAX_ORIGS); orig = orig->next) - announce->origs[announce->n_origs++] = orig->rev; + packet->version_magic = htons(FFD_VERSION_MAGIC); + packet->len = 0; - netif_foreach(send_announce, announce); + netif_foreach(send_hello, packet); } static void receive_packet() { @@ -212,17 +204,17 @@ int main() { update_time(); - struct timespec next_announce = now; + struct timespec next_hello = now; while (true) { netif_foreach(join_mcast, NULL); - int timeout = timespec_diff(&next_announce, &now); + int timeout = timespec_diff(&next_hello, &now); if (timeout <= 0) { - send_announces(); + send_hellos(); - next_announce.tv_sec += ANNOUNCE_INTERVAL; + next_hello.tv_sec += HELLO_INTERVAL; continue; } diff --git a/ffd/packet.h b/ffd/packet.h index 9252c4e..a44824a 100644 --- a/ffd/packet.h +++ b/ffd/packet.h @@ -27,31 +27,20 @@ #ifndef _FFD_PACKET_H_ #define _FFD_PACKET_H_ -#include "util.h" +#include "types.h" -typedef enum _ffd_packet_type_t { - PACKET_UNSPEC = 0, - PACKET_ANNOUNCE, - PACKET_ORIG, -} ffd_packet_type_t; +#define FFD_MAGIC 0xffd +#define FFD_VERSION 0 -typedef struct __attribute__((packed)) _ffd_packet_head_t { - uint8_t version; - uint8_t type; -} ffd_packet_head_t; +#define FFD_VERSION_MAGIC ((FFD_MAGIC<<4)|FFD_VERSION) -typedef struct __attribute__((packed)) _ffd_packet_announce_t { - uint8_t version; - uint8_t type; - uint64_t self_rev; - uint8_t flags; - uint8_t n_origs; - uint64_t origs[]; -} ffd_packet_announce_t; +struct __attribute__((packed)) _ffd_packet_t { + uint16_t version_magic; + uint16_t len; + uint8_t tlv[0]; +}; -#define PACKET_ANNOUNCE_MAX_ORIGS 16 -#define PACKET_ANNOUNCE_SIZE(n_origs) (sizeof(ffd_packet_announce_t) + (n_origs)*sizeof(uint64_t)) #endif /* _FFD_PACKET_H_ */ diff --git a/ffd/tlv.c b/ffd/tlv.c new file mode 100644 index 0000000..684a0cc --- /dev/null +++ b/ffd/tlv.c @@ -0,0 +1,73 @@ +/* + 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 "tlv.h" +#include "packet.h" + +#include + +#include + + +bool ffd_tlv_parse(const ffd_packet_t *packet, ffd_tlv_cb cb) { + if (packet->version_magic != htons(FFD_VERSION_MAGIC)) + return false; + + const uint8_t *data = packet->tlv + 2, *end = ((uint8_t*)packet->tlv) + packet->len; + + while (data <= end) { + if (!data[-2]) { + /* Pad1 */ + data++; + continue; + } + + if (data + data[-1] > end) { + /* warn */ + break; + } + + cb(data[-2], data, data[-1]); + data += data[-1] + 2; + } + + return true; +} + +bool ffd_tlv_add(ffd_packet_t *packet, size_t max_len, ffd_tlv_type_t type, void *data, size_t len) { + if (ntohs(packet->len)+len+2 > max_len) + return false; + + packet->len = htons(ntohs(packet->len)+len+2); + + uint8_t *pkgdata = packet->tlv; + + pkgdata[0] = type; + pkgdata[1] = len; + memcpy(pkgdata+2, data, len); + + return true; +} diff --git a/ffd/tlv.h b/ffd/tlv.h new file mode 100644 index 0000000..b8501a3 --- /dev/null +++ b/ffd/tlv.h @@ -0,0 +1,46 @@ +/* + 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 _FFD_TLV_H_ +#define _FFD_TLV_H_ + +#include "types.h" + + +typedef enum _ffd_tlv_type_t { + TLV_PAD1 = 0, + TLV_PADN, +} ffd_tlv_type_t; + + +typedef void (*ffd_tlv_cb)(ffd_tlv_type_t type, const void *data, size_t len); + + +bool ffd_tlv_parse(const ffd_packet_t *packet, ffd_tlv_cb cb); +bool ffd_tlv_add(ffd_packet_t *packet, size_t max_len, ffd_tlv_type_t type, void *data, size_t len); + + +#endif /* _FFD_TLV_H_ */ diff --git a/ffd/types.h b/ffd/types.h index 5eaaf46..cd2cbd2 100644 --- a/ffd/types.h +++ b/ffd/types.h @@ -37,8 +37,10 @@ typedef struct __attribute__((__packed__)) _eth_addr_t { uint8_t d[ETH_ALEN]; } eth_addr_t; - #define ETH_ADDR_UNSPEC ((eth_addr_t){{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}) +typedef struct _ffd_packet_t ffd_packet_t; + + #endif /* _FFD_TYPES_H_ */ -- cgit v1.2.3