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

Plain UDP does not preserve packet borders #907

Closed
numo68 opened this issue Mar 23, 2018 · 3 comments
Closed

Plain UDP does not preserve packet borders #907

numo68 opened this issue Mar 23, 2018 · 3 comments

Comments

@numo68
Copy link

numo68 commented Mar 23, 2018

This issue is based just on looking at the 6.11 code while assessing of whether it is suitable for implementing a general UDP communication. I cannot exclude that I am overlooking something but I'll document the issue nevertheless.

One of the primary features of UDP is that it preserves the message borders - if I send a 1024 bytes packet, a 16384 one and a 384 one using three sendto calls, the other side gets those exact data in three subsequent recvfrom calls.

If I look into the mongoose code there is just one mbuf for the connection to send the data from. Two mg_sends without the underlying sendto (for whatever reason) will result in one packet containing data from both sends.

In the receiving direction the mg_handle_udp_read calls mg_recvfrom that uses a fixed buffer of a 1500 size and appends the data to a receiving mbuf. The code is completely unable to accept packets larger than a compile-time defined size, as the recvfrom on a datagram socket will silently truncate the excess data.

That means that the MG_F_UDP code is basically unusable for what the UDP communication is usually being used for, i.e. being able to send datagrams that are unreliable and might be out of order or duplicated, but a datagram is either completely received or not at all. If this is indeed the case I'd recommend to either fix it (e.g. by using linked mbufs, each containing one and only one packet, and a configurable buffer at the receiving side), or to remove the functionality completely, as it will only work for packets <= 1500 bytes being sent occasionally.

@jagoyette
Copy link

I stumbled across this problem also and submitted a pull request that resolves the issue (see #900). The fix uses MG_PEEK to increment the receive buffer size until the whole datagram can be read.

@cpq
Copy link
Member

cpq commented Dec 14, 2020

I guess a note in the documentation about the compile-time limit would do the job.

@cpq
Copy link
Member

cpq commented Dec 17, 2020

Closing this.

Added note and a link to this issue to https://cesanta.com/docs/#build-options

@cpq cpq closed this as completed Dec 17, 2020
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

No branches or pull requests

3 participants