Getting Through NAT: Hole Punching, STUN, TURN, ICE, UPnP and PCP
How two hosts behind NATs manage to talk directly: pinholes and UDP hole punching measured in a real lab, STUN and its XOR-MAPPED-ADDRESS, NAT behavior discovery, TURN relays, ICE candidate gathering and checks, and asking the NAT directly with UPnP, NAT-PMP and PCP.
kvi5hnuCode Sprout team member21 min readOn this page
Article 35 showed that a NAT creates mappings only for traffic that starts inside. That's fine for browsing, where the client always makes the first move. It's a real problem for applications where either side may need to reach the other: voice and video calls, online games, file sharing, remote desktop and peer-to-peer VPNs (virtual private networks). If both peers are behind NATs, neither can simply connect to the other, because neither has an address the other can reach.
There are two broad ways around this:
- Work around the NAT from the outside. The application discovers what mapping the NAT gave it and arranges for traffic to arrive in a way the NAT will accept. STUN, TURN and ICE, each explained below, do this.
- Ask the NAT to open a port. UPnP, NAT-PMP and PCP (covered at the end of the article) do this, when the NAT supports them.
Pinholes and hole punching
When a NAT creates a mapping for an outgoing flow, it also lets matching replies back in. That narrow, temporary opening is called a pinhole. Hole punching (RFC 5128) uses pinholes deliberately. Two hosts each send packets to the other's public address at about the same time, so each NAT opens a pinhole that the other host's packets then fit through.
But first, each host must learn the other's public address and port, which it can't see from inside its NAT. So hole punching needs a third party: a rendezvous server with a public address that both hosts can reach. The diagram shows the whole procedure in five steps, and the lab below runs it.

The lab builds this exactly: hostA (10.0.1.10) behind NAT A (203.0.113.1), hostB (10.0.2.10) behind NAT B (203.0.113.2), both Linux NATs with the behavior measured in article 35, and a small rendezvous program on 198.51.100.10. Both hosts use local UDP port 7000. The rendezvous server's view:
rendezvous: hostA registered, seen as 203.0.113.1:7000
rendezvous: hostB registered, seen as 203.0.113.2:7000
rendezvous: told hostA about 203.0.113.2:7000 and hostB about 203.0.113.1:7000
Then each host sends a datagram to the other's public endpoint every half second for 3 seconds. Each prints the first two datagrams it receives:
hostA: peer is at 203.0.113.2:7000
hostA: t=0.0s received 'hostB packet 1' from 203.0.113.2:7000
hostA: t=0.5s received 'hostB packet 2' from 203.0.113.2:7000
hostA: sent 6 packets, received 6
hostB: peer is at 203.0.113.1:7000
hostB: t=0.0s received 'hostA packet 1' from 203.0.113.1:7000
hostB: t=0.5s received 'hostA packet 2' from 203.0.113.1:7000
hostB: sent 6 packets, received 6
Every datagram got through, in both directions, with no relay. Notice what each host sees as the source: the other NAT's public address. Neither host ever learns the other's private address, and doesn't need to.
When hole punching fails
Hole punching relies on the timing and on how the NATs behave. In a second run, everything is the same except that hostB waits 1.5 seconds before it starts sending:
hostA: peer is at 203.0.113.2:7100
hostA: sent 6 packets, received 0
hostB: peer is at 203.0.113.1:7100
hostB: sent 6 packets, received 0
This time nothing got through at all, and NAT B's connection-tracking table shows why:
udp 17 src=10.0.2.10 dst=198.51.100.10 sport=7100 dport=3478 src=198.51.100.10 dst=203.0.113.2 sport=3478 dport=7100
udp 17 src=10.0.2.10 dst=203.0.113.1 sport=7100 dport=7100 [UNREPLIED] src=203.0.113.1 dst=203.0.113.2 sport=7100 dport=62965
udp 17 src=203.0.113.1 dst=203.0.113.2 sport=7100 dport=7100 [UNREPLIED] src=203.0.113.2 dst=203.0.113.1 sport=7100 dport=7100
There are three entries, and the order conntrack lists them in doesn't matter. In the order things happened:
- hostB's registration with the rendezvous server, mapped to
203.0.113.2:7100as expected. - hostA's early datagrams, which arrived at NAT B before hostB had sent anything to hostA. NAT B had no pinhole for them, so it dropped them. But it still recorded them as a new flow, from
203.0.113.1:7100to its own address203.0.113.2:7100. - When hostB finally sent to
203.0.113.1:7100, its translated packet would have looked exactly like the reply to that stray flow. To keep the two apart, NAT B gave hostB's packet a different public port (62965in this run). hostA's packets keep arriving at port 7100, which leads nowhere, and hostB's packets now come from a port hostA doesn't expect.
So a NAT that normally has endpoint-independent mapping can break it under exactly the conditions hole punching creates. Real applications handle this by starting both sides as close together as possible, retrying, trying several ports, and, when all else fails, using a relay. That's what ICE, later in this article, automates.
Hole punching for TCP
TCP can be hole-punched too, but it's harder. Both hosts send a SYN to each other at the same time, a simultaneous open that TCP allows (article 59). Each SYN opens a pinhole in its sender's NAT, and the two SYNs cross. It needs the NATs to handle this unusual case correctly, which RFC 5382 requires, and the timing is tighter than for UDP. That's one reason real-time applications prefer UDP.
UNSAF: fixing your own address
Learning your public address and port from an outside server, as the hosts did with the rendezvous server, is called UNilateral Self-Address Fixing (UNSAF). "Unilateral" means the NAT isn't involved: the application guesses what the NAT is doing by watching the results.
The IAB, which gives architectural advice to the IETF, considers this fragile and wrote down its concerns in RFC 3424:
- The answer depends on which server you ask. With several layers of NAT, a server inside your ISP's (internet service provider's) network may see a different address than one on the Internet.
- NAT behavior varies by vendor and can change over time, for example when mappings expire.
- It's a workaround, not a fix.
Any UNSAF protocol is therefore expected to state its limited scope, an exit strategy (usually "until IPv6 removes the need"), what makes it brittle, and what a long-term solution would need. In practice, UNSAF techniques are everywhere, because NATs are everywhere.
STUN: what's my public address?
Session Traversal Utilities for NAT (STUN), currently RFC 8489, is the standard UNSAF tool. Its main job is simple. A client sends a Binding request to a STUN server, and the server replies with the source address and port it saw, which is the client's public mapping. The answer is called the server-reflexive address.
STUN usually runs over UDP port 3478. It can also run over TCP on port 3478, or over TLS or DTLS on port 5349. Every message starts with a 20-byte header:

- The first two bits are always zero. That lets STUN share a port with other protocols, such as media streams, and be told apart from them.
- The message type combines a method (such as Binding) and a class: request, success response, error response, or indication (a message that gets no reply).
- The message length counts the bytes after the 20-byte header.
- The magic cookie is always
0x2112A442. It also identifies STUN packets, and was added when STUN was revised in RFC 5389 (2008, the predecessor of RFC 8489) to tell it apart from the "classic STUN" of RFC 3489. - The transaction ID is random. It matches a response to its request.
Attributes follow the header, each a type-length-value item padded to a multiple of 4 bytes. Attribute types below 0x8000 are comprehension-required: a receiver that doesn't understand one must reject the message. Types 0x8000 and above are comprehension-optional and may be ignored.
Here is a real exchange from the lab, run by coturn's turnutils_natdiscovery tool on hostA (10.0.0.10) behind the single NAT from article 35, against a coturn STUN server listening on two addresses, 198.51.100.10 and 198.51.100.11. The packet summary, captured on hostA:
1 0.000000 10.0.0.10 → 198.51.100.10 STUN 62 Binding Request
2 0.001568 198.51.100.10 → 10.0.0.10 STUN 134 Binding Success Response XOR-MAPPED-ADDRESS: 203.0.113.1:36075 MAPPED-ADDRESS: 203.0.113.1:36075 RESPONSE-ORIGIN: 198.51.100.10:3478 OTHER-ADDRESS: 198.51.100.11:3
3 0.002120 10.0.0.10 → 198.51.100.11 STUN 62 Binding Request
4 0.002599 198.51.100.11 → 10.0.0.10 STUN 134 Binding Success Response XOR-MAPPED-ADDRESS: 203.0.113.1:36075 MAPPED-ADDRESS: 203.0.113.1:36075 RESPONSE-ORIGIN: 198.51.100.11:3478 OTHER-ADDRESS: 198.51.100.11:3
5 0.002675 10.0.0.10 → 198.51.100.10 STUN 62 Binding Request
6 0.003115 198.51.100.10 → 10.0.0.10 STUN 134 Binding Success Response XOR-MAPPED-ADDRESS: 203.0.113.1:34607 MAPPED-ADDRESS: 203.0.113.1:34607 RESPONSE-ORIGIN: 198.51.100.10:3478 OTHER-ADDRESS: 198.51.100.11:3
7 0.003171 10.0.0.10 → 198.51.100.10 STUN 70 Binding Request, Change IP and Port
8 3.008541 10.0.0.10 → 198.51.100.10 STUN 70 Binding Request, Change Port
The decoded STUN part of the second packet, the server's Binding Success Response:
Session Traversal Utilities for NAT
Message Type: 0x0101 (Binding Success Response)
.... ...1 ...0 .... = Message Class: 0x10 Success Response (2)
..00 000. 000. 0001 = Message Method: 0x0001 Binding (0x001)
..0. .... .... .... = Message Method Assignment: IETF Review (0x0)
Message Length: 72
Message Cookie: 2112a442
Message Transaction ID: 0ec14a7e3a04a616f59fba0a
Attributes
XOR-MAPPED-ADDRESS: 203.0.113.1:36075
Attribute Type: XOR-MAPPED-ADDRESS
0... .... .... .... = Attribute Type Comprehension: Required (0x0)
.0.. .... .... .... = Attribute Type Assignment: IETF Review (0x0)
Attribute Length: 8
Reserved: 00
Protocol Family: IPv4 (0x01)
Port (XOR-d): adf9
IP (XOR-d): ea12d543
MAPPED-ADDRESS: 203.0.113.1:36075
Attribute Type: MAPPED-ADDRESS
0... .... .... .... = Attribute Type Comprehension: Required (0x0)
.0.. .... .... .... = Attribute Type Assignment: IETF Review (0x0)
Attribute Length: 8
Reserved: 00
Protocol Family: IPv4 (0x01)
Port: 36075
IP: 203.0.113.1
RESPONSE-ORIGIN: 198.51.100.10:3478
Attribute Type: RESPONSE-ORIGIN
1... .... .... .... = Attribute Type Comprehension: Optional (0x1)
.0.. .... .... .... = Attribute Type Assignment: IETF Review (0x0)
Attribute Length: 8
Reserved: 00
Protocol Family: IPv4 (0x01)
Port: 3478
IP: 198.51.100.10
OTHER-ADDRESS: 198.51.100.11:3479
Attribute Type: OTHER-ADDRESS
1... .... .... .... = Attribute Type Comprehension: Optional (0x1)
.0.. .... .... .... = Attribute Type Assignment: IETF Review (0x0)
The XOR-MAPPED-ADDRESS attribute carries the answer, but not in plain form. The port is XORed with the top 16 bits of the magic cookie (0x2112), and an IPv4 address with the whole cookie (0x2112A442). In this run, the mapped port 36075 is 0x8ceb, and 0x8ceb XOR 0x2112 = 0xadf9, which is the "Port (XOR-d)" value shown. You can XOR one hex digit at a time: 8 XOR 2 = a (1000 XOR 0010 = 1010), c XOR 1 = d, e XOR 1 = f and b XOR 2 = 9. The address 203.0.113.1 is cb 00 71 01, and XORed with 21 12 a4 42 gives ea 12 d5 43. The point of the XOR is to defeat ALGs (application-level gateways, article 35): some NATs "helpfully" rewrite any copy of their public address they find in a packet's payload, which would corrupt the plain MAPPED-ADDRESS attribute (kept only for compatibility with old clients). You can see the encoded bytes in the raw packet:
0000 02 00 00 00 0a 0a 02 00 00 00 0a f0 08 00 45 00 ..............E.
0010 00 78 9e 18 40 00 3e 11 6a 15 c6 33 64 0a 0a 00 .x..@.>.j..3d...
0020 00 0a 0d 96 8c eb 00 64 34 bd 01 01 00 48 21 12 .......d4....H!.
0030 a4 42 0e c1 4a 7e 3a 04 a6 16 f5 9f ba 0a 00 20 .B..J~:........
0040 00 08 00 01 ad f9 ea 12 d5 43 00 01 00 08 00 01 .........C......
0050 8c eb cb 00 71 01 80 2b 00 08 00 01 0d 96 c6 33 ....q..+.......3
0060 64 0a 80 2c 00 08 00 01 0d 97 c6 33 64 0b 80 22 d..,.......3d.."
0070 00 14 43 6f 74 75 72 6e 2d 34 2e 36 2e 31 20 27 ..Coturn-4.6.1 '
0080 47 6f 72 73 74 27 Gorst'
Look for 21 12 a4 42 (the magic cookie) on the line starting 0020, then 00 20 00 08 (attribute type 0x0020, length 8) followed by 00 01 ad f9 ea 12 d5 43 on the next line.
NAT behavior discovery
With a server that has two addresses, a client can also test how its NAT behaves, using the extensions in RFC 5780. The server tells the client its alternate address in the OTHER-ADDRESS attribute, and the client can ask the server to answer from a different address or port (CHANGE-REQUEST). The tool's full report:
-= Mapping Behavior Discovery =-
========================================
RFC 5780 response 1
No ALG: Mapped == XOR-Mapped
0: : IPv4. Response origin: : 198.51.100.10:3478
0: : IPv4. Response origin: : 198.51.100.10:3478
0: : IPv4. Other addr: : 198.51.100.11:3479
0: : IPv4. UDP reflexive addr: 203.0.113.1:36075
0: : IPv4. Local addr: : 0.0.0.0:36075
========================================
RFC 5780 response 2
No ALG: Mapped == XOR-Mapped
0: : IPv4. Response origin: : 198.51.100.11:3478
0: : IPv4. Other addr: : 198.51.100.11:3479
0: : IPv4. UDP reflexive addr: 203.0.113.1:36075
0: : IPv4. Local addr: : 0.0.0.0:36075
========================================
NAT with Endpoint Independent Mapping!
========================================
-= Filtering Behavior Discovery =-
========================================
RFC 5780 response 3
No ALG: Mapped == XOR-Mapped
0: : IPv4. Response origin: : 198.51.100.10:3478
0: : IPv4. Other addr: : 198.51.100.11:3479
0: : IPv4. UDP reflexive addr: 203.0.113.1:34607
0: : IPv4. Local addr: : 0.0.0.0:34607
STUN receive timeout..
STUN receive timeout..
========================================
NAT with Address and Port Dependent Filtering!
========================================
It reached the same conclusion as article 35's hand-made tests:
- Mapping: the same reflexive address (
203.0.113.1with the same port) for two different server addresses, so the NAT has endpoint-independent mapping. - Filtering: replies sent from a different address and port ("Change IP and Port") and from a different port ("Change Port") never arrived, so the NAT has address and port dependent filtering.
RFC 5780 is experimental, and its results describe the NAT only at that moment. Applications don't rely on it to decide what to do. They use ICE, which simply tests what works.
TURN: when direct paths fail
Hole punching can't always work. If either NAT has address-dependent or address and port dependent mapping (the old "symmetric NAT"), or a firewall blocks UDP entirely, no trick gets a direct path. Traversal Using Relays around NAT (TURN), RFC 8656, provides a fallback: a relay server on the public Internet that forwards traffic between the peers.
TURN is an extension of STUN, using the same message format and port. The client asks the server for an allocation: a public relayed address on the server, on which the server will receive traffic for the client and forward it. Because a relay costs bandwidth, TURN servers require authentication. Here's the start of a real allocation from the lab, using coturn's test client, user alice:
1 0.000000 10.0.0.10 → 198.51.100.10 STUN 94 Allocate Request UDP lifetime: 777
2 0.005303 198.51.100.10 → 10.0.0.10 STUN 150 Allocate Error Response error-code: 401 (Unauthenticated) Unauthorized with nonce realm: example.com
3 0.005799 10.0.0.10 → 198.51.100.10 STUN 166 Allocate Request UDP lifetime: 777 user: alice with nonce realm: example.com
4 0.010934 198.51.100.10 → 10.0.0.10 STUN 162 Allocate Success Response XOR-RELAYED-ADDRESS: 198.51.100.10:49194 XOR-MAPPED-ADDRESS: 203.0.113.1:37489 lifetime: 777
5 0.011090 10.0.0.10 → 198.51.100.10 STUN 150 Refresh Request lifetime: 777 user: alice with nonce realm: example.com
6 0.011241 198.51.100.10 → 10.0.0.10 STUN 126 Refresh Success Response lifetime: 777
- The first Allocate request has no credentials, so the server answers with error 401 Unauthenticated, including a realm and a nonce (a one-time value).
- The client repeats the request with its username and a message-integrity value computed from its password, the realm and the nonce.
- The success response gives two addresses. XOR-RELAYED-ADDRESS is the relay address on the server, for the peer to send to. XOR-MAPPED-ADDRESS is the client's own public mapping, so a TURN server also does STUN's job.
- Allocations expire. The client sends Refresh requests to keep them, and the default lifetime is 10 minutes.
Before relaying, the client must also say which peers may send to its relay address, with CreatePermission (a permission lasts 5 minutes unless refreshed). This keeps the relay from becoming an open door. Data then flows in one of two ways:
- Send and Data indications, STUN messages that carry the peer's address and the data. This is simple, but it adds about 36 bytes per packet.
- Channels. The client binds a peer to a 16-bit channel number with ChannelBind, and afterward sends data with just a 4-byte header. RFC 8656 limits channel numbers to
0x4000–0x4FFF, so the first byte of a channel message is always 64 to 79, which distinguishes it from STUN and from media packets sharing the port.
Later in the same lab capture, the test client binds channels to its peers:
31 0.023390 10.0.0.10 → 198.51.100.10 STUN 162 Channel-Bind Request ChannelNumber=0x496c XOR-PEER-ADDRESS: 198.51.100.10:49199 user: alice with nonce realm: example.com
32 0.023540 198.51.100.10 → 10.0.0.10 STUN 118 Channel-Bind Success Response
33 0.023614 10.0.0.10 → 198.51.100.10 STUN 162 Channel-Bind Request ChannelNumber=0x4200 XOR-PEER-ADDRESS: 198.51.100.10:49184 user: alice with nonce realm: example.com
34 0.023726 198.51.100.10 → 10.0.0.10 STUN 118 Channel-Bind Success Response
35 0.023784 10.0.0.10 → 198.51.100.10 STUN 162 Channel-Bind Request ChannelNumber=0x4fa8 XOR-PEER-ADDRESS: 198.51.100.10:49195 user: alice with nonce realm: example.com
36 0.023908 198.51.100.10 → 10.0.0.10 STUN 118 Channel-Bind Success Response
TURN always works when UDP or TCP to the server works, which is why every serious real-time application has a TURN server available. The cost is the relay: every packet crosses the server, adding delay and using its bandwidth. So TURN is the last resort.
ICE: try everything, use the best
Interactive Connectivity Establishment (ICE), RFC 8445, is the procedure that ties these pieces together. WebRTC (real-time communication in browsers), most VoIP (voice over IP) systems, and many games and VPNs use it. Each peer:
- Gathers candidates, which are addresses it might be reachable at:
- host candidates: its own interface addresses;
- server-reflexive candidates: its public mapping, learned with STUN;
- relayed candidates: an allocation on a TURN server.
- Exchanges candidates with the other peer through a signaling channel, which is whatever the application already uses, such as a web server. With trickle ICE (RFC 8838), candidates are sent as soon as each is found, instead of waiting for all of them, which speeds up call setup.
- Forms pairs of local and remote candidates and checks them, by sending STUN Binding requests directly over each pair. The checks are hole punching: both sides send at once, and the first packets open pinholes for the replies. A check may reveal a new address, called a peer-reflexive candidate.
- Picks the best pair that works. Each candidate gets a priority, and the recommended type preferences rank host candidates highest (126), then peer-reflexive (110), server-reflexive (100), and relayed lowest (0). The controlling side nominates the pair to use.
The diagram shows where each type of candidate comes from.

ICE keeps working after setup: consent freshness checks (repeated STUN requests) make sure the peer still wants the traffic, and the checks double as keepalives for the NAT mappings. If the network changes, for example when a phone moves from Wi-Fi to mobile data, ICE can restart and find a new pair.
Two lighter forms exist. ICE-lite is for servers with public addresses, such as conferencing servers: they only answer checks and never gather or send them. And many applications use ICE's ideas without the full protocol.
Asking the NAT directly: UPnP, NAT-PMP and PCP
All of the above works around a NAT that won't cooperate. If the NAT supports it, an application can simply ask it to create a mapping, the way a manual port forward would:
- UPnP Internet Gateway Device (IGD) is the oldest and most widely supported. The application finds the router with SSDP, the Simple Service Discovery Protocol (multicast to
239.255.255.250port 1900), then sends it SOAP requests (an XML-based request format) over HTTP such as "AddPortMapping". It has no authentication: any program on the local network can open ports, including malware. Badly implemented routers have even accepted UPnP requests from the Internet side. Many security guides recommend turning UPnP off. - NAT Port Mapping Protocol (NAT-PMP), RFC 6886, came from Apple. It's a small binary protocol over UDP port 5351, sent to the default gateway.
- Port Control Protocol (PCP), RFC 6887, is NAT-PMP's standardized successor, on the same UDP port 5351. It supports IPv6, firewalls as well as NATs, mappings with explicit lifetimes, and, importantly, carrier-grade NATs. A MAP request asks for an inbound mapping, and a PEER request asks the NAT to create or extend a mapping for a specific remote peer. With a PCP-capable CGN, a customer can get an inbound port even without a public address.
None of these work when the NAT doesn't support them or has them turned off, or through several layers of NAT unless each one cooperates. So applications use them as one more way to get a candidate, alongside STUN and TURN, not instead of them.
Common questions
Why can't the rendezvous server just forward all the traffic?
It could, and then it would be a relay, which is what TURN is. The point of hole punching is to avoid that: a direct path has lower latency and doesn't cost the server operator bandwidth. Relays are kept for the cases where nothing else works.
What fraction of connections need a TURN relay?
It depends heavily on the networks involved. Most home routers allow hole punching for UDP, so most consumer calls end up direct. Relays are needed more often on enterprise networks that block UDP or use strict NATs, and on some mobile and carrier-grade NAT networks. Operators of large video services commonly report that a minority of sessions, often in the range of 10 to 20 percent, go through a relay.
Is STUN a security risk?
STUN reveals only your public address and port to the server you ask, which anyone you connect to learns anyway. A known privacy issue is that a web page using WebRTC can learn a visitor's addresses, including the public one behind a VPN in some configurations. Browsers now hide local addresses behind random mDNS (multicast DNS, article 54) names, and let users or policies limit which candidates WebRTC uses.
Why does STUN XOR the address instead of encrypting it?
The goal isn't secrecy, it's to stop broken ALGs from rewriting the address. Some NATs scan payloads for their own public address and replace it with the private one, thinking they're fixing an embedded address. XOR with a known constant makes the bytes unrecognizable to such a NAT while costing nothing to undo.
Does IPv6 make all of this unnecessary?
Mostly, but not entirely. Without NAT, every host has a reachable address. But IPv6 hosts are usually behind stateful firewalls that block unsolicited inbound traffic, so peers still need simultaneous sending, which is hole punching through firewalls rather than NATs. ICE handles both, which is why WebRTC gathers IPv6 candidates too.
Should I turn UPnP off on my router?
If nothing on your network needs it, turning it off is safer: any device or program on your network can otherwise open ports to the Internet without asking you. Game consoles and some peer-to-peer apps work better with it, so the trade-off is convenience against control. If your router supports PCP or NAT-PMP, those are better-designed alternatives.
Why do my calls work but file transfers in the same app are slow?
A common cause is that the call fell back to a TURN relay, which may limit bandwidth per session. Small, steady audio fits easily, but a large transfer through a busy relay is slow. Tools that show WebRTC statistics (such as a browser's internal WebRTC page) show the selected candidate pair, so you can see whether it's relay or a direct type.
Summary
- A NAT lets replies in through narrow, temporary pinholes. Hole punching gets two hosts behind NATs to open pinholes for each other by sending to each other's public endpoints at about the same time, after a rendezvous server tells them those endpoints.
- The lab showed hole punching succeeding when both sides sent together, and failing when one side was late: the early packets created conntrack state on the other NAT, which then changed the late side's public port.
- Learning your public address from a server is UNSAF, which the IAB considers fragile, but it's universal.
- STUN (RFC 8489) answers "what's my public address and port?" with XOR-MAPPED-ADDRESS, XORed with the magic cookie
0x2112A442to survive ALGs. RFC 5780 extensions can probe mapping and filtering behavior. - TURN (RFC 8656) allocates a relay address on a server, with authentication, permissions, lifetimes and compact channels. It always works but costs latency and bandwidth.
- ICE (RFC 8445) gathers host, server-reflexive and relayed candidates, checks pairs with STUN, and picks the best working pair, preferring direct paths.
- UPnP IGD, NAT-PMP and PCP ask the NAT for a mapping directly. PCP is the modern standard and works with carrier-grade NAT. UPnP's lack of authentication makes it a security risk.
Practice questions
1. Two hosts behind NATs with endpoint-independent mapping and address and port dependent filtering want to talk. What must each host do, and in what order, for UDP hole punching to work?
Answer
Each host first sends to the rendezvous server, which creates its public mapping and lets the server learn that endpoint. The server tells each host the other's public endpoint. Then both hosts send to each other's public endpoint, repeatedly, at about the same time. Each host's outgoing packet opens a pinhole in its own NAT for exactly the other's endpoint, and once both pinholes exist, packets from each side are delivered.
2. A STUN response has XOR-MAPPED-ADDRESS with port bytes 0x5d14. What is the real mapped port?
Answer
XOR with 0x2112: 0x5d14 XOR 0x2112 = 0x7c06, which is 31750 in decimal. The real mapped port is 31750.
3. Why does a TURN server answer the first Allocate request with a 401 error instead of an allocation?
Answer
TURN requires authentication, because a relay uses the server's bandwidth and could be abused. The first request carries no credentials, so the server replies with 401 Unauthenticated, giving its realm and a fresh nonce. The client then resends the request with its username and a message-integrity value computed from its password, the realm and the nonce, which proves it knows the password without sending it.
4. In ICE, why are host candidates given the highest priority and relayed candidates the lowest?
Answer
A pair of host candidates means the peers can reach each other directly on the same network, which is the fastest path with no NAT involved. Server-reflexive pairs are direct but go through NATs. Relayed candidates send every packet through a TURN server, adding latency and costing the relay operator bandwidth, so they're used only when no better pair works.
5. In the lab's failed run, NAT B gave hostB public port 62965 instead of 7100. Why did that break communication even though both hosts kept sending?
Answer
hostA was sending to 203.0.113.2:7100, the endpoint the rendezvous server had reported, but NAT B had no mapping on port 7100 leading to hostB for that flow, so hostA's packets were dropped. hostB's packets reached NAT A from port 62965, but hostA had opened a pinhole only for 203.0.113.2:7100, so NAT A dropped those too. Neither side's packets matched the other's pinhole, so nothing got through.
6. A customer behind carrier-grade NAT wants an inbound port for a game server. Which of UPnP, NAT-PMP and PCP could help, and what must be true for it to work?
Answer
PCP, because it's designed to work with carrier-grade NATs, and can be used on a mapping that crosses the ISP's NAT. It works only if the ISP's CGN supports PCP and allows customers to create mappings, and the home router either supports PCP itself or passes requests on. UPnP and NAT-PMP talk only to the home router, which can't open a port on the CGN.
Previous: Network Address Translation: NAPT, Mapping and Filtering Behavior, Hairpinning and Carrier-Grade NAT

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.