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, 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
3646std::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
0 commit comments