Skip to content

Commit

Permalink
Add control.SetKeepAlivePeriod
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Apr 10, 2024
1 parent 0ea2687 commit 76e2ce0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
29 changes: 29 additions & 0 deletions common/control/tcp_keep_alive_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package control

import (
"syscall"
"time"
_ "unsafe"

E "github.com/sagernet/sing/common/exceptions"
N "github.com/sagernet/sing/common/network"

"golang.org/x/sys/unix"
)

func SetKeepAlivePeriod(idle time.Duration, interval time.Duration) Func {
return func(network, address string, conn syscall.RawConn) error {
if N.NetworkName(network) != N.NetworkTCP {
return nil
}
return Raw(conn, func(fd uintptr) error {
return E.Errors(
unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPIDLE, int(roundDurationUp(idle, time.Second))),
unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPINTVL, int(roundDurationUp(interval, time.Second))),
)
})
}
}

//go:linkname roundDurationUp net.roundDurationUp
func roundDurationUp(d time.Duration, to time.Duration) time.Duration
11 changes: 11 additions & 0 deletions common/control/tcp_keep_alive_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build !linux

package control

import (
"time"
)

func SetKeepAlivePeriod(idle time.Duration, interval time.Duration) Func {
return nil
}

0 comments on commit 76e2ce0

Please sign in to comment.