Change lots of data structures for the new TLV-based protocool

This commit is contained in:
Matthias Schiffer 2012-09-24 22:44:42 +02:00
parent b3b606e7f3
commit e16168401d
6 changed files with 149 additions and 46 deletions

View file

@ -3,6 +3,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${FFD_BINARY_DIR} ${CMAKE_CURREN
add_executable(ffd add_executable(ffd
ffd.c ffd.c
netif.c netif.c
tlv.c
util.c util.c
) )
target_link_libraries(ffd rt) target_link_libraries(ffd rt)

View file

@ -42,11 +42,10 @@
#define FFD_PROTO 0xffd #define FFD_PROTO 0xffd
#define FFD_VERSION 0
static const eth_addr_t ffd_addr = {{0x03, 0x00, 0x00, 0x00, 0x0f, 0xfd}}; 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"; 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 */ /* neighs and origs that have been changed must be moved to front */
//static ffd_neigh_t *neigh_data = NULL; //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) { 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; 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)) if (!use_netif(ifname))
return; 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"); fprintf(stderr, "send_eth: %m\n");
} }
static void send_announces() { static void send_hellos() {
if (!orig_data) /*if (!orig_data)
return; return;*/
ffd_packet_announce_t *announce = alloca(PACKET_ANNOUNCE_SIZE(PACKET_ANNOUNCE_MAX_ORIGS)); ffd_packet_t *packet = alloca(sizeof(ffd_packet_t)+1000);
memset(announce, 0, PACKET_ANNOUNCE_SIZE(PACKET_ANNOUNCE_MAX_ORIGS)); packet->version_magic = htons(FFD_VERSION_MAGIC);
packet->len = 0;
announce->version = FFD_VERSION; netif_foreach(send_hello, packet);
announce->type = PACKET_ANNOUNCE;
announce->self_rev = self.rev;
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;
netif_foreach(send_announce, announce);
} }
static void receive_packet() { static void receive_packet() {
@ -212,17 +204,17 @@ int main() {
update_time(); update_time();
struct timespec next_announce = now; struct timespec next_hello = now;
while (true) { while (true) {
netif_foreach(join_mcast, NULL); netif_foreach(join_mcast, NULL);
int timeout = timespec_diff(&next_announce, &now); int timeout = timespec_diff(&next_hello, &now);
if (timeout <= 0) { if (timeout <= 0) {
send_announces(); send_hellos();
next_announce.tv_sec += ANNOUNCE_INTERVAL; next_hello.tv_sec += HELLO_INTERVAL;
continue; continue;
} }

View file

@ -27,31 +27,20 @@
#ifndef _FFD_PACKET_H_ #ifndef _FFD_PACKET_H_
#define _FFD_PACKET_H_ #define _FFD_PACKET_H_
#include "util.h" #include "types.h"
typedef enum _ffd_packet_type_t { #define FFD_MAGIC 0xffd
PACKET_UNSPEC = 0, #define FFD_VERSION 0
PACKET_ANNOUNCE,
PACKET_ORIG,
} ffd_packet_type_t;
typedef struct __attribute__((packed)) _ffd_packet_head_t { #define FFD_VERSION_MAGIC ((FFD_MAGIC<<4)|FFD_VERSION)
uint8_t version;
uint8_t type;
} ffd_packet_head_t;
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;
#define PACKET_ANNOUNCE_MAX_ORIGS 16 struct __attribute__((packed)) _ffd_packet_t {
#define PACKET_ANNOUNCE_SIZE(n_origs) (sizeof(ffd_packet_announce_t) + (n_origs)*sizeof(uint64_t)) uint16_t version_magic;
uint16_t len;
uint8_t tlv[0];
};
#endif /* _FFD_PACKET_H_ */ #endif /* _FFD_PACKET_H_ */

73
ffd/tlv.c Normal file
View file

@ -0,0 +1,73 @@
/*
Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
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 <string.h>
#include <arpa/inet.h>
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;
}

46
ffd/tlv.h Normal file
View file

@ -0,0 +1,46 @@
/*
Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
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_ */

View file

@ -37,8 +37,10 @@ typedef struct __attribute__((__packed__)) _eth_addr_t {
uint8_t d[ETH_ALEN]; uint8_t d[ETH_ALEN];
} eth_addr_t; } eth_addr_t;
#define ETH_ADDR_UNSPEC ((eth_addr_t){{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}) #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_ */ #endif /* _FFD_TYPES_H_ */