diff --git a/go.mod b/go.mod index 86c4a3c..a997305 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/cakturk/go-netstat +module github.com/abakum/go-netstat -go 1.13 +go 1.20 diff --git a/main.go b/main.go index 7a0c77c..fc71030 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,4 @@ +// rm go.mod;go mod init github.com/abakum/go-netstat;go mod tidy package main import ( @@ -6,7 +7,7 @@ import ( "net" "os" - "github.com/cakturk/go-netstat/netstat" + "github.com/abakum/go-netstat/netstat" ) var ( diff --git a/netstat/netstat.go b/netstat/netstat.go index 4d95c51..e89c09a 100644 --- a/netstat/netstat.go +++ b/netstat/netstat.go @@ -25,6 +25,22 @@ type SockTabEntry struct { Process *Process } +func (s SockTabEntry) String() string { + l := "" + if s.LocalAddr != nil { + l = s.LocalAddr.String() + } + r := "" + if s.RemoteAddr != nil { + r = s.RemoteAddr.String() + } + p := "" + if s.Process != nil { + p = s.Process.String() + } + return fmt.Sprintf("%s %s %s %s", l, r, s.State, p) +} + // Process holds the PID and process name to which each socket belongs type Process struct { Pid int diff --git a/netstat/netstat_linux.go b/netstat/netstat_etc.go similarity index 99% rename from netstat/netstat_linux.go rename to netstat/netstat_etc.go index 05fb56d..2c8299f 100644 --- a/netstat/netstat_linux.go +++ b/netstat/netstat_etc.go @@ -1,3 +1,6 @@ +//go:build !windows +// +build !windows + // Package netstat provides primitives for getting socket information on a // Linux based operating system. package netstat diff --git a/netstat/netstat_windows.go b/netstat/netstat_windows.go index 8e3b289..01dd6d2 100644 --- a/netstat/netstat_windows.go +++ b/netstat/netstat_windows.go @@ -1,4 +1,5 @@ -// +build amd64 +//go:build windows +// +build windows package netstat @@ -268,7 +269,7 @@ func rawGetTCPTable2(proc uintptr, tab unsafe.Pointer, size *uint32, order bool) return nil } -func getTCPTable2(proc uintptr, order bool) ([]byte, error) { +func getTCPTable2(proc uintptr, _ bool) ([]byte, error) { var ( size uint32 buf []byte