/* 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 "ffd.h" #include "netif.h" #include "packet.h" #include "tlv.h" #include "tlv_types.h" #include #include #include #include #include #include #include #include #include #define FFD_PROTO 0xffd 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"; 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_hello(const char *ifname, unsigned ifindex, void *arg) { if (!use_netif(ifname)) return; ffd_packet_t *packet = arg; 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)+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); } 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_hello = now; while (true) { netif_foreach(join_mcast, NULL); int timeout = timespec_diff(&next_hello, &now); if (timeout <= 0) { send_hellos(); next_hello.tv_sec += HELLO_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; }