zephyr: websocket: websocket_recv_msg breaks when there is no data after header

If there is no data after the header and the message length is 0, the next packet will be incorrectly analyzed, because payload data will be expected instead of the header. I tested it on zephyr 2.7.2, nothing has changed in version 3.2.99. https://github.com/zephyrproject-rtos/zephyr/blob/56664826b27c1b716483fbad35961450794c3089/subsys/net/lib/websocket/websocket.c#L847 I catch this error when I receive a ping message without payload data (for ping messages, payload data is not required [RFC6455]). I fixed it this way:

if (ctx->tmp_buf_pos == 0) {
	/* No data after the header, let the caller call
		* this function again to get the payload.
		*/
	if (ctx->message_len == 0) {
		ctx->header_received = false;
		ctx->message_len = 0;
		ctx->message_type = 0;
		ctx->total_read = 0;
	}
	return -EAGAIN;
}

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (15 by maintainers)

Commits related to this issue

Most upvoted comments

@rlubos, @jumo-sballing, @svenstorp I have written new receive algorithm. At first i has updated tests (add receiving empty ping and fix receivng two messages), so that this two test become failure. With new algorithm all tests pass. Also i have tested with real websocket-server (simple python script) using zephyr\samples\net\sockets\websocket_client with little changes (add ping waiting before sending).

You can see my updates in my repo. I does not formalize commit message yet. But you can test already. Note, websocket_recv_msg() can return 0 now, and it is no error, it is empty payload. -EAGAIN will be returned when timeout occured, -ENOTCONN – when socket close.

New FSM and flowchart.

WebSocket

@GrixaYrev Your proposal sounds ok, so please prepare a PR for it if you can.