blob: c8970d1b57475070971d488bf4d0a93b402ed797 [file] [log] [blame]
Mikio Harabf61a97f2013-07-28 11:18:06 +09001// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Aram Hăvărneanudb8d5b72015-03-17 13:50:40 +01005// +build freebsd linux netbsd
Mikio Harabf61a97f2013-07-28 11:18:06 +09006
7package net
8
9import (
10 "os"
11 "syscall"
12 "time"
13)
14
Mikio Harabf61a97f2013-07-28 11:18:06 +090015func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
Dmitriy Vyukov23e15f72013-08-09 21:43:00 +040016 if err := fd.incref(); err != nil {
Mikio Harabf61a97f2013-07-28 11:18:06 +090017 return err
18 }
19 defer fd.decref()
Mikio Harabf61a97f2013-07-28 11:18:06 +090020 // The kernel expects seconds so round to next highest second.
21 d += (time.Second - time.Nanosecond)
22 secs := int(d.Seconds())
Mikio Haraf9567402014-09-11 17:56:58 +090023 if err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, secs); err != nil {
24 return os.NewSyscallError("setsockopt", err)
Mikio Harabf61a97f2013-07-28 11:18:06 +090025 }
26 return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, secs))
27}