Back to Site
Loading...
Searching...
No Matches
address.h
Go to the documentation of this file.
1#pragma once
2
23#include "util/rats_export.h"
24#include "core/ip_address.h"
25
26#include <cstdint>
27#include <functional>
28#include <optional>
29#include <string>
30#include <string_view>
31
32namespace librats {
33
34struct RATS_API Address {
35 IpAddress ip;
36 uint16_t port = 0;
37
38 Address() = default;
39 Address(IpAddress ip, uint16_t port) : ip(ip), port(port) {}
40
45 Address(std::string_view numeric_ip, uint16_t port);
46
51 static std::optional<Address> parse(std::string_view text);
52
54 std::string to_string() const;
55
57 bool is_valid() const noexcept { return !ip.is_unspecified() && port != 0; }
58
59 bool operator==(const Address& o) const noexcept { return port == o.port && ip == o.ip; }
60 bool operator!=(const Address& o) const noexcept { return !(*this == o); }
61 bool operator<(const Address& o) const noexcept {
62 return ip != o.ip ? ip < o.ip : port < o.port;
63 }
64};
65
66} // namespace librats
67
68namespace std {
69template <>
70struct hash<librats::Address> {
71 std::size_t operator()(const librats::Address& a) const noexcept {
72 // Fold the port into the ip hash (FNV-style) — no temporary string.
73 std::size_t h = a.ip.hash();
74 h ^= (static_cast<std::size_t>(a.port) + 0x9e3779b97f4a7c15ull + (h << 6) + (h >> 2));
75 return h;
76 }
77};
78} // namespace std
Definition node.h:66
STL namespace.
Address(IpAddress ip, uint16_t port)
Definition address.h:39
Address(std::string_view numeric_ip, uint16_t port)
Build from a numeric IP literal + port.
std::string to_string() const
IPv6 endpoints serialise bracketed; everything else plain. "" ip → ":port".
bool operator!=(const Address &o) const noexcept
Definition address.h:60
bool operator<(const Address &o) const noexcept
Definition address.h:61
IpAddress ip
Definition address.h:35
Address()=default
static std::optional< Address > parse(std::string_view text)
Parse "host:port" or "[ipv6]:port".
bool operator==(const Address &o) const noexcept
Definition address.h:59
bool is_valid() const noexcept
A usable dial target: a specified ip and a non-zero port.
Definition address.h:57
std::size_t operator()(const librats::Address &a) const noexcept
Definition address.h:71