Skip to content

Commit 08a84fb

Browse files
committed
Merge pull request 'resolver: accept the hostname to resolve as command-line argument' (#14) from minor into main
Reviewed-on: https://git.robur.coop/robur/mnet/pulls/14 Reviewed-by: dinosaure <romain.calascibetta@gmail.com>
2 parents f3eb0fe + 8b80f85 commit 08a84fb

File tree

2 files changed

+56
-37
lines changed

2 files changed

+56
-37
lines changed

unikernels/resolver/resolver.ml

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
module RNG = Mirage_crypto_rng.Fortuna
22
module Hash = Digestif.SHA1
3-
let ( let@ ) finally fn = Fun.protect ~finally fn
43

4+
let ( let@ ) finally fn = Fun.protect ~finally fn
55
let rng () = Mirage_crypto_rng_mkernel.initialize (module RNG)
66
let rng = Mkernel.map rng Mkernel.[]
77
let _5s = Duration.of_sec 5
8-
let robur_coop = Domain_name.(host_exn (of_string_exn "robur.coop"))
98

10-
let run _quiet (cidrv4, gateway, ipv6) nameservers =
9+
let run _quiet (cidrv4, gateway, ipv6) nameservers host =
1110
Mkernel.(run [ rng; Mnet.stack ~name:"service" ?gateway ~ipv6 cidrv4 ])
1211
@@ fun rng (daemon, tcp, udp) () ->
1312
let@ () = fun () -> Mnet.kill daemon in
@@ -17,8 +16,8 @@ let run _quiet (cidrv4, gateway, ipv6) nameservers =
1716
let dns = Mnet_dns.create ~nameservers (udp, he) in
1817
let t = Mnet_dns.transport dns in
1918
let@ () = fun () -> Mnet_dns.Transport.kill t in
20-
match Mnet_dns.gethostbyname dns robur_coop with
21-
| Ok ipv4 -> Fmt.pr "%a: %a\n%!" Domain_name.pp robur_coop Ipaddr.V4.pp ipv4
19+
match Mnet_dns.gethostbyname dns host with
20+
| Ok ipv4 -> Fmt.pr "%a: %a\n%!" Domain_name.pp host Ipaddr.V4.pp ipv4
2221
| Error (`Msg msg) -> Fmt.epr "%s\n%!" msg
2322

2423
open Cmdliner
@@ -103,43 +102,48 @@ let setup_logs =
103102
let nameserver_of_string str =
104103
let ( let* ) = Result.bind in
105104
begin match String.split_on_char ':' str with
106-
| "tls" :: rest ->
105+
| "tls" :: rest -> (
107106
let str = String.concat ":" rest in
108-
( match String.split_on_char '!' str with
109-
| [ nameserver ] ->
110-
let* ipaddr, port = Ipaddr.with_port_of_string ~default:853 nameserver in
107+
match String.split_on_char '!' str with
108+
| [ nameserver ] ->
109+
let* ipaddr, port =
110+
Ipaddr.with_port_of_string ~default:853 nameserver
111+
in
111112
let* authenticator = Ca_certs_nss.authenticator () in
112113
let* tls = Tls.Config.client ~authenticator () in
113114
Ok (`Tcp, `Tls (tls, ipaddr, port))
114-
| nameserver :: opt_hostname :: authenticator ->
115-
let* ipaddr, port = Ipaddr.with_port_of_string ~default:853 nameserver in
115+
| nameserver :: opt_hostname :: authenticator ->
116+
let* ipaddr, port =
117+
Ipaddr.with_port_of_string ~default:853 nameserver
118+
in
116119
let peer_name, data =
117120
match
118121
let* dn = Domain_name.of_string opt_hostname in
119122
Domain_name.host dn
120123
with
121-
| Ok hostname -> Some hostname, String.concat "!" authenticator
122-
| Error _ -> None, String.concat "!" (opt_hostname :: authenticator)
124+
| Ok hostname -> (Some hostname, String.concat "!" authenticator)
125+
| Error _ ->
126+
(None, String.concat "!" (opt_hostname :: authenticator))
123127
in
124-
let* authenticator = match data with
128+
let* authenticator =
129+
match data with
125130
| "" -> Ca_certs_nss.authenticator ()
126131
| data ->
127-
let* a = X509.Authenticator.of_string data in
128-
Ok (a (fun () -> Some (Mirage_ptime.now ())))
132+
let* a = X509.Authenticator.of_string data in
133+
Ok (a (fun () -> Some (Mirage_ptime.now ())))
129134
in
130135
let* tls = Tls.Config.client ~authenticator ?peer_name () in
131136
Ok (`Tcp, `Tls (tls, ipaddr, port))
132-
| [] -> assert false )
133-
| "tcp" :: nameserver ->
137+
| [] -> assert false)
138+
| "tcp" :: nameserver ->
134139
let str = String.concat ":" nameserver in
135140
let* ipaddr, port = Ipaddr.with_port_of_string ~default:53 str in
136141
Ok (`Tcp, `Plaintext (ipaddr, port))
137-
| "udp" :: nameserver ->
142+
| "udp" :: nameserver ->
138143
let str = String.concat ":" nameserver in
139144
let* ipaddr, port = Ipaddr.with_port_of_string ~default:53 str in
140145
Ok (`Udp, `Plaintext (ipaddr, port))
141-
| _ ->
142-
Error (`Msg ("Unable to decode nameserver " ^ str))
146+
| _ -> Error (`Msg ("Unable to decode nameserver " ^ str))
143147
end
144148

145149
let nsec_per_day = Int64.mul 86_400L 1_000_000_000L
@@ -152,7 +156,7 @@ let time () =
152156
let rem_ps = Int64.mul rem_ns ps_per_ns in
153157
Some (Ptime.v (Int64.to_int days, rem_ps))
154158

155-
let _8_8_8_8 = `Udp, `Plaintext (Ipaddr.of_string_exn "8.8.8.8", 53)
159+
let _8_8_8_8 = (`Udp, `Plaintext (Ipaddr.of_string_exn "8.8.8.8", 53))
156160

157161
let uncensoreddns_org =
158162
let ipaddr = Ipaddr.of_string_exn "89.233.43.71" in
@@ -170,33 +174,48 @@ let nameservers =
170174
let doc = "A DNS nameserver." in
171175
let parser = nameserver_of_string in
172176
let pp ppf (proto, nameserver) =
173-
match proto, nameserver with
174-
| `Udp, `Plaintext (ipaddr, port) -> Fmt.pf ppf "udp:%a:%d" Ipaddr.pp ipaddr port
175-
| `Tcp, `Plaintext (ipaddr, port) -> Fmt.pf ppf "tcp:%a:%d" Ipaddr.pp ipaddr port
176-
| `Tcp, `Tls (_, ipaddr, port) -> Fmt.pf ppf "tls:%a:%d" Ipaddr.pp ipaddr port
177-
| `Udp, _ -> assert false in
177+
match (proto, nameserver) with
178+
| `Udp, `Plaintext (ipaddr, port) ->
179+
Fmt.pf ppf "udp:%a:%d" Ipaddr.pp ipaddr port
180+
| `Tcp, `Plaintext (ipaddr, port) ->
181+
Fmt.pf ppf "tcp:%a:%d" Ipaddr.pp ipaddr port
182+
| `Tcp, `Tls (_, ipaddr, port) ->
183+
Fmt.pf ppf "tls:%a:%d" Ipaddr.pp ipaddr port
184+
| `Udp, _ -> assert false
185+
in
178186
let open Arg in
179-
value & opt_all (conv (parser, pp)) [ uncensoreddns_org ] & info [ "n"; "nameserver" ] ~doc ~docv:"NAMESERVER"
187+
value
188+
& opt_all (conv (parser, pp)) [ uncensoreddns_org ]
189+
& info [ "n"; "nameserver" ] ~doc ~docv:"NAMESERVER"
180190

181191
let setup_nameservers nameservers =
182192
let fn = function
183193
| `Udp, ns -> Either.Left ns
184-
| `Tcp, ns -> Either.Right ns in
194+
| `Tcp, ns -> Either.Right ns
195+
in
185196
match List.partition_map fn nameservers with
186-
| [], nss -> `Tcp, nss
187-
| nss, [] -> `Udp, nss
188-
| _ -> Fmt.failwith "It is impossible to mix multiple nameservers over TCP and UDP"
197+
| [], nss -> (`Tcp, nss)
198+
| nss, [] -> (`Udp, nss)
199+
| _ ->
200+
Fmt.failwith
201+
"It is impossible to mix multiple nameservers over TCP and UDP"
189202

190203
let setup_nameservers =
191204
let open Term in
192205
const setup_nameservers $ nameservers
193206

207+
let host =
208+
let doc = "Hostname to query for." in
209+
let parser s = Result.bind (Domain_name.of_string s) Domain_name.host in
210+
let robur_coop = Domain_name.(host_exn (of_string_exn "robur.coop")) in
211+
let open Arg in
212+
value
213+
& opt (conv (parser, Domain_name.pp)) robur_coop
214+
& info [ "host" ] ~doc ~docv:"HOST"
215+
194216
let term =
195217
let open Term in
196-
const run
197-
$ setup_logs
198-
$ Mnet_cli.setup
199-
$ setup_nameservers
218+
const run $ setup_logs $ Mnet_cli.setup $ setup_nameservers $ host
200219

201220
let cmd =
202221
let info = Cmd.info "dns" in

unikernels/source.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/sh
22

33
[ ! -d "vendors" ] && mkdir vendors
44
[ ! -d "vendors/bstr" ] && opam source bstr --dir vendors/bstr

0 commit comments

Comments
 (0)