1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-01 03:25:23 +00:00
v2fly/transport/internet/sockopt_linux.go

53 lines
1.2 KiB
Go
Raw Normal View History

2018-09-06 08:06:57 +00:00
package internet
2018-09-10 11:23:27 +00:00
import (
"syscall"
)
2018-09-06 08:06:57 +00:00
2018-09-10 11:23:27 +00:00
const (
// For incoming connections.
TCP_FASTOPEN = 23
// For out-going connections.
TCP_FASTOPEN_CONNECT = 30
)
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
2018-09-06 08:06:57 +00:00
if config.Mark != 0 {
2018-09-06 08:18:11 +00:00
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(config.Mark)); err != nil {
2018-09-06 08:06:57 +00:00
return err
}
}
2018-09-10 11:23:27 +00:00
2018-09-11 08:03:26 +00:00
if isTCPSocket(network) {
2018-09-10 11:23:27 +00:00
switch config.Tfo {
case SocketConfig_Enable:
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN_CONNECT, 1); err != nil {
return err
}
case SocketConfig_Disable:
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN_CONNECT, 0); err != nil {
return err
}
}
}
return nil
}
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
2018-09-11 08:03:26 +00:00
if isTCPSocket(network) {
2018-09-10 11:23:27 +00:00
switch config.Tfo {
case SocketConfig_Enable:
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, 1); err != nil {
return err
}
case SocketConfig_Disable:
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, 0); err != nil {
return err
}
}
}
2018-09-06 08:06:57 +00:00
return nil
}