Add Read function

This commit is contained in:
Neale Pickett 2013-12-24 16:37:33 -07:00
parent 5a587e8898
commit af0ebc7836
1 changed files with 33 additions and 9 deletions

42
pcap.go
View File

@ -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 {