244 lines
5.9 KiB
C
244 lines
5.9 KiB
C
/*
|
|
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 "ffd.h"
|
|
#include "netif.h"
|
|
#include "packet.h"
|
|
|
|
#include <errno.h>
|
|
#include <poll.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include <netpacket/packet.h>
|
|
|
|
#include <sys/socket.h>
|
|
#include <sys/uio.h>
|
|
|
|
|
|
#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
|
|
|
|
|
|
static char *mesh = "bat0";
|
|
static unsigned mtu = 1528;
|
|
|
|
static int sockfd;
|
|
static struct timespec now;
|
|
|
|
static ffd_neigh_t self;
|
|
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 inline bool use_netif(const char *ifname) {
|
|
char *if_mesh = netif_get_mesh(ifname);
|
|
bool ret = (if_mesh && !strcmp(if_mesh, mesh));
|
|
free(if_mesh);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static void update_time() {
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
}
|
|
|
|
static bool check_config() {
|
|
if (!netif_is_mesh(mesh)) {
|
|
fprintf(stderr, "error: configured interface is no mesh\n");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool init_self() {
|
|
eth_addr_t primary_addr = netif_mesh_get_primary_addr(mesh);
|
|
if (is_eth_addr_unspec(&primary_addr))
|
|
return false;
|
|
|
|
memset(&self, 0, sizeof(self));
|
|
memset(&own_data, 0, sizeof(own_data));
|
|
|
|
self.addr = own_data.addr = primary_addr;
|
|
|
|
random_bytes(&self.rev, sizeof(self.rev));
|
|
random_bytes(&own_data.rev, sizeof(own_data.rev));
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool init_socket() {
|
|
sockfd = socket(AF_PACKET, SOCK_DGRAM, htons(FFD_PROTO));
|
|
if (sockfd < 0) {
|
|
fprintf(stderr, "error: socket: %m\n");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static void join_mcast(const char *ifname, unsigned ifindex, void *arg) {
|
|
if (!use_netif(ifname))
|
|
return;
|
|
|
|
struct packet_mreq mr;
|
|
memset(&mr, 0, sizeof(mr));
|
|
mr.mr_ifindex = ifindex;
|
|
mr.mr_type = PACKET_MR_MULTICAST;
|
|
mr.mr_alen = ETH_ALEN;
|
|
memcpy(mr.mr_address, ffd_addr.d, ETH_ALEN);
|
|
if (setsockopt(sockfd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) && errno != EADDRINUSE)
|
|
fprintf(stderr, "warning: setsockopt: %m\n");
|
|
}
|
|
|
|
static bool send_eth(const eth_addr_t *addr, unsigned ifindex, void *buf, size_t len) {
|
|
static const uint8_t zeros[46] = {0};
|
|
|
|
struct sockaddr_ll sa;
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.sll_family = AF_PACKET;
|
|
sa.sll_protocol = htons(FFD_PROTO);
|
|
sa.sll_ifindex = ifindex;
|
|
sa.sll_halen = ETH_ALEN;
|
|
memcpy(sa.sll_addr, addr->d, ETH_ALEN);
|
|
|
|
struct iovec vec[2] = {
|
|
{ .iov_base = buf, .iov_len = len },
|
|
{ .iov_base = (void*)zeros, .iov_len = sizeof(zeros) - len },
|
|
};
|
|
|
|
struct msghdr msg;
|
|
memset(&msg, 0, sizeof(msg));
|
|
msg.msg_name = &sa;
|
|
msg.msg_namelen = sizeof(sa);
|
|
msg.msg_iov = vec;
|
|
msg.msg_iovlen = (len < 46) ? 2 : 1;
|
|
|
|
|
|
while (sendmsg(sockfd, &msg, 0) < 0) {
|
|
if (errno != EINTR)
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static void send_announce(const char *ifname, unsigned ifindex, void *arg) {
|
|
if (!use_netif(ifname))
|
|
return;
|
|
|
|
ffd_packet_announce_t *announce = arg;
|
|
|
|
if (!send_eth(&ffd_addr, ifindex, announce, PACKET_ANNOUNCE_SIZE(announce->n_origs)))
|
|
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));
|
|
|
|
announce->version = FFD_VERSION;
|
|
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() {
|
|
uint8_t buf[mtu];
|
|
struct sockaddr_ll from;
|
|
socklen_t fromlen = sizeof(from);
|
|
|
|
ssize_t readlen = recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr*)&from, &fromlen);
|
|
|
|
if (readlen < 0) {
|
|
fprintf(stderr, "error: recvfrom: %m\n");
|
|
return;
|
|
}
|
|
|
|
fprintf(stderr, "debug: read %zi bytes.\n", readlen);
|
|
}
|
|
|
|
int main() {
|
|
if (!check_config())
|
|
return 1;
|
|
|
|
if (!init_self())
|
|
return 1;
|
|
|
|
if (!init_socket())
|
|
return 1;
|
|
|
|
update_time();
|
|
|
|
struct timespec next_announce = now;
|
|
|
|
while (true) {
|
|
netif_foreach(join_mcast, NULL);
|
|
|
|
int timeout = timespec_diff(&next_announce, &now);
|
|
|
|
if (timeout <= 0) {
|
|
send_announces();
|
|
|
|
next_announce.tv_sec += ANNOUNCE_INTERVAL;
|
|
continue;
|
|
}
|
|
|
|
struct pollfd fds[1];
|
|
|
|
fds[0].fd = sockfd;
|
|
fds[0].events = POLLIN;
|
|
|
|
poll(fds, 1, timeout);
|
|
|
|
update_time();
|
|
|
|
if (fds[0].revents & POLLIN)
|
|
receive_packet();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|