aboutsummaryrefslogtreecommitdiff
path: root/src/net/tcpsockopt_windows.go
blob: f635d47999a47d8a1830a0664ef8d936fce92f3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package net

import (
	"internal/syscall/windows"
	"os"
	"runtime"
	"syscall"
	"time"
	"unsafe"
)

// Default values of KeepAliveTime and KeepAliveInterval on Windows,
// check out https://learn.microsoft.com/en-us/windows/win32/winsock/sio-keepalive-vals#remarks for details.
const (
	defaultKeepAliveIdle     = 2 * time.Hour
	defaultKeepAliveInterval = time.Second
)

func setKeepAliveIdle(fd *netFD, d time.Duration) error {
	if !windows.SupportTCPKeepAliveIdle() {
		return setKeepAliveIdleAndInterval(fd, d, -1)
	}

	if d == 0 {
		d = defaultTCPKeepAliveIdle
	} else if d < 0 {
		return nil
	}
	// The kernel expects seconds so round to next highest second.
	secs := int(roundDurationUp(d, time.Second))
	err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, windows.TCP_KEEPIDLE, secs)
	runtime.KeepAlive(fd)
	return os.NewSyscallError("setsockopt", err)
}

func setKeepAliveInterval(fd *netFD, d time.Duration) error {
	if !windows.SupportTCPKeepAliveInterval() {
		return setKeepAliveIdleAndInterval(fd, -1, d)
	}

	if d == 0 {
		d = defaultTCPKeepAliveInterval
	} else if d < 0 {
		return nil
	}
	// The kernel expects seconds so round to next highest second.
	secs := int(roundDurationUp(d, time.Second))
	err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, windows.TCP_KEEPINTVL, secs)
	runtime.KeepAlive(fd)
	return os.NewSyscallError("setsockopt", err)
}

func setKeepAliveCount(fd *netFD, n int) error {
	if n == 0 {
		n = defaultTCPKeepAliveCount
	} else if n < 0 {
		return nil
	}

	err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, windows.TCP_KEEPCNT, n)
	runtime.KeepAlive(fd)
	return os.NewSyscallError("setsockopt", err)
}

// setKeepAliveIdleAndInterval serves for kernels prior to Windows 10, version 1709.
func setKeepAliveIdleAndInterval(fd *netFD, idle, interval time.Duration) error {
	// WSAIoctl with SIO_KEEPALIVE_VALS control code requires all fields in
	// `tcp_keepalive` struct to be provided.
	// Otherwise, if any of the fields were not provided, just leaving them
	// zero will knock off any existing values of keep-alive.
	// Unfortunately, Windows doesn't support retrieving current keep-alive
	// settings in any form programmatically, which disable us to first retrieve
	// the current keep-alive settings, then set it without unwanted corruption.
	switch {
	case idle < 0 && interval >= 0:
		// Given that we can't set KeepAliveInterval alone, and this code path
		// is new, it doesn't exist before, so we just return an error.
		return syscall.WSAENOPROTOOPT
	case idle >= 0 && interval < 0:
		// Although we can't set KeepAliveTime alone either, this existing code
		// path had been backing up [SetKeepAlivePeriod] which used to be set both
		// KeepAliveTime and KeepAliveInterval to 15 seconds.
		// Now we will use the default of KeepAliveInterval on Windows if user doesn't
		// provide one.
		interval = defaultKeepAliveInterval
	case idle < 0 && interval < 0:
		// Nothing to do, just bail out.
		return nil
	case idle >= 0 && interval >= 0:
		// Go ahead.
	}

	if idle == 0 {
		idle = defaultTCPKeepAliveIdle
	}
	if interval == 0 {
		interval = defaultTCPKeepAliveInterval
	}

	// The kernel expects milliseconds so round to next highest
	// millisecond.
	tcpKeepAliveIdle := uint32(roundDurationUp(idle, time.Millisecond))
	tcpKeepAliveInterval := uint32(roundDurationUp(interval, time.Millisecond))
	ka := syscall.TCPKeepalive{
		OnOff:    1,
		Time:     tcpKeepAliveIdle,
		Interval: tcpKeepAliveInterval,
	}
	ret := uint32(0)
	size := uint32(unsafe.Sizeof(ka))
	err := fd.pfd.WSAIoctl(syscall.SIO_KEEPALIVE_VALS, (*byte)(unsafe.Pointer(&ka)), size, nil, 0, &ret, nil, 0)
	runtime.KeepAlive(fd)
	return os.NewSyscallError("wsaioctl", err)
}