diff --git a/examples/xnet/main.go b/examples/xnet/main.go index d91b29d..7667f9d 100644 --- a/examples/xnet/main.go +++ b/examples/xnet/main.go @@ -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) } } } diff --git a/internet/pcap/capture.go b/internet/pcap/capture.go index b5a1c7e..3b4e1d0 100644 --- a/internet/pcap/capture.go +++ b/internet/pcap/capture.go @@ -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, }, diff --git a/internet/pcap/format.go b/internet/pcap/format.go index e8ec509..2d54439 100644 --- a/internet/pcap/format.go +++ b/internet/pcap/format.go @@ -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) diff --git a/internet/pcap/stringers.go b/internet/pcap/stringers.go index 3af28c7..b195f53 100644 --- a/internet/pcap/stringers.go +++ b/internet/pcap/stringers.go @@ -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) { diff --git a/stringers.go b/stringers.go index 98a48b2..cc4d002 100644 --- a/stringers.go +++ b/stringers.go @@ -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 diff --git a/x/xnet/xnet_test.go b/x/xnet/xnet_test.go index 3e19ddc..9f56c30 100644 --- a/x/xnet/xnet_test.go +++ b/x/xnet/xnet_test.go @@ -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)) }