From f8ba80e7d89b5dabb3eb965d3e84cd482c3e9b5b Mon Sep 17 00:00:00 2001 From: Arceliar Date: Thu, 7 Jun 2018 14:13:31 -0500 Subject: [PATCH] remove old switchMessage and clean up related code --- src/yggdrasil/peer.go | 3 +- src/yggdrasil/switch.go | 95 ++++++++++++++--------------------------- 2 files changed, 32 insertions(+), 66 deletions(-) diff --git a/src/yggdrasil/peer.go b/src/yggdrasil/peer.go index 042702e..f9aca08 100644 --- a/src/yggdrasil/peer.go +++ b/src/yggdrasil/peer.go @@ -319,7 +319,6 @@ func (p *peer) handleSwitchMsg(packet []byte) { panic("FIXME testing") return } - var info switchMessage var loc switchLocator prevKey := msg.Root for idx, hop := range msg.Hops { @@ -335,7 +334,7 @@ func (p *peer) handleSwitchMsg(packet []byte) { } prevKey = hop.Next } - p.core.switchTable.handleMsg(&msg, &info, p.port) + p.core.switchTable.handleMsg(&msg, p.port) // Pass a mesage to the dht informing it that this peer (still) exists loc.coords = loc.coords[:len(loc.coords)-1] dinfo := dhtInfo{ diff --git a/src/yggdrasil/switch.go b/src/yggdrasil/switch.go index 0908326..aa9e3d3 100644 --- a/src/yggdrasil/switch.go +++ b/src/yggdrasil/switch.go @@ -113,18 +113,10 @@ type peerInfo struct { key sigPubKey // ID of this peer locator switchLocator // Should be able to respond with signatures upon request degree uint64 // Self-reported degree - coords []switchPort // Coords of this peer (taken from coords of the sent locator) time time.Time // Time this node was last seen firstSeen time.Time port switchPort // Interface number of this peer - seq uint64 // Seq number we last saw this peer advertise - smsg switchMsg // The wire switchMsg used -} - -type switchMessage struct { - from sigPubKey // key of the sender - locator switchLocator // Locator advertised for the receiver, not the sender's loc! - seq uint64 + msg switchMsg // The wire switchMsg used } type switchPort uint64 @@ -144,7 +136,6 @@ type switchData struct { locator switchLocator seq uint64 // Sequence number, reported to peers, so they know about changes peers map[switchPort]peerInfo - sigs []sigInfo msg *switchMsg } @@ -224,7 +215,6 @@ func (t *switchTable) cleanRoot() { } } t.data.locator = switchLocator{root: t.key, tstamp: now.Unix()} - t.data.sigs = nil t.core.peers.sendSwitchMsgs() } } @@ -244,15 +234,6 @@ func (t *switchTable) cleanDropped() { } } -func (t *switchTable) createMessage(port switchPort) (*switchMessage, []sigInfo) { - t.mutex.RLock() - defer t.mutex.RUnlock() - msg := switchMessage{from: t.key, locator: t.data.locator.clone()} - msg.locator.coords = append(msg.locator.coords, port) - msg.seq = t.data.seq - return &msg, t.data.sigs -} - type switchMsg struct { Root sigPubKey TStamp int64 @@ -271,7 +252,7 @@ func (t *switchTable) getMsg() *switchMsg { if t.parent == 0 { return &switchMsg{Root: t.key, TStamp: t.data.locator.tstamp} } else if parent, isIn := t.data.peers[t.parent]; isIn { - msg := parent.smsg + msg := parent.msg msg.Hops = append([]switchMsgHop(nil), msg.Hops...) return &msg } else { @@ -279,47 +260,34 @@ func (t *switchTable) getMsg() *switchMsg { } } -func (t *switchTable) handleMsg(smsg *switchMsg, xmsg *switchMessage, fromPort switchPort) { +func (t *switchTable) handleMsg(msg *switchMsg, fromPort switchPort) { // TODO directly use a switchMsg instead of switchMessage + sigs t.mutex.Lock() defer t.mutex.Unlock() now := time.Now() - - //* - var msg switchMessage - var sigs []sigInfo - msg.locator.root = smsg.Root - msg.locator.tstamp = smsg.TStamp - msg.from = smsg.Root - prevKey := msg.from - for _, hop := range smsg.Hops { + // Set up the sender peerInfo + var sender peerInfo + sender.locator.root = msg.Root + sender.locator.tstamp = msg.TStamp + prevKey := msg.Root + for _, hop := range msg.Hops { // Build locator and signatures var sig sigInfo sig.next = hop.Next sig.sig = hop.Sig - sigs = append(sigs, sig) - msg.locator.coords = append(msg.locator.coords, hop.Port) - msg.from = prevKey + sender.locator.coords = append(sender.locator.coords, hop.Port) + sender.key = prevKey prevKey = hop.Next } - msg.seq = uint64(now.Unix()) - //*/ - - if len(msg.locator.coords) == 0 { - return - } // Should always have >=1 links + sender.msg = *msg oldSender, isIn := t.data.peers[fromPort] if !isIn { oldSender.firstSeen = now } - sender := peerInfo{key: msg.from, - locator: msg.locator, - coords: msg.locator.coords[:len(msg.locator.coords)-1], - time: now, - firstSeen: oldSender.firstSeen, - port: fromPort, - seq: msg.seq, - smsg: *smsg} + sender.firstSeen = oldSender.firstSeen + sender.port = fromPort + sender.time = now + // Decide what to do equiv := func(x *switchLocator, y *switchLocator) bool { if x.root != y.root { return false @@ -335,7 +303,7 @@ func (t *switchTable) handleMsg(smsg *switchMsg, xmsg *switchMessage, fromPort s return true } doUpdate := false - if !equiv(&msg.locator, &oldSender.locator) { + if !equiv(&sender.locator, &oldSender.locator) { doUpdate = true //sender.firstSeen = now // TODO? uncomment to prevent flapping? } @@ -344,12 +312,12 @@ func (t *switchTable) handleMsg(smsg *switchMsg, xmsg *switchMessage, fromPort s oldParent, isIn := t.data.peers[t.parent] noParent := !isIn noLoop := func() bool { - for idx := 0; idx < len(sigs)-1; idx++ { - if sigs[idx].next == t.core.sigPub { + for idx := 0; idx < len(msg.Hops)-1; idx++ { + if msg.Hops[idx].Next == t.core.sigPub { return false } } - if msg.locator.root == t.core.sigPub { + if sender.locator.root == t.core.sigPub { return false } return true @@ -358,30 +326,30 @@ func (t *switchTable) handleMsg(smsg *switchMsg, xmsg *switchMessage, fromPort s pTime := oldParent.time.Sub(oldParent.firstSeen) + switch_timeout // Really want to compare sLen/sTime and pLen/pTime // Cross multiplied to avoid divide-by-zero - cost := len(msg.locator.coords) * int(pTime.Seconds()) + cost := len(sender.locator.coords) * int(pTime.Seconds()) pCost := len(t.data.locator.coords) * int(sTime.Seconds()) - dropTstamp, isIn := t.drop[msg.locator.root] + dropTstamp, isIn := t.drop[sender.locator.root] // Here be dragons switch { case !noLoop: // do nothing - case isIn && dropTstamp >= msg.locator.tstamp: // do nothing - case firstIsBetter(&msg.locator.root, &t.data.locator.root): + case isIn && dropTstamp >= sender.locator.tstamp: // do nothing + case firstIsBetter(&sender.locator.root, &t.data.locator.root): updateRoot = true - case t.data.locator.root != msg.locator.root: // do nothing - case t.data.locator.tstamp > msg.locator.tstamp: // do nothing + case t.data.locator.root != sender.locator.root: // do nothing + case t.data.locator.tstamp > sender.locator.tstamp: // do nothing case noParent: updateRoot = true case cost < pCost: updateRoot = true case sender.port != t.parent: // do nothing - case !equiv(&msg.locator, &t.data.locator): + case !equiv(&sender.locator, &t.data.locator): updateRoot = true case now.Sub(t.time) < switch_throttle: // do nothing - case msg.locator.tstamp > t.data.locator.tstamp: + case sender.locator.tstamp > t.data.locator.tstamp: updateRoot = true } if updateRoot { - if !equiv(&msg.locator, &t.data.locator) { + if !equiv(&sender.locator, &t.data.locator) { doUpdate = true t.data.seq++ select { @@ -391,12 +359,11 @@ func (t *switchTable) handleMsg(smsg *switchMsg, xmsg *switchMessage, fromPort s //t.core.log.Println("Switch update:", msg.locator.root, msg.locator.tstamp, msg.locator.coords) //fmt.Println("Switch update:", msg.Locator.Root, msg.Locator.Tstamp, msg.Locator.Coords) } - if t.data.locator.tstamp != msg.locator.tstamp { + if t.data.locator.tstamp != sender.locator.tstamp { t.time = now } - t.data.locator = msg.locator + t.data.locator = sender.locator t.parent = sender.port - t.data.sigs = sigs //t.core.log.Println("Switch update:", msg.Locator.Root, msg.Locator.Tstamp, msg.Locator.Coords) t.core.peers.sendSwitchMsgs() }