forked from datarhei/gosrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_windows.go
69 lines (61 loc) · 1.51 KB
/
net_windows.go
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
//go:build windows
package srt
import (
"syscall"
"golang.org/x/sys/windows"
)
func ListenControl(config Config) func(network, address string, c syscall.RawConn) error {
return func(network, address string, c syscall.RawConn) error {
var opErr error
err := c.Control(func(fd uintptr) {
// Set REUSEADDR
opErr = windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_REUSEADDR, 1)
if opErr != nil {
return
}
// Set TOS
if config.IPTOS > 0 {
opErr = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, windows.IP_TOS, config.IPTOS)
if opErr != nil {
return
}
}
// Set TTL
if config.IPTTL > 0 {
opErr = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, windows.IP_TTL, config.IPTTL)
if opErr != nil {
return
}
}
})
if err != nil {
return err
}
return opErr
}
}
func DialControl(config Config) func(network string, address string, c syscall.RawConn) error {
return func(network, address string, c syscall.RawConn) error {
var opErr error
err := c.Control(func(fd uintptr) {
// Set TOS
if config.IPTOS > 0 {
opErr = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, windows.IP_TOS, config.IPTOS)
if opErr != nil {
return
}
}
// Set TTL
if config.IPTTL > 0 {
opErr = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, windows.IP_TTL, config.IPTTL)
if opErr != nil {
return
}
}
})
if err != nil {
return err
}
return opErr
}
}