blob: 1f1609088baf196d39e2066afcabc48d11fd66a7 [file] [log] [blame]
Colin Cross7bb052a2015-02-03 12:59:37 -08001// 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
5package net
6
7import (
8 "os"
9 "syscall"
10 "time"
11)
12
13const sysTCP_KEEPINTVL = 0x101
14
15func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
16 if err := fd.incref(); err != nil {
17 return err
18 }
19 defer fd.decref()
20 // The kernel expects seconds so round to next highest second.
21 d += (time.Second - time.Nanosecond)
22 secs := int(d.Seconds())
23 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 }
28 return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPALIVE, secs))
29}