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

Whether an address shares a subnet with one of this host's interfaces. More...

#include <algorithm>
#include <array>
#include <bit>
#include <cstdint>
#include <cstring>
#include <optional>
#include <span>
#include <netinet/in.h>
#include <sys/socket.h>
Include dependency graph for network_link.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  net::raw_address_t
 An IP address as the bytes the network carries it in. More...
struct  net::interface_address_t
 An address of one of this host's interfaces and the length of its subnet's prefix. More...

Functions

bool net::same_scope (const raw_address_t &a, const raw_address_t &b)
 Whether two scopes can name the same link.
std::optional< raw_address_t > net::raw_from_sockaddr (const sockaddr *addr)
 The address a sockaddr holds.
unsigned net::prefix_from_netmask (const sockaddr *mask, bool v6)
 The prefix length of a netmask.
bool net::in_subnet (const raw_address_t &address, const raw_address_t &network, unsigned prefix)
 Whether address lies in network / prefix.
bool net::on_link (const raw_address_t &address, std::span< const interface_address_t > links)
 Whether address is on a subnet one of links is attached to.
bool net::same_address (const raw_address_t &interface_address, const raw_address_t &wanted)
 Whether an interface address is the address the host was reached on.

Detailed Description

Whether an address shares a subnet with one of this host's interfaces.

Pure logic over raw addresses, so it can be tested without a network: the platform lists the interfaces, net::from_address() decides with them.

Definition in file network_link.h.

Function Documentation

◆ in_subnet()

bool net::in_subnet ( const raw_address_t & address,
const raw_address_t & network,
unsigned prefix )
inline

Whether address lies in network / prefix.

Addresses of different families never match.

Definition at line 120 of file network_link.h.

◆ on_link()

bool net::on_link ( const raw_address_t & address,
std::span< const interface_address_t > links )
inline

Whether address is on a subnet one of links is attached to.

Hestia RS decides "local" the same way (netif::on_link): an IPv6 global address of the home network shares the interface's /64 even though no private range covers it. A VPN such as Tailscale gives the host a host route (/32, /128), which covers no other address, and an interface without a netmask covers nothing.

Definition at line 144 of file network_link.h.

◆ prefix_from_netmask()

unsigned net::prefix_from_netmask ( const sockaddr * mask,
bool v6 )
inline

The prefix length of a netmask.

The mask is read as the address's family (v6): some platforms leave the netmask's own family unset. Counting stops at the first zero bit.

Returns
The prefix length, 0 for a null mask.

Definition at line 93 of file network_link.h.

◆ raw_from_sockaddr()

std::optional< raw_address_t > net::raw_from_sockaddr ( const sockaddr * addr)
inline

The address a sockaddr holds.

IPv4-mapped IPv6 comes back as the IPv4 address it carries.

Returns
The address, or nothing for a null pointer or another family.

Definition at line 61 of file network_link.h.

◆ same_address()

bool net::same_address ( const raw_address_t & interface_address,
const raw_address_t & wanted )
inline

Whether an interface address is the address the host was reached on.

Compares the bytes, not the text: a link-local address printed with its scope (fe80::1eth0) and the same address read from the interface are equal, as long as the scopes do not name different links.

Definition at line 156 of file network_link.h.

◆ same_scope()

bool net::same_scope ( const raw_address_t & a,
const raw_address_t & b )
inline

Whether two scopes can name the same link.

An address without a scope matches any; two scoped addresses (IPv6 link-local) only match on the same interface.

Definition at line 52 of file network_link.h.