Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

linux - 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 !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...