23#ifndef JAIABOT_UTILS_IP_H
24#define JAIABOT_UTILS_IP_H
34#include <boost/asio/ip/address_v4.hpp>
35#include <boost/asio/ip/address_v6.hpp>
36#include <boost/system/error_code.hpp>
93 "the VirtualFleet WLAN octet must not be one an IPv4 fleet uses");
98 "cloudhub_id must be a valid hub id");
104 "iridium_id must be a valid hub id other than the CloudHub's");
116 throw std::invalid_argument(
"Unknown node type");
131 throw std::invalid_argument(
"Unknown node type: " + s);
147 throw std::invalid_argument(
"Unknown network");
154 if (s ==
"fleet_vpn")
156 if (s ==
"vfleet_vpn")
158 if (s ==
"cloudhub_vpn")
160 if (s ==
"cloudhub_eth")
162 if (s ==
"vfleet_eth")
164 if (s ==
"vfleet_wlan")
168 throw std::invalid_argument(
"Unknown network: " + s);
182 throw std::invalid_argument(
"Unknown node type");
188 throw std::invalid_argument(
"fleet_id " + std::to_string(fleet_id) +
189 " is not in range: " + std::to_string(
fleet_id_min) +
" to " +
196 if (node_id < range.first || node_id > range.second)
197 throw std::invalid_argument(
"node_id " + std::to_string(node_id) +
" for " +
199 " is not in range: " + std::to_string(range.first) +
" to " +
200 std::to_string(range.second));
229 throw std::invalid_argument(
"Unknown network");
237 using namespace boost::asio::ip;
240 auto fleet_octet = [&]()
243 throw std::invalid_argument(
"fleet_id " + std::to_string(fleet_id) +
" has no IPv4 " +
246 return static_cast<uint8_t
>(fleet_id);
253 return make_address_v4(address_v4::bytes_type{10, 23, fleet_octet(), 0});
256 return make_address_v4(address_v4::bytes_type{
263 return make_address_v4(address_v4::bytes_type{172, 23, fleet_octet(), 0});
266 return make_address_v4(address_v4::bytes_type{10, 23, 255, 0});
269 return make_address_v4(address_v4::bytes_type{10, 23, 254, 0});
272 return make_address_v4(address_v4::bytes_type{10, 23, 0, 0});
273 default:
throw std::invalid_argument(
"No IPv4 base for network: " +
network_to_string(net));
288 throw std::invalid_argument(
"No IPv4 prefix for network: " +
network_to_string(net));
296 case NodeType::hub:
return static_cast<uint32_t
>(10 + node_id);
297 case NodeType::bot:
return static_cast<uint32_t
>(100 + node_id);
301 throw std::invalid_argument(
"desktop node is not supported for ipv4");
303 throw std::invalid_argument(
"Unknown node type for IPv4");
310 auto bytes =
addr.to_bytes();
311 uint64_t carry = offset;
312 for (
int i = 15; i >= 0 && carry > 0; --i)
314 uint64_t sum =
static_cast<uint64_t
>(bytes[i]) + (carry & 0xFF);
315 bytes[i] =
static_cast<uint8_t
>(sum & 0xFF);
316 carry = (carry >> 8) + (sum >> 8);
318 return boost::asio::ip::address_v6(bytes);
326 auto bytes =
addr.to_bytes();
327 uint64_t carry = offset;
328 for (
int i = 7; i >= 0 && carry > 0; --i)
330 uint64_t sum =
static_cast<uint64_t
>(bytes[i]) + (carry & 0xFF);
331 bytes[i] =
static_cast<uint8_t
>(sum & 0xFF);
332 carry = (carry >> 8) + (sum >> 8);
334 return boost::asio::ip::address_v6(bytes);
340 auto bytes =
addr.to_bytes();
341 for (
int i = 8; i < 16; ++i) bytes[i] = 0;
342 return boost::asio::ip::address_v6(bytes);
345inline boost::asio::ip::address_v6
parse_ipv6(
const std::string& s)
349 std::string addr_str = s.substr(0, s.find(
'/'));
350 boost::system::error_code ec;
351 auto addr = boost::asio::ip::make_address_v6(addr_str, ec);
353 throw std::invalid_argument(
"Invalid IPv6 address: " + s +
" (" + ec.message() +
")");
360 const std::string& ipv6_base_str =
"")
362 auto make_prefix_addr = [&](
const char* prefix)
367 for (
int shift = 12; shift >= 0; shift -= 4)
369 int nibble = (fleet_id >> shift) & 0xF;
370 if (nibble != 0 || n != 0)
371 hex[n++] =
"0123456789abcdef"[nibble];
376 return parse_ipv6(std::string(prefix) + hex +
"::");
379 auto get_base_from_arg = [&]()
381 if (ipv6_base_str.empty())
382 throw std::invalid_argument(
"--ipv6_base is required for network: " +
389 case Network::wlan:
return make_prefix_addr(
"fddd:7f2e:3258:");
396 return get_base_from_arg();
406 default:
throw std::invalid_argument(
"No IPv6 base for network: " +
network_to_string(net));
416 case NodeType::hub:
return static_cast<uint64_t
>(1 * 65536 + node_id);
417 case NodeType::bot:
return static_cast<uint64_t
>(2 * 65536 + node_id);
419 return static_cast<uint64_t
>(3 * 65536 + node_id);
421 return static_cast<uint64_t
>(4 * 65536 + node_id);
424 throw std::invalid_argument(
"Unknown node type for IPv6");
430inline bool read_digits(
const std::string& s, std::size_t& pos,
int& out,
int max_value = 1000000)
432 std::size_t start = pos;
434 while (pos < s.size() && s[pos] >=
'0' && s[pos] <=
'9')
436 value = value * 10 + (s[pos] -
'0');
437 if (value > max_value)
454 const char* env_fleet_id = std::getenv(
"jaia_fleet_id");
455 if (env_fleet_id !=
nullptr && *env_fleet_id !=
'\0')
457 std::string env_str(env_fleet_id);
458 std::size_t env_pos = 0;
460 if (!
read_digits(env_str, env_pos, env_value) || env_pos != env_str.size())
461 throw std::invalid_argument(
"Invalid 'jaia_fleet_id' environmental variable: " +
466 char hostname[HOST_NAME_MAX + 1] = {0};
467 if (gethostname(hostname,
sizeof(hostname) - 1) == 0)
469 std::string host(hostname);
470 const std::string marker(
"-fleet");
471 std::size_t marker_pos = host.find(marker);
472 if (marker_pos != std::string::npos)
474 std::size_t host_pos = marker_pos + marker.size();
476 if (
read_digits(host, host_pos, host_value) && host_pos == host.size())
481 throw std::invalid_argument(
482 "Could not find fleet ID. Either specify as 'fN' suffix (e.g., b1f3), or provide via the "
483 "'jaia_fleet_id' environmental variable (set for login shells by /etc/profile.d/jaia.sh), "
484 "or run on a host named '<type><id>-fleet<fleet_id>'");
496 auto result = boost::asio::ip::make_address_v4(base.to_uint() + offset);
497 return result.to_string();
506 return base.to_string() +
"/" + std::to_string(prefix);
512 const std::string& ipv6_base_str =
"")
519 return result.to_string();
523inline std::string
ipv6_net(
int fleet_id,
Network net,
const std::string& ipv6_base_str =
"")
527 return base.to_string() +
"/64";
534 return ipv4_addr(fleet_id, net, node, node_id);
536 return ipv6_addr(fleet_id, net, node, node_id);
540 "It must be b<bot_id>[svc]f<fleet_id> or h<hub_id>[svc]f<fleet_id> or chf<fleet_id> "
563 if (host_code ==
"self")
571 const std::string jaia_tech_domain =
".jaia.tech";
572 if (host_code.size() > jaia_tech_domain.size() &&
573 host_code.compare(host_code.size() - jaia_tech_domain.size(), jaia_tech_domain.size(),
574 jaia_tech_domain) == 0)
583 return std::invalid_argument(
"Host string is invalid: " + host_code +
". " +
587 auto read_int = [&](std::size_t& pos,
int& out)
594 if (host_code.empty())
599 if (host_code.compare(0, 2,
"ch") == 0)
607 else if (host_code[0] ==
'b' || host_code[0] ==
'h')
611 if (!read_int(pos, result.
node_id))
614 if (pos < host_code.size())
616 switch (host_code[pos])
639 bool have_fleet_id =
false;
640 if (pos < host_code.size())
642 if (host_code[pos] !=
'f')
645 if (!read_int(pos, result.
fleet_id))
647 have_fleet_id =
true;
650 if (pos != host_code.size())
686 if (parsed !=
nullptr)
bool read_digits(const std::string &s, std::size_t &pos, int &out, int max_value=1000000)
boost::asio::ip::address_v6 ipv6_base(int fleet_id, Network net, const std::string &ipv6_base_str="")
int ipv4_prefix_len(Network net)
boost::asio::ip::address_v6 ipv6_add_subnet_offset(boost::asio::ip::address_v6 addr, uint64_t offset)
uint64_t ipv6_node_offset(NodeType node, int node_id)
boost::asio::ip::address_v6 ipv6_add_offset(boost::asio::ip::address_v6 addr, uint64_t offset)
boost::asio::ip::address_v4 ipv4_base(int fleet_id, Network net)
uint32_t ipv4_node_offset(NodeType node, int node_id)
boost::asio::ip::address_v6 ipv6_mask64(boost::asio::ip::address_v6 addr)
boost::asio::ip::address_v6 parse_ipv6(const std::string &s)
std::string addr(int fleet_id, Network net, NodeType node, int node_id)
constexpr int rpicam_id_max
constexpr int desktop_id_max
std::string host_code_to_addr(const std::string &host_code, HostCode *parsed=nullptr)
void validate_fleet_id(int fleet_id)
std::string ipv4_addr(int fleet_id, Network net, NodeType node, int node_id)
void validate_node_id(NodeType node, int node_id)
std::pair< int, int > node_id_range(NodeType node)
constexpr int fleet_id_ipv4_max
bool is_ipv4_fleet(int fleet_id)
IPVersion ip_version(int fleet_id, Network net)
constexpr int vfleet_wlan_ipv4_octet
NodeType node_type_from_string(const std::string &s)
std::string node_type_to_string(NodeType node)
std::string network_to_string(Network net)
constexpr const char * host_code_format_msg
Network network_from_string(const std::string &s)
constexpr int rpicam_id_min
HostCode parse_host_code(const std::string &host_code, int default_fleet_id=-1)
constexpr int cloudhub_id
std::string host_code_addr(const HostCode &host, IPVersion version)
constexpr int gateway_id_min
constexpr int desktop_id_min
constexpr int fleet_id_min
std::string ipv4_net(int fleet_id, Network net)
std::string ipv6_addr(int fleet_id, Network net, NodeType node, int node_id, const std::string &ipv6_base_str="")
constexpr int fleet_id_max
constexpr int gateway_id_max
std::string ipv6_net(int fleet_id, Network net, const std::string &ipv6_base_str="")