-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcp.go
311 lines (263 loc) · 8.1 KB
/
tcp.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
Copyright 2023 Alexander Bartolomey ([email protected])
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 (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
)
type TCPListener struct {
bindAddr string
packetCh chan []byte
addr *net.TCPAddr
listener *net.TCPListener
}
func NewTCPListener(bindAddr string) *TCPListener {
return &TCPListener{
bindAddr: bindAddr,
packetCh: make(chan []byte, tcpChannelBufferSize),
}
}
func (l *TCPListener) Listen(ctx context.Context) (err error) {
logger := FromContext(ctx)
l.addr, err = net.ResolveTCPAddr("tcp", l.bindAddr)
if err != nil {
return err
}
l.listener, err = net.ListenTCP("tcp", l.addr)
if err != nil {
return err
}
defer l.listener.Close()
// async tcp handler function
go func() {
for {
if l.listener == nil {
return
}
conn, rerr := l.listener.Accept()
TCPActiveConnections.Inc()
if err != nil {
if errors.Is(err, net.ErrClosed) {
return
}
ErrorsTotal.Inc()
logger.Error(err, "failed to accept TCP connection", "addr", l.addr)
err = rerr
return
}
// handle each accepted connection in a separate goroutine for S C A L E
// IPFIX associates an entire TCP connection with a session. It may transmit more than
// one packet, and it may be kept alive during the entire exporting process (at least
// that is what yaf does).
go func(conn net.Conn) {
if conn == nil {
return
}
// initiate close after being done reading
defer logger.V(3).Info("tcp: closed connection")
defer TCPActiveConnections.Dec()
defer conn.Close()
var rerr error
defer func() {
if rerr != nil {
ErrorsTotal.Inc()
}
}()
// instantiate a new session from the connection to receive packets from
session := newSessionFromConnection(conn)
logger.V(3).Info("starting new session from TCP connection", "source", conn.RemoteAddr().String())
errorCh := make(chan error)
// run this loop indefinitely in a goroutine to not block. The session resets internally
// and will be reused for subsequent packets.
go func() {
for {
err := session.receive(ctx)
if err != nil {
errorCh <- err
return
}
}
}()
for {
select {
case <-ctx.Done():
return
case err := <-errorCh:
if errors.Is(err, io.EOF) {
logger.V(1).Info("connection closed by remote", "remote_addr", conn.RemoteAddr().String())
} else {
logger.Error(err, "failed to read IPFIX packet", "remote_addr", conn.RemoteAddr().String())
}
return
case packet := <-session.messages():
// write packet to event source channel
TCPReceivedBytes.Add(float64(len(packet)))
logger.V(3).Info("wrote IPFIX packet to event source channel", "length", len(packet))
l.packetCh <- packet
}
}
}(conn)
}
}()
logger.Info("Started TCP listener", "addr", l.bindAddr)
<-ctx.Done()
logger.Info("Shutting down TCP listener", "addr", l.addr)
return
}
func (l *TCPListener) Messages() <-chan []byte {
return l.packetCh
}
var (
tcpChannelBufferSize int = 10
// ipfixMessageHeaderLength is the number of bytes in an IPFIX packet header
ipfixMessageHeaderLength uint16 = 16
)
type session struct {
offset uint16
length uint16
messageCh chan []byte
message bytes.Buffer
reader io.Reader
}
func newSessionFromConnection(conn net.Conn) *session {
return &session{
messageCh: make(chan []byte),
reader: conn,
}
}
func (s *session) messages() <-chan []byte {
return s.messageCh
}
// receive successively reads from the connection's reader to piece together a message
func (s *session) receive(ctx context.Context) error {
logger := FromContext(ctx)
// working on header bytes
if s.offset < ipfixMessageHeaderLength {
_, err := s.receiveHeader()
if err != nil {
return err
}
if s.offset < ipfixMessageHeaderLength {
// still too little bytes, will call this method again from the connection
return nil
}
}
_, err := s.receiveBody()
if err != nil {
return err
}
if s.offset < s.length {
// not done with body yet, next call to Receive will advance the reading
return nil
}
s.messageCh <- s.message.Bytes()
// since messageCh is unbuffered, when the above unblocks, the buffer has been consumed by the handler
// to be passed on to the EventSource. Afterwards, we can reset all internal fields for re-use
s.length = 0
s.offset = 0
s.message.Reset()
logger.V(3).Info("session: cleaning up session after received ipfix message for reuse")
return nil
}
func (s *session) receiveHeader() (int, error) {
var remains uint16 = ipfixMessageHeaderLength
var offset uint16
headerBuffer := &bytes.Buffer{}
// if this method was already called but not enough bytes for a header were read, this is true...
if s.offset > 0 {
// there's already stuff in the buffer, copy it into an ipfixHeader struct
remains = ipfixMessageHeaderLength - s.offset // update "remains" such that we don't exceed the header when reading from the socket
_, err := headerBuffer.ReadFrom(&s.message) // the buffer abstraction here makes it easy to do this multiple times
if err != nil {
return -1, fmt.Errorf("failed to read from message buffer into header buffer, %w", err)
}
}
// only read "remaining bytes upto a full header"
b := make([]byte, remains)
len, err := s.reader.Read(b)
if len == 0 {
if offset > 0 {
return len, fmt.Errorf("session closed unexpectedly: %w", err)
}
if errors.Is(err, io.EOF) {
return len, err
}
return len, fmt.Errorf("failed to read from socket: %w", err)
}
_, err = headerBuffer.Write(b)
if err != nil {
return len, fmt.Errorf("failed to write into header buffer: %w", err)
}
offset += uint16(len)
if offset < ipfixMessageHeaderLength {
// not a whole header yet...
// copy the internal header buffer which contains all read bytes for the header up to now
// into the message of the session
// reset the message buffer and write the currently read header bytes to it
s.message.Reset()
_, err = s.message.ReadFrom(headerBuffer)
if err != nil {
return -1, fmt.Errorf("failed to write from header buffer to message: %w", err)
}
// s.message = bytes.NewBuffer(headerBuffer.Bytes())
s.offset = offset
s.length = offset
return len, nil
}
// now we have the full header
b = headerBuffer.Bytes()
// bytes 0 and 1 are "version" but we don't need that here
msgLength := binary.BigEndian.Uint16(b[2:4])
if err != nil {
return len, fmt.Errorf("failed to read packet length from header buffer, %w", err)
}
// we've read the first 4 bytes of the headerBuffer, we need to reset the offset to be
// able to read from it in its entirety
// reset the entire message buffer and write the current header bytes to it
s.message.Reset()
_, err = s.message.Write(b)
if err != nil {
return len, fmt.Errorf("failed to write from header buffer to message, %w", err)
}
// s.message = bytes.NewBuffer(headerBuffer.Bytes())
s.offset = ipfixMessageHeaderLength
s.length = msgLength
return len, nil
}
func (s *session) receiveBody() (int, error) {
remains := s.length - s.offset
if remains == 0 {
// IPFIX message without a body
return 0, nil
}
b := make([]byte, remains)
len, err := s.reader.Read(b)
if err != nil {
if errors.Is(err, io.EOF) {
return len, err
} else {
return len, fmt.Errorf("session closed unexpectedly: %w", err)
}
}
_, err = s.message.Write(b[:len]) // only write the newly read portion of the buffer to the message buffer, otherwise this adds 0s
if err != nil {
return len, fmt.Errorf("failed to write read bytes to message buffer, %w", err)
}
s.offset += uint16(len)
return len, nil
}