net: sockets: Implement MSG_TRUNC flag

Add implementation of MSG_TRUNC `recv()` flag for UDP sockets.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
This commit is contained in:
Robert Lubos 2021-03-19 12:24:15 +01:00 committed by Jukka Rissanen
parent e5023b045d
commit 833517f994
3 changed files with 10 additions and 5 deletions

View file

@ -55,6 +55,10 @@ struct zsock_pollfd {
/** zsock_recv: Read data without removing it from socket input queue */
#define ZSOCK_MSG_PEEK 0x02
/** zsock_recv: return the real length of the datagram, even when it was longer
* than the passed buffer
*/
#define ZSOCK_MSG_TRUNC 0x20
/** zsock_recv/zsock_send: Override operation to non-blocking */
#define ZSOCK_MSG_DONTWAIT 0x40
/** zsock_recv: block until the full amount of data can be returned */
@ -773,6 +777,7 @@ static inline char *inet_ntop(sa_family_t family, const void *src, char *dst,
#define POLLNVAL ZSOCK_POLLNVAL
#define MSG_PEEK ZSOCK_MSG_PEEK
#define MSG_TRUNC ZSOCK_MSG_TRUNC
#define MSG_DONTWAIT ZSOCK_MSG_DONTWAIT
#define MSG_WAITALL ZSOCK_MSG_WAITALL

View file

@ -28,6 +28,7 @@ static inline int socketpair(int family, int type, int proto, int sv[2])
#define SHUT_RDWR ZSOCK_SHUT_RDWR
#define MSG_PEEK ZSOCK_MSG_PEEK
#define MSG_TRUNC ZSOCK_MSG_TRUNC
#define MSG_DONTWAIT ZSOCK_MSG_DONTWAIT
#define MSG_WAITALL ZSOCK_MSG_WAITALL

View file

@ -934,6 +934,7 @@ static inline ssize_t zsock_recv_dgram(struct net_context *ctx,
{
k_timeout_t timeout = K_FOREVER;
size_t recv_len = 0;
size_t read_len;
struct net_pkt_cursor backup;
struct net_pkt *pkt;
@ -1007,11 +1008,9 @@ static inline ssize_t zsock_recv_dgram(struct net_context *ctx,
}
recv_len = net_pkt_remaining_data(pkt);
if (recv_len > max_len) {
recv_len = max_len;
}
read_len = MIN(recv_len, max_len);
if (net_pkt_read(pkt, buf, recv_len)) {
if (net_pkt_read(pkt, buf, read_len)) {
errno = ENOBUFS;
goto fail;
}
@ -1027,7 +1026,7 @@ static inline ssize_t zsock_recv_dgram(struct net_context *ctx,
net_pkt_cursor_restore(pkt, &backup);
}
return recv_len;
return (flags & ZSOCK_MSG_TRUNC) ? recv_len : read_len;
fail:
if (!(flags & ZSOCK_MSG_PEEK)) {