-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eio(client): add ohost.exe sample app
This commit adds `ohost.exe` sample app in the same spirit as `dns-client.unix` package.
- Loading branch information
Showing
4 changed files
with
64 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
(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 | ||
mtime.clock.os | ||
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |