clinuxsocketssendmsg

c sockets sendmsg MSG_DONTWAIT - buffer reuse


I'm using C Sockets to send ICMP packets with the MSG_DONTWAIT flag set. My program is single threaded but it expected to send messages at high frequency so I'm setting the message send as non blocking. Is it safe to share/modify/reuse the message buffer after each call ? (Unless EAGAIN or EWOULDBLOCK is returned).

msg_control (the ancillary data) is reused and msg_control->struct in_pktinfo->ipi_ifindex (outbound interface ifindex) is modified between calls.

The iov.iov_base buffer content (not pointer!) and iov.iov_len can also change between calls. (Less likely but still possible).

Is it OK to change ifinex and iov_base content between calsl at high frequency in non blocking mode ? (unless I get back EAGAIN or EWOULDBLOCK)

Thanks !


Solution

  • Yes, it's safe. On Linux, all the data you specify gets immediately copied into a buffer in the kernel, before send returns. If the kernel's buffer is full, it returns EAGAIN or EWOULDBLOCK (which are the same thing in Linux, apparently) and nothing happens. You don't have to worry that the kernel will go and send the packet later after you've changed the data in the buffer.

    On Windows, non-blocking "overlapped" operations do remember your buffer and use it later - so watch out for that if you ever do non-blocking I/O on Windows. (You'll know if you do, because it's totally different from blocking I/O)