
P2P behind a firewall with .NET System.Net.Quic and hole punching
Public postsEnglish
An experiment in peer-to-peer connections behind NAT using .NET QUIC, covering socket rebinding, a presence server, a custom STUN implementation, and automatic reconnection.
Hello. This time, I’d like to share a topic that I have been exploring lately. It’s about Can the P2P connection occur behind the firewall from .NET to System.Net.Quic? ^^
It started out as a simple curiosity. Recently, .NET became populated with QUIC… It’s not TCP, but it’s based on UDP. I thought, “If this is the case, maybe we can try breaking through the difficult NAT/firewall (holpunching)?”
QUIC Why is it suitable for P2P?
QUIC is a transmission protocol that operates over UDP. It was officially introduced from .NET 7 to System.Net.Quic, and internally it uses MsQuic. Since TLS 1.3 is the default, encryption is free, and it also has fun features like connection migration.
More than anything, the UDP-based feature is important. It allows us to use a UDP hole punch that is almost impossible with TCP. This hole punch is the key to passing through P2P NAT.
But... there is a trap in System.Net.Quic
First, I looked for verified cases. Places like go-libp2p or quic-go(Go) already used QUIC as a shortcut in production. The secret was using One UDP socket is shared by all STUN, pinching, and QUIC codes..
The problem arises here. System.Net.Quic is the internal MsQuic, and I directly opened my own socket, so there’s no way to inject the socket I created.... Ah, we get stuck here ^^;
So, the workaround I came up with is "close & rebind". Just like the name suggests.
- Open
UdpClientfor punching on local portP, and send packets to the opponent to break through the NAT hole… - That socket is Closed after,
- Bind QUIC back to Same port
P. (SpecifyLocalEndPoint/ListenEndPoint)
Just talking might be a bit confusing, so I drew a picture instead.
[로컬 포트 P]
│
├─ ① UdpClient로 상대에게 펀칭 패킷 발사 ──► NAT에 구멍이 뚫림!
│
├─ ② 펀칭 소켓 close (포트 P 반납)
│
└─ ③ QUIC를 같은 포트 P로 rebind ─────────► 뚫린 그 구멍으로 QUIC 핸드셰이크
(LocalEndPoint = P)
Since most cone NAT uses the mapping based on the internal (IP, 포트), re-uploading to the same port simply keeps the exposed holes intact. I thought it might not work, but it does!
// 펀칭용 소켓으로 구멍을 뚫고 닫은 뒤,
// 같은 로컬 포트로 QUIC를 다시 올립니다.
var options = new QuicClientConnectionOptions
{
LocalEndPoint = new IPEndPoint(IPAddress.Any, localPort), // ★ 핵심
RemoteEndPoint = peerEndpoint,
// ... TLS, KeepAlive 등
};
var connection = await QuicConnection.ConnectAsync(options);
Key sentence: Even if you cannot share the socket, binding it back to the same port will allow QUIC to directly inherit the exposed NAT hole.
I actually made it myself.
Just talking about it is boring, so I created a small PoC. The overall flow is as follows.
sequenceDiagram
autonumber
participant A as 피어A
participant S as 내 서버 (presence + STUN)
participant B as 피어B
A->>S: 상시 접속(SSE)
B->>S: 상시 접속(SSE)
Note over A,B: 둘 다 online 확인
S-->>A: 상대 주소 + 펀치 시각
S-->>B: 상대 주소 + 펀치 시각
par 동시에 펀칭
A->>B: 펀칭 패킷
B->>A: 펀칭 패킷
end
Note over A,B: close → 같은 포트로 QUIC rebind
A->>B: 직접 QUIC P2P 연결
Note over A,B: 서버 경유 X
- presence server: Both peers are connected continuously (SSE), so the server knows “who is online”. If both are online, they provide a synchronized handshake timestamp with each other’s public addresses.
- Self-contained STUN reflector: Without relying on Google STUN, my server returned the public address via UDP. Fully self-sufficient ^^
- Automatic reconnection: If the connection between QUIC is lost (either the NAT mapping expires or the other party disconnects), it is detected and reconnected as Re-rolling into a new port.
The last auto-reconnect was a part I really wanted to include after reading a post by a Syncthing user who said, “The QUIC Hallpunch works well for about 36 hours before it dies, and then no reconnection is possible anymore.” In reality, when I forced the opposite peer to die, it automatically changed the port and reconnected again. Satisfying ^^
The limits are clear
It shouldn’t be described as just too pink. I’ll keep it honest.
- In Symmetric NAT, this approach breaks down. The external port changes depending on the destination. This is not a limitation of QUIC, but a limitation of NAT; therefore, TURN relay bounce is required.
- I have still set Only local validation and loopback checks. The NAT traversal between two different networks is left as the next assignment. I plan to deploy this part on a public server and verify it experimentally.
- Since MsQuic opens the socket directly, the very short period between close and rebind is still a conflict zone. (The client is allowed to try several times.)
Finalization
In summary, it would be around The answer to the question “Can Holpunching be done via System.Net.Quic?” is ‘It depends.’. If the limitation of unable to share sockets is overcome by close & rebind, direct QUIC P2P can be established in a cone NAT environment. However, challenges such as symmetric NAT and actual internet verification remain.
.NET In the community, I don’t see any public references that handle this directly yet. So, feeling like a little pioneer(?), I happily explored it ^^ If anyone is thinking about something similar, I’d be happy to talk with them.
Then next, let’s return to a real NAT pass success example... Hope so ^^;



Comments: 0
Sign in to leave a comment.
No comments yet. Write the first one.