Skip to content

Commit

Permalink
Merge pull request #3 from LinkShen/master
Browse files Browse the repository at this point in the history
use RawConn.Control to get fd instead of Fd()
  • Loading branch information
brucespang authored Nov 4, 2020
2 parents d1655d7 + 4ae9843 commit 2d09013
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions tcpinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ func GetsockoptTCPInfo(tcpConn *net.TCPConn) (*TCPInfo, error) {
return nil, fmt.Errorf("tcp conn is nil")
}

file, err := tcpConn.File()
rawConn, err := tcpConn.SyscallConn()
if err != nil {
return nil, err
return nil, fmt.Errorf("error getting raw connection. err=%v", err)
}
defer file.Close()

fd := file.Fd()
tcpInfo := TCPInfo{}
size := unsafe.Sizeof(tcpInfo)
_, _, errno := syscall.Syscall6(syscall.SYS_GETSOCKOPT, fd, syscall.SOL_TCP, syscall.TCP_INFO,
uintptr(unsafe.Pointer(&tcpInfo)), uintptr(unsafe.Pointer(&size)), 0)
var errno syscall.Errno
err = rawConn.Control(func(fd uintptr) {
_, _, errno = syscall.Syscall6(syscall.SYS_GETSOCKOPT, fd, syscall.SOL_TCP, syscall.TCP_INFO,
uintptr(unsafe.Pointer(&tcpInfo)), uintptr(unsafe.Pointer(&size)), 0)
})
if err != nil {
return nil, fmt.Errorf("rawconn control failed. err=%v", err)
}

if errno != 0 {
return nil, fmt.Errorf("syscall failed. errno=%d", errno)
}
Expand Down

0 comments on commit 2d09013

Please sign in to comment.