github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/transport/internet/udp/hub_linux.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package udp
     5  
     6  import (
     7  	"syscall"
     8  
     9  	"github.com/xtls/xray-core/common/net"
    10  	"golang.org/x/sys/unix"
    11  )
    12  
    13  func RetrieveOriginalDest(oob []byte) net.Destination {
    14  	msgs, err := syscall.ParseSocketControlMessage(oob)
    15  	if err != nil {
    16  		return net.Destination{}
    17  	}
    18  	for _, msg := range msgs {
    19  		if msg.Header.Level == syscall.SOL_IP && msg.Header.Type == syscall.IP_RECVORIGDSTADDR {
    20  			ip := net.IPAddress(msg.Data[4:8])
    21  			port := net.PortFromBytes(msg.Data[2:4])
    22  			return net.UDPDestination(ip, port)
    23  		} else if msg.Header.Level == syscall.SOL_IPV6 && msg.Header.Type == unix.IPV6_RECVORIGDSTADDR {
    24  			ip := net.IPAddress(msg.Data[8:24])
    25  			port := net.PortFromBytes(msg.Data[2:4])
    26  			return net.UDPDestination(ip, port)
    27  		}
    28  	}
    29  	return net.Destination{}
    30  }
    31  
    32  func ReadUDPMsg(conn *net.UDPConn, payload []byte, oob []byte) (int, int, int, *net.UDPAddr, error) {
    33  	return conn.ReadMsgUDP(payload, oob)
    34  }