Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update MQTTSNPacket.c #192

Closed
wants to merge 1 commit into from
Closed

Conversation

davegish
Copy link

Looks like a bug in MQTTSNPacket_len(). If the input parameter is 255, it will return 256, but the actual packet size will be 258.

Looks like a bug in MQTTSNPacket_len(). If the input parameter is 255, it will return 256, but the actual packet size will be 258.
@martinkirsche
Copy link
Contributor

There is already a pull request containing this fix at #187 but unfortunately the maintainer reverted it within #189

@ty4tw
Copy link
Contributor

ty4tw commented Apr 12, 2020

Hi,

Could you explain why the actual length is 258?
According to the spec,

Messages with lengths smaller than 256 octets may use the shorter 1-octet format.

@martinkirsche
Copy link
Contributor

martinkirsche commented Apr 12, 2020

Could you explain why the actual length is 258?

The length will become 258 because ...

the output of the function MQTTSNPacket_len is fet into MQTTSNPacket_encode. So if I put 255 as the length of the packet without the length field into MQTTSNPacket_len it will currently return 256. But MQTTSNPacket_encode will use a 3-octed length field for such a message. The result is a MQTT-SN message that is longer (258 bytes) than its length field specifies (256 bytes).

See #187 for a demonstration of the effect.

@ty4tw
Copy link
Contributor

ty4tw commented Apr 13, 2020

Hi Martin,
It was a bug of GatewayTester and fixed.
MQTTSNPacket_encode() is correct.

/**
 * Encodes the MQTT-SN message length
 * @param buf the buffer into which the encoded data is written
 * @param length the length to be encoded
 * @return the number of bytes written to the buffer
 */
int MQTTSNPacket_encode(unsigned char* buf, int length)
{
	int rc = 0;

	FUNC_ENTRY;
	if (length > 255)
	{
		writeChar(&buf, 0x01);
		writeInt(&buf, length);
		rc += 3;
	}
	else
		buf[rc++] = length;

	FUNC_EXIT_RC(rc);
	return rc;
}

The specification defines the message length as follows:

5.2.1 Length
The Length field is either 1- or 3-octet long and specifies the total number of octets contained in the message (including the Length field itself).
If the first octet of the Length field is coded “0x01” then the Length field is 3-octet long; in this case, the two following octets specify the total number of octets of the message (most-significant octet first). Otherwise, the Length field is only 1-octet long and specifies itself the total number of octets contained in the message.
The 3-octet format allows the encoding of message lengths up to 65535 octets. Messages with lengths smaller than 256 octets may use the shorter 1-octet format.
Note that because MQTT-SN does not support message fragmentation and reassembly, the maximum message length that could be used in a network is governed by the maximum packet size that is supported by that network, and not by the maximum length that could be encoded by MQTT-SN.

@davegish
Copy link
Author

davegish commented Apr 13, 2020 via email

@ty4tw
Copy link
Contributor

ty4tw commented Apr 13, 2020

Hi davegish,
you are misunderstanding the length parameter.
@param length the length of the MQTT-SN packet without the length field
int MQTTSNPacket_len(int length);
int MQTTSNPacket_encode(unsigned char* buf, int length)
when the length is 255 MQTTSNPacket_len returns 256 and MQTTSNPacket_encode returns 1.
Nothing is wrong. see the definition of the message length in the specification.

return (length >*=* 255) ? length + 3 : length + 1;     

This returns 258 when the length is 255.

@davegish
Copy link
Author

davegish commented Apr 13, 2020 via email

@davegish
Copy link
Author

davegish commented Apr 13, 2020 via email

@ty4tw
Copy link
Contributor

ty4tw commented Apr 13, 2020

Hi,
Ok I understood.
I will fix the bugs.
Thank you for your cooperation.

ty4tw added a commit that referenced this pull request Apr 14, 2020
Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
@ty4tw ty4tw closed this Apr 15, 2020
@saumilsdk
Copy link

hi @ty4tw is that bug fixed in latest release? There are multiple PRs getting rejected and those fixes we are missing in our implementation. Can you please also apply those fixes and release a new version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants