Skip to content

Commit 69edb0e

Browse files
committed
TCP input TLS - Use TlsDecoder in DecoderFactory.
1 parent e380d8f commit 69edb0e

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/plugins/input/tcp/src/DecoderFactory.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,22 @@
2626
#include "Decoder.hpp" // Decoder
2727
#include "Lz4Decoder.hpp" // LZ4_MAGIC, Lz4Decoder
2828
#include "IpfixDecoder.hpp" // IPFIX_MAGIC, IpfixDecoder
29+
#include "tls/TlsDecoder.hpp"
2930

3031
#include <iostream>
3132

3233
namespace tcp_in {
3334

34-
DecoderFactory::DecoderFactory() {};
35+
DecoderFactory::DecoderFactory(ipx_ctx_t *ctx, const Config &conf) {
36+
if (!conf.certificate_file.empty()) {
37+
IPX_CTX_INFO(ctx, "Initializing TLS decoder.");
38+
m_tls_factory = std::unique_ptr<tls::DecoderFactory>(
39+
new tls::DecoderFactory(conf.certificate_file)
40+
);
41+
} else {
42+
IPX_CTX_INFO(ctx, "TLS Decoder is disabled.");
43+
}
44+
};
3545

3646
std::unique_ptr<Decoder> DecoderFactory::detect_decoder(int fd) {
3747
// number of bytes neaded to detect the decoder
@@ -48,14 +58,26 @@ std::unique_ptr<Decoder> DecoderFactory::detect_decoder(int fd) {
4858
if (res == -1) {
4959
const char *err_msg;
5060
ipx_strerror(errno, err_msg);
51-
throw std::runtime_error("Failed to receive start of first message: " + std::string(err_msg));
61+
throw std::runtime_error(
62+
"Failed to receive start of first message: " + std::string(err_msg)
63+
);
5264
}
5365

5466
constexpr const char *not_enough_data_err =
5567
"Failed to read enough bytes to recognize the decoder";
5668

5769
// check decoders in order from shortest magic number to longest
5870

71+
if (res < 1) {
72+
throw std::runtime_error(not_enough_data_err);
73+
}
74+
75+
// TLS decoder
76+
auto magic_u8 = buf[0];
77+
if (magic_u8 == tls::TLS_MAGIC) {
78+
return create_tls_decoder(fd);
79+
}
80+
5981
if (res < 2) {
6082
throw std::runtime_error(not_enough_data_err);
6183
}
@@ -87,5 +109,12 @@ std::unique_ptr<Decoder> DecoderFactory::create_lz4_decoder(int fd) {
87109
return std::unique_ptr<Decoder>(new Lz4Decoder(fd));
88110
}
89111

112+
std::unique_ptr<Decoder> DecoderFactory::create_tls_decoder(int fd) {
113+
if (!m_tls_factory) {
114+
throw std::runtime_error("TLS decoder is not enabled.");
115+
}
116+
return m_tls_factory->create(fd);
117+
}
118+
90119
} // namespace tcp_in
91120

src/plugins/input/tcp/src/DecoderFactory.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212

1313
#include <memory> // std::unique_ptr
1414

15+
#include "Config.hpp"
1516
#include "Decoder.hpp" // Decoder
17+
#include "tls/DecoderFactory.hpp"
1618

1719
namespace tcp_in {
1820

1921
/** Factory for TCP decoders. */
2022
class DecoderFactory {
2123
public:
22-
DecoderFactory();
24+
DecoderFactory(ipx_ctx_t *ctx, const Config &conf);
2325

2426
/**
2527
* @brief Detects the type of decoder that should be used to decode the given stream and
@@ -31,9 +33,14 @@ class DecoderFactory {
3133
*/
3234
std::unique_ptr<Decoder> detect_decoder(int fd);
3335

36+
void initialize_tls(std::string hostname, const char *cert_path);
37+
3438
private:
3539
std::unique_ptr<Decoder> create_ipfix_decoder(int fd);
3640
std::unique_ptr<Decoder> create_lz4_decoder(int fd);
41+
std::unique_ptr<Decoder> create_tls_decoder(int fd);
42+
43+
std::unique_ptr<tls::DecoderFactory> m_tls_factory;
3744
};
3845

3946
} // namespace tcp_in

src/plugins/input/tcp/src/Plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace tcp_in {
2424

2525
Plugin::Plugin(ipx_ctx_t *ctx, Config &config) :
2626
m_ctx(ctx),
27-
m_clients(ctx, DecoderFactory()),
27+
m_clients(ctx, DecoderFactory(ctx, config)),
2828
m_acceptor(m_clients, ctx)
2929
{
3030
m_acceptor.bind_addresses(config);

0 commit comments

Comments
 (0)