ArticleslgStudy

computer science

TCP hole punching

TCP hole punching is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand TCP hole punching rather than just read about it. In short: TCP NAT traversal and TCP hole punching (sometimes NAT punch-through) in computer networking occurs when two hosts behind a network address translation (NAT) are trying to connect to each other with outbound TCP connections. Such a scenario is particularly important in the case of peer-to-peer communications, such as Voice-over-IP (VoIP), file sharing, teleconferencing, chat systems and similar applications.

Key takeaways

  • TCP hole punching belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect TCP hole punching to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of TCP hole punching from memory before moving on to harder problems.

Reference excerpt

TCP NAT traversal and TCP hole punching (sometimes NAT punch-through) in computer networking occurs when two hosts behind a network address translation (NAT) are trying to connect to each other with outbound TCP connections. Such a scenario is particularly important in the case of peer-to-peer communications, such as Voice-over-IP (VoIP), file sharing, teleconferencing, chat systems and similar applications. TCP hole punching is an experimentally used NAT traversal technique for establishing a TCP connection between two peers on the Internet behind NAT devices. NAT traversal is a general term for techniques that establish and maintain TCP/IP network and/or TCP connections traversing NAT gateways.

Terminology In the following, the terms host, client and peer are used almost interchangeably.

local endpoint, internal endpoint the local IP:port as seen locally by the host and the internal part of the NAT. public endpoint, external endpoint the external IP:port mapped by the NAT, as seen by the network and the external part of the NAT. remote endpoint the IP:port of the other peer as seen by the network, or the external parts of both NATs.

Description NAT traversal, through TCP hole punching, establishes bidirectional TCP connections between Internet hosts in private networks using NAT. It does not work with all types of NATs, as their behavior is not standardized. When two hosts are connecting to each other in TCP, both via outbound connections, they are in the "simultaneous TCP open" case of the TCP state machine diagram.

Network Drawing

Types of NAT The availability of TCP hole punching depends on the type of computer port allocation used by the NAT. For two peers behind a NAT to connect to each other via TCP simultaneous open, they need to know a little bit about each other. One thing that they absolutely need to know is the "location" of the other peer, or the remote endpoint. The remote endpoint is the data of the IP address and a port that the peer will connect to. So when two peers, A and B, initiate TCP connections by binding to local ports Pa and Pb, respectively, they need to know the remote endpoint port as mapped by the NAT to make the connection. When both peers are behind a NAT, how to discover the public remote endpoint of the other peer is a problem called NAT port prediction. All TCP NAT traversal and hole punching techniques have to solve the port prediction problem. A NAT port allocation can be one of the two:

predictable the gateway uses a simple algorithm to map the local port to the NAT port. Most of the time a NAT will use port preservation, which means that the local port is mapped to the same port on the NAT. non predictable the gateways use an algorithm that is either random or too impractical to predict. Depending on whether the NATs exhibit a predictable or non-predictable behavior, it will be possible or not to perform the TCP connection via a TCP simultaneous open, as shown below by the connection matrix representing the different cases and their impact on end-to-end communication:

YES: the connection will work all the time NO: the connection will almost never work

Techniques

Methods of Port Prediction (with predictable NATs) Here are some of the methods used by NATs to allow peers to perform port prediction:

The NAT assigns to sequential internal ports sequential external ports: If the remote peer has the information of one mapping, then it can guess the value of subsequent mappings. The TCP connection will happen in two steps, at first the peers make a connection to a third party and learn their mapping. For the second step, both peers can then guess what the NAT port mapping will be for all subsequent connections, which solves port prediction. This method requires making at least two consecutive connections for each peer and requires the use of a third party. This method does not work properly in case of Carrier-grade NAT with a lot of subscribers behind each IP addresses, as only a limited number of ports are available and allocating consecutive ports to the same internal host may be impractical or impossible. The NAT uses the port preservation allocation scheme: the NAT maps the source port of the internal peer to the same public port. In this case, port prediction is trivial, and the peers simply have to exchange the port to which they are bound through another communication channel (such as UDP, or DHT) before making the outbound connections of the TCP simultaneous open. This method requires only one connection per peer and does not require a third party to perform port prediction. The NAT uses "endpoint independent mapping": two successive TCP connections coming from the same internal endpoint are mapped to the same public endpoint. With this solution, the peers will first connect to a third party server that will save their port mapping value and give to both peers the port mapping value of the other peer. In a second step, both peers will reuse the same local endpoint to perform a TCP simultaneous open with each other. This unfortunately requires the use of the SO_REUSEADDR on the TCP sockets, and such use violates the TCP standard and can lead to data corruption. It should only be used if the application can protect itself against such data corruption.

Details of a typical TCP connection instantiation with TCP Hole Punching We assume here that port prediction has already taken place through one of the methods outlined above, and that each peer knows the remote peer endpoint. Both peers make a POSIX connect call to the other peer endpoint. TCP simultaneous open will happen as follows:

Peer A sends a SYN to Peer B Peer B sends a SYN to Peer A

When NAT-a receives the outgoing SYN from Peer A, it creates a mapping in its state machine. When NAT-b receives the outgoing SYN from Peer B, it creates a mapping in its state machine. Both SYN cross somewhere along the network path, then: SYN from Peer A reaches NAT-b, SYN from Peer B reaches NAT-a Depending on the timing of these events (where in the network the SYN cross), at least one of the NAT will let the incoming SYN through, and map it to the internal destination peer Upon receipt of the SYN, the peer sends a SYN+ACK back and the connection is established.

Interoperability requirements on the NAT for TCP Hole Punching

Other requirements on the NAT to comply with TCP simultaneous open For the TCP simultaneous open to work, the NAT should:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with TCP hole punching

Start with the simplest possible case. Write down what TCP hole punching claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to TCP hole punching before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about TCP hole punching ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of TCP hole punching

In research
TCP hole punching appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses TCP hole punching in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
TCP hole punching is common in secondary-school and first-year university syllabi. It links to neighbouring topics Internet security, Peer-to-peer file sharing, Transmission Control Protocol, so understanding it makes those chapters shorter.
In everyday life
Look for TCP hole punching outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “TCP hole punching” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study TCP hole punching in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what TCP hole punching means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain TCP hole punching out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is TCP hole punching in simple terms?

TCP NAT traversal and TCP hole punching (sometimes NAT punch-through) in computer networking occurs when two hosts behind a network address translation (NAT) are trying to connect to each other with outbound TCP connections. Such a scenario is particularly important in the case of peer-to-peer comm…

Why does TCP hole punching matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study TCP hole punching?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on TCP hole punching.

Tags

  • Internet security
  • Peer-to-peer file sharing
  • Transmission Control Protocol
  • VoIP software

Keep exploring