diff --git a/router/connection.go b/router/connection.go index 66b11d08..fd118b59 100644 --- a/router/connection.go +++ b/router/connection.go @@ -16,6 +16,7 @@ type Connection interface { BreakTie(Connection) ConnectionTieBreak RemoteTCPAddr() string Established() bool + Outbound() bool Shutdown(error) Log(args ...interface{}) } @@ -33,6 +34,7 @@ type RemoteConnection struct { remote *Peer remoteTCPAddr string established bool + outbound bool } type LocalConnection struct { @@ -66,12 +68,13 @@ type ConnectionInteraction struct { payload interface{} } -func NewRemoteConnection(from, to *Peer, tcpAddr string, established bool) *RemoteConnection { +func NewRemoteConnection(from, to *Peer, tcpAddr string, established bool, outbound bool) *RemoteConnection { return &RemoteConnection{ local: from, remote: to, remoteTCPAddr: tcpAddr, - established: established} + established: established, + outbound: outbound} } func (conn *RemoteConnection) Local() *Peer { @@ -94,6 +97,10 @@ func (conn *RemoteConnection) Established() bool { return conn.established } +func (conn *RemoteConnection) Outbound() bool { + return conn.outbound +} + func (conn *RemoteConnection) Shutdown(error) { } diff --git a/router/connection_maker.go b/router/connection_maker.go index c236b54b..6f7781fb 100644 --- a/router/connection_maker.go +++ b/router/connection_maker.go @@ -140,7 +140,7 @@ func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration { // aren't cm.peers.ForEach(func(name PeerName, peer *Peer) { peer.ForEachConnection(func(otherPeer PeerName, conn Connection) { - if otherPeer == cm.ourself.Name || ourConnectedPeers[otherPeer] { + if otherPeer == cm.ourself.Name || ourConnectedPeers[otherPeer] || !conn.Outbound() { return } address := conn.RemoteTCPAddr() diff --git a/router/gossip_test.go b/router/gossip_test.go index cc8c6a9d..f93ea069 100644 --- a/router/gossip_test.go +++ b/router/gossip_test.go @@ -41,7 +41,7 @@ func (router *Router) AddTestChannelConnection(r *Router) { r.Peers.FetchWithDefault(fromPeer) // Has side-effect of incrementing refcount router.Peers.FetchWithDefault(toPeer) // - conn := &mockChannelConnection{RemoteConnection{router.Ourself.Peer, toPeer, "", false}, r} + conn := &mockChannelConnection{RemoteConnection{router.Ourself.Peer, toPeer, "", false, true}, r} router.Ourself.handleAddConnection(conn) router.Ourself.handleConnectionEstablished(conn) } diff --git a/router/local_peer.go b/router/local_peer.go index 97684ea0..120719b4 100644 --- a/router/local_peer.go +++ b/router/local_peer.go @@ -111,7 +111,7 @@ func (peer *LocalPeer) CreateConnection(peerAddr string, acceptNewPeer bool) err if err != nil { return err } - connRemote := NewRemoteConnection(peer.Peer, nil, tcpConn.RemoteAddr().String(), false) + connRemote := NewRemoteConnection(peer.Peer, nil, tcpConn.RemoteAddr().String(), false, true) connLocal := NewLocalConnection(connRemote, tcpConn, udpAddr, peer.Router) connLocal.Start(acceptNewPeer) return nil diff --git a/router/mocks_test.go b/router/mocks_test.go index 2380a0fe..369271b0 100644 --- a/router/mocks_test.go +++ b/router/mocks_test.go @@ -27,7 +27,7 @@ func (peers *Peers) AddTestRemoteConnection(p1, p2 *Peer) { toName := p2.Name toPeer := NewPeer(toName, "", p2.UID, 0) toPeer = peers.FetchWithDefault(toPeer) - peers.ourself.addConnection(&RemoteConnection{fromPeer, toPeer, "", false}) + peers.ourself.addConnection(&RemoteConnection{fromPeer, toPeer, "", false, false}) } func (peers *Peers) DeleteTestConnection(p *Peer) { @@ -44,7 +44,7 @@ func (peers *Peers) DeleteTestConnection(p *Peer) { // from what is created by the real code. func newMockConnection(from, to *Peer) Connection { type mockConnection struct{ RemoteConnection } - return &mockConnection{RemoteConnection{from, to, "", false}} + return &mockConnection{RemoteConnection{from, to, "", false, false}} } func checkEqualConns(t *testing.T, ourName PeerName, got, wanted map[PeerName]Connection) { diff --git a/router/peers.go b/router/peers.go index e8540a8a..2e1a1f6f 100644 --- a/router/peers.go +++ b/router/peers.go @@ -199,7 +199,7 @@ func (peers *Peers) decodeUpdate(update []byte) (newPeers map[PeerName]*Peer, de } for _, connsBuf := range decodedConns { - decErr := connsIterator(connsBuf, func(remoteNameByte []byte, _ string, _ bool) { + decErr := connsIterator(connsBuf, func(remoteNameByte []byte, _ string, _, _ bool) { remoteName := PeerNameFromBin(remoteNameByte) if _, found := newPeers[remoteName]; found { return @@ -268,6 +268,7 @@ func (peer *Peer) encode(enc *gob.Encoder) { checkFatal(connsEnc.Encode(conn.RemoteTCPAddr())) // DANGER holding rlock on peer, going to take rlock on conn checkFatal(connsEnc.Encode(conn.Established())) + checkFatal(connsEnc.Encode(conn.Outbound())) } checkFatal(enc.Encode(connsBuf.Bytes())) } @@ -291,7 +292,7 @@ func decodePeerNoConns(dec *gob.Decoder) (nameByte []byte, nickName string, uid return } -func connsIterator(input []byte, fun func([]byte, string, bool)) error { +func connsIterator(input []byte, fun func([]byte, string, bool, bool)) error { buf := new(bytes.Buffer) buf.Write(input) dec := gob.NewDecoder(buf) @@ -308,16 +309,20 @@ func connsIterator(input []byte, fun func([]byte, string, bool)) error { if err := dec.Decode(&established); err != nil { return err } - fun(nameByte, foundAt, established) + var outbound bool + if err := dec.Decode(&outbound); err != nil { + return err + } + fun(nameByte, foundAt, established, outbound) } } func readConnsMap(peer *Peer, buf []byte, table map[PeerName]*Peer) map[PeerName]Connection { conns := make(map[PeerName]Connection) - if err := connsIterator(buf, func(nameByte []byte, remoteTCPAddr string, established bool) { + if err := connsIterator(buf, func(nameByte []byte, remoteTCPAddr string, established bool, outbound bool) { name := PeerNameFromBin(nameByte) remotePeer := table[name] - conn := NewRemoteConnection(peer, remotePeer, remoteTCPAddr, established) + conn := NewRemoteConnection(peer, remotePeer, remoteTCPAddr, established, outbound) conns[name] = conn }); err != io.EOF { // this should never happen since we've already successfully diff --git a/router/protocol.go b/router/protocol.go index 9db4160f..9184512c 100644 --- a/router/protocol.go +++ b/router/protocol.go @@ -2,7 +2,7 @@ package router const ( Protocol = "weave" - ProtocolVersion = 13 + ProtocolVersion = 14 ) type ProtocolTag byte diff --git a/router/router.go b/router/router.go index a2bbc707..16a29eb3 100644 --- a/router/router.go +++ b/router/router.go @@ -192,7 +192,7 @@ func (router *Router) acceptTCP(tcpConn *net.TCPConn) { // start. remoteAddrStr := tcpConn.RemoteAddr().String() log.Printf("->[%s] connection accepted\n", remoteAddrStr) - connRemote := NewRemoteConnection(router.Ourself.Peer, nil, remoteAddrStr, false) + connRemote := NewRemoteConnection(router.Ourself.Peer, nil, remoteAddrStr, false, false) connLocal := NewLocalConnection(connRemote, tcpConn, nil, router) connLocal.Start(true) }