From f79eaba9b9ed602c746ec83e42fef1bba2d3586c Mon Sep 17 00:00:00 2001 From: Magdalena Szumny Date: Mon, 16 Feb 2026 08:13:43 +0000 Subject: [PATCH] Add sorting of optlist --- include/coap3/coap_option.h | 10 ++++++++++ libcoap-3.map | 1 + libcoap-3.sym | 1 + man/Makefile.am | 1 + man/coap_pdu_setup.txt.in | 18 ++++++++++++++++-- src/coap_option.c | 10 ++++++++++ 6 files changed, 39 insertions(+), 2 deletions(-) diff --git a/include/coap3/coap_option.h b/include/coap3/coap_option.h index 4c8efb3f..91cbad5e 100644 --- a/include/coap3/coap_option.h +++ b/include/coap3/coap_option.h @@ -376,6 +376,16 @@ coap_optlist_t *coap_new_optlist(uint16_t number, */ int coap_add_optlist_pdu(coap_pdu_t *pdu, coap_optlist_t **optlist_chain); + +/** + * @brief Sorts the optlist chain + * + * @param options The optlist chain to sort + * + * @return @c 1 if sorted successfully, @c 0 otherwise. + */ +int coap_sort_optlist(coap_optlist_t **options); + /** * Adds @p optlist to the given @p optlist_chain. The optlist_chain variable * be set to NULL before the initial call to coap_insert_optlist(). diff --git a/libcoap-3.map b/libcoap-3.map index 572380fa..fc08738b 100644 --- a/libcoap-3.map +++ b/libcoap-3.map @@ -333,6 +333,7 @@ global: coap_socket_get_flags; coap_socket_set_flags; coap_socket_strerror; + coap_sort_optlist; coap_split_path; coap_split_proxy_uri; coap_split_query; diff --git a/libcoap-3.sym b/libcoap-3.sym index 297b5741..a33eab23 100644 --- a/libcoap-3.sym +++ b/libcoap-3.sym @@ -331,6 +331,7 @@ coap_socket_get_fd coap_socket_get_flags coap_socket_set_flags coap_socket_strerror +coap_sort_optlist coap_split_path coap_split_proxy_uri coap_split_query diff --git a/man/Makefile.am b/man/Makefile.am index cb013c54..6ed38b60 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -192,6 +192,7 @@ install-man: install-man3 install-man5 install-man7 @echo ".so man3/coap_pdu_access.3" > coap_get_uri_path.3 @echo ".so man3/coap_pdu_setup.3" > coap_insert_optlist.3 @echo ".so man3/coap_pdu_setup.3" > coap_delete_optlist.3 + @echo ".so man3/coap_pdu_setup.3" > coap_sort_optlist.3 @echo ".so man3/coap_pdu_setup.3" > coap_encode_var_safe.3 @echo ".so man3/coap_pdu_setup.3" > coap_encode_var_safe8.3 @echo ".so man3/coap_pdu_setup.3" > coap_add_optlist_pdu.3 diff --git a/man/coap_pdu_setup.txt.in b/man/coap_pdu_setup.txt.in index ca439772..04ecdecd 100644 --- a/man/coap_pdu_setup.txt.in +++ b/man/coap_pdu_setup.txt.in @@ -20,6 +20,7 @@ coap_add_token, coap_new_optlist, coap_insert_optlist, coap_delete_optlist, +coap_sort_optlist, coap_encode_var_safe, coap_encode_var_safe8, coap_add_optlist_pdu, @@ -64,6 +65,8 @@ coap_optlist_t *_optlist_);* *void coap_delete_optlist(coap_optlist_t *_optlist_chain_);* +*int coap_sort_optlist(coap_optlist_t **_options_);* + *unsigned int coap_encode_var_safe(uint8_t *_buffer_, size_t _size_, unsigned int _value_);* @@ -383,6 +386,17 @@ off all the _optlist_chain_ entries. The *coap_delete_optlist*() function deletes and frees off all the optlist entries in the _optlist_chain_. +*Function: coap_sort_optlist()* + +The *coap_sort_optlist*() function sorts the optlist chain pointed to by +_options_ into ascending option number order (RFC 7252 ordering for delta +encoding). The chain is reordered in place. Use this when the list was +built with *coap_insert_optlist*() and you need it ordered before use (for +example, before adding options to a PDU manually or for inspection). +*coap_add_optlist_pdu*() sorts internally, so calling *coap_sort_optlist*() +is not required when using that function. Returns 1 if sorted successfully, +0 if _options_ is NULL or the chain is empty. + *Function: coap_add_optlist_pdu()* The *coap_add_optlist_pdu*() function sorts all of the entries in @@ -570,8 +584,8 @@ _PDU_ or NULL if there is a malloc or parameter failure. *coap_new_optlist*() returns a newly created _optlist_ or NULL if there is a malloc failure. -*coap_add_token*(), *coap_insert_optlist*(), *coap_add_optlist_pdu*() -and *coap_add_data*() return 0 on failure, 1 on success. +*coap_add_token*(), *coap_insert_optlist*(), *coap_sort_optlist*(), +*coap_add_optlist_pdu*() and *coap_add_data*() return 0 on failure, 1 on success. *coap_encode_var_safe*() returns either the length of bytes encoded (which can be 0 when encoding 0) or 0 on failure. diff --git a/src/coap_option.c b/src/coap_option.c index 70fce21d..334f542d 100644 --- a/src/coap_option.c +++ b/src/coap_option.c @@ -567,6 +567,16 @@ coap_add_optlist_pdu(coap_pdu_t *pdu, coap_optlist_t **options) { return 1; } +int +coap_sort_optlist(coap_optlist_t **options) { + if (options && *options) { + /* sort options for delta encoding */ + LL_SORT((*options), order_opts); + return 1; + } + return 0; +} + int coap_insert_optlist(coap_optlist_t **head, coap_optlist_t *node) { if (!node) {