From 00e4da28c74155c3b8310a0a1d702b1aea974b99 Mon Sep 17 00:00:00 2001 From: Arceliar Date: Thu, 7 Jun 2018 13:56:11 -0500 Subject: [PATCH] use/store switchMsg in the switch instead of going through the old switchMessage --- src/yggdrasil/peer.go | 58 +++++++++++++++++------------------------ src/yggdrasil/switch.go | 54 ++++++++++++++++++++++++++++++++++++-- src/yggdrasil/wire.go | 12 --------- 3 files changed, 76 insertions(+), 48 deletions(-) diff --git a/src/yggdrasil/peer.go b/src/yggdrasil/peer.go index 00f8a9c1..042702e5 100644 --- a/src/yggdrasil/peer.go +++ b/src/yggdrasil/peer.go @@ -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.Hops = append(msg.Hops, hop) + msg := p.core.switchTable.getMsg() + if msg == nil { + return } - 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 } diff --git a/src/yggdrasil/switch.go b/src/yggdrasil/switch.go index fec47af1..09083262 100644 --- a/src/yggdrasil/switch.go +++ b/src/yggdrasil/switch.go @@ -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 diff --git a/src/yggdrasil/wire.go b/src/yggdrasil/wire.go index be2fb4e6..305e5fca 100644 --- a/src/yggdrasil/wire.go +++ b/src/yggdrasil/wire.go @@ -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[:]...)