1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-28 14:56:33 -04:00
v2fly/transport/internet/tcp/sockopt_linux.go

39 lines
1.0 KiB
Go
Raw Normal View History

2016-06-11 19:35:40 -04:00
// +build linux
package tcp
2016-06-11 19:35:40 -04:00
import (
"syscall"
2017-08-25 17:17:18 -04:00
"v2ray.com/core/app/log"
2017-04-18 06:02:43 -04:00
v2net "v2ray.com/core/common/net"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/transport/internet"
2016-06-11 19:35:40 -04:00
)
const SO_ORIGINAL_DST = 80
2017-04-18 06:35:54 -04:00
func GetOriginalDestination(conn internet.Connection) (v2net.Destination, error) {
2017-08-25 17:02:54 -04:00
sysrawconn, f := conn.(syscall.Conn)
if !f {
return v2net.Destination{}, newError("unable to get syscall.Conn")
}
rawConn, err := sysrawconn.SyscallConn()
2016-06-11 19:35:40 -04:00
if err != nil {
2017-08-25 17:02:54 -04:00
return v2net.Destination{}, newError("failed to get sys fd").Base(err)
2016-06-11 19:35:40 -04:00
}
2017-08-25 17:02:54 -04:00
var dest v2net.Destination
2017-08-25 17:17:18 -04:00
err = rawConn.Control(func(fd uintptr) {
2017-08-25 17:02:54 -04:00
addr, err := syscall.GetsockoptIPv6Mreq(int(fd), syscall.IPPROTO_IP, SO_ORIGINAL_DST)
if err != nil {
2017-08-25 17:17:18 -04:00
log.Trace(newError("failed to call getsockopt").Base(err))
2017-08-25 17:02:54 -04:00
}
ip := v2net.IPAddress(addr.Multiaddr[4:8])
port := uint16(addr.Multiaddr[2])<<8 + uint16(addr.Multiaddr[3])
2017-08-25 17:17:18 -04:00
dest = v2net.TCPDestination(ip, v2net.Port(port))
2017-08-25 17:02:54 -04:00
})
2016-06-11 19:35:40 -04:00
if err != nil {
2017-08-25 17:02:54 -04:00
return v2net.Destination{}, newError("failed to get original destination").Base(err)
2016-06-11 19:35:40 -04:00
}
2017-08-25 17:02:54 -04:00
return dest, nil
2016-06-11 19:35:40 -04:00
}