/* Build the DoS defense cell extension and put it in the given extensions
* object. This can't fail. */
-static void
+static int
build_establish_intro_dos_extension(const hs_service_config_t *service_config,
trn_cell_extension_t *extensions)
{
- ssize_t ret, dos_ext_encoded_len;
+ ssize_t ret;
+ size_t dos_ext_encoded_len;
uint8_t *field_array;
trn_cell_extension_field_t *field;
trn_cell_extension_dos_t *dos_ext;
service_config->intro_dos_burst_per_sec);
/* Set the field with the encoded DoS extension. */
- dos_ext_encoded_len = trn_cell_extension_dos_encoded_len(dos_ext);
+ ret = trn_cell_extension_dos_encoded_len(dos_ext);
+ if (BUG(ret <= 0)) {
+ return -1;
+ }
+ dos_ext_encoded_len = ret;
/* Set length field and the field array size length. */
trn_cell_extension_field_set_field_len(field, dos_ext_encoded_len);
trn_cell_extension_field_setlen_field(field, dos_ext_encoded_len);
field_array = trn_cell_extension_field_getarray_field(field);
ret = trn_cell_extension_dos_encode(field_array,
trn_cell_extension_field_getlen_field(field), dos_ext);
- tor_assert(ret == dos_ext_encoded_len);
+ if (BUG(ret <= 0)) {
+ return -1;
+ }
+ tor_assert(ret == (ssize_t) dos_ext_encoded_len);
/* Finally, encode field into the cell extension. */
trn_cell_extension_add_fields(extensions, field);
/* Cleanup. DoS extension has been encoded at this point. */
trn_cell_extension_dos_free(dos_ext);
+
+ return 0;
}
/* ========== */
build_establish_intro_extensions(const hs_service_config_t *service_config,
const hs_service_intro_point_t *ip)
{
+ int ret;
trn_cell_extension_t *extensions;
tor_assert(service_config);
if (service_config->has_dos_defense_enabled &&
ip->support_intro2_dos_defense) {
/* This function takes care to increment the number of extensions. */
- build_establish_intro_dos_extension(service_config, extensions);
+ ret = build_establish_intro_dos_extension(service_config, extensions);
+ if (ret < 0) {
+ /* Return no extensions on error. */
+ goto end;
+ }
}
+ end:
return extensions;
}