-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* \file | ||
* \author Jakub Antonín Štigler <[email protected]> | ||
* \brief IPFIX decoder for tcp plugin (header file) | ||
* \date 2024 | ||
* | ||
* Copyright: (C) 2023 CESNET, z.s.p.o. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#include "IpfixDecoder.hpp" | ||
|
||
#include <stdexcept> // runtime_error | ||
#include <string> // string | ||
#include <errno.h> // ERRNO | ||
|
||
#include <unistd.h> // read | ||
|
||
#include <ipfixcol2.h> // fds_ipfix_msg_header, ipx_strerror | ||
|
||
#include "DecodeBuffer.hpp" // DecodeBuffer | ||
|
||
namespace tcp_in { | ||
|
||
using namespace std; | ||
|
||
DecodeBuffer &IpfixDecoder::decode() { | ||
while (!m_decoded.enough_data()) { | ||
if (!read_header()) { | ||
break; | ||
} | ||
|
||
if (!read_body()) { | ||
break; | ||
} | ||
} | ||
|
||
return m_decoded; | ||
} | ||
|
||
bool IpfixDecoder::read_header() { | ||
if (m_msg_size != 0) { | ||
return true; | ||
} | ||
|
||
if (!read_to_size(sizeof(fds_ipfix_msg_hdr))) { | ||
return false; | ||
} | ||
|
||
auto hdr = reinterpret_cast<fds_ipfix_msg_hdr *>(m_part_readed.get()); | ||
m_msg_size = hdr->length; | ||
return true; | ||
} | ||
|
||
bool IpfixDecoder::read_body() { | ||
if (!read_to_size(m_msg_size)) { | ||
return false; | ||
} | ||
|
||
m_decoded.add(m_part_readed); | ||
m_msg_size = 0; | ||
return true; | ||
} | ||
|
||
bool IpfixDecoder::read_to_size(size_t size) { | ||
auto filled = m_part_readed.size(); | ||
auto remaining = size - filled; | ||
m_part_readed.resize(size); | ||
|
||
int res = read(m_fd, m_part_readed.get() + filled, remaining); | ||
if (res == -1) { | ||
m_part_readed.resize(filled); | ||
int err = errno; | ||
if (err == EWOULDBLOCK || err == EAGAIN) { | ||
return false; | ||
} | ||
|
||
const char *err_str; | ||
ipx_strerror(err, err_str); | ||
throw runtime_error( | ||
"IPFIX Decoder: Failed to read from descriptor: " | ||
+ string(err_str) | ||
); | ||
} | ||
|
||
if (res != remaining) { | ||
m_part_readed.resize(filled + res); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} // namespace tcp_in | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
/** | ||
* \file | ||
* \author Jakub Antonín Štigler <[email protected]> | ||
* \brief Tcp input plugin for ipfixcol2 (source file) | ||
* \date 2024 | ||
* | ||
* Copyright: (C) 2023 CESNET, z.s.p.o. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#include "Plugin.hpp" | ||
|
||
#include <array> // array | ||
|