# Ports

> From the Guides page of the Rtorrent 16 panel, version 16.29-14U. Polish: https://nasutils.net/rtorrent16/docs/pl/ports.md

Peers reach rtorrent on one BitTorrent port, TCP and UDP, and DHT on one UDP port.

## Check that peers can reach you

*Figure: The BitTorrent port card on the Network & NAT page*

1. Open **Network & NAT**. The page checks the route and the port as it opens, reusing a result younger than five
   minutes.
2. Press **Check TCP port** for a fresh check. It asks a public checker to connect back to the port.
3. Read the verdict: the port is open, not reachable, or unverified when no outside check gave an answer.
4. Read the four steps under it. They show where the way in stops: something listens on the port on the NAS, the
   router lists a mapping for it, a connection to your own public address comes back through the router, and a
   checker on the internet connects. Many routers refuse the third one; that alone is no fault.

The Dashboard has the same check in its **BitTorrent port** card: **Check port** runs the steps on the NAS,
**Ask the internet** adds the public checker, and the card checks again by itself when its result is older than ten
minutes. Quick access shows the result in its **Ports** strip, a dot for IPv4 and one for IPv6.

## Open the port on the router

*Figure: The Network services table on the Network & NAT page*

1. Find the port in use under **Network services**: the same number for BitTorrent over TCP and over UDP
   (uTP), and the DHT port on its own row.
2. Let the router map it: turn on **Automatic port forwarding (UPnP)** and **Automatic port forwarding (PCP /
   NAT-PMP)** (`upnp`, `natpmp`; the row **Router port mappings** shows them as UPnP and PCP / NAT-PMP) in
   **Settings → Port forwarding & VPN**. The package asks the router at every start, and **Mapped as** shows what
   the router granted. **By** shows how the mapping was requested. Every five minutes the watchdog renews the PCP and NAT-PMP
   mappings; with only UPnP on, it reads the router's UPnP table and asks again for a mapping the router no longer has
   (after a router restart, for example).
3. Or forward the ports by hand on the router: TCP and UDP of the BitTorrent port and UDP of the DHT port, to the
   NAS's LAN address. A forward made by hand does not show under **Mapped as**. When the router refuses a mapping,
   the package asks a port checker on the internet whether the TCP port reaches the NAS all the same. A forward made
   by hand gets one line in the System Logs ("Port 42000 is reachable from the internet (forwarded on the router,
   checked over TCP). UPnP not needed.") and no warning. A warning comes only when the port is closed from outside,
   and it names the ports to forward and the NAS's address. A UDP port cannot be checked from outside.
4. Press **Check** to test the TCP port from the internet.

The two switches also decide how the package asks the router for TCP port 80 while it gets or renews a Let's Encrypt
certificate (**Security → Certificate & ports**): only through the methods that are on. With both off it asks the
router for nothing, and the certificate card says so. Then forward TCP port 80 to the NAS on the router yourself and
leave it there: the first certificate needs it, and so does every renewal, which starts by itself 30 days before the
certificate expires.

![How the router and a VPN forward the port](https://nasutils.net/rtorrent16/docs/img/portfwd-vpn-en.svg)

With **Pick a random port from the range** off, **Settings → Connection** shows **Listening port (1024-65535)**
with +/− controls. A stored range such as `50000-50010` stays unchanged until you edit the number; the field shows
its lower bound and the help names the retained range. Editing it to `42000` saves `42000-42000`. With random
selection on, the field accepts a range. The default is `50000-50010`, with random selection off.

At startup rtorrent tries ports within the saved range. A single-port range has no other candidate; it does not
try ports outside that range. Forward the range or the measured listening port shown by **Network & NAT**.
Saving a range applies its lower bound live when that succeeds; follow any restart notice. Changing random
selection requires a restart. When the measured port differs from the saved selection, Settings shows a red
notice with both facts; saved changes may still need a restart. This notice does not identify a bind failure.

## Keep the ports open on a UniFi gateway

Rtorrent 16 can create the port forwards on a UniFi gateway for you, through the gateway's API with an API key or a
local account. With the API key kept it also keeps them in order: when a port changes, it removes the old rules and
creates new ones.

1. In **Settings → Port forwarding & VPN**, set **Port forwarding method** (`portfwd_router`) to **UniFi gateway: rules
   through its API**. The UPnP and PCP / NAT-PMP switches, **Router address** and the VPN's port lock while it is
   chosen.
2. Under the Router fields, in **UniFi gateway**, keep **Sign in with** at **API key (recommended)**. With a key
   Rtorrent 16 keeps the rules in place and opens IPv6 too. **Create a key on the gateway** opens the gateway's own page
   for it (`https://<gateway address>/network/default/integrations`, in UniFi Network under **Settings → Control Plane
   → Integrations**); paste the key into **API key**. A **Local account** (a UniFi user name and password) works too,
   but with it only IPv4 rules are made and the package signs out when it is done.
3. Leave **Gateway address** empty to use the NAS's default gateway, or enter the gateway's address (with a port, such
   as `192.168.1.1:8443`, when the Network application uses another one).
4. Press **Connect**. Rtorrent 16 signs in and reads the gateway without changing anything (rtorrent has to run, so
   that its port is known). A window lists, per WAN, the ports and what happens to them, for example
   `6891 UDP, 33023 TCP, 42000 TCP/UDP: "Rtorrent-Pro" → Rtorrent16`, and what your own rules keep:
   **Your rule "Rtorrent-Pro" keeps 50000, 6880-6890.** Rtorrent 16 moves only its own ports out of your rule; a rule
   with nothing else left is removed. A rule of yours that sends a port to another device, to another port inside, or
   lets in only some addresses stays as it is, and the window says why.
5. With an API key, choose **Keep the key** (the package protects the rules, below) or **Delete the key when done** (the
   rules stay, but nobody watches or removes them). A local account is always signed out and its user name and
   password deleted at the end.
6. Press **Take over**. The package saves the gateway's own backup, changes the rules, sets the method to UniFi gateway
   if it was not saved yet, runs the port check of **Network & NAT** and shows in the same window what it changed
   (**Moved ports … out of your rule**, **Created 5 Rtorrent16 rules.**) and what the check found, with the cause when
   the port is closed.

What the package adds, for the ports the switches above name (the BitTorrent port over TCP and UDP, the DHT port over
UDP while DHT runs, the WebUI and phone ports when their switches are on):

- one port forward per port on every WAN of the gateway that has IPv4, named like
  `Rtorrent16 BitTorrent 51413 (WAN2)`. A WAN without IPv4 (an IPv6-only line, or IPv4 switched off on it) gets no
  forward;
- one IPv6 allow rule per protocol (`Rtorrent16 IPv6 TCP`, `Rtorrent16 IPv6 UDP`), from the internet to this NAS by its
  interface identifier, so it keeps working when the provider changes the prefix. One rule opens the ports on every
  WAN with IPv6, an IPv6-only WAN included. It needs an API key and a global IPv6 address on the NAS.

A port moves out of your rule with all its protocols: when your rule forwarded 6891 over TCP and UDP and Rtorrent 16
needs only UDP, TCP 6891 is no longer forwarded. The window names every port that moves, so you can check that nothing
else of yours uses one of them. Rules the package made under the older name "Rtorrent 16 …" get the new name at the next takeover.

Once connected, **Settings** shows one line (connected, and whether Rtorrent 16 keeps the rules in place), a link to
**Network & NAT**, **Change sign-in** and **Disconnect**. The rules themselves are on **Network & NAT**, in the **UniFi
gateway** card under the BitTorrent port check: each WAN with its addresses (marked **IPv6 only** or **No IPv6** where
that is so), for each port the rule that forwards it, **No IPv4 on this WAN** with how the port is open there, or why
it is not forwarded (another rule sends the port to another device, or the gateway refused in its own words), the IPv6
rules, what the last round changed, and **Check now**. A WAN behind another router or the provider's NAT
(100.64.0.0/10) does not let peers in through a rule on its own. Without the kept sign-in the line says so and
**Protect the rules** asks for an API key and runs the same window.

### How the package watches the rules

The rules stay while rtorrent is stopped, across restarts and package updates. With the API key kept, the package
watches them the way UPnP renewals did:

- Every 5 minutes it reads the gateway's forwards and networks (and the IPv6 rules when it has some there). When one
  of its rules is missing, turned off or sends the port to another address (after the NAS's LAN address changed, for
  example), or a WAN was added or removed, it repairs the rules at once. Nothing is written to the gateway while all is
  in order. Every 30 minutes it also reads the gateway's WAN addresses. A rule that changes again after two repairs
  within an hour is left alone for an hour, and the card says so: check it in UniFi Network.
- When peers cannot get in, it checks the rules at once: after the port check (**Network & NAT** or the Dashboard)
  could not reach a forwarded TCP port from the internet over IPv4 or over IPv6 (IPv6 only while the package keeps IPv6
  rules) while rtorrent listened on it, and when no
  peer has connected in for 30 minutes while rtorrent holds 10 or more connections. The card says what it found: the
  rules it repaired, or that all were in place, which means something other than these rules blocks the port.
- After a change of port, a Save or **Check now** it runs a full round at once.

The **UniFi gateway** card on **Network & NAT** shows when the rules were last checked and what the last round changed
(**Created**, **Created again**, **Repaired**, **Removed**, **Moved ports … out of your rule**, **Renamed**). A gateway that no longer accepts the key or the password, or shows another certificate,
pauses the checks until you press **Connect** again; with a local account the package keeps the gateway's session, so
the checks do not sign in every 5 minutes.

Before its first change after you connect, the package saves the gateway's own backup in the package's
`etc/router/backups/` folder. With the API key kept, it removes the rules it added when you choose another method,
press **Disconnect**, or uninstall Rtorrent 16. Without the key it cannot: **Disconnect** then only forgets the gateway,
and the rules stay until you delete them in UniFi Network. Rules others made change only through a takeover you
confirmed. A kept key stays on the NAS (`etc/router/unifi.json`, readable by the administrator only), and the panel
never shows it again; a local account's password is kept only while the takeover runs.

### UniFi gateway or VPN

**Port forwarding method** has one value at a time. Choose **UniFi gateway** when peers reach the NAS through your
UniFi gateway. Choose **VPN: the port from your VPN provider** when rtorrent's traffic goes through a VPN on the NAS
with port forwarding (ProtonVPN gives the port through NAT-PMP in the tunnel; other providers give a port you enter
in **Port your VPN gave you**). With **VPN** the gateway's rules are removed; with **UniFi gateway** the VPN's port is
off. A settings file that had both from an earlier version keeps UniFi, and the UniFi gateway card on **Network & NAT**
says that the VPN's port is off.


### QuFirewall on the NAS

QuFirewall, QNAP's firewall app, filters new connections on the NAS itself, after the router. Rtorrent 16 reads its
rules (it never changes them) and says, for every port it forwards, whether new connections from the internet pass:
from all regions, only from some ("QuFirewall on this NAS lets new connections to port 42000 in only from Poland."),
or not at all. Port check services and peers in other countries cannot connect through a rule that lets in only one
country. The line shows under **Port forwarding & VPN** whatever the method, on **Network & NAT** under the BitTorrent
port check, and in the UniFi takeover's result, with the one fix and the exact ports: **In QuFirewall, allow these
ports from all regions: TCP 42000, UDP 42000, UDP 6891, TCP 33023.** In QuFirewall's active profile, add a rule that
allows these ports from any address, then run the port check again. The line is not shown while QuFirewall is off or
not installed.

## Find a second router or the carrier's NAT

*Figure: The Route to the internet card on the Network & NAT page*

1. Read the verdict at the top of **Route to the internet**. The page compares the IPv4 address your router
   reports with the one a public service sees, and reads the first hops on the way out.
2. Find on the drawing the node that stops peers: a second router, or your ISP's NAT.
3. Note **Router's WAN address**: the WAN address of your router, and the address to forward to on an outer
   router.

The two verdicts that mean a forward on your router alone cannot open the port:

- **Possible multiple routers** (double NAT): "The router's WAN address is private, and the route out passes another private address: a second router may sit between yours and the internet and need the forward too."
  Forward the port on the outer router to the inner router's WAN address, then on the inner router to the NAS, the
  DHT UDP port too. Or put the outer device in bridge mode when its WAN address is public, or connect the NAS to the
  outer router.
- **Another NAT likely before your router (IPv4)** (carrier-grade NAT): "The router's WAN address is in 100.64.0.0/10 (carrier-grade NAT): a forward on this router cannot open the port."
  Ask your ISP for a public IPv4 address (it need not be static), or use a VPN with port forwarding with rtorrent
  bound to the tunnel (**Settings → Port forwarding & VPN**, **Port forwarding method** set to **VPN**). A global IPv6
  address lets IPv6 peers in either way.

**Another NAT likely before your router (IPv4)** also appears when an address on the route out is in 100.64.0.0/10, or when the
router's WAN address differs from the public IP, as it does behind a VPN or a proxy. The verdict shows where an extra
NAT may sit; only the port check above shows whether the port is open.

Only an address between your router and the first public hop can belong to a router in front of yours. A private
address after a public hop, or one in your own network's range, is a router inside your ISP. The hop list marks it
**ISP network**, and the page asks for no forward there. When the port check finds the port open, the verdict reads
**Peers can reach this NAS** and the drawing shows the route as working, a router in front included. When your router
reports no WAN address and a private address answers before the first public hop, the drawing shows **Possible second
router**. Forward the port there only if the port check says closed and the device is yours.

myQNAPcloud Link opens the panel from outside without a forward when the QTS panel route is on (**Settings → Web
access**). It does not open the BitTorrent port.

## Let peers reach you over IPv6

*Figure: The IPv6 block of the BitTorrent port card*

1. Check **Address**: the stable global IPv6 address of the NAS. A unique-local address (`fd…`) does not count:
   the internet cannot route to it.
2. Check **Announced to trackers**: the address trackers hand to IPv6 peers. With **IPv6** on Auto
   (**Settings → Connection**) the package announces the stable global address by itself, or the one typed under
   **Announce this IPv6 to trackers**. It never announces a temporary (privacy) address, which changes every few
   hours.
3. Read **Internet over**: whether rtorrent reaches the internet over IPv4, IPv6 or both, from the interface or
   address in **Bind to interface / IP**. When the
   **IPv6** setting does not fit what the check found, a button under it offers the fitting setting; pressing it
   saves the setting and restarts rtorrent. Nothing changes until you press it.
4. Allow inbound TCP and UDP on the BitTorrent port, and UDP on the DHT port, to this address in the router's IPv6
   firewall and in QuFirewall. There is no NAT to forward on IPv6, and most routers drop inbound IPv6 by default.

The card's steps 5 and 6 check the IPv6 socket and ask port.tools (and, without a verdict, ifconfig.co) to connect
to the address over IPv6. The request carries the address and the port, no torrent data. When the checker sees another
source address, the step names it.

![Addresses announced to trackers](https://nasutils.net/rtorrent16/docs/img/connection-announce-en.svg)

## Details

### The ports

| What | Port |
|---|---|
| Control Center and ruTorrent, http | 6019 |
| The same over https (the package's own certificate) | 6018 |
| BitTorrent, TCP and UDP | one port of `port_range`, 50000-50010 by default |
| DHT, UDP | 6891; when another program holds it, the package moves it and forwards the new one |

The Dashboard's **BitTorrent port** card lists what the router mapped at start under **Forwarded**.

When another program holds the saved BitTorrent port at start (every port of the range, when it is a range), rtorrent
takes the nearest free port of the 20 above it for that start, and the router and the trackers get that port. Ports the
package uses itself (DHT, the panel, the phone apps) and Download Station's range are skipped. The saved setting does not
change: the next start tries it again. The Dashboard then shows a red badge, such as **Port 50000 was busy · running on
50001**, with the program that holds the port in its tooltip and under the BitTorrent port card; the Network card shows
**50001 (moved from 50000)**, and Settings shows the same red notice beside the listening port. With no free port in
those 20, the badge says that rtorrent cannot listen until the port is free.

### Firewalls

For incoming peers allow TCP and UDP on the BitTorrent port, and UDP on the DHT port when it differs. The public
IP lookup and the port check need outbound HTTPS (TCP 443) from the NAS. On pfSense or OPNsense add the port
forwards and the WAN rules; with QuFirewall on, its active profile needs the same allow rules.

A failed external check can come from a missing forward, a firewall rule, a stopped rtorrent, or another address or
port. The NAT verdict alone cannot tell them apart. The **Router loopback** step connects from the NAS to your own
public address. Many routers do not pass such a connection back in (NAT loopback), so a failure there proves nothing; a
pass proves only the path through your own router.

### Jumbo frames

An MTU above 1500 on the NAS interface loses incoming connections quietly: the internet carries 1500, a larger
frame is dropped by the first hop that does not take it, and from outside it looks like a closed port. When the
interface of the default route has an MTU above 1500, the Route to the internet card shows a warning with the
interface and the value. The MTU is in QTS under **Network & Virtual Switch → Interfaces** (a NAS with a virtual switch names the
interface `qvs0`). Keep 1500 for BitTorrent; jumbo frames help only traffic that stays on your LAN.

### IPv6 and DHT

rtorrent listens on one socket for both families, so an IPv6 peer comes in on the same port. DHT keeps an IPv4 and an
IPv6 table on the same UDP port, so a torrent without a tracker finds IPv6 peers too. With **IPv6** off, or with
rtorrent bound to an interface without an IPv6 address (most VPN tunnels), the IPv6 table does not start and no peer
is reached over IPv6.

**Settings → Connection → DHT** has three modes. With **On**, the default, the node starts with rtorrent, and the
watchdog starts it again when rtorrent drops it. With **Off**, the node stops, and you can switch back to **On** while
rtorrent runs. With **Disabled**, the node stops too, and switching it back on takes a restart of rtorrent. rtorrent
never announces a torrent with the private flag to DHT, in any mode. The **DHT off** and **DHT on** buttons on the
**Dashboard** change this same setting.

The node works while no torrent runs: it keeps its table of other nodes fresh and answers their questions about peers.
A router's flow log counts each exchange with another node as a flow, so it lists many short UDP flows on the DHT port,
to and from addresses all over the world. None of them is a download. With **Off**, rtorrent stops asking and
answering, and the package stops asking the router to forward the DHT port, so other nodes' questions no longer reach
the NAS. A DHT port forwarded by hand on the router lets them in until that rule is removed. What Off costs: a magnet
without a tracker gets no metadata, and a public torrent finds fewer peers. Private torrents do not use DHT.

![Finding peers: DHT, PEX and tracker lists](https://nasutils.net/rtorrent16/docs/img/dht-pex-en.svg)

### Encryption and proxies

**Protocol encryption** (`encryption`) prefers encrypted peer connections and falls back to plain ones by default.
Requiring encryption cuts the number of peers, and encryption does not hide your address. A global proxy
(`proxy_global`) carries the peer connections; UDP trackers, DHT and incoming connections stop while it is set.

![Peer encryption and proxies](https://nasutils.net/rtorrent16/docs/img/encryption-proxy-en.svg)

### Blocklist

**Settings → Blocklist** blocks the IPv4 addresses of a list (`ipfilter_enable`, `ipfilter_url`) and refreshes it
daily. IPv6 peers pass. After you turn it off, restart rtorrent to clear the loaded list.

The list can be a P2P list (`name:1.2.3.0-1.2.3.255`, the `.dat` form too) or a list of IPv4 CIDR blocks
(`1.2.3.0/24`), packed with gzip or not. Each CIDR block is saved as the range it covers. A file with neither, such as
a web page served in place of the list, is refused, and the previous list stays.

![Updating the IPv4 blocklist](https://nasutils.net/rtorrent16/docs/img/blocklist-en.svg)
