Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 77 additions & 22 deletions socket_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,37 +338,92 @@ func SocketDiagTCPInfo(family uint8) ([]*InetDiagTCPInfoResp, error) {
return pkgHandle.SocketDiagTCPInfo(family)
}

// SocketDiagTCP requests INET_DIAG_INFO for TCP protocol for specified family type and return related socket.
// SocketDiagTCPOptions represents the configuration options for socket diagnostics.
type SocketDiagTCPOptions struct {
// Families is a bitset representing the internet address families of interest to be used in the query. The bits
// are expected to be set according to the unix.AF_* constants. If Families is zero, both AF_INET and AF_INET6
// address families will be retrieved by default.
Families uint32

// States is a bitset that specifies the TCP state of the target sockets to be retrieved. The bits are
// expected to be set according to TCP_* state constants. If States is zero, all states will be retrieved.
States uint32
}

// SocketDiagTCPWithOptions requests INET_DIAG_INFO for TCP protocol for specified options and returns related sockets.
// Currently only AF_INET and AF_INET6 address families are supported.
//
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
// or incomplete.
func (h *Handle) SocketDiagTCP(family uint8) ([]*Socket, error) {
// Construct the request
req := h.newNetlinkRequest(nl.SOCK_DIAG_BY_FAMILY, unix.NLM_F_DUMP)
req.AddData(&socketRequest{
Family: family,
Protocol: unix.IPPROTO_TCP,
Ext: (1 << (INET_DIAG_VEGASINFO - 1)) | (1 << (INET_DIAG_INFO - 1)),
States: uint32(0xfff), // all states
})
func (h *Handle) SocketDiagTCPWithOptions(opts SocketDiagTCPOptions) ([]*Socket, error) {
Comment on lines +353 to +358
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Doc fix: this path does not request INET_DIAG_INFO extensions.

Ext is 0; the function returns base Socket messages only. Update the comment to avoid misleading users.

-// SocketDiagTCPWithOptions requests INET_DIAG_INFO for TCP protocol for specified options and returns related sockets.
-// Currently only AF_INET and AF_INET6 address families are supported.
+// SocketDiagTCPWithOptions performs TCP socket diagnostics (INET_DIAG) for the
+// specified options and returns base Socket records (no INET_DIAG_* extensions).
+// Currently only AF_INET and AF_INET6 address families are supported.
 //
-// If the returned error is [ErrDumpInterrupted], results may be inconsistent
-// or incomplete.
+// If the returned error is [ErrDumpInterrupted], results may be inconsistent or incomplete.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// SocketDiagTCPWithOptions requests INET_DIAG_INFO for TCP protocol for specified options and returns related sockets.
// Currently only AF_INET and AF_INET6 address families are supported.
//
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
// or incomplete.
func (h *Handle) SocketDiagTCP(family uint8) ([]*Socket, error) {
// Construct the request
req := h.newNetlinkRequest(nl.SOCK_DIAG_BY_FAMILY, unix.NLM_F_DUMP)
req.AddData(&socketRequest{
Family: family,
Protocol: unix.IPPROTO_TCP,
Ext: (1 << (INET_DIAG_VEGASINFO - 1)) | (1 << (INET_DIAG_INFO - 1)),
States: uint32(0xfff), // all states
})
func (h *Handle) SocketDiagTCPWithOptions(opts SocketDiagTCPOptions) ([]*Socket, error) {
// SocketDiagTCPWithOptions performs TCP socket diagnostics (INET_DIAG) for the
// specified options and returns base Socket records (no INET_DIAG_* extensions).
// Currently only AF_INET and AF_INET6 address families are supported.
//
// If the returned error is [ErrDumpInterrupted], results may be inconsistent or incomplete.
func (h *Handle) SocketDiagTCPWithOptions(opts SocketDiagTCPOptions) ([]*Socket, error) {
🤖 Prompt for AI Agents
In socket_linux.go around lines 353 to 358, the function comment incorrectly
states it "requests INET_DIAG_INFO" — update the docstring to accurately state
that Ext is 0 and the function does not request INET_DIAG_INFO extensions, and
that it returns only base Socket messages (not extended info); keep the note
about ErrDumpInterrupted unchanged.

if opts.Families == 0 {
opts.Families = (1 << unix.AF_INET) | (1 << unix.AF_INET6) // all IPv4 and IPv6 sockets
}
if opts.States == 0 {
opts.States = uint32(0xfff) // all states
}

// check if any unsupported families are set
supportedFamilies := uint32((1 << unix.AF_INET) | (1 << unix.AF_INET6))
if opts.Families&^supportedFamilies != 0 {
return nil, fmt.Errorf("unsupported address families specified: only AF_INET and AF_INET6 are supported")
}

// Do the query and parse the result
var result []*Socket
executeErr := req.ExecuteIter(unix.NETLINK_INET_DIAG, nl.SOCK_DIAG_BY_FAMILY, func(msg []byte) bool {
sockInfo := &Socket{}
if err := sockInfo.deserialize(msg); err != nil {
return false
var err error
for _, family := range []uint8{unix.AF_INET, unix.AF_INET6} {
if opts.Families&(1<<family) != 0 {
// Make request for the specific family
req := h.newNetlinkRequest(nl.SOCK_DIAG_BY_FAMILY, unix.NLM_F_DUMP)
req.AddData(&socketRequest{
Family: family,
Protocol: unix.IPPROTO_TCP,
Ext: 0, // no extensions requested
States: opts.States,
})

// Process response and append to results
executeErr := req.ExecuteIter(unix.NETLINK_INET_DIAG, nl.SOCK_DIAG_BY_FAMILY, func(msg []byte) bool {
sockInfo := &Socket{}
if err := sockInfo.deserialize(msg); err != nil {
return false
}
result = append(result, sockInfo)
return true
})
if executeErr != nil {
if !errors.Is(executeErr, ErrDumpInterrupted) {
return nil, executeErr
} else {
err = executeErr
}
}
}
result = append(result, sockInfo)
return true
})
if executeErr != nil && !errors.Is(executeErr, ErrDumpInterrupted) {
return nil, executeErr
}
return result, executeErr
return result, err
}

// SocketDiagTCPWithOptions requests INET_DIAG_INFO for TCP protocol for specified options and returns related sockets.
// Currently only AF_INET and AF_INET6 address families are supported.
//
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
// or incomplete.
func SocketDiagTCPWithOptions(opts SocketDiagTCPOptions) ([]*Socket, error) {
return pkgHandle.SocketDiagTCPWithOptions(opts)
}

Comment on lines +406 to +414
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Doc fix: wrapper also does not request INET_DIAG_INFO.
Ext is 0 in the callee; adjust the comment.

-// SocketDiagTCPWithOptions requests INET_DIAG_INFO for TCP protocol for specified options and returns related sockets.
-// Currently only AF_INET and AF_INET6 address families are supported.
+// SocketDiagTCPWithOptions performs TCP socket diagnostics (INET_DIAG) for the
+// specified options and returns base Socket records (no INET_DIAG_* extensions).
+// Currently only AF_INET and AF_INET6 address families are supported.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// SocketDiagTCPWithOptions requests INET_DIAG_INFO for TCP protocol for specified options and returns related sockets.
// Currently only AF_INET and AF_INET6 address families are supported.
//
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
// or incomplete.
func SocketDiagTCPWithOptions(opts SocketDiagTCPOptions) ([]*Socket, error) {
return pkgHandle.SocketDiagTCPWithOptions(opts)
}
// SocketDiagTCPWithOptions performs TCP socket diagnostics (INET_DIAG) for the
// specified options and returns base Socket records (no INET_DIAG_* extensions).
// Currently only AF_INET and AF_INET6 address families are supported.
//
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
// or incomplete.
func SocketDiagTCPWithOptions(opts SocketDiagTCPOptions) ([]*Socket, error) {
return pkgHandle.SocketDiagTCPWithOptions(opts)
}
🤖 Prompt for AI Agents
In socket_linux.go around lines 406 to 414, the function comment incorrectly
states that the wrapper requests INET_DIAG_INFO; the callee currently uses ext=0
so INET_DIAG_INFO is not requested — update the doc comment to remove the claim
about requesting INET_DIAG_INFO, note that ext is 0 in the callee (so no
INET_DIAG_INFO returned), and keep the rest of the description (supported
families AF_INET/AF_INET6 and ErrDumpInterrupted caveat) accurate.

// SocketDiagTCP requests INET_DIAG_INFO for TCP protocol for specified family type and returns related sockets.
//
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
// or incomplete.
func (h *Handle) SocketDiagTCP(family uint8) ([]*Socket, error) {
opts := SocketDiagTCPOptions{
Families: 1 << family,
}
return h.SocketDiagTCPWithOptions(opts)
}
Comment on lines +415 to 424
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Doc fix: this wrapper returns base sockets, not INET_DIAG_INFO.

Also hint at SocketDiagTCPInfo for extended info.

-// SocketDiagTCP requests INET_DIAG_INFO for TCP protocol for specified family type and returns related sockets.
+// SocketDiagTCP performs TCP socket diagnostics (INET_DIAG) for the specified family
+// and returns base Socket records. For extended TCPInfo, use SocketDiagTCPInfo.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// SocketDiagTCP requests INET_DIAG_INFO for TCP protocol for specified family type and returns related sockets.
//
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
// or incomplete.
func (h *Handle) SocketDiagTCP(family uint8) ([]*Socket, error) {
opts := SocketDiagTCPOptions{
Families: 1 << family,
}
return h.SocketDiagTCPWithOptions(opts)
}
// SocketDiagTCP performs TCP socket diagnostics (INET_DIAG) for the specified family
// and returns base Socket records. For extended TCPInfo, use SocketDiagTCPInfo.
//
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
// or incomplete.
func (h *Handle) SocketDiagTCP(family uint8) ([]*Socket, error) {
opts := SocketDiagTCPOptions{
Families: 1 << family,
}
return h.SocketDiagTCPWithOptions(opts)
}
🤖 Prompt for AI Agents
In socket_linux.go around lines 415 to 424, the function comment incorrectly
states it "requests INET_DIAG_INFO" and should instead document that this
wrapper returns base Socket structures (not INET_DIAG_INFO); update the
docstring to say it returns base sockets and add a brief hint referencing
SocketDiagTCPInfo for callers who need extended INET_DIAG_INFO details or richer
diagnostics.


// SocketDiagTCP requests INET_DIAG_INFO for TCP protocol for specified family type and return related socket.
// SocketDiagTCP requests INET_DIAG_INFO for TCP protocol for specified family type and returns related sockets.
//
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
// or incomplete.
Expand Down