Skip to main content

turmoil/net/
mod.rs

1//! This module contains the simulated TCP/UDP networking types.
2//!
3//! They mirror [tokio::net](https://docs.rs/tokio/latest/tokio/net/) to provide
4//! a high fidelity implementation.
5
6use std::net::SocketAddr;
7use std::{io, iter};
8
9use crate::world::World;
10use crate::ToSocketAddrs;
11
12pub mod tcp;
13pub use tcp::{listener::TcpListener, stream::TcpStream};
14
15pub(crate) mod udp;
16pub use udp::UdpSocket;
17
18/// Performs a DNS resolution.
19///
20/// Must be called from within a turmoil simulation context.
21pub async fn lookup_host<T>(host: T) -> io::Result<impl Iterator<Item = SocketAddr>>
22where
23    T: ToSocketAddrs,
24{
25    let addr = World::current(|world| host.to_socket_addr(&world.dns))?;
26    Ok(iter::once(addr))
27}
28
29#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
30pub(crate) struct SocketPair {
31    pub(crate) local: SocketAddr,
32    pub(crate) remote: SocketAddr,
33}
34
35impl SocketPair {
36    pub(crate) fn new(local: SocketAddr, remote: SocketAddr) -> SocketPair {
37        assert_ne!(local, remote);
38        SocketPair { local, remote }
39    }
40}