diff --git a/eio/client/dns_client_eio.ml b/eio/client/dns_client_eio.ml index 3ca71a4e2..cd08663f3 100644 --- a/eio/client/dns_client_eio.ml +++ b/eio/client/dns_client_eio.ml @@ -4,7 +4,7 @@ type env = < clock : E.Time.clock ; net : E.Net.t; fs : E.Dir.t; - secure_random : Eio.Flow.source; + secure_random : E.Flow.source; > type io_addr = Ipaddr.t * int @@ -12,8 +12,8 @@ type stack = env * E.Switch.t module Transport : Dns_client.S with type io_addr = io_addr - and type stack = stack - and type +'a io = 'a + and type stack = stack + and type +'a io = 'a = struct type nonrec io_addr = io_addr type nonrec stack = stack @@ -36,7 +36,7 @@ module Transport : Dns_client.S } let read_file env file = - match E.Dir.load (E.Stdenv.fs env) file with + match E.Dir.load env#fs file with | content -> Ok content | exception e -> let err = "Error while reading file: " ^ file ^ ". " ^ (Printexc.to_string e) in @@ -159,8 +159,8 @@ module Transport : Dns_client.S let stream = `Tcp (ip, port) in try let timeout = Duration.to_f t.timeout_ns in - E.Time.with_timeout_exn (E.Stdenv.clock t.env) timeout @@ fun () -> - let flow = E.Net.connect ~sw:t.sw (E.Stdenv.net t.env) stream in + E.Time.with_timeout_exn t.env#clock timeout @@ fun () -> + let flow = E.Net.connect ~sw:t.sw t.env#net stream in Ok flow with E.Time.Timeout -> (* Push the non responsive nameserver to the back of the queue. *) diff --git a/eio/client/dns_client_eio.mli b/eio/client/dns_client_eio.mli index 6a510ac65..3f24517f1 100644 --- a/eio/client/dns_client_eio.mli +++ b/eio/client/dns_client_eio.mli @@ -2,7 +2,7 @@ type env = < clock : Eio.Time.clock ; net : Eio.Net.t ; fs : Eio.Dir.t ; - secure_random : Eio.Flow.source; + secure_random : Eio.Flow.source ; > module Transport : Dns_client.S diff --git a/eio/client/dune b/eio/client/dune index 90905c036..5329344da 100644 --- a/eio/client/dune +++ b/eio/client/dune @@ -1,10 +1,10 @@ (library - (name dns_client_eio) + (name dns_client_eio) + (modules dns_client_eio) (public_name dns-client-eio) (libraries cstruct duration - logs ipaddr dns-client dns-client.resolvconf @@ -12,3 +12,9 @@ mirage-crypto-rng mirage-crypto-rng-eio)) +(executable + (name ohost) + (modules ohost) + (public_name dns-client-eio.ohost) + (package dns-client-eio) + (libraries dns-client-eio mtime.clock.os eio_main domain-name fmt)) diff --git a/eio/client/ohost.ml b/eio/client/ohost.ml new file mode 100644 index 000000000..7001ef0bd --- /dev/null +++ b/eio/client/ohost.ml @@ -0,0 +1,48 @@ +(** + A Simple command line app to demonstrate usage of dns-client-eio package. + Usage: ohost.exe [HOSTNAME] + e.g: ohost.exe google.com +*) + +let (let+) r f = Result.map f r + +let display_host_ips h_name = + Eio_main.run @@ fun env -> + Eio.Switch.run @@ fun sw -> + Dns_client_eio.run env @@ fun (module Client) -> + let env = (env :> Dns_client_eio.env) in + let c = Client.create (env, sw) in + let domain = Domain_name.(host_exn (of_string_exn h_name)) in + let ipv4 = + let+ addr = Client.gethostbyname c domain in + Fmt.pr "%a has IPv4 address %a\n" Domain_name.pp domain Ipaddr.V4.pp addr + in + let ipv6 = + let+ addr = Client.gethostbyname6 c domain in + Fmt.pr "%a has IPv6 address %a\n" Domain_name.pp domain Ipaddr.V6.pp addr + in + let mx = + let+ (_ttl,resp) = Client.getaddrinfo c Mx domain in + Fmt.pr "%a\n" + (Fmt.list (fun ppf -> + Fmt.pf ppf "%a mail is handled by %a" + Domain_name.pp domain + Dns.Mx.pp)) + (Dns.Rr_map.Mx_set.elements resp); + in + let results = [ ipv4 ; ipv6 ; mx ] in + let is_error = (function Error _ -> true | Ok _ -> false) in + match List.find_opt is_error results with + | None | Some Ok _ -> () (* no errors *) + | Some (Error `Msg msg) -> (* at least one error *) + if List.for_all is_error results then begin + (Fmt.epr "Host %a not found: @[%s@]\n") Domain_name.pp domain msg; + exit 1 + end + +let () = + if Array.length Sys.argv <= 1 then + Printf.printf "Usage: ohost.exe [HOSTNAME]\n\nFor example:\nohost.exe google.com\nohost.exe firefox.com" + else + let h_name = Sys.argv.(1) in + display_host_ips h_name