diff --git a/pcap.go b/pcap.go index 5bc44a6..3d15792 100644 --- a/pcap.go +++ b/pcap.go @@ -15,20 +15,13 @@ const MAXFRAME = 9000 const LINKTYPE_ETHERNET = 1 const LINKTYPE_RAW = 101 -type Frame struct { - ts time.Time - caplen uint32 - framelen uint32 - payload string -} - type FileHeader struct { VersionMajor uint16 VersionMinor uint16 ThisZone int32 SigFigs uint32 - SnapLen int32 - LinkType int32 + SnapLen uint32 + LinkType uint32 } type PcapFile struct { @@ -36,6 +29,17 @@ type PcapFile struct { header FileHeader } +type FrameHeader struct { + ts time.Time + caplen uint32 + framelen uint32 +} + +type Frame struct { + FrameHeader + payload []byte +} + type Reader struct { PcapFile r io.Reader @@ -67,6 +71,26 @@ func NewReader(r io.Reader) (*Reader, error) { return ret, nil } +func (r *Reader) Read() (*Frame, error) { + var h FrameHeader + + if err := binary.Read(r.r, r.order, &h); err != nil { + return nil, err + } + + payload := make([]byte, h.caplen) + l, err := r.r.Read(payload) + if err != nil { + return nil, err + } + if uint32(l) < h.caplen { + return nil, fmt.Errorf("Short read: %d (wanted %d)", l, h.caplen) + } + + ret := &Frame{h, payload} + return ret, nil +} + func main() { r, err := NewReader(os.Stdin) if err != nil {