From 8b3e97b96a63e899e6fd7c1cc9298b26daba8de1 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 8 Sep 2014 14:07:42 +0100 Subject: [PATCH] Change from stating which peer we expect to be on the end of a connection to accepting any known peer --- connection.go | 16 +++++++++------- connection_maker.go | 20 +++++++++----------- peer.go | 4 ++-- router.go | 2 +- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/connection.go b/connection.go index 0eb1bd63..138df0c6 100644 --- a/connection.go +++ b/connection.go @@ -51,7 +51,7 @@ func (conn *RemoteConnection) String() string { // Async. Does not return anything. If the connection is successful, // it will end up in the local peer's connections map. -func NewLocalConnection(connRemote *RemoteConnection, expectedName PeerName, tcpConn *net.TCPConn, udpAddr *net.UDPAddr, router *Router) { +func NewLocalConnection(connRemote *RemoteConnection, acceptNewPeer bool, tcpConn *net.TCPConn, udpAddr *net.UDPAddr, router *Router) { if connRemote.local != router.Ourself { log.Fatal("Attempt to create local connection from a peer which is not ourself") } @@ -65,7 +65,7 @@ func NewLocalConnection(connRemote *RemoteConnection, expectedName PeerName, tcp remoteUDPAddr: udpAddr, effectivePMTU: DefaultPMTU, queryChan: queryChan} - go connLocal.queryLoop(queryChan, expectedName) + go connLocal.queryLoop(queryChan, acceptNewPeer) } func (conn *LocalConnection) Established() bool { @@ -161,8 +161,8 @@ func (conn *LocalConnection) SendTCP(msg []byte) { // ACTOR server -func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction, expectedName PeerName) { - err := conn.handshake(expectedName) +func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction, acceptNewPeer bool) { + err := conn.handshake(acceptNewPeer) if err != nil { log.Printf("->[%s] encountered error during handshake: %v\n", conn.remoteTCPAddr, err) conn.handleShutdown() @@ -283,7 +283,7 @@ func (conn *LocalConnection) handleShutdown() { } } -func (conn *LocalConnection) handshake(expectedName PeerName) error { +func (conn *LocalConnection) handshake(acceptNewPeer bool) error { // We do not need to worry about locking in here as at this point, // the connection is not reachable by any go-routine other than // ourself. Only when we add this connection to the conn.local @@ -344,8 +344,10 @@ func (conn *LocalConnection) handshake(expectedName PeerName) error { if err != nil { return err } - if expectedName != UnknownPeerName && expectedName != name { - return fmt.Errorf("Unexpected remote name: expected %s, received %s", expectedName, name) + if !acceptNewPeer { + if _, found := conn.Router.Peers.Fetch(name); !found { + return fmt.Errorf("Found unknown remote name: %s at %s", name, conn.remoteTCPAddr) + } } uidStr, err := checkHandshakeStringField("UID", "", handshakeRecv) diff --git a/connection_maker.go b/connection_maker.go index 49254ed8..df7346df 100644 --- a/connection_maker.go +++ b/connection_maker.go @@ -78,20 +78,18 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio case query.code == CMConnSucceeded: cm.targets[query.foundAt].attempting = false case query.code == CMConnFailed: - cm.targets[query.foundAt].attempting = false + target := cm.targets[query.foundAt] + target.attempting = false + after, interval := tryAfter(target.tryInterval) + target.tryInterval = interval + target.tryAfter = after + maybeTick() default: log.Fatal("Unexpected connection maker query:", query) } case now := <-tick: for address, target := range cm.targets { if target.conn == nil && !target.attempting && now.After(target.tryAfter) { - if target.attemptCount == MaxAttemptCount { - // FIXME - continue - } - after, interval := tryAfter(target.tryInterval) - target.tryInterval = interval - target.tryAfter = after target.attemptCount += 1 target.attempting = true; go cm.attemptConnection(address, target.commandLine) @@ -130,7 +128,7 @@ func (cm *ConnectionMaker) status() string { if (target.conn != nil) { buf.WriteString(fmt.Sprintf("%s connected to: %v\n", address, target.conn)) } else if target.attempting { - buf.WriteString(fmt.Sprintf("%s (%v attempts, trying again now)\n", address, target.attemptCount)) + buf.WriteString(fmt.Sprintf("%s (%v attempts, trying since %v)\n", address, target.attemptCount, target.tryAfter)) } else { buf.WriteString(fmt.Sprintf("%s (%v attempts, next at %v)\n", address, target.attemptCount, target.tryAfter)) } @@ -139,9 +137,9 @@ func (cm *ConnectionMaker) status() string { } func (cm *ConnectionMaker) attemptConnection(address string, acceptNewPeer bool) { - log.Println("Attempting connection to ", address) + log.Println("Attempting connection to", address) var conncode = CMConnSucceeded - if err := cm.router.Ourself.CreateConnection(address, UnknownPeerName); err != nil { + if err := cm.router.Ourself.CreateConnection(address, acceptNewPeer); err != nil { log.Println(err) conncode = CMConnFailed } diff --git a/peer.go b/peer.go index b01a3053..b818a7b5 100644 --- a/peer.go +++ b/peer.go @@ -190,7 +190,7 @@ func (peer *Peer) StartLocalPeer() { go peer.queryLoop(queryChan) } -func (peer *Peer) CreateConnection(peerAddr string, expectedName PeerName) error { +func (peer *Peer) CreateConnection(peerAddr string, acceptNewPeer bool) error { if err := peer.checkConnectionLimit(); err != nil { return err } @@ -216,7 +216,7 @@ func (peer *Peer) CreateConnection(peerAddr string, expectedName PeerName) error return err } connRemote := NewRemoteConnection(peer, nil, tcpConn.RemoteAddr().String()) - NewLocalConnection(connRemote, expectedName, tcpConn, udpAddr, peer.Router) + NewLocalConnection(connRemote, acceptNewPeer, tcpConn, udpAddr, peer.Router) return nil } diff --git a/router.go b/router.go index 39a7bac6..ec2d9de1 100644 --- a/router.go +++ b/router.go @@ -154,7 +154,7 @@ func (router *Router) acceptTCP(tcpConn *net.TCPConn) { // on Port and we wait for them to send us something on UDP to // start. connRemote := NewRemoteConnection(router.Ourself, nil, tcpConn.RemoteAddr().String()) - NewLocalConnection(connRemote, UnknownPeerName, tcpConn, nil, router) + NewLocalConnection(connRemote, true, tcpConn, nil, router) } func (router *Router) listenUDP(localPort int, po PacketSink) *net.UDPConn {