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

View File

@ -118,6 +118,7 @@ type peerInfo struct {
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 {
@ -144,6 +145,7 @@ type switchData struct {
seq uint64 // Sequence number, reported to peers, so they know about changes
peers map[switchPort]peerInfo
sigs []sigInfo
msg *switchMsg
}
type switchTable struct {
@ -251,11 +253,58 @@ func (t *switchTable) createMessage(port switchPort) (*switchMessage, []sigInfo)
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
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 {
// 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 {
return
} // Should always have >=1 links
@ -269,7 +318,8 @@ func (t *switchTable) handleMessage(msg *switchMessage, fromPort switchPort, sig
time: now,
firstSeen: oldSender.firstSeen,
port: fromPort,
seq: msg.seq}
seq: msg.seq,
smsg: *smsg}
equiv := func(x *switchLocator, y *switchLocator) bool {
if x.root != y.root {
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 {
bs := wire_encode_uint64(wire_SwitchMsg)
bs = append(bs, m.Root[:]...)