Back to Site
Loading...
Searching...
No Matches
port_mapping_service.h
Go to the documentation of this file.
1#pragma once
2
19#include "util/rats_export.h"
20#include "node/peer_network.h"
21#include "nat/port_mapping.h" // PortMappingConfig, PortMapResult, PortMapCallback
22
23#include <cstdint>
24#include <memory>
25#include <mutex>
26#include <optional>
27#include <string>
28#include <utility>
29
30namespace librats {
31
32class UpnpClient;
33class NatPmpClient;
34
38class RATS_API PortMappingService final : public Subsystem {
39public:
42
43 void attach(NodeContext& ctx) override;
44 void start() override;
45 void stop() override;
46
49 void on_result(PortMapCallback cb) { user_callback_ = std::move(cb); }
50
53 std::optional<std::pair<std::string, uint16_t>> mapped_public_address() const;
54
55private:
56 void handle_result(const PortMapResult& result);
57
58 PortMappingConfig config_;
59 PeerNetwork* network_ = nullptr;
60 PortMapCallback user_callback_;
61
62 mutable std::mutex mutex_;
63 std::unique_ptr<UpnpClient> upnp_;
64 std::unique_ptr<NatPmpClient> natpmp_;
65 std::string mapped_external_ip_;
66 uint16_t mapped_external_tcp_port_ = 0;
67 bool double_nat_warned_ = false;
68};
69
70} // namespace librats
Maps the node's TCP listen port through the home router via UPnP and/or NAT-PMP.
void attach(NodeContext &ctx) override
void on_result(PortMapCallback cb)
Observe mapping results (established / refreshed / failed).
std::optional< std::pair< std::string, uint16_t > > mapped_public_address() const
The public endpoint peers should reach us on, once a backend reports a usable (genuinely public) exte...
PortMappingService(PortMappingConfig config={})
A pluggable network subsystem.
Definition node.h:66
std::function< void(const PortMapResult &)> PortMapCallback
Callback invoked whenever a mapping is established, refreshed, removed or fails.
The narrow contract a subsystem needs from the node — and nothing more.
Shared types for automatic NAT port forwarding (UPnP IGD + NAT-PMP)
Result of a port mapping attempt.
Configuration for automatic port forwarding (see PortMappingService).