Skip to content

Commit

Permalink
quic: cleanup QuicClientSession constructor
Browse files Browse the repository at this point in the history
PR-URL: #34283
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
jasnell committed Jul 16, 2020
1 parent fe4e7e4 commit 8e5c5b1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 53 deletions.
30 changes: 27 additions & 3 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1748,12 +1748,36 @@ Accessing `Object.prototype.__proto__` has been forbidden using
[`Object.setPrototypeOf`][] should be used to get and set the prototype of an
object.

<a id="ERR_QUICSESSION_VERSION_NEGOTIATION"></a>
### `ERR_QUICSESSION_VERSION_NEGOTIATION`
<a id="ERR_QUIC_FAILED_TO_CREATE_SESSION"></a>
### `ERR_QUIC_FAILED_TO_CREATE_SESSION`

> Stability: 1 - Experimental
TBD
An unspecified failure occured trying to initialize a new `QuicClientSession`.

<a id="ERR_QUIC_INVALID_REMOTE_TRANSPORT_PARAMS"></a>
### `ERR_QUIC_INVALID_REMOTE_TRANSPORT_PARAMS`

> Stability: 1 - Experimental
An attempt to resume a `QuicClientSession` using remembered remote transport
parameters failed because the transport parameters were invalid.

<a id="ERR_QUIC_INVALID_TLS_SESSION_TICKET"></a>
### `ERR_QUIC_INVALID_TLS_SESSION_TICKET`

> Stability: 1 - Experimental
An attempt resume a `QuicClientSession` using a remembered TLS session ticket
failed because the session ticket was invalid.

<a id="ERR_QUIC_VERSION_NEGOTIATION"></a>
### `ERR_QUIC_VERSION_NEGOTIATION`

> Stability: 1 - Experimental
A `QuicClientSession` received a version negotiation request from the
server and was shutdown accordingly.

<a id="ERR_REQUIRE_ESM"></a>
### `ERR_REQUIRE_ESM`
Expand Down
7 changes: 6 additions & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,12 @@ E('ERR_PACKAGE_PATH_NOT_EXPORTED', (pkgPath, subpath, base = undefined) => {
return `Package subpath '${subpath}' is not defined by "exports" in ${
pkgPath}package.json${base ? ` imported from ${base}` : ''}`;
}, Error);
E('ERR_QUICSESSION_VERSION_NEGOTIATION',
E('ERR_QUIC_FAILED_TO_CREATE_SESSION', 'Failed to create QuicSession', Error);
E('ERR_QUIC_INVALID_REMOTE_TRANSPORT_PARAMS',
'Invalid remote transport params', Error);
E('ERR_QUIC_INVALID_TLS_SESSION_TICKET',
'Invalid TLS session ticket', Error);
E('ERR_QUIC_VERSION_NEGOTIATION',
(version, requestedVersions, supportedVersions) => {
return 'QUIC session received version negotiation from server. ' +
`Version: ${version}. Requested: ${requestedVersions.join(', ')} ` +
Expand Down
72 changes: 23 additions & 49 deletions lib/internal/quic/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ const {
ERR_INVALID_CALLBACK,
ERR_INVALID_STATE,
ERR_OPERATION_FAILED,
ERR_QUICSESSION_VERSION_NEGOTIATION,
ERR_QUIC_FAILED_TO_CREATE_SESSION,
ERR_QUIC_INVALID_REMOTE_TRANSPORT_PARAMS,
ERR_QUIC_INVALID_TLS_SESSION_TICKET,
ERR_QUIC_VERSION_NEGOTIATION,
ERR_TLS_DH_PARAM_SIZE,
},
hideStackFrames,
Expand Down Expand Up @@ -1753,7 +1756,7 @@ class QuicSession extends EventEmitter {
// QuicSessions.
[kVersionNegotiation](version, requestedVersions, supportedVersions) {
const err =
new ERR_QUICSESSION_VERSION_NEGOTIATION(
new ERR_QUIC_VERSION_NEGOTIATION(
version,
requestedVersions,
supportedVersions);
Expand Down Expand Up @@ -2347,19 +2350,9 @@ class QuicServerSession extends QuicSession {
class QuicClientSession extends QuicSession {
[kInternalClientState] = {
allowEarlyData: false,
autoStart: true,
dcid: undefined,
handshakeStarted: false,
minDHSize: undefined,
port: undefined,
remoteTransportParams: undefined,
requestOCSP: undefined,
secureContext: undefined,
sessionTicket: undefined,
transportParams: undefined,
preferredAddressPolicy: undefined,
verifyHostnameIdentity: true,
qlogEnabled: false,
};

constructor(socket, options, type, ip) {
Expand Down Expand Up @@ -2396,74 +2389,55 @@ class QuicClientSession extends QuicSession {

super(socket, { servername, alpn, highWaterMark, defaultEncoding });
const state = this[kInternalClientState];
state.autoStart = autoStart;
state.handshakeStarted = autoStart;
state.dcid = dcid;
state.minDHSize = minDHSize;
state.port = port || 0;
state.preferredAddressPolicy = preferredAddressPolicy;
state.requestOCSP = requestOCSP;

state.secureContext =
createSecureContext(
sc_options,
initSecureContextClient);
state.transportParams = validateTransportParams(options);
state.verifyHostnameIdentity = verifyHostnameIdentity;
state.qlogEnabled = qlog;

// If provided, indicates that the client is attempting to
// resume a prior session. Early data would be enabled.
state.remoteTransportParams = remoteTransportParams;
state.sessionTicket = sessionTicket;

const transportParams = validateTransportParams(options);

state.allowEarlyData =
remoteTransportParams !== undefined &&
sessionTicket !== undefined;

setTransportParams(state.transportParams);
setTransportParams(transportParams);

const handle =
_createClientSession(
this.socket[kHandle],
type,
ip,
state.port,
port,
state.secureContext.context,
this.servername || ip,
state.remoteTransportParams,
state.sessionTicket,
state.dcid,
state.preferredAddressPolicy,
remoteTransportParams,
sessionTicket,
dcid,
preferredAddressPolicy,
this.alpnProtocol,
(state.verifyHostnameIdentity ?
(verifyHostnameIdentity ?
QUICCLIENTSESSION_OPTION_VERIFY_HOSTNAME_IDENTITY : 0) |
(state.requestOCSP ?
(requestOCSP ?
QUICCLIENTSESSION_OPTION_REQUEST_OCSP : 0),
state.qlogEnabled,
state.autoStart);

// We no longer need these, unset them so
// memory can be garbage collected.
state.remoteTransportParams = undefined;
state.sessionTicket = undefined;
state.dcid = undefined;
qlog,
autoStart);

// If handle is a number, creating the session failed.
if (typeof handle === 'number') {
let reason;
switch (handle) {
case ERR_FAILED_TO_CREATE_SESSION:
reason = 'QuicSession bootstrap failed';
break;
throw new ERR_QUIC_FAILED_TO_CREATE_SESSION();
case ERR_INVALID_REMOTE_TRANSPORT_PARAMS:
reason = 'Invalid Remote Transport Params';
break;
throw new ERR_QUIC_INVALID_REMOTE_TRANSPORT_PARAMS();
case ERR_INVALID_TLS_SESSION_TICKET:
reason = 'Invalid TLS Session Ticket';
break;
throw new ERR_QUIC_INVALID_TLS_SESSION_TICKET();
default:
reason = `${handle}`;
throw new ERR_OPERATION_FAILED(`Unspecified reason [${reason}]`);
}
throw new ERR_OPERATION_FAILED(reason);
}

this[kSetHandle](handle);
Expand Down

0 comments on commit 8e5c5b1

Please sign in to comment.