-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for a connection to check if it's sending bytes
- Loading branch information
1 parent
8f91506
commit e3d398e
Showing
3 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//go:build linux | ||
|
||
package sockopt | ||
|
||
import ( | ||
"net" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func isSocketFdSendingBytes(fd int) (bool, error) { | ||
tcpInfo, err := unix.GetsockoptTCPInfo(fd, unix.IPPROTO_TCP, unix.TCP_INFO) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
// 1 == TCP_ESTABLISHED, but for some reason not available in the package | ||
if tcpInfo.State != unix.BPF_TCP_ESTABLISHED { | ||
// If the connection is not established, the socket is not sending bytes | ||
return false, nil | ||
} | ||
|
||
return tcpInfo.Notsent_bytes != 0, nil | ||
} | ||
|
||
func isConnectionSendingBytesImplemented() bool { | ||
return true | ||
} | ||
|
||
func isConnectionSendingBytes(conn *net.TCPConn) (result bool, err error) { | ||
syscallConn, err := conn.SyscallConn() | ||
if err != nil { | ||
return false, err | ||
} | ||
syscallConn.Control(func(fd uintptr) { | ||
result, err = isSocketFdSendingBytes(int(fd)) | ||
}) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//go:build !linux | ||
|
||
package sockopt | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
) | ||
|
||
func isConnectionSendingBytesImplemented() bool { | ||
return false | ||
} | ||
|
||
func isConnectionSendingBytes(_ *net.TCPConn) (bool, error) { | ||
return false, fmt.Errorf("%w: checking if socket is sending bytes is not implemented on this platform", errors.ErrUnsupported) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters