Skip to content

Commit

Permalink
Add Connection.User() to lookup user associated with the connection
Browse files Browse the repository at this point in the history
  • Loading branch information
bastjan committed Oct 21, 2018
1 parent 3dc8bec commit 5df161a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 36 deletions.
49 changes: 49 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package netstat

import (
"net"
"os/user"
)

// Connection contains the gathered information about an open network connection.
type Connection struct {
// Exe contains the path to the process.
// Exe is empty if there was an error reading /proc/pid/exe.
Exe string
// Cmdline contains the complete command line for the process split by \000. Trailing \000 removed.
// Returns an empty array if /proc/pid/cmdline can't be read.
Cmdline []string
// Pid contains the pid of the process. Is zero if open connection can't be assigned to a pid.
Pid int

// UserID represents the user account id of the user owning the socket.
// On Linux systems it is usually a uint32.
// Type string was chosen because os/user.LookupId() wants a string.
UserID string

// Inode contains the inode for the open connection.
Inode uint64

// IP holds the local IP for the connection.
IP net.IP
// Port holds the local port for the connection.
Port int
// RemoteIP holds the remote IP for the connection.
RemoteIP net.IP
// RemotePort holds the remote port for the connection.
RemotePort int
// State represents the state of a TCP connection. The UDP 'states' shown
// are recycled from TCP connection states and have a slightly different meaning.
State TCPState

// TransmitQueue is the outgoing data queue in terms of kernel memory usage in bytes.
TransmitQueue uint64
// ReceiveQueue is the incoming data queue in terms of kernel memory usage in bytes.
ReceiveQueue uint64
}

// User looks up the user owning the socket.
// If the user cannot be found, the returned error is of type UnknownUserIdError.
func (c *Connection) User() (*user.User, error) {
return user.LookupId(c.UserID)
}
20 changes: 20 additions & 0 deletions connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package netstat_test

import (
"os/user"
"testing"

"gotest.tools/assert"

"github.com/bastjan/netstat"
)

func TestConnectionUser(t *testing.T) {
validUser, _ := user.Current()
subject := &netstat.Connection{UserID: validUser.Uid}

expectedUser, expectedErr := user.LookupId(validUser.Uid)
user, err := subject.User()
assert.DeepEqual(t, expectedUser, user)
assert.DeepEqual(t, expectedErr, err)
}
36 changes: 0 additions & 36 deletions netstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,6 @@ type Netstat struct {
RelPath string
}

// Connection contains the gathered information about an open network connection.
type Connection struct {
// Exe contains the path to the process.
// Exe is empty if there was an error reading /proc/pid/exe.
Exe string
// Cmdline contains the complete command line for the process split by \000. Trailing \000 removed.
// Returns an empty array if /proc/pid/cmdline can't be read.
Cmdline []string
// Pid contains the pid of the process. Is zero if open connection can't be assigned to a pid.
Pid int

// UserID represents a user account id. On Linux systems it is usually a uint32.
// Type string was chosen because os/user.LookupId() wants a string.
UserID string

// Inode contains the inode for the open connection.
Inode uint64

// IP holds the local IP for the connection.
IP net.IP
// Port holds the local port for the connection.
Port int
// RemoteIP holds the remote IP for the connection.
RemoteIP net.IP
// RemotePort holds the remote port for the connection.
RemotePort int
// State represents the state of a TCP connection. The UDP 'states' shown
// are recycled from TCP connection states and have a slightly different meaning.
State TCPState

// TransmitQueue is the outgoing data queue in terms of kernel memory usage in bytes.
TransmitQueue uint64
// ReceiveQueue is the incoming data queue in terms of kernel memory usage in bytes.
ReceiveQueue uint64
}

// ProcRoot should point to the root of the proc file system
var ProcRoot = "/proc"

Expand Down

0 comments on commit 5df161a

Please sign in to comment.