Debian Codegraph @

crt/aws-c-mqtt/source/mqtt_iot_metrics.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/byte_buf.h>
7 #include <aws/common/system_info.h>
8 #include <aws/common/uri.h>
9 #include <aws/mqtt/mqtt.h>
10 #include <aws/mqtt/private/mqtt_iot_metrics.h>
11
12 #include <stdio.h>
13
14 // Use packet encoding limit for now: https://github.com/awslabs/aws-c-mqtt/blob/v0.13.3/source/packets.c#L26
15 const size_t AWS_IOT_MAX_USERNAME_SIZE = UINT16_MAX;
16 const size_t DEFAULT_QUERY_PARAM_COUNT = 10;
17
18 /*********************************************************************************************************************
19 * Helper functions for parsing and merging key-value pairs
20 ********************************************************************************************************************/
21
22 /**
23 * Find a parameter by key in a list of aws_uri_param.
24 *
25 * @param params_list List of aws_uri_param to search
26 * @param key The key to search for
27 * @param out_index [Optional] If not NULL and key is found, will be set to the index of the found parameter
28 * @return Pointer to the found parameter, or NULL if not found
29 */
30 static struct aws_uri_param *s_find_param_by_key(
31 const struct aws_array_list *params_list,
32 const struct aws_byte_cursor key,
33 size_t *out_index) {
34
35 size_t params_count = aws_array_list_length(params_list);
36 for (size_t i = 0; i < params_count; ++i) {
37 struct aws_uri_param *param = NULL;
38 aws_array_list_get_at_ptr(params_list, (void **)&param, i);
39 if (aws_byte_cursor_eq(&param->key, &key)) {
40 if (out_index) {
41 *out_index = i;
42 }
43 return param;
44 }
45 }
46 return NULL;
47 }
48
49 /**
50 * Add a parameter to the list only if a parameter with the same key doesn't already exist.
51 *
52 * @param params_list List of aws_uri_param to add to
53 * @param key The key for the new parameter
54 * @param value The value for the new parameter
55 * @return true if the parameter was added, false if a parameter with the same key already exists
56 */
57 static bool s_add_param_if_not_exists(
58 struct aws_array_list *params_list,
59 const struct aws_byte_cursor key,
60 const struct aws_byte_cursor value) {
61
62 if (s_find_param_by_key(params_list, key, NULL) != NULL) {
63 return false;
64 }
65
66 struct aws_uri_param param = {
67 .key = key,
68 .value = value,
69 };
70 aws_array_list_push_back(params_list, &param);
71 return true;
72 }
73
74 /**
75 * Parse key-value entries from a aws_byte_cursor using a specified delimiter.
76 * Each entry is expected to be in the format "key=value" or just "key". (e.g., "key1=value1&key2=value2")
77 *
78 * @param content The content to parse
79 * @param delimiter The delimiter cursor (e.g., ";" or "&"), length of delimiter must > 0.
80 * @param out_entries List to populate with parsed aws_uri_param entries. Must be initialized by caller.
81 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
82 */
83 static int s_parse_delimited_entries(
84 const struct aws_byte_cursor content,
85 const struct aws_byte_cursor delimiter,
86 struct aws_array_list *out_entries) {
87
88 AWS_ASSERT(delimiter.len > 0);
89
90 struct aws_byte_cursor equals_delim = aws_byte_cursor_from_c_str("=");
91 struct aws_byte_cursor entry_cursor;
92 AWS_ZERO_STRUCT(entry_cursor);
93
94 while (aws_byte_cursor_next_split_on_cursor(&content, delimiter, &entry_cursor)) {
95 /* Skip empty entries */
96 if (entry_cursor.len == 0) {
97 continue;
98 }
99
100 /* Parse key=value from entry_cursor */
101 struct aws_uri_param entry_param;
102 AWS_ZERO_STRUCT(entry_param);
103
104 /* Use a copy to avoid modifying entry_cursor which tracks iteration state */
105 struct aws_byte_cursor working_cursor = entry_cursor;
106 struct aws_byte_cursor equals_pos;
107 if (aws_byte_cursor_find_exact(&working_cursor, &equals_delim, &equals_pos) == AWS_OP_SUCCESS) {
108 size_t equals_offset = equals_pos.ptr - working_cursor.ptr;
109 entry_param.key = aws_byte_cursor_advance(&working_cursor, equals_offset);
110 /* Skip the "=" delimiter */
111 aws_byte_cursor_advance(&working_cursor, 1);
112 entry_param.value = working_cursor;
113 } else {
114 /* No equals sign, treat entire entry as key */
115 entry_param.key = entry_cursor;
116 }
117
118 aws_array_list_push_back(out_entries, &entry_param);
119 }
120
121 return AWS_OP_SUCCESS;
122 }
123
124 /**
125 * Merge new entries into an existing entries list without overwriting existing keys.
126 *
127 * @param existing_metadata_list List of existing aws_uri_param entries (will be modified)
128 * @param new_entries Array of new entries to merge
129 * @param new_entries_count Number of new entries
130 */
131 static void s_merge_metadata_entries_no_overwrite(
132 struct aws_array_list *existing_metadata_list,
133 const struct aws_mqtt_metadata_entry *new_entries_list,
134 const size_t new_entries_list_count) {
135
136 for (size_t i = 0; i < new_entries_list_count; ++i) {
137 const struct aws_mqtt_metadata_entry *new_entry = &new_entries_list[i];
138 s_add_param_if_not_exists(existing_metadata_list, new_entry->key, new_entry->value);
139 }
140 }
141
142 /**
143 * Build a delimited key-value string from a list of parameters.
144 * Can be used to either calculate the size, build the string, or both.
145 *
146 * Format: {prefix}{key1}={value1}{delimiter}{key2}={value2}...{suffix}
147 * If params_list is empty, returns empty string (no prefix/suffix).
148 *
149 * @param params_list List of aws_uri_param entries
150 * @param prefix [Optional] String to prepend (e.g., "?" for query strings, "(" for metadata)
151 * @param suffix [Optional] String to append (e.g., ")" for metadata)
152 * @param entry_delimiter Delimiter between entries (e.g., "&" for query strings, ";" for metadata)
153 * @param output_buf [Optional] Buffer to write the result to. Must be initialized with sufficient capacity.
154 * @param out_size [Optional] If not NULL, will be set to the size of the result string
155 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
156 */
157 static int s_build_delimited_params(
158 const struct aws_array_list *params_list,
159 const struct aws_byte_cursor *prefix,
160 const struct aws_byte_cursor *suffix,
161 const struct aws_byte_cursor entry_delimiter,
162 struct aws_byte_buf *output_buf,
163 size_t *out_size) {
164
165 AWS_ASSERT(entry_delimiter.len > 0);
166
167 size_t local_out_size = 0;
168 struct aws_byte_cursor key_value_delim = aws_byte_cursor_from_c_str("=");
169
170 size_t params_count = aws_array_list_length(params_list);
171
172 /* If params_list is empty, return empty string (no prefix/suffix) */
173 if (params_count == 0) {
174 if (out_size) {
175 *out_size = 0;
176 }
177 return AWS_OP_SUCCESS;
178 }
179
180 if (prefix) {
181 local_out_size += prefix->len;
182 }
183 if (suffix) {
184 local_out_size += suffix->len;
185 }
186
187 if (output_buf && prefix && prefix->len > 0) {
188 if (aws_byte_buf_append(output_buf, prefix)) {
189 return AWS_OP_ERR;
190 }
191 }
192
193 for (size_t i = 0; i < params_count; ++i) {
194 struct aws_uri_param param;
195 AWS_ZERO_STRUCT(param);
196 aws_array_list_get_at(params_list, &param, i);
197
198 /* Add delimiter between entries (not before the first one) */
199 if (i > 0) {
200 if (output_buf) {
201 if (aws_byte_buf_append(output_buf, &entry_delimiter)) {
202 return AWS_OP_ERR;
203 }
204 }
205 local_out_size += entry_delimiter.len;
206 }
207
208 /* Append key */
209 if (output_buf && param.key.len > 0) {
210 if (aws_byte_buf_append(output_buf, &param.key)) {
211 return AWS_OP_ERR;
212 }
213 }
214 local_out_size += param.key.len;
215
216 /* Append =value if value exists */
217 if (param.value.len > 0) {
218 if (output_buf) {
219 if (aws_byte_buf_append(output_buf, &key_value_delim) ||
220 aws_byte_buf_append(output_buf, &param.value)) {
221 return AWS_OP_ERR;
222 }
223 }
224 local_out_size += 1 + param.value.len; /* '=' + value */
225 }
226 }
227
228 if (output_buf && suffix && suffix->len > 0) {
229 if (aws_byte_buf_append(output_buf, suffix)) {
230 return AWS_OP_ERR;
231 }
232 }
233
234 if (out_size) {
235 *out_size = local_out_size;
236 }
237
238 return AWS_OP_SUCCESS;
239 }
240
241 /**
242 * Build the metadata value string from entries: (key1=value1;key2=value2;...)
243 * Can be used to either calculate the size, build the string, or both.
244 *
245 * @param entries List of aws_uri_param entries
246 * @param output_buf [Optional] Buffer to write the metadata value to. Must be initialized with sufficient capacity.
247 * @param out_size [Optional] If not NULL, will be set to the size of the metadata value string
248 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
249 */
250 static int s_build_metadata_value(
251 const struct aws_array_list *entries,
252 struct aws_byte_buf *output_buf,
253 size_t *out_size) {
254
255 struct aws_byte_cursor open_paren = aws_byte_cursor_from_c_str("(");
256 struct aws_byte_cursor close_paren = aws_byte_cursor_from_c_str(")");
257 struct aws_byte_cursor semicolon_delim = aws_byte_cursor_from_c_str(";");
258
259 return s_build_delimited_params(entries, &open_paren, &close_paren, semicolon_delim, output_buf, out_size);
260 }
261
262 /**
263 * Builds final username with query parameters appended.
264 *
265 * @param base_username The original username cursor
266 * @param base_username_length Length of base username to use (may differ from cursor length)
267 * @param params_list List of query parameters to append
268 * @param output_username [Optional] Buffer to write result. Caller must init/cleanup the buffer.
269 * @param out_final_username_size [Optional] Outputs the final username size
270 *
271 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
272 */
273 static int s_build_username_query(
274 const struct aws_byte_cursor base_username,
275 const size_t base_username_length,
276 const struct aws_array_list *params_list,
277 struct aws_byte_buf *output_username,
278 size_t *out_final_username_size) {
279
280 /* Write base username first */
281 if (output_username) {
282 if (!aws_byte_buf_write(output_username, base_username.ptr, base_username_length)) {
283 return AWS_OP_ERR;
284 }
285 }
286
287 if (out_final_username_size) {
288 *out_final_username_size = base_username_length;
289 }
290
291 /* Build query parameters using the generic helper */
292 struct aws_byte_cursor query_prefix = aws_byte_cursor_from_c_str("?");
293 struct aws_byte_cursor ampersand_delim = aws_byte_cursor_from_c_str("&");
294
295 size_t params_size = 0;
296 if (s_build_delimited_params(params_list, &query_prefix, NULL, ampersand_delim, output_username, &params_size)) {
297 return AWS_OP_ERR;
298 }
299
300 if (out_final_username_size) {
301 *out_final_username_size += params_size;
302 }
303
304 return AWS_OP_SUCCESS;
305 }
306
307 /**
308 * Appends SDK metrics to an MQTT username for IoT service telemetry.
309 *
310 * This function transforms a username by appending SDK metrics as query parameters.
311 * The IoT service uses the last '?' in the username to identify the metrics section.
312 *
313 * Step-by-step process:
314 * 1. Parse existing username: Find the last '?' in the original username and parse query params into
315 * username_params_list
316 * 2. Add "SDK" and "Platform" parameters if not already present
317 * 3. Handle Metadata entries if metadata entries are provided
318 * a. Check if existing "Metadata" parameter exists in username_params_list
319 * b. If existing Metadata found with valid format:
320 * - Parse existing entries into a key-value metadata_param_list
321 * - Remove existing Metadata from username_params_list (will be rebuilt later)
322 * c. If existing Metadata has an invalid format:
323 * - Log debug message and skip adding metrics metadata entries
324 * - Keep original Metadata value unchanged, skip (d)
325 * d. Append metrics metadata entries to metadata_param_list
326 * - Add new metadata entries into metadata_param_list (existing keys take precedence, won't be overwritten)
327 * - build metadata value string from metadata_param_list
328 * - Add Metadata parameter to username_params_list
329 * 4. Build final username from username_params_list
330 *
331 * Example transformation:
332 * Input: "myuser?existing=value"
333 * Metrics: {library_name="MySDK/1.0", metadata=[{key="ver", value="1.0"}]}
334 * Output: "myuser?existing=value&SDK=MySDK/1.0&Platform=Darwin&Metadata=(ver=1.0)"
335 */
336 int aws_mqtt_append_sdk_metrics_to_username(
337 struct aws_allocator *allocator,
338 const struct aws_byte_cursor *original_username,
339 const struct aws_mqtt_iot_metrics *metrics,
340 struct aws_byte_buf *output_username,
341 size_t *out_full_username_size) {
342 AWS_PRECONDITION(
343 output_username == NULL ||
344 (aws_byte_buf_is_valid(output_username) && (output_username && output_username->buffer == NULL)));
345
346 if (!allocator) {
347 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
348 }
349 const struct aws_byte_cursor local_original_username =
350 original_username == NULL ? aws_byte_cursor_from_c_str("") : *original_username;
351
352 if (!metrics) {
353 if (out_full_username_size) {
354 *out_full_username_size = local_original_username.len;
355 }
356
357 if (output_username) {
358 return aws_byte_buf_init_copy_from_cursor(output_username, allocator, local_original_username);
359 }
360
361 return AWS_OP_SUCCESS;
362 }
363
364 if (aws_mqtt_validate_iot_metrics(metrics)) {
365 return AWS_OP_ERR;
366 }
367
368 int result = AWS_OP_ERR;
369 size_t base_username_length = 0;
370 struct aws_byte_cursor question_mark_str = aws_byte_cursor_from_c_str("?");
371 struct aws_byte_cursor sdk_str = aws_byte_cursor_from_c_str("SDK");
372 struct aws_byte_cursor platform_str = aws_byte_cursor_from_c_str("Platform");
373 struct aws_byte_cursor metadata_str = aws_byte_cursor_from_c_str("Metadata");
374
375 struct aws_array_list username_params_list;
376 aws_array_list_init_dynamic(
377 &username_params_list, allocator, DEFAULT_QUERY_PARAM_COUNT, sizeof(struct aws_uri_param));
378
379 struct aws_byte_buf metadata_value_buf;
380 AWS_ZERO_STRUCT(metadata_value_buf);
381
382 struct aws_array_list metadata_param_list;
383 AWS_ZERO_STRUCT(metadata_param_list);
384
385 /* Parse existing query parameters from the original username */
386 if (local_original_username.len > 0) {
387 struct aws_byte_cursor question_mark_find = local_original_username;
388
389 bool found_query = false;
390 /* Find the last question mark. The IoT service will trim string after last question mark and handle it as
391 * metrics metadata */
392 while (AWS_OP_SUCCESS ==
393 aws_byte_cursor_find_exact(&question_mark_find, &question_mark_str, &question_mark_find)) {
394 // Advance cursor to skip the "?" character
395 aws_byte_cursor_advance(&question_mark_find, 1);
396 found_query = true;
397 }
398
399 if (found_query) {
400 // Trim out the query delimiter from the base username
401 base_username_length = question_mark_find.ptr - 1 - local_original_username.ptr;
402 aws_query_string_params(question_mark_find, &username_params_list);
403 } else {
404 base_username_length = local_original_username.len;
405 }
406 }
407
408 /* Add SDK parameter if not already present */
409 struct aws_byte_cursor sdk_value =
410 metrics->library_name.len > 0 ? metrics->library_name : aws_byte_cursor_from_c_str("IoTDeviceSDK/C");
411 s_add_param_if_not_exists(&username_params_list, sdk_str, sdk_value);
412
413 /* Add Platform parameter if not already present */
414 s_add_param_if_not_exists(&username_params_list, platform_str, aws_get_platform_build_os_string());
415
416 /* Handle metadata entries: parse existing, merge with new, and rebuild */
417 bool has_new_metadata = metrics->metadata_entries != NULL && metrics->metadata_count > 0;
418
419 if (has_new_metadata) {
420 struct aws_byte_cursor semicolon_delim = aws_byte_cursor_from_c_str(";");
421 bool should_merge_metadata = true;
422
423 aws_array_list_init_dynamic(
424 &metadata_param_list, allocator, DEFAULT_QUERY_PARAM_COUNT, sizeof(struct aws_uri_param));
425
426 /* Find existing Metadata parameter */
427 size_t existing_metadata_index = 0;
428 struct aws_uri_param *existing_metadata_param =
429 s_find_param_by_key(&username_params_list, metadata_str, &existing_metadata_index);
430
431 if (existing_metadata_param != NULL && existing_metadata_param->value.len > 0) {
432 /* Extract content without parentheses: (key1=value1;key2=value2) -> key1=value1;key2=value2 */
433 struct aws_byte_cursor existing_content = existing_metadata_param->value;
434 if (existing_content.len >= 2 && existing_content.ptr[0] == '(' &&
435 existing_content.ptr[existing_content.len - 1] == ')') {
436 aws_byte_cursor_advance(&existing_content, 1);
437 existing_content.len -= 1;
438
439 /* Parse existing metadata entries */
440 if (existing_content.len > 0) {
441 s_parse_delimited_entries(existing_content, semicolon_delim, &metadata_param_list);
442 }
443 } else {
444 /* Wrong format: keep the original metadata value unchanged and don't append new metadata */
445 AWS_LOGF_DEBUG(
446 AWS_LS_MQTT_GENERAL,
447 "Existing Metadata parameter has invalid format (expected parentheses). "
448 "Keeping original value and skipping new metadata entries.");
449 should_merge_metadata = false;
450 }
451 }
452
453 if (should_merge_metadata) {
454 /* Merge new metadata entries (won't overwrite existing keys) */
455 s_merge_metadata_entries_no_overwrite(
456 &metadata_param_list, metrics->metadata_entries, metrics->metadata_count);
457
458 /* Calculate size needed for metadata value string first */
459 size_t metadata_value_size = 0;
460 s_build_metadata_value(&metadata_param_list, NULL, &metadata_value_size);
461
462 /* Initialize buffer and build the metadata value string */
463 if (aws_byte_buf_init(&metadata_value_buf, allocator, metadata_value_size)) {
464 goto cleanup;
465 }
466
467 if (s_build_metadata_value(&metadata_param_list, &metadata_value_buf, NULL)) {
468 goto cleanup;
469 }
470
471 /* Add or replace Metadata parameter in username_params_list */
472 struct aws_uri_param metadata_params = {
473 .key = metadata_str,
474 .value = aws_byte_cursor_from_buf(&metadata_value_buf),
475 };
476 if (existing_metadata_param != NULL) {
477 /* Replace existing Metadata parameter at the same index */
478 aws_array_list_set_at(&username_params_list, &metadata_params, existing_metadata_index);
479 } else {
480 aws_array_list_push_back(&username_params_list, &metadata_params);
481 }
482 }
483 }
484
485 /* Rebuild metrics string from username_params_list */
486 // Calculate the final username size first.
487 size_t total_size = 0;
488 s_build_username_query(local_original_username, base_username_length, &username_params_list, NULL, &total_size);
489
490 if (total_size > AWS_IOT_MAX_USERNAME_SIZE) {
491 AWS_LOGF_ERROR(
492 AWS_LS_MQTT_GENERAL,
493 "Failed to append SDK metrics to username: resulting username exceeds max username size.");
494 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
495 goto cleanup;
496 }
497
498 if (output_username && aws_byte_buf_init(output_username, allocator, total_size)) {
499 goto cleanup;
500 }
501
502 if (s_build_username_query(
503 local_original_username,
504 base_username_length,
505 &username_params_list,
506 output_username,
507 out_full_username_size)) {
508 goto cleanup;
509 }
510
511 result = AWS_OP_SUCCESS;
512
513 cleanup:
514 aws_byte_buf_clean_up(&metadata_value_buf);
515 aws_array_list_clean_up(&username_params_list);
516 if (aws_array_list_is_valid(&metadata_param_list)) {
517 aws_array_list_clean_up(&metadata_param_list);
518 }
519
520 if (result == AWS_OP_ERR) {
521 aws_byte_buf_clean_up(output_username);
522 }
523 return result;
524 }
525
526 /*********************************************************************************************************************
527 * IoT SDK Metrics
528 ********************************************************************************************************************/
529
530 size_t aws_mqtt_iot_metrics_compute_storage_size(const struct aws_mqtt_iot_metrics *metrics) {
531 if (metrics == NULL) {
532 return 0;
533 }
534
535 size_t storage_size = 0;
536 storage_size += metrics->library_name.len;
537
538 /* Add storage for metadata entries */
539 if (metrics->metadata_entries != NULL && metrics->metadata_count > 0) {
540 for (size_t i = 0; i < metrics->metadata_count; ++i) {
541 const struct aws_mqtt_metadata_entry *entry = &metrics->metadata_entries[i];
542 storage_size += entry->key.len;
543 storage_size += entry->value.len;
544 }
545 }
546
547 return storage_size;
548 }
549
550 struct aws_mqtt_iot_metrics_storage *aws_mqtt_iot_metrics_storage_new(
551 struct aws_allocator *allocator,
552 const struct aws_mqtt_iot_metrics *metrics_options) {
553
554 if (allocator == NULL || metrics_options == NULL) {
555 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
556 return NULL;
557 }
558
559 struct aws_mqtt_iot_metrics_storage *metrics_storage =
560 aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt_iot_metrics_storage));
561
562 size_t storage_capacity = aws_mqtt_iot_metrics_compute_storage_size(metrics_options);
563 if (aws_byte_buf_init(&metrics_storage->storage, allocator, storage_capacity)) {
564 goto cleanup_storage;
565 }
566
567 metrics_storage->allocator = allocator;
568
569 struct aws_mqtt_iot_metrics *storage_view = &metrics_storage->storage_view;
570
571 if (metrics_options->library_name.len > 0) {
572 metrics_storage->library_name = metrics_options->library_name;
573 if (aws_byte_buf_append_and_update(&metrics_storage->storage, &metrics_storage->library_name)) {
574 goto cleanup_storage;
575 }
576 storage_view->library_name = metrics_storage->library_name;
577 }
578
579 /* Copy metadata entries */
580 if (metrics_options->metadata_entries != NULL && metrics_options->metadata_count > 0) {
581 if (aws_array_list_init_dynamic(
582 &metrics_storage->metadata_entries,
583 allocator,
584 metrics_options->metadata_count,
585 sizeof(struct aws_mqtt_metadata_entry))) {
586 goto cleanup_storage;
587 }
588
589 for (size_t i = 0; i < metrics_options->metadata_count; ++i) {
590 struct aws_mqtt_metadata_entry entry;
591 entry.key = metrics_options->metadata_entries[i].key;
592 entry.value = metrics_options->metadata_entries[i].value;
593
594 /* Copy key into storage buffer and update cursor */
595 if (entry.key.len > 0) {
596 if (aws_byte_buf_append_and_update(&metrics_storage->storage, &entry.key)) {
597 goto cleanup_storage;
598 }
599 }
600
601 /* Copy value into storage buffer and update cursor */
602 if (entry.value.len > 0) {
603 if (aws_byte_buf_append_and_update(&metrics_storage->storage, &entry.value)) {
604 goto cleanup_storage;
605 }
606 }
607
608 aws_array_list_push_back(&metrics_storage->metadata_entries, &entry);
609 }
610
611 /* Set storage_view to point to the metadata entries array */
612 storage_view->metadata_count = aws_array_list_length(&metrics_storage->metadata_entries);
613 storage_view->metadata_entries = metrics_storage->metadata_entries.data;
614 }
615
616 return metrics_storage;
617
618 cleanup_storage:
619 if (aws_array_list_is_valid(&metrics_storage->metadata_entries)) {
620 aws_array_list_clean_up(&metrics_storage->metadata_entries);
621 }
622
623 aws_byte_buf_clean_up(&metrics_storage->storage);
624 aws_mem_release(allocator, metrics_storage);
625 return NULL;
626 }
627
628 void aws_mqtt_iot_metrics_storage_destroy(struct aws_mqtt_iot_metrics_storage *metrics_storage) {
629 if (metrics_storage == NULL) {
630 return;
631 }
632
633 if (aws_array_list_is_valid(&metrics_storage->metadata_entries)) {
634 aws_array_list_clean_up(&metrics_storage->metadata_entries);
635 }
636
637 aws_byte_buf_clean_up(&metrics_storage->storage);
638
639 aws_mem_release(metrics_storage->allocator, metrics_storage);
640 }
641
642 int aws_mqtt_validate_iot_metrics(const struct aws_mqtt_iot_metrics *metrics) {
643 if (metrics == NULL) {
644 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
645 }
646
647 if (aws_mqtt_validate_utf8_text(metrics->library_name)) {
648 return AWS_OP_ERR;
649 }
650
651 /* Validate metadata entries */
652 if (metrics->metadata_entries != NULL && metrics->metadata_count > 0) {
653 for (size_t i = 0; i < metrics->metadata_count; ++i) {
654 if (aws_mqtt_validate_utf8_text(metrics->metadata_entries[i].key)) {
655 return AWS_OP_ERR;
656 }
657 if (aws_mqtt_validate_utf8_text(metrics->metadata_entries[i].value)) {
658 return AWS_OP_ERR;
659 }
660 }
661 }
662
663 return AWS_OP_SUCCESS;
664 }
665
666 bool aws_mqtt_has_non_empty_username(
667 const struct aws_byte_cursor *username,
668 const struct aws_mqtt_iot_metrics_storage *metrics_storage) {
669 return (username != NULL && username->len > 0) || metrics_storage != NULL;
670 }