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

Declarations for networking related functions. More...

#include <span>
#include <tuple>
#include <utility>
#include <vector>
#include <boost/asio.hpp>
#include <enet/enet.h>
#include "network_link.h"
#include "utility.h"
Include dependency graph for network.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

using net::host_t = util::safe_ptr<ENetHost, free_host>
using net::peer_t = ENetPeer *
using net::packet_t = util::safe_ptr<ENetPacket, enet_packet_destroy>

Enumerations

enum  net::net_e : int { net::PC , net::LAN , net::WAN }
enum  net::af_e : int { net::IPV4 , net::BOTH }

Functions

void net::free_host (ENetHost *host)
std::uint16_t net::map_port (int port)
 Map a specified port based on the base port.
net_e net::from_enum_string (const std::string_view &view)
std::string_view net::to_enum_string (net_e net)
net_e net::from_address (const std::string_view &view)
 Classify the network an address belongs to.
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_ranges (const std::string_view &view)
 Classify an address by the fixed ranges alone.
std::vector< interface_address_t > net::interface_addresses ()
 The addresses of every interface that is up, loopback aside, with their prefix lengths.
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.
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.
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.
std::string net::mdns_instance_name (const std::string_view &hostname)
 Returns a string for use as the instance name for mDNS.

Detailed Description

Declarations for networking related functions.

Definition in file network.h.

Typedef Documentation

◆ host_t

using net::host_t = util::safe_ptr<ENetHost, free_host>

Definition at line 35 of file network.h.

◆ packet_t

using net::packet_t = util::safe_ptr<ENetPacket, enet_packet_destroy>

Definition at line 37 of file network.h.

◆ peer_t

using net::peer_t = ENetPeer *

Definition at line 36 of file network.h.

Enumeration Type Documentation

◆ af_e

enum net::af_e : int
Enumerator
IPV4 

IPv4 only.

BOTH 

IPv4 and IPv6.

Definition at line 45 of file network.h.

◆ net_e

enum net::net_e : int
Enumerator
PC 

PC.

LAN 

LAN.

WAN 

WAN.

Definition at line 39 of file network.h.

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.