Back to Site
Loading...
Searching...
No Matches
port_mapping_service.h
Go to the documentation of this file.
1#pragma once
2
26
27#include "librats/util/rats_export.h"
29#include "librats/nat/port_mapping.h" // PortMappingConfig, PortMapResult, PortMapCallback
30
31#include <cstdint>
32#include <memory>
33#include <mutex>
34#include <optional>
35#include <string>
36#include <utility>
37
38namespace librats {
39
40class EventBus;
41class UpnpClient;
42class NatPmpClient;
43
47class RATS_API PortMappingService final : public Subsystem {
48public:
51
52 void attach(NodeContext& ctx) override;
53 void start() override;
54 void stop() override;
55
58 void on_result(PortMapCallback cb) { user_callback_ = std::move(cb); }
59
64 std::optional<std::pair<std::string, uint16_t>> mapped_public_address(
65 PortMapProtocol protocol = PortMapProtocol::TCP) const;
66
67private:
68 void handle_result(const PortMapResult& result);
70 uint16_t& external_port_slot(PortMapProtocol protocol);
71
72 PortMappingConfig config_;
73 PeerNetwork* network_ = nullptr;
74 EventBus* events_ = nullptr;
75 PortMapCallback user_callback_;
76
77 mutable std::mutex mutex_;
78 std::unique_ptr<UpnpClient> upnp_;
79 std::unique_ptr<NatPmpClient> natpmp_;
80 std::string mapped_external_ip_;
81 uint16_t mapped_external_tcp_port_ = 0;
82 uint16_t mapped_external_udp_port_ = 0;
83 bool double_nat_warned_ = false;
84};
85
86} // namespace librats
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(PortMapProtocol protocol=PortMapProtocol::TCP) const
The public endpoint peers should reach us on over protocol, once a backend reports a usable (genuinel...
PortMappingService(PortMappingConfig config={})
A pluggable network subsystem.
Definition node.h:75
PortMapProtocol
Transport protocol of a port mapping.
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).