Skip to content

Commit

Permalink
eio(client) : add ohost.exe sample app
Browse files Browse the repository at this point in the history
This commit adds `ohost.exe` sample app in the same spirit as
`dns-client.unix` package.
  • Loading branch information
bikallem committed Jun 24, 2022
1 parent aa9de1a commit 99244fb
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
12 changes: 6 additions & 6 deletions eio/client/dns_client_eio.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ 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
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
Expand All @@ -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
Expand Down Expand Up @@ -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. *)
Expand Down
2 changes: 1 addition & 1 deletion eio/client/dns_client_eio.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion eio/client/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(library
(name dns_client_eio)
(name dns_client_eio)
(modules dns_client_eio)
(public_name dns-client-eio)
(libraries
cstruct
Expand All @@ -10,3 +11,10 @@
dns-client.resolvconf
mtime.clock.os
mirage-crypto-rng-eio))

(executable
(name ohost)
(modules ohost)
(public_name dns-client-eio.eio)
(package dns-client-eio)
(libraries dns-client-eio mtime.clock.os eio_main domain-name fmt))
48 changes: 48 additions & 0 deletions eio/client/ohost.ml
Original file line number Diff line number Diff line change
@@ -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: @[<v>%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

0 comments on commit 99244fb

Please sign in to comment.