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
3233namespace tcp_in {
3334
34- DecoderFactory::DecoderFactory () {};
35+ DecoderFactory::DecoderFactory (ipx_ctx_t *ctx) : m_ctx(ctx) {
36+ // TLS is initialized separately because it may prompt the user.
37+ };
3538
3639std::unique_ptr<Decoder> DecoderFactory::detect_decoder (int fd) {
3740 // number of bytes neaded to detect the decoder
@@ -48,14 +51,26 @@ std::unique_ptr<Decoder> DecoderFactory::detect_decoder(int fd) {
4851 if (res == -1 ) {
4952 const char *err_msg;
5053 ipx_strerror (errno, err_msg);
51- throw std::runtime_error (" Failed to receive start of first message: " + std::string (err_msg));
54+ throw std::runtime_error (
55+ " Failed to receive start of first message: " + std::string (err_msg)
56+ );
5257 }
5358
5459 constexpr const char *not_enough_data_err =
5560 " Failed to read enough bytes to recognize the decoder" ;
5661
5762 // check decoders in order from shortest magic number to longest
5863
64+ if (res < 1 ) {
65+ throw std::runtime_error (not_enough_data_err);
66+ }
67+
68+ // TLS decoder
69+ auto magic_u8 = buf[0 ];
70+ if (magic_u8 == tls::TLS_MAGIC) {
71+ return create_tls_decoder (fd);
72+ }
73+
5974 if (res < 2 ) {
6075 throw std::runtime_error (not_enough_data_err);
6176 }
@@ -79,6 +94,15 @@ std::unique_ptr<Decoder> DecoderFactory::detect_decoder(int fd) {
7994 throw std::runtime_error (" Failed to recognize the decoder." );
8095}
8196
97+ void DecoderFactory::initialize_tls (const Config &conf) {
98+ if (!conf.certificate_file .empty ()) {
99+ IPX_CTX_INFO (m_ctx, " Initializing TLS decoder." );
100+ m_tls_factory = std::unique_ptr<tls::DecoderFactory>(new tls::DecoderFactory (conf));
101+ } else {
102+ IPX_CTX_INFO (m_ctx, " TLS Decoder is disabled." );
103+ }
104+ }
105+
82106std::unique_ptr<Decoder> DecoderFactory::create_ipfix_decoder (int fd) {
83107 return std::unique_ptr<Decoder>(new IpfixDecoder (fd));
84108}
@@ -87,5 +111,12 @@ std::unique_ptr<Decoder> DecoderFactory::create_lz4_decoder(int fd) {
87111 return std::unique_ptr<Decoder>(new Lz4Decoder (fd));
88112}
89113
114+ std::unique_ptr<Decoder> DecoderFactory::create_tls_decoder (int fd) {
115+ if (!m_tls_factory) {
116+ throw std::runtime_error (" TLS decoder is not enabled." );
117+ }
118+ return m_tls_factory->create (fd);
119+ }
120+
90121} // namespace tcp_in
91122
0 commit comments