github.com/tumi8/quic-go@v0.37.4-tum/sys_conn_helper_linux.go (about)

     1  //go:build linux
     2  
     3  package quic
     4  
     5  import (
     6  	"encoding/binary"
     7  	"net/netip"
     8  	"syscall"
     9  
    10  	"golang.org/x/sys/unix"
    11  )
    12  
    13  const (
    14  	msgTypeIPTOS = unix.IP_TOS
    15  	ipv4PKTINFO  = unix.IP_PKTINFO
    16  )
    17  
    18  const batchSize = 8 // needs to smaller than MaxUint8 (otherwise the type of oobConn.readPos has to be changed)
    19  
    20  func forceSetReceiveBuffer(c syscall.RawConn, bytes int) error {
    21  	var serr error
    22  	if err := c.Control(func(fd uintptr) {
    23  		serr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_RCVBUFFORCE, bytes)
    24  	}); err != nil {
    25  		return err
    26  	}
    27  	return serr
    28  }
    29  
    30  func forceSetSendBuffer(c syscall.RawConn, bytes int) error {
    31  	var serr error
    32  	if err := c.Control(func(fd uintptr) {
    33  		serr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_SNDBUFFORCE, bytes)
    34  	}); err != nil {
    35  		return err
    36  	}
    37  	return serr
    38  }
    39  
    40  func parseIPv4PktInfo(body []byte) (ip netip.Addr, ifIndex uint32, ok bool) {
    41  	// struct in_pktinfo {
    42  	// 	unsigned int   ipi_ifindex;  /* Interface index */
    43  	// 	struct in_addr ipi_spec_dst; /* Local address */
    44  	// 	struct in_addr ipi_addr;     /* Header Destination address */
    45  	// };
    46  	if len(body) != 12 {
    47  		return netip.Addr{}, 0, false
    48  	}
    49  	return netip.AddrFrom4(*(*[4]byte)(body[8:12])), binary.LittleEndian.Uint32(body), true
    50  }