Skip to content

Refactor NAT to support TCP, fix typos#388

Merged
sirzooro merged 1 commit into
pion:mainfrom
sirzooro:tcp_nat
Jul 7, 2026
Merged

Refactor NAT to support TCP, fix typos#388
sirzooro merged 1 commit into
pion:mainfrom
sirzooro:tcp_nat

Conversation

@sirzooro

@sirzooro sirzooro commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Refactor and extend the NAT translation layer.

  • Fix typos in error variable names: errNATRequriesMappingerrNATRequiresMapping, errMismatchLengthIP (spelling), errNonUDPTranslationNotSupportederrTranslationNotSupported.
  • Extract the NAPT outbound logic into a shared translateOutboundNAPT closure (previously duplicated per-protocol).
  • Add a tcpPortCounter field.
  • Add case tcp: branches in translateOutbound and translateInbound to support TCP NAT/NAPT and 1:1 NAT.
  • Update nat_test.go to cover the new TCP NAT cases.

Split out from #375

@sirzooro sirzooro requested a review from JoTurk July 6, 2026 17:40
@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.59322% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.62%. Comparing base (a4a04ed) to head (813c2c5).

Files with missing lines Patch % Lines
vnet/nat.go 85.59% 10 Missing and 7 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #388      +/-   ##
==========================================
- Coverage   84.72%   84.62%   -0.11%     
==========================================
  Files          38       38              
  Lines        3122     3173      +51     
==========================================
+ Hits         2645     2685      +40     
- Misses        347      354       +7     
- Partials      130      134       +4     
Flag Coverage Δ
go 84.62% <85.59%> (-0.11%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sirzooro

sirzooro commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

There is a lot of whitespace changes here. In diff options (gear icon next to "Submit review" button on the top) you can enable split layout and hide whitespaces to see actual changes.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the vnet NAT translation logic to share NAPT outbound behavior between protocols and extends NAT/NAPT translation to support TCP alongside UDP, while also cleaning up several error variable typos and expanding test coverage accordingly.

Changes:

  • Renames/fixes NAT error variables and clarifies unsupported-protocol handling.
  • Extracts shared outbound NAPT mapping logic and adds a separate TCP port counter.
  • Adds TCP translation paths for outbound/inbound NAT/NAPT and updates NAT tests to run against both UDP and TCP.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
vnet/nat.go Refactors outbound NAPT mapping into a shared closure, adds TCP translation support and a TCP port counter, and renames error variables.
vnet/nat_test.go Refactors NAT tests to be protocol-parameterized (UDP/TCP) and adds TCP coverage across existing NAT behavior scenarios.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread vnet/nat.go Outdated
errNoAssociatedLocalAddress = errors.New("no associated local address")
errNoNATBindingFound = errors.New("no NAT binding found")
errHasNoPermission = errors.New("has no permission")
errNATRequiresMapping = errors.New("1:1 NAT requires more than one mapping")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@JoTurk JoTurk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would do this in multiple commits, but since it's vnet ig that's not important.

@JoTurk

JoTurk commented Jul 7, 2026

Copy link
Copy Markdown
Member

thank you.

@sirzooro sirzooro merged commit 55e2272 into pion:main Jul 7, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants