Skip to content

Commit ee57b47

Browse files
committed
Return more data from Info calls
1 parent 71e3dab commit ee57b47

File tree

7 files changed

+94
-19
lines changed

7 files changed

+94
-19
lines changed

protocols/arp.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,19 @@ func (p ARPPacket) Source() string {
6565
}
6666

6767
func (p ARPPacket) Info() string {
68-
return fmt.Sprintf("%s ARP packet from %s to %s", HardwareTypeValues[p.HardwareType], p.Source(), p.Destination())
68+
hwType := HardwareTypeValues[p.HardwareType]
69+
70+
return fmt.Sprintf(`
71+
ARP Packet
72+
73+
Hardware Type: %s
74+
Protocol Type: %d
75+
Sender HW Address: %s
76+
Target HW Address: %s
77+
78+
===============================
79+
%s`,
80+
hwType, p.ProtocolType, p.SenderHWAddr.String(), p.TargetHWAddr.String(), p.EthFrame.Info(),
81+
)
82+
6983
}

protocols/eth.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,14 @@ func (f EthernetFrame) Type() string {
4848

4949
// Info returns an human-readable string containing all the ETH frame data
5050
func (f EthernetFrame) Info() string {
51-
return fmt.Sprintf("%s Ethernet Frame from MAC %s to MAC %s", f.Type(), f.SourceMAC, f.DestinationMAC)
51+
etv := EtherTypesValues[f.EtherType]
52+
53+
return fmt.Sprintf(`
54+
Ethernet Frame
55+
56+
Destination MAC: %s
57+
Source MAC: %s
58+
EtherType: 0x%X (%s)`,
59+
f.DestinationMAC, f.SourceMAC, f.EtherType, etv,
60+
)
5261
}

protocols/ip.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,30 @@ func (p ipv4Packet) Payload() []byte {
155155

156156
// Info returns an human-readable string containing the main IPv4 packet data
157157
func (p ipv4Packet) Info() string {
158-
return fmt.Sprintf("%s IPv4 packet from IP %s to IP %s",
159-
p.header.TransportLayerProtocol(), p.header.sourceIP, p.header.destinationIP,
158+
return fmt.Sprintf(`
159+
IPv4 packet
160+
161+
Version: %d
162+
Header Length: %d bytes
163+
DSCP: %d
164+
ECN: %d
165+
Total Length: %d
166+
Identification: %d
167+
Flags: %d
168+
Fragment Offset: %d
169+
TTL: %d
170+
Transport Layer Protocol: %d (%s)
171+
Header Checksum: %d
172+
Source IP: %s
173+
Destination IP: %s
174+
Options: %v
175+
176+
===============================
177+
%s`,
178+
p.header.version, p.header.ihl*4, p.header.dscp, p.header.ecn, p.header.totalLength, p.header.identification,
179+
p.header.flags, p.header.fragmentOffset, p.header.ttl, p.header.protocol, p.header.TransportLayerProtocol(), p.header.headerChecksum,
180+
p.header.sourceIP, p.header.destinationIP, p.header.options,
181+
p.ethFrame.Info(),
160182
)
161183
}
162184

@@ -247,8 +269,24 @@ func (p ipv6Packet) Payload() []byte {
247269

248270
// Info returns an human-readable string containing the main IPv6 packet data
249271
func (p ipv6Packet) Info() string {
250-
return fmt.Sprintf("%s IPv6 packet from IP %s to IP %s",
251-
p.header.TransportLayerProtocol(), p.header.sourceIP, p.header.destinationIP,
272+
return fmt.Sprintf(`
273+
IPv6 packet
274+
275+
Version: %d
276+
Traffic Class: %d
277+
Flow Label: %d
278+
Payload Length: %d
279+
Transport Layer Protocol: %d (%s)
280+
Hop Limit: %d
281+
Source IP: %s
282+
Destination IP: %s
283+
===============================
284+
%s
285+
===============================
286+
`,
287+
p.header.version, p.header.trafficClass, p.header.flowLabel, p.header.payloadLength,
288+
p.header.nextHeader, p.header.TransportLayerProtocol(), p.header.hopLimit, p.header.sourceIP, p.header.destinationIP,
289+
p.ethFrame.Info(),
252290
)
253291
}
254292

protocols/tcp.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,16 @@ func TCPHeaderFromBytes(raw []byte) (*TCPHeader, error) {
7171

7272
// Info return an human-readable string containing the main TCP packet data
7373
func (p TCPPacket) Info() string {
74-
return fmt.Sprintf("%s - port %d to port %d",
75-
p.IPPacket.Info(), p.Header.SourcePort, p.Header.DestinationPort,
74+
return fmt.Sprintf(`
75+
TCP packet
76+
77+
Source Port: %d
78+
Destination Port: %d
79+
Checksum: %d
80+
81+
===============================
82+
%s`,
83+
p.Header.SourcePort, p.Header.SourcePort, p.Header.Checksum, p.IPPacket.Info(),
7684
)
7785
}
7886

protocols/udp.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,17 @@ func UDPPacketFromIPPacket(ip IPPacket) (*UDPPacket, error) {
3636

3737
// Info return an human-readable string containing the main UDP packet data
3838
func (p UDPPacket) Info() string {
39-
return fmt.Sprintf("%s - port %d to port %d",
40-
p.IPPacket.Info(), p.Header.SourcePort, p.Header.DestinationPort,
39+
return fmt.Sprintf(`
40+
UDP packet
41+
42+
Source Port: %d
43+
Destination Port: %d
44+
Length: %d
45+
Checksum: %d
46+
47+
===============================
48+
%s`,
49+
p.Header.SourcePort, p.Header.SourcePort, p.Header.Length, p.Header.Checksum, p.IPPacket.Info(),
4150
)
4251
}
4352

tui/models/bisturi_model.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,12 @@ func (m bisturiModel) View() string {
103103

104104
switch m.step {
105105
case retrieveIfaces:
106-
sb.WriteString(fmt.Sprintf("\n\nWelcome!\n\nRetrieving network interfaces \n\n%s", m.spinner.View()))
106+
sb.WriteString(fmt.Sprintf("\nWelcome!\nRetrieving network interfaces \n\n%s", m.spinner.View()))
107107
case selectIface, selectProtocol:
108108
sb.WriteString(m.startMenu.View())
109109
case selectRows:
110110
sb.WriteString(m.rowsInput.View())
111111
case receivePackets:
112-
sb.WriteString(fmt.Sprintf("\n\nReceiving %s packets on %s ...\n\n", m.selectedProtocol, m.selectedInterface.Name))
113112
sb.WriteString(m.packetsTable.View())
114113
default:
115114
sb.WriteString("The program is in an unknown state\nQuit with 'q'")

tui/models/packets_table.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package tui
22

33
import (
4-
"fmt"
54
"time"
65

76
"github.com/NamelessOne91/bisturi/sockets"
8-
"github.com/NamelessOne91/bisturi/tui/styles"
97
tea "github.com/charmbracelet/bubbletea"
108
"github.com/charmbracelet/lipgloss"
119
"github.com/evertras/bubble-table/table"
1210
)
1311

1412
const (
1513
columnKeyID = "id"
16-
columnKeyDate = "date"
14+
columnKeyTime = "time"
1715
columnKeySource = "source"
1816
columnKeyDestination = "destination"
1917
columnKeyInfo = "info"
@@ -31,7 +29,7 @@ type packetsTableModel struct {
3129
func (m *packetsTableModel) buildTable() {
3230
m.table = table.New([]table.Column{
3331
table.NewColumn(columnKeyID, "#", (3*m.width)/100),
34-
table.NewColumn(columnKeyDate, "Date", (7*m.width)/100),
32+
table.NewColumn(columnKeyTime, "Time", (7*m.width)/100),
3533
table.NewColumn(columnKeySource, "Source", (20*m.width)/100),
3634
table.NewColumn(columnKeyDestination, "Destination", (20*m.width)/100),
3735
}).
@@ -95,9 +93,10 @@ func (m packetsTableModel) View() string {
9593
detailsBox := lipgloss.NewStyle().
9694
BorderStyle(lipgloss.NormalBorder()).
9795
BorderForeground(lipgloss.Color("#00cc99")).
96+
Foreground(lipgloss.Color("#00cc99")).
9897
Padding(1, 2).
9998
Width((40 * m.width) / 100).
100-
Height((80 * m.height) / 100).
99+
Height((90 * m.height) / 100).
101100
Render(detailTxt)
102101

103102
mainView := lipgloss.JoinHorizontal(
@@ -108,7 +107,6 @@ func (m packetsTableModel) View() string {
108107

109108
view := lipgloss.JoinVertical(
110109
lipgloss.Left,
111-
styles.Subtle.Render(fmt.Sprintf("Displaying up to the last %d rows", m.maxRows)),
112110
mainView,
113111
) + "\n"
114112

@@ -135,7 +133,7 @@ func (m *packetsTableModel) addRows(packets []sockets.NetworkPacket) {
135133

136134
newRow := table.NewRow(table.RowData{
137135
columnKeyID: m.counter,
138-
columnKeyDate: time.Now().Local().Format(time.TimeOnly),
136+
columnKeyTime: time.Now().Local().Format(time.TimeOnly),
139137
columnKeySource: np.Source(),
140138
columnKeyDestination: np.Destination(),
141139
columnKeyInfo: np.Info(),

0 commit comments

Comments
 (0)