forked from tehmaze/netflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.go
66 lines (52 loc) · 1.42 KB
/
decoder.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package netflow
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"github.com/tehmaze/netflow/ipfix"
"github.com/tehmaze/netflow/netflow1"
"github.com/tehmaze/netflow/netflow5"
"github.com/tehmaze/netflow/netflow6"
"github.com/tehmaze/netflow/netflow7"
"github.com/tehmaze/netflow/netflow9"
"github.com/tehmaze/netflow/session"
)
// Decoder for NetFlow messages.
type Decoder struct {
session.Session
}
// Message generlized interface.
type Message interface {
}
// NewDecoder sets up a decoder suitable for reading NetFlow packets.
func NewDecoder(s session.Session) *Decoder {
return &Decoder{s}
}
// Read a single Netflow message from the network. If an error is returned,
// there is no guarantee the following reads will be succesful.
func (d *Decoder) Read(r io.Reader) (Message, error) {
data := [2]byte{}
if _, err := r.Read(data[:]); err != nil {
return nil, err
}
version := binary.BigEndian.Uint16(data[:])
buffer := bytes.NewBuffer(data[:])
mr := io.MultiReader(buffer, r)
switch version {
case netflow1.Version:
return netflow1.Read(mr)
case netflow5.Version:
return netflow5.Read(mr)
case netflow6.Version:
return netflow6.Read(mr)
case netflow7.Version:
return netflow7.Read(mr)
case netflow9.Version:
return netflow9.Read(mr, d.Session, nil)
case ipfix.Version:
return ipfix.Read(mr, d.Session, nil)
default:
return nil, fmt.Errorf("netflow: unsupported version %d", version)
}
}