From adcffa835a8eabda9e9e1ac5dc59597b2ea67747 Mon Sep 17 00:00:00 2001 From: Pete Skeggs Date: Sat, 28 Sep 2024 14:46:21 -0700 Subject: [PATCH] [nrf noup] net: lib: coap: Make use of ZSOCK_MSG_TRUNC configurable Not all offloaded network stacks support this socket option. Go back to previous behavior that it is not used, but allow it to be enabled using CONFIG_COAP_CLIENT_TRUNCATE_MSGS. Signed-off-by: Pete Skeggs (cherry picked from commit fddbbd1ed0458577fae792a5ace40bd1b9742753) --- subsys/net/lib/coap/Kconfig | 5 +++++ subsys/net/lib/coap/coap_client.c | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/subsys/net/lib/coap/Kconfig b/subsys/net/lib/coap/Kconfig index 61a5d2ab785..e41e80ef1d8 100644 --- a/subsys/net/lib/coap/Kconfig +++ b/subsys/net/lib/coap/Kconfig @@ -152,6 +152,11 @@ config COAP_CLIENT_MAX_REQUESTS help Maximum number of CoAP requests a single client can handle at a time +config COAP_CLIENT_TRUNCATE_MSGS + bool "Truncate blocks larger than the buffer size" + help + Include ZSOCK_MSG_TRUNC in flags passed to zsock_recvfrom() + endif # COAP_CLIENT config COAP_SERVER diff --git a/subsys/net/lib/coap/coap_client.c b/subsys/net/lib/coap/coap_client.c index c99a612cc88..7c49a90981b 100644 --- a/subsys/net/lib/coap/coap_client.c +++ b/subsys/net/lib/coap/coap_client.c @@ -552,14 +552,21 @@ static int recv_response(struct coap_client *client, struct coap_packet *respons int total_len; int available_len; int ret; + int flags = ZSOCK_MSG_DONTWAIT; + + if (IS_ENABLED(CONFIG_COAP_CLIENT_TRUNCATE_MSGS)) { + flags |= ZSOCK_MSG_TRUNC; + } memset(client->recv_buf, 0, sizeof(client->recv_buf)); total_len = receive(client->fd, client->recv_buf, sizeof(client->recv_buf), - ZSOCK_MSG_DONTWAIT | ZSOCK_MSG_TRUNC, &client->address, - &client->socklen); + flags, &client->address, &client->socklen); if (total_len < 0) { LOG_ERR("Error reading response: %d", errno); + if (errno == EOPNOTSUPP) { + return -errno; + } return -EINVAL; } else if (total_len == 0) { LOG_ERR("Zero length recv"); @@ -896,6 +903,10 @@ static void coap_client_recv(void *coap_cl, void *a, void *b) LOG_ERR("Error receiving response"); clients[i]->response_ready = false; k_mutex_unlock(&clients[i]->lock); + if (ret == -EOPNOTSUPP) { + LOG_ERR("Socket misconfigured."); + goto idle; + } continue; }