From 1c50bc502bdee9764a836fa1699a6af33538e35b Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Fri, 9 Feb 2024 09:15:47 -0500 Subject: [PATCH] refactor(main): move map_port to network (#2115) --- src/confighttp.cpp | 2 +- src/main.cpp | 32 ++++---------------------------- src/main.h | 2 -- src/network.cpp | 26 ++++++++++++++++++++++++++ src/network.h | 3 +++ src/nvhttp.cpp | 12 ++++++------ src/platform/linux/publish.cpp | 4 ++-- src/platform/macos/publish.cpp | 4 ++-- src/platform/windows/publish.cpp | 2 +- src/rtsp.cpp | 10 +++++----- src/stream.cpp | 6 +++--- src/upnp.cpp | 22 +++++++++++----------- 12 files changed, 64 insertions(+), 61 deletions(-) diff --git a/src/confighttp.cpp b/src/confighttp.cpp index 45062254..37233d6e 100644 --- a/src/confighttp.cpp +++ b/src/confighttp.cpp @@ -723,7 +723,7 @@ namespace confighttp { start() { auto shutdown_event = mail::man->event(mail::shutdown); - auto port_https = map_port(PORT_HTTPS); + auto port_https = net::map_port(PORT_HTTPS); auto address_family = net::af_from_enum_string(config::sunshine.address_family); https_server_t server { config::nvhttp.cert, config::nvhttp.pkey }; diff --git a/src/main.cpp b/src/main.cpp index e1a00731..e317e4d6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,6 +23,7 @@ #include "httpcommon.h" #include "logging.h" #include "main.h" +#include "network.h" #include "nvhttp.h" #include "platform/common.h" #include "process.h" @@ -297,7 +298,7 @@ namespace service_ctrl { return false; } - uint16_t port_nbo = htons(map_port(confighttp::PORT_HTTPS)); + uint16_t port_nbo = htons(net::map_port(confighttp::PORT_HTTPS)); for (DWORD i = 0; i < tcp_table->dwNumEntries; i++) { auto &entry = tcp_table->table[i]; @@ -348,7 +349,7 @@ is_gamestream_enabled() { */ void launch_ui() { - std::string url = "https://localhost:" + std::to_string(map_port(confighttp::PORT_HTTPS)); + std::string url = "https://localhost:" + std::to_string(net::map_port(confighttp::PORT_HTTPS)); platf::open_url(url); } @@ -362,7 +363,7 @@ launch_ui() { */ void launch_ui_with_path(std::string path) { - std::string url = "https://localhost:" + std::to_string(map_port(confighttp::PORT_HTTPS)) + path; + std::string url = "https://localhost:" + std::to_string(net::map_port(confighttp::PORT_HTTPS)) + path; platf::open_url(url); } @@ -811,28 +812,3 @@ write_file(const char *path, const std::string_view &contents) { return 0; } - -/** - * @brief Map a specified port based on the base port. - * @param port The port to map as a difference from the base port. - * @return `std:uint16_t` : The mapped port number. - * - * EXAMPLES: - * ```cpp - * std::uint16_t mapped_port = map_port(1); - * ``` - */ -std::uint16_t -map_port(int port) { - // calculate the port from the config port - auto mapped_port = (std::uint16_t)((int) config::sunshine.port + port); - - // Ensure port is in the range of 1024-65535 - if (mapped_port < 1024 || mapped_port > 65535) { - BOOST_LOG(warning) << "Port out of range: "sv << mapped_port; - } - - // TODO: Ensure port is not already in use by another application - - return mapped_port; -} diff --git a/src/main.h b/src/main.h index 0114586d..ba726539 100644 --- a/src/main.h +++ b/src/main.h @@ -30,8 +30,6 @@ std::string read_file(const char *path); int write_file(const char *path, const std::string_view &contents); -std::uint16_t -map_port(int port); void launch_ui(); void diff --git a/src/network.cpp b/src/network.cpp index 6980af2a..5c158ee6 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -4,6 +4,7 @@ */ #include "network.h" #include "config.h" +#include "logging.h" #include "utility.h" #include @@ -222,4 +223,29 @@ namespace net { enet_host_destroy(host); } + + /** + * @brief Map a specified port based on the base port. + * @param port The port to map as a difference from the base port. + * @return `std:uint16_t` : The mapped port number. + * + * EXAMPLES: + * ```cpp + * std::uint16_t mapped_port = net::map_port(1); + * ``` + */ + std::uint16_t + map_port(int port) { + // calculate the port from the config port + auto mapped_port = (std::uint16_t)((int) config::sunshine.port + port); + + // Ensure port is in the range of 1024-65535 + if (mapped_port < 1024 || mapped_port > 65535) { + BOOST_LOG(warning) << "Port out of range: "sv << mapped_port; + } + + // TODO: Ensure port is not already in use by another application + + return mapped_port; + } } // namespace net diff --git a/src/network.h b/src/network.h index 0363f62d..a4f74fb4 100644 --- a/src/network.h +++ b/src/network.h @@ -16,6 +16,9 @@ namespace net { void free_host(ENetHost *host); + std::uint16_t + map_port(int port); + using host_t = util::safe_ptr; using peer_t = ENetPeer *; using packet_t = util::safe_ptr; diff --git a/src/nvhttp.cpp b/src/nvhttp.cpp index 04cfd5b3..6fb7ab36 100644 --- a/src/nvhttp.cpp +++ b/src/nvhttp.cpp @@ -657,8 +657,8 @@ namespace nvhttp { tree.put("root.appversion", VERSION); tree.put("root.GfeVersion", GFE_VERSION); tree.put("root.uniqueid", http::unique_id); - tree.put("root.HttpsPort", map_port(PORT_HTTPS)); - tree.put("root.ExternalPort", map_port(PORT_HTTP)); + tree.put("root.HttpsPort", net::map_port(PORT_HTTPS)); + tree.put("root.ExternalPort", net::map_port(PORT_HTTP)); tree.put("root.mac", platf::get_mac_address(net::addr_to_normalized_string(local_endpoint.address()))); tree.put("root.MaxLumaPixelsHEVC", video::active_hevc_mode > 1 ? "1869449984" : "0"); @@ -846,7 +846,7 @@ namespace nvhttp { tree.put("root..status_code", 200); tree.put("root.sessionUrl0", launch_session->rtsp_url_scheme + net::addr_to_url_escaped_string(request->local_endpoint().address()) + ':' + - std::to_string(map_port(rtsp_stream::RTSP_SETUP_PORT))); + std::to_string(net::map_port(rtsp_stream::RTSP_SETUP_PORT))); tree.put("root.gamesession", 1); rtsp_stream::launch_session_raise(launch_session); @@ -932,7 +932,7 @@ namespace nvhttp { tree.put("root..status_code", 200); tree.put("root.sessionUrl0", launch_session->rtsp_url_scheme + net::addr_to_url_escaped_string(request->local_endpoint().address()) + ':' + - std::to_string(map_port(rtsp_stream::RTSP_SETUP_PORT))); + std::to_string(net::map_port(rtsp_stream::RTSP_SETUP_PORT))); tree.put("root.resume", 1); rtsp_stream::launch_session_raise(launch_session); @@ -995,8 +995,8 @@ namespace nvhttp { start() { auto shutdown_event = mail::man->event(mail::shutdown); - auto port_http = map_port(PORT_HTTP); - auto port_https = map_port(PORT_HTTPS); + auto port_http = net::map_port(PORT_HTTP); + auto port_https = net::map_port(PORT_HTTPS); auto address_family = net::af_from_enum_string(config::sunshine.address_family); bool clean_slate = config::sunshine.flags[config::flag::FRESH_STATE]; diff --git a/src/platform/linux/publish.cpp b/src/platform/linux/publish.cpp index f78b7d9d..bc876e77 100644 --- a/src/platform/linux/publish.cpp +++ b/src/platform/linux/publish.cpp @@ -8,7 +8,7 @@ #include "misc.h" #include "src/logging.h" -#include "src/main.h" +#include "src/network.h" #include "src/nvhttp.h" #include "src/platform/common.h" #include "src/utility.h" @@ -349,7 +349,7 @@ namespace platf::publish { name.get(), SERVICE_TYPE, nullptr, nullptr, - map_port(nvhttp::PORT_HTTP), + net::map_port(nvhttp::PORT_HTTP), nullptr); if (ret < 0) { diff --git a/src/platform/macos/publish.cpp b/src/platform/macos/publish.cpp index 51dd0cd2..eb8823e5 100644 --- a/src/platform/macos/publish.cpp +++ b/src/platform/macos/publish.cpp @@ -8,7 +8,7 @@ #include "misc.h" #include "src/logging.h" -#include "src/main.h" +#include "src/network.h" #include "src/nvhttp.h" #include "src/platform/common.h" #include "src/utility.h" @@ -349,7 +349,7 @@ namespace platf::publish { name.get(), SERVICE_TYPE, nullptr, nullptr, - map_port(nvhttp::PORT_HTTP), + net::map_port(nvhttp::PORT_HTTP), nullptr); if (ret < 0) { diff --git a/src/platform/windows/publish.cpp b/src/platform/windows/publish.cpp index 4ba17ab9..47c16721 100644 --- a/src/platform/windows/publish.cpp +++ b/src/platform/windows/publish.cpp @@ -117,7 +117,7 @@ namespace platf::publish { DNS_SERVICE_INSTANCE instance {}; instance.pszInstanceName = name.data(); - instance.wPort = map_port(nvhttp::PORT_HTTP); + instance.wPort = net::map_port(nvhttp::PORT_HTTP); instance.pszHostName = host.data(); // Setting these values ensures Windows mDNS answers comply with RFC 1035. diff --git a/src/rtsp.cpp b/src/rtsp.cpp index 6276753a..73f224b8 100644 --- a/src/rtsp.cpp +++ b/src/rtsp.cpp @@ -869,13 +869,13 @@ namespace rtsp_stream { std::uint16_t port; if (type == "audio"sv) { - port = map_port(stream::AUDIO_STREAM_PORT); + port = net::map_port(stream::AUDIO_STREAM_PORT); } else if (type == "video"sv) { - port = map_port(stream::VIDEO_STREAM_PORT); + port = net::map_port(stream::VIDEO_STREAM_PORT); } else if (type == "control"sv) { - port = map_port(stream::CONTROL_PORT); + port = net::map_port(stream::CONTROL_PORT); } else { cmd_not_found(sock, session, std::move(req)); @@ -1129,8 +1129,8 @@ namespace rtsp_stream { server.map("PLAY"sv, &cmd_play); boost::system::error_code ec; - if (server.bind(net::af_from_enum_string(config::sunshine.address_family), map_port(rtsp_stream::RTSP_SETUP_PORT), ec)) { - BOOST_LOG(fatal) << "Couldn't bind RTSP server to port ["sv << map_port(rtsp_stream::RTSP_SETUP_PORT) << "], " << ec.message(); + if (server.bind(net::af_from_enum_string(config::sunshine.address_family), net::map_port(rtsp_stream::RTSP_SETUP_PORT), ec)) { + BOOST_LOG(fatal) << "Couldn't bind RTSP server to port ["sv << net::map_port(rtsp_stream::RTSP_SETUP_PORT) << "], " << ec.message(); shutdown_event->raise(true); return; diff --git a/src/stream.cpp b/src/stream.cpp index 8db8374b..12fb8663 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -1600,9 +1600,9 @@ namespace stream { start_broadcast(broadcast_ctx_t &ctx) { auto address_family = net::af_from_enum_string(config::sunshine.address_family); auto protocol = address_family == net::IPV4 ? udp::v4() : udp::v6(); - auto control_port = map_port(CONTROL_PORT); - auto video_port = map_port(VIDEO_STREAM_PORT); - auto audio_port = map_port(AUDIO_STREAM_PORT); + auto control_port = net::map_port(CONTROL_PORT); + auto video_port = net::map_port(VIDEO_STREAM_PORT); + auto audio_port = net::map_port(AUDIO_STREAM_PORT); if (ctx.control_server.bind(address_family, control_port)) { BOOST_LOG(error) << "Couldn't bind Control server to port ["sv << control_port << "], likely another process already bound to the port"sv; diff --git a/src/upnp.cpp b/src/upnp.cpp index 91a5ad85..55c49aaf 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -62,13 +62,13 @@ namespace upnp { class deinit_t: public platf::deinit_t { public: deinit_t() { - auto rtsp = std::to_string(::map_port(rtsp_stream::RTSP_SETUP_PORT)); - auto video = std::to_string(::map_port(stream::VIDEO_STREAM_PORT)); - auto audio = std::to_string(::map_port(stream::AUDIO_STREAM_PORT)); - auto control = std::to_string(::map_port(stream::CONTROL_PORT)); - auto gs_http = std::to_string(::map_port(nvhttp::PORT_HTTP)); - auto gs_https = std::to_string(::map_port(nvhttp::PORT_HTTPS)); - auto wm_http = std::to_string(::map_port(confighttp::PORT_HTTPS)); + auto rtsp = std::to_string(net::map_port(rtsp_stream::RTSP_SETUP_PORT)); + auto video = std::to_string(net::map_port(stream::VIDEO_STREAM_PORT)); + auto audio = std::to_string(net::map_port(stream::AUDIO_STREAM_PORT)); + auto control = std::to_string(net::map_port(stream::CONTROL_PORT)); + auto gs_http = std::to_string(net::map_port(nvhttp::PORT_HTTP)); + auto gs_https = std::to_string(net::map_port(nvhttp::PORT_HTTPS)); + auto wm_http = std::to_string(net::map_port(confighttp::PORT_HTTPS)); mappings.assign({ { { rtsp, rtsp, "TCP"s }, "Sunshine - RTSP"s }, @@ -179,7 +179,7 @@ namespace upnp { * @return `true` on success. */ bool - map_port(const IGDdatas &data, const urls_t &urls, const std::string &lan_addr, const mapping_t &mapping) { + map_upnp_port(const IGDdatas &data, const urls_t &urls, const std::string &lan_addr, const mapping_t &mapping) { char intClient[16]; char intPort[6]; char desc[80]; @@ -284,7 +284,7 @@ namespace upnp { * @param data urls_t from UPNP_GetValidIGD() */ void - unmap_all_ports(const urls_t &urls, const IGDdatas &data) { + unmap_all_upnp_ports(const urls_t &urls, const IGDdatas &data) { for (auto it = std::begin(mappings); it != std::end(mappings); ++it) { auto status = UPNP_DeletePortMapping( urls->controlURL, @@ -343,7 +343,7 @@ namespace upnp { BOOST_LOG(debug) << "Found valid IGD device: "sv << urls->rootdescURL; for (auto it = std::begin(mappings); it != std::end(mappings) && !shutdown_event->peek(); ++it) { - map_port(data, urls, lan_addr_str, *it); + map_upnp_port(data, urls, lan_addr_str, *it); } if (!mapped) { @@ -365,7 +365,7 @@ namespace upnp { if (mapped) { // Unmap ports upon termination BOOST_LOG(info) << "Unmapping UPNP ports..."sv; - unmap_all_ports(mapped_urls, data); + unmap_all_upnp_ports(mapped_urls, data); } }