Fix and simplify SockOpt in UDP

https://github.com/golang/go/issues/29277
This commit is contained in:
yuhan6665 2024-02-21 13:58:50 -05:00
parent a0f1e1f377
commit 3778a367c8

View File

@ -2,7 +2,6 @@ package internet
import ( import (
"context" "context"
"runtime"
"syscall" "syscall"
"time" "time"
@ -67,17 +66,16 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
if err != nil { if err != nil {
return nil, err return nil, err
} }
if runtime.GOOS != "windows" && (sockopt != nil || len(d.controllers) > 0) { if sockopt != nil {
file, err := packetConn.(*net.UDPConn).File() sys, err := packetConn.(*net.UDPConn).SyscallConn()
if err != nil { if err != nil {
return nil, err return nil, err
} }
fd := file.Fd() sys.Control(func(fd uintptr) {
if sockopt != nil {
if err := applyOutboundSocketOptions("udp", dest.NetAddr(), fd, sockopt); err != nil { if err := applyOutboundSocketOptions("udp", dest.NetAddr(), fd, sockopt); err != nil {
newError("failed to apply socket options").Base(err).WriteToLog(session.ExportIDToError(ctx)) newError("failed to apply socket options").Base(err).WriteToLog(session.ExportIDToError(ctx))
} }
} })
} }
return &PacketConnWrapper{ return &PacketConnWrapper{
Conn: packetConn, Conn: packetConn,