Compare commits

...

2 Commits

Author SHA1 Message Date
风扇滑翔翼
d5698333cd
use resolved addr 2025-04-03 18:47:34 +00:00
风扇滑翔翼
ea5106e8ec
fix windows udp by the way 2025-04-03 18:36:47 +00:00
2 changed files with 9 additions and 6 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

@ -60,10 +60,14 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
}
}
var lc net.ListenConfig
destAddr, err := net.ResolveUDPAddr("udp", dest.NetAddr())
if err != nil {
return nil, err
}
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, destAddr.String(), fd, sockopt); err != nil {
errors.LogInfo(ctx, err, "failed to apply socket options")
}
}
@ -73,10 +77,6 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
if err != nil {
return nil, err
}
destAddr, err := net.ResolveUDPAddr("udp", dest.NetAddr())
if err != nil {
return nil, err
}
return &PacketConnWrapper{
Conn: packetConn,
Dest: destAddr,