vector/sources/util/net/
udp.rs

1use std::io;
2
3use listenfd::ListenFd;
4use tokio::net::UdpSocket;
5
6use super::SocketListenAddr;
7
8/// Binds a UDP socket to the listen address.
9pub async fn try_bind_udp_socket(
10    addr: SocketListenAddr,
11    mut listenfd: ListenFd,
12) -> io::Result<UdpSocket> {
13    match addr {
14        SocketListenAddr::SocketAddr(addr) => UdpSocket::bind(&addr).await,
15        SocketListenAddr::SystemdFd(offset) => match listenfd.take_udp_socket(offset)? {
16            Some(socket) => UdpSocket::from_std(socket),
17            None => Err(io::Error::new(
18                io::ErrorKind::AddrInUse,
19                "systemd fd already consumed",
20            )),
21        },
22    }
23}