hub_linux.go 914 B

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