Skip to content

Commit

Permalink
refactor(main): move map_port to network (LizardByte#2115)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher authored and KuleRucket committed Jun 6, 2024
1 parent 7555709 commit e2abd5d
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/confighttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
32 changes: 4 additions & 28 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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];

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

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

Expand Down Expand Up @@ -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;
}
2 changes: 0 additions & 2 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
#include "network.h"
#include "config.h"
#include "logging.h"
#include "utility.h"
#include <algorithm>

Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions src/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down
12 changes: 6 additions & 6 deletions src/nvhttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions src/platform/linux/publish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/platform/macos/publish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/platform/windows/publish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions src/rtsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 11 additions & 11 deletions src/upnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}

Expand Down

0 comments on commit e2abd5d

Please sign in to comment.