File tree Expand file tree Collapse file tree 2 files changed +60
-1
lines changed
src/plugins/input/tcp/src Expand file tree Collapse file tree 2 files changed +60
-1
lines changed Original file line number Diff line number Diff line change 1010
1111#include " IpfixDecoder.hpp"
1212
13+ #include < stdexcept>
14+
1315#include " DecodeBuffer.hpp" // DecodeBuffer
1416#include " Reader.hpp"
1517
1618namespace tcp_in {
1719
1820void IpfixDecoder::progress () {
19- m_decoded.read_from (m_reader);
21+ // Directly calling `m_decoded.read_from` is somehow much slower than copying the code directly
22+ // here.
23+
24+ while (!m_decoded.enough_data ()) {
25+ if (!read_header ()) {
26+ break ;
27+ }
28+
29+ if (!read_body ()) {
30+ break ;
31+ }
32+ }
33+ }
34+
35+ bool IpfixDecoder::read_header () {
36+ if (m_decoded_size != 0 ) {
37+ return true ;
38+ }
39+
40+ if (!read_until_n (sizeof (fds_ipfix_msg_hdr))) {
41+ return false ;
42+ }
43+
44+
45+ // The header has been successfully read. Load the size of the message.
46+ auto hdr = reinterpret_cast <const fds_ipfix_msg_hdr *>(m_part_decoded.data ());
47+ m_decoded_size = ntohs (hdr->length );
48+
49+ if (m_decoded_size < sizeof (fds_ipfix_msg_hdr)) {
50+ throw std::runtime_error (" Invalid IPFIX message header size." );
51+ }
52+
53+ return true ;
54+ }
55+
56+ bool IpfixDecoder::read_body () {
57+ // Read the body
58+ if (!read_until_n (m_decoded_size)) {
59+ // There is not enough data to read the whole body.
60+ return false ;
61+ }
62+
63+ m_decoded.add (std::move (m_part_decoded));
64+ m_part_decoded = ByteVector ();
65+ m_decoded_size = 0 ;
66+ return true ;
67+ }
68+
69+ bool IpfixDecoder::read_until_n (size_t n) {
70+ m_reader.read_until_n (n, m_part_decoded, m_decoded);
71+ return m_part_decoded.size () == n;
2072}
2173
2274} // namespace tcp_in
Original file line number Diff line number Diff line change @@ -40,6 +40,13 @@ class IpfixDecoder : public Decoder {
4040 }
4141
4242private:
43+ bool read_header ();
44+ bool read_body ();
45+ bool read_until_n (size_t n);
46+
47+ ByteVector m_part_decoded;
48+ size_t m_decoded_size = 0 ;
49+
4350 TcpReader m_reader;
4451 DecodeBuffer m_decoded;
4552};
You can’t perform that action at this time.
0 commit comments