sockopt_linux.go 793 B

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