-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TCP - partially implement DecoderFactory
- Loading branch information
Showing
5 changed files
with
98 additions
and
2 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
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,90 @@ | ||
/** | ||
* \file | ||
* \author Jakub Antonín Štigler <[email protected]> | ||
* \brief Factory for creating decoders (source file) | ||
* \date 2024 | ||
* | ||
* Copyright: (C) 2023 CESNET, z.s.p.o. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#include "DecoderFactory.hpp" | ||
|
||
#include <memory> // unique_ptr | ||
#include <array> // array | ||
#include <cstdint> // uint8_t, uint16_t, uint32_t | ||
#include <stdexcept> // runtime_error | ||
#include <cerrno> // errno | ||
#include <string> // string | ||
|
||
#include <sys/socket.h> // recv, MSG_PEEK, MSG_WAITALL | ||
#include <netinet/in.h> // ntohl, ntohs | ||
|
||
#include <ipfixcol2.h> // ipx_strerror | ||
|
||
#include "Decoder.hpp" // Decoder | ||
|
||
namespace tcp_in { | ||
|
||
using namespace std; | ||
|
||
constexpr uint16_t IPFIX_MAGIC = 10; | ||
constexpr uint32_t LZ4_MAGIC = 0; | ||
|
||
DecoderFactory::DecoderFactory() {}; | ||
|
||
unique_ptr<Decoder> DecoderFactory::detect_decoder(int fd) { | ||
// number of bytes neaded to detect the decoder | ||
constexpr size_t MAX_MAGIC_LEN = 4; | ||
|
||
array<uint8_t, MAX_MAGIC_LEN> buf{}; | ||
|
||
auto res = recv(fd, buf.begin(), buf.size(), MSG_PEEK | MSG_WAITALL); | ||
if (res == -1) { | ||
const char *err_msg; | ||
ipx_strerror(errno, err_msg); | ||
throw runtime_error("Failed to receive start of first message: " + string(err_msg)); | ||
} | ||
|
||
constexpr const char *not_enough_data_err = | ||
"Failed to read enough bytes to recognize the decoder"; | ||
|
||
// check decoders in order from shortest magic number to longest | ||
|
||
if (res < 2) { | ||
throw runtime_error(not_enough_data_err); | ||
} | ||
|
||
// IPFIX decoder | ||
auto magic_u16 = ntohs(*reinterpret_cast<uint16_t *>(buf.begin())); | ||
if (magic_u16 == IPFIX_MAGIC) { | ||
return create_ipfix_decoder(fd); | ||
} | ||
|
||
if (res < 4) { | ||
throw runtime_error(not_enough_data_err); | ||
} | ||
|
||
// LZ4 decoder | ||
auto magic_u32 = ntohl(*reinterpret_cast<uint16_t *>(buf.begin())); | ||
if (magic_u32 == LZ4_MAGIC) { | ||
return create_lz4_decoder(fd); | ||
} | ||
|
||
throw runtime_error("Failed to recognize the decoder."); | ||
} | ||
|
||
unique_ptr<Decoder> DecoderFactory::create_ipfix_decoder(int fd) { | ||
(void)fd; | ||
// TODO | ||
throw runtime_error("Not implemented"); | ||
} | ||
|
||
unique_ptr<Decoder> DecoderFactory::create_lz4_decoder(int fd) { | ||
(void)fd; | ||
// TODO | ||
throw runtime_error("Not implemented"); | ||
} | ||
|
||
} // 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