mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-21 15:43:12 +01:00
Clarify XORMapped panic case (#877)
This commit is contained in:
parent
8a8c4bdddd
commit
8b78209ae5
@ -1,8 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/netbirdio/netbird/client/cmd"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/netbirdio/netbird/client/cmd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -209,7 +209,7 @@ func (m *UniversalUDPMuxDefault) GetXORMappedAddr(serverAddr net.Addr, deadline
|
|||||||
|
|
||||||
// otherwise, make a STUN request to discover the address
|
// otherwise, make a STUN request to discover the address
|
||||||
// or wait for already sent request to complete
|
// or wait for already sent request to complete
|
||||||
waitAddrReceived, err := m.sendStun(serverAddr)
|
waitAddrReceived, err := m.sendSTUN(serverAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s: %s", "failed to send STUN packet", err)
|
return nil, fmt.Errorf("%s: %s", "failed to send STUN packet", err)
|
||||||
}
|
}
|
||||||
@ -221,8 +221,10 @@ func (m *UniversalUDPMuxDefault) GetXORMappedAddr(serverAddr net.Addr, deadline
|
|||||||
var addr *stun.XORMappedAddress
|
var addr *stun.XORMappedAddress
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
// A very odd case that mappedAddr is nil.
|
// A very odd case that mappedAddr is nil.
|
||||||
// But can happen when the deadline property is larger than params.XORMappedAddrCacheTTL.
|
// Can happen when the deadline property is larger than params.XORMappedAddrCacheTTL.
|
||||||
// We protect the code from panic.
|
// Or when we don't receive a response to our m.sendSTUN request (the response is handled asynchronously) and
|
||||||
|
// the XORMapped expires meanwhile triggering a closure of the waitAddrReceived channel.
|
||||||
|
// We protect the code from panic here.
|
||||||
if mappedAddr, ok := m.xorMappedMap[serverAddr.String()]; ok {
|
if mappedAddr, ok := m.xorMappedMap[serverAddr.String()]; ok {
|
||||||
addr = mappedAddr.addr
|
addr = mappedAddr.addr
|
||||||
}
|
}
|
||||||
@ -236,11 +238,11 @@ func (m *UniversalUDPMuxDefault) GetXORMappedAddr(serverAddr net.Addr, deadline
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// sendStun sends a STUN request via UDP conn.
|
// sendSTUN sends a STUN request via UDP conn.
|
||||||
//
|
//
|
||||||
// The returned channel is closed when the STUN response has been received.
|
// The returned channel is closed when the STUN response has been received.
|
||||||
// Method is safe for concurrent use.
|
// Method is safe for concurrent use.
|
||||||
func (m *UniversalUDPMuxDefault) sendStun(serverAddr net.Addr) (chan struct{}, error) {
|
func (m *UniversalUDPMuxDefault) sendSTUN(serverAddr net.Addr) (chan struct{}, error) {
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user