5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-23 00:51:35 +00:00

remove old switchMessage and clean up related code

This commit is contained in:
Arceliar 2018-06-07 14:13:31 -05:00
parent 00e4da28c7
commit f8ba80e7d8
2 changed files with 32 additions and 66 deletions

View File

@ -319,7 +319,6 @@ func (p *peer) handleSwitchMsg(packet []byte) {
panic("FIXME testing") panic("FIXME testing")
return return
} }
var info switchMessage
var loc switchLocator var loc switchLocator
prevKey := msg.Root prevKey := msg.Root
for idx, hop := range msg.Hops { for idx, hop := range msg.Hops {
@ -335,7 +334,7 @@ func (p *peer) handleSwitchMsg(packet []byte) {
} }
prevKey = hop.Next 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 // Pass a mesage to the dht informing it that this peer (still) exists
loc.coords = loc.coords[:len(loc.coords)-1] loc.coords = loc.coords[:len(loc.coords)-1]
dinfo := dhtInfo{ dinfo := dhtInfo{

View File

@ -113,18 +113,10 @@ type peerInfo struct {
key sigPubKey // ID of this peer key sigPubKey // ID of this peer
locator switchLocator // Should be able to respond with signatures upon request locator switchLocator // Should be able to respond with signatures upon request
degree uint64 // Self-reported degree 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 time time.Time // Time this node was last seen
firstSeen time.Time firstSeen time.Time
port switchPort // Interface number of this peer port switchPort // Interface number of this peer
seq uint64 // Seq number we last saw this peer advertise msg switchMsg // The wire switchMsg used
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
} }
type switchPort uint64 type switchPort uint64
@ -144,7 +136,6 @@ type switchData struct {
locator switchLocator locator switchLocator
seq uint64 // Sequence number, reported to peers, so they know about changes seq uint64 // Sequence number, reported to peers, so they know about changes
peers map[switchPort]peerInfo peers map[switchPort]peerInfo
sigs []sigInfo
msg *switchMsg msg *switchMsg
} }
@ -224,7 +215,6 @@ func (t *switchTable) cleanRoot() {
} }
} }
t.data.locator = switchLocator{root: t.key, tstamp: now.Unix()} t.data.locator = switchLocator{root: t.key, tstamp: now.Unix()}
t.data.sigs = nil
t.core.peers.sendSwitchMsgs() 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 { type switchMsg struct {
Root sigPubKey Root sigPubKey
TStamp int64 TStamp int64
@ -271,7 +252,7 @@ func (t *switchTable) getMsg() *switchMsg {
if t.parent == 0 { if t.parent == 0 {
return &switchMsg{Root: t.key, TStamp: t.data.locator.tstamp} return &switchMsg{Root: t.key, TStamp: t.data.locator.tstamp}
} else if parent, isIn := t.data.peers[t.parent]; isIn { } else if parent, isIn := t.data.peers[t.parent]; isIn {
msg := parent.smsg msg := parent.msg
msg.Hops = append([]switchMsgHop(nil), msg.Hops...) msg.Hops = append([]switchMsgHop(nil), msg.Hops...)
return &msg return &msg
} else { } 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 // TODO directly use a switchMsg instead of switchMessage + sigs
t.mutex.Lock() t.mutex.Lock()
defer t.mutex.Unlock() defer t.mutex.Unlock()
now := time.Now() now := time.Now()
// Set up the sender peerInfo
//* var sender peerInfo
var msg switchMessage sender.locator.root = msg.Root
var sigs []sigInfo sender.locator.tstamp = msg.TStamp
msg.locator.root = smsg.Root prevKey := msg.Root
msg.locator.tstamp = smsg.TStamp for _, hop := range msg.Hops {
msg.from = smsg.Root
prevKey := msg.from
for _, hop := range smsg.Hops {
// Build locator and signatures // Build locator and signatures
var sig sigInfo var sig sigInfo
sig.next = hop.Next sig.next = hop.Next
sig.sig = hop.Sig sig.sig = hop.Sig
sigs = append(sigs, sig) sender.locator.coords = append(sender.locator.coords, hop.Port)
msg.locator.coords = append(msg.locator.coords, hop.Port) sender.key = prevKey
msg.from = prevKey
prevKey = hop.Next prevKey = hop.Next
} }
msg.seq = uint64(now.Unix()) sender.msg = *msg
//*/
if len(msg.locator.coords) == 0 {
return
} // Should always have >=1 links
oldSender, isIn := t.data.peers[fromPort] oldSender, isIn := t.data.peers[fromPort]
if !isIn { if !isIn {
oldSender.firstSeen = now oldSender.firstSeen = now
} }
sender := peerInfo{key: msg.from, sender.firstSeen = oldSender.firstSeen
locator: msg.locator, sender.port = fromPort
coords: msg.locator.coords[:len(msg.locator.coords)-1], sender.time = now
time: now, // Decide what to do
firstSeen: oldSender.firstSeen,
port: fromPort,
seq: msg.seq,
smsg: *smsg}
equiv := func(x *switchLocator, y *switchLocator) bool { equiv := func(x *switchLocator, y *switchLocator) bool {
if x.root != y.root { if x.root != y.root {
return false return false
@ -335,7 +303,7 @@ func (t *switchTable) handleMsg(smsg *switchMsg, xmsg *switchMessage, fromPort s
return true return true
} }
doUpdate := false doUpdate := false
if !equiv(&msg.locator, &oldSender.locator) { if !equiv(&sender.locator, &oldSender.locator) {
doUpdate = true doUpdate = true
//sender.firstSeen = now // TODO? uncomment to prevent flapping? //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] oldParent, isIn := t.data.peers[t.parent]
noParent := !isIn noParent := !isIn
noLoop := func() bool { noLoop := func() bool {
for idx := 0; idx < len(sigs)-1; idx++ { for idx := 0; idx < len(msg.Hops)-1; idx++ {
if sigs[idx].next == t.core.sigPub { if msg.Hops[idx].Next == t.core.sigPub {
return false return false
} }
} }
if msg.locator.root == t.core.sigPub { if sender.locator.root == t.core.sigPub {
return false return false
} }
return true return true
@ -358,30 +326,30 @@ func (t *switchTable) handleMsg(smsg *switchMsg, xmsg *switchMessage, fromPort s
pTime := oldParent.time.Sub(oldParent.firstSeen) + switch_timeout pTime := oldParent.time.Sub(oldParent.firstSeen) + switch_timeout
// Really want to compare sLen/sTime and pLen/pTime // Really want to compare sLen/sTime and pLen/pTime
// Cross multiplied to avoid divide-by-zero // 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()) 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 // Here be dragons
switch { switch {
case !noLoop: // do nothing case !noLoop: // do nothing
case isIn && dropTstamp >= msg.locator.tstamp: // do nothing case isIn && dropTstamp >= sender.locator.tstamp: // do nothing
case firstIsBetter(&msg.locator.root, &t.data.locator.root): case firstIsBetter(&sender.locator.root, &t.data.locator.root):
updateRoot = true updateRoot = true
case t.data.locator.root != msg.locator.root: // do nothing case t.data.locator.root != sender.locator.root: // do nothing
case t.data.locator.tstamp > msg.locator.tstamp: // do nothing case t.data.locator.tstamp > sender.locator.tstamp: // do nothing
case noParent: case noParent:
updateRoot = true updateRoot = true
case cost < pCost: case cost < pCost:
updateRoot = true updateRoot = true
case sender.port != t.parent: // do nothing case sender.port != t.parent: // do nothing
case !equiv(&msg.locator, &t.data.locator): case !equiv(&sender.locator, &t.data.locator):
updateRoot = true updateRoot = true
case now.Sub(t.time) < switch_throttle: // do nothing 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 updateRoot = true
} }
if updateRoot { if updateRoot {
if !equiv(&msg.locator, &t.data.locator) { if !equiv(&sender.locator, &t.data.locator) {
doUpdate = true doUpdate = true
t.data.seq++ t.data.seq++
select { 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) //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) //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.time = now
} }
t.data.locator = msg.locator t.data.locator = sender.locator
t.parent = sender.port 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.log.Println("Switch update:", msg.Locator.Root, msg.Locator.Tstamp, msg.Locator.Coords)
t.core.peers.sendSwitchMsgs() t.core.peers.sendSwitchMsgs()
} }