Skip to content

pcapgo/read.go: Shouldn't ReadPacketData try to peek the packet header to check for corruption before reading? #550

@assafmo

Description

@assafmo

In this code section:

gopacket/pcapgo/read.go

Lines 119 to 140 in a35e09f

func (r *Reader) ReadPacketData() (data []byte, ci gopacket.CaptureInfo, err error) {
if ci, err = r.readPacketHeader(); err != nil {
return
}
if ci.CaptureLength > int(r.snaplen) {
err = fmt.Errorf("capture length exceeds snap length: %d > %d", 16+ci.CaptureLength, r.snaplen)
return
}
data = make([]byte, ci.CaptureLength)
_, err = io.ReadFull(r.r, data)
return data, ci, err
}
func (r *Reader) readPacketHeader() (ci gopacket.CaptureInfo, err error) {
if _, err = io.ReadFull(r.r, r.buf[:]); err != nil {
return
}
ci.Timestamp = time.Unix(int64(r.byteOrder.Uint32(r.buf[0:4])), int64(r.byteOrder.Uint32(r.buf[4:8])*r.nanoSecsFactor)).UTC()
ci.CaptureLength = int(r.byteOrder.Uint32(r.buf[8:12]))
ci.Length = int(r.byteOrder.Uint32(r.buf[12:16]))
return
}

I think maybe ReadPacketData should peek 16 bytes, check for corruption according to the packet header specs, and if there is corruption then skip 1 bytes.

My thinking is that maybe the next good packet header can start somewhere in the last 15 bytes of the 16 bytes read.

Right now, if there is corruption somewhere in the input file, pcapgo/read.go reads in increments of 16 bytes and might miss on good packets.

This of course will be much slower when dealing with a corrupted pcap file, but OTOH might find more good packets otherwise missed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions