Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum buffer length allowed by the sendto function in C

Tags:

c

sockets

udp

I am implementing a simple network stack using UDP sockets and I wish to send about 1 MB of string data from the client to the server. However, I am not aware if there is a limit to the size of the length in the UDP sendto() API in C. If there is a limit and sendto() won't handle packetization beyond a limit then I would have to manually split the string into smaller blocks and then send them.

Is there a limit on the length of the buffer? Or does the sendto() API handle packetization by itself.

Any insight is appreciated.

like image 248
user2058668 Avatar asked Feb 13 '23 08:02

user2058668


1 Answers

There's no API limit on sendto -- it can handle any size the underlying protocol can.

There IS a packet limit for UDP -- 64K; if you exceed this, the sendto call will fail with an EMSGSIZE error code. There are also packet size limits for IP which differ between IPv4 and IPv6. Finally, the low level transport has an MTU size which may or may not be an issue. IP packets can be fragemented into multiple lower level packets and automatically reassembled, unless you've used an IP_OPTIONS setsockopt call to disable fragmentation.

The easiest way to deal with all this complexity is to make your code flexible -- detect EMSGSIZE errors from sendto and switch to using smaller messages if you get it. The also works well if you want to do path MTU discovery, which will generally accept larger messages at first, but will cut down the maximum message size when you send one that ends up exceeding the path MTU.

If you just want to avoid worrying about it, a send of 1452 bytes or less is likely to always be fine (that's the 1500 byte normal ethernet payload max minus 40 for a normal IPv6 header and 8 for a UDP header), unless you're using a VPN (in which case you need to worry about encapsulation overhead).

like image 169
Chris Dodd Avatar answered Mar 22 '23 21:03

Chris Dodd