silk.net.resolver_native.

The native Resolver provider using the system's getaddrinfo, or GetAddrInfoW on Windows.

When to use#

Provide NativeSystemResolver for Resolver in a native program when a blocking lookup is acceptable. Code that resolves uses silk.net.resolver, so a test can provide a fake.

Details#

A host name without a deadline makes one getaddrinfo call with the name as written, so names in the hosts file, such as localhost, resolve. The result is copied, and the list of the system is freed one time, also on errors. Numeric hosts do not call the provider. Windows preserves validated ASCII host names in UTF-16 and owns a Winsock reference until the native result chain is freed. The Windows provider supports the x86_64 MSVC system profile.

Errors#

ResolverError if the host is not found, has no address of the selected family, or the lookup fails. A host name with a deadline fails with DeadlineUnsupported.

Gotchas#

  • A host name with a deadline fails with DeadlineUnsupported before the lookup. After getaddrinfo starts, it cannot be cancelled or limited in time or memory.

  • The lookup blocks the thread. It does not let another fiber run.

  • The supported targets are Windows x86_64 MSVC, Darwin x86_64 or aarch64, and GNU/Linux x86_64 or aarch64.

Examples#

Use the system resolver#

silk
import silk.mem.allocator { Allocator, OutOfMemoryError }
import silk.core.effect { Effect }
import silk.system.monotonic_clock { MonotonicClock }
import silk.net.resolver_native { NativeSystemResolver }
import silk.net.address { AddressError, Host, Port }
import silk.core.option { Option }
import silk.system.monotonic_clock_native { OsMonotonicClock }
import silk.core.result { Result }
import silk.net.resolver { FamilySelection, ResolveRequest, ResolvedEndpoints, Resolver, ResolverError }
import silk.time.mark { Mark }

fn request(name: string, deadline: Option<Mark>) -> Option<ResolveRequest> {
  let host = match Host.parse(name) {
    Result<Host, AddressError>.Success { value } => value
    Result<Host, AddressError>.Failure {..} => {
      return Option<ResolveRequest>.none()
    }
  }
  return match ResolveRequest.make(host, Port.fromU16(443), FamilySelection.Any, 8, move deadline) {
    Result<ResolveRequest, ResolverError>.Success { value } => Option<ResolveRequest>.some(
      move value
    )
    Result<ResolveRequest, ResolverError>.Failure {..} => Option<ResolveRequest>.none()
  }
}

// Returns the number of endpoints of `name`. It blocks until the system lookup ends.
effect fn countEndpoints(name: string) -> usize
  ! ResolverError | OutOfMemoryError
  ? &mut Resolver | &mut MonotonicClock | &mut Allocator {
  let lookup = match request(name, Option<Mark>.none()) {
    Option.Some { value } => move value
    Option.None => {
      return 0
    }
  }
  let endpoints = run Resolver.resolve(&lookup)
  return ResolvedEndpoints.length(&endpoints)
}

pub fn main() -> i32 {
  let mut allocator = Allocator.systemAllocatorProvider()
  let mut resolver = NativeSystemResolver.make()
  let mut clock = OsMonotonicClock.make()

  // A host name with a deadline fails before the lookup starts.
  let limited = match request("example.com", Option<Mark>.some(Mark.make(1, 0))) {
    Option.Some { value } => move value
    Option.None => {
      return 1
    }
  }
  let attempted = run Effect.result(Resolver.resolve(&limited))
    |> Effect.provideMut<Resolver>(&mut resolver)
    |> Effect.provideMut<MonotonicClock>(&mut clock)
    |> Effect.provideMut<Allocator>(&mut allocator)
  return match move attempted {
    Result.Success {..} => 1
    Result.Failure { error } => match move error {
      ResolverError.DeadlineUnsupported => 0
      _ => 1
    }
  }
}

Import as NativeSystemResolver with import silk.net.resolver_native { NativeSystemResolver }.

Public declarations: 1.

NativeSystemResolver#

silk
pub struct NativeSystemResolver

The native Resolver provider, with the getaddrinfo function of the system. It has no state.

When to use#

Provide it for Resolver in a native program when a blocking lookup is acceptable. Code that resolves uses silk.net.resolver, so a test can provide a fake.

Details#

A host name without a deadline makes one getaddrinfo call with the name as written, so names in the hosts file, such as localhost, resolve. The result is copied, and the list of the system is freed one time, also on errors. Numeric hosts do not call the provider. Windows uses GetAddrInfoW, preserves the validated ASCII name, and keeps a scoped Winsock reference alive until the returned list is freed. Numeric hosts need no Winsock initialization.

Gotchas#

  • A host name with a deadline fails with DeadlineUnsupported before the lookup. After getaddrinfo starts, it cannot be cancelled or limited in time or memory.

  • The lookup blocks the thread. It does not let another fiber run.

Implementation NativeSystemResolver#

silk
impl NativeSystemResolver

Associated function make#

silk
pub fn make() -> NativeSystemResolver

Returns a resolver. It does no lookup.

Gotchas#

Supported targets are Windows x86_64 MSVC, Darwin x86_64 or aarch64, and GNU/Linux x86_64 or aarch64.

Implementation Resolver for NativeSystemResolver#

silk
impl Resolver for NativeSystemResolver

Operation resolveDomain#

silk
resolveDomain: NativeSystemResolver.resolveDomain

Category: Networking.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page