v2ray-core/transport/internet/tcp/sockopt_linux.go

30 lines
793 B
Go
Raw Normal View History

2016-06-11 23:35:40 +00:00
// +build linux
package tcp
2016-06-11 23:35:40 +00:00
import (
2017-04-18 10:02:43 +00:00
"net"
2016-06-11 23:35:40 +00:00
"syscall"
2017-04-18 10:02:43 +00:00
v2net "v2ray.com/core/common/net"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/transport/internet"
2017-04-18 10:02:43 +00:00
"v2ray.com/core/transport/internet/internal"
2016-06-11 23:35:40 +00:00
)
const SO_ORIGINAL_DST = 80
2017-04-18 10:35:54 +00:00
func GetOriginalDestination(conn internet.Connection) (v2net.Destination, error) {
2017-04-18 10:02:43 +00:00
fd, err := internal.GetSysFd(conn.(net.Conn))
2016-06-11 23:35:40 +00:00
if err != nil {
2017-04-18 10:35:54 +00:00
return v2net.Destination{}, newError("failed to get original destination").Base(err)
2016-06-11 23:35:40 +00:00
}
addr, err := syscall.GetsockoptIPv6Mreq(fd, syscall.IPPROTO_IP, SO_ORIGINAL_DST)
if err != nil {
2017-04-18 10:35:54 +00:00
return v2net.Destination{}, newError("failed to call getsockopt").Base(err)
2016-06-11 23:35:40 +00:00
}
2017-04-18 10:02:43 +00:00
ip := v2net.IPAddress(addr.Multiaddr[4:8])
2016-06-11 23:35:40 +00:00
port := uint16(addr.Multiaddr[2])<<8 + uint16(addr.Multiaddr[3])
2017-04-18 10:35:54 +00:00
return v2net.TCPDestination(ip, v2net.Port(port)), nil
2016-06-11 23:35:40 +00:00
}