hub_linux.go 792 B

12345678910111213141516171819202122232425262728293031323334
  1. // +build linux
  2. package udp
  3. import (
  4. "syscall"
  5. v2net "github.com/v2ray/v2ray-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) v2net.Destination {
  17. msgs, err := syscall.ParseSocketControlMessage(oob)
  18. if err != nil {
  19. return nil
  20. }
  21. for _, msg := range msgs {
  22. if msg.Header.Level == syscall.SOL_IP && msg.Header.Type == syscall.IP_ORIGDSTADDR {
  23. ip := v2net.IPAddress(msg.Data[4:8])
  24. port := v2net.PortFromBytes(msg.Data[2:4])
  25. return v2net.UDPDestination(ip, port)
  26. }
  27. }
  28. return nil
  29. }