From 429ab1dce611dd9db564c69ac58a1dc622233357 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 9 Jul 2020 12:34:28 -0700 Subject: [PATCH] quic: minor reduction in code duplication PR-URL: https://github.com/nodejs/node/pull/34283 Reviewed-By: Anna Henningsen --- doc/api/quic.md | 6 ++++++ lib/internal/quic/core.js | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/doc/api/quic.md b/doc/api/quic.md index 0c8f73a0e5c8d1..b21d432642580b 100644 --- a/doc/api/quic.md +++ b/doc/api/quic.md @@ -255,6 +255,8 @@ added: REPLACEME * `options` {Object} * `client` {Object} A default configuration for QUIC client sessions created using `quicsocket.connect()`. + * `disableStatelessReset` {boolean} When `true` the `QuicSocket` will not + send stateless resets. **Default**: `false`. * `endpoint` {Object} An object describing the local address to bind to. * `address` {string} The local address to bind to. This may be an IPv4 or IPv6 address or a host name. If a host name is given, it will be resolved @@ -276,6 +278,10 @@ added: REPLACEME * `retryTokenTimeout` {number} The maximum number of *seconds* for retry token validation. Default: `10` seconds. * `server` {Object} A default configuration for QUIC server sessions. + * `statelessResetSecret` {Buffer|Uint8Array} A 16-byte `Buffer` or + `Uint8Array` providing the secret to use when generating stateless reset + tokens. If not specified, a random secret will be generated for the + `QuicSocket`. **Default**: `undefined`. * `validateAddress` {boolean} When `true`, the `QuicSocket` will use explicit address validation using a QUIC `RETRY` frame when listening for new server sessions. Default: `false`. diff --git a/lib/internal/quic/core.js b/lib/internal/quic/core.js index 908e01b625b665..9995b935b1c17c 100644 --- a/lib/internal/quic/core.js +++ b/lib/internal/quic/core.js @@ -581,6 +581,14 @@ function getStats(obj, idx) { return stats[idx]; } +function addressOrLocalhost(address, type) { + return address || (type === AF_INET6 ? '::' : '0.0.0.0'); +} + +function lookupOrDefault(lookup, type) { + return lookup || (type === AF_INET6 ? lookup6 : lookup4); +} + // QuicEndpoint wraps a UDP socket and is owned // by a QuicSocket. It does not exist independently // of the QuicSocket. @@ -610,9 +618,9 @@ class QuicEndpoint { } = validateQuicEndpointOptions(options); const state = this[kInternalState]; state.socket = socket; - state.address = address || (type === AF_INET6 ? '::' : '0.0.0.0'); + state.address = addressOrLocalhost(address, type); + state.lookup = lookupOrDefault(lookup, type); state.ipv6Only = ipv6Only; - state.lookup = lookup || (type === AF_INET6 ? lookup6 : lookup4); state.port = port; state.reuseAddr = reuseAddr; state.type = type; @@ -901,8 +909,8 @@ class QuicSocket extends EventEmitter { const state = this[kInternalState]; state.client = client; - state.lookup = lookup || (type === AF_INET6 ? lookup6 : lookup4); state.server = server; + state.lookup = lookupOrDefault(lookup, type); let socketOptions = 0; if (validateAddress) @@ -1286,7 +1294,7 @@ class QuicSocket extends EventEmitter { // Notice here that connectAfterLookup is bound to the QuicSession // that was created... lookup( - address || (type === AF_INET6 ? '::' : '0.0.0.0'), + addressOrLocalhost(address, type), connectAfterLookup.bind(session, type)); }