Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify XORMapped panic case #877

Merged
merged 2 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix doc
  • Loading branch information
braginini committed May 18, 2023
commit fe8c33463c583a55f7eb54836f2cc28ddb88f00e
45 changes: 4 additions & 41 deletions client/main.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,13 @@
package main

import (
"context"
"fmt"
"math/rand"
"net"
"time"
"os"

"github.com/netbirdio/netbird/iface/bind"
"github.com/netbirdio/netbird/client/cmd"
)

func main() {

udp, err := net.ListenUDP("udp4", nil)
if err != nil {
panic(err)
}
muxDefault := bind.NewUniversalUDPMuxDefault(bind.UniversalUDPMuxParams{UDPConn: udp, XORMappedAddrCacheTTL: 25 * time.Second})

go func() {
muxDefault.ReadFromConn(context.TODO())
}()

addr, err := net.ResolveUDPAddr("udp4", "18.198.11.240:5555")
if err != nil {
panic(err)
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
for i := 0; i < 2; i++ {
go func() {
millis := rand.Intn(400) + 50
time.Sleep(time.Duration(millis) * time.Millisecond)

for {
a, err := muxDefault.GetXORMappedAddr(addr, 3*time.Second)
if err != nil {
//fmt.Println(time.Now())
fmt.Println(time.Now().String() + ": " + err.Error())
continue
}
//fmt.Println(time.Now())
fmt.Println(a)
/*millis = rand.Intn(1000-300) + 300
time.Sleep(time.Duration(millis) * time.Millisecond)*/
}
}()
}

select {}
}
12 changes: 7 additions & 5 deletions iface/bind/udp_mux_universal.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (m *UniversalUDPMuxDefault) GetXORMappedAddr(serverAddr net.Addr, deadline

// otherwise, make a STUN request to discover the address
// or wait for already sent request to complete
waitAddrReceived, err := m.sendStun(serverAddr)
waitAddrReceived, err := m.sendSTUN(serverAddr)
if err != nil {
return nil, fmt.Errorf("%s: %s", "failed to send STUN packet", err)
}
Expand All @@ -221,8 +221,10 @@ func (m *UniversalUDPMuxDefault) GetXORMappedAddr(serverAddr net.Addr, deadline
var addr *stun.XORMappedAddress
m.mu.Lock()
// A very odd case that mappedAddr is nil.
// But can happen when the deadline property is larger than params.XORMappedAddrCacheTTL.
// We protect the code from panic.
// Can happen when the deadline property is larger than params.XORMappedAddrCacheTTL.
// 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 {
addr = mappedAddr.addr
}
Expand All @@ -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.
// 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()
defer m.mu.Unlock()

Expand Down