Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
quic: minor reduction in code duplication
PR-URL: #34283
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
jasnell committed Jul 16, 2020
1 parent aafdc2f commit 429ab1d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 6 additions & 0 deletions doc/api/quic.md
Expand Up @@ -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
Expand All @@ -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`.
Expand Down
16 changes: 12 additions & 4 deletions lib/internal/quic/core.js
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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));
}

Expand Down

0 comments on commit 429ab1d

Please sign in to comment.