refactor(main): move map_port to network (#2115)

This commit is contained in:
ReenigneArcher 2024-02-09 09:15:47 -05:00 committed by GitHub
parent d91e2c9ecb
commit 1c50bc502b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 64 additions and 61 deletions

View File

@ -723,7 +723,7 @@ namespace confighttp {
start() {
auto shutdown_event = mail::man->event<bool>(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 };

View File

@ -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;
}

View File

@ -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

View File

@ -4,6 +4,7 @@
*/
#include "network.h"
#include "config.h"
#include "logging.h"
#include "utility.h"
#include <algorithm>
@ -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

View File

@ -16,6 +16,9 @@ namespace net {
void
free_host(ENetHost *host);
std::uint16_t
map_port(int port);
using host_t = util::safe_ptr<ENetHost, free_host>;
using peer_t = ENetPeer *;
using packet_t = util::safe_ptr<ENetPacket, enet_packet_destroy>;

View File

@ -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.<xmlattr>.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.<xmlattr>.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<bool>(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];

View File

@ -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) {

View File

@ -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) {

View File

@ -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.

View File

@ -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;

View File

@ -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;

View File

@ -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);
}
}