Back to Site
Loading...
Searching...
No Matches
nat_status.h
Go to the documentation of this file.
1#pragma once
2
37#include "librats/util/rats_export.h"
40
41#include <cstddef>
42#include <mutex>
43#include <unordered_map>
44#include <vector>
45
46namespace librats {
47
50enum class NatMapping : uint8_t {
51 Unknown,
52 Open,
55};
56
57RATS_API const char* to_string(NatMapping) noexcept;
58
65public:
66 virtual ~ExternalAddressService() = default;
67
71 virtual std::vector<Address> external_udp_endpoints() const = 0;
72
74 virtual NatMapping udp_mapping() const = 0;
75};
76
77class RATS_API NatStatus final : public ExternalAddressService {
78public:
82 static constexpr size_t kMaxObservers = 32;
83
88 void record_udp_observation(const PeerId& reporter, const Address& external,
89 bool is_own_address);
90
93 void forget(const PeerId& reporter);
94
97 void reset();
98
100 size_t observation_count() const;
101
102 // — ExternalAddressService —
103 std::vector<Address> external_udp_endpoints() const override;
104 NatMapping udp_mapping() const override;
105
106private:
107 struct Observation {
108 Address endpoint;
109 bool is_own_address = false;
110 uint64_t seq = 0;
111 };
112
113 mutable std::mutex mutex_;
114 std::unordered_map<PeerId, Observation, PeerId::Hash> seen_;
115 uint64_t next_seq_ = 1;
116};
117
118} // namespace librats
A dialable transport endpoint: a numeric IP + port.
The read side, published in the node's ServiceRegistry so a NAT-traversal module can consult it witho...
Definition nat_status.h:64
virtual std::vector< Address > external_udp_endpoints() const =0
Endpoints peers have reported observing our shared datagram socket at, most recently confirmed first.
virtual NatMapping udp_mapping() const =0
What the observations so far say about the mapping.
virtual ~ExternalAddressService()=default
NatMapping udp_mapping() const override
What the observations so far say about the mapping.
void forget(const PeerId &reporter)
Forget a reporter's observation — its link is gone and what it saw may have been a mapping that died ...
size_t observation_count() const
How many distinct peers have reported an endpoint.
void record_udp_observation(const PeerId &reporter, const Address &external, bool is_own_address)
Record that reporter observed our datagram socket at external.
void reset()
Drop everything.
std::vector< Address > external_udp_endpoints() const override
Endpoints peers have reported observing our shared datagram socket at, most recently confirmed first.
Definition node.h:73
const char * to_string(NatMapping) noexcept
NatMapping
How the NAT in front of our shared datagram socket maps it, as far as the mesh has been able to show.
Definition nat_status.h:50
@ EndpointIndependent
one external port for every destination — punchable
@ Unknown
not enough independent observations yet
@ Open
no NAT: peers see us at an address we hold ourselves
@ EndpointDependent
a fresh mapping per destination (symmetric) — not punchable
Self-certifying peer identity.