Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
network_link.h
Go to the documentation of this file.
1
8#pragma once
9
10// standard includes
11#include <algorithm>
12#include <array>
13#include <bit>
14#include <cstdint>
15#include <cstring>
16#include <optional>
17#include <span>
18
19// platform includes
20#include <netinet/in.h>
21#include <sys/socket.h>
22
23namespace net {
24
29 bool v6 {false};
30 std::array<std::uint8_t, 16> bytes {};
31 std::uint32_t scope {0};
32
34 std::size_t size() const {
35 return v6 ? 16 : 4;
36 }
37 };
38
43 raw_address_t address;
44 unsigned prefix {0};
45 };
46
52 inline bool same_scope(const raw_address_t &a, const raw_address_t &b) {
53 return a.scope == 0 || b.scope == 0 || a.scope == b.scope;
54 }
55
61 inline std::optional<raw_address_t> raw_from_sockaddr(const sockaddr *addr) {
62 if (addr == nullptr) {
63 return std::nullopt;
64 }
65 raw_address_t raw;
66 if (addr->sa_family == AF_INET) {
67 const auto *sin = reinterpret_cast<const sockaddr_in *>(addr);
68 std::memcpy(raw.bytes.data(), &sin->sin_addr, 4);
69 return raw;
70 }
71 if (addr->sa_family != AF_INET6) {
72 return std::nullopt;
73 }
74 const auto *sin6 = reinterpret_cast<const sockaddr_in6 *>(addr);
75 std::memcpy(raw.bytes.data(), &sin6->sin6_addr, 16);
76 constexpr std::array<std::uint8_t, 12> v4_mapped {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff};
77 if (std::equal(v4_mapped.begin(), v4_mapped.end(), raw.bytes.begin())) {
78 std::memmove(raw.bytes.data(), raw.bytes.data() + 12, 4);
79 std::fill(raw.bytes.begin() + 4, raw.bytes.end(), 0);
80 return raw;
81 }
82 raw.v6 = true;
83 raw.scope = sin6->sin6_scope_id;
84 return raw;
85 }
86
93 inline unsigned prefix_from_netmask(const sockaddr *mask, bool v6) {
94 if (mask == nullptr) {
95 return 0;
96 }
97 std::array<std::uint8_t, 16> bytes {};
98 std::size_t size = 4;
99 if (v6) {
100 std::memcpy(bytes.data(), &reinterpret_cast<const sockaddr_in6 *>(mask)->sin6_addr, 16);
101 size = 16;
102 } else {
103 std::memcpy(bytes.data(), &reinterpret_cast<const sockaddr_in *>(mask)->sin_addr, 4);
104 }
105 unsigned prefix = 0;
106 for (std::size_t i = 0; i < size; ++i) {
107 const auto ones = static_cast<unsigned>(std::countl_one(bytes[i]));
108 prefix += ones;
109 if (ones < 8) {
110 break;
111 }
112 }
113 return prefix;
114 }
115
120 inline bool in_subnet(const raw_address_t &address, const raw_address_t &network, unsigned prefix) {
121 if (address.v6 != network.v6) {
122 return false;
123 }
124 const auto bits = std::min<std::size_t>(prefix, address.size() * 8);
125 const auto whole = bits / 8;
126 if (!std::equal(address.bytes.begin(), address.bytes.begin() + whole, network.bytes.begin())) {
127 return false;
128 }
129 if (const auto rest = bits % 8) {
130 const auto mask = static_cast<std::uint8_t>(0xff << (8 - rest));
131 return (address.bytes[whole] & mask) == (network.bytes[whole] & mask);
132 }
133 return true;
134 }
135
144 inline bool on_link(const raw_address_t &address, std::span<const interface_address_t> links) {
145 return std::ranges::any_of(links, [&](const interface_address_t &link) {
146 return link.prefix > 0 && same_scope(address, link.address) && in_subnet(address, link.address, link.prefix);
147 });
148 }
149
156 inline bool same_address(const raw_address_t &interface_address, const raw_address_t &wanted) {
157 return interface_address.v6 == wanted.v6 && same_scope(interface_address, wanted) &&
158 std::equal(wanted.bytes.begin(), wanted.bytes.begin() + wanted.size(), interface_address.bytes.begin());
159 }
160
161} // namespace net
An address of one of this host's interfaces and the length of its subnet's prefix.
unsigned prefix
0 when the interface reported no netmask.
An IP address as the bytes the network carries it in.
bool v6
IPv6 when set, IPv4 otherwise.
std::uint32_t scope
IPv6 scope (interface index), 0 when it has none.
std::array< std::uint8_t, 16 > bytes
Network byte order; IPv4 uses the first four.
std::size_t size() const
Bytes that carry the address: 4 for IPv4, 16 for IPv6.