@@ -34,22 +34,23 @@ TransportParams::Config::Config(Side side,
3434Maybe<TransportParams::Options> TransportParams::Options::From (
3535 Environment* env, Local<Value> value) {
3636 if (value.IsEmpty ()) {
37- THROW_ERR_INVALID_ARG_TYPE (env, " options must be an object" );
37+ THROW_ERR_INVALID_ARG_TYPE (env, " options must be an object or undefined" );
38+ return Nothing<Options>();
39+ } else if (value->IsUndefined ()) {
40+ return Just<Options>(kDefault );
41+ } else if (!value->IsObject ()) {
42+ THROW_ERR_INVALID_ARG_TYPE (env, " options must be an object or undefined" );
3843 return Nothing<Options>();
3944 }
4045
4146 Options options;
42- auto & state = BindingData::Get (env);
43-
44- if (value->IsUndefined ()) {
45- return Just<Options>(options);
46- }
4747
48- if (!value-> IsObject ()) {
49- THROW_ERR_INVALID_ARG_TYPE (env, " options must be an object " );
50- return Nothing<Options>();
51- }
48+ // TODO(@jasnell): We currently only support version 1 of the transport
49+ // parameters, so the options.transportParamsVersion is hardcoded to that.
50+ // In the future, when we support multiple versions, we will need to
51+ // expose this via the options object.
5252
53+ auto & state = BindingData::Get (env);
5354 auto params = value.As <Object>();
5455
5556#define SET (name ) \
@@ -68,14 +69,20 @@ Maybe<TransportParams::Options> TransportParams::Options::From(
6869
6970#undef SET
7071
72+ // TODO(@jasnell): We are not yet exposing the ability to set the preferred
73+ // adddress via the options, tho the underlying support is here in the class.
74+ options.preferred_address_ipv4 = std::nullopt ;
75+ options.preferred_address_ipv6 = std::nullopt ;
76+
7177 return Just<Options>(options);
7278}
7379
7480std::string TransportParams::Options::ToString () const {
7581 DebugIndentScope indent;
7682 auto prefix = indent.Prefix ();
7783 std::string res (" {" );
78- res += prefix + " version: " + std::to_string (transportParamsVersion);
84+ res += prefix +
85+ " version: " + std::to_string (static_cast <int >(transportParamsVersion));
7986 if (preferred_address_ipv4.has_value ()) {
8087 res += prefix + " preferred_address_ipv4: " +
8188 preferred_address_ipv4.value ().ToString ();
@@ -131,36 +138,39 @@ TransportParams::TransportParams(const ngtcp2_transport_params* ptr)
131138TransportParams::TransportParams (const Config& config, const Options& options)
132139 : TransportParams() {
133140 ngtcp2_transport_params_default (¶ms_);
134- params_.active_connection_id_limit = options.active_connection_id_limit ;
135- params_.initial_max_stream_data_bidi_local =
136- options.initial_max_stream_data_bidi_local ;
137- params_.initial_max_stream_data_bidi_remote =
138- options.initial_max_stream_data_bidi_remote ;
139- params_.initial_max_stream_data_uni = options.initial_max_stream_data_uni ;
140- params_.initial_max_streams_bidi = options.initial_max_streams_bidi ;
141- params_.initial_max_streams_uni = options.initial_max_streams_uni ;
142- params_.initial_max_data = options.initial_max_data ;
143- params_.max_idle_timeout = options.max_idle_timeout * NGTCP2_SECONDS;
144- params_.max_ack_delay = options.max_ack_delay ;
145- params_.ack_delay_exponent = options.ack_delay_exponent ;
146- params_.max_datagram_frame_size = options.max_datagram_frame_size ;
147- params_.disable_active_migration = options.disable_active_migration ? 1 : 0 ;
148- params_.preferred_addr_present = 0 ;
149- params_.stateless_reset_token_present = 0 ;
150- params_.retry_scid_present = 0 ;
141+ #define SET_PARAM (name ) params_.name = options.name
142+ #define SET_PARAM_V (name, value ) params_.name = value
143+ SET_PARAM (active_connection_id_limit);
144+ SET_PARAM (initial_max_stream_data_bidi_local);
145+ SET_PARAM (initial_max_stream_data_bidi_remote);
146+ SET_PARAM (initial_max_stream_data_uni);
147+ SET_PARAM (initial_max_streams_bidi);
148+ SET_PARAM (initial_max_streams_uni);
149+ SET_PARAM (initial_max_data);
150+ SET_PARAM (max_ack_delay);
151+ SET_PARAM (ack_delay_exponent);
152+ SET_PARAM (max_datagram_frame_size);
153+ SET_PARAM_V (max_idle_timeout, options.max_idle_timeout * NGTCP2_SECONDS);
154+ SET_PARAM_V (disable_active_migration,
155+ options.disable_active_migration ? 1 : 0 );
156+ SET_PARAM_V (preferred_addr_present, 0 );
157+ SET_PARAM_V (stateless_reset_token_present, 0 );
158+ SET_PARAM_V (retry_scid_present, 0 );
151159
152160 if (config.side == Side::SERVER) {
153161 // For the server side, the original dcid is always set.
154162 CHECK (config.ocid );
155- params_. original_dcid = config.ocid ;
156- params_. original_dcid_present = 1 ;
163+ SET_PARAM_V ( original_dcid, config.ocid ) ;
164+ SET_PARAM_V ( original_dcid_present, 1 ) ;
157165
158166 // The retry_scid is only set if the server validated a retry token.
159167 if (config.retry_scid ) {
160- params_. retry_scid = config.retry_scid ;
161- params_. retry_scid_present = 1 ;
168+ SET_PARAM_V ( retry_scid, config.retry_scid ) ;
169+ SET_PARAM_V ( retry_scid_present, 1 ) ;
162170 }
163171 }
172+ #undef SET_PARAM
173+ #undef SET_PARAM_V
164174
165175 if (options.preferred_address_ipv4 .has_value ())
166176 SetPreferredAddress (options.preferred_address_ipv4 .value ());
@@ -169,33 +179,39 @@ TransportParams::TransportParams(const Config& config, const Options& options)
169179 SetPreferredAddress (options.preferred_address_ipv6 .value ());
170180}
171181
172- TransportParams::TransportParams (const ngtcp2_vec& vec, int version)
182+ TransportParams::TransportParams (const ngtcp2_vec& vec, Version version)
173183 : TransportParams() {
174184 int ret = ngtcp2_transport_params_decode_versioned (
175- version, ¶ms_, vec.base , vec.len );
185+ static_cast < int >( version) , ¶ms_, vec.base , vec.len );
176186
187+ // The only error we should see here is NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM,
188+ // which indicates that the provided data was not valid transport parameters.
189+ // In that case, we set ptr_ to nullptr to indicate that the parameters
190+ // could not be decoded.
177191 if (ret != 0 ) {
178192 ptr_ = nullptr ;
179- error_ = QuicError::ForNgtcp2Error (ret);
180193 }
181194}
182195
183- Store TransportParams::Encode (Environment* env, int version) const {
196+ Store TransportParams::Encode (Environment* env, Version version) const {
184197 if (ptr_ == nullptr ) {
185198 return {};
186199 }
187200
188201 // Preflight to see how much storage we'll need.
189- ssize_t size =
190- ngtcp2_transport_params_encode_versioned ( nullptr , 0 , version, ¶ms_);
202+ ssize_t size = ngtcp2_transport_params_encode_versioned (
203+ nullptr , 0 , static_cast < int >( version) , ¶ms_);
191204 if (size == 0 ) {
192205 return {};
193206 }
194207
195208 JS_TRY_ALLOCATE_BACKING_OR_RETURN (env, result, size, {});
196209
197210 auto ret = ngtcp2_transport_params_encode_versioned (
198- static_cast <uint8_t *>(result->Data ()), size, version, ¶ms_);
211+ static_cast <uint8_t *>(result->Data ()),
212+ size,
213+ static_cast <int >(version),
214+ ¶ms_);
199215
200216 // The ret is the number of bytes written, or a negative error code.
201217 if (ret < 0 ) return {};
@@ -214,6 +230,7 @@ void TransportParams::SetPreferredAddress(const SocketAddress& address) {
214230 &src->sin_addr ,
215231 sizeof (params_.preferred_addr .ipv4 .sin_addr ));
216232 params_.preferred_addr .ipv4 .sin_port = address.port ();
233+ params_.preferred_addr .ipv4_present = 1 ;
217234 return ;
218235 }
219236 case AF_INET6: {
@@ -223,6 +240,7 @@ void TransportParams::SetPreferredAddress(const SocketAddress& address) {
223240 &src->sin6_addr ,
224241 sizeof (params_.preferred_addr .ipv6 .sin6_addr ));
225242 params_.preferred_addr .ipv6 .sin6_port = address.port ();
243+ params_.preferred_addr .ipv6_present = 1 ;
226244 return ;
227245 }
228246 }
@@ -273,10 +291,6 @@ TransportParams::operator bool() const {
273291 return ptr_ != nullptr ;
274292}
275293
276- const QuicError& TransportParams::error () const {
277- return error_;
278- }
279-
280294void TransportParams::Initialize (Environment* env, Local<Object> target) {
281295 NODE_DEFINE_CONSTANT (target, DEFAULT_MAX_STREAM_DATA);
282296 NODE_DEFINE_CONSTANT (target, DEFAULT_MAX_DATA);
0 commit comments