-
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.
- Loading branch information
Showing
11 changed files
with
374 additions
and
128 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* \file | ||
* \author Jakub Antonín Štigler <[email protected]> | ||
* \brief Acceptor thread for TCP clients (header file) | ||
* \date 2024 | ||
* | ||
* Copyright: (C) 2023 CESNET, z.s.p.o. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <thread> // std::thread | ||
#include <vector> // std::vector | ||
#include <ipfixcol2.h> // ipx_ctx_t | ||
|
||
#include "ClientManager.hpp" // ClientManager | ||
#include "DecoderFactory.hpp" // DecoderFactory | ||
#include "Config.hpp" | ||
|
||
namespace tcp_in | ||
{ | ||
|
||
/** | ||
* @brief Acceptor thread for TCP clients. | ||
*/ | ||
class Acceptor { | ||
public: | ||
/** | ||
* @brief Creates the acceptor thread. | ||
* | ||
* @param clients Reference to client manager. | ||
* @param factory Initialized decoder factory. | ||
* @param config File configuration. | ||
* @param ctx The plugin context. | ||
*/ | ||
Acceptor(ClientManager &clients, DecoderFactory factory, Config config, ipx_ctx_t *ctx); | ||
|
||
/** | ||
* @brief Starts the acceptor thread. | ||
*/ | ||
void start(); | ||
|
||
/** | ||
* @brief Stops the acceptor thread. | ||
*/ | ||
void stop(); | ||
|
||
private: | ||
/** | ||
* @brief The function that runs on the thread. | ||
*/ | ||
void mainloop(); | ||
|
||
/** | ||
* @brief File descriptor of epoll for accepting connections. | ||
*/ | ||
int m_epoll_fd; | ||
/** | ||
* @brief Sockets listened to by epoll. | ||
*/ | ||
std::vector<int> m_sockets; | ||
|
||
/** | ||
* @brief Write to this to gracefully exit the thread. | ||
*/ | ||
int m_pipe_in_fd; | ||
/** | ||
* @brief Epoll listens to this, when it activates the acceptor thread will gracefuly exit. | ||
*/ | ||
int m_pipe_out_fd; | ||
|
||
/** | ||
* @brief Accepted clients. | ||
*/ | ||
ClientManager &m_clients; | ||
DecoderFactory m_factory; | ||
std::thread m_thread; | ||
ipx_ctx_t *ctx; | ||
}; | ||
|
||
} // 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,7 +1,7 @@ | ||
/** | ||
* \file | ||
* \author Jakub Antonín Štigler <[email protected]> | ||
* \brief Mutexed vector of tcp connections (header file) | ||
* \brief Manages TCP connection (header file) | ||
* \date 2024 | ||
* | ||
* Copyright: (C) 2023 CESNET, z.s.p.o. | ||
|
@@ -21,10 +21,11 @@ | |
namespace tcp_in | ||
{ | ||
|
||
class ConnectionVec { | ||
/** | ||
* @brief Manager for TCP connections | ||
*/ | ||
class ClientManager { | ||
public: | ||
// TODO: methods for waiting for connection and getting the connections | ||
|
||
/** | ||
* @brief Adds connection to the vector and epoll. | ||
* @param fd file descriptor of the new tcp connection. | ||
|
@@ -36,7 +37,7 @@ class ConnectionVec { | |
* main thread (not the acceptor thread). | ||
* @param session session of the connection to remove. | ||
*/ | ||
void remove_connection(struct ipx_session *session); | ||
void close_connection(struct ipx_session *session); | ||
private: | ||
/** | ||
* @brief Locks the session for safe adding, removing is safe only for the main thread (not the | ||
|
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,105 @@ | ||
/** | ||
* \file | ||
* \author Jakub Antonín Štigler <[email protected]> | ||
* \brief Buffer for managing decoded IPFIX data (header file) | ||
* \date 2024 | ||
* | ||
* Copyright: (C) 2023 CESNET, z.s.p.o. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <vector> // std::vector | ||
#include <cstdint> // uint8_t | ||
|
||
#include "ByteVector.hpp" // ByteVector | ||
|
||
namespace tcp_in { | ||
|
||
/** | ||
* @brief Buffer for collecting and reconstructing decoded IPFIX messages. | ||
*/ | ||
class DecodeBuffer { | ||
public: | ||
/** | ||
* @brief Creates empty decode buffer. | ||
*/ | ||
DecodeBuffer() : m_close_requested(false), m_decoded(), m_part_decoded(), m_decoded_size(0) {}; | ||
|
||
/** | ||
* @brief Gets decoded data. Shouldn't be used by decoders. | ||
*/ | ||
inline std::vector<ByteVector> &get_decoded() { | ||
return m_decoded; | ||
} | ||
|
||
/** | ||
* @brief Adds new decoded IPFX message to the buffer. The passed buffer is emptied. | ||
* | ||
* @param data IPFIX message to add. This buffer will be emptied. | ||
*/ | ||
inline void add(ByteVector &data) { | ||
m_decoded.push_back(data.move()); | ||
} | ||
|
||
/** | ||
* @brief Copies IPFIX data from buffer. | ||
* | ||
* The data may be any part of message (possibly incomplete or even multiple messages) but | ||
* multiple calls to this metod must be with the message data in correct order so that it can be | ||
* reconstructed. | ||
* @param data data with the message | ||
* @param size size of the data in `data` | ||
*/ | ||
void read_from(const uint8_t *data, size_t size); | ||
|
||
/** | ||
* @brief Copies IPFIX data from circullar buffer. | ||
* | ||
* The data may be any part of message (possibly incomplete or even multiple messages) but | ||
* multiple calls to this metod must be with the message data in correct order so that it can be | ||
* reconstructed. | ||
* @param data data of the circullar buffer | ||
* @param buffer_size size of the circullar buffer (allocated space) | ||
* @param data_size size of data to copy from the buffer | ||
* @param position start position of the data in the buffer. | ||
*/ | ||
void read_from(const uint8_t *data, size_t buffer_size, size_t data_size, size_t position); | ||
|
||
/** | ||
* @brief Called by decoders to request closing the connection because there was invalid data. | ||
*/ | ||
inline void request_close() { | ||
m_close_requested = true; | ||
} | ||
|
||
/** | ||
* @brief Checks whether closing the connection was requested. | ||
* @return true if connection should be closed, otherwise false. | ||
*/ | ||
inline bool is_close_requested() const { | ||
return m_close_requested; | ||
} | ||
|
||
private: | ||
/** | ||
* @brief True if buffer/decoder encountered broken data. | ||
*/ | ||
bool m_close_requested; | ||
|
||
/** | ||
* @brief Decoded data waiting to be sent. | ||
*/ | ||
std::vector<ByteVector> m_decoded; | ||
/** | ||
* @brief Partially decoded data. | ||
*/ | ||
ByteVector m_part_decoded; | ||
/** | ||
* @brief Expected length of fully decoded data. | ||
*/ | ||
size_t m_decoded_size; | ||
}; | ||
|
||
} // 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* \file | ||
* \author Jakub Antonín Štigler <[email protected]> | ||
* \brief Factory for creating decoders (header file) | ||
* \date 2024 | ||
* | ||
* Copyright: (C) 2023 CESNET, z.s.p.o. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <memory> // std::unique_ptr | ||
|
||
#include "Decoder.hpp" // Decoder | ||
|
||
namespace tcp_in | ||
{ | ||
|
||
/** | ||
* @brief Factory for TCP decoders. | ||
*/ | ||
class DecoderFactory { | ||
public: | ||
DecoderFactory(); | ||
|
||
/** | ||
* @brief Detects the type of decoder that should be used to decode the given stream and | ||
* constructs it. This function may block if the decoder cannot be determined without | ||
* recieving more data. | ||
* @param fd TCP stream file descriptor | ||
* @return Instance of the correct decoder, nullptr no decoder matches the data. | ||
*/ | ||
std::unique_ptr<Decoder> detect_decoder(int fd); | ||
}; | ||
|
||
} // namespace tcp_in |
Oops, something went wrong.