Skip to content

feat: allow listen on ANY port - #1203

Open
flaneur2020 wants to merge 8 commits into
smoltcp-rs:mainfrom
flaneur2020:feat/allow-listen-any
Open

flaneur2020 wants to merge 8 commits into
smoltcp-rs:mainfrom
flaneur2020:feat/allow-listen-any

Conversation

@flaneur2020

Copy link
Copy Markdown

I've been trying to build something like tun2socks (which based on gVisor's network stack) in Rust using smoltcp to route TUN traffic through a SOCKS proxy.

However, I've run into a limitation: smoltcp does not yet support listening on any ports. In the context of a TUN device, we really need to be able to catch traffic on any port to forward it to the proxy.

I found @chayleaf's PR #814 which was tackling this, but it hasn't been merged yet. I decide to build on top of the code from PR #814 and incorporated the comment from @raftario.

I think this functionality would be super useful for anyone building VPN-like tools. I've passed the tests in my tun2socks impl with this branch here: https://github.com/flaneur2020/smoltcp-socks/blob/master/src/netstack/actor.rs#L288

Any feedback would be appreciated!

Adapt UDP sockets to optional listen ports while preserving the unbound default. Carry local ports in packet metadata, validate outgoing source ports, and cover wildcard receive/reply flows.
@codecov

codecov Bot commented Sep 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.64045% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.67%. Comparing base (efcba2e) to head (0e05e3b).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
src/socket/udp.rs 90.80% 8 Missing ⚠️
src/wire/ip.rs 66.66% 8 Missing ⚠️
src/socket/icmp.rs 50.00% 3 Missing ⚠️
src/wire/ieee802154.rs 25.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1203      +/-   ##
==========================================
+ Coverage   82.57%   82.67%   +0.10%     
==========================================
  Files          83       83              
  Lines       26174    26321     +147     
==========================================
+ Hits        21613    21761     +148     
+ Misses       4561     4560       -1     

☔ 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.

@whitequark

Copy link
Copy Markdown
Contributor

I think it's a great feature! I'll give it a closer look during the workweek.

Comment thread src/wire/ip.rs Outdated
impl defmt::Format for ListenEndpoint {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "{:?}:{=u16}", self.addr, self.port);
defmt::write!(f, "{:?}:{:?}", self.addr, self.port);

@sigvartmh sigvartmh Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This will print Some(<num>) in defmt won't it?
It's not unreadable it will just be a bit different than before. So <num>.<num>.<num>.<num>:Some(<num>) now I could be wrong so feel free to ignore it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

yeah, it really doesn't look great. i think it would look better if it followed the same format as the Display above. i've fixed it in 0e05e3b, PTAL. thanks for pointing this out!

(though I suspect it was originally showing up as Some(<num>.<num>.<num>.<num>):<num>, since self.addr is also an Option 😲...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants