github.com/quic-go/quic-go@v0.44.0/sys_conn_df_windows.go (about)

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