Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sock_diag): add state filter option to SocketDiag #916

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
28 changes: 22 additions & 6 deletions socket_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,17 @@ func SocketGet(local, remote net.Addr) (*Socket, error) {
}

// SocketDiagTCPInfo requests INET_DIAG_INFO for TCP protocol for specified family type and return with extension TCP info.
func SocketDiagTCPInfo(family uint8) ([]*InetDiagTCPInfoResp, error) {
func SocketDiagTCPInfo(family uint8, states ...uint8) ([]*InetDiagTCPInfoResp, error) {
var state = uint32(1<<TCP_MAX_STATES - 1) // default all state
if len(states) > 0 {
state = 0
for _, s := range states {
state |= 1 << s
}
}

var result []*InetDiagTCPInfoResp
err := socketDiagTCPExecutor(family, func(m syscall.NetlinkMessage) error {
err := socketDiagTCPExecutor(family, state, func(m syscall.NetlinkMessage) error {
sockInfo := &Socket{}
if err := sockInfo.deserialize(m.Data); err != nil {
return err
Expand All @@ -200,9 +208,17 @@ func SocketDiagTCPInfo(family uint8) ([]*InetDiagTCPInfoResp, error) {
}

// SocketDiagTCP requests INET_DIAG_INFO for TCP protocol for specified family type and return related socket.
func SocketDiagTCP(family uint8) ([]*Socket, error) {
func SocketDiagTCP(family uint8, states ...uint8) ([]*Socket, error) {
var state = uint32(1<<TCP_MAX_STATES - 1) // default all state
if len(states) > 0 {
state = 0
for _, s := range states {
state |= 1 << s
}
}

var result []*Socket
err := socketDiagTCPExecutor(family, func(m syscall.NetlinkMessage) error {
err := socketDiagTCPExecutor(family, state, func(m syscall.NetlinkMessage) error {
sockInfo := &Socket{}
if err := sockInfo.deserialize(m.Data); err != nil {
return err
Expand All @@ -217,7 +233,7 @@ func SocketDiagTCP(family uint8) ([]*Socket, error) {
}

// socketDiagTCPExecutor requests INET_DIAG_INFO for TCP protocol for specified family type.
func socketDiagTCPExecutor(family uint8, receiver func(syscall.NetlinkMessage) error) error {
func socketDiagTCPExecutor(family uint8, state uint32, receiver func(syscall.NetlinkMessage) error) error {
s, err := nl.Subscribe(unix.NETLINK_INET_DIAG)
if err != nil {
return err
Expand All @@ -229,7 +245,7 @@ func socketDiagTCPExecutor(family uint8, receiver func(syscall.NetlinkMessage) e
Family: family,
Protocol: unix.IPPROTO_TCP,
Ext: (1 << (INET_DIAG_VEGASINFO - 1)) | (1 << (INET_DIAG_INFO - 1)),
States: uint32(0xfff), // All TCP states
States: state, // All TCP states
})
s.Send(req)

Expand Down
26 changes: 26 additions & 0 deletions socket_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

package netlink
Expand Down Expand Up @@ -75,3 +76,28 @@ func TestSocketDiagTCPInfo(t *testing.T) {
}
}
}

func TestSocketDiagTCPInfoFilterState(t *testing.T) {
Family4 := uint8(syscall.AF_INET)
Family6 := uint8(syscall.AF_INET6)
families := []uint8{Family4, Family6}

wantState := uint8(TCP_LISTEN)
for _, wantFamily := range families {
res, err := SocketDiagTCPInfo(wantFamily, wantState)
if err != nil {
t.Fatal(err)
}
for _, i := range res {
gotFamily := i.InetDiagMsg.Family
if gotFamily != wantFamily {
t.Fatalf("Socket family = %d, want %d", gotFamily, wantFamily)
}

gotState := i.InetDiagMsg.State
if gotState != wantState {
t.Fatalf("Socket state = %d, want %d", gotState, wantState)
}
}
}
}