-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathsocket.go
More file actions
48 lines (40 loc) · 1.05 KB
/
socket.go
File metadata and controls
48 lines (40 loc) · 1.05 KB
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
package nftables
import (
"fmt"
"github.com/mdlayher/netlink"
"golang.org/x/sys/unix"
)
// isReadReady reports whether the netlink connection is ready for reading.
// It uses pselect6 with a zero timeout on the underlying raw connection.
// This allows for an efficient check of socket readiness without blocking.
// If the Conn was created with a TestDial function, it assumes readiness.
func (cc *Conn) isReadReady(conn *netlink.Conn) (bool, error) {
if cc.TestDial != nil {
return true, nil
}
rawConn, err := conn.SyscallConn()
if err != nil {
return false, fmt.Errorf("get raw conn: %w", err)
}
var n int
var opErr error
err = rawConn.Control(func(fd uintptr) {
var readfds unix.FdSet
readfds.Zero()
readfds.Set(int(fd))
ts := &unix.Timespec{} // zero timeout: immediate return
for {
n, opErr = unix.Pselect(int(fd)+1, &readfds, nil, nil, ts, nil)
if opErr != unix.EINTR {
break
}
}
})
if err != nil {
return false, err
}
if opErr != nil {
return false, fmt.Errorf("pselect6: %w", opErr)
}
return n > 0, nil
}