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; }