On Fri, 4 Apr 2025 21:15:33 +1100 David Gibson <david(a)gibson.dropbear.id.au> wrote:udp_vu_sock_info() uses MSG_PEEK to look ahead at the next datagram to be received and gets its source address. Currently we only use it in the vhost-user path, but there's nothing inherently vhost-user specific about it. We have upcoming uses for it elsewhere so rename and move to udp.c. While we're there, polish its error reporting a litle. Signed-off-by: David Gibson <david(a)gibson.dropbear.id.au> --- udp.c | 25 +++++++++++++++++++++++++ udp_internal.h | 1 + udp_vu.c | 19 +------------------ 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/udp.c b/udp.c index 8125cfcb..6b72c30f 100644 --- a/udp.c +++ b/udp.c @@ -629,6 +629,31 @@ static int udp_sock_errs(const struct ctx *c, union epoll_ref ref) return n_err; } +/** + * udp_peek_addr() - Get source address for next packet + * @s: Socket to get information from + * @src: Socket address (output) + * + * Return: 0 on success, -1 otherwise + */ +int udp_peek_addr(int s, union sockaddr_inany *src) +{ + struct msghdr msg = { + .msg_name = src, + .msg_namelen = sizeof(*src), + }; + int rc; + + rc = recvmsg(s, &msg, MSG_PEEK | MSG_DONTWAIT); + if (rc < 0) { + if (errno != EAGAIN && errno != EWOULDBLOCK) + warn_perror("Error peeking at socket address"); + return rc; + } + return 0; +} + + /**Excess newline. Fixed on merge.* udp_sock_recv() - Receive datagrams from a socket * @c: Execution context-- Stefano