Skip to content

Commit d2f982b

Browse files
committed
feat: Add NAT-PMP gateway_address configuration option.
1 parent 0f7f460 commit d2f982b

File tree

8 files changed

+33
-4
lines changed

8 files changed

+33
-4
lines changed

docs/Editing-Configuration-Files.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Here is a sample of the three basic types: respectively Boolean, Number and Stri
113113
* **peer-port-random-low:** Number (default = 1024)
114114
* **peer-port-random-on-start:** Boolean (default = false)
115115
* **port-forwarding-enabled:** Boolean (default = true) Enable [UPnP](https://en.wikipedia.org/wiki/Universal_Plug_and_Play) or [NAT-PMP](https://en.wikipedia.org/wiki/NAT_Port_Mapping_Protocol).
116+
* **gateway_address**: String? (default = null) Gateway to use for NAT-PMP.
116117

117118
#### Queuing
118119
* **download-queue-enabled:** Boolean (default = true) When true, Transmission will only download `download-queue-size` non-stalled torrents at once.

libtransmission/port-forwarding-natpmp.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,19 @@ void tr_natpmp::setCommandTime()
6464
command_time_ = tr_time() + CommandWaitSecs;
6565
}
6666

67-
tr_natpmp::PulseResult tr_natpmp::pulse(tr_port local_port, bool is_enabled)
67+
tr_natpmp::PulseResult tr_natpmp::pulse(tr_port local_port, bool is_enabled, std::optional<tr_address> gateway)
6868
{
6969
if (is_enabled && state_ == State::Discover)
7070
{
71-
int val = initnatpmp(&natpmp_, 0, 0);
71+
int forcegw = 0;
72+
in_addr_t forcedgw = 0;
73+
if (gateway && gateway->is_ipv4())
74+
{
75+
forcegw = 1;
76+
forcedgw = gateway->addr.addr4.s_addr;
77+
}
78+
79+
int val = initnatpmp(&natpmp_, forcegw, forcedgw);
7280
log_val("initnatpmp", val);
7381
val = sendpublicaddressrequest(&natpmp_);
7482
log_val("sendpublicaddressrequest", val);

libtransmission/port-forwarding-natpmp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class tr_natpmp
4949
tr_port advertised_port;
5050
};
5151

52-
PulseResult pulse(tr_port local_port, bool is_enabled);
52+
PulseResult pulse(tr_port local_port, bool is_enabled, std::optional<tr_address> gateway);
5353

5454
private:
5555
enum class State : uint8_t

libtransmission/port-forwarding.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class tr_port_forwarding_impl final : public tr_port_forwarding
192192

193193
auto const old_state = state();
194194

195-
auto const result = natpmp_->pulse(mediator_.local_peer_port(), is_enabled);
195+
auto const result = natpmp_->pulse(mediator_.local_peer_port(), is_enabled, mediator_.gateway_address());
196196
natpmp_state_ = result.state;
197197
if (!std::empty(result.local_port) && !std::empty(result.advertised_port))
198198
{

libtransmission/port-forwarding.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class tr_port_forwarding
3030

3131
[[nodiscard]] virtual tr_port advertised_peer_port() const = 0;
3232
[[nodiscard]] virtual tr_port local_peer_port() const = 0;
33+
[[nodiscard]] virtual std::optional<tr_address> gateway_address() const = 0;
3334
[[nodiscard]] virtual tr_address incoming_peer_address() const = 0;
3435
[[nodiscard]] virtual libtransmission::TimerMaker& timer_maker() = 0;
3536
virtual void on_port_forwarded(tr_port advertised_port) = 0;

libtransmission/quark.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ auto constexpr MyStatic = std::array<std::string_view, TR_N_KEYS>{
136136
"fromLtep"sv,
137137
"fromPex"sv,
138138
"fromTracker"sv,
139+
"gateway_address"sv,
139140
"group"sv,
140141
"hasAnnounced"sv,
141142
"hasScraped"sv,

libtransmission/quark.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ enum // NOLINT(performance-enum-size)
138138
TR_KEY_fromLtep,
139139
TR_KEY_fromPex,
140140
TR_KEY_fromTracker,
141+
TR_KEY_gateway_address,
141142
TR_KEY_group,
142143
TR_KEY_hasAnnounced,
143144
TR_KEY_hasScraped,

libtransmission/session.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ struct tr_session
213213
return session_.localPeerPort();
214214
}
215215

216+
[[nodiscard]] std::optional<tr_address> gateway_address() const override
217+
{
218+
return session_.gateway_address();
219+
}
220+
216221
[[nodiscard]] libtransmission::TimerMaker& timer_maker() override
217222
{
218223
return session_.timerMaker();
@@ -433,6 +438,7 @@ struct tr_session
433438
std::array<tr_preferred_transport, TR_NUM_PREFERRED_TRANSPORT> preferred_transport = { TR_PREFER_UTP, TR_PREFER_TCP };
434439
std::chrono::milliseconds sleep_per_seconds_during_verify = std::chrono::milliseconds{ 100 };
435440
std::optional<std::string> proxy_url;
441+
std::optional<std::string> gateway_address;
436442
std::string announce_ip;
437443
std::string bind_address_ipv4;
438444
std::string bind_address_ipv6;
@@ -471,6 +477,7 @@ struct tr_session
471477
{ TR_KEY_download_queue_enabled, &download_queue_enabled },
472478
{ TR_KEY_download_queue_size, &download_queue_size },
473479
{ TR_KEY_encryption, &encryption_mode },
480+
{ TR_KEY_gateway_address, &gateway_address },
474481
{ TR_KEY_idle_seeding_limit, &idle_seeding_limit_minutes },
475482
{ TR_KEY_idle_seeding_limit_enabled, &idle_seeding_limit_enabled },
476483
{ TR_KEY_incomplete_dir, &incomplete_dir },
@@ -910,6 +917,16 @@ struct tr_session
910917
return advertised_peer_port_;
911918
}
912919

920+
[[nodiscard]] std::optional<tr_address> gateway_address() const noexcept
921+
{
922+
if (const auto addr = settings().gateway_address)
923+
{
924+
return tr_address::from_string(*addr);
925+
}
926+
927+
return {};
928+
}
929+
913930
[[nodiscard]] constexpr auto queueEnabled(tr_direction dir) const noexcept
914931
{
915932
return dir == TR_DOWN ? settings_.download_queue_enabled : settings_.seed_queue_enabled;

0 commit comments

Comments
 (0)