diff --git a/.github/workflows/example_0.13.yml b/.github/workflows/example_0.13.yml index 916a1082..aceba364 100644 --- a/.github/workflows/example_0.13.yml +++ b/.github/workflows/example_0.13.yml @@ -70,7 +70,7 @@ jobs: - name: UDP Socket Example run: | - cargo build --target wasm32-wasi --example=udp_socket --release --no-default-features --features=built-in-dns + cargo build --target wasm32-wasi --example=udp_socket --release wasmedge target/wasm32-wasi/release/examples/udp_socket.wasm - name: DNS Example diff --git a/Cargo.toml b/Cargo.toml index ddb8d913..9154d338 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,5 @@ rand = "0.8.5" [features] default = [] -built-in-dns = [] wasi_poll = [] epoll = [] diff --git a/src/lib.rs b/src/lib.rs index 5301b288..6edd2d1d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -318,8 +318,16 @@ impl AsRawFd for UdpSocket { } } -#[cfg(not(feature = "built-in-dns"))] pub fn nslookup(node: &str, service: &str) -> std::io::Result> { + let dns_server = std::env::var("DNS_SERVER"); + if let Ok(dns_server) = dns_server { + nslookup_with_dns_server(&dns_server, node, service) + } else { + nslookup_with_host(node, service) + } +} + +pub fn nslookup_with_host(node: &str, service: &str) -> std::io::Result> { use socket::WasiAddrinfo; let hints: WasiAddrinfo = WasiAddrinfo::default(); let mut sockaddrs = Vec::new(); @@ -367,11 +375,13 @@ pub fn nslookup(node: &str, service: &str) -> std::io::Result> { Ok(r_addrs) } -#[cfg(feature = "built-in-dns")] -pub fn nslookup(node: &str, _service: &str) -> std::io::Result> { - let dns_server = std::env::var("DNS_SERVER").unwrap_or("8.8.8.8:53".into()); +pub fn nslookup_with_dns_server( + dns_server: &str, + node: &str, + _service: &str, +) -> std::io::Result> { let mut conn = TcpStream::connect(dns_server)?; - let timeout = std::time::Duration::from_secs(1); + let timeout = std::time::Duration::from_secs(5); let _ignore = conn.as_mut().set_send_timeout(Some(timeout)); let _ignore = conn.as_mut().set_recv_timeout(Some(timeout)); diff --git a/src/socket.rs b/src/socket.rs index 3df9fdd1..7754ef1d 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -101,7 +101,6 @@ impl Default for WasiSockaddr { } } -#[cfg(not(feature = "built-in-dns"))] #[derive(Debug, Clone)] #[repr(C, packed(4))] pub struct WasiAddrinfo { @@ -116,7 +115,6 @@ pub struct WasiAddrinfo { pub ai_next: *mut WasiAddrinfo, } -#[cfg(not(feature = "built-in-dns"))] impl WasiAddrinfo { pub fn default() -> WasiAddrinfo { WasiAddrinfo {