@@ -22,11 +22,11 @@ import (
2222//
2323// Multiple goroutines may invoke methods on an Endpoint simultaneously.
2424type Endpoint struct {
25- config * Config
26- packetConn packetConn
27- testHooks endpointTestHooks
28- resetGen statelessResetTokenGenerator
29- retry retryState
25+ listenConfig * Config
26+ packetConn packetConn
27+ testHooks endpointTestHooks
28+ resetGen statelessResetTokenGenerator
29+ retry retryState
3030
3131 acceptQueue queue [* Conn ] // new inbound connections
3232 connsMap connsMap // only accessed by the listen loop
@@ -51,9 +51,11 @@ type packetConn interface {
5151}
5252
5353// Listen listens on a local network address.
54- // The configuration config must be non-nil.
55- func Listen (network , address string , config * Config ) (* Endpoint , error ) {
56- if config .TLSConfig == nil {
54+ //
55+ // The config is used to for connections accepted by the endpoint.
56+ // If the config is nil, the endpoint will not accept connections.
57+ func Listen (network , address string , listenConfig * Config ) (* Endpoint , error ) {
58+ if listenConfig != nil && listenConfig .TLSConfig == nil {
5759 return nil , errors .New ("TLSConfig is not set" )
5860 }
5961 a , err := net .ResolveUDPAddr (network , address )
@@ -68,21 +70,25 @@ func Listen(network, address string, config *Config) (*Endpoint, error) {
6870 if err != nil {
6971 return nil , err
7072 }
71- return newEndpoint (pc , config , nil )
73+ return newEndpoint (pc , listenConfig , nil )
7274}
7375
7476func newEndpoint (pc packetConn , config * Config , hooks endpointTestHooks ) (* Endpoint , error ) {
7577 e := & Endpoint {
76- config : config ,
77- packetConn : pc ,
78- testHooks : hooks ,
79- conns : make (map [* Conn ]struct {}),
80- acceptQueue : newQueue [* Conn ](),
81- closec : make (chan struct {}),
82- }
83- e .resetGen .init (config .StatelessResetKey )
78+ listenConfig : config ,
79+ packetConn : pc ,
80+ testHooks : hooks ,
81+ conns : make (map [* Conn ]struct {}),
82+ acceptQueue : newQueue [* Conn ](),
83+ closec : make (chan struct {}),
84+ }
85+ var statelessResetKey [32 ]byte
86+ if config != nil {
87+ statelessResetKey = config .StatelessResetKey
88+ }
89+ e .resetGen .init (statelessResetKey )
8490 e .connsMap .init ()
85- if config .RequireAddressValidation {
91+ if config != nil && config .RequireAddressValidation {
8692 if err := e .retry .init (); err != nil {
8793 return nil , err
8894 }
@@ -141,14 +147,15 @@ func (e *Endpoint) Accept(ctx context.Context) (*Conn, error) {
141147}
142148
143149// Dial creates and returns a connection to a network address.
144- func (e * Endpoint ) Dial (ctx context.Context , network , address string ) (* Conn , error ) {
150+ // The config cannot be nil.
151+ func (e * Endpoint ) Dial (ctx context.Context , network , address string , config * Config ) (* Conn , error ) {
145152 u , err := net .ResolveUDPAddr (network , address )
146153 if err != nil {
147154 return nil , err
148155 }
149156 addr := u .AddrPort ()
150157 addr = netip .AddrPortFrom (addr .Addr ().Unmap (), addr .Port ())
151- c , err := e .newConn (time .Now (), clientSide , newServerConnIDs {}, addr )
158+ c , err := e .newConn (time .Now (), config , clientSide , newServerConnIDs {}, address , addr )
152159 if err != nil {
153160 return nil , err
154161 }
@@ -159,13 +166,13 @@ func (e *Endpoint) Dial(ctx context.Context, network, address string) (*Conn, er
159166 return c , nil
160167}
161168
162- func (e * Endpoint ) newConn (now time.Time , side connSide , cids newServerConnIDs , peerAddr netip.AddrPort ) (* Conn , error ) {
169+ func (e * Endpoint ) newConn (now time.Time , config * Config , side connSide , cids newServerConnIDs , peerHostname string , peerAddr netip.AddrPort ) (* Conn , error ) {
163170 e .connsMu .Lock ()
164171 defer e .connsMu .Unlock ()
165172 if e .closing {
166173 return nil , errors .New ("endpoint closed" )
167174 }
168- c , err := newConn (now , side , cids , peerAddr , e . config , e )
175+ c , err := newConn (now , side , cids , peerHostname , peerAddr , config , e )
169176 if err != nil {
170177 return nil , err
171178 }
@@ -288,11 +295,15 @@ func (e *Endpoint) handleUnknownDestinationDatagram(m *datagram) {
288295 // https://www.rfc-editor.org/rfc/rfc9000#section-10.3-16
289296 return
290297 }
298+ if e .listenConfig == nil {
299+ // We are not configured to accept connections.
300+ return
301+ }
291302 cids := newServerConnIDs {
292303 srcConnID : p .srcConnID ,
293304 dstConnID : p .dstConnID ,
294305 }
295- if e .config .RequireAddressValidation {
306+ if e .listenConfig .RequireAddressValidation {
296307 var ok bool
297308 cids .retrySrcConnID = p .dstConnID
298309 cids .originalDstConnID , ok = e .validateInitialAddress (now , p , m .peerAddr )
@@ -303,7 +314,7 @@ func (e *Endpoint) handleUnknownDestinationDatagram(m *datagram) {
303314 cids .originalDstConnID = p .dstConnID
304315 }
305316 var err error
306- c , err := e .newConn (now , serverSide , cids , m .peerAddr )
317+ c , err := e .newConn (now , e . listenConfig , serverSide , cids , "" , m .peerAddr )
307318 if err != nil {
308319 // The accept queue is probably full.
309320 // We could send a CONNECTION_CLOSE to the peer to reject the connection.
0 commit comments