github.com/metacubex/quic-go@v0.44.1-0.20240520163451-20b689a59136/sys_conn_df_linux.go (about)

     1  //go:build linux
     2  
     3  package quic
     4  
     5  import (
     6  	"errors"
     7  	"syscall"
     8  
     9  	"golang.org/x/sys/unix"
    10  
    11  	"github.com/metacubex/quic-go/internal/utils"
    12  )
    13  
    14  func setDF(rawConn syscall.RawConn) (bool, error) {
    15  	// Enabling IP_MTU_DISCOVER will force the kernel to return "sendto: message too long"
    16  	// and the datagram will not be fragmented
    17  	var errDFIPv4, errDFIPv6 error
    18  	if err := rawConn.Control(func(fd uintptr) {
    19  		errDFIPv4 = unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_MTU_DISCOVER, unix.IP_PMTUDISC_DO)
    20  		errDFIPv6 = unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_MTU_DISCOVER, unix.IPV6_PMTUDISC_DO)
    21  	}); err != nil {
    22  		return false, err
    23  	}
    24  	switch {
    25  	case errDFIPv4 == nil && errDFIPv6 == nil:
    26  		utils.DefaultLogger.Debugf("Setting DF for IPv4 and IPv6.")
    27  	case errDFIPv4 == nil && errDFIPv6 != nil:
    28  		utils.DefaultLogger.Debugf("Setting DF for IPv4.")
    29  	case errDFIPv4 != nil && errDFIPv6 == nil:
    30  		utils.DefaultLogger.Debugf("Setting DF for IPv6.")
    31  	case errDFIPv4 != nil && errDFIPv6 != nil:
    32  		utils.DefaultLogger.Debugf("Setting DF failed for both IPv4 and IPv6.")
    33  	}
    34  	return true, nil
    35  }
    36  
    37  func isSendMsgSizeErr(err error) bool {
    38  	// https://man7.org/linux/man-pages/man7/udp.7.html
    39  	return errors.Is(err, unix.EMSGSIZE)
    40  }
    41  
    42  func isRecvMsgSizeErr(error) bool { return false }