From 442cceb9fa07e84980d7c7ec5a4c8ac1553932d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 12 Dec 2024 20:40:01 +0800 Subject: [PATCH] Fix disable UDP fragment --- common/control/frag_darwin.go | 17 ++++++++++++----- common/control/frag_linux.go | 16 +++++++++------- common/control/frag_windows.go | 16 +++++++++------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/common/control/frag_darwin.go b/common/control/frag_darwin.go index 567a821..f76b241 100644 --- a/common/control/frag_darwin.go +++ b/common/control/frag_darwin.go @@ -4,19 +4,26 @@ import ( "os" "syscall" + N "github.com/sagernet/sing/common/network" + "golang.org/x/sys/unix" ) func DisableUDPFragment() Func { return func(network, address string, conn syscall.RawConn) error { + if N.NetworkName(network) != N.NetworkUDP { + return nil + } return Raw(conn, func(fd uintptr) error { - switch network { - case "udp4": - if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_DONTFRAG, 1); err != nil { + if network == "udp" || network == "udp4" { + err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_DONTFRAG, 1) + if err != nil { return os.NewSyscallError("SETSOCKOPT IP_DONTFRAG", err) } - case "udp6": - if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_DONTFRAG, 1); err != nil { + } + if network == "udp" || network == "udp6" { + err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_DONTFRAG, 1) + if err != nil { return os.NewSyscallError("SETSOCKOPT IPV6_DONTFRAG", err) } } diff --git a/common/control/frag_linux.go b/common/control/frag_linux.go index 5cb5fca..3bf9d57 100644 --- a/common/control/frag_linux.go +++ b/common/control/frag_linux.go @@ -11,17 +11,19 @@ import ( func DisableUDPFragment() Func { return func(network, address string, conn syscall.RawConn) error { - switch N.NetworkName(network) { - case N.NetworkUDP: - default: + if N.NetworkName(network) != N.NetworkUDP { return nil } return Raw(conn, func(fd uintptr) error { - if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_MTU_DISCOVER, unix.IP_PMTUDISC_DO); err != nil { - return os.NewSyscallError("SETSOCKOPT IP_MTU_DISCOVER IP_PMTUDISC_DO", err) + if network == "udp" || network == "udp4" { + err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_MTU_DISCOVER, unix.IP_PMTUDISC_DO) + if err != nil { + return os.NewSyscallError("SETSOCKOPT IP_MTU_DISCOVER IP_PMTUDISC_DO", err) + } } - if network == "udp6" { - if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_MTU_DISCOVER, unix.IP_PMTUDISC_DO); err != nil { + if network == "udp" || network == "udp6" { + err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_MTU_DISCOVER, unix.IP_PMTUDISC_DO) + if err != nil { return os.NewSyscallError("SETSOCKOPT IPV6_MTU_DISCOVER IP_PMTUDISC_DO", err) } } diff --git a/common/control/frag_windows.go b/common/control/frag_windows.go index bf02948..8e9bb83 100644 --- a/common/control/frag_windows.go +++ b/common/control/frag_windows.go @@ -25,17 +25,19 @@ const ( func DisableUDPFragment() Func { return func(network, address string, conn syscall.RawConn) error { - switch N.NetworkName(network) { - case N.NetworkUDP: - default: + if N.NetworkName(network) != N.NetworkUDP { return nil } return Raw(conn, func(fd uintptr) error { - if err := windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_DO); err != nil { - return os.NewSyscallError("SETSOCKOPT IP_MTU_DISCOVER IP_PMTUDISC_DO", err) + if network == "udp" || network == "udp4" { + err := windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_DO) + if err != nil { + return os.NewSyscallError("SETSOCKOPT IP_MTU_DISCOVER IP_PMTUDISC_DO", err) + } } - if network == "udp6" { - if err := windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IPV6, IPV6_MTU_DISCOVER, IP_PMTUDISC_DO); err != nil { + if network == "udp" || network == "udp6" { + err := windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IPV6, IPV6_MTU_DISCOVER, IP_PMTUDISC_DO) + if err != nil { return os.NewSyscallError("SETSOCKOPT IPV6_MTU_DISCOVER IP_PMTUDISC_DO", err) } }