-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipfix_file_format.go
176 lines (145 loc) · 3.93 KB
/
ipfix_file_format.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Copyright 2023 Alexander Bartolomey (github@alexanderbartolomey.de)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ipfix
import (
"context"
"encoding/binary"
"errors"
"io"
"sync"
)
type ipfixFileReader struct {
handle io.ReadCloser
messageCh chan []byte
errorCh chan error
closer *sync.Once
}
type RawMessage []byte
// ReadFull consumes an entire io.Reader of a file containing IPFIX File Format messages
// and returns those messages as byte slices sliced up in a wrapping slice.
// Different to the IPFIXFileReader implementation this is generally synchronous.
//
// Notably, if an error occurs during reading of messages, where err != io.EOF,
// the returned []RawMessage slice is nil, while the error propagates the returned error.
// This is different to the behaviour of the IPFIXFileReader implementation.
// Additionally, io.EOF errors are NOT propagated, i.e., ReadFull just returns the []RawMessage slice
// on occurence of an EOF.
//
// decoder := ipfix.NewDecoder(...)
// file, _ := os.Open("flow_records.ipfix")
// msgs, err := ipfix.ReadFull(file)
// if err != nil {
// log.Fatal(err)
// }
// for _, msg := range msgs {
// n, err := decoder.Decode(bytes.NewBuffer(msg))
// if err != nil {
// log.Fatal(err)
// }
// // Do anything with the decoded message afterwards
// }
func ReadFull(f io.Reader) ([]RawMessage, error) {
b := make([]RawMessage, 0)
for {
msg, err := readMessage(f)
if msg != nil {
b = append(b, msg)
}
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
}
return b, nil
}
// NewIPFIXFileReader creates a new reader from a file-like reader.
// It is intended to be used asynchronously using the message channel.
// from a parent context. Therefore, it needs to be started using Start(context.Context)
// inside a goroutine, because Start(context.Context) blocks until
func NewIPFIXFileReader(f io.ReadCloser) *ipfixFileReader {
r := &ipfixFileReader{
handle: f,
messageCh: make(chan []byte),
errorCh: make(chan error),
closer: &sync.Once{},
}
return r
}
func (r *ipfixFileReader) Start(ctx context.Context) error {
childCtx, cancel := context.WithCancel(ctx)
defer cancel()
defer r.Close()
go func() {
for {
msg, err := r.readMessage()
if msg != nil {
r.messageCh <- msg
}
if err != nil {
r.errorCh <- err
return
}
}
}()
<-childCtx.Done()
return nil
}
func (r *ipfixFileReader) Close() error {
var err error
r.closer.Do(func() {
defer close(r.errorCh)
defer close(r.messageCh)
err = r.handle.Close()
})
return err
}
func (r *ipfixFileReader) Messages() <-chan []byte {
return r.messageCh
}
func (r *ipfixFileReader) Errors() <-chan error {
return r.errorCh
}
func readMessage(r io.Reader) ([]byte, error) {
var version, length uint16
var read int
messageHeader := make([]byte, 4)
n, err := r.Read(messageHeader)
if err != nil || n != 4 {
return nil, err
}
read += n
version = binary.BigEndian.Uint16(messageHeader[0:2])
length = binary.BigEndian.Uint16(messageHeader[2:4])
if version != 10 {
return nil, errors.New("ipfixFileReader: unknown protocol version number")
}
rem := length - 4
payload := make([]byte, rem)
n, err = r.Read(payload)
read += n
p := make([]byte, 0)
p = append(p, messageHeader...)
p = append(p, payload[0:n]...)
if err != nil {
if errors.Is(err, io.EOF) {
return p, err
}
return nil, err
}
return p, nil
}
func (r *ipfixFileReader) readMessage() ([]byte, error) {
return readMessage(r.handle)
}