sockopt_linux.go 829 B

1234567891011121314151617181920212223242526272829303132
  1. // +build linux
  2. package tcp
  3. import (
  4. "net"
  5. "syscall"
  6. "v2ray.com/core/app/log"
  7. v2net "v2ray.com/core/common/net"
  8. "v2ray.com/core/transport/internet"
  9. "v2ray.com/core/transport/internet/internal"
  10. )
  11. const SO_ORIGINAL_DST = 80
  12. func GetOriginalDestination(conn internet.Connection) v2net.Destination {
  13. fd, err := internal.GetSysFd(conn.(net.Conn))
  14. if err != nil {
  15. log.Trace(newError("failed to get original destination").Base(err))
  16. return v2net.Destination{}
  17. }
  18. addr, err := syscall.GetsockoptIPv6Mreq(fd, syscall.IPPROTO_IP, SO_ORIGINAL_DST)
  19. if err != nil {
  20. log.Trace(newError("failed to call getsockopt").Base(err))
  21. return v2net.Destination{}
  22. }
  23. ip := v2net.IPAddress(addr.Multiaddr[4:8])
  24. port := uint16(addr.Multiaddr[2])<<8 + uint16(addr.Multiaddr[3])
  25. return v2net.TCPDestination(ip, v2net.Port(port))
  26. }