Skip to content

Commit 662df87

Browse files
committed
Sensibly shorten the output of Info calls
1 parent 2385b0b commit 662df87

File tree

4 files changed

+18
-78
lines changed

4 files changed

+18
-78
lines changed

protocols/eth.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,9 @@ func EthFrameFromBytes(raw []byte) (*EthernetFrame, error) {
3939
}, nil
4040
}
4141

42-
// Info returns an human-readable string containing the ETH frame data
42+
// Info returns an human-readable string containing all the ETH frame data
4343
func (f *EthernetFrame) Info() string {
4444
etv := etherTypesValues[f.etherType]
4545

46-
return fmt.Sprintf(`Ethernet Frame
47-
48-
Destination MAC: %s
49-
Source MAC: %s
50-
EtherType: 0x%X (%s)`,
51-
f.destinationMAC, f.sourceMAC, f.etherType, etv,
52-
)
46+
return fmt.Sprintf("%s Ethernet Frame from MAC %s to MAC %s", f.sourceMAC, f.destinationMAC, etv)
5347
}

protocols/ip.go

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -142,33 +142,10 @@ func (p ipv4Packet) Payload() []byte {
142142
return p.payload
143143
}
144144

145-
// Info returns an human-readable string containing the IPv6 packet data
145+
// Info returns an human-readable string containing the main IPv4 packet data
146146
func (p ipv4Packet) Info() string {
147-
return fmt.Sprintf(`
148-
IPv4 packet
149-
150-
Version: %d
151-
Header Length: %d bytes
152-
DSCP: %d
153-
ECN: %d
154-
Total Length: %d
155-
Identification: %d
156-
Flags: %d
157-
Fragment Offset: %d
158-
TTL: %d
159-
Transport Layer Protocol: %d (%s)
160-
Header Checksum: %d
161-
Source IP: %s
162-
Destination IP: %s
163-
Options: %v
164-
===============================
165-
%s
166-
===============================
167-
`,
168-
p.header.version, p.header.ihl*4, p.header.dscp, p.header.ecn, p.header.totalLength, p.header.identification,
169-
p.header.flags, p.header.fragmentOffset, p.header.ttl, p.header.protocol, p.header.TransportLayerProtocol(), p.header.headerChecksum,
170-
p.header.sourceIP, p.header.destinationIP, p.header.options,
171-
p.ethFrame.Info(),
147+
return fmt.Sprintf("%s IPv4 packet from IP %s to IP %s",
148+
p.header.TransportLayerProtocol(), p.header.sourceIP, p.header.destinationIP,
172149
)
173150
}
174151

@@ -249,26 +226,10 @@ func (p ipv6Packet) Payload() []byte {
249226
return p.payload
250227
}
251228

252-
// Info returns an human-readable string containing the IPv6 packet data
229+
// Info returns an human-readable string containing the main IPv6 packet data
253230
func (p ipv6Packet) Info() string {
254-
return fmt.Sprintf(`
255-
IPv6 packet
256-
257-
Version: %d
258-
Traffic Class: %d
259-
Flow Label: %d
260-
Payload Length: %d
261-
Transport Layer Protocol: %d (%s)
262-
Hop Limit: %d
263-
Source IP: %s
264-
Destination IP: %s
265-
===============================
266-
%s
267-
===============================
268-
`,
269-
p.header.version, p.header.trafficClass, p.header.flowLabel, p.header.payloadLength,
270-
p.header.nextHeader, p.header.TransportLayerProtocol(), p.header.hopLimit, p.header.sourceIP, p.header.destinationIP,
271-
p.ethFrame.Info(),
231+
return fmt.Sprintf("%s IPv6 packet from IP %s to IP %s",
232+
p.header.TransportLayerProtocol(), p.header.sourceIP, p.header.destinationIP,
272233
)
273234
}
274235

protocols/tcp.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,9 @@ func tcpHeaderFromBytes(raw []byte) (*tcpHeader, error) {
6161
}, nil
6262
}
6363

64+
// Info return an human-readable string containing the main TCP packet data
6465
func (p TCPPacket) Info() string {
65-
return fmt.Sprintf(`
66-
TCP packet
67-
68-
Source Port: %d
69-
Destination Port: %d
70-
Checksum: %d
71-
72-
===============================
73-
%s
74-
===============================
75-
`,
76-
p.header.sourcePort, p.header.sourcePort, p.header.checksum, p.ipPacket.Info(),
66+
return fmt.Sprintf("%s - port %d to port %d",
67+
p.ipPacket.Info(), p.header.sourcePort, p.header.destinationPort,
7768
)
7869
}

protocols/udp.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type udpHeader struct {
2020

2121
var errInvalidUDPHeader = errors.New("UDP header must be 8 bytes")
2222

23+
// UDPPacketFromIPPacket parses the passed IPv4 or IPv6 packet's data returning a struct conatining the encapsulated UDP's packet data.
24+
// An error is returned if the headers' constraints are not respected.
2325
func UDPPacketFromIPPacket(ip IPPacket) (*UDPPacket, error) {
2426
udpHeader, err := udpHeaderFromBytes(ip.Payload())
2527

@@ -65,23 +67,15 @@ func udpv6PacketFromBytes(raw []byte) (*UDPPacket, error) {
6567
}, nil
6668
}
6769

70+
// Info return an human-readable string containing the main UDP packet data
6871
func (p UDPPacket) Info() string {
69-
return fmt.Sprintf(`
70-
UDP packet
71-
72-
Source Port: %d
73-
Destination Port: %d
74-
Length: %d
75-
Checksum: %d
76-
77-
===============================
78-
%s
79-
===============================
80-
`,
81-
p.header.sourcePort, p.header.sourcePort, p.header.length, p.header.checksum, p.ipPacket.Info(),
72+
return fmt.Sprintf("%s - port %d to port %d",
73+
p.ipPacket.Info(), p.header.sourcePort, p.header.destinationPort,
8274
)
8375
}
8476

77+
// udpHeaderFromBytes parses the passed bytes to a struct containing the UDP header data and returns a pointer to it.
78+
// It expects an array of at least 8 bytes
8579
func udpHeaderFromBytes(raw []byte) (*udpHeader, error) {
8680
if len(raw) < 8 {
8781
return nil, errInvalidUDPHeader

0 commit comments

Comments
 (0)