From c5a1d00fc40a9c55024cfc7f92b0a455975ba503 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 26 Sep 2012 19:16:53 +0200 Subject: Keep track of interfaces --- ffd/ffd.c | 83 +++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 23 deletions(-) (limited to 'ffd/ffd.c') diff --git a/ffd/ffd.c b/ffd/ffd.c index 27e5c29..e729a43 100644 --- a/ffd/ffd.c +++ b/ffd/ffd.c @@ -57,8 +57,10 @@ static unsigned mtu = 1528; static int sockfd; static struct timespec now; +static ffd_iface_t *iface_list = NULL; + static ffd_neigh_t self; -static ffd_orig_t own_data; +//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; @@ -92,12 +94,12 @@ static bool init_self() { return false; memset(&self, 0, sizeof(self)); - memset(&own_data, 0, sizeof(own_data)); + //memset(&own_data, 0, sizeof(own_data)); - self.addr = own_data.addr = primary_addr; + //self.addr = own_data.addr = primary_addr; - random_bytes(&self.rev, sizeof(self.rev)); - random_bytes(&own_data.rev, sizeof(own_data.rev)); + //random_bytes(&self.rev, sizeof(self.rev)); + //random_bytes(&own_data.rev, sizeof(own_data.rev)); return true; } @@ -112,10 +114,11 @@ static bool init_socket() { return true; } -static void join_mcast(const char *ifname, unsigned ifindex, void *arg) { +static void update_netif(const char *ifname, unsigned ifindex, void *arg) { if (!use_netif(ifname)) return; + /* join multicast group */ struct packet_mreq mr; memset(&mr, 0, sizeof(mr)); mr.mr_ifindex = ifindex; @@ -124,6 +127,48 @@ static void join_mcast(const char *ifname, unsigned ifindex, void *arg) { 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"); + + ffd_iface_t *iface; + for (iface = iface_list; iface; iface = iface->next) { + if (iface->ifindex == ifindex) + break; + } + + if (!iface) { + /* new iface */ + iface = malloc(sizeof(ffd_iface_t)); + iface->next = iface_list; + iface_list = iface; + + iface->seqno = 0; + iface->neighs = NULL; + } + + iface->ifindex = ifindex; + strncpy(iface->name, ifname, IF_NAMESIZE); + iface->type = netif_get_type(ifname); +} + +static void update_netifs() { + ffd_iface_t *iface, **cur; + + for (iface = iface_list; iface; iface = iface->next) + iface->type = IF_UNSPEC; + + netif_foreach(update_netif, NULL); + + cur = &iface_list; + while (*cur) { + iface = *cur; + + if (iface->type == IF_UNSPEC) { + *cur = iface->next; + free(iface); + } + else { + cur = &iface->next; + } + } } static bool send_eth(const eth_addr_t *addr, unsigned ifindex, void *buf, size_t len) { @@ -158,21 +203,8 @@ static bool send_eth(const eth_addr_t *addr, unsigned ifindex, void *buf, size_t 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); @@ -182,11 +214,16 @@ static void send_hellos() { if (!hello) return; - memset(hello, 0, sizeof(ffd_tlv_hello_t)); - hello->seqno = htons(seqno++); + hello->reserved = 0; hello->interval = htons(HELLO_INTERVAL*100); - netif_foreach(send_hello, packet); + ffd_iface_t *iface; + for (iface = iface_list; iface; iface = iface->next) { + hello->seqno = htons(iface->seqno++); + + if (!send_eth(&ffd_addr, iface->ifindex, packet, sizeof(ffd_packet_t)+ntohs(packet->len))) + fprintf(stderr, "send_eth: %m\n"); + } } static void receive_packet() { @@ -219,7 +256,7 @@ int main() { struct timespec next_hello = now; while (true) { - netif_foreach(join_mcast, NULL); + update_netifs(); int timeout = timespec_diff(&next_hello, &now); -- cgit v1.2.3