Change from stating which peer we expect to be on the end of a connection to accepting any known peer
This commit is contained in:
1 parent
0a94388f0a
commit
8b3e97b96a
4 files changed
+21
-21
No files matched your search
+9
-7
@@ -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)
|
||||
|
||||
+9
-11
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user