Skip to content

Commit 2d09013

Browse files
authored
Merge pull request #3 from LinkShen/master
use RawConn.Control to get fd instead of Fd()
2 parents d1655d7 + 4ae9843 commit 2d09013

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

tcpinfo.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,22 @@ func GetsockoptTCPInfo(tcpConn *net.TCPConn) (*TCPInfo, error) {
1616
return nil, fmt.Errorf("tcp conn is nil")
1717
}
1818

19-
file, err := tcpConn.File()
19+
rawConn, err := tcpConn.SyscallConn()
2020
if err != nil {
21-
return nil, err
21+
return nil, fmt.Errorf("error getting raw connection. err=%v", err)
2222
}
23-
defer file.Close()
2423

25-
fd := file.Fd()
2624
tcpInfo := TCPInfo{}
2725
size := unsafe.Sizeof(tcpInfo)
28-
_, _, errno := syscall.Syscall6(syscall.SYS_GETSOCKOPT, fd, syscall.SOL_TCP, syscall.TCP_INFO,
29-
uintptr(unsafe.Pointer(&tcpInfo)), uintptr(unsafe.Pointer(&size)), 0)
26+
var errno syscall.Errno
27+
err = rawConn.Control(func(fd uintptr) {
28+
_, _, errno = syscall.Syscall6(syscall.SYS_GETSOCKOPT, fd, syscall.SOL_TCP, syscall.TCP_INFO,
29+
uintptr(unsafe.Pointer(&tcpInfo)), uintptr(unsafe.Pointer(&size)), 0)
30+
})
31+
if err != nil {
32+
return nil, fmt.Errorf("rawconn control failed. err=%v", err)
33+
}
34+
3035
if errno != 0 {
3136
return nil, fmt.Errorf("syscall failed. errno=%d", errno)
3237
}

0 commit comments

Comments
 (0)