avoid leaking sessions when no listener exists, or blocking if it's busy

This commit is contained in:
Arceliar 2019-08-12 18:22:30 -05:00
parent c15976e4dc
commit b2cb1d965c

View File

@ -348,31 +348,30 @@ func (ss *sessions) sendPingPong(sinfo *sessionInfo, isPong bool) {
func (ss *sessions) handlePing(ping *sessionPing) { func (ss *sessions) handlePing(ping *sessionPing) {
// Get the corresponding session (or create a new session) // Get the corresponding session (or create a new session)
sinfo, isIn := ss.getByTheirPerm(&ping.SendPermPub) sinfo, isIn := ss.getByTheirPerm(&ping.SendPermPub)
// Check if the session is allowed switch {
// TODO: this check may need to be moved case isIn: // Session already exists
if !isIn && !ss.isSessionAllowed(&ping.SendPermPub, false) { case !ss.isSessionAllowed(&ping.SendPermPub, false): // Session is not allowed
return case ping.IsPong: // This is a response, not an initial ping, so ignore it.
} default:
// Create the session if it doesn't already exist ss.listenerMutex.Lock()
if !isIn { if ss.listener != nil {
ss.createSession(&ping.SendPermPub) // This is a ping from an allowed node for which no session exists, and we have a listener ready to handle sessions.
sinfo, isIn = ss.getByTheirPerm(&ping.SendPermPub) // We need to create a session and pass it to the listener.
if !isIn { sinfo = ss.createSession(&ping.SendPermPub)
if s, _ := ss.getByTheirPerm(&ping.SendPermPub); s != sinfo {
panic("This should not happen") panic("This should not happen")
} }
ss.listenerMutex.Lock()
// Check and see if there's a Listener waiting to accept connections
// TODO: this should not block if nothing is accepting
if !ping.IsPong && ss.listener != nil {
conn := newConn(ss.core, crypto.GetNodeID(&sinfo.theirPermPub), &crypto.NodeID{}, sinfo) conn := newConn(ss.core, crypto.GetNodeID(&sinfo.theirPermPub), &crypto.NodeID{}, sinfo)
for i := range conn.nodeMask { for i := range conn.nodeMask {
conn.nodeMask[i] = 0xFF conn.nodeMask[i] = 0xFF
} }
conn.session.startWorkers() conn.session.startWorkers()
ss.listener.conn <- conn c := ss.listener.conn
go func() { c <- conn }()
} }
ss.listenerMutex.Unlock() ss.listenerMutex.Unlock()
} }
if sinfo != nil {
sinfo.doFunc(func() { sinfo.doFunc(func() {
// Update the session // Update the session
if !sinfo.update(ping) { /*panic("Should not happen in testing")*/ if !sinfo.update(ping) { /*panic("Should not happen in testing")*/
@ -382,6 +381,7 @@ func (ss *sessions) handlePing(ping *sessionPing) {
ss.sendPingPong(sinfo, true) ss.sendPingPong(sinfo, true)
} }
}) })
}
} }
// Get the MTU of the session. // Get the MTU of the session.