Skip to content

Commit

Permalink
Sensibly shorten the output of Info calls
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessOne91 committed Jul 1, 2024
1 parent 2385b0b commit 662df87
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 78 deletions.
10 changes: 2 additions & 8 deletions protocols/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,9 @@ func EthFrameFromBytes(raw []byte) (*EthernetFrame, error) {
}, nil
}

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

return fmt.Sprintf(`Ethernet Frame
Destination MAC: %s
Source MAC: %s
EtherType: 0x%X (%s)`,
f.destinationMAC, f.sourceMAC, f.etherType, etv,
)
return fmt.Sprintf("%s Ethernet Frame from MAC %s to MAC %s", f.sourceMAC, f.destinationMAC, etv)
}
51 changes: 6 additions & 45 deletions protocols/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,10 @@ func (p ipv4Packet) Payload() []byte {
return p.payload
}

// Info returns an human-readable string containing the IPv6 packet data
// Info returns an human-readable string containing the main IPv4 packet data
func (p ipv4Packet) Info() string {
return fmt.Sprintf(`
IPv4 packet
Version: %d
Header Length: %d bytes
DSCP: %d
ECN: %d
Total Length: %d
Identification: %d
Flags: %d
Fragment Offset: %d
TTL: %d
Transport Layer Protocol: %d (%s)
Header Checksum: %d
Source IP: %s
Destination IP: %s
Options: %v
===============================
%s
===============================
`,
p.header.version, p.header.ihl*4, p.header.dscp, p.header.ecn, p.header.totalLength, p.header.identification,
p.header.flags, p.header.fragmentOffset, p.header.ttl, p.header.protocol, p.header.TransportLayerProtocol(), p.header.headerChecksum,
p.header.sourceIP, p.header.destinationIP, p.header.options,
p.ethFrame.Info(),
return fmt.Sprintf("%s IPv4 packet from IP %s to IP %s",
p.header.TransportLayerProtocol(), p.header.sourceIP, p.header.destinationIP,
)
}

Expand Down Expand Up @@ -249,26 +226,10 @@ func (p ipv6Packet) Payload() []byte {
return p.payload
}

// Info returns an human-readable string containing the IPv6 packet data
// Info returns an human-readable string containing the main IPv6 packet data
func (p ipv6Packet) Info() string {
return fmt.Sprintf(`
IPv6 packet
Version: %d
Traffic Class: %d
Flow Label: %d
Payload Length: %d
Transport Layer Protocol: %d (%s)
Hop Limit: %d
Source IP: %s
Destination IP: %s
===============================
%s
===============================
`,
p.header.version, p.header.trafficClass, p.header.flowLabel, p.header.payloadLength,
p.header.nextHeader, p.header.TransportLayerProtocol(), p.header.hopLimit, p.header.sourceIP, p.header.destinationIP,
p.ethFrame.Info(),
return fmt.Sprintf("%s IPv6 packet from IP %s to IP %s",
p.header.TransportLayerProtocol(), p.header.sourceIP, p.header.destinationIP,
)
}

Expand Down
15 changes: 3 additions & 12 deletions protocols/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,9 @@ func tcpHeaderFromBytes(raw []byte) (*tcpHeader, error) {
}, nil
}

// Info return an human-readable string containing the main TCP packet data
func (p TCPPacket) Info() string {
return fmt.Sprintf(`
TCP packet
Source Port: %d
Destination Port: %d
Checksum: %d
===============================
%s
===============================
`,
p.header.sourcePort, p.header.sourcePort, p.header.checksum, p.ipPacket.Info(),
return fmt.Sprintf("%s - port %d to port %d",
p.ipPacket.Info(), p.header.sourcePort, p.header.destinationPort,
)
}
20 changes: 7 additions & 13 deletions protocols/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type udpHeader struct {

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

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

Expand Down Expand Up @@ -65,23 +67,15 @@ func udpv6PacketFromBytes(raw []byte) (*UDPPacket, error) {
}, nil
}

// Info return an human-readable string containing the main UDP packet data
func (p UDPPacket) Info() string {
return fmt.Sprintf(`
UDP packet
Source Port: %d
Destination Port: %d
Length: %d
Checksum: %d
===============================
%s
===============================
`,
p.header.sourcePort, p.header.sourcePort, p.header.length, p.header.checksum, p.ipPacket.Info(),
return fmt.Sprintf("%s - port %d to port %d",
p.ipPacket.Info(), p.header.sourcePort, p.header.destinationPort,
)
}

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

0 comments on commit 662df87

Please sign in to comment.