zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.37k stars 6.35k forks source link

coap_server separate.c sample is reusing MID incorrectly #71620

Closed jvermillard closed 4 months ago

jvermillard commented 4 months ago

The net/socket/coap_server/seperate.c resource showing how to do separate coap response is reusing the MID between the request and the response, it should be different MID: image

To Reproduce Steps to reproduce the behavior:

  1. build the coap_server sample
  2. open wireshark in the background on port 5683
  3. With the libcoap CLI client issue: coap-client -m get coap://[IP of your device]:5683/separate

Expected behavior The response 2.05 and the following ACK should use a different MID

Impact Since the message reuses the MID, the peer implementation could discard them as spurious retransmissions.

jvermillard commented 4 months ago

looks like this solution works:

diff --git a/samples/net/sockets/coap_server/src/separate.c b/samples/net/sockets/coap_server/src/separate.c
index 68bba0bb47..7a5b1737cb 100644
--- a/samples/net/sockets/coap_server/src/separate.c
+++ b/samples/net/sockets/coap_server/src/separate.c
@@ -57,7 +57,7 @@ static int separate_get(struct coap_resource *resource,
        /* Re-use the buffer */
        r = coap_packet_init(&response, data, sizeof(data),
                             COAP_VERSION_1, type, tkl, token,
-                            COAP_RESPONSE_CODE_CONTENT, id);
+                            COAP_RESPONSE_CODE_CONTENT, coap_next_id());
        if (r < 0) {
                return r;
        }

image