1//go:build linux
2// +build linux
3
4package udp
5
6import (
7	"syscall"
8
9	"golang.org/x/sys/unix"
10
11	"github.com/xtls/xray-core/common/net"
12)
13
14func 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
33func ReadUDPMsg(conn *net.UDPConn, payload []byte, oob []byte) (int, int, int, *net.UDPAddr, error) {
34	return conn.ReadMsgUDP(payload, oob)
35}
36