From 9eeec649fb91784d26cbfcad876cff1d6b950408 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 25 Sep 2012 00:38:19 +0200 Subject: Send hello packets --- ffd/ffd.c | 16 ++++++++++++++-- ffd/tlv.c | 29 ++++++++++++++++------------- ffd/tlv.h | 15 ++++++++++++--- ffd/tlv_types.h | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 18 deletions(-) create mode 100644 ffd/tlv_types.h diff --git a/ffd/ffd.c b/ffd/ffd.c index fcf41e3..27e5c29 100644 --- a/ffd/ffd.c +++ b/ffd/ffd.c @@ -27,6 +27,8 @@ #include "ffd.h" #include "netif.h" #include "packet.h" +#include "tlv.h" +#include "tlv_types.h" #include #include @@ -46,6 +48,7 @@ static const eth_addr_t ffd_addr = {{0x03, 0x00, 0x00, 0x00, 0x0f, 0xfd}}; #define HELLO_INTERVAL 10 +#define PACKET_MAX 1000 static char *mesh = "bat0"; @@ -161,19 +164,28 @@ static void send_hello(const char *ifname, unsigned ifindex, void *arg) { ffd_packet_t *packet = arg; - if (!send_eth(&ffd_addr, ifindex, packet, sizeof(ffd_packet_t)+packet->len)) + if (!send_eth(&ffd_addr, ifindex, packet, sizeof(ffd_packet_t)+ntohs(packet->len))) fprintf(stderr, "send_eth: %m\n"); } static void send_hellos() { /*if (!orig_data) return;*/ + static uint16_t seqno = 0; - ffd_packet_t *packet = alloca(sizeof(ffd_packet_t)+1000); + ffd_packet_t *packet = alloca(sizeof(ffd_packet_t)+PACKET_MAX); packet->version_magic = htons(FFD_VERSION_MAGIC); packet->len = 0; + ffd_tlv_hello_t *hello = ffd_tlv_add(packet, PACKET_MAX, TLV_HELLO, sizeof(ffd_tlv_hello_t)); + if (!hello) + return; + + memset(hello, 0, sizeof(ffd_tlv_hello_t)); + hello->seqno = htons(seqno++); + hello->interval = htons(HELLO_INTERVAL*100); + netif_foreach(send_hello, packet); } diff --git a/ffd/tlv.c b/ffd/tlv.c index 684a0cc..0a1950d 100644 --- a/ffd/tlv.c +++ b/ffd/tlv.c @@ -32,14 +32,14 @@ #include -bool ffd_tlv_parse(const ffd_packet_t *packet, ffd_tlv_cb cb) { +bool ffd_tlv_parse(const ffd_packet_t *packet, ffd_tlv_cb cb, void *arg) { if (packet->version_magic != htons(FFD_VERSION_MAGIC)) return false; - const uint8_t *data = packet->tlv + 2, *end = ((uint8_t*)packet->tlv) + packet->len; + const uint8_t *data = packet->tlv + 2, *end = packet->tlv + packet->len; while (data <= end) { - if (!data[-2]) { + if (data[-2] == TLV_PAD1) { /* Pad1 */ data++; continue; @@ -50,24 +50,27 @@ bool ffd_tlv_parse(const ffd_packet_t *packet, ffd_tlv_cb cb) { break; } - cb(data[-2], data, data[-1]); + if (data[-2] != TLV_PADN) + cb(data[-2], data, data[-1], arg); + 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; +void* ffd_tlv_add(ffd_packet_t *packet, size_t max_len, ffd_tlv_type_t type, size_t len) { + size_t pktlen = ntohs(packet->len); - packet->len = htons(ntohs(packet->len)+len+2); + if (pktlen+len+2 > max_len) + return NULL; - uint8_t *pkgdata = packet->tlv; + uint8_t *data = packet->tlv+pktlen+2; - pkgdata[0] = type; - pkgdata[1] = len; - memcpy(pkgdata+2, data, len); + data[-2] = type; + data[-1] = len; - return true; + packet->len = htons(pktlen+len+2); + + return data; } diff --git a/ffd/tlv.h b/ffd/tlv.h index b8501a3..287c5c6 100644 --- a/ffd/tlv.h +++ b/ffd/tlv.h @@ -33,14 +33,23 @@ typedef enum _ffd_tlv_type_t { TLV_PAD1 = 0, TLV_PADN, + TLV_ACK_REQ, + TLV_ACK, + TLV_HELLO, + TLV_IHU, + TLV_NODE_ID, + TLV_RESERVED, + TLV_UPDATE, + TLV_INFO_REQ, + TLV_SEQNO_REQ, } ffd_tlv_type_t; -typedef void (*ffd_tlv_cb)(ffd_tlv_type_t type, const void *data, size_t len); +typedef void (*ffd_tlv_cb)(ffd_tlv_type_t type, const void *data, size_t len, void *arg); -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); +bool ffd_tlv_parse(const ffd_packet_t *packet, ffd_tlv_cb cb, void *arg); +void* ffd_tlv_add(ffd_packet_t *packet, size_t max_len, ffd_tlv_type_t type, size_t len); #endif /* _FFD_TLV_H_ */ diff --git a/ffd/tlv_types.h b/ffd/tlv_types.h new file mode 100644 index 0000000..39efc13 --- /dev/null +++ b/ffd/tlv_types.h @@ -0,0 +1,39 @@ +/* + 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_TYPES_H_ +#define _FFD_TLV_TYPES_H_ + +#include "tlv.h" + + +typedef struct __attribute__((packed)) _ffd_tlv_hello_t { + uint16_t reserved; + uint16_t seqno; + uint16_t interval; +} ffd_tlv_hello_t; + +#endif /* _FFD_TLV_TYPES_H_ */ -- cgit v1.2.3