From a1ef2def2bcf18c2c805f9931f3a65272cf0a122 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Tue, 20 Jan 2015 21:41:08 +0000 Subject: [PATCH 1/7] refactor: extract pdu statics into separate file --- router/consts.go | 23 ++--------------------- router/protocol.go | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 router/protocol.go diff --git a/router/consts.go b/router/consts.go index 84baddd0..b15e24a4 100644 --- a/router/consts.go +++ b/router/consts.go @@ -6,8 +6,6 @@ import ( ) const ( - Protocol = "weave" - ProtocolVersion = 10 EthernetOverhead = 14 UDPOverhead = 28 // 20 bytes for IPv4, 8 bytes for UDP Port = 6783 @@ -28,24 +26,7 @@ const ( MaxDuration = time.Duration(math.MaxInt64) ) -const ( - ProtocolConnectionEstablished = iota - ProtocolFragmentationReceived - ProtocolStartFragmentationTest - ProtocolNonce - ProtocolFetchAll - ProtocolUpdate - ProtocolPMTUVerified -) - var ( - FragTest = make([]byte, FragTestSize) - PMTUDiscovery = make([]byte, PMTUDiscoverySize) - ProtocolConnectionEstablishedByte = []byte{ProtocolConnectionEstablished} - ProtocolFragmentationReceivedByte = []byte{ProtocolFragmentationReceived} - ProtocolStartFragmentationTestByte = []byte{ProtocolStartFragmentationTest} - ProtocolNonceByte = []byte{ProtocolNonce} - ProtocolFetchAllByte = []byte{ProtocolFetchAll} - ProtocolUpdateByte = []byte{ProtocolUpdate} - ProtocolPMTUVerifiedByte = []byte{ProtocolPMTUVerified} + FragTest = make([]byte, FragTestSize) + PMTUDiscovery = make([]byte, PMTUDiscoverySize) ) diff --git a/router/protocol.go b/router/protocol.go new file mode 100644 index 00000000..05c36a45 --- /dev/null +++ b/router/protocol.go @@ -0,0 +1,26 @@ +package router + +const ( + Protocol = "weave" + ProtocolVersion = 10 +) + +const ( + ProtocolConnectionEstablished = iota + ProtocolFragmentationReceived + ProtocolStartFragmentationTest + ProtocolNonce + ProtocolFetchAll + ProtocolUpdate + ProtocolPMTUVerified +) + +var ( + ProtocolConnectionEstablishedByte = []byte{ProtocolConnectionEstablished} + ProtocolFragmentationReceivedByte = []byte{ProtocolFragmentationReceived} + ProtocolStartFragmentationTestByte = []byte{ProtocolStartFragmentationTest} + ProtocolNonceByte = []byte{ProtocolNonce} + ProtocolFetchAllByte = []byte{ProtocolFetchAll} + ProtocolUpdateByte = []byte{ProtocolUpdate} + ProtocolPMTUVerifiedByte = []byte{ProtocolPMTUVerified} +) From 644697977b84264cc3e6964b74e8473edcdf9ed2 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Tue, 20 Jan 2015 21:51:18 +0000 Subject: [PATCH 2/7] refactor: make protocol message tags a separate type --- router/connection.go | 2 +- router/protocol.go | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/router/connection.go b/router/connection.go index e2c77bd1..8eb1625f 100644 --- a/router/connection.go +++ b/router/connection.go @@ -530,7 +530,7 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder) { continue } payload := msg[1:] - switch msg[0] { + switch ProtocolMsg(msg[0]) { case ProtocolConnectionEstablished: // We sent fast heartbeats to the remote peer, which has // now received at least one of them and told us via this diff --git a/router/protocol.go b/router/protocol.go index 05c36a45..9fffe7f5 100644 --- a/router/protocol.go +++ b/router/protocol.go @@ -5,8 +5,10 @@ const ( ProtocolVersion = 10 ) +type ProtocolMsg byte + const ( - ProtocolConnectionEstablished = iota + ProtocolConnectionEstablished ProtocolMsg = iota ProtocolFragmentationReceived ProtocolStartFragmentationTest ProtocolNonce @@ -16,11 +18,11 @@ const ( ) var ( - ProtocolConnectionEstablishedByte = []byte{ProtocolConnectionEstablished} - ProtocolFragmentationReceivedByte = []byte{ProtocolFragmentationReceived} - ProtocolStartFragmentationTestByte = []byte{ProtocolStartFragmentationTest} - ProtocolNonceByte = []byte{ProtocolNonce} - ProtocolFetchAllByte = []byte{ProtocolFetchAll} - ProtocolUpdateByte = []byte{ProtocolUpdate} - ProtocolPMTUVerifiedByte = []byte{ProtocolPMTUVerified} + ProtocolConnectionEstablishedByte = []byte{byte(ProtocolConnectionEstablished)} + ProtocolFragmentationReceivedByte = []byte{byte(ProtocolFragmentationReceived)} + ProtocolStartFragmentationTestByte = []byte{byte(ProtocolStartFragmentationTest)} + ProtocolNonceByte = []byte{byte(ProtocolNonce)} + ProtocolFetchAllByte = []byte{byte(ProtocolFetchAll)} + ProtocolUpdateByte = []byte{byte(ProtocolUpdate)} + ProtocolPMTUVerifiedByte = []byte{byte(ProtocolPMTUVerified)} ) From 1181a0cb33e34cfd005356609f535620635843bb Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Tue, 20 Jan 2015 22:31:16 +0000 Subject: [PATCH 3/7] better APIs for sending protocol messages no longer tagging requiring the caller to construct them --- router/connection.go | 36 ++++++++++++++++++++---------------- router/crypto.go | 6 +++--- router/local_peer.go | 19 ++++++++++--------- router/protocol.go | 15 +++++---------- router/router.go | 4 ++-- 5 files changed, 40 insertions(+), 40 deletions(-) diff --git a/router/connection.go b/router/connection.go index 8eb1625f..08c742c9 100644 --- a/router/connection.go +++ b/router/connection.go @@ -167,7 +167,7 @@ func (conn *LocalConnection) log(args ...interface{}) { // ACTOR client API const ( - CSendTCP = iota + CSendProtocolMsg = iota CSetEstablished CReceivedHeartbeat CShutdown @@ -200,10 +200,10 @@ func (conn *LocalConnection) SetEstablished() { } // Async -func (conn *LocalConnection) SendTCP(msg []byte) { +func (conn *LocalConnection) SendProtocolMsg(tag ProtocolMsg, msg []byte) { conn.queryChan <- &ConnectionInteraction{ - Interaction: Interaction{code: CSendTCP}, - payload: msg} + Interaction: Interaction{code: CSendProtocolMsg}, + payload: TaggedProtocolMsg{tag, msg}} } // ACTOR server @@ -265,16 +265,16 @@ func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction) err = conn.handleReceivedHeartbeat(query.payload.(*net.UDPAddr)) case CSetEstablished: err = conn.handleSetEstablished() - case CSendTCP: - err = conn.handleSendTCP(query.payload.([]byte)) + case CSendProtocolMsg: + err = conn.handleSendProtocolMsg(query.payload.(TaggedProtocolMsg)) } case <-tickerChan(conn.heartbeat): conn.Forward(true, conn.heartbeatFrame, nil) case <-tickerChan(conn.fetchAll): - err = conn.handleSendTCP(ProtocolFetchAllByte) + err = conn.handleSendSimpleProtocolMsg(ProtocolFetchAll) case <-tickerChan(conn.fragTest): conn.setStackFrag(false) - err = conn.handleSendTCP(ProtocolStartFragmentationTestByte) + err = conn.handleSendSimpleProtocolMsg(ProtocolStartFragmentationTest) } } return @@ -295,7 +295,7 @@ func (conn *LocalConnection) handleReceivedHeartbeat(remoteUDPAddr *net.UDPAddr) conn.receivedHeartbeat = true conn.Unlock() if !old { - if err := conn.handleSendTCP(ProtocolConnectionEstablishedByte); err != nil { + if err := conn.handleSendSimpleProtocolMsg(ProtocolConnectionEstablished); err != nil { return err } } @@ -332,18 +332,22 @@ func (conn *LocalConnection) handleSetEstablished() error { conn.fragTest = time.NewTicker(FragTestInterval) // avoid initial waits for timers to fire conn.Forward(true, conn.heartbeatFrame, nil) - if err := conn.handleSendTCP(ProtocolFetchAllByte); err != nil { + if err := conn.handleSendSimpleProtocolMsg(ProtocolFetchAll); err != nil { return err } conn.setStackFrag(false) - if err := conn.handleSendTCP(ProtocolStartFragmentationTestByte); err != nil { + if err := conn.handleSendSimpleProtocolMsg(ProtocolStartFragmentationTest); err != nil { return err } return nil } -func (conn *LocalConnection) handleSendTCP(msg []byte) error { - return conn.tcpSender.Send(msg) +func (conn *LocalConnection) handleSendSimpleProtocolMsg(tag ProtocolMsg) error { + return conn.handleSendProtocolMsg(TaggedProtocolMsg{tag: tag}) +} + +func (conn *LocalConnection) handleSendProtocolMsg(m TaggedProtocolMsg) error { + return conn.tcpSender.Send(Concat([]byte{byte(m.tag)}, m.msg)) } func (conn *LocalConnection) handleShutdown() { @@ -564,13 +568,13 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder) { // payload is a subset of the receiver's topology, no // further action is taken. Otherwise, the receiver sends // out to all its connections an "improved" update. - conn.SendTCP(Concat(ProtocolUpdateByte, conn.Router.Peers.EncodeAllPeers())) + conn.SendProtocolMsg(ProtocolUpdate, conn.Router.Peers.EncodeAllPeers()) case ProtocolUpdate: newUpdate, err := conn.Router.Peers.ApplyUpdate(payload) if _, ok := err.(UnknownPeersError); err != nil && ok { // That update contained a peer we didn't know about; // request full update - conn.SendTCP(ProtocolFetchAllByte) + conn.SendProtocolMsg(ProtocolFetchAll, nil) continue } if conn.CheckFatal(err) != nil { @@ -579,7 +583,7 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder) { if len(newUpdate) != 0 { conn.Router.ConnectionMaker.Refresh() conn.Router.Routes.Recalculate() - conn.Router.Ourself.BroadcastTCP(Concat(ProtocolUpdateByte, newUpdate)) + conn.Router.Ourself.SendProtocolMsg(ProtocolUpdate, newUpdate) } case ProtocolPMTUVerified: conn.verifyPMTU <- int(binary.BigEndian.Uint16(payload)) diff --git a/router/crypto.go b/router/crypto.go index ef9c1b4b..0bb7dd14 100644 --- a/router/crypto.go +++ b/router/crypto.go @@ -85,7 +85,7 @@ func EncodeNonce(df bool) (*[24]byte, []byte, error) { flags = flags | 1 } SetNonceLow15Bits(&nonce, flags) - return &nonce, Concat(ProtocolNonceByte, nonce[:]), nil + return &nonce, nonce[:], nil } func DecodeNonce(msg []byte) (bool, *[24]byte) { @@ -206,7 +206,7 @@ func (ne *NaClEncryptor) Bytes() []byte { if err = ne.conn.CheckFatal(err); err != nil { return []byte{} } - ne.conn.SendTCP(encodedNonce) + ne.conn.SendProtocolMsg(ProtocolNonce, encodedNonce) ne.nonce = freshNonce nonce = freshNonce } @@ -225,7 +225,7 @@ func (ne *NaClEncryptor) Bytes() []byte { return []byte{} } ne.nonceChan <- nonce - ne.conn.SendTCP(encodedNonce) + ne.conn.SendProtocolMsg(ProtocolNonce, encodedNonce) } ne.offset = offset diff --git a/router/local_peer.go b/router/local_peer.go index 9c85959f..62bbeb7b 100644 --- a/router/local_peer.go +++ b/router/local_peer.go @@ -117,9 +117,9 @@ func (peer *LocalPeer) CreateConnection(peerAddr string, acceptNewPeer bool) err const ( PAddConnection = iota - PBroadcastTCP PDeleteConnection PConnectionEstablished + PSendProtocolMsg ) // Async: rely on the peer to shut us down if we shouldn't be adding @@ -147,10 +147,10 @@ func (peer *LocalPeer) ConnectionEstablished(conn *LocalConnection) { } // Async. -func (peer *LocalPeer) BroadcastTCP(msg []byte) { +func (peer *LocalPeer) SendProtocolMsg(tag ProtocolMsg, msg []byte) { peer.queryChan <- &PeerInteraction{ - Interaction: Interaction{code: PBroadcastTCP}, - payload: msg} + Interaction: Interaction{code: PSendProtocolMsg}, + payload: TaggedProtocolMsg{tag, msg}} } // ACTOR server @@ -169,8 +169,8 @@ func (peer *LocalPeer) queryLoop(queryChan <-chan *PeerInteraction) { query.resultChan <- nil case PConnectionEstablished: peer.handleConnectionEstablished(query.payload.(*LocalConnection)) - case PBroadcastTCP: - peer.handleBroadcastTCP(query.payload.([]byte)) + case PSendProtocolMsg: + peer.handleSendProtocolMsg(query.payload.(TaggedProtocolMsg)) } } } @@ -248,9 +248,9 @@ func (peer *LocalPeer) handleConnectionEstablished(conn *LocalConnection) { peer.broadcastPeerUpdate(conn.Remote()) } -func (peer *LocalPeer) handleBroadcastTCP(msg []byte) { +func (peer *LocalPeer) handleSendProtocolMsg(m TaggedProtocolMsg) { peer.ForEachConnection(func(_ PeerName, conn Connection) { - conn.(*LocalConnection).SendTCP(msg) + conn.(*LocalConnection).SendProtocolMsg(m.tag, m.msg) }) } @@ -278,7 +278,8 @@ func (peer *LocalPeer) connectionEstablished(conn Connection) { func (peer *LocalPeer) broadcastPeerUpdate(peers ...*Peer) { peer.Router.Routes.Recalculate() - peer.handleBroadcastTCP(Concat(ProtocolUpdateByte, EncodePeers(append(peers, peer.Peer)...))) + update := EncodePeers(append(peers, peer.Peer)...) + peer.handleSendProtocolMsg(TaggedProtocolMsg{tag: ProtocolUpdate, msg: update}) } func (peer *LocalPeer) checkConnectionLimit() error { diff --git a/router/protocol.go b/router/protocol.go index 9fffe7f5..df22d918 100644 --- a/router/protocol.go +++ b/router/protocol.go @@ -7,6 +7,11 @@ const ( type ProtocolMsg byte +type TaggedProtocolMsg struct { + tag ProtocolMsg + msg []byte +} + const ( ProtocolConnectionEstablished ProtocolMsg = iota ProtocolFragmentationReceived @@ -16,13 +21,3 @@ const ( ProtocolUpdate ProtocolPMTUVerified ) - -var ( - ProtocolConnectionEstablishedByte = []byte{byte(ProtocolConnectionEstablished)} - ProtocolFragmentationReceivedByte = []byte{byte(ProtocolFragmentationReceived)} - ProtocolStartFragmentationTestByte = []byte{byte(ProtocolStartFragmentationTest)} - ProtocolNonceByte = []byte{byte(ProtocolNonce)} - ProtocolFetchAllByte = []byte{byte(ProtocolFetchAll)} - ProtocolUpdateByte = []byte{byte(ProtocolUpdate)} - ProtocolPMTUVerifiedByte = []byte{byte(ProtocolPMTUVerified)} -) diff --git a/router/router.go b/router/router.go index d9e31687..33c03f51 100644 --- a/router/router.go +++ b/router/router.go @@ -308,12 +308,12 @@ func (router *Router) handleUDPPacketFunc(dec *EthernetDecoder, po PacketSink) F case frameLen == EthernetOverhead+8: relayConn.ReceivedHeartbeat(sender, binary.BigEndian.Uint64(frame[EthernetOverhead:])) case frameLen == FragTestSize && bytes.Equal(frame, FragTest): - relayConn.SendTCP(ProtocolFragmentationReceivedByte) + relayConn.SendProtocolMsg(ProtocolFragmentationReceived, nil) case frameLen == PMTUDiscoverySize && bytes.Equal(frame, PMTUDiscovery): default: frameLenBytes := []byte{0, 0} binary.BigEndian.PutUint16(frameLenBytes, uint16(frameLen-EthernetOverhead)) - relayConn.SendTCP(Concat(ProtocolPMTUVerifiedByte, frameLenBytes)) + relayConn.SendProtocolMsg(ProtocolPMTUVerified, frameLenBytes) } return nil } From d089900632771a2c1b79129ad91d3f1894e44172 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Tue, 20 Jan 2015 23:22:44 +0000 Subject: [PATCH 4/7] fix crypto --- router/crypto.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/router/crypto.go b/router/crypto.go index 0bb7dd14..c8cd1e8e 100644 --- a/router/crypto.go +++ b/router/crypto.go @@ -85,7 +85,8 @@ func EncodeNonce(df bool) (*[24]byte, []byte, error) { flags = flags | 1 } SetNonceLow15Bits(&nonce, flags) - return &nonce, nonce[:], nil + // NB: need to make a copy since callers may modify the array + return &nonce, Concat(nonce[:]), nil } func DecodeNonce(msg []byte) (bool, *[24]byte) { From ac95d5ab294dfa4122fbb72f1eaa21a327d2e49f Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Tue, 20 Jan 2015 23:23:59 +0000 Subject: [PATCH 5/7] rename and lift ProtocolMsg into API --- router/connection.go | 20 ++++++++++---------- router/crypto.go | 4 ++-- router/local_peer.go | 13 ++++++------- router/protocol.go | 8 ++++---- router/router.go | 4 ++-- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/router/connection.go b/router/connection.go index 08c742c9..f4cc3762 100644 --- a/router/connection.go +++ b/router/connection.go @@ -200,10 +200,10 @@ func (conn *LocalConnection) SetEstablished() { } // Async -func (conn *LocalConnection) SendProtocolMsg(tag ProtocolMsg, msg []byte) { +func (conn *LocalConnection) SendProtocolMsg(m ProtocolMsg) { conn.queryChan <- &ConnectionInteraction{ Interaction: Interaction{code: CSendProtocolMsg}, - payload: TaggedProtocolMsg{tag, msg}} + payload: m} } // ACTOR server @@ -266,7 +266,7 @@ func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction) case CSetEstablished: err = conn.handleSetEstablished() case CSendProtocolMsg: - err = conn.handleSendProtocolMsg(query.payload.(TaggedProtocolMsg)) + err = conn.handleSendProtocolMsg(query.payload.(ProtocolMsg)) } case <-tickerChan(conn.heartbeat): conn.Forward(true, conn.heartbeatFrame, nil) @@ -342,11 +342,11 @@ func (conn *LocalConnection) handleSetEstablished() error { return nil } -func (conn *LocalConnection) handleSendSimpleProtocolMsg(tag ProtocolMsg) error { - return conn.handleSendProtocolMsg(TaggedProtocolMsg{tag: tag}) +func (conn *LocalConnection) handleSendSimpleProtocolMsg(tag ProtocolTag) error { + return conn.handleSendProtocolMsg(ProtocolMsg{tag: tag}) } -func (conn *LocalConnection) handleSendProtocolMsg(m TaggedProtocolMsg) error { +func (conn *LocalConnection) handleSendProtocolMsg(m ProtocolMsg) error { return conn.tcpSender.Send(Concat([]byte{byte(m.tag)}, m.msg)) } @@ -534,7 +534,7 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder) { continue } payload := msg[1:] - switch ProtocolMsg(msg[0]) { + switch ProtocolTag(msg[0]) { case ProtocolConnectionEstablished: // We sent fast heartbeats to the remote peer, which has // now received at least one of them and told us via this @@ -568,13 +568,13 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder) { // payload is a subset of the receiver's topology, no // further action is taken. Otherwise, the receiver sends // out to all its connections an "improved" update. - conn.SendProtocolMsg(ProtocolUpdate, conn.Router.Peers.EncodeAllPeers()) + conn.SendProtocolMsg(ProtocolMsg{ProtocolUpdate, conn.Router.Peers.EncodeAllPeers()}) case ProtocolUpdate: newUpdate, err := conn.Router.Peers.ApplyUpdate(payload) if _, ok := err.(UnknownPeersError); err != nil && ok { // That update contained a peer we didn't know about; // request full update - conn.SendProtocolMsg(ProtocolFetchAll, nil) + conn.SendProtocolMsg(ProtocolMsg{ProtocolFetchAll, nil}) continue } if conn.CheckFatal(err) != nil { @@ -583,7 +583,7 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder) { if len(newUpdate) != 0 { conn.Router.ConnectionMaker.Refresh() conn.Router.Routes.Recalculate() - conn.Router.Ourself.SendProtocolMsg(ProtocolUpdate, newUpdate) + conn.Router.Ourself.SendProtocolMsg(ProtocolMsg{ProtocolUpdate, newUpdate}) } case ProtocolPMTUVerified: conn.verifyPMTU <- int(binary.BigEndian.Uint16(payload)) diff --git a/router/crypto.go b/router/crypto.go index c8cd1e8e..8b57722c 100644 --- a/router/crypto.go +++ b/router/crypto.go @@ -207,7 +207,7 @@ func (ne *NaClEncryptor) Bytes() []byte { if err = ne.conn.CheckFatal(err); err != nil { return []byte{} } - ne.conn.SendProtocolMsg(ProtocolNonce, encodedNonce) + ne.conn.SendProtocolMsg(ProtocolMsg{ProtocolNonce, encodedNonce}) ne.nonce = freshNonce nonce = freshNonce } @@ -226,7 +226,7 @@ func (ne *NaClEncryptor) Bytes() []byte { return []byte{} } ne.nonceChan <- nonce - ne.conn.SendProtocolMsg(ProtocolNonce, encodedNonce) + ne.conn.SendProtocolMsg(ProtocolMsg{ProtocolNonce, encodedNonce}) } ne.offset = offset diff --git a/router/local_peer.go b/router/local_peer.go index 62bbeb7b..9d0356f2 100644 --- a/router/local_peer.go +++ b/router/local_peer.go @@ -147,10 +147,10 @@ func (peer *LocalPeer) ConnectionEstablished(conn *LocalConnection) { } // Async. -func (peer *LocalPeer) SendProtocolMsg(tag ProtocolMsg, msg []byte) { +func (peer *LocalPeer) SendProtocolMsg(m ProtocolMsg) { peer.queryChan <- &PeerInteraction{ Interaction: Interaction{code: PSendProtocolMsg}, - payload: TaggedProtocolMsg{tag, msg}} + payload: m} } // ACTOR server @@ -170,7 +170,7 @@ func (peer *LocalPeer) queryLoop(queryChan <-chan *PeerInteraction) { case PConnectionEstablished: peer.handleConnectionEstablished(query.payload.(*LocalConnection)) case PSendProtocolMsg: - peer.handleSendProtocolMsg(query.payload.(TaggedProtocolMsg)) + peer.handleSendProtocolMsg(query.payload.(ProtocolMsg)) } } } @@ -248,9 +248,9 @@ func (peer *LocalPeer) handleConnectionEstablished(conn *LocalConnection) { peer.broadcastPeerUpdate(conn.Remote()) } -func (peer *LocalPeer) handleSendProtocolMsg(m TaggedProtocolMsg) { +func (peer *LocalPeer) handleSendProtocolMsg(m ProtocolMsg) { peer.ForEachConnection(func(_ PeerName, conn Connection) { - conn.(*LocalConnection).SendProtocolMsg(m.tag, m.msg) + conn.(*LocalConnection).SendProtocolMsg(m) }) } @@ -278,8 +278,7 @@ func (peer *LocalPeer) connectionEstablished(conn Connection) { func (peer *LocalPeer) broadcastPeerUpdate(peers ...*Peer) { peer.Router.Routes.Recalculate() - update := EncodePeers(append(peers, peer.Peer)...) - peer.handleSendProtocolMsg(TaggedProtocolMsg{tag: ProtocolUpdate, msg: update}) + peer.handleSendProtocolMsg(ProtocolMsg{ProtocolUpdate, EncodePeers(append(peers, peer.Peer)...)}) } func (peer *LocalPeer) checkConnectionLimit() error { diff --git a/router/protocol.go b/router/protocol.go index df22d918..455c561d 100644 --- a/router/protocol.go +++ b/router/protocol.go @@ -5,15 +5,15 @@ const ( ProtocolVersion = 10 ) -type ProtocolMsg byte +type ProtocolTag byte -type TaggedProtocolMsg struct { - tag ProtocolMsg +type ProtocolMsg struct { + tag ProtocolTag msg []byte } const ( - ProtocolConnectionEstablished ProtocolMsg = iota + ProtocolConnectionEstablished ProtocolTag = iota ProtocolFragmentationReceived ProtocolStartFragmentationTest ProtocolNonce diff --git a/router/router.go b/router/router.go index 33c03f51..bc6170b3 100644 --- a/router/router.go +++ b/router/router.go @@ -308,12 +308,12 @@ func (router *Router) handleUDPPacketFunc(dec *EthernetDecoder, po PacketSink) F case frameLen == EthernetOverhead+8: relayConn.ReceivedHeartbeat(sender, binary.BigEndian.Uint64(frame[EthernetOverhead:])) case frameLen == FragTestSize && bytes.Equal(frame, FragTest): - relayConn.SendProtocolMsg(ProtocolFragmentationReceived, nil) + relayConn.SendProtocolMsg(ProtocolMsg{ProtocolFragmentationReceived, nil}) case frameLen == PMTUDiscoverySize && bytes.Equal(frame, PMTUDiscovery): default: frameLenBytes := []byte{0, 0} binary.BigEndian.PutUint16(frameLenBytes, uint16(frameLen-EthernetOverhead)) - relayConn.SendProtocolMsg(ProtocolPMTUVerified, frameLenBytes) + relayConn.SendProtocolMsg(ProtocolMsg{ProtocolPMTUVerified, frameLenBytes}) } return nil } From 76e44afdead5867d1757813535c05f11f5084026 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Tue, 20 Jan 2015 23:36:02 +0000 Subject: [PATCH 6/7] cosmetic --- router/protocol.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/router/protocol.go b/router/protocol.go index 455c561d..1d8cd104 100644 --- a/router/protocol.go +++ b/router/protocol.go @@ -7,11 +7,6 @@ const ( type ProtocolTag byte -type ProtocolMsg struct { - tag ProtocolTag - msg []byte -} - const ( ProtocolConnectionEstablished ProtocolTag = iota ProtocolFragmentationReceived @@ -21,3 +16,8 @@ const ( ProtocolUpdate ProtocolPMTUVerified ) + +type ProtocolMsg struct { + tag ProtocolTag + msg []byte +} From c6f5e7ec9474f20953860757d05237f9cfcec6d5 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Tue, 20 Jan 2015 23:41:42 +0000 Subject: [PATCH 7/7] introduce ProtocolSender interface --- router/local_peer.go | 2 +- router/protocol.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/router/local_peer.go b/router/local_peer.go index 9d0356f2..1b1160f5 100644 --- a/router/local_peer.go +++ b/router/local_peer.go @@ -250,7 +250,7 @@ func (peer *LocalPeer) handleConnectionEstablished(conn *LocalConnection) { func (peer *LocalPeer) handleSendProtocolMsg(m ProtocolMsg) { peer.ForEachConnection(func(_ PeerName, conn Connection) { - conn.(*LocalConnection).SendProtocolMsg(m) + conn.(ProtocolSender).SendProtocolMsg(m) }) } diff --git a/router/protocol.go b/router/protocol.go index 1d8cd104..2218d813 100644 --- a/router/protocol.go +++ b/router/protocol.go @@ -21,3 +21,7 @@ type ProtocolMsg struct { tag ProtocolTag msg []byte } + +type ProtocolSender interface { + SendProtocolMsg(m ProtocolMsg) +}