Summary

  • RFC 8489, February 2020, defines STUN as a tool for other protocols: an endpoint uses it to learn "the IP address and port allocated to it by a NAT", to run connectivity checks, and to keep bindings alive.
  • The memo is blunt about its own limits: STUN is "not a NAT traversal solution by itself", and it "works with many existing NATs and does not require any special behavior from them".

The cheapest way to learn your own address

Your laptop is certain it lives at 10.0.0.5. The internet has never heard of 10.0.0.5. Somewhere in between, a NAT rewrote the address and kept the receipt. The world sees 198.51.100.7 and a port number nobody thought to mention to you.

Everything that wants to receive a connection — a call, a game, a peer — has to learn that second number. The host cannot look it up, because the only copy sits inside the box that did the rewriting, and that box does not answer questions.

STUN's answer is to ask anyway. The endpoint fires a request at a server somewhere on the internet; the server replies with the source address and port it just observed. That reply is the only evidence the endpoint will ever get about its outer self, and it is evidence from a stranger.

The protocol is small on purpose: an extensible packet format, a few transports, two authentication mechanisms. Small enough to be implemented by people who have other things to do.

A tool that refuses to be a solution

Most protocol documents would blur the distinction. This one spends its abstract sharpening it. STUN is "not a NAT traversal solution by itself. Rather, it is a tool to be used in the context of a NAT traversal solution".

The solutions are called STUN Usages, and each one says what STUN is being used for. One usage runs connectivity checks between two endpoints; another relays packets through a server. STUN itself answers the question and gets out of the way.

That framing is a design decision with consequences. A toolkit can be picked up by protocols that did not exist when it was written — and it also means nobody can complain that STUN failed to solve their problem. Solving was never the job.

It is a rare document that argues for its own narrowness in the abstract and then delivers on the promise for two decades. The restraint is the feature.

The cookie in the transaction ID

Every STUN message carries a Magic Cookie of 0x2112A442 in a fixed position in its header. The explanation is the interesting part.

In the earlier version of the protocol, those 32 bits were part of the transaction ID, chosen at random. Parking a constant there does two things: a server can tell whether a client understands the attributes added later, because a client speaking the older format will not put the constant in the right place; and STUN packets can be told apart from other protocols multiplexed on the same port.

So a field that once carried randomness now carries a version number and a demultiplexing key. Neat trick. Also a small warning: a field's meaning can be repurposed whenever the new meaning is worth more than the old freedom.

Why the answer is only half the truth

STUN works with many NATs and asks nothing special of them, which is exactly why it deployed. The same flexibility means the answer is a fact about one moment: the binding this NAT created for this flow, from this server, at this time.

Ask a second server and you may get a different mapping, because plenty of NATs hand out a different port per destination. That is why the protocol is a tool rather than a promise, and why the connectivity checks built on top of it exist: knowing your address is only useful if you can also find out whether the other side can reach it.

The memo mentions one more use in passing — keep-alives to hold the binding open — and that is the unglamorous half of NAT traversal. A mapping that expires is a mapping that must be refreshed, and a protocol that can ask about its address can also keep the answer alive.

Which is the whole trick, really. Not knowing who you are, but paying a stranger just enough to keep being told.