Skip to content

Commit 7c444a9

Browse files
committed
fix: correctly handle ipv6 zone
1 parent e3d4ec2 commit 7c444a9

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

common/net/packet/packet_posix.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ func (c *enhanceUDPConn) WaitReadFrom() (data []byte, put func(), addr net.Addr,
4545
addr = &net.UDPAddr{IP: ip[:], Port: from.Port}
4646
case *syscall.SockaddrInet6:
4747
ip := from.Addr // copy from.Addr; ip escapes, so this line allocates 16 bytes
48-
addr = &net.UDPAddr{IP: ip[:], Port: from.Port, Zone: strconv.FormatInt(int64(from.ZoneId), 10)}
48+
zone := ""
49+
if from.ZoneId != 0 {
50+
zone = strconv.FormatInt(int64(from.ZoneId), 10)
51+
}
52+
addr = &net.UDPAddr{IP: ip[:], Port: from.Port, Zone: zone}
4953
}
5054
}
5155
// udp should not convert readN == 0 to io.EOF

common/net/packet/packet_windows.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ func (c *enhanceUDPConn) WaitReadFrom() (data []byte, put func(), addr net.Addr,
5454
addr = &net.UDPAddr{IP: ip[:], Port: from.Port}
5555
case *windows.SockaddrInet6:
5656
ip := from.Addr // copy from.Addr; ip escapes, so this line allocates 16 bytes
57-
addr = &net.UDPAddr{IP: ip[:], Port: from.Port, Zone: strconv.FormatInt(int64(from.ZoneId), 10)}
57+
zone := ""
58+
if from.ZoneId != 0 {
59+
zone = strconv.FormatInt(int64(from.ZoneId), 10)
60+
}
61+
addr = &net.UDPAddr{IP: ip[:], Port: from.Port, Zone: zone}
5862
}
5963
}
6064
// udp should not convert readN == 0 to io.EOF

dns/system_windows.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
package dns
44

55
import (
6-
"net"
6+
"net/netip"
77
"os"
8+
"strconv"
89
"syscall"
910
"unsafe"
1011

@@ -23,28 +24,31 @@ func dnsReadConfig() (servers []string, err error) {
2324
if err != nil {
2425
continue
2526
}
26-
var ip net.IP
27+
var ip netip.Addr
2728
switch sa := sa.(type) {
2829
case *syscall.SockaddrInet4:
29-
ip = net.IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])
30+
ip = netip.AddrFrom4(sa.Addr)
3031
case *syscall.SockaddrInet6:
31-
ip = make(net.IP, net.IPv6len)
32-
copy(ip, sa.Addr[:])
33-
if ip[0] == 0xfe && ip[1] == 0xc0 {
32+
if sa.Addr[0] == 0xfe && sa.Addr[1] == 0xc0 {
3433
// Ignore these fec0/10 ones. Windows seems to
3534
// populate them as defaults on its misc rando
3635
// interfaces.
3736
continue
3837
}
38+
ip = netip.AddrFrom16(sa.Addr)
39+
if sa.ZoneId != 0 {
40+
ip = ip.WithZone(strconv.FormatInt(int64(sa.ZoneId), 10))
41+
}
3942
//continue
4043
default:
4144
// Unexpected type.
4245
continue
4346
}
44-
if slices.Contains(servers, ip.String()) {
47+
ipStr := ip.String()
48+
if slices.Contains(servers, ipStr) {
4549
continue
4650
}
47-
servers = append(servers, ip.String())
51+
servers = append(servers, ipStr)
4852
}
4953
}
5054
return

0 commit comments

Comments
 (0)