fix windows udp by the way

This commit is contained in:
风扇滑翔翼 2025-04-03 18:36:47 +00:00 committed by GitHub
parent e70488287b
commit ea5106e8ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 2 deletions

View file

@ -40,7 +40,10 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
if err != nil {
return errors.New("failed to find the interface").Base(err)
}
isV4 := (network == "tcp4" || network == "udp4")
// easy way to check if the address is ipv4
isV4 := strings.Contains(address, ".")
// note: DO NOT trust the passed network variable, it can be udp6 even if the address is ipv4
// because operating system might(always) use ipv6 socket to process ipv4
if isV4 {
var bytes [4]byte
binary.BigEndian.PutUint32(bytes[:], uint32(inf.Index))

View file

@ -63,7 +63,7 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
lc.Control = func(network, address string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
if sockopt != nil {
if err := applyOutboundSocketOptions(network, "", fd, sockopt); err != nil {
if err := applyOutboundSocketOptions(network, dest.NetAddr(), fd, sockopt); err != nil {
errors.LogInfo(ctx, err, "failed to apply socket options")
}
}