Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
Loading...
Searching...
No Matches
network.cpp File Reference

Definitions for networking related functions. More...

#include <algorithm>
#include <sstream>
#include "config.h"
#include "logging.h"
#include "network.h"
#include "utility.h"
Include dependency graph for network.cpp:

Functions

std::string net::addr_to_normalized_string (boost::asio::ip::address address)
 Get the given address in normalized string form.
std::string net::addr_to_url_escaped_string (boost::asio::ip::address address)
 Get the given address in a normalized form for the host portion of a URL.
af_e net::af_from_enum_string (const std::string_view &view)
 Convert a config address-family string to the matching enum.
std::string_view net::af_to_any_address_string (af_e af)
 Get the wildcard binding address for a given address family.
int net::encryption_mode_for_address (boost::asio::ip::address address)
 Get the encryption mode for the given remote endpoint address.
void net::free_host (ENetHost *host)
 Destroy an ENet host allocated by host_create().
net_e net::from_address (const std::string_view &view)
 Classify an address using the configured loopback and LAN ranges without modifying them.
net_e net::from_enum_string (const std::string_view &view)
 Convert configuration text to a network enum value.
std::string net::get_bind_address (af_e af)
 Get the binding address to use based on config.
std::string net::get_bind_address_url_host ()
 Get the configured bind address formatted as a URL host.
af_e net::get_effective_address_family (af_e af)
 Get the effective address family for listening sockets.
host_t net::host_create (af_e af, ENetAddress &addr, std::uint16_t port)
 Create an ENet host with the requested address family.
std::uint16_t net::map_port (int port)
 Map a specified port based on the base port.
std::string net::mdns_instance_name (const std::string_view &hostname)
 Returns a string for use as the instance name for mDNS.
boost::asio::ip::address net::normalize_address (boost::asio::ip::address address)
 Convert an address to a normalized form.
std::string_view net::to_enum_string (net_e net)
 Convert a network enum value to configuration text.

Variables

std::vector net::lan_ips_v4
 IPv4 private and link-local networks used for LAN classification.
std::vector net::lan_ips_v6
 IPv6 private and link-local networks used for LAN classification.
std::vector net::pc_ips_v4
 IPv4 loopback networks used for local-client classification.
std::vector net::pc_ips_v6
 IPv6 loopback networks used for local-client classification.

Detailed Description

Definitions for networking related functions.

Function Documentation

◆ addr_to_normalized_string()

std::string net::addr_to_normalized_string ( boost::asio::ip::address address)

Get the given address in normalized string form.

Normalization converts IPv4-mapped IPv6 addresses into IPv4 addresses.

Parameters
addressThe address to normalize.
Returns
Normalized address in string form.

◆ addr_to_url_escaped_string()

std::string net::addr_to_url_escaped_string ( boost::asio::ip::address address)

Get the given address in a normalized form for the host portion of a URL.

Normalization converts IPv4-mapped IPv6 addresses into IPv4 addresses.

Parameters
addressThe address to normalize and escape.
Returns
Normalized address in URL-escaped string.

◆ af_from_enum_string()

af_e net::af_from_enum_string ( const std::string_view & view)

Convert a config address-family string to the matching enum.

Parameters
viewThe config option value.
Returns
Address-family enum represented by the string.

◆ af_to_any_address_string()

std::string_view net::af_to_any_address_string ( af_e af)

Get the wildcard binding address for a given address family.

Parameters
afAddress family.
Returns
Normalized address.

◆ encryption_mode_for_address()

int net::encryption_mode_for_address ( boost::asio::ip::address address)

Get the encryption mode for the given remote endpoint address.

Parameters
addressThe address used to look up the desired encryption mode.
Returns
The WAN or LAN encryption mode, based on the provided address.

◆ free_host()

void net::free_host ( ENetHost * host)

Destroy an ENet host allocated by host_create().

Parameters
hostHost name or address to resolve.

◆ from_address()

net_e net::from_address ( const std::string_view & view)

Classify an address using the configured loopback and LAN ranges without modifying them.

Convert a Boost address family to Sunshine network enum value.

Parameters
viewBoost.Log record view being formatted.
Returns
Value converted from address.

◆ from_enum_string()

net_e net::from_enum_string ( const std::string_view & view)

Convert configuration text to a network enum value.

Parameters
viewBoost.Log record view being formatted.
Returns
Value converted from enum string.

◆ get_bind_address()

std::string net::get_bind_address ( af_e af)

Get the binding address to use based on config.

Parameters
afAddress family.
Returns
The configured bind address or wildcard if not configured.

◆ get_bind_address_url_host()

std::string net::get_bind_address_url_host ( )

Get the configured bind address formatted as a URL host.

IPv6 addresses are enclosed in brackets. An empty bind address is represented as localhost.

Returns
URL host for links to services bound by Sunshine.

◆ get_effective_address_family()

af_e net::get_effective_address_family ( af_e af)

Get the effective address family for listening sockets.

An IPv4 bind address narrows dual-stack mode to IPv4 so the socket protocol matches the bind endpoint.

Parameters
afConfigured address family.
Returns
IPv4 for a valid IPv4 bind address in dual-stack mode, or af otherwise.

◆ host_create()

host_t net::host_create ( af_e af,
ENetAddress & addr,
std::uint16_t port )

Create an ENet host with the requested address family.

Parameters
afAddress family used for socket creation or binding.
addrNetwork address to bind, parse, or format.
portTCP or UDP port number.
Returns
ENet host bound to the requested address and port, or an empty handle on failure.

◆ map_port()

std::uint16_t net::map_port ( int port)

Map a specified port based on the base port.

Parameters
portThe port to map as a difference from the base port.
Returns
The mapped port number.

Examples

std::uint16_t mapped_port = net::map_port(1);
std::uint16_t map_port(int port)
Map a specified port based on the base port.
Definition network.cpp:243
Todo
Ensure port is not already in use by another application.

◆ mdns_instance_name()

std::string net::mdns_instance_name ( const std::string_view & hostname)

Returns a string for use as the instance name for mDNS.

Parameters
hostnameThe hostname to use for instance name generation.
Returns
Hostname-based instance name or "Sunshine" if hostname is invalid.

◆ normalize_address()

boost::asio::ip::address net::normalize_address ( boost::asio::ip::address address)

Convert an address to a normalized form.

Normalization converts IPv4-mapped IPv6 addresses into IPv4 addresses.

Parameters
addressThe address to normalize.
Returns
Normalized address.

◆ to_enum_string()

std::string_view net::to_enum_string ( net_e net)

Convert a network enum value to configuration text.

Parameters
netNetwork scope to convert or format.
Returns
Value converted to enum string.

Variable Documentation

◆ lan_ips_v4

std::vector net::lan_ips_v4
Initial value:
{
ip::make_network_v4("192.168.0.0/16"sv),
ip::make_network_v4("172.16.0.0/12"sv),
ip::make_network_v4("10.0.0.0/8"sv),
ip::make_network_v4("100.64.0.0/10"sv),
ip::make_network_v4("169.254.0.0/16"sv),
}

IPv4 private and link-local networks used for LAN classification.

◆ lan_ips_v6

std::vector net::lan_ips_v6
Initial value:
{
ip::make_network_v6("fc00::/7"sv),
ip::make_network_v6("fe80::/64"sv),
}

IPv6 private and link-local networks used for LAN classification.

◆ pc_ips_v4

std::vector net::pc_ips_v4
Initial value:
{
ip::make_network_v4("127.0.0.0/8"sv),
}

IPv4 loopback networks used for local-client classification.

◆ pc_ips_v6

std::vector net::pc_ips_v6
Initial value:
{
ip::make_network_v6("::1/128"sv),
}

IPv6 loopback networks used for local-client classification.