From 9706934d897105cf504d1b831c834f38f42efa17 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Wed, 3 Sep 2014 23:43:40 +0100 Subject: [PATCH] log mac expiry for symmetry with mac discovery and w/o polluting the mac_cache code with logging --- mac_cache.go | 9 ++++++--- router.go | 5 ++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/mac_cache.go b/mac_cache.go index a387c136..c08e04c4 100644 --- a/mac_cache.go +++ b/mac_cache.go @@ -18,12 +18,14 @@ type MacCache struct { table map[uint64]*MacCacheEntry maxAge time.Duration expiryTimer *time.Timer + onExpiry func(net.HardwareAddr, *Peer) } -func NewMacCache(maxAge time.Duration) *MacCache { +func NewMacCache(maxAge time.Duration, onExpiry func(net.HardwareAddr, *Peer)) *MacCache { res := &MacCache{ - table: make(map[uint64]*MacCacheEntry), - maxAge: maxAge} + table: make(map[uint64]*MacCacheEntry), + maxAge: maxAge, + onExpiry: onExpiry} res.setExpiryTimer() return res } @@ -102,6 +104,7 @@ func (cache *MacCache) expire() { for key, entry := range cache.table { if now.After(entry.lastSeen.Add(cache.maxAge)) { delete(cache.table, key) + cache.onExpiry(intmac(key), entry.peer) } } cache.setExpiryTimer() diff --git a/router.go b/router.go index 17d395dc..649777fc 100644 --- a/router.go +++ b/router.go @@ -15,9 +15,12 @@ import ( const macMaxAge = 10 * time.Minute func NewRouter(iface *net.Interface, name PeerName, password []byte, connLimit int, bufSz int, logFrame func(string, []byte, *layers.Ethernet)) *Router { + onMacExpiry := func(mac net.HardwareAddr, peer *Peer) { + log.Println("Expired MAC", mac, "at", peer.Name) + } router := &Router{ Iface: iface, - Macs: NewMacCache(macMaxAge), + Macs: NewMacCache(macMaxAge, onMacExpiry), Peers: NewPeerCache(), ConnLimit: connLimit, BufSz: bufSz,