keeperofdakeys / dns-lookup Goto Github PK
View Code? Open in Web Editor NEWA small libc::getaddrinfo wrapper for Rust to perform dns lookups.
License: Apache License 2.0
A small libc::getaddrinfo wrapper for Rust to perform dns lookups.
License: Apache License 2.0
Thanks for this library! I'm not sure why the standard library almost exposes this but then seems to go out of its way not to.
This is two combined issues:
family
hint is named address
which doesn't match the documentation for getaddrinfo and is ordered differently from the documentation. I was pretty worried this didn't support the family field or was based on some other function and I was confusing them.as_addrinfo
automatically do the into()
conversions would be easier and would result in a compile error if you accidentally swapped them.Compiling dns-lookup v1.0.8
error[E0308]: mismatched types
--> C:\Users\runneradmin.cargo\registry\src\github.com-1ecc6299db9ec823\dns-lookup-1.0.8\src\nameinfo.rs:47:9
|
47 | c_sock, c_sock_len,
| ^^^^^^ expected struct SOCKADDR
, found struct windows_sys::Windows::Win32::Networking::WinSock::SOCKADDR
|
= note: expected raw pointer *const SOCKADDR
found raw pointer *const windows_sys::Windows::Win32::Networking::WinSock::SOCKADDR
For more information about this error, try rustc --explain E0308
.
error: could not compile dns-lookup
due to previous error
warning: build failed, waiting for other jobs to finish...
error: build failed
After having read the documentation I still have some doubts.
When does lookup_addr()
exactly return an Err
? Has this function some kind of timeout?
I would like to use this function in a real time application and I'm wondering if it'd be better to launch this function on a separate thread waiting for its result, since DNS lookups can take up to some hundreds of milliseconds.
Why is the IP address obtained here a public address instead of a private address?
fn main() {
let hostname = hostname::get()
.expect("REASON")
.to_string_lossy()
.into_owned();
let service = "ssh";
let hints = AddrInfoHints {
socktype: 2,
flags: 0x0002,
..AddrInfoHints::default()
};
let sockets = getaddrinfo(Some(&hostname), Some(service), Some(hints))
.unwrap()
.collect::<std::io::Result<Vec<_>>>()
.unwrap();
for socket in sockets {
println!("{:?}", socket.sockaddr.ip()); //69.167.164.199
}
}
I'm porting something from Python that uses socket.getfqdn()
, which is basically gethostbyaddr(gethostname())
. It would be nice to be able to do this with this crate as well, without having to fall back to libc.
you've published 0.2.1 on crates.io, but the code here doesn't have all the commits.
looks like commit 59e87439ea17116476fafed1e53627bee26779c3
is causing freebsd builds to fail, i haven't had time to put together a pr, but i probably can soon.
I just pulled a request and coded a new Implement which name is Raw .
It will be specify DNS server and retrieve an array directly from server with Libc.
With a simple program on MacOs, I am running into the following problem:
fn main() {
dbg!(dns_lookup::lookup_addr(&"192.168.150.220".parse().unwrap()));
}
output
[src/main.rs:2] dns_lookup::lookup_addr(&"192.168.150.220".parse().unwrap()) = Err(
Custom {
kind: Other,
error: "failed to lookup address information: nodename nor servname provided, or not known",
},
)
Is there any known solution?
When exhausted AddrInfoIter
always continues to yield None
. Thus IWBNI it implemented FusedIterator
.
Currently the API is a single function to lookup host names (copied from the rust unstable api). A more useful api for a simple dns library should be designed.
The pull request from dependabot to upgrade dns-lookup to 1.0.4 fails with:
error: expected an item keyword
--> /home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/dns-lookup-1.0.4/src/lookup.rs:59:7
|
59 | unsafe {
| ^^^^^^
|
::: /home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/cfg-if-0.1.9/src/lib.rs:41:40
|
41 | if #[cfg($($meta:meta),*)] { $($it:item)* }
| -------- while parsing argument for this `item` macro fragment
error: aborting due to previous error
error: could not compile `dns-lookup`.
Not sure if this is because of other dependencies or why it happens. The corresponding pull request is here: adrianreber/mirrorlist-server#53
Is there anything I need to change in my project?
getnameinfo
and getaddrinfo
return an error of type LookupError
, which has a method kind()
which returns a LookupErrorKind
enum. Unfortunately, LookupErrorKind
is not made public, so it cannot be used.
Please add pub use err::LookupErrorKind
to lib.rs. Thanks :)
The init function is private, and seems to initialise data for Windows (src/libstd/sys/windows/net.rs). This, and more, may be required for full Window support.
OSX support is unknown.
After upgrading to dns-lookup 2.0.1, my software doesn't work on Alpine anymore. Having compiled the code below with
$ docker build --target foo-alpine -t foo-alpine . && docker build --target foo-debian -t foo-debian .
running docker run -it --rm foo-debian
executes correctly, printing both "before" and "after", while docker run -it --rm foo-alpine
crashes silently after "before".
src/main.rs
fn main() {
println!("before");
dns_lookup::lookup_host("127.0.0.1").ok();
println!("after");
}
Dockerfile
FROM rust:1.69.0-alpine3.17 AS foo-alpine
WORKDIR /app
COPY Cargo.* ./
COPY src/ ./src/
RUN cargo build --release
CMD ["/app/target/release/foo"]
FROM rust:1.69.0-slim-bookworm AS foo-debian
WORKDIR /app
COPY Cargo.* ./
COPY src/ ./src/
RUN cargo build --release
CMD ["/app/target/release/foo"]
Cargo.toml
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[dependencies]
dns-lookup = { version = "2.0.1", default-features = false }
Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "dns-lookup"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a5e05b775571a604d4bd5fa4afb2df8cb8f8f8f18dd1d8a4ba76ce8291aaa11"
dependencies = [
"cfg-if",
"libc",
"socket2",
"windows-sys",
]
[[package]]
name = "foo"
version = "0.1.0"
dependencies = [
"dns-lookup",
]
[[package]]
name = "libc"
version = "0.2.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
[[package]]
name = "socket2"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d283f86695ae989d1e18440a943880967156325ba025f05049946bff47bcc2b"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3"
[[package]]
name = "windows_i686_gnu"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241"
[[package]]
name = "windows_i686_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a"
When downgrading to dns-lookup 1.0.8, the code works as expected on both Alpine and Debian. You can try replacing the files above with the following and running the containers again.
Cargo.toml
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[dependencies]
dns-lookup = { version = "1.0.8", default-features = false }
Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "dns-lookup"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53ecafc952c4528d9b51a458d1a8904b81783feff9fde08ab6ed2545ff396872"
dependencies = [
"cfg-if",
"libc",
"socket2",
"winapi",
]
[[package]]
name = "foo"
version = "0.1.0"
dependencies = [
"dns-lookup",
]
[[package]]
name = "libc"
version = "0.2.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
[[package]]
name = "socket2"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64a4a911eed85daf18834cfaa86a79b7d266ff93ff5ba14005426219480ed662"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
Hello, how to distinguish between NXDOMAIN and SERVFAIL? Currently for both cases lookup_host
returns failed to lookup address information: Name or service not known
. For example bind9 tool named host
returns 2(SERVFAIL)
and 3(NXDOMAIN)
correctly.
Calling libc::res_init();
on DNS failure creates a mandatory dependency requirement on libresolv.tbd
for MacOS and IOS. Based on my testing this is not needed. I have created a test program to continuously make DNS request every second. Neither IOS nor MacOS versions have any problems when switching between different network modes (WIFI, Ethernet, Cellular).
Catting /etc/resolv.conf
on MacOS gives the following output:
#
# macOS Notice
#
# This file is not consulted for DNS hostname resolution, address
# resolution, or the DNS query routing mechanism used by most
# processes on this system.
#
# To view the DNS configuration used by this system, use:
# scutil --dns
#
# SEE ALSO
# dns-sd(1), scutil(8)
#
# This file is automatically generated.
#
nameserver 212.59.1.1
nameserver 8.8.8.8
I'm trying to build this for arm (aarch64-unknown-linux-gnu) and running into this error:
#0 10.60 error[E0308]: mismatched types
#0 10.60 --> /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/dns-lookup-2.0.1/src/addrinfo.rs:147:41
#0 10.60 |
#0 10.60 147 | .map(|s| CStr::from_ptr(*s as *mut i8).to_str().unwrap().to_owned()),
#0 10.60 | -------------- ^^^^^^^^^^^^^ expected `*const u8`, found `*mut i8`
#0 10.60 | |
#0 10.60 | arguments to this function are incorrect
#0 10.60 |
#0 10.60 = note: expected raw pointer `*const u8`
#0 10.60 found raw pointer `*mut i8`
#0 10.60 note: associated function defined here
#0 10.60 --> /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/ffi/c_str.rs:259:25
#0 10.60
#0 10.61 error[E0308]: mismatched types
#0 10.61 --> /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/dns-lookup-2.0.1/src/hostname.rs:27:38
#0 10.61 |
#0 10.61 27 | let res = unsafe { c_gethostname(c_name.as_mut_ptr(), c_name.len() as _) };
#0 10.61 | ------------- ^^^^^^^^^^^^^^^^^^^ expected `*mut u8`, found `*mut i8`
#0 10.61 | |
#0 10.61 | arguments to this function are incorrect
#0 10.61 |
#0 10.61 = note: expected raw pointer `*mut u8`
#0 10.61 found raw pointer `*mut i8`
#0 10.61 note: function defined here
#0 10.61 --> /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.144/src/unix/mod.rs:1285:12
#0 10.61 |
#0 10.61 1285 | pub fn gethostname(name: *mut ::c_char, len: ::size_t) -> ::c_int;
#0 10.61 | ^^^^^^^^^^^
#0 10.61
#0 10.61 error[E0308]: mismatched types
#0 10.61 --> /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/dns-lookup-2.0.1/src/hostname.rs:34:44
#0 10.61 |
#0 10.61 34 | let hostname = unsafe { CStr::from_ptr(c_name.as_ptr()) };
#0 10.61 | -------------- ^^^^^^^^^^^^^^^ expected `*const u8`, found `*const i8`
#0 10.61 | |
#0 10.61 | arguments to this function are incorrect
#0 10.61 |
#0 10.61 = note: expected raw pointer `*const u8`
#0 10.61 found raw pointer `*const i8`
#0 10.61 note: associated function defined here
#0 10.61 --> /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/ffi/c_str.rs:259:25
#0 10.61
#0 10.61 error[E0308]: arguments to this function are incorrect
#0 10.61 --> /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/dns-lookup-2.0.1/src/nameinfo.rs:61:38
#0 10.61 |
#0 10.61 61 | LookupError::match_gai_error(c_getnameinfo(
#0 10.61 | ^^^^^^^^^^^^^
#0 10.61 |
#0 10.61 note: expected `*mut u8`, found `*mut i8`
#0 10.61 --> /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/dns-lookup-2.0.1/src/nameinfo.rs:64:13
#0 10.61 |
#0 10.61 64 | c_host.as_mut_ptr(),
#0 10.61 | ^^^^^^^^^^^^^^^^^^^
#0 10.61 = note: expected raw pointer `*mut u8`
#0 10.61 found raw pointer `*mut i8`
#0 10.61 note: expected `*mut u8`, found `*mut i8`
#0 10.61 --> /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/dns-lookup-2.0.1/src/nameinfo.rs:66:13
#0 10.61 |
#0 10.61 66 | c_service.as_mut_ptr(),
#0 10.61 | ^^^^^^^^^^^^^^^^^^^^^^
#0 10.61 = note: expected raw pointer `*mut u8`
#0 10.61 found raw pointer `*mut i8`
#0 10.61 note: function defined here
#0 10.61 --> /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.144/src/unix/linux_like/linux/mod.rs:4114:20
#0 10.61 |
#0 10.61 4114 | pub fn getnameinfo(
#0 10.61 | ^^^^^^^^^^^
#0 10.61
#0 10.61 error[E0308]: mismatched types
#0 10.61 --> /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/dns-lookup-2.0.1/src/nameinfo.rs:72:40
#0 10.61 |
#0 10.61 72 | let host = unsafe { CStr::from_ptr(c_host.as_ptr()) };
#0 10.61 | -------------- ^^^^^^^^^^^^^^^ expected `*const u8`, found `*const i8`
#0 10.61 | |
#0 10.61 | arguments to this function are incorrect
#0 10.61 |
#0 10.61 = note: expected raw pointer `*const u8`
#0 10.61 found raw pointer `*const i8`
#0 10.61 note: associated function defined here
#0 10.61 --> /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/ffi/c_str.rs:259:25
#0 10.61
#0 10.61 error[E0308]: mismatched types
#0 10.61 --> /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/dns-lookup-2.0.1/src/nameinfo.rs:73:43
#0 10.61 |
#0 10.61 73 | let service = unsafe { CStr::from_ptr(c_service.as_ptr()) };
#0 10.61 | -------------- ^^^^^^^^^^^^^^^^^^ expected `*const u8`, found `*const i8`
#0 10.61 | |
#0 10.61 | arguments to this function are incorrect
#0 10.61 |
#0 10.61 = note: expected raw pointer `*const u8`
#0 10.61 found raw pointer `*const i8`
#0 10.61 note: associated function defined here
#0 10.61 --> /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/ffi/c_str.rs:259:25
The issue looks similar to fede1024/rust-rdkafka#121 so I suspect the code will have to change to fix arm compatibility for this crate as well.
Here's the Dockerfile I'm using to build
FROM rust:1.69-slim AS builder
WORKDIR /app
COPY . .
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/usr/local/cargo/git \
--mount=type=cache,target=/usr/local/rustup \
set -eux; \
rustup default stable; \
rustup target add aarch64-unknown-linux-gnu; \
cargo build --release --target=aarch64-unknown-linux-gnu
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.