From c15976e4dc50e1a2c8308513bcbc426f98995e03 Mon Sep 17 00:00:00 2001 From: Arceliar Date: Mon, 12 Aug 2019 18:08:02 -0500 Subject: [PATCH 1/2] go.sum --- go.sum | 2 ++ 1 file changed, 2 insertions(+) diff --git a/go.sum b/go.sum index 0369a78..c786f5e 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,7 @@ github.com/yggdrasil-network/water v0.0.0-20190725073841-250edb919f8a h1:mQ0mPD+ github.com/yggdrasil-network/water v0.0.0-20190725073841-250edb919f8a/go.mod h1:R0SBCsugm+Sf1katgTb2t7GXMm+nRIv43tM4VDZbaOs= github.com/yggdrasil-network/water v0.0.0-20190725123504-a16161896c34 h1:Qh5FE+Q5iGqpmR/FPMYHuoZLN921au/nxAlmKe+Hdbo= github.com/yggdrasil-network/water v0.0.0-20190725123504-a16161896c34/go.mod h1:R0SBCsugm+Sf1katgTb2t7GXMm+nRIv43tM4VDZbaOs= +github.com/yggdrasil-network/water v0.0.0-20190812103929-c83fe40250f8 h1:YY9Pg2BEp0jeUVU60svTOaDr+fs1ySC9RbdC1Qc6wOw= github.com/yggdrasil-network/water v0.0.0-20190812103929-c83fe40250f8/go.mod h1:R0SBCsugm+Sf1katgTb2t7GXMm+nRIv43tM4VDZbaOs= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 h1:mKdxBk7AujPs8kU4m80U72y/zjbZ3UcXC7dClwKbUI0= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= @@ -45,6 +46,7 @@ golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3 h1:4y9KwBHBgBNwDbtu44R5o1fdOCQUEXhbk/P4A9WmJq0= golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190812073006-9eafafc0a87e h1:TsjK5I7fXk8f2FQrgu6NS7i5Qih3knl2FL1htyguLRE= golang.org/x/sys v0.0.0-20190812073006-9eafafc0a87e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= From b2cb1d965cd3951a224397a7a1d421c411e6fc8a Mon Sep 17 00:00:00 2001 From: Arceliar Date: Mon, 12 Aug 2019 18:22:30 -0500 Subject: [PATCH 2/2] avoid leaking sessions when no listener exists, or blocking if it's busy --- src/yggdrasil/session.go | 50 ++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/yggdrasil/session.go b/src/yggdrasil/session.go index da66ad5..179273e 100644 --- a/src/yggdrasil/session.go +++ b/src/yggdrasil/session.go @@ -348,40 +348,40 @@ func (ss *sessions) sendPingPong(sinfo *sessionInfo, isPong bool) { func (ss *sessions) handlePing(ping *sessionPing) { // Get the corresponding session (or create a new session) sinfo, isIn := ss.getByTheirPerm(&ping.SendPermPub) - // Check if the session is allowed - // TODO: this check may need to be moved - if !isIn && !ss.isSessionAllowed(&ping.SendPermPub, false) { - return - } - // Create the session if it doesn't already exist - if !isIn { - ss.createSession(&ping.SendPermPub) - sinfo, isIn = ss.getByTheirPerm(&ping.SendPermPub) - if !isIn { - panic("This should not happen") - } + switch { + case isIn: // Session already exists + case !ss.isSessionAllowed(&ping.SendPermPub, false): // Session is not allowed + case ping.IsPong: // This is a response, not an initial ping, so ignore it. + default: 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 { + if ss.listener != nil { + // This is a ping from an allowed node for which no session exists, and we have a listener ready to handle sessions. + // We need to create a session and pass it to the listener. + sinfo = ss.createSession(&ping.SendPermPub) + if s, _ := ss.getByTheirPerm(&ping.SendPermPub); s != sinfo { + panic("This should not happen") + } conn := newConn(ss.core, crypto.GetNodeID(&sinfo.theirPermPub), &crypto.NodeID{}, sinfo) for i := range conn.nodeMask { conn.nodeMask[i] = 0xFF } conn.session.startWorkers() - ss.listener.conn <- conn + c := ss.listener.conn + go func() { c <- conn }() } ss.listenerMutex.Unlock() } - sinfo.doFunc(func() { - // Update the session - if !sinfo.update(ping) { /*panic("Should not happen in testing")*/ - return - } - if !ping.IsPong { - ss.sendPingPong(sinfo, true) - } - }) + if sinfo != nil { + sinfo.doFunc(func() { + // Update the session + if !sinfo.update(ping) { /*panic("Should not happen in testing")*/ + return + } + if !ping.IsPong { + ss.sendPingPong(sinfo, true) + } + }) + } } // Get the MTU of the session.