Skip to main content

udp_connect

Function udp_connect 

Source
pub fn udp_connect(backend: SocketAddr) -> Result<UdpSocket, ServerBindError>
Expand description

Create a non-blocking connected per-flow upstream UDP socket toward backend.

The socket is bound to an ephemeral local port (family matched to the backend) and connect-ed to the backend address. A connected UDP socket “only receives from the connected address” (connect(2)), so its fd is the symmetric-NAT return-demux key for one flow: the shell registers upstream_token -> FlowId and feeds anything that arrives on it back into the manager as a BackendDatagram. send (not send_to) is then used for the forward path. Errors (EMFILE/ENFILE/connect refusal) bubble up so the caller can shed the flow rather than panic.

On Linux the socket is created non-blocking and close-on-exec in the one socket(2) call (SOCK_NONBLOCK | SOCK_CLOEXEC; Socket::new adds the latter), so a new flow costs socket, bind, connect and no fcntl(2). Other platforms keep the set_nonblocking round trip. The post-condition queries (fcntl(F_GETFL), getsockname(2), getpeername(2)) only feed debug_assert!s, so they are compiled out of release builds.