hub_linux.go 879 B

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