forked from gravwell/ipfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.go
43 lines (36 loc) · 1.07 KB
/
read.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package ipfix
import "io"
// Read reads and returns an IPFIX message, the parsed message header and
// an error or nil. The given byte slice is used and returned (sliced to the
// message length) if it is large enough to contain the message; otherwise a
// new slice is allocated. The returned message slice contains the message
// header.
func Read(r io.Reader, bs []byte) ([]byte, MessageHeader, error) {
if len(bs) < msgIpfixHeaderLength {
bs = make([]byte, 65536)
}
_, err := io.ReadFull(r, bs[:msgIpfixHeaderLength])
if err != nil {
return nil, MessageHeader{}, err
}
var hdr MessageHeader
hdr.unmarshal(newSlice(bs))
if hdr.Version != 10 {
return nil, hdr, ErrVersion
}
if len(bs) < int(hdr.Length) {
newBs := make([]byte, 65536)
copy(newBs, bs[:msgIpfixHeaderLength])
bs = newBs
}
if hdr.Length < msgIpfixHeaderLength {
// Message can't be shorter than its header
return nil, hdr, io.ErrUnexpectedEOF
}
bs = bs[:int(hdr.Length)]
_, err = io.ReadFull(r, bs[msgIpfixHeaderLength:])
if err != nil {
return nil, hdr, err
}
return bs, hdr, nil
}