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

avoid costly process info extraction if unnecessary both on Linux and Windows #19

Open
wants to merge 6 commits into
base: master
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
6 changes: 5 additions & 1 deletion netstat/netstat_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ func doNetstat(path string, fn AcceptFn) ([]SockTabEntry, error) {
if err != nil {
return nil, err
}
extractProcInfo(tabs)

if len(tabs) != 0 {
extractProcInfo(tabs)
}

return tabs, nil
}

Expand Down
80 changes: 57 additions & 23 deletions netstat/netstat_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build amd64
// +build amd64

package netstat
Expand Down Expand Up @@ -118,7 +119,7 @@ type MibTCPRow2 struct {

type WinPid uint32

func (pid WinPid) Process(snp ProcessSnapshot) *Process {
func sockProcess(snp ProcessSnapshot, pid uint32) *Process {
if pid < 1 {
return nil
}
Expand All @@ -131,6 +132,7 @@ func (pid WinPid) Process(snp ProcessSnapshot) *Process {
func (m *MibTCPRow2) LocalSock() *SockAddr { return m.LocalAddr.Sock() }
func (m *MibTCPRow2) RemoteSock() *SockAddr { return m.RemoteAddr.Sock() }
func (m *MibTCPRow2) SockState() SkState { return SkState(m.State) }
func (m *MibTCPRow2) UID() uint32 { return uint32(m.WinPid) }

type MibTCPTable2 struct {
NumEntries uint32
Expand Down Expand Up @@ -159,6 +161,7 @@ type MibTCP6Row2 struct {
func (m *MibTCP6Row2) LocalSock() *SockAddr { return m.LocalAddr.Sock() }
func (m *MibTCP6Row2) RemoteSock() *SockAddr { return m.RemoteAddr.Sock() }
func (m *MibTCP6Row2) SockState() SkState { return SkState(m.State) }
func (m *MibTCP6Row2) UID() uint32 { return uint32(m.WinPid) }

// MibTCP6Table2 structure contains a table of IPv6 TCP connections on the
// local computer.
Expand Down Expand Up @@ -188,6 +191,7 @@ type MibUDPRowOwnerPID struct {
func (m *MibUDPRowOwnerPID) LocalSock() *SockAddr { return m.Sock() }
func (m *MibUDPRowOwnerPID) RemoteSock() *SockAddr { return &SockAddr{net.IPv4zero, 0} }
func (m *MibUDPRowOwnerPID) SockState() SkState { return Close }
func (m *MibUDPRowOwnerPID) UID() uint32 { return uint32(m.WinPid) }

// MibUDPTableOwnerPID structure contains the User Datagram Protocol (UDP)
// listener table for IPv4 on the local computer. The table also includes the
Expand Down Expand Up @@ -217,6 +221,7 @@ type MibUDP6RowOwnerPID struct {
func (m *MibUDP6RowOwnerPID) LocalSock() *SockAddr { return m.Sock() }
func (m *MibUDP6RowOwnerPID) RemoteSock() *SockAddr { return &SockAddr{net.IPv4zero, 0} }
func (m *MibUDP6RowOwnerPID) SockState() SkState { return Close }
func (m *MibUDP6RowOwnerPID) UID() uint32 { return uint32(m.WinPid) }

// MibUDP6TableOwnerPID serves the same purpose as MibUDPTableOwnerPID for IPv6
type MibUDP6TableOwnerPID struct {
Expand Down Expand Up @@ -468,15 +473,15 @@ type winSockEnt interface {
LocalSock() *SockAddr
RemoteSock() *SockAddr
SockState() SkState
Process(snp ProcessSnapshot) *Process
UID() uint32
}

func toSockTabEntry(ws winSockEnt, snp ProcessSnapshot) SockTabEntry {
func toSockTabEntry(ws winSockEnt) SockTabEntry {
return SockTabEntry{
LocalAddr: ws.LocalSock(),
RemoteAddr: ws.RemoteSock(),
State: ws.SockState(),
Process: ws.Process(snp),
UID: uint32(ws.UID()),
}
}

Expand All @@ -485,19 +490,28 @@ func osTCPSocks(accept AcceptFn) ([]SockTabEntry, error) {
if err != nil {
return nil, err
}
snp, err := CreateToolhelp32Snapshot(Th32csSnapProcess, 0)
if err != nil {
return nil, err
}

var sktab []SockTabEntry
s := tbl.Rows()
for i := range s {
ent := toSockTabEntry(&s[i], snp)
ent := toSockTabEntry(&s[i])
if accept(&ent) {
sktab = append(sktab, ent)
}
}
snp.Close()
if len(sktab) != 0 {
snp, err := CreateToolhelp32Snapshot(Th32csSnapProcess, 0)
if err != nil {
return nil, err
}

for i := range sktab {
sktab[i].Process = sockProcess(snp, sktab[i].UID)
}

snp.Close()
}

return sktab, nil
}

Expand All @@ -513,7 +527,7 @@ func osTCP6Socks(accept AcceptFn) ([]SockTabEntry, error) {
var sktab []SockTabEntry
s := tbl.Rows()
for i := range s {
ent := toSockTabEntry(&s[i], snp)
ent := toSockTabEntry(&s[i])
if accept(&ent) {
sktab = append(sktab, ent)
}
Expand All @@ -527,19 +541,29 @@ func osUDPSocks(accept AcceptFn) ([]SockTabEntry, error) {
if err != nil {
return nil, err
}
snp, err := CreateToolhelp32Snapshot(Th32csSnapProcess, 0)
if err != nil {
return nil, err
}

var sktab []SockTabEntry
s := tbl.Rows()
for i := range s {
ent := toSockTabEntry(&s[i], snp)
ent := toSockTabEntry(&s[i])
if accept(&ent) {
sktab = append(sktab, ent)
}
}
snp.Close()

if len(sktab) != 0 {
snp, err := CreateToolhelp32Snapshot(Th32csSnapProcess, 0)
if err != nil {
return nil, err
}

for i := range sktab {
sktab[i].Process = sockProcess(snp, sktab[i].UID)
}

snp.Close()
}

return sktab, nil
}

Expand All @@ -548,18 +572,28 @@ func osUDP6Socks(accept AcceptFn) ([]SockTabEntry, error) {
if err != nil {
return nil, err
}
snp, err := CreateToolhelp32Snapshot(Th32csSnapProcess, 0)
if err != nil {
return nil, err
}

var sktab []SockTabEntry
s := tbl.Rows()
for i := range s {
ent := toSockTabEntry(&s[i], snp)
ent := toSockTabEntry(&s[i])
if accept(&ent) {
sktab = append(sktab, ent)
}
}
snp.Close()

if len(sktab) != 0 {
snp, err := CreateToolhelp32Snapshot(Th32csSnapProcess, 0)
if err != nil {
return nil, err
}

for i := range sktab {
sktab[i].Process = sockProcess(snp, sktab[i].UID)
}

snp.Close()
}

return sktab, nil
}