Skip to content

Commit

Permalink
[Lib] remove feature built-in-dns
Browse files Browse the repository at this point in the history
  • Loading branch information
L-jasmine authored and hydai committed Feb 26, 2024
1 parent 0d413b2 commit 2bd7d5c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/example_0.13.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ rand = "0.8.5"

[features]
default = []
built-in-dns = []
wasi_poll = []
epoll = []
20 changes: 15 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,16 @@ impl AsRawFd for UdpSocket {
}
}

#[cfg(not(feature = "built-in-dns"))]
pub fn nslookup(node: &str, service: &str) -> std::io::Result<Vec<SocketAddr>> {
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<Vec<SocketAddr>> {
use socket::WasiAddrinfo;
let hints: WasiAddrinfo = WasiAddrinfo::default();
let mut sockaddrs = Vec::new();
Expand Down Expand Up @@ -367,11 +375,13 @@ pub fn nslookup(node: &str, service: &str) -> std::io::Result<Vec<SocketAddr>> {
Ok(r_addrs)
}

#[cfg(feature = "built-in-dns")]
pub fn nslookup(node: &str, _service: &str) -> std::io::Result<Vec<SocketAddr>> {
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<Vec<SocketAddr>> {
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));

Expand Down
2 changes: 0 additions & 2 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ impl Default for WasiSockaddr {
}
}

#[cfg(not(feature = "built-in-dns"))]
#[derive(Debug, Clone)]
#[repr(C, packed(4))]
pub struct WasiAddrinfo {
Expand All @@ -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 {
Expand Down

0 comments on commit 2bd7d5c

Please sign in to comment.