github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/conn/conn_tclass_no_dualstack.go (about)

     1  //go:build aix || darwin || dragonfly || freebsd || netbsd || openbsd || solaris || zos
     2  
     3  package conn
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"golang.org/x/sys/unix"
     9  )
    10  
    11  func setTrafficClass(fd int, network string, trafficClass int) error {
    12  	switch network {
    13  	case "tcp4", "udp4":
    14  		if err := unix.SetsockoptInt(fd, unix.IPPROTO_IP, unix.IP_TOS, trafficClass); err != nil {
    15  			return fmt.Errorf("failed to set socket option IP_TOS: %w", err)
    16  		}
    17  	case "tcp6", "udp6":
    18  		if err := unix.SetsockoptInt(fd, unix.IPPROTO_IPV6, unix.IPV6_TCLASS, trafficClass); err != nil {
    19  			return fmt.Errorf("failed to set socket option IPV6_TCLASS: %w", err)
    20  		}
    21  	default:
    22  		return fmt.Errorf("unsupported network: %s", network)
    23  	}
    24  	return nil
    25  }