Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
network.cpp File Reference

Definitions for networking related functions. More...

#include <algorithm>
#include <boost/asio.hpp>
#include <cerrno>
#include <cstring>
#include <sstream>
#include <fcntl.h>
#include <ifaddrs.h>
#include <net/if.h>
#include "config.h"
#include "logging.h"
#include "network.h"
#include "utility.h"
Include dependency graph for network.cpp:

Go to the source code of this file.

Functions

net_e net::from_enum_string (const std::string_view &view)
raw_address_t net::to_raw_address (const boost::asio::ip::address &address)
 The raw bytes of an address, IPv4-mapped IPv6 as IPv4, the IPv6 scope kept.
net_e net::from_address_ranges (const std::string_view &view)
 Classify an address by the fixed ranges alone.
net_e net::from_address (const std::string_view &view, std::span< const interface_address_t > links)
 Classify an address against the given interface subnets.
net_e net::from_address (const std::string_view &view)
 Classify the network an address belongs to.
std::vector< interface_address_t > net::interface_addresses ()
 The addresses of every interface that is up, loopback aside, with their prefix lengths.
std::string_view net::to_enum_string (net_e net)
af_e net::af_from_enum_string (const std::string_view &view)
 Get the address family enum value from a string.
std::string_view net::af_to_any_address_string (af_e af)
 Get the wildcard binding address for a given address family.
boost::asio::ip::address net::normalize_address (boost::asio::ip::address address)
 Convert an address to a normalized form.
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.
int net::encryption_mode_for_address (boost::asio::ip::address address)
 Get the encryption mode for the given remote endpoint address.
host_t net::host_create (af_e af, ENetAddress &addr, std::uint16_t port, boost::system::error_code *error_out)
bool net::prevent_socket_inheritance (boost::asio::ip::udp::socket::native_handle_type socket, boost::system::error_code &error)
bool net::udp_port_available (af_e af, std::uint16_t port)
 Whether a UDP port can still be bound on this host.
void net::free_host (ENetHost *host)
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.

Variables

std::vector< ip::network_v4 > net::pc_ips_v4
std::vector< ip::network_v4 > net::lan_ips_v4
std::vector< ip::network_v6 > net::pc_ips_v6
std::vector< ip::network_v6 > net::lan_ips_v6

Detailed Description

Definitions for networking related functions.

Definition in file network.cpp.

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.

Definition at line 202 of file network.cpp.

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

Definition at line 206 of file network.cpp.

◆ af_from_enum_string()

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

Get the address family enum value from a string.

Parameters
viewThe config option value.
Returns
The address family enum value.

Definition at line 166 of file network.cpp.

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

Definition at line 178 of file network.cpp.

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

Definition at line 217 of file network.cpp.

◆ free_host()

void net::free_host ( ENetHost * host)

Definition at line 338 of file network.cpp.

◆ from_address() [1/2]

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

Classify the network an address belongs to.

PC for loopback, LAN for the private, shared and link-local ranges and for any address on a subnet this host is attached to (an IPv6 global address of the home network, for one), WAN otherwise.

Parameters
viewThe address.
Returns
The network type.

Definition at line 120 of file network.cpp.

◆ from_address() [2/2]

net_e net::from_address ( const std::string_view & view,
std::span< const interface_address_t > links )

Classify an address against the given interface subnets.

from_address() with the subnets passed in instead of read from the host's interfaces.

Parameters
viewThe address.
linksThe host's interface addresses and their prefixes.
Returns
The network type.

Definition at line 112 of file network.cpp.

◆ from_address_ranges()

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

Classify an address by the fixed ranges alone.

PC or LAN only for loopback and the private, shared and link-local ranges, whatever this host is attached to. For questions about an address itself, such as whether a router's external address is private.

Parameters
viewThe address.
Returns
The network type.

Definition at line 108 of file network.cpp.

◆ from_enum_string()

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

Definition at line 46 of file network.cpp.

◆ host_create()

host_t net::host_create ( af_e af,
ENetAddress & addr,
std::uint16_t port,
boost::system::error_code * error_out )

Definition at line 226 of file network.cpp.

◆ interface_addresses()

std::vector< interface_address_t > net::interface_addresses ( )

The addresses of every interface that is up, loopback aside, with their prefix lengths.

Returns
The addresses; empty when they cannot be listed, so nothing counts as on-link.

Definition at line 132 of file network.cpp.

◆ 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:350
Todo
Ensure port is not already in use by another application.

Definition at line 350 of file network.cpp.

◆ 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 "Hermes" if hostname is invalid.

Definition at line 367 of file network.cpp.

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

Definition at line 190 of file network.cpp.

◆ prevent_socket_inheritance()

bool net::prevent_socket_inheritance ( boost::asio::ip::udp::socket::native_handle_type socket,
boost::system::error_code & error )

Definition at line 306 of file network.cpp.

◆ to_enum_string()

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

Definition at line 152 of file network.cpp.

◆ to_raw_address()

raw_address_t net::to_raw_address ( const boost::asio::ip::address & address)

The raw bytes of an address, IPv4-mapped IPv6 as IPv4, the IPv6 scope kept.

Parameters
addressThe address.
Returns
The raw address.

Definition at line 92 of file network.cpp.

◆ udp_port_available()

bool net::udp_port_available ( af_e af,
std::uint16_t port )

Whether a UDP port can still be bound on this host.

The streaming ports are only bound when a session starts, so a port another program has taken is invisible until someone tries to play - at which point the stream fails for a reason that is nowhere near where it is felt. This answers the question ahead of time, by doing exactly what the stream will do: bind the wildcard address on the same address family.

Returns
false when the bind fails, which is what "in use" looks like from here; a port Hermes itself is already streaming on answers false too, so callers should ask only when nothing is streaming.

Definition at line 316 of file network.cpp.

Variable Documentation

◆ lan_ips_v4

std::vector<ip::network_v4> 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),
}

Definition at line 30 of file network.cpp.

◆ lan_ips_v6

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

Definition at line 41 of file network.cpp.

◆ pc_ips_v4

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

Definition at line 27 of file network.cpp.

◆ pc_ips_v6

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

Definition at line 38 of file network.cpp.