Skip to content

Commit

Permalink
Provide a better table sorter for the conversations view
Browse files Browse the repository at this point in the history
Prior to this change, the address columns were sorted as string. This
meant that, for example, 108.177.111.188 would appear in order before
13.225.190.211. This change makes use of better IPCompare and MACCompare
structs to provide the sorting logic in the conversations UI.
  • Loading branch information
gcla committed Nov 26, 2021
1 parent df4d2aa commit 4fa9320
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
21 changes: 17 additions & 4 deletions ui/convsui.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ const (
From Direction = iota
)

type ConvAddr int

const (
IPv4Addr ConvAddr = 0
IPv6Addr ConvAddr = iota
MacAddr ConvAddr = iota
)

type FilterMask int

const (
Expand Down Expand Up @@ -833,6 +841,11 @@ func (w *ConvsUiWidget) OnData(data string, app gowid.IApp) {
ports = termshark.StringInSlice(cur, []string{"UDP", "TCP"})
ipv6 := (cur == "IPv6")

var addrComp table.ICompare = termshark.IPCompare{}
if termshark.StringInSlice(cur, []string{"Ethernet"}) {
addrComp = termshark.MACCompare{}
}

if ports {
hdrs = []string{
"Addr A",
Expand Down Expand Up @@ -863,9 +876,9 @@ func (w *ConvsUiWidget) OnData(data string, app gowid.IApp) {
weightupto(200, 8), // durn
}
comps = []table.ICompare{
table.StringCompare{},
addrComp,
table.IntCompare{},
table.StringCompare{},
addrComp,
table.IntCompare{},
table.IntCompare{},
table.IntCompare{},
Expand Down Expand Up @@ -908,8 +921,8 @@ func (w *ConvsUiWidget) OnData(data string, app gowid.IApp) {
wids[1] = weightupto(500, 42)
}
comps = []table.ICompare{
table.StringCompare{},
table.StringCompare{},
addrComp,
addrComp,
table.IntCompare{},
table.IntCompare{},
table.IntCompare{},
Expand Down
30 changes: 30 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,36 @@ var _ table.ICompare = IPCompare{}

//======================================================================

// MacCompare is a unit type that satisfies ICompare, and can be used
// for numerically comparing MAC addresses.
type MACCompare struct{}

func (s MACCompare) Less(i, j string) bool {
x, errx := net.ParseMAC(i)
y, erry := net.ParseMAC(j)
if errx == nil && erry == nil {
for i := 0; i < len(x); i++ {
switch {
case x[i] < y[i]:
return true
case y[i] < x[i]:
return false
}
}
return false
} else if errx == nil {
return true
} else if erry == nil {
return false
} else {
return i < j
}
}

var _ table.ICompare = MACCompare{}

//======================================================================

func PrunePcapCache() error {
// This is a new option. Best to err on the side of caution and, if not, present
// assume the cache can grow indefinitely - in case users are now relying on this
Expand Down
10 changes: 10 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ func TestIPComp1(t *testing.T) {
assert.False(t, ip.Less("2001:db8::68", "192.168.0.253"))
}

func TestMACComp1(t *testing.T) {
var mac MACCompare
assert.True(t, mac.Less("x", "y"))
assert.True(t, mac.Less("11:22:33:44:55:66", "y"))
assert.True(t, mac.Less("xx:22:33:44:55:66", "y"))
assert.False(t, mac.Less("xx:22:33:44:55:66", "11:22:33:44:55:66"))
assert.True(t, mac.Less("11:22:33:44:55:66", "11:22:33:44:55:67"))
assert.False(t, mac.Less("11:22:33:44:55:66", "11:22:33:44:54:66"))
}

func TestFolders(t *testing.T) {
tmp := os.Getenv("TMPDIR")
os.Setenv("TMPDIR", "/foo")
Expand Down

0 comments on commit 4fa9320

Please sign in to comment.