github.com/v2fly/v2ray-core/v4@v4.45.2/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  	"golang.org/x/sys/unix"
    10  
    11  	"github.com/v2fly/v2ray-core/v4/common/net"
    12  )
    13  
    14  func RetrieveOriginalDest(oob []byte) net.Destination {
    15  	msgs, err := syscall.ParseSocketControlMessage(oob)
    16  	if err != nil {
    17  		return net.Destination{}
    18  	}
    19  	for _, msg := range msgs {
    20  		if msg.Header.Level == syscall.SOL_IP && msg.Header.Type == syscall.IP_RECVORIGDSTADDR {
    21  			ip := net.IPAddress(msg.Data[4:8])
    22  			port := net.PortFromBytes(msg.Data[2:4])
    23  			return net.UDPDestination(ip, port)
    24  		} else if msg.Header.Level == syscall.SOL_IPV6 && msg.Header.Type == unix.IPV6_RECVORIGDSTADDR {
    25  			ip := net.IPAddress(msg.Data[8:24])
    26  			port := net.PortFromBytes(msg.Data[2:4])
    27  			return net.UDPDestination(ip, port)
    28  		}
    29  	}
    30  	return net.Destination{}
    31  }
    32  
    33  func ReadUDPMsg(conn *net.UDPConn, payload []byte, oob []byte) (int, int, int, *net.UDPAddr, error) {
    34  	return conn.ReadMsgUDP(payload, oob)
    35  }