Skip to content

Commit 83649fa

Browse files
committed
eio(client): add ohost.exe sample app
This commit adds `ohost.exe` sample app in the same spirit as `dns-client.unix` package.
1 parent 5709ff3 commit 83649fa

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

eio/client/dns_client_eio.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ type env = <
44
clock : E.Time.clock ;
55
net : E.Net.t;
66
fs : E.Dir.t;
7-
secure_random : Eio.Flow.source;
7+
secure_random : E.Flow.source;
88
>
99

1010
type io_addr = Ipaddr.t * int
1111
type stack = env * E.Switch.t
1212

1313
module Transport : Dns_client.S
1414
with type io_addr = io_addr
15-
and type stack = stack
16-
and type +'a io = 'a
15+
and type stack = stack
16+
and type +'a io = 'a
1717
= struct
1818
type nonrec io_addr = io_addr
1919
type nonrec stack = stack
@@ -36,7 +36,7 @@ module Transport : Dns_client.S
3636
}
3737

3838
let read_file env file =
39-
match E.Dir.load (E.Stdenv.fs env) file with
39+
match E.Dir.load env#fs file with
4040
| content -> Ok content
4141
| exception e ->
4242
let err = "Error while reading file: " ^ file ^ ". " ^ (Printexc.to_string e) in
@@ -159,8 +159,8 @@ module Transport : Dns_client.S
159159
let stream = `Tcp (ip, port) in
160160
try
161161
let timeout = Duration.to_f t.timeout_ns in
162-
E.Time.with_timeout_exn (E.Stdenv.clock t.env) timeout @@ fun () ->
163-
let flow = E.Net.connect ~sw:t.sw (E.Stdenv.net t.env) stream in
162+
E.Time.with_timeout_exn t.env#clock timeout @@ fun () ->
163+
let flow = E.Net.connect ~sw:t.sw t.env#net stream in
164164
Ok flow
165165
with E.Time.Timeout ->
166166
(* Push the non responsive nameserver to the back of the queue. *)

eio/client/dns_client_eio.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ type env = <
22
clock : Eio.Time.clock ;
33
net : Eio.Net.t ;
44
fs : Eio.Dir.t ;
5-
secure_random : Eio.Flow.source;
5+
secure_random : Eio.Flow.source ;
66
>
77

88
module Transport : Dns_client.S

eio/client/dune

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
(library
2-
(name dns_client_eio)
2+
(name dns_client_eio)
3+
(modules dns_client_eio)
34
(public_name dns-client-eio)
45
(libraries
56
cstruct
67
duration
7-
logs
88
ipaddr
99
dns-client
1010
dns-client.resolvconf
1111
mtime.clock.os
1212
mirage-crypto-rng-eio))
13+
14+
(executable
15+
(name ohost)
16+
(modules ohost)
17+
(public_name dns-client-eio.ohost)
18+
(package dns-client-eio)
19+
(libraries dns-client-eio mtime.clock.os eio_main domain-name fmt))

eio/client/ohost.ml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
(**
2+
A Simple command line app to demonstrate usage of dns-client-eio package.
3+
Usage: ohost.exe [HOSTNAME]
4+
e.g: ohost.exe google.com
5+
*)
6+
7+
let (let+) r f = Result.map f r
8+
9+
let display_host_ips h_name =
10+
Eio_main.run @@ fun env ->
11+
Eio.Switch.run @@ fun sw ->
12+
Dns_client_eio.run env @@ fun (module Client) ->
13+
let env = (env :> Dns_client_eio.env) in
14+
let c = Client.create (env, sw) in
15+
let domain = Domain_name.(host_exn (of_string_exn h_name)) in
16+
let ipv4 =
17+
let+ addr = Client.gethostbyname c domain in
18+
Fmt.pr "%a has IPv4 address %a\n" Domain_name.pp domain Ipaddr.V4.pp addr
19+
in
20+
let ipv6 =
21+
let+ addr = Client.gethostbyname6 c domain in
22+
Fmt.pr "%a has IPv6 address %a\n" Domain_name.pp domain Ipaddr.V6.pp addr
23+
in
24+
let mx =
25+
let+ (_ttl,resp) = Client.getaddrinfo c Mx domain in
26+
Fmt.pr "%a\n"
27+
(Fmt.list (fun ppf ->
28+
Fmt.pf ppf "%a mail is handled by %a"
29+
Domain_name.pp domain
30+
Dns.Mx.pp))
31+
(Dns.Rr_map.Mx_set.elements resp);
32+
in
33+
let results = [ ipv4 ; ipv6 ; mx ] in
34+
let is_error = (function Error _ -> true | Ok _ -> false) in
35+
match List.find_opt is_error results with
36+
| None | Some Ok _ -> () (* no errors *)
37+
| Some (Error `Msg msg) -> (* at least one error *)
38+
if List.for_all is_error results then begin
39+
(Fmt.epr "Host %a not found: @[<v>%s@]\n") Domain_name.pp domain msg;
40+
exit 1
41+
end
42+
43+
let () =
44+
if Array.length Sys.argv <= 1 then
45+
Printf.printf "Usage: ohost.exe [HOSTNAME]\n\nFor example:\nohost.exe google.com\nohost.exe firefox.com"
46+
else
47+
let h_name = Sys.argv.(1) in
48+
display_host_ips h_name

0 commit comments

Comments
 (0)