chore: clear windows bind error

This commit is contained in:
wwqgtxx 2023-04-20 10:22:51 +08:00
parent 31095e4a8c
commit ec234ac0a8

View file

@ -3,6 +3,7 @@ package dialer
import ( import (
"context" "context"
"encoding/binary" "encoding/binary"
"fmt"
"net" "net"
"net/netip" "net/netip"
"syscall" "syscall"
@ -20,11 +21,19 @@ func bind4(handle syscall.Handle, ifaceIdx int) error {
var bytes [4]byte var bytes [4]byte
binary.BigEndian.PutUint32(bytes[:], uint32(ifaceIdx)) binary.BigEndian.PutUint32(bytes[:], uint32(ifaceIdx))
idx := *(*uint32)(unsafe.Pointer(&bytes[0])) idx := *(*uint32)(unsafe.Pointer(&bytes[0]))
return syscall.SetsockoptInt(handle, syscall.IPPROTO_IP, IP_UNICAST_IF, int(idx)) err := syscall.SetsockoptInt(handle, syscall.IPPROTO_IP, IP_UNICAST_IF, int(idx))
if err != nil {
err = fmt.Errorf("bind4: %w", err)
}
return err
} }
func bind6(handle syscall.Handle, ifaceIdx int) error { func bind6(handle syscall.Handle, ifaceIdx int) error {
return syscall.SetsockoptInt(handle, syscall.IPPROTO_IPV6, IPV6_UNICAST_IF, ifaceIdx) err := syscall.SetsockoptInt(handle, syscall.IPPROTO_IPV6, IPV6_UNICAST_IF, ifaceIdx)
if err != nil {
err = fmt.Errorf("bind6: %w", err)
}
return err
} }
func bindControl(ifaceIdx int) controlFn { func bindControl(ifaceIdx int) controlFn {
@ -49,9 +58,9 @@ func bindControl(ifaceIdx int) controlFn {
if (!addrPort.Addr().IsValid() || addrPort.Addr().IsUnspecified()) && bind6err != nil { if (!addrPort.Addr().IsValid() || addrPort.Addr().IsUnspecified()) && bind6err != nil {
// try bind ipv6, if failed, ignore. it's a workaround for windows disable interface ipv6 // try bind ipv6, if failed, ignore. it's a workaround for windows disable interface ipv6
if bind4err != nil { if bind4err != nil {
innerErr = bind6err innerErr = fmt.Errorf("%w (%s)", bind6err, bind4err)
} else { } else {
innerErr = bind4err innerErr = nil
} }
} else { } else {
innerErr = bind6err innerErr = bind6err