Skip to content
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/bastjan/netstat

require github.com/google/go-cmp v0.2.0

go 1.13
23 changes: 22 additions & 1 deletion netstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,29 @@ var (
UDP6 = &Protocol{"udp6", "net/udp6"}
)

type option int

const (
// SkipProcessLookup skips the rather slow PID lookup.
// Exe, Cmdline, and Pid will be empty if called with this options.
SkipProcessLookup option = iota + 1
)

var (
procFdLinkParseType1 = regexp.MustCompile(`^socket:\[(\d+)\]$`)
procFdLinkParseType2 = regexp.MustCompile(`^\[0000\]:(\d+)$`)
)

// Connections queries the given /proc/net file and returns the found connections.
// Returns an error if the /proc/net file can't be read.
func (p *Protocol) Connections() ([]*Connection, error) {
func (p *Protocol) Connections(options ...option) ([]*Connection, error) {
inodeToPid := make(chan map[uint64]int)

go func() {
if hasOption(options, SkipProcessLookup) {
inodeToPid <- make(map[uint64]int)
return
}
inodeToPid <- procFdInodeToPid()
}()

Expand Down Expand Up @@ -227,3 +239,12 @@ func procFdExtractInode(fdLinkTarget string) (inode uint64, found bool) {
inode, _ = strconv.ParseUint(match[1], 10, 64)
return inode, true
}

func hasOption(os []option, o option) bool {
for _, ot := range os {
if ot == o {
return true
}
}
return false
}
26 changes: 26 additions & 0 deletions netstat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ func TestConnections(t *testing.T) {
compareResult(netstat.TCP6, []*netstat.Connection{tcp6Connection})
}

func TestConnectionsOptionSkipPidLookup(t *testing.T) {
compareResult := func(p *netstat.Protocol, expected []*netstat.Connection) {
connections, err := p.Connections(netstat.SkipProcessLookup)
if err != nil {
t.Error("Connections() returned unexpected errors:", err)
}
if diff := cmp.Diff(connections, expected); diff != "" {
t.Error("Connections() returned connections differ from expected connections:\n", diff)
}
}
compareResult(netstat.TCP, []*netstat.Connection{stripProcessInfo(tcpConnection)})
compareResult(netstat.TCP6, []*netstat.Connection{stripProcessInfo(tcp6Connection)})
}

func TestConnectionsProcNetNotFound(t *testing.T) {
_, err := (&netstat.Protocol{RelPath: "./nothere"}).Connections()
expectError(t, err, "test/proc/nothere: no such file or directory", "Connections() should return an error if the proc file can't be found")
Expand All @@ -87,3 +101,15 @@ func expectError(t *testing.T, err error, expectedErr, nilMessage string) {
}
t.Error("Error message should contain filename and error.", "Expected:", expectedErr, "Got:", err.Error())
}

func stripProcessInfo(c *netstat.Connection) *netstat.Connection {
r := new(netstat.Connection)

*r = *c

r.Exe = ""
r.Cmdline = []string{}
r.Pid = 0

return r
}