coapjs / node-coap

CoAP - Node.js style
MIT License
528 stars 154 forks source link

Missing options in COAP response #365

Closed alfiedouglas-tewke closed 6 months ago

alfiedouglas-tewke commented 1 year ago

I am trying to add a Content-Format option to my COAP response, this seems to work when the data size is below 1280. But if it exceeds 1280 the data is received, but removes the added option. I have checked the response in wireshark and the option is there, so it must be where the packet is reconstructed on the client.

Working example with a small packet

CoapServer.on('request', (req: IncomingMessage, res: OutgoingMessage) => {
    const data = "a".repeat(10);
    res.code = COAP.HttpToCoapStatus(resp.status);
    res.setOption("Content-Format", 'application/cbor');
    res.end(CBOR.encode(data));
});

Capture

Here I loose the Content-Format option on the client, see screenshot:

CoapServer.on('request', (req: IncomingMessage, res: OutgoingMessage) => {
    const data = "a".repeat(1300);
    res.code = COAP.HttpToCoapStatus(resp.status);
    res.setOption("Content-Format", 'application/cbor');
    res.end(CBOR.encode(data));
});

Capture2

I assume this is to do with when the client receiving a blockwise transfer?

JKRhb commented 1 year ago

Hi @alfiedouglas-tewke, thank you for reporting this issue! I will try to look into it in the upcoming days and provide a fix as soon as possible.

JKRhb commented 1 year ago

Could you try out if #366 works for you as a workaround? After doing some investigation, the server side seemed to be the cause of the problem.

Apollon77 commented 1 year ago

@alfiedouglas-tewke An chance to verify the fix from your side?

alfiedouglas-tewke commented 1 year ago

Hi @JKRhb @Apollon77, thank so much for the fix! Unfortunately it did not solve the bug for me. When looking in Wireshark, the responses contain the options, so I think this is a bug on the client side?

JKRhb commented 1 year ago

Hi @alfiedouglas-tewke, thank you for your feedback :) It is a bit strange, since when I did the tests using adjusted versions of the client and server examples, the server options only appeared in the first Block2 message, but not in any message that followed (and the client then only used the last message's options to compose the response that got emitted in the end).

Could you maybe try to create a minimal, self-contained example (i.e., containing both client and server code) that reproduces the bug?

alfiedouglas-tewke commented 1 year ago

Hi @JKRhb, I have recreated it in this example. When I use payload2 I loose the option Content-Format option. I have attached the source and have used a copy of the branch https://github.com/coapjs/node-coap/tree/fix-block2-options as node-coap in this example.

Using (small) payload1: Capture

Using (large) payload2: Capture2

node-coap-bug-example.zip