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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
#[allow(unused_imports)]
use core::ffi::{c_char, c_int, c_void};
use std::ffi::CString;
use std::fmt;
use std::mem;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::ptr;
use std::sync::Arc;
use std::time::Duration;
mod options;
mod sockets;
pub use options::Options;
#[cfg(cares1_29)]
pub use options::ServerFailoverOptions;
pub use sockets::{Sockets, SocketsIter};
use options::SocketStateCallback;
#[cfg(cares1_29)]
use crate::ServerStateFlags;
use crate::a::AResults;
use crate::aaaa::AAAAResults;
use crate::addrinfo::{AddrInfoHints, AddrInfoResults, get_addrinfo_callback};
use crate::caa::CAAResults;
use crate::cname::CNameResults;
#[cfg(cares1_28)]
use crate::dns::callback::dnsrec_callback;
#[cfg(cares1_28)]
use crate::dns::{DnsCls, DnsRecord, DnsRecordType};
use crate::error::{Error, Result};
#[cfg(cares1_34)]
use crate::events::{FdEvents, ProcessFlags};
use crate::host::HostResults;
use crate::host::get_host_callback;
use crate::mx::MXResults;
use crate::nameinfo::{NameInfoResult, get_name_info_callback};
use crate::naptr::NAPTRResults;
use crate::ni_flags::NIFlags;
use crate::ns::NSResults;
use crate::panic;
use crate::ptr::PTRResults;
use crate::query::{query_callback, raw_query_callback};
use crate::record::QueryRecord;
use crate::soa::SOAResult;
use crate::srv::SRVResults;
#[cfg(cares1_24)]
use crate::string::AresString;
use crate::txt::TXTResults;
use crate::types::{AddressFamily, DnsClass, Socket};
use crate::uri::URIResults;
#[allow(unused_imports)]
use crate::utils::{
c_string_as_str_unchecked, ipv4_as_in_addr, ipv6_as_in6_addr, socket_addrv4_as_sockaddr_in,
socket_addrv6_as_sockaddr_in6, status_to_result,
};
use std::sync::Mutex;
// ares_library_init is not thread-safe, so we put a lock around it.
static ARES_LIBRARY_LOCK: Mutex<()> = Mutex::new(());
#[cfg(cares1_29)]
type ServerStateCallback = dyn Fn(&str, bool, ServerStateFlags) + Send + Sync + 'static;
#[cfg(cares1_34)]
type PendingWriteCallback = dyn Fn() + Send + Sync + 'static;
/// A channel for name service lookups.
pub struct Channel {
// The `ares_` prefix mirrors the FFI type name; the apparent name overlap
// with the struct is deliberate.
#[allow(clippy::struct_field_names)]
ares_channel: c_ares_sys::ares_channel,
// For ownership only.
socket_state_callback: Option<Arc<SocketStateCallback>>,
// For ownership only.
#[cfg(cares1_29)]
server_state_callback: Option<Arc<ServerStateCallback>>,
// For ownership only.
#[cfg(cares1_34)]
pending_write_callback: Option<Arc<PendingWriteCallback>>,
}
impl Channel {
/// Returns the raw `ares_channel` pointer.
///
/// # Safety
///
/// The returned pointer is only valid for the lifetime of this `Channel`.
/// Callers must ensure that any use of the pointer is compatible with
/// concurrent access (e.g. only call thread-safe c-ares functions).
pub fn as_raw(&self) -> c_ares_sys::ares_channel {
self.ares_channel
}
/// Create a new channel for name service lookups, with default `Options`.
///
/// # Examples
///
/// ```
/// let channel = c_ares::Channel::new().unwrap();
/// ```
pub fn new() -> Result<Self> {
let options = Options::default();
Self::with_options(options)
}
/// Create a new channel for name service lookups, with the given `Options`.
///
/// # Examples
///
/// ```
/// let mut options = c_ares::Options::new();
/// options.set_flags(c_ares::Flags::STAYOPEN)
/// .set_tries(2);
/// let channel = c_ares::Channel::with_options(options).unwrap();
/// ```
pub fn with_options(mut options: Options) -> Result<Channel> {
// Initialize the library.
let lib_rc = {
let _lock = ARES_LIBRARY_LOCK.lock().unwrap();
unsafe { c_ares_sys::ares_library_init(c_ares_sys::ARES_LIB_INIT_ALL) }
};
if lib_rc != c_ares_sys::ares_status_t::ARES_SUCCESS as i32 {
return Err(Error::from(lib_rc));
}
// We deferred setting up domains in the options - do it now.
let domains: Vec<_> = options.domains.iter().map(|s| s.as_ptr()).collect();
options.ares_options.domains = domains.as_ptr().cast_mut().cast();
options.ares_options.ndomains = domains.len() as i32;
// Likewise for lookups.
if let Some(c_lookup) = &options.lookups {
options.ares_options.lookups = c_lookup.as_ptr().cast_mut();
}
// And the resolvconf_path.
if let Some(c_resolvconf_path) = &options.resolvconf_path {
options.ares_options.resolvconf_path = c_resolvconf_path.as_ptr().cast_mut();
}
// And the hosts_path.
#[cfg(cares1_19)]
if let Some(c_hosts_path) = &options.hosts_path {
options.ares_options.hosts_path = c_hosts_path.as_ptr().cast_mut();
}
// Initialize the channel.
let mut ares_channel = ptr::null_mut();
let channel_rc = unsafe {
c_ares_sys::ares_init_options(
&raw mut ares_channel,
&raw const options.ares_options,
options.optmask,
)
};
if channel_rc != c_ares_sys::ares_status_t::ARES_SUCCESS as i32 {
let _lock = ARES_LIBRARY_LOCK.lock().unwrap();
unsafe { c_ares_sys::ares_library_cleanup() }
return Err(Error::from(channel_rc));
}
let channel = Channel {
ares_channel,
socket_state_callback: options.socket_state_callback,
#[cfg(cares1_29)]
server_state_callback: None,
#[cfg(cares1_34)]
pending_write_callback: None,
};
Ok(channel)
}
/// Reinitialize a channel from system configuration.
#[cfg(cares1_22)]
pub fn reinit(&mut self) -> Result<&mut Self> {
let rc = unsafe { c_ares_sys::ares_reinit(self.ares_channel) };
status_to_result(rc)?;
Ok(self)
}
/// Duplicate a channel.
pub fn try_clone(&self) -> Result<Channel> {
// Balance the ares_library_cleanup() that will run when the clone is dropped.
let lib_rc = {
let _lock = ARES_LIBRARY_LOCK.lock().unwrap();
unsafe { c_ares_sys::ares_library_init(c_ares_sys::ARES_LIB_INIT_ALL) }
};
if lib_rc != c_ares_sys::ares_status_t::ARES_SUCCESS as i32 {
return Err(Error::from(lib_rc));
}
let mut ares_channel = ptr::null_mut();
let rc = unsafe { c_ares_sys::ares_dup(&raw mut ares_channel, self.ares_channel) };
if rc != c_ares_sys::ares_status_t::ARES_SUCCESS as i32 {
let _lock = ARES_LIBRARY_LOCK.lock().unwrap();
unsafe { c_ares_sys::ares_library_cleanup() }
return Err(Error::from(rc));
}
let socket_state_callback = self.socket_state_callback.clone();
#[cfg(cares1_29)]
let server_state_callback = self.server_state_callback.clone();
#[cfg(cares1_34)]
let pending_write_callback = self.pending_write_callback.clone();
let channel = Channel {
ares_channel,
socket_state_callback,
#[cfg(cares1_29)]
server_state_callback,
#[cfg(cares1_34)]
pending_write_callback,
};
Ok(channel)
}
/// Handle input, output, and timeout events associated with the specified file descriptors
/// (sockets).
///
/// Providing a value for `read_fd` indicates that the identified socket is readable; likewise
/// providing a value for `write_fd` indicates that the identified socket is writable. Use
/// `None` for "no action".
pub fn process_fd(&mut self, read_fd: Option<Socket>, write_fd: Option<Socket>) {
let rfd = read_fd.unwrap_or(c_ares_sys::ARES_SOCKET_BAD);
let wfd = write_fd.unwrap_or(c_ares_sys::ARES_SOCKET_BAD);
unsafe { c_ares_sys::ares_process_fd(self.ares_channel, rfd, wfd) }
}
/// Handle input and output events associated with the specified file descriptors (sockets).
/// Also handles timeouts associated with the `Channel`.
pub fn process(&mut self, read_fds: &mut c_types::fd_set, write_fds: &mut c_types::fd_set) {
unsafe { c_ares_sys::ares_process(self.ares_channel, read_fds, write_fds) }
}
/// Process events on multiple file descriptors based on the event mask associated with each
/// file descriptor. Recommended over calling `process_fd()` multiple times since it would
/// trigger additional logic such as timeout processing on each call.
#[cfg(cares1_34)]
pub fn process_fds(&mut self, events: &[FdEvents], flags: ProcessFlags) -> Result<()> {
let rc = unsafe {
c_ares_sys::ares_process_fds(
self.ares_channel,
events.as_ptr().cast(),
events.len(),
flags.bits(),
)
};
status_to_result(rc)
}
/// Retrieve the set of socket descriptors which the calling application should wait on for
/// reading and / or writing.
///
/// # Examples
///
/// ```
/// let channel = c_ares::Channel::new().unwrap();
/// for (socket, readable, writable) in &channel.sockets() {
/// println!("socket {socket}: read={readable}, write={writable}");
/// }
/// ```
pub fn sockets(&self) -> Sockets {
let mut socks = [0; c_ares_sys::ARES_GETSOCK_MAXNUM];
let bitmask = unsafe {
c_ares_sys::ares_getsock(
self.ares_channel,
socks.as_mut_ptr(),
c_ares_sys::ARES_GETSOCK_MAXNUM as i32,
)
};
Sockets::new(socks, bitmask as u32)
}
/// Retrieve the set of socket descriptors which the calling application should wait on for
/// reading and / or writing.
pub fn fds(&self, read_fds: &mut c_types::fd_set, write_fds: &mut c_types::fd_set) -> u32 {
unsafe { c_ares_sys::ares_fds(self.ares_channel, read_fds, write_fds) as u32 }
}
/// Set the list of servers to contact, instead of the servers specified in resolv.conf or the
/// local named.
///
/// String format is `host[:port]`. IPv6 addresses with ports require square brackets eg
/// `[2001:4860:4860::8888]:53`.
///
/// # Examples
///
/// ```
/// let mut channel = c_ares::Channel::new().unwrap();
/// channel.set_servers(&["8.8.8.8", "8.8.4.4:53"]).unwrap();
/// ```
pub fn set_servers<I, S>(&mut self, servers: I) -> Result<&mut Self>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut servers_csv = String::new();
for (i, s) in servers.into_iter().enumerate() {
if i > 0 {
servers_csv.push(',');
}
servers_csv.push_str(s.as_ref());
}
let c_servers = CString::new(servers_csv).map_err(|_| Error::EBADSTR)?;
let ares_rc = unsafe {
c_ares_sys::ares_set_servers_ports_csv(self.ares_channel, c_servers.as_ptr())
};
if ares_rc == c_ares_sys::ares_status_t::ARES_SUCCESS as i32 {
Ok(self)
} else {
Err(Error::from(ares_rc))
}
}
/// Retrieves the list of configured servers.
///
/// Each entry is in `host[:port]` format, matching what [`set_servers`](Self::set_servers)
/// accepts.
#[cfg(cares1_24)]
pub fn servers(&self) -> Vec<String> {
let csv = unsafe { c_ares_sys::ares_get_servers_csv(self.ares_channel) };
Self::parse_servers_csv(csv)
}
// Parse the result of `ares_get_servers_csv()` into a list of servers,
// taking ownership of (and freeing) the c-ares-allocated string.
#[cfg(cares1_24)]
fn parse_servers_csv(csv: *mut c_char) -> Vec<String> {
// `ares_get_servers_csv()` returns NULL on allocation failure.
if csv.is_null() {
return Vec::new();
}
let csv = AresString::new(csv);
let csv: &str = &csv;
if csv.is_empty() {
Vec::new()
} else {
csv.split(',').map(String::from).collect()
}
}
/// Set the local IPv4 address from which to make queries.
pub fn set_local_ipv4(&mut self, ipv4: Ipv4Addr) -> &mut Self {
unsafe { c_ares_sys::ares_set_local_ip4(self.ares_channel, u32::from(ipv4)) }
self
}
/// Set the local IPv6 address from which to make queries.
pub fn set_local_ipv6(&mut self, ipv6: Ipv6Addr) -> &mut Self {
let in6_addr = ipv6_as_in6_addr(&ipv6);
unsafe {
c_ares_sys::ares_set_local_ip6(self.ares_channel, ptr::from_ref(&in6_addr).cast());
}
self
}
/// Set the local device from which to make queries.
pub fn set_local_device(&mut self, device: &str) -> Result<&mut Self> {
let c_dev = CString::new(device).map_err(|_| Error::EBADSTR)?;
unsafe { c_ares_sys::ares_set_local_dev(self.ares_channel, c_dev.as_ptr()) }
Ok(self)
}
/// Initializes an address sortlist configuration, so that addresses returned by
/// `get_host_by_name()` are sorted according to the sortlist.
///
/// Each element of the sortlist holds an IP-address/netmask pair. The netmask is optional but
/// follows the address after a slash if present. For example: "130.155.160.0/255.255.240.0",
/// or "130.155.0.0".
pub fn set_sortlist<I, S>(&mut self, sortlist: I) -> Result<&mut Self>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut sortlist_str = String::new();
for (i, s) in sortlist.into_iter().enumerate() {
if i > 0 {
sortlist_str.push(' ');
}
sortlist_str.push_str(s.as_ref());
}
let c_sortlist = CString::new(sortlist_str).map_err(|_| Error::EBADSTR)?;
let ares_rc =
unsafe { c_ares_sys::ares_set_sortlist(self.ares_channel, c_sortlist.as_ptr()) };
if ares_rc == c_ares_sys::ares_status_t::ARES_SUCCESS as i32 {
Ok(self)
} else {
Err(Error::from(ares_rc))
}
}
/// Set a callback function to be invoked whenever a query on the channel completes.
///
/// `callback(server, success, flags)` will be called when a query completes.
///
/// - `server` indicates the DNS server that was used for the query.
/// - `success` indicates whether the query succeeded or not.
/// - `flags` is a bitmask of flags describing various aspects of the query.
#[cfg(cares1_29)]
pub fn set_server_state_callback<F>(&mut self, callback: F) -> &mut Self
where
F: Fn(&str, bool, ServerStateFlags) + Send + Sync + 'static,
{
let boxed_callback = Arc::new(callback);
let data = Arc::as_ptr(&boxed_callback).cast_mut().cast();
unsafe {
c_ares_sys::ares_set_server_state_callback(
self.ares_channel,
Some(server_state_callback::<F>),
data,
);
}
self.server_state_callback = Some(boxed_callback);
self
}
/// Set a callback function to be invoked when there is potential pending data
/// which needs to be written.
#[cfg(cares1_34)]
pub fn set_pending_write_callback<F>(&mut self, callback: F) -> &mut Self
where
F: Fn() + Send + Sync + 'static,
{
let boxed_callback = Arc::new(callback);
let data = Arc::as_ptr(&boxed_callback).cast_mut().cast();
unsafe {
c_ares_sys::ares_set_pending_write_cb(
self.ares_channel,
Some(pending_write_callback::<F>),
data,
);
}
self.pending_write_callback = Some(boxed_callback);
self
}
/// Initiate a single-question DNS query for the A records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn query_a<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<AResults>) + Send + 'static,
{
self.do_query(name, handler);
}
/// Initiate a series of single-question DNS queries for the A records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn search_a<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<AResults>) + Send + 'static,
{
self.do_search(name, handler);
}
/// Initiate a single-question DNS query for the AAAA records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn query_aaaa<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<AAAAResults>) + Send + 'static,
{
self.do_query(name, handler);
}
/// Initiate a series of single-question DNS queries for the AAAA records associated with
/// `name`.
///
/// On completion, `handler` is called with the result.
pub fn search_aaaa<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<AAAAResults>) + Send + 'static,
{
self.do_search(name, handler);
}
/// Initiate a single-question DNS query for the CAA records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn query_caa<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<CAAResults>) + Send + 'static,
{
self.do_query(name, handler);
}
/// Initiate a series of single-question DNS queries for the CAA records associated with
/// `name`.
///
/// On completion, `handler` is called with the result.
pub fn search_caa<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<CAAResults>) + Send + 'static,
{
self.do_search(name, handler);
}
/// Initiate a single-question DNS query for the CNAME records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn query_cname<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<CNameResults>) + Send + 'static,
{
self.do_query(name, handler);
}
/// Initiate a series of single-question DNS queries for the CNAME records associated with
/// `name`.
///
/// On completion, `handler` is called with the result.
pub fn search_cname<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<CNameResults>) + Send + 'static,
{
self.do_search(name, handler);
}
/// Initiate a single-question DNS query for the MX records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn query_mx<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<MXResults>) + Send + 'static,
{
self.do_query(name, handler);
}
/// Initiate a series of single-question DNS queries for the MX records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn search_mx<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<MXResults>) + Send + 'static,
{
self.do_search(name, handler);
}
/// Initiate a single-question DNS query for the NAPTR records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn query_naptr<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<NAPTRResults>) + Send + 'static,
{
self.do_query(name, handler);
}
/// Initiate a series of single-question DNS queries for the NAPTR records associated with
/// `name`.
///
/// On completion, `handler` is called with the result.
pub fn search_naptr<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<NAPTRResults>) + Send + 'static,
{
self.do_search(name, handler);
}
/// Initiate a single-question DNS query for the NS records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn query_ns<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<NSResults>) + Send + 'static,
{
self.do_query(name, handler);
}
/// Initiate a series of single-question DNS queries for the NS records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn search_ns<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<NSResults>) + Send + 'static,
{
self.do_search(name, handler);
}
/// Initiate a single-question DNS query for the PTR records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn query_ptr<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<PTRResults>) + Send + 'static,
{
self.do_query(name, handler);
}
/// Initiate a series of single-question DNS queries for the PTR records associated with
/// `name`.
///
/// On completion, `handler` is called with the result.
pub fn search_ptr<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<PTRResults>) + Send + 'static,
{
self.do_search(name, handler);
}
/// Initiate a single-question DNS query for the SOA records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn query_soa<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<SOAResult>) + Send + 'static,
{
self.do_query(name, handler);
}
/// Initiate a series of single-question DNS queries for the SOA records associated with
/// `name`.
///
/// On completion, `handler` is called with the result.
pub fn search_soa<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<SOAResult>) + Send + 'static,
{
self.do_search(name, handler);
}
/// Initiate a single-question DNS query for the SRV records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn query_srv<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<SRVResults>) + Send + 'static,
{
self.do_query(name, handler);
}
/// Initiate a series of single-question DNS queries for the SRV records associated with
/// `name`.
///
/// On completion, `handler` is called with the result.
pub fn search_srv<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<SRVResults>) + Send + 'static,
{
self.do_search(name, handler);
}
/// Initiate a single-question DNS query for the TXT records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn query_txt<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<TXTResults>) + Send + 'static,
{
self.do_query(name, handler);
}
/// Initiate a series of single-question DNS queries for the TXT records associated with
/// `name`.
///
/// On completion, `handler` is called with the result.
pub fn search_txt<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<TXTResults>) + Send + 'static,
{
self.do_search(name, handler);
}
/// Initiate a single-question DNS query for the URI records associated with `name`.
///
/// On completion, `handler` is called with the result.
pub fn query_uri<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<URIResults>) + Send + 'static,
{
self.do_query(name, handler);
}
/// Initiate a series of single-question DNS queries for the URI records associated with
/// `name`.
///
/// On completion, `handler` is called with the result.
pub fn search_uri<F>(&mut self, name: &str, handler: F)
where
F: FnOnce(Result<URIResults>) + Send + 'static,
{
self.do_search(name, handler);
}
fn do_query<R, F>(&mut self, name: &str, handler: F)
where
R: QueryRecord,
F: FnOnce(Result<R>) + Send + 'static,
{
ares_query!(
self.ares_channel,
name,
DnsClass::IN,
R::QUERY_TYPE,
query_callback::<R, F>,
handler
);
}
fn do_search<R, F>(&mut self, name: &str, handler: F)
where
R: QueryRecord,
F: FnOnce(Result<R>) + Send + 'static,
{
ares_search!(
self.ares_channel,
name,
DnsClass::IN,
R::QUERY_TYPE,
query_callback::<R, F>,
handler
);
}
/// Perform a host query by address.
///
/// On completion, `handler` is called with the result.
// `in_addr` and `in6_addr` mirror the C struct names; the apparent
// similarity is deliberate.
#[allow(clippy::similar_names)]
pub fn get_host_by_address<F>(&mut self, address: &IpAddr, handler: F)
where
F: FnOnce(Result<&HostResults>) + Send + 'static,
{
let in_addr: c_types::in_addr;
let in6_addr: c_types::in6_addr;
let c_addr = match *address {
IpAddr::V4(v4) => {
in_addr = ipv4_as_in_addr(v4);
ptr::from_ref(&in_addr).cast()
}
IpAddr::V6(ref v6) => {
in6_addr = ipv6_as_in6_addr(v6);
ptr::from_ref(&in6_addr).cast()
}
};
let (family, length) = match *address {
IpAddr::V4(_) => (AddressFamily::INET, mem::size_of::<c_types::in_addr>()),
IpAddr::V6(_) => (AddressFamily::INET6, mem::size_of::<c_types::in6_addr>()),
};
let c_arg = Box::into_raw(Box::new(handler));
unsafe {
c_ares_sys::ares_gethostbyaddr(
self.ares_channel,
c_addr,
length as i32,
family as i32,
Some(get_host_callback::<F>),
c_arg.cast(),
);
}
}
/// Perform a host query by name.
///
/// On completion, `handler` is called with the result.
pub fn get_host_by_name<F>(&mut self, name: &str, family: AddressFamily, handler: F)
where
F: FnOnce(Result<&HostResults>) + Send + 'static,
{
let Ok(c_name) = CString::new(name) else {
handler(Err(Error::EBADNAME));
return;
};
let c_arg = Box::into_raw(Box::new(handler));
unsafe {
c_ares_sys::ares_gethostbyname(
self.ares_channel,
c_name.as_ptr(),
family as i32,
Some(get_host_callback::<F>),
c_arg.cast(),
);
}
}
/// Address-to-nodename translation in protocol-independent manner.
///
/// The valid values for `flags` are documented [here](ni_flags/index.html).
///
/// On completion, `handler` is called with the result.
pub fn get_name_info<F>(&mut self, address: &SocketAddr, flags: NIFlags, handler: F)
where
F: FnOnce(Result<NameInfoResult>) + Send + 'static,
{
let sockaddr_in: c_types::sockaddr_in;
let sockaddr_in6: c_types::sockaddr_in6;
let c_addr = match *address {
SocketAddr::V4(ref v4) => {
sockaddr_in = socket_addrv4_as_sockaddr_in(v4);
ptr::from_ref(&sockaddr_in).cast()
}
SocketAddr::V6(ref v6) => {
sockaddr_in6 = socket_addrv6_as_sockaddr_in6(v6);
ptr::from_ref(&sockaddr_in6).cast()
}
};
let length = match *address {
SocketAddr::V4(_) => mem::size_of::<c_types::sockaddr_in>(),
SocketAddr::V6(_) => mem::size_of::<c_types::sockaddr_in6>(),
};
let c_arg = Box::into_raw(Box::new(handler));
unsafe {
c_ares_sys::ares_getnameinfo(
self.ares_channel,
c_addr,
length as c_ares_sys::ares_socklen_t,
flags.bits(),
Some(get_name_info_callback::<F>),
c_arg.cast(),
);
}
}
/// Initiate a host query by name and service.
///
/// The `hints` parameter controls the desired address family, socket type, protocol, and
/// behaviour flags.
///
/// On completion, `handler` is called with the result.
pub fn get_addrinfo<F>(
&mut self,
name: &str,
service: Option<&str>,
hints: &AddrInfoHints,
handler: F,
) where
F: FnOnce(Result<AddrInfoResults>) + Send + 'static,
{
let Ok(c_name) = CString::new(name) else {
handler(Err(Error::EBADNAME));
return;
};
let Ok(c_service) = service.map(CString::new).transpose() else {
handler(Err(Error::EBADNAME));
return;
};
let c_hints: c_ares_sys::ares_addrinfo_hints = hints.into();
let c_arg = Box::into_raw(Box::new(handler));
unsafe {
c_ares_sys::ares_getaddrinfo(
self.ares_channel,
c_name.as_ptr(),
c_service.as_ref().map_or(ptr::null(), |s| s.as_ptr()),
&raw const c_hints,
Some(get_addrinfo_callback::<F>),
c_arg.cast(),
);
}
}
/// Initiate a single-question DNS query for `name`. The class and type of the query are per
/// the provided parameters, taking values as defined in `arpa/nameser.h`.
///
/// On completion, `handler` is called with the result.
///
/// This method is provided so that users can query DNS types for which `c-ares` does not
/// provide a parser. This is expected to be a last resort; if a suitable `query_xxx()` is
/// available, that should be preferred.
pub fn query<F>(&mut self, name: &str, dns_class: u16, query_type: u16, handler: F)
where
F: FnOnce(Result<&[u8]>) + Send + 'static,
{
ares_query!(
self.ares_channel,
name,
c_int::from(dns_class),
c_int::from(query_type),
raw_query_callback::<F>,
handler
);
}
/// Initiate a series of single-question DNS queries for `name`. The class and type of the
/// query are per the provided parameters, taking values as defined in `arpa/nameser.h`.
///
/// On completion, `handler` is called with the result.
///
/// This method is provided so that users can search DNS types for which `c-ares` does not
/// provide a parser. This is expected to be a last resort; if a suitable `search_xxx()` is
/// available, that should be preferred.
pub fn search<F>(&mut self, name: &str, dns_class: u16, query_type: u16, handler: F)
where
F: FnOnce(Result<&[u8]>) + Send + 'static,
{
ares_search!(
self.ares_channel,
name,
c_int::from(dns_class),
c_int::from(query_type),
raw_query_callback::<F>,
handler
);
}
/// Send a DNS query using a pre-built [`DnsRecord`].
///
/// On completion, `handler` is called with a `Result<DnsRecord>` containing
/// the parsed response.
///
/// Returns the query ID on success.
///
/// # Examples
///
/// ```no_run
/// use c_ares::*;
///
/// let mut channel = Channel::new().unwrap();
/// let mut query = DnsRecord::new(0, DnsFlags::RD, DnsOpcode::Query, DnsRcode::NoError).unwrap();
/// query.query_add("example.com", DnsRecordType::A, DnsCls::IN).unwrap();
/// channel.send_dnsrec(&query, move |result| {
/// let record = result.unwrap();
/// for rr in record.rrs(DnsSection::Answer) {
/// if let Some(addr) = rr.get_addr(DnsRrKey::A_ADDR) {
/// println!("address: {addr}");
/// }
/// }
/// }).unwrap();
/// // ... drive the event loop ...
/// ```
#[cfg(cares1_28)]
pub fn send_dnsrec<F>(&mut self, dnsrec: &DnsRecord, handler: F) -> Result<u16>
where
F: FnOnce(Result<&DnsRecord>) + Send + 'static,
{
let mut qid: u16 = 0;
let c_arg = Box::into_raw(Box::new(handler));
let status = unsafe {
c_ares_sys::ares_send_dnsrec(
self.ares_channel,
dnsrec.as_raw(),
Some(dnsrec_callback::<F>),
c_arg.cast(),
&raw mut qid,
)
};
status_to_result(status)?;
Ok(qid)
}
/// Initiate a DNS query for `name` with the given class and type, receiving
/// a parsed [`DnsRecord`] in the callback.
///
/// Returns the query ID on success.
///
/// # Examples
///
/// ```no_run
/// use c_ares::{Channel, DnsCls, DnsRecordType, DnsRrKey, DnsSection};
///
/// let mut channel = Channel::new().unwrap();
/// channel.query_dnsrec(
/// "example.com",
/// DnsCls::IN,
/// DnsRecordType::A,
/// move |result| {
/// let record = result.unwrap();
/// for rr in record.rrs(DnsSection::Answer) {
/// if let Some(addr) = rr.get_addr(DnsRrKey::A_ADDR) {
/// println!("address: {addr}");
/// }
/// }
/// },
/// ).unwrap();
/// // ... drive the event loop with channel.sockets() / channel.process_fd() ...
/// ```
#[cfg(cares1_28)]
pub fn query_dnsrec<F>(
&mut self,
name: &str,
dns_class: DnsCls,
query_type: DnsRecordType,
handler: F,
) -> Result<u16>
where
F: FnOnce(Result<&DnsRecord>) + Send + 'static,
{
let c_name = CString::new(name).map_err(|_| Error::EBADNAME)?;
let mut qid: u16 = 0;
let c_arg = Box::into_raw(Box::new(handler));
let status = unsafe {
c_ares_sys::ares_query_dnsrec(
self.ares_channel,
c_name.as_ptr(),
dns_class.into(),
query_type.into(),
Some(dnsrec_callback::<F>),
c_arg.cast(),
&raw mut qid,
)
};
status_to_result(status)?;
Ok(qid)
}
/// Initiate a series of DNS queries using a pre-built [`DnsRecord`],
/// receiving a parsed [`DnsRecord`] in the callback.
///
/// # Examples
///
/// ```no_run
/// use c_ares::*;
///
/// let mut channel = Channel::new().unwrap();
/// let mut query = DnsRecord::new(0, DnsFlags::RD, DnsOpcode::Query, DnsRcode::NoError).unwrap();
/// query.query_add("example.com", DnsRecordType::A, DnsCls::IN).unwrap();
/// channel.search_dnsrec(&query, move |result| {
/// let record = result.unwrap();
/// for rr in record.rrs(DnsSection::Answer) {
/// if let Some(addr) = rr.get_addr(DnsRrKey::A_ADDR) {
/// println!("address: {addr}");
/// }
/// }
/// }).unwrap();
/// // ... drive the event loop ...
/// ```
#[cfg(cares1_28)]
pub fn search_dnsrec<F>(&mut self, dnsrec: &DnsRecord, handler: F) -> Result<()>
where
F: FnOnce(Result<&DnsRecord>) + Send + 'static,
{
let c_arg = Box::into_raw(Box::new(handler));
let status = unsafe {
c_ares_sys::ares_search_dnsrec(
self.ares_channel,
dnsrec.as_raw(),
Some(dnsrec_callback::<F>),
c_arg.cast(),
)
};
status_to_result(status)
}
/// Cancel all requests made on this `Channel`.
///
/// Callbacks will be invoked for each pending query, passing a result
/// `Err(Error::ECANCELLED)`.
pub fn cancel(&mut self) {
unsafe { c_ares_sys::ares_cancel(self.ares_channel) }
}
/// Kick c-ares to process a pending write.
#[cfg(cares1_34)]
pub fn process_pending_write(&mut self) {
unsafe { c_ares_sys::ares_process_pending_write(self.ares_channel) }
}
/// Return the maximum time to wait before processing timeouts.
///
/// If there are pending queries, returns the time until the next timeout
/// fires, optionally capped at `max_timeout`. If there are no pending
/// queries, returns `max_timeout` (which may be `None`).
pub fn timeout(&self, max_timeout: Option<Duration>) -> Option<Duration> {
let mut maxtv;
let maxtv_ptr = match max_timeout {
Some(d) => {
maxtv = c_ares_sys::timeval {
tv_sec: d.as_secs() as _,
// `tv_usec` is `i32` on some targets and `i64` on others;
// `u32 -> i32` cannot use `From`/`Into`.
#[allow(clippy::cast_lossless, clippy::cast_possible_wrap)]
tv_usec: d.subsec_micros() as _,
};
&mut maxtv
}
None => ptr::null_mut(),
};
let mut tv = c_ares_sys::timeval {
tv_sec: 0,
tv_usec: 0,
};
let result = unsafe { c_ares_sys::ares_timeout(self.ares_channel, maxtv_ptr, &raw mut tv) };
unsafe { result.as_ref() }
.map(|tv| Duration::new(tv.tv_sec as u64, (tv.tv_usec as u32) * 1000))
}
/// Block until notified that there are no longer any queries in queue, or
/// the specified timeout has expired.
///
/// Pass `None` to wait indefinitely.
#[cfg(cares1_27)]
pub fn queue_wait_empty(&self, timeout: Option<Duration>) -> Result<()> {
let timeout_ms = match timeout {
None => -1,
Some(d) => d.as_millis().try_into().unwrap_or(c_int::MAX),
};
let rc = unsafe { c_ares_sys::ares_queue_wait_empty(self.ares_channel, timeout_ms) };
status_to_result(rc)
}
/// Retrieve the total number of active queries pending answers from servers.
///
/// Some c-ares requests may spawn multiple queries, such as
/// `get_addrinfo()` when using `AddressFamily::UNSPEC`, which will be
/// reflected in this number.
#[cfg(cares1_27)]
pub fn queue_active_queries(&self) -> usize {
unsafe { c_ares_sys::ares_queue_active_queries(self.ares_channel) }
}
}
impl Drop for Channel {
fn drop(&mut self) {
unsafe { c_ares_sys::ares_destroy(self.ares_channel) }
{
let _lock = ARES_LIBRARY_LOCK.lock().unwrap();
unsafe { c_ares_sys::ares_library_cleanup() }
}
}
}
unsafe impl Send for Channel {}
unsafe impl Sync for Channel {}
impl fmt::Debug for Channel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Channel").finish_non_exhaustive()
}
}
unsafe extern "C" fn socket_state_callback<F>(
data: *mut c_void,
socket_fd: c_ares_sys::ares_socket_t,
readable: c_int,
writable: c_int,
) where
F: Fn(Socket, bool, bool) + Send + Sync + 'static,
{
let handler = data.cast::<F>();
let handler = unsafe { &*handler };
panic::abort_on_panic(|| handler(socket_fd, readable != 0, writable != 0));
}
#[cfg(cares1_29)]
unsafe extern "C" fn server_state_callback<F>(
server_string: *const c_char,
success: c_ares_sys::ares_bool_t,
flags: c_int,
data: *mut c_void,
) where
F: Fn(&str, bool, ServerStateFlags) + Send + Sync + 'static,
{
let handler = data.cast::<F>();
let handler = unsafe { &*handler };
let server = unsafe { c_string_as_str_unchecked(server_string) };
panic::abort_on_panic(|| {
handler(
server,
success != c_ares_sys::ares_bool_t::ARES_FALSE,
ServerStateFlags::from_bits_truncate(flags),
);
});
}
#[cfg(cares1_34)]
unsafe extern "C" fn pending_write_callback<F>(data: *mut c_void)
where
F: Fn() + Send + Sync + 'static,
{
let handler = data.cast::<F>();
let handler = unsafe { &*handler };
panic::abort_on_panic(handler);
}
#[cfg(test)]
mod tests {
use super::*;
use std::net::{Ipv4Addr, Ipv6Addr};
#[test]
fn channel_new_default() {
let channel = Channel::new();
assert!(channel.is_ok());
}
#[test]
fn channel_with_options() {
let mut options = Options::new();
options.set_flags(crate::Flags::STAYOPEN).set_tries(2);
let channel = Channel::with_options(options);
assert!(channel.is_ok());
}
#[test]
fn channel_set_servers_empty() {
let mut channel = Channel::new().unwrap();
let result = channel.set_servers(Vec::<&str>::new());
assert!(result.is_ok());
}
#[test]
fn channel_set_servers_ipv4() {
let mut channel = Channel::new().unwrap();
let result = channel.set_servers(["8.8.8.8", "8.8.4.4"]);
assert!(result.is_ok());
}
#[test]
fn channel_set_servers_ipv6() {
let mut channel = Channel::new().unwrap();
let result = channel.set_servers(["[2001:4860:4860::8888]"]);
assert!(result.is_ok());
}
#[test]
fn channel_set_servers_with_port() {
let mut channel = Channel::new().unwrap();
let result = channel.set_servers(["8.8.8.8:53", "[2001:4860:4860::8888]:53"]);
assert!(result.is_ok());
}
#[test]
fn channel_sockets() {
let channel = Channel::new().unwrap();
let sockets = channel.sockets();
assert_eq!(sockets.iter().count(), 0);
}
#[test]
fn channel_cancel() {
let mut channel = Channel::new().unwrap();
channel.cancel();
}
#[test]
fn channel_process_fd_none() {
let mut channel = Channel::new().unwrap();
channel.process_fd(None, None);
}
#[test]
fn channel_try_clone() {
let channel = Channel::new().unwrap();
let cloned = channel.try_clone();
assert!(cloned.is_ok());
}
#[test]
fn channel_set_local_ipv4() {
let mut channel = Channel::new().unwrap();
channel.set_local_ipv4(Ipv4Addr::UNSPECIFIED);
}
#[test]
fn channel_set_local_ipv6() {
let mut channel = Channel::new().unwrap();
channel.set_local_ipv6(Ipv6Addr::UNSPECIFIED);
}
#[test]
fn channel_set_local_device() {
let mut channel = Channel::new().unwrap();
channel.set_local_device("lo").unwrap();
}
#[test]
fn channel_set_sortlist() {
let mut channel = Channel::new().unwrap();
let result = channel.set_sortlist(["130.155.160.0/255.255.240.0", "130.155.0.0"]);
assert!(result.is_ok());
}
#[cfg(cares1_24)]
#[test]
fn parse_servers_csv_null_is_empty() {
// ares_get_servers_csv() returns NULL on allocation failure; this must
// not dereference the null pointer.
assert!(Channel::parse_servers_csv(std::ptr::null_mut()).is_empty());
}
#[cfg(cares1_24)]
#[test]
fn channel_servers_round_trip() {
let mut channel = Channel::new().unwrap();
channel.set_servers(["8.8.8.8:53"]).unwrap();
let servers = channel.servers();
assert!(servers.iter().any(|s| s.contains("8.8.8.8")));
}
#[test]
fn channel_set_servers_owned_strings() {
let mut channel = Channel::new().unwrap();
let servers: Vec<String> = vec!["8.8.8.8".into(), "8.8.4.4".into()];
channel.set_servers(servers).unwrap();
}
#[test]
fn channel_set_sortlist_owned_strings() {
let mut channel = Channel::new().unwrap();
let sortlist: Vec<String> = vec!["130.155.0.0".into()];
channel.set_sortlist(sortlist).unwrap();
}
#[cfg(cares1_22)]
#[test]
fn channel_reinit() {
let mut channel = Channel::new().unwrap();
let result = channel.reinit();
assert!(result.is_ok());
}
#[cfg(cares1_24)]
#[test]
fn channel_servers() {
let mut channel = Channel::new().unwrap();
channel.set_servers(["8.8.8.8"]).unwrap();
let servers = channel.servers();
assert!(!servers.is_empty());
}
#[cfg(cares1_24)]
#[test]
fn channel_servers_empty() {
let mut channel = Channel::new().unwrap();
channel.set_servers(Vec::<&str>::new()).unwrap();
let servers = channel.servers();
assert!(servers.is_empty());
}
#[cfg(cares1_34)]
#[test]
fn channel_process_fds_empty() {
use crate::ProcessFlags;
let mut channel = Channel::new().unwrap();
let result = channel.process_fds(&[], ProcessFlags::empty());
assert!(result.is_ok());
}
#[cfg(cares1_34)]
#[test]
fn channel_process_pending_write() {
let mut channel = Channel::new().unwrap();
channel.process_pending_write();
}
#[cfg(cares1_28)]
#[test]
fn query_dnsrec_rejects_nul_in_name_with_ebadname() {
let mut channel = Channel::new().unwrap();
let result = channel.query_dnsrec("ex\0ample.com", DnsCls::IN, DnsRecordType::A, |_| {});
assert_eq!(result, Err(Error::EBADNAME));
}
#[test]
fn channel_is_send() {
fn assert_send<T: Send>() {}
assert_send::<Channel>();
}
#[test]
fn channel_is_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<Channel>();
}
#[test]
fn channel_fds() {
use std::mem::MaybeUninit;
let channel = Channel::new().unwrap();
unsafe {
let mut read_fds: c_types::fd_set = MaybeUninit::zeroed().assume_init();
let mut write_fds: c_types::fd_set = MaybeUninit::zeroed().assume_init();
let nfds = channel.fds(&mut read_fds, &mut write_fds);
// No queries started, so should be 0
assert_eq!(nfds, 0);
}
}
#[test]
fn set_servers_invalid() {
let mut channel = Channel::new().unwrap();
// Invalid server format
let result = channel.set_servers(["not-a-valid-ip"]);
assert!(result.is_err());
}
#[test]
fn set_sortlist_invalid() {
let mut channel = Channel::new().unwrap();
// Invalid sortlist format
let result = channel.set_sortlist(["not-a-valid-address"]);
// Should fail
assert!(result.is_err());
}
#[cfg(cares1_29)]
#[test]
fn channel_set_server_state_callback() {
use crate::ServerStateFlags;
let mut channel = Channel::new().unwrap();
channel.set_server_state_callback(
|_server: &str, _success: bool, _flags: ServerStateFlags| {
// Callback for server state changes
},
);
}
#[cfg(cares1_34)]
#[test]
fn channel_set_pending_write_callback() {
let mut channel = Channel::new().unwrap();
channel.set_pending_write_callback(|| {
// Callback for pending writes
});
}
#[test]
fn channel_process() {
use std::mem::MaybeUninit;
let mut channel = Channel::new().unwrap();
unsafe {
let mut read_fds: c_types::fd_set = MaybeUninit::zeroed().assume_init();
let mut write_fds: c_types::fd_set = MaybeUninit::zeroed().assume_init();
channel.process(&mut read_fds, &mut write_fds);
}
}
#[cfg(cares1_27)]
#[test]
fn channel_queue_active_queries() {
let channel = Channel::new().unwrap();
assert_eq!(channel.queue_active_queries(), 0);
}
#[cfg(cares1_27)]
#[test]
fn channel_queue_wait_empty() {
let channel = Channel::new().unwrap();
// No pending queries, should return immediately.
// Returns ENOTIMP if c-ares was not built with threading support.
let result = channel.queue_wait_empty(Some(Duration::ZERO));
assert!(result.is_ok() || result == Err(Error::ENOTIMP));
}
#[cfg(cares1_27)]
#[test]
fn channel_queue_wait_empty_none_timeout() {
let channel = Channel::new().unwrap();
// None means "wait indefinitely", but with no pending queries it returns immediately.
let result = channel.queue_wait_empty(None);
assert!(result.is_ok() || result == Err(Error::ENOTIMP));
}
#[test]
fn debug_channel() {
let channel = Channel::new().unwrap();
let debug = format!("{channel:?}");
assert!(debug.contains("Channel"));
}
#[test]
fn timeout_no_queries_with_max() {
let channel = Channel::new().unwrap();
// No pending queries: returns the max_timeout we provide.
let max = Duration::from_secs(5);
let result = channel.timeout(Some(max));
assert_eq!(result, Some(max));
}
#[test]
fn timeout_no_queries_without_max() {
let channel = Channel::new().unwrap();
// No pending queries and no max: returns None.
let result = channel.timeout(None);
assert_eq!(result, None);
}
}