From 7ee3882ab16e30c4c4eb05b756409213fa640b51 Mon Sep 17 00:00:00 2001 From: Maxim Sharabayko Date: Thu, 5 Nov 2020 14:54:44 +0100 Subject: [PATCH] Throw exception on invalid URI query --- xtransmit/generate.cpp | 2 +- xtransmit/receive.cpp | 2 +- xtransmit/route.cpp | 2 +- xtransmit/srt_socket.cpp | 19 +++++++---- xtransmit/srt_socket.hpp | 70 +++++++++++++++++++++------------------- 5 files changed, 53 insertions(+), 42 deletions(-) diff --git a/xtransmit/generate.cpp b/xtransmit/generate.cpp index aa55538..13113a8 100644 --- a/xtransmit/generate.cpp +++ b/xtransmit/generate.cpp @@ -118,7 +118,7 @@ void xtransmit::generate::run(const string& dst_url, const config& cfg, const at } catch (const socket::exception& e) { - spdlog::warn(LOG_SC_GENERATE "{}", e.what()); + spdlog::error(LOG_SC_GENERATE "{}", e.what()); return; } } diff --git a/xtransmit/receive.cpp b/xtransmit/receive.cpp index 762738d..e4c47a1 100644 --- a/xtransmit/receive.cpp +++ b/xtransmit/receive.cpp @@ -176,7 +176,7 @@ void xtransmit::receive::run(const string &src_url, const config &cfg, const ato } catch (const socket::exception & e) { - cerr << e.what() << endl; + spdlog::error(LOG_SC_RECEIVE "{}", e.what()); } } diff --git a/xtransmit/route.cpp b/xtransmit/route.cpp index 387acc1..febb4ee 100644 --- a/xtransmit/route.cpp +++ b/xtransmit/route.cpp @@ -120,7 +120,7 @@ void xtransmit::route::run(const string& src_url, const string& dst_url, } catch (const socket::exception & e) { - cerr << e.what() << endl; + spdlog::error(LOG_SC_ROUTE "{}", e.what()); } } diff --git a/xtransmit/srt_socket.cpp b/xtransmit/srt_socket.cpp index 2b0bb46..d8d23e6 100644 --- a/xtransmit/srt_socket.cpp +++ b/xtransmit/srt_socket.cpp @@ -53,7 +53,7 @@ socket::srt::srt(const UriParser &src_uri) throw socket::exception(srt_getlasterror_str()); } - check_options_exist(); + assert_options_valid(); if (SRT_SUCCESS != configure_pre(m_bind_socket)) throw socket::exception(srt_getlasterror_str()); @@ -242,13 +242,13 @@ std::future socket::srt::async_read(std::vector &buffer) return std::future(); } -void socket::srt::check_options_exist() const +void socket::srt::assert_options_valid(const std::map& options) { #ifdef ENABLE_CXX17 - for (const auto& [key, val] : myMap) + for (const auto& [key, val] : options) { #else - for (const auto el : m_options) + for (const auto el : options) { const string& key = el.first; const string& val = el.second; @@ -266,11 +266,18 @@ void socket::srt::check_options_exist() const if (opt_found || key == "bind" || key == "mode") continue; - spdlog::warn(LOG_SOCK_SRT "srt://{}:{:d}: Ignoring socket option '{}={}' (not recognized)!", - m_host, m_port, key, val); + stringstream ss; + ss << "Invalid URI query option '"; + ss << key << "=" << val << " (not recognized)!"; + throw socket::exception(ss.str()); } } +void socket::srt::assert_options_valid() const +{ + assert_options_valid(m_options); +} + int socket::srt::configure_pre(SRTSOCKET sock) { diff --git a/xtransmit/srt_socket.hpp b/xtransmit/srt_socket.hpp index 1b8f2b1..e1bbcda 100644 --- a/xtransmit/srt_socket.hpp +++ b/xtransmit/srt_socket.hpp @@ -15,17 +15,17 @@ #include "udt.h" #include "uriparser.hpp" - -namespace xtransmit { -namespace socket { - +namespace xtransmit +{ +namespace socket +{ class srt : public std::enable_shared_from_this , public isocket { - using string = std::string; - using shared_srt = std::shared_ptr; + using string = std::string; + using shared_srt = std::shared_ptr; public: explicit srt(const UriParser& src_uri); @@ -34,33 +34,38 @@ class srt virtual ~srt(); - public: std::future async_connect() noexcept(false); - std::future async_accept() noexcept(false); - + std::future async_accept() noexcept(false); - shared_srt connect(); - shared_srt accept(); + shared_srt connect(); + shared_srt accept(); /** * Start listening on the incomming connection requests. * - * May throw a socket_exception. + * May throw a socket::exception. */ - void listen() noexcept(false); + void listen() noexcept(false); + + /// Verifies URI options provided are valid. + /// + /// @param [in] options a map of options key-value pairs to validate + /// @throw socket::exception on failure + /// + static void assert_options_valid(const std::map& options); private: - void configure(const std::map &options); + void configure(const std::map& options); - void check_options_exist() const; + void assert_options_valid() const; int configure_pre(SRTSOCKET sock); int configure_post(SRTSOCKET sock); void handle_hosts(); public: - std::future async_read(std::vector &buffer); - void async_write(); + std::future async_read(std::vector& buffer); + void async_write(); /** * @returns The number of bytes received. @@ -68,11 +73,11 @@ class srt * @throws socket_exception Thrown on failure. */ size_t read(const mutable_buffer& buffer, int timeout_ms = -1) final; - int write(const const_buffer& buffer, int timeout_ms = -1) final; + int write(const const_buffer& buffer, int timeout_ms = -1) final; enum connection_mode { - FAILURE =-1, + FAILURE = -1, LISTENER = 0, CALLER = 1, RENDEZVOUS = 2 @@ -83,28 +88,27 @@ class srt bool is_caller() const final { return m_mode == CALLER; } public: - int id() const final { return m_bind_socket; } - int statistics(SRT_TRACEBSTATS &stats, bool instant = true); - bool supports_statistics() const final { return true; } - const std::string statistics_csv(bool print_header) final; + int id() const final { return m_bind_socket; } + int statistics(SRT_TRACEBSTATS& stats, bool instant = true); + bool supports_statistics() const final { return true; } + const std::string statistics_csv(bool print_header) final; static const std::string stats_to_csv(int socketid, const SRT_TRACEBSTATS& stats, bool print_header); private: void raise_exception(const string&& place) const; - void raise_exception(const string &&place, const string&& reason) const; + void raise_exception(const string&& place, const string&& reason) const; private: - int m_bind_socket = SRT_INVALID_SOCK; - int m_epoll_connect = -1; - int m_epoll_io = -1; - - connection_mode m_mode = FAILURE; - bool m_blocking_mode = false; - string m_host; - int m_port = -1; + int m_bind_socket = SRT_INVALID_SOCK; + int m_epoll_connect = -1; + int m_epoll_io = -1; + + connection_mode m_mode = FAILURE; + bool m_blocking_mode = false; + string m_host; + int m_port = -1; std::map m_options; // All other options, as provided in the URI }; } // namespace socket } // namespace xtransmit -