Signal interrupt sendto/recvfrom on datagram socket - sockets

What happens if signal interrupts sendto or recvfrom call on datagram socket? Can I expect that these calls always return -1 with errno == EINTR or they can return positive number of bytes, but I shall repeat the call entirely?

On a datagram socket, sendto and recvfrom always send and receive complete datagrams, or nothing at all. If they could stop in the middle of a datagram that would defeat the point of datagrams.
There is an exception - recvfrom might cut off the end of a datagram if the buffer is too small. The entire datagram is received, but your program doesn't get to see all of it.

Related

(recv() == 0) means disconnected or timed out? (sockets, Linux&Windows)

I have set a timeout on my blocking socket ..
DWORD to = 1200;
if (setsockopt (soc, SOL_SOCKET, SO_RCVTIMEO, (char *)&to, sizeof(to))) {
...
}
In the case that recv () then returns zero, how can I tell this is link disconnected or read timed out? If it's t/o I would like to read more, if it's discon I would like to take other action. I realise I could just remove the t/o, then I would know it's discon, but I also need to routinely monitor how the read process is progressing.
Any help much appreciated. Cheers - Rich
From the SO_RCVTIMEO section of the man page for socket:
...if no data has been transferred and the time‐out has been reached,
then -1 is returned with errno set to EAGAIN or EWOULDBLOCK, or
EINPROGRESS (for connect(2)) just as if the socket was specified to
be non‐blocking.
From the man page for recv:
These calls return the number of bytes received, or -1 if an error
occurred. In the event of an error, errno is set to indicate the
error.
When a stream socket peer has performed an orderly shutdown, the
return value will be 0 (the traditional "end-of-file" return).
Datagram sockets in various domains (e.g., the UNIX and Internet
domains) permit zero-length datagrams. When such a datagram is
received, the return value is 0.
The value 0 may also be returned if the requested number of bytes to
receive from a stream socket was 0.
A call to recv will return 0 on disconnect, or if a zero-length datagram is received, or if the requested number of bytes is 0.
A call to recv will return -1 on any error, including a timeout. You need to examine errno to differentiate between a timeout versus some other error.

will poll() return immediately for UDP socket if there are data not read from last read()?

An existing program is written this way for UDP socket (in blocking mode):
while (true) {
poll();
if (POLLIN is set) {
read(fd, buf, bufSize);
}
}
For UDP, each read() read 1 and only 1 datagram (packet). If there are multiple packets available in the socket recv buf, the above code reads 1 packet only at each read(). My question is: will the next poll() return immediately, thus the above code still can read from socket very quickly? Or could next poll() wait until there is new packet arrives on the socket thus the code effectively falls behind in reading?
The doc seem to suggest that next poll() will return immediately as long as there are data in the buffer. But the code seem to fall behind in reading, and I don't know the cause is in the above code or somewhere else.
The preferred way is likely:
set the socket to non blocking
read in the loop until errno = EWOULDBLOCK or EAGAIN
Thanks.
If there is already data in the socket buffer when poll() is called, it should signal POLLIN immediately if POLLIN is requested, yes. It should not wait for the next packet to arrive in the buffer before signaling POLLIN.

TCP blocking socket - possibility of recv small amount of bytes in different packets

If the server sends 4 bytes
send(sock, buffer1, 4, 0);
And client waits for exactly 4 bytes
recv(sock, buffer2, 4, 0);
Is there a possibility that less than 4 bytes will be written to buffer2?
No other send or recv was made before.
If there is no possibility, what's the maximum size of buffer that send can do, so that recv could get the same buffer size in one call.
There's no such thing as a "message", except what you delimit yourself.
REPEAT: THERE IS NO SUCH THING AS A MESSAGE.
TCP does not send messages, it sends an octet stream.
You need to send in a loop, in case there is a backlog of unacknowledged data and send does not use the whole buffer you passed in. You need to recv in a loop, in case the sending stack chunked it in an unexpected way. You need to delimit your messages (eg by prepending a network-endian length), so you can recover them properly.
From the man page for recv:
"These calls return the number of bytes received, or -1 if an error occurred.
For TCP sockets, the return value 0 means the peer has closed its half side of the connection."
As such recv is always allowed return all of the bytes sent, fewer than the bytes sent, or none of the bytes sent. You cannot assume anything simply because you happen to know what send is doing at the other end of the connection.
There is no way to guarantee that you can always get complete message.
Yes, there's this possibility. In fact, recv gives you as return value the amount of bytes received.

recvfrom() only gets up to 2048 bytes from UDP socket

I have to call the function repeatedly to get all data, given that the len argument is set to 10240. But this results in blocking at last. How can I get all the data and safely return in a platform independent way?
BTW, I use netcat at the sender side:
cat ocr_pi.png | nc -u server 5555
Is this issue relative to nc's behavior? I didn't find any parameter to set UDP packet size(-O is for TCP).
Thanks.
UDP sends and receives data as messages. In the len argument, you tell recvfrom() the max message size you can receive, and then recvfrom() waits until a full message arrives, regardless of its size. UDP messages are self-contained. Unlike TCP, a UDP message cannot be partially sent/received. It is an all-or-nothing thing. If the size of the received message is greater than the len value you specify, the message is discarded and you get an error.
The only way recvfrom() blocks is if there is no message available to read. If you don't want to block, use select() (or pselect() or epoll or other platform equivalent) to specify a timeout to wait for a message to arrive, and then call recvfrom() only if there is actually something to read.

read() on a NON-BLOCKING tun/tap file descriptor gets EAGAIN error

I want to read IP packets from a non-blocking tun/tap file descriptor tunfd
I set the tunfd as non-blocking and register a READ_EV event for it in libevent.
when the event is triggered, I read the first 20 bytes first to get the IP header, and then
read the rest.
nr_bytes = read(tunfd, buf, 20);
...
ip_len = .... // here I get the IP length
....
nr_bytes = read(tunfd, buf+20, ip_len-20);
but for the read(tunfd, buf+20, ip_len-20)
I got EAGAIN error, actually there should be a full packet,
so there should be some bytes,
why I get such an error?
tunfd is not compatible with non-blocking mode or libevent?
thanks!
Reads and writes with TUN/TAP, much like reads and writes on datagram sockets, must be for complete packets. If you read into a buffer that is too small to fit a full packet, the buffer will be filled up and the rest of the packet will be discarded. For writes, if you write a partial packet, the driver will think it's a full packet and deliver the truncated packet through the tunnel device.
Therefore, when you read a TUN/TAP device, you must supply a buffer that is at least as large as the configured MTU on the tun or tap interface.