Add ListenPacketConn to accept connections on a supplied PacketConn - #837
Add ListenPacketConn to accept connections on a supplied PacketConn#837folbricht wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #837 +/- ##
==========================================
+ Coverage 82.35% 82.67% +0.32%
==========================================
Files 123 123
Lines 6999 7015 +16
==========================================
+ Hits 5764 5800 +36
+ Misses 820 807 -13
+ Partials 415 408 -7
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
e5ecb0c to
d1ee218
Compare
|
The
|
|
@folbricht Hello we can maybe do a better API and break the current one, and keep this PR targeted at |
|
Either works for me. Just let me know what you prefer and how I can update it. There's no urgency to it. |
d1ee218 to
7834c87
Compare
|
@JoTurk Going with the first option, keeping this targeted at This is what I went with: // New: supplied-conn path as first-class functions
func ListenPacketConn(pconn net.PacketConn, config *Config) (net.Listener, error)
func ListenPacketConnWithOptions(pconn net.PacketConn, opts ...ServerOption) (net.Listener, error)
// Unchanged: address-based listening keeps its name and signature
func Listen(network string, laddr *net.UDPAddr, config *Config) (net.Listener, error) // Deprecated
func ListenWithOptions(network string, laddr *net.UDPAddr, opts ...ServerOption) (net.Listener, error)
// Unchanged: wrapping an existing demuxing PacketListener
func NewListener(inner dtlsnet.PacketListener, config *Config) (net.Listener, error) // Deprecated
func NewListenerWithOptions(inner dtlsnet.PacketListener, opts ...ServerOption) (net.Listener, error)If you prefer, this could work as well, just let me know: // The DTLS analogue would have been:
func Listen(pconn net.PacketConn, config *Config) (net.Listener, error) // BREAKING: same name, new meaning
func ListenWithOptions(pconn net.PacketConn, opts ...ServerOption) (net.Listener, error) // BREAKING
func ListenAddr(network string, laddr *net.UDPAddr, config *Config) (net.Listener, error) // old behavior, new name
func ListenAddrWithOptions(network string, laddr *net.UDPAddr, opts ...ServerOption) (net.Listener, error) |
7834c87 to
9d7e893
Compare
|
Could some collaborator in this repository review this PR? |
|
@przemyslaw0 Sorry I saw your email the other day, I was going to response today, it takes time to introduce new APIs because it's not something we can revert easily in the future, and network APIs are tricky because we would want to normalize them across ice, stun etc |
|
Thanks, that makes sense. I looked at #766 to see whether a single API could cover both. My take: the two issues only share one concept, "the caller controls the packet path", and they need it at different layers. What each issue needs:
Why a single mechanism is hard to share directly: a A unified design that covers both:
One honest caveat: with the wrapper approach, classifying inbound datagrams still requires peeking at the record header content type (one byte per record), since there's no inbound equivalent of So my suggestion: this PR is the first half of the unified design rather than something it would replace. It's purely additive, and the #766 work can layer on top of the supplied-conn path with one small optional interface. Happy to adjust the shape here if you'd prefer the |
Pion has pion/transport, which defines similar network abstraction interfaces and includes vnet, but that does not cleanly solve this DTLS issue by itself. The missing piece is a DTLS server listener that can accept an existing/multiplexed net.PacketConn and still provide listener-style accept or similar. |
|
@JoTurk No problem, sorry, I'm really looking forward for support for setting socket options in listener. No hurry. |
Description
Adds
ListenPacketConnandListenPacketConnWithOptions, which create a DTLS listener that accepts connections on a caller-suppliednet.PacketConninstead of opening a socket itself.This complements #802:
WithListenConfigcovers socket options viaControl, but some use cases need the socket created entirely outside the library, e.g. a socket created in another network namespace and passed overSCM_RIGHTS, or one bound/configured by other means.Following the review discussion, this makes the supplied-connection path a first-class entry point rather than a
ServerOption. Compared to the earlierWithPacketConnapproach, there are no ignorednetwork/addressarguments and no interaction withWithListenConfig. The split mirrorscrypto/tls, wheretls.NewListenerwraps an existing listener andtls.Listenis the convenience that opens the socket, and is similar toquic.Listentaking anet.PacketConn.Internally,
udp.ListenConfiggains aListenPacketConnmethod holding the listener construction, withListenreduced to opening the socket and delegating to it. The internaludp.listeneronly usesnet.PacketConnmethods, so itspConnfield is widened from*net.UDPConn, which also makes the*net.UDPConnassertion afterListenPacketunnecessary.The listener takes ownership of the supplied connection and closes it on
Close. The existing public API is unchanged, so this is purely additive and no longer depends on a breaking release.