blob: 1f1609088baf196d39e2066afcabc48d11fd66a7 [file] [log] [blame]
David Presotto918922c2013-07-15 18:40:55 -04001// 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
David Presotto918922c2013-07-15 18:40:55 -04005package net
6
7import (
8 "os"
9 "syscall"
10 "time"
11)
12
Mikio Haraf9567402014-09-11 17:56:58 +090013const sysTCP_KEEPINTVL = 0x101
14
David Presotto918922c2013-07-15 18:40:55 -040015func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
Dmitriy Vyukov23e15f72013-08-09 21:43:00 +040016 if err := fd.incref(); err != nil {
David Presotto918922c2013-07-15 18:40:55 -040017 return err
18 }
19 defer fd.decref()
David Presotto918922c2013-07-15 18:40:55 -040020 // 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 switch err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, sysTCP_KEEPINTVL, secs); err {
24 case nil, syscall.ENOPROTOOPT: // OS X 10.7 and earlier don't support this option
25 default:
26 return os.NewSyscallError("setsockopt", err)
27 }
David Presotto918922c2013-07-15 18:40:55 -040028 return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPALIVE, secs))
29}