From 4fa93202be03548397aa3c60717e141a31b2b778 Mon Sep 17 00:00:00 2001 From: Graham Clark Date: Fri, 26 Nov 2021 12:48:05 -0500 Subject: [PATCH] Provide a better table sorter for the conversations view 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. --- ui/convsui.go | 21 +++++++++++++++++---- utils.go | 30 ++++++++++++++++++++++++++++++ utils_test.go | 10 ++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/ui/convsui.go b/ui/convsui.go index 474c47a..5ff6a7e 100644 --- a/ui/convsui.go +++ b/ui/convsui.go @@ -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 ( @@ -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", @@ -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{}, @@ -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{}, diff --git a/utils.go b/utils.go index 8588ea7..f56da4a 100644 --- a/utils.go +++ b/utils.go @@ -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 diff --git a/utils_test.go b/utils_test.go index 3288ab0..514846a 100644 --- a/utils_test.go +++ b/utils_test.go @@ -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")