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

use/store switchMsg in the switch instead of going through the old switchMessage

This commit is contained in:
Arceliar 2018-06-07 13:56:11 -05:00
parent 3dab94be9f
commit 00e4da28c7
3 changed files with 76 additions and 48 deletions

View File

@ -294,18 +294,11 @@ func (p *peer) handleLinkTraffic(bs []byte) {
} }
func (p *peer) sendSwitchMsg() { func (p *peer) sendSwitchMsg() {
info, sigs := p.core.switchTable.createMessage(p.port) msg := p.core.switchTable.getMsg()
var msg switchMsg if msg == nil {
msg.Root, msg.TStamp = info.locator.root, info.locator.tstamp return
for idx, sig := range sigs {
hop := switchMsgHop{
Port: info.locator.coords[idx],
Next: sig.next,
Sig: sig.sig,
} }
msg.Hops = append(msg.Hops, hop) bs := getBytesForSig(&p.sig, msg)
}
bs := getBytesForSig(&p.sig, &info.locator)
msg.Hops = append(msg.Hops, switchMsgHop{ msg.Hops = append(msg.Hops, switchMsgHop{
Port: p.port, Port: p.port,
Next: p.sig, Next: p.sig,
@ -313,6 +306,7 @@ func (p *peer) sendSwitchMsg() {
}) })
packet := msg.encode() packet := msg.encode()
//p.core.log.Println("Encoded msg:", msg, "; bytes:", packet) //p.core.log.Println("Encoded msg:", msg, "; bytes:", packet)
//fmt.Println("Encoded msg:", msg, "; bytes:", packet)
p.sendLinkPacket(packet) p.sendLinkPacket(packet)
} }
@ -326,44 +320,40 @@ func (p *peer) handleSwitchMsg(packet []byte) {
return return
} }
var info switchMessage var info switchMessage
var sigs []sigInfo var loc switchLocator
info.locator.root = msg.Root
info.locator.tstamp = msg.TStamp
prevKey := msg.Root prevKey := msg.Root
for _, hop := range msg.Hops { for idx, hop := range msg.Hops {
// Build locator and signatures // Check signatures and collect coords for dht
var sig sigInfo sigMsg := msg
sig.next = hop.Next sigMsg.Hops = msg.Hops[:idx]
sig.sig = hop.Sig loc.coords = append(loc.coords, hop.Port)
sigs = append(sigs, sig) bs := getBytesForSig(&hop.Next, &sigMsg)
info.locator.coords = append(info.locator.coords, hop.Port) if !p.core.sigs.check(&prevKey, &hop.Sig, bs) {
// Check signature
bs := getBytesForSig(&sig.next, &info.locator)
if !p.core.sigs.check(&prevKey, &sig.sig, bs) {
p.throttle++ p.throttle++
panic("FIXME testing") panic("FIXME testing")
return return
} }
prevKey = sig.next prevKey = hop.Next
} }
info.from = p.sig p.core.switchTable.handleMsg(&msg, &info, p.port)
info.seq = uint64(time.Now().Unix())
p.core.switchTable.handleMessage(&info, p.port, sigs)
// 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
l := info.locator loc.coords = loc.coords[:len(loc.coords)-1]
l.coords = l.coords[:len(l.coords)-1]
dinfo := dhtInfo{ dinfo := dhtInfo{
key: p.box, key: p.box,
coords: l.getCoords(), coords: loc.getCoords(),
} }
p.core.dht.peers <- &dinfo p.core.dht.peers <- &dinfo
p.dinfo = &dinfo p.dinfo = &dinfo
} }
func getBytesForSig(next *sigPubKey, loc *switchLocator) []byte { func getBytesForSig(next *sigPubKey, msg *switchMsg) []byte {
var loc switchLocator
for _, hop := range msg.Hops {
loc.coords = append(loc.coords, hop.Port)
}
bs := append([]byte(nil), next[:]...) bs := append([]byte(nil), next[:]...)
bs = append(bs, loc.root[:]...) bs = append(bs, msg.Root[:]...)
bs = append(bs, wire_encode_uint64(wire_intToUint(loc.tstamp))...) bs = append(bs, wire_encode_uint64(wire_intToUint(msg.TStamp))...)
bs = append(bs, wire_encode_coords(loc.getCoords())...) bs = append(bs, wire_encode_coords(loc.getCoords())...)
return bs return bs
} }

View File

@ -118,6 +118,7 @@ type peerInfo struct {
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 seq uint64 // Seq number we last saw this peer advertise
smsg switchMsg // The wire switchMsg used
} }
type switchMessage struct { type switchMessage struct {
@ -144,6 +145,7 @@ type switchData struct {
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 sigs []sigInfo
msg *switchMsg
} }
type switchTable struct { type switchTable struct {
@ -251,11 +253,58 @@ func (t *switchTable) createMessage(port switchPort) (*switchMessage, []sigInfo)
return &msg, t.data.sigs return &msg, t.data.sigs
} }
func (t *switchTable) handleMessage(msg *switchMessage, fromPort switchPort, sigs []sigInfo) { type switchMsg struct {
Root sigPubKey
TStamp int64
Hops []switchMsgHop
}
type switchMsgHop struct {
Port switchPort
Next sigPubKey
Sig sigBytes
}
func (t *switchTable) getMsg() *switchMsg {
t.mutex.RLock()
defer t.mutex.RUnlock()
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.Hops = append([]switchMsgHop(nil), msg.Hops...)
return &msg
} else {
return nil
}
}
func (t *switchTable) handleMsg(smsg *switchMsg, xmsg *switchMessage, 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()
//*
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 {
// 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
prevKey = hop.Next
}
msg.seq = uint64(now.Unix())
//*/
if len(msg.locator.coords) == 0 { if len(msg.locator.coords) == 0 {
return return
} // Should always have >=1 links } // Should always have >=1 links
@ -269,7 +318,8 @@ func (t *switchTable) handleMessage(msg *switchMessage, fromPort switchPort, sig
time: now, time: now,
firstSeen: oldSender.firstSeen, firstSeen: oldSender.firstSeen,
port: fromPort, port: fromPort,
seq: msg.seq} 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

View File

@ -115,18 +115,6 @@ func wire_decode_coords(packet []byte) ([]byte, int) {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
type switchMsg struct {
Root sigPubKey
TStamp int64
Hops []switchMsgHop
}
type switchMsgHop struct {
Port switchPort
Next sigPubKey
Sig sigBytes
}
func (m *switchMsg) encode() []byte { func (m *switchMsg) encode() []byte {
bs := wire_encode_uint64(wire_SwitchMsg) bs := wire_encode_uint64(wire_SwitchMsg)
bs = append(bs, m.Root[:]...) bs = append(bs, m.Root[:]...)