github.com/sagernet/quic-go@v0.43.1-beta.1/sys_conn_df_windows.go (about)

     1  //go:build windows
     2  
     3  package quic
     4  
     5  import (
     6  	"errors"
     7  	"syscall"
     8  
     9  	"github.com/sagernet/quic-go/internal/utils"
    10  	"golang.org/x/sys/windows"
    11  )
    12  
    13  const (
    14  	// IP_DONTFRAGMENT controls the Don't Fragment (DF) bit.
    15  	//
    16  	// It's the same code point for both IPv4 and IPv6 on Windows.
    17  	// https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Networking/WinSock/constant.IP_DONTFRAG.html
    18  	// https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Networking/WinSock/constant.IPV6_DONTFRAG.html
    19  	//
    20  	//nolint:stylecheck
    21  	IP_DONTFRAGMENT = 14
    22  )
    23  
    24  func setDF(rawConn syscall.RawConn) (bool, error) {
    25  	var errDFIPv4, errDFIPv6 error
    26  	if err := rawConn.Control(func(fd uintptr) {
    27  		errDFIPv4 = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, IP_DONTFRAGMENT, 1)
    28  		errDFIPv6 = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IPV6, IP_DONTFRAGMENT, 1)
    29  	}); err != nil {
    30  		return false, err
    31  	}
    32  	switch {
    33  	case errDFIPv4 == nil && errDFIPv6 == nil:
    34  		utils.DefaultLogger.Debugf("Setting DF for IPv4 and IPv6.")
    35  	case errDFIPv4 == nil && errDFIPv6 != nil:
    36  		utils.DefaultLogger.Debugf("Setting DF for IPv4.")
    37  	case errDFIPv4 != nil && errDFIPv6 == nil:
    38  		utils.DefaultLogger.Debugf("Setting DF for IPv6.")
    39  	case errDFIPv4 != nil && errDFIPv6 != nil:
    40  		utils.DefaultLogger.Debugf("Setting DF failed for both IPv4 and IPv6.")
    41  	}
    42  	return true, nil
    43  }
    44  
    45  func isSendMsgSizeErr(err error) bool {
    46  	// https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2
    47  	return errors.Is(err, windows.WSAEMSGSIZE)
    48  }
    49  
    50  func isRecvMsgSizeErr(err error) bool {
    51  	// https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2
    52  	return errors.Is(err, windows.WSAEMSGSIZE)
    53  }