Back to Site
Loading...
Searching...
No Matches
port_mapping_service.h
Go to the documentation of this file.
1#pragma once
2
19#include "node/peer_network.h"
20#include "nat/port_mapping.h" // PortMappingConfig, PortMapResult, PortMapCallback
21
22#include <cstdint>
23#include <memory>
24#include <mutex>
25#include <optional>
26#include <string>
27#include <utility>
28
29namespace librats {
30
31class UpnpClient;
32class NatPmpClient;
33
37class PortMappingService final : public Subsystem {
38public:
41
42 void attach(NodeContext& ctx) override;
43 void start() override;
44 void stop() override;
45
48 void on_result(PortMapCallback cb) { user_callback_ = std::move(cb); }
49
52 std::optional<std::pair<std::string, uint16_t>> mapped_public_address() const;
53
54private:
55 void handle_result(const PortMapResult& result);
56
57 PortMappingConfig config_;
58 PeerNetwork* network_ = nullptr;
59 PortMapCallback user_callback_;
60
61 mutable std::mutex mutex_;
62 std::unique_ptr<UpnpClient> upnp_;
63 std::unique_ptr<NatPmpClient> natpmp_;
64 std::string mapped_external_ip_;
65 uint16_t mapped_external_tcp_port_ = 0;
66 bool double_nat_warned_ = false;
67};
68
69} // 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:65
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).