c-ares 13.0.0

A Rust wrapper for the c-ares library, for asynchronous DNS requests.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use crate::error::{Error, Result};
use crate::string::{AresBuf, AresString};
use crate::types::AddressFamily;
use core::ffi::{c_char, c_int};
use std::ffi::CStr;
use std::mem;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
use std::str;

// Convert an address family into a more strongly typed AddressFamily.
pub fn address_family(family: c_types::ADDRESS_FAMILY) -> Option<AddressFamily> {
    match family {
        c_types::AF_INET => Some(AddressFamily::INET),
        c_types::AF_INET6 => Some(AddressFamily::INET6),
        c_types::AF_UNSPEC => Some(AddressFamily::UNSPEC),
        _ => None,
    }
}

// Get an in_addr from an Ipv4Addr.
#[cfg(unix)]
pub fn ipv4_as_in_addr(ipv4: Ipv4Addr) -> c_types::in_addr {
    c_types::in_addr {
        s_addr: u32::from(ipv4).to_be(),
    }
}

#[cfg(windows)]
pub fn ipv4_as_in_addr(ipv4: Ipv4Addr) -> c_types::in_addr {
    let octets = ipv4.octets();
    let mut in_addr: c_types::in_addr = unsafe { mem::zeroed() };
    in_addr.S_un.S_un_b.s_b1 = octets[0];
    in_addr.S_un.S_un_b.s_b2 = octets[1];
    in_addr.S_un.S_un_b.s_b3 = octets[2];
    in_addr.S_un.S_un_b.s_b4 = octets[3];
    in_addr
}

// Get an Ipv4Addr from an in_addr.
#[cfg(unix)]
pub fn ipv4_from_in_addr(in_addr: c_types::in_addr) -> Ipv4Addr {
    Ipv4Addr::from(u32::from_be(in_addr.s_addr))
}

#[cfg(windows)]
pub fn ipv4_from_in_addr(in_addr: c_types::in_addr) -> Ipv4Addr {
    let bytes = unsafe { in_addr.S_un.S_un_b };
    Ipv4Addr::new(bytes.s_b1, bytes.s_b2, bytes.s_b3, bytes.s_b4)
}

// Get an in6_addr from an Ipv6Addr.
#[cfg(unix)]
pub fn ipv6_as_in6_addr(ipv6: &Ipv6Addr) -> c_types::in6_addr {
    let octets = ipv6.octets();
    let mut in6_addr: c_types::in6_addr = unsafe { mem::zeroed() };
    in6_addr.s6_addr.copy_from_slice(&octets);
    in6_addr
}

#[cfg(windows)]
pub fn ipv6_as_in6_addr(ipv6: &Ipv6Addr) -> c_types::in6_addr {
    let octets = ipv6.octets();
    let mut in6_addr: c_types::in6_addr = unsafe { mem::zeroed() };
    unsafe { in6_addr.u.Byte.copy_from_slice(&octets) }
    in6_addr
}

// Get an Ipv6Addr from an in6_addr.
#[cfg(unix)]
pub fn ipv6_from_in6_addr(in6_addr: c_types::in6_addr) -> Ipv6Addr {
    Ipv6Addr::from(in6_addr.s6_addr)
}

#[cfg(windows)]
pub fn ipv6_from_in6_addr(in6_addr: c_types::in6_addr) -> Ipv6Addr {
    Ipv6Addr::from(unsafe { in6_addr.u.Byte })
}

// Get a sockaddr_in from a SocketAddrV4.
#[cfg(any(
    target_os = "macos",
    target_os = "ios",
    target_os = "freebsd",
    target_os = "dragonfly",
    target_os = "openbsd",
    target_os = "netbsd",
))]
#[allow(clippy::trivially_copy_pass_by_ref)] // mirrors the v6 helper signature
pub fn socket_addrv4_as_sockaddr_in(sock_v4: &SocketAddrV4) -> c_types::sockaddr_in {
    let in_addr = ipv4_as_in_addr(*sock_v4.ip());
    c_types::sockaddr_in {
        sin_len: mem::size_of::<c_types::sockaddr_in>() as u8,
        sin_family: c_types::AF_INET as c_types::sa_family_t,
        sin_port: sock_v4.port().to_be(),
        sin_addr: in_addr,
        sin_zero: [0; 8],
    }
}

#[cfg(not(any(
    target_os = "macos",
    target_os = "ios",
    target_os = "freebsd",
    target_os = "dragonfly",
    target_os = "openbsd",
    target_os = "netbsd",
)))]
#[allow(clippy::trivially_copy_pass_by_ref)] // mirrors the v6 helper signature
pub fn socket_addrv4_as_sockaddr_in(sock_v4: &SocketAddrV4) -> c_types::sockaddr_in {
    let in_addr = ipv4_as_in_addr(*sock_v4.ip());
    c_types::sockaddr_in {
        sin_family: c_types::AF_INET as c_types::sa_family_t,
        sin_port: sock_v4.port().to_be(),
        sin_addr: in_addr,
        sin_zero: [0; 8],
    }
}

// Get a sockaddr_in6 from a SocketAddrV6.
#[cfg(any(
    target_os = "macos",
    target_os = "ios",
    target_os = "freebsd",
    target_os = "dragonfly",
    target_os = "openbsd",
    target_os = "netbsd",
))]
pub fn socket_addrv6_as_sockaddr_in6(sock_v6: &SocketAddrV6) -> c_types::sockaddr_in6 {
    let in6_addr = ipv6_as_in6_addr(sock_v6.ip());
    c_types::sockaddr_in6 {
        sin6_len: mem::size_of::<c_types::sockaddr_in6>() as u8,
        sin6_family: c_types::AF_INET6 as c_types::sa_family_t,
        sin6_port: sock_v6.port().to_be(),
        sin6_addr: in6_addr,
        sin6_flowinfo: sock_v6.flowinfo(),
        sin6_scope_id: sock_v6.scope_id(),
    }
}

#[cfg(all(
    unix,
    not(any(
        target_os = "macos",
        target_os = "ios",
        target_os = "freebsd",
        target_os = "dragonfly",
        target_os = "openbsd",
        target_os = "netbsd",
    ))
))]
pub fn socket_addrv6_as_sockaddr_in6(sock_v6: &SocketAddrV6) -> c_types::sockaddr_in6 {
    let in6_addr = ipv6_as_in6_addr(sock_v6.ip());
    c_types::sockaddr_in6 {
        sin6_family: c_types::AF_INET6 as c_types::sa_family_t,
        sin6_port: sock_v6.port().to_be(),
        sin6_addr: in6_addr,
        sin6_flowinfo: sock_v6.flowinfo(),
        sin6_scope_id: sock_v6.scope_id(),
    }
}

#[cfg(windows)]
pub fn socket_addrv6_as_sockaddr_in6(sock_v6: &SocketAddrV6) -> c_types::sockaddr_in6 {
    let mut sockaddr_in6: c_types::sockaddr_in6 = unsafe { mem::zeroed() };
    sockaddr_in6.sin6_family = c_types::AF_INET6 as c_types::sa_family_t;
    sockaddr_in6.sin6_port = sock_v6.port().to_be();
    sockaddr_in6.sin6_addr = ipv6_as_in6_addr(sock_v6.ip());
    sockaddr_in6.sin6_flowinfo = sock_v6.flowinfo();
    sockaddr_in6.Anonymous.sin6_scope_id = sock_v6.scope_id();
    sockaddr_in6
}

// Get scope_id from a sockaddr_in6.
#[cfg(unix)]
pub fn sockaddr_in6_scope_id(sa: &c_types::sockaddr_in6) -> u32 {
    sa.sin6_scope_id
}

#[cfg(windows)]
pub fn sockaddr_in6_scope_id(sa: &c_types::sockaddr_in6) -> u32 {
    unsafe { sa.Anonymous.sin6_scope_id }
}

pub unsafe fn c_string_as_str_unchecked<'a>(c_str: *const c_char) -> &'a str {
    let bytes = unsafe { CStr::from_ptr(c_str) }.to_bytes();
    unsafe { str::from_utf8_unchecked(bytes) }
}

#[cfg(not(cares1_30))]
pub unsafe fn c_string_as_str_checked<'a>(c_str: *const c_char) -> &'a str {
    let c_str = unsafe { CStr::from_ptr(c_str) };
    c_str.to_str().unwrap()
}

pub unsafe fn hostname_as_str<'a>(hostname: *const c_char) -> &'a str {
    unsafe { c_string_as_str_unchecked(hostname) }
}

#[cfg(not(cares1_30))]
pub unsafe fn dns_string_as_str<'a>(hostname: *const c_char) -> &'a str {
    unsafe { c_string_as_str_checked(hostname) }
}

#[cfg(cares1_30)]
pub unsafe fn dns_string_as_str<'a>(hostname: *const c_char) -> &'a str {
    unsafe { c_string_as_str_unchecked(hostname) }
}

/// Helper to convert an `ares_status_t` to our `Result`.
pub fn status_to_result(status: c_ares_sys::ares_status_t) -> Result<()> {
    match Error::try_from(status) {
        Ok(err) => Err(err),
        Err(()) => Ok(()),
    }
}

/// Get the version number of the underlying `c-ares` library.
///
/// The version is returned as both a string and an integer.  The integer is built up as 24bit
/// number, with 8 separate bits used for major number, minor number and patch number.  For
/// example, the version string "1.2.3" is returned as hexadecimal number 0x010203 (decimal 66051).
///
/// # Examples
///
/// ```
/// let (version_str, version_int) = c_ares::version();
/// assert!(!version_str.is_empty());
/// assert!(version_int >= 0x011000); // at least 1.16.0
/// ```
pub fn version() -> (&'static str, u32) {
    let mut int_version: c_int = 0;
    let str_version = unsafe {
        let ptr = c_ares_sys::ares_version(&raw mut int_version);
        c_string_as_str_unchecked(ptr)
    };
    (str_version, int_version as u32)
}

/// Whether the underlying `c-ares` library was built with thread safety enabled or not.
///
/// This is unlikely to be of interest to users of this crate.  Our API assumes that c-ares was not
/// built with thread safety, and uses Rust's safety features to prevent errors.
#[cfg(cares1_23)]
pub fn thread_safety() -> bool {
    let safety = unsafe { c_ares_sys::ares_threadsafety() };
    safety != c_ares_sys::ares_bool_t::ARES_FALSE
}

/// Expand a DNS-encoded domain name from a DNS message.
///
/// The encoded name starts at `buf[offset]`.  Returns the expanded name and
/// the number of bytes consumed from the encoded input.
///
/// # Panics
///
/// Panics if `offset` is out of bounds.
pub fn expand_name(buf: &[u8], offset: usize) -> Result<(AresString, usize)> {
    let encoded = &buf[offset..];
    let mut s: *mut c_char = std::ptr::null_mut();
    let mut enclen: core::ffi::c_long = 0;
    let status = unsafe {
        c_ares_sys::ares_expand_name(
            encoded.as_ptr(),
            buf.as_ptr(),
            buf.len() as c_int,
            &raw mut s,
            &raw mut enclen,
        )
    };
    if status != c_ares_sys::ares_status_t::ARES_SUCCESS as i32 {
        return Err(Error::from(status));
    }
    Ok((AresString::new(s), enclen as usize))
}

/// Expand a single DNS-encoded string from a DNS message.
///
/// The encoded string starts at `buf[offset]`.  Returns the expanded string
/// as bytes and the number of bytes consumed from the encoded input.
///
/// # Panics
///
/// Panics if `offset` is out of bounds.
pub fn expand_string(buf: &[u8], offset: usize) -> Result<(AresBuf, usize)> {
    let encoded = &buf[offset..];
    let mut s: *mut core::ffi::c_uchar = std::ptr::null_mut();
    let mut enclen: core::ffi::c_long = 0;
    let status = unsafe {
        c_ares_sys::ares_expand_string(
            encoded.as_ptr(),
            buf.as_ptr(),
            buf.len() as c_int,
            &raw mut s,
            &raw mut enclen,
        )
    };
    if status != c_ares_sys::ares_status_t::ARES_SUCCESS as i32 {
        return Err(Error::from(status));
    }

    // enclen includes the 1-byte length prefix; the decoded data is the rest.
    // We cannot use CStr::from_ptr because the data may contain embedded nulls.
    debug_assert!(
        enclen >= 1,
        "ares_expand_string returned enclen < 1 on success"
    );
    let len = (enclen - 1) as usize;
    Ok((AresBuf::new(s, len), enclen as usize))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn version_returns_string_and_int() {
        let (version_str, version_int) = version();
        assert!(!version_str.is_empty());
        assert!(version_int > 0);
    }

    #[test]
    fn version_string_format() {
        let (version_str, _) = version();
        assert!(version_str.contains('.'));
    }

    #[test]
    fn version_int_format() {
        let (_, version_int) = version();
        assert!(version_int >= 0x010000);
    }

    #[cfg(cares1_23)]
    #[test]
    fn thread_safety_returns_bool() {
        let _safety = thread_safety();
    }

    #[test]
    fn address_family_inet() {
        let result = address_family(c_types::AF_INET);
        assert_eq!(result, Some(AddressFamily::INET));
    }

    #[test]
    fn address_family_inet6() {
        let result = address_family(c_types::AF_INET6);
        assert_eq!(result, Some(AddressFamily::INET6));
    }

    #[test]
    fn address_family_unspec() {
        let result = address_family(c_types::AF_UNSPEC);
        assert_eq!(result, Some(AddressFamily::UNSPEC));
    }

    #[test]
    fn address_family_unknown() {
        // Use an invalid address family value
        let result = address_family(255);
        assert_eq!(result, None);
    }

    #[test]
    fn expand_name_simple() {
        // A minimal DNS-like buffer containing the encoded name
        // "\x07example\x03com\x00" at offset 0.
        let buf: &[u8] = b"\x07example\x03com\x00";
        let (name, enclen) = expand_name(buf, 0).expect("expand_name");
        assert_eq!(&*name, "example.com");
        assert_eq!(enclen, buf.len());
    }

    #[test]
    fn expand_name_invalid() {
        // A truncated buffer should fail
        let buf: &[u8] = b"\x07exam";
        assert!(expand_name(buf, 0).is_err());
    }

    #[test]
    #[should_panic(expected = "out of range")]
    fn expand_name_out_of_bounds() {
        let buf: &[u8] = b"\x07example\x03com\x00";
        let _ = expand_name(buf, buf.len() + 1);
    }

    #[test]
    fn expand_string_simple() {
        // A length-prefixed string: \x05hello
        let buf: &[u8] = b"\x05hello";
        let (data, enclen) = expand_string(buf, 0).expect("expand_string");
        assert_eq!(&*data, b"hello");
        assert_eq!(enclen, buf.len());
    }

    #[test]
    fn expand_string_invalid() {
        // Length prefix says 10 but only 3 bytes follow
        let buf: &[u8] = b"\x0aabc";
        assert!(expand_string(buf, 0).is_err());
    }

    #[test]
    fn expand_string_embedded_null() {
        // Binary data with an embedded null: length 4, then "a\x00bc"
        let buf: &[u8] = b"\x04a\x00bc";
        let (data, enclen) = expand_string(buf, 0).expect("expand_string");
        assert_eq!(&*data, b"a\x00bc");
        assert_eq!(data.len(), 4);
        assert_eq!(enclen, buf.len());
    }

    #[test]
    #[should_panic(expected = "out of range")]
    fn expand_string_out_of_bounds() {
        let buf: &[u8] = b"\x05hello";
        let _ = expand_string(buf, buf.len() + 1);
    }
}