-
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
70 additions
and
8 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
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,54 @@ | ||
(** | ||
A Simple command line app to demonstrate usage of dns-client-eio package. | ||
Usage: ohost.exe [HOSTNAME] | ||
e.g: ohost.exe google.com | ||
*) | ||
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 = | ||
match Client.gethostbyname c domain with | ||
| Ok addr -> | ||
Fmt.pr "%a has IPv4 address %a\n" Domain_name.pp domain Ipaddr.V4.pp addr ; | ||
Ok () | ||
| Error _ as err -> err | ||
in | ||
let ipv6 = | ||
match Client.gethostbyname6 c domain with | ||
| Ok addr -> | ||
Fmt.pr "%a has IPv6 address %a\n" Domain_name.pp domain Ipaddr.V6.pp addr; | ||
Ok () | ||
| Error _ as err -> err | ||
in | ||
let mx = | ||
match Client.getaddrinfo c Mx domain with | ||
| Ok(_ttl, resp) -> | ||
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); | ||
Ok () | ||
| Error _ as err -> err | ||
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 |