blob: 45a4dca5257d3e0a7b314fc31c5f53b8593bdae8 [file] [log] [blame]
Brad Fitzpatrick51947442016-03-01 22:57:46 +00001// Copyright 2009 The Go Authors. All rights reserved.
David Presotto918922c2013-07-15 18:40:55 -04002// 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 (
Nicholas Katsaros5277b902014-01-10 14:33:54 +11008 "os"
9 "syscall"
David Presotto918922c2013-07-15 18:40:55 -040010 "time"
Nicholas Katsaros5277b902014-01-10 14:33:54 +110011 "unsafe"
David Presotto918922c2013-07-15 18:40:55 -040012)
13
14func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
Dmitriy Vyukov23e15f72013-08-09 21:43:00 +040015 if err := fd.incref(); err != nil {
David Presotto918922c2013-07-15 18:40:55 -040016 return err
17 }
18 defer fd.decref()
Mikio Haraf9567402014-09-11 17:56:58 +090019 // The kernel expects milliseconds so round to next highest
20 // millisecond.
Nicholas Katsaros5277b902014-01-10 14:33:54 +110021 d += (time.Millisecond - time.Nanosecond)
Mikio Haraf9567402014-09-11 17:56:58 +090022 msecs := uint32(d / time.Millisecond)
Nicholas Katsaros5277b902014-01-10 14:33:54 +110023 ka := syscall.TCPKeepalive{
24 OnOff: 1,
Mikio Haraf9567402014-09-11 17:56:58 +090025 Time: msecs,
26 Interval: msecs,
Nicholas Katsaros5277b902014-01-10 14:33:54 +110027 }
28 ret := uint32(0)
29 size := uint32(unsafe.Sizeof(ka))
30 err := syscall.WSAIoctl(fd.sysfd, syscall.SIO_KEEPALIVE_VALS, (*byte)(unsafe.Pointer(&ka)), size, nil, 0, &ret, nil, 0)
Mikio Hara055ecb72015-04-21 21:20:15 +090031 return os.NewSyscallError("wsaioctl", err)
David Presotto918922c2013-07-15 18:40:55 -040032}