add FieldClassOperation to pcap and associated formatting changes

This commit is contained in:
soypat
2025-12-31 18:54:21 -03:00
parent dc374169dc
commit 3cbef6fa92
6 changed files with 17 additions and 21 deletions
+6 -7
View File
@@ -115,7 +115,7 @@ func run() (err error) {
var cap pcap.PacketBreakdown
var frames []pcap.Frame
pf := pcap.Formatter{
FilterClasses: []pcap.FieldClass{pcap.FieldClassFlags, pcap.FieldClassDst, pcap.FieldClassSrc, pcap.FieldClassAddress},
FilterClasses: []pcap.FieldClass{pcap.FieldClassFlags, pcap.FieldClassOperation, pcap.FieldClassDst, pcap.FieldClassSrc, pcap.FieldClassAddress},
}
var pfbuf []byte
logFrames := func(context string, pkt []byte) error {
@@ -163,12 +163,11 @@ func run() (err error) {
log.Fatal("groutine read:", err)
} else if nread > 0 {
err = stack.Demux(buf[:nread], 0)
if err != nil {
if !errors.Is(err, lneto.ErrPacketDrop) {
err = logFrames("IN", buf[:nread])
if err != nil {
log.Println("ERR:INLOG", err)
}
if !errors.Is(err, lneto.ErrPacketDrop) {
// Only skip logging packet in case of dropped packet.
err = logFrames("IN", buf[:nread])
if err != nil {
log.Println("ERR:INLOG", err)
}
}
}
+3 -4
View File
@@ -670,6 +670,7 @@ const (
FieldClassAddress // address
// FieldClassBinaryText represents long stretches of binary data such as BOOTP DHCPv4 field.
FieldClassBinaryText // binary-text
FieldClassOperation // op
)
const octet = 8
@@ -718,8 +719,7 @@ var baseARPFields = [...]FrameField{
BitLength: 1 * octet,
},
{
Name: "Opcode",
Class: FieldClassType,
Class: FieldClassOperation,
FrameBitOffset: 6 * octet,
BitLength: 2 * octet,
},
@@ -924,8 +924,7 @@ var baseUDPFields = [...]FrameField{
var baseDHCPv4Fields = [...]FrameField{
{
Name: "Opcode",
Class: FieldClassType,
Class: FieldClassOperation,
FrameBitOffset: 0,
BitLength: 1 * octet,
},
+1 -1
View File
@@ -112,7 +112,7 @@ func (f *Formatter) formatField(dst []byte, pktStartOff int, field FrameField, p
dst = hex.AppendEncode(dst, f.buf)
case FieldClassText:
dst = strconv.AppendQuote(dst, string(f.buf))
case FieldClassDst, FieldClassSrc, FieldClassSize, FieldClassAddress:
case FieldClassDst, FieldClassSrc, FieldClassSize, FieldClassAddress, FieldClassOperation:
// IP, MAC addresses and ports.
if field.BitLength <= 16 {
v, err := fieldAsUint(pkt, fieldBitStart, field.BitLength, field.RightAligned)
+3 -2
View File
@@ -23,11 +23,12 @@ func _() {
_ = x[FieldClassText-12]
_ = x[FieldClassAddress-13]
_ = x[FieldClassBinaryText-14]
_ = x[FieldClassOperation-15]
}
const _FieldClass_name = "undefinedsourcedestinationprotocolversiontypesizeflagsidentificationchecksumoptionspayloadtextaddressbinary-text"
const _FieldClass_name = "undefinedsourcedestinationprotocolversiontypesizeflagsidentificationchecksumoptionspayloadtextaddressbinary-textop"
var _FieldClass_index = [...]uint8{0, 9, 15, 26, 34, 41, 45, 49, 54, 68, 76, 83, 90, 94, 101, 112}
var _FieldClass_index = [...]uint8{0, 9, 15, 26, 34, 41, 45, 49, 54, 68, 76, 83, 90, 94, 101, 112, 114}
func (i FieldClass) String() string {
if i >= FieldClass(len(_FieldClass_index)-1) {
+3 -2
View File
@@ -195,11 +195,12 @@ func _() {
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[ErrPacketDrop-1]
_ = x[ErrBadCRC-2]
}
const _errGeneric_name = "packet dropped"
const _errGeneric_name = "packet droppedincorrect checksum"
var _errGeneric_index = [...]uint8{0, 14}
var _errGeneric_index = [...]uint8{0, 14, 32}
func (i errGeneric) String() string {
i -= 1
+1 -5
View File
@@ -2,7 +2,6 @@ package xnet
import (
"bytes"
"encoding/binary"
"errors"
"math/rand"
"net/netip"
@@ -602,8 +601,5 @@ func (tst *tester) getFieldByClassLen(proto any, class pcap.FieldClass, octetLen
func (tst *tester) getARPOperation() arp.Operation {
tst.t.Helper()
// ARP has 3 FieldClassType fields: Hardware type (0), Protocol type (1), Opcode (2)
// All are 2 bytes, so we need occurrence=2 to get Opcode.
data := tst.getFieldByClassLen(ethernet.TypeARP, pcap.FieldClassType, 2, 2)
return arp.Operation(binary.BigEndian.Uint16(data))
return arp.Operation(tst.getInt(ethernet.TypeARP, pcap.FieldClassOperation))
}