gio/auto/resolver.rs
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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT
#[cfg(feature = "v2_60")]
#[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))]
use crate::ResolverNameLookupFlags;
use crate::{ffi, AsyncResult, Cancellable, InetAddress, ResolverRecordType, SrvTarget};
use glib::{
prelude::*,
signal::{connect_raw, SignalHandlerId},
translate::*,
};
use std::{boxed::Box as Box_, pin::Pin};
glib::wrapper! {
/// The object that handles DNS resolution. Use [`default()`][Self::default()]
/// to get the default resolver.
///
/// `GResolver` provides cancellable synchronous and asynchronous DNS
/// resolution, for hostnames ([`ResolverExt::lookup_by_address()`][crate::prelude::ResolverExt::lookup_by_address()],
/// [`ResolverExt::lookup_by_name()`][crate::prelude::ResolverExt::lookup_by_name()] and their async variants) and SRV
/// (service) records ([`ResolverExt::lookup_service()`][crate::prelude::ResolverExt::lookup_service()]).
///
/// [`NetworkAddress`][crate::NetworkAddress] and [`NetworkService`][crate::NetworkService] provide wrappers
/// around `GResolver` functionality that also implement
/// [`SocketConnectable`][crate::SocketConnectable], making it easy to connect to a remote
/// host/service.
///
/// The default resolver (see [`default()`][Self::default()]) has a timeout of
/// 30s set on it since GLib 2.78. Earlier versions of GLib did not support
/// resolver timeouts.
///
/// This is an abstract type; subclasses of it implement different resolvers for
/// different platforms and situations.
///
/// This is an Abstract Base Class, you cannot instantiate it.
///
/// ## Properties
///
///
/// #### `timeout`
/// The timeout applied to all resolver lookups, in milliseconds.
///
/// This may be changed through the lifetime of the #GResolver. The new value
/// will apply to any lookups started after the change, but not to any
/// already-ongoing lookups.
///
/// If this is `0`, no timeout is applied to lookups.
///
/// No timeout was applied to lookups before this property was added in
/// GLib 2.78.
///
/// Readable | Writeable
///
/// ## Signals
///
///
/// #### `reload`
/// Emitted when the resolver notices that the system resolver
/// configuration has changed.
///
///
///
/// # Implements
///
/// [`ResolverExt`][trait@crate::prelude::ResolverExt], [`trait@glib::ObjectExt`]
#[doc(alias = "GResolver")]
pub struct Resolver(Object<ffi::GResolver, ffi::GResolverClass>);
match fn {
type_ => || ffi::g_resolver_get_type(),
}
}
impl Resolver {
pub const NONE: Option<&'static Resolver> = None;
//#[doc(alias = "g_resolver_free_addresses")]
//pub fn free_addresses(addresses: /*Unimplemented*/&[&Basic: Pointer]) {
// unsafe { TODO: call ffi:g_resolver_free_addresses() }
//}
//#[doc(alias = "g_resolver_free_targets")]
//pub fn free_targets(targets: /*Unimplemented*/&[&Basic: Pointer]) {
// unsafe { TODO: call ffi:g_resolver_free_targets() }
//}
/// Gets the default #GResolver. You should unref it when you are done
/// with it. #GResolver may use its reference count as a hint about how
/// many threads it should allocate for concurrent DNS resolutions.
///
/// # Returns
///
/// the default #GResolver.
#[doc(alias = "g_resolver_get_default")]
#[doc(alias = "get_default")]
#[allow(clippy::should_implement_trait)]
pub fn default() -> Resolver {
unsafe { from_glib_full(ffi::g_resolver_get_default()) }
}
}
/// Trait containing all [`struct@Resolver`] methods.
///
/// # Implementors
///
/// [`Resolver`][struct@crate::Resolver]
pub trait ResolverExt: IsA<Resolver> + 'static {
/// Get the timeout applied to all resolver lookups. See #GResolver:timeout.
///
/// # Returns
///
/// the resolver timeout, in milliseconds, or `0` for no timeout
#[cfg(feature = "v2_78")]
#[cfg_attr(docsrs, doc(cfg(feature = "v2_78")))]
#[doc(alias = "g_resolver_get_timeout")]
#[doc(alias = "get_timeout")]
fn timeout(&self) -> u32 {
unsafe { ffi::g_resolver_get_timeout(self.as_ref().to_glib_none().0) }
}
/// Synchronously reverse-resolves @address to determine its
/// associated hostname.
///
/// If the DNS resolution fails, @error (if non-[`None`]) will be set to
/// a value from #GResolverError.
///
/// If @cancellable is non-[`None`], it can be used to cancel the
/// operation, in which case @error (if non-[`None`]) will be set to
/// [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled].
/// ## `address`
/// the address to reverse-resolve
/// ## `cancellable`
/// a #GCancellable, or [`None`]
///
/// # Returns
///
/// a hostname (either ASCII-only, or in ASCII-encoded
/// form), or [`None`] on error.
#[doc(alias = "g_resolver_lookup_by_address")]
fn lookup_by_address(
&self,
address: &impl IsA<InetAddress>,
cancellable: Option<&impl IsA<Cancellable>>,
) -> Result<glib::GString, glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let ret = ffi::g_resolver_lookup_by_address(
self.as_ref().to_glib_none().0,
address.as_ref().to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
}
}
}
/// Begins asynchronously reverse-resolving @address to determine its
/// associated hostname, and eventually calls @callback, which must
/// call g_resolver_lookup_by_address_finish() to get the final result.
/// ## `address`
/// the address to reverse-resolve
/// ## `cancellable`
/// a #GCancellable, or [`None`]
/// ## `callback`
/// callback to call after resolution completes
#[doc(alias = "g_resolver_lookup_by_address_async")]
fn lookup_by_address_async<P: FnOnce(Result<glib::GString, glib::Error>) + 'static>(
&self,
address: &impl IsA<InetAddress>,
cancellable: Option<&impl IsA<Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn lookup_by_address_async_trampoline<
P: FnOnce(Result<glib::GString, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut crate::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = std::ptr::null_mut();
let ret =
ffi::g_resolver_lookup_by_address_finish(_source_object as *mut _, res, &mut error);
let result = if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = lookup_by_address_async_trampoline::<P>;
unsafe {
ffi::g_resolver_lookup_by_address_async(
self.as_ref().to_glib_none().0,
address.as_ref().to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
fn lookup_by_address_future(
&self,
address: &(impl IsA<InetAddress> + Clone + 'static),
) -> Pin<Box_<dyn std::future::Future<Output = Result<glib::GString, glib::Error>> + 'static>>
{
let address = address.clone();
Box_::pin(crate::GioFuture::new(
self,
move |obj, cancellable, send| {
obj.lookup_by_address_async(&address, Some(cancellable), move |res| {
send.resolve(res);
});
},
))
}
/// Synchronously resolves @hostname to determine its associated IP
/// address(es). @hostname may be an ASCII-only or UTF-8 hostname, or
/// the textual form of an IP address (in which case this just becomes
/// a wrapper around g_inet_address_new_from_string()).
///
/// On success, g_resolver_lookup_by_name() will return a non-empty #GList of
/// #GInetAddress, sorted in order of preference and guaranteed to not
/// contain duplicates. That is, if using the result to connect to
/// @hostname, you should attempt to connect to the first address
/// first, then the second if the first fails, etc. If you are using
/// the result to listen on a socket, it is appropriate to add each
/// result using e.g. g_socket_listener_add_address().
///
/// If the DNS resolution fails, @error (if non-[`None`]) will be set to a
/// value from #GResolverError and [`None`] will be returned.
///
/// If @cancellable is non-[`None`], it can be used to cancel the
/// operation, in which case @error (if non-[`None`]) will be set to
/// [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled].
///
/// If you are planning to connect to a socket on the resolved IP
/// address, it may be easier to create a #GNetworkAddress and use its
/// #GSocketConnectable interface.
/// ## `hostname`
/// the hostname to look up
/// ## `cancellable`
/// a #GCancellable, or [`None`]
///
/// # Returns
///
/// a non-empty #GList
/// of #GInetAddress, or [`None`] on error. You
/// must unref each of the addresses and free the list when you are
/// done with it. (You can use g_resolver_free_addresses() to do this.)
#[doc(alias = "g_resolver_lookup_by_name")]
fn lookup_by_name(
&self,
hostname: &str,
cancellable: Option<&impl IsA<Cancellable>>,
) -> Result<Vec<InetAddress>, glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let ret = ffi::g_resolver_lookup_by_name(
self.as_ref().to_glib_none().0,
hostname.to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(FromGlibPtrContainer::from_glib_full(ret))
} else {
Err(from_glib_full(error))
}
}
}
/// Begins asynchronously resolving @hostname to determine its
/// associated IP address(es), and eventually calls @callback, which
/// must call g_resolver_lookup_by_name_finish() to get the result.
/// See g_resolver_lookup_by_name() for more details.
/// ## `hostname`
/// the hostname to look up the address of
/// ## `cancellable`
/// a #GCancellable, or [`None`]
/// ## `callback`
/// callback to call after resolution completes
#[doc(alias = "g_resolver_lookup_by_name_async")]
fn lookup_by_name_async<P: FnOnce(Result<Vec<InetAddress>, glib::Error>) + 'static>(
&self,
hostname: &str,
cancellable: Option<&impl IsA<Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn lookup_by_name_async_trampoline<
P: FnOnce(Result<Vec<InetAddress>, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut crate::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = std::ptr::null_mut();
let ret =
ffi::g_resolver_lookup_by_name_finish(_source_object as *mut _, res, &mut error);
let result = if error.is_null() {
Ok(FromGlibPtrContainer::from_glib_full(ret))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = lookup_by_name_async_trampoline::<P>;
unsafe {
ffi::g_resolver_lookup_by_name_async(
self.as_ref().to_glib_none().0,
hostname.to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
fn lookup_by_name_future(
&self,
hostname: &str,
) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<InetAddress>, glib::Error>> + 'static>>
{
let hostname = String::from(hostname);
Box_::pin(crate::GioFuture::new(
self,
move |obj, cancellable, send| {
obj.lookup_by_name_async(&hostname, Some(cancellable), move |res| {
send.resolve(res);
});
},
))
}
/// This differs from g_resolver_lookup_by_name() in that you can modify
/// the lookup behavior with @flags. For example this can be used to limit
/// results with [`ResolverNameLookupFlags::IPV4_ONLY`][crate::ResolverNameLookupFlags::IPV4_ONLY].
/// ## `hostname`
/// the hostname to look up
/// ## `flags`
/// extra #GResolverNameLookupFlags for the lookup
/// ## `cancellable`
/// a #GCancellable, or [`None`]
///
/// # Returns
///
/// a non-empty #GList
/// of #GInetAddress, or [`None`] on error. You
/// must unref each of the addresses and free the list when you are
/// done with it. (You can use g_resolver_free_addresses() to do this.)
#[cfg(feature = "v2_60")]
#[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))]
#[doc(alias = "g_resolver_lookup_by_name_with_flags")]
fn lookup_by_name_with_flags(
&self,
hostname: &str,
flags: ResolverNameLookupFlags,
cancellable: Option<&impl IsA<Cancellable>>,
) -> Result<Vec<InetAddress>, glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let ret = ffi::g_resolver_lookup_by_name_with_flags(
self.as_ref().to_glib_none().0,
hostname.to_glib_none().0,
flags.into_glib(),
cancellable.map(|p| p.as_ref()).to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(FromGlibPtrContainer::from_glib_full(ret))
} else {
Err(from_glib_full(error))
}
}
}
/// Begins asynchronously resolving @hostname to determine its
/// associated IP address(es), and eventually calls @callback, which
/// must call g_resolver_lookup_by_name_with_flags_finish() to get the result.
/// See g_resolver_lookup_by_name() for more details.
/// ## `hostname`
/// the hostname to look up the address of
/// ## `flags`
/// extra #GResolverNameLookupFlags for the lookup
/// ## `cancellable`
/// a #GCancellable, or [`None`]
/// ## `callback`
/// callback to call after resolution completes
#[cfg(feature = "v2_60")]
#[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))]
#[doc(alias = "g_resolver_lookup_by_name_with_flags_async")]
fn lookup_by_name_with_flags_async<
P: FnOnce(Result<Vec<InetAddress>, glib::Error>) + 'static,
>(
&self,
hostname: &str,
flags: ResolverNameLookupFlags,
cancellable: Option<&impl IsA<Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn lookup_by_name_with_flags_async_trampoline<
P: FnOnce(Result<Vec<InetAddress>, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut crate::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = std::ptr::null_mut();
let ret = ffi::g_resolver_lookup_by_name_with_flags_finish(
_source_object as *mut _,
res,
&mut error,
);
let result = if error.is_null() {
Ok(FromGlibPtrContainer::from_glib_full(ret))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = lookup_by_name_with_flags_async_trampoline::<P>;
unsafe {
ffi::g_resolver_lookup_by_name_with_flags_async(
self.as_ref().to_glib_none().0,
hostname.to_glib_none().0,
flags.into_glib(),
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
#[cfg(feature = "v2_60")]
#[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))]
fn lookup_by_name_with_flags_future(
&self,
hostname: &str,
flags: ResolverNameLookupFlags,
) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<InetAddress>, glib::Error>> + 'static>>
{
let hostname = String::from(hostname);
Box_::pin(crate::GioFuture::new(
self,
move |obj, cancellable, send| {
obj.lookup_by_name_with_flags_async(
&hostname,
flags,
Some(cancellable),
move |res| {
send.resolve(res);
},
);
},
))
}
/// Synchronously performs a DNS record lookup for the given @rrname and returns
/// a list of records as #GVariant tuples. See #GResolverRecordType for
/// information on what the records contain for each @record_type.
///
/// If the DNS resolution fails, @error (if non-[`None`]) will be set to
/// a value from #GResolverError and [`None`] will be returned.
///
/// If @cancellable is non-[`None`], it can be used to cancel the
/// operation, in which case @error (if non-[`None`]) will be set to
/// [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled].
/// ## `rrname`
/// the DNS name to look up the record for
/// ## `record_type`
/// the type of DNS record to look up
/// ## `cancellable`
/// a #GCancellable, or [`None`]
///
/// # Returns
///
/// a non-empty #GList of
/// #GVariant, or [`None`] on error. You must free each of the records and the list
/// when you are done with it. (You can use g_list_free_full() with
/// g_variant_unref() to do this.)
#[doc(alias = "g_resolver_lookup_records")]
fn lookup_records(
&self,
rrname: &str,
record_type: ResolverRecordType,
cancellable: Option<&impl IsA<Cancellable>>,
) -> Result<Vec<glib::Variant>, glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let ret = ffi::g_resolver_lookup_records(
self.as_ref().to_glib_none().0,
rrname.to_glib_none().0,
record_type.into_glib(),
cancellable.map(|p| p.as_ref()).to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(FromGlibPtrContainer::from_glib_full(ret))
} else {
Err(from_glib_full(error))
}
}
}
/// Begins asynchronously performing a DNS lookup for the given
/// @rrname, and eventually calls @callback, which must call
/// g_resolver_lookup_records_finish() to get the final result. See
/// g_resolver_lookup_records() for more details.
/// ## `rrname`
/// the DNS name to look up the record for
/// ## `record_type`
/// the type of DNS record to look up
/// ## `cancellable`
/// a #GCancellable, or [`None`]
/// ## `callback`
/// callback to call after resolution completes
#[doc(alias = "g_resolver_lookup_records_async")]
fn lookup_records_async<P: FnOnce(Result<Vec<glib::Variant>, glib::Error>) + 'static>(
&self,
rrname: &str,
record_type: ResolverRecordType,
cancellable: Option<&impl IsA<Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn lookup_records_async_trampoline<
P: FnOnce(Result<Vec<glib::Variant>, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut crate::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = std::ptr::null_mut();
let ret =
ffi::g_resolver_lookup_records_finish(_source_object as *mut _, res, &mut error);
let result = if error.is_null() {
Ok(FromGlibPtrContainer::from_glib_full(ret))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = lookup_records_async_trampoline::<P>;
unsafe {
ffi::g_resolver_lookup_records_async(
self.as_ref().to_glib_none().0,
rrname.to_glib_none().0,
record_type.into_glib(),
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
fn lookup_records_future(
&self,
rrname: &str,
record_type: ResolverRecordType,
) -> Pin<
Box_<dyn std::future::Future<Output = Result<Vec<glib::Variant>, glib::Error>> + 'static>,
> {
let rrname = String::from(rrname);
Box_::pin(crate::GioFuture::new(
self,
move |obj, cancellable, send| {
obj.lookup_records_async(&rrname, record_type, Some(cancellable), move |res| {
send.resolve(res);
});
},
))
}
/// Synchronously performs a DNS SRV lookup for the given @service and
/// @protocol in the given @domain and returns an array of #GSrvTarget.
/// @domain may be an ASCII-only or UTF-8 hostname. Note also that the
/// @service and @protocol arguments do not include the leading underscore
/// that appears in the actual DNS entry.
///
/// On success, g_resolver_lookup_service() will return a non-empty #GList of
/// #GSrvTarget, sorted in order of preference. (That is, you should
/// attempt to connect to the first target first, then the second if
/// the first fails, etc.)
///
/// If the DNS resolution fails, @error (if non-[`None`]) will be set to
/// a value from #GResolverError and [`None`] will be returned.
///
/// If @cancellable is non-[`None`], it can be used to cancel the
/// operation, in which case @error (if non-[`None`]) will be set to
/// [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled].
///
/// If you are planning to connect to the service, it is usually easier
/// to create a #GNetworkService and use its #GSocketConnectable
/// interface.
/// ## `service`
/// the service type to look up (eg, "ldap")
/// ## `protocol`
/// the networking protocol to use for @service (eg, "tcp")
/// ## `domain`
/// the DNS domain to look up the service in
/// ## `cancellable`
/// a #GCancellable, or [`None`]
///
/// # Returns
///
/// a non-empty #GList of
/// #GSrvTarget, or [`None`] on error. You must free each of the targets and the
/// list when you are done with it. (You can use g_resolver_free_targets() to do
/// this.)
#[doc(alias = "g_resolver_lookup_service")]
fn lookup_service(
&self,
service: &str,
protocol: &str,
domain: &str,
cancellable: Option<&impl IsA<Cancellable>>,
) -> Result<Vec<SrvTarget>, glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let ret = ffi::g_resolver_lookup_service(
self.as_ref().to_glib_none().0,
service.to_glib_none().0,
protocol.to_glib_none().0,
domain.to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(FromGlibPtrContainer::from_glib_full(ret))
} else {
Err(from_glib_full(error))
}
}
}
/// Begins asynchronously performing a DNS SRV lookup for the given
/// @service and @protocol in the given @domain, and eventually calls
/// @callback, which must call g_resolver_lookup_service_finish() to
/// get the final result. See g_resolver_lookup_service() for more
/// details.
/// ## `service`
/// the service type to look up (eg, "ldap")
/// ## `protocol`
/// the networking protocol to use for @service (eg, "tcp")
/// ## `domain`
/// the DNS domain to look up the service in
/// ## `cancellable`
/// a #GCancellable, or [`None`]
/// ## `callback`
/// callback to call after resolution completes
#[doc(alias = "g_resolver_lookup_service_async")]
fn lookup_service_async<P: FnOnce(Result<Vec<SrvTarget>, glib::Error>) + 'static>(
&self,
service: &str,
protocol: &str,
domain: &str,
cancellable: Option<&impl IsA<Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn lookup_service_async_trampoline<
P: FnOnce(Result<Vec<SrvTarget>, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut crate::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = std::ptr::null_mut();
let ret =
ffi::g_resolver_lookup_service_finish(_source_object as *mut _, res, &mut error);
let result = if error.is_null() {
Ok(FromGlibPtrContainer::from_glib_full(ret))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = lookup_service_async_trampoline::<P>;
unsafe {
ffi::g_resolver_lookup_service_async(
self.as_ref().to_glib_none().0,
service.to_glib_none().0,
protocol.to_glib_none().0,
domain.to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
fn lookup_service_future(
&self,
service: &str,
protocol: &str,
domain: &str,
) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<SrvTarget>, glib::Error>> + 'static>>
{
let service = String::from(service);
let protocol = String::from(protocol);
let domain = String::from(domain);
Box_::pin(crate::GioFuture::new(
self,
move |obj, cancellable, send| {
obj.lookup_service_async(
&service,
&protocol,
&domain,
Some(cancellable),
move |res| {
send.resolve(res);
},
);
},
))
}
/// Sets @self to be the application's default resolver (reffing
/// @self, and unreffing the previous default resolver, if any).
/// Future calls to g_resolver_get_default() will return this resolver.
///
/// This can be used if an application wants to perform any sort of DNS
/// caching or "pinning"; it can implement its own #GResolver that
/// calls the original default resolver for DNS operations, and
/// implements its own cache policies on top of that, and then set
/// itself as the default resolver for all later code to use.
#[doc(alias = "g_resolver_set_default")]
fn set_default(&self) {
unsafe {
ffi::g_resolver_set_default(self.as_ref().to_glib_none().0);
}
}
/// Set the timeout applied to all resolver lookups. See #GResolver:timeout.
/// ## `timeout_ms`
/// timeout in milliseconds, or `0` for no timeouts
#[cfg(feature = "v2_78")]
#[cfg_attr(docsrs, doc(cfg(feature = "v2_78")))]
#[doc(alias = "g_resolver_set_timeout")]
#[doc(alias = "timeout")]
fn set_timeout(&self, timeout_ms: u32) {
unsafe {
ffi::g_resolver_set_timeout(self.as_ref().to_glib_none().0, timeout_ms);
}
}
/// Emitted when the resolver notices that the system resolver
/// configuration has changed.
#[doc(alias = "reload")]
fn connect_reload<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn reload_trampoline<P: IsA<Resolver>, F: Fn(&P) + 'static>(
this: *mut ffi::GResolver,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(Resolver::from_glib_borrow(this).unsafe_cast_ref())
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
b"reload\0".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
reload_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[cfg(feature = "v2_78")]
#[cfg_attr(docsrs, doc(cfg(feature = "v2_78")))]
#[doc(alias = "timeout")]
fn connect_timeout_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_timeout_trampoline<P: IsA<Resolver>, F: Fn(&P) + 'static>(
this: *mut ffi::GResolver,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(Resolver::from_glib_borrow(this).unsafe_cast_ref())
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
b"notify::timeout\0".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
notify_timeout_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
}
impl<O: IsA<Resolver>> ResolverExt for O {}