hub_linux.go 1.1 KB

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