Skip to content

Commit

Permalink
Use netns only if EADDRINUSE occurred
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwinger233 committed Jan 5, 2024
1 parent 4df5451 commit 282c5f9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
6 changes: 1 addition & 5 deletions control/anyfrom_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,7 @@ func (p *AnyfromPool) GetOrCreate(lAddr string, ttl time.Duration) (conn *Anyfro
},
KeepAlive: 0,
}
var pc net.PacketConn
err := WithIndieNetns(func() (err error) {
pc, err = d.ListenPacket(context.Background(), "udp", lAddr)
return err
})
pc, err := d.ListenPacket(context.Background(), "udp", lAddr)
if err != nil {
return nil, true, err
}
Expand Down
17 changes: 8 additions & 9 deletions control/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,16 @@ func sendPkt(data []byte, from netip.AddrPort, realTo, to netip.AddrPort, lConn
}

uConn, _, err := DefaultAnyfromPool.GetOrCreate(from.String(), AnyfromTimeout)
if err != nil {
if errors.Is(err, syscall.EADDRINUSE) {
// Port collision, use traditional method.
return sendPktWithHdrWithFlag(data, from, lConn, to, lanWanFlag)
}
return err
if err != nil && errors.Is(err, syscall.EADDRINUSE) {
err = WithIndieNetns(func() (err error) {
uConn, _, err = DefaultAnyfromPool.GetOrCreate(from.String(), AnyfromTimeout)
return err
})
}
err = WithIndieNetns(func() (err error) {
_, err = uConn.WriteToUDPAddrPort(data, realTo)
if err != nil {
return
})
}
_, err = uConn.WriteToUDPAddrPort(data, realTo)
return err
}

Expand Down

0 comments on commit 282c5f9

Please sign in to comment.