Skip to content

Commit a4b5626

Browse files
committed
Merge pull request 'format' (#15) from format into main
Reviewed-on: https://git.robur.coop/robur/mnet/pulls/15
2 parents 08a84fb + 9962fca commit a4b5626

File tree

2 files changed

+59
-50
lines changed

2 files changed

+59
-50
lines changed

src/ipv6/neighbors.mli

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
(** IPv6 Neighbor Cache.
22
3-
The Neighbor Cache ({{:https://www.rfc-editor.org/rfc/rfc4861#section-5.1} RFC 4861 Section 5.1}})
4-
stores information about individual neighbors to which traffic has been sent
5-
recently. Each entry maps an IPv6 address to its link-layer (MAC) address,
6-
along with reachability state used by the Neighbor Unreachability Detection
7-
(NUD) algorithm.
8-
9-
When a packet needs to be sent to a neighbor whose MAC address is unknown,
10-
a Neighbor Solicitation is sent and the packet is queued until a Neighbor
3+
The Neighbor Cache
4+
({{:https://www.rfc-editor.org/rfc/rfc4861#section-5.1} RFC 4861 Section
5+
5.1}) stores information about individual neighbors to which traffic has
6+
been sent recently. Each entry maps an IPv6 address to its link-layer (MAC)
7+
address, along with reachability state used by the Neighbor Unreachability
8+
Detection (NUD) algorithm.
9+
10+
When a packet needs to be sent to a neighbor whose MAC address is unknown, a
11+
Neighbor Solicitation is sent and the packet is queued until a Neighbor
1112
Advertisement is received. *)
1213

1314
module Packet : sig

unikernels/echo/echo.ml

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
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

8-
let source_of_flow ?(close= ignore) flow =
8+
let source_of_flow ?(close = ignore) flow =
99
let init () = (flow, Bytes.create 0x7ff)
1010
and pull (flow, buf) =
1111
match Mnet.TCP.read flow buf with
12-
| exception _ | 0 -> None
12+
| (exception _) | 0 -> None
1313
| len ->
1414
let str = Bytes.sub_string buf 0 len in
1515
Some (str, (flow, buf))
1616
and stop (flow, _) = close flow in
1717
Flux.Source { init; pull; stop }
1818

19-
let sink_of_flow ?(close= ignore) flow =
19+
let sink_of_flow ?(close = ignore) flow =
2020
let init () = flow
2121
and push flow str = Mnet.TCP.write flow str; Miou.yield (); flow
2222
and full = Fun.const false
@@ -31,20 +31,26 @@ let handler flow =
3131
Option.iter Flux.Source.dispose src;
3232
Mnet.TCP.close flow
3333

34-
let rec clean_up orphans = match Miou.care orphans with
34+
let rec clean_up orphans =
35+
match Miou.care orphans with
3536
| None | Some None -> ()
3637
| Some (Some prm) ->
3738
let result = Miou.await prm in
38-
let fn err = Logs.err (fun m -> m "Unexpected error: %S" (Printexc.to_string err)) in
39+
let fn err =
40+
Logs.err (fun m -> m "Unexpected error: %S" (Printexc.to_string err))
41+
in
3942
Result.iter_error fn result;
4043
clean_up orphans
4144

42-
let rec terminate orphans = match Miou.care orphans with
45+
let rec terminate orphans =
46+
match Miou.care orphans with
4347
| None -> ()
4448
| Some None -> Mkernel.sleep 100_000_000; terminate orphans
4549
| Some (Some prm) ->
4650
let result = Miou.await prm in
47-
let fn err = Logs.err (fun m -> m "Unexpected error: %S" (Printexc.to_string err)) in
51+
let fn err =
52+
Logs.err (fun m -> m "Unexpected error: %S" (Printexc.to_string err))
53+
in
4854
Result.iter_error fn result;
4955
terminate orphans
5056

@@ -62,23 +68,29 @@ let run _quiet (cidrv4, gateway, ipv6) mode =
6268
match limit with
6369
| Some limit when limit <= 0 -> ()
6470
| None | Some _ ->
65-
let flow = Mnet.TCP.accept tcp listen in
66-
let _ = Miou.async ~orphans @@ fun () -> handler flow in
67-
let limit = Option.map pred limit in
68-
go orphans listen limit in
71+
let flow = Mnet.TCP.accept tcp listen in
72+
let _ = Miou.async ~orphans @@ fun () -> handler flow in
73+
let limit = Option.map pred limit in
74+
go orphans listen limit
75+
in
6976
let orphans = Miou.orphans () in
7077
go orphans (Mnet.TCP.listen tcp port) limit;
7178
terminate orphans
7279
| `Client (edn, length) ->
73-
let result = match edn with
80+
let result =
81+
match edn with
7482
| `Ipaddr edn -> Mnet_happy_eyeballs.connect_ip he [ edn ]
75-
| `Domain domain_name -> Mnet_happy_eyeballs.connect_host he domain_name [ 9000 ] in
76-
let flow = match result with
83+
| `Domain domain_name ->
84+
Mnet_happy_eyeballs.connect_host he domain_name [ 9000 ]
85+
in
86+
let flow =
87+
match result with
7788
| Ok (_, flow) -> flow
78-
| Error (`Msg msg) -> failwith msg in
89+
| Error (`Msg msg) -> failwith msg
90+
in
7991
let@ () = fun () -> Mnet.TCP.close flow in
8092
let buf = Bytes.create 0x7ff in
81-
let rec go ctx0 ctx1 rem0 rem1 =
93+
let rec go ctx0 ctx1 rem0 rem1 =
8294
let len = Int.min rem0 (Bytes.length buf) in
8395
Mirage_crypto_rng.generate_into buf len;
8496
Mnet.TCP.write flow (Bytes.to_string buf) ~off:0 ~len;
@@ -87,29 +99,27 @@ let run _quiet (cidrv4, gateway, ipv6) mode =
8799
let len = Mnet.TCP.read flow buf in
88100
let ctx1 = Digestif.SHA1.feed_bytes ctx1 buf ~off:0 ~len in
89101
let rem1 = rem1 - len in
90-
if rem0 <= 0 && rem1 <= 0
91-
then Digestif.SHA1.(get ctx0, get ctx1)
102+
if rem0 <= 0 && rem1 <= 0 then Digestif.SHA1.(get ctx0, get ctx1)
92103
else if rem0 > 0 then go ctx0 ctx1 rem0 rem1
93104
else (* if rem1 > 0 *)
94105
let () = Mnet.TCP.shutdown flow `write in
95-
remaining (Digestif.SHA1.get ctx0) ctx1 rem1
106+
remaining (Digestif.SHA1.get ctx0) ctx1 rem1
96107
and remaining hash0 ctx1 rem1 =
97108
match Mnet.TCP.read flow buf with
98-
| 0 -> hash0, Digestif.SHA1.get ctx1
109+
| 0 -> (hash0, Digestif.SHA1.get ctx1)
99110
| len ->
100111
let ctx1 = Digestif.SHA1.feed_bytes ctx1 buf ~off:0 ~len in
101112
let rem1 = rem1 - len in
102113
if rem1 > 0 then remaining hash0 ctx1 rem1
103-
else hash0, Digestif.SHA1.get ctx1
114+
else (hash0, Digestif.SHA1.get ctx1)
115+
in
116+
let hash0, hash1 =
117+
go Digestif.SHA1.empty Digestif.SHA1.empty length length
104118
in
105-
let hash0, hash1 = go Digestif.SHA1.empty Digestif.SHA1.empty length length in
106119
if not (Digestif.SHA1.equal hash0 hash1) then exit 1
107120

108-
let run_client _quiet mnet edn length =
109-
run _quiet mnet (`Client (edn, length))
110-
111-
let run_server _quiet mnet port limit =
112-
run _quiet mnet (`Server (port, limit))
121+
let run_client _quiet mnet edn length = run _quiet mnet (`Client (edn, length))
122+
let run_server _quiet mnet port limit = run _quiet mnet (`Server (port, limit))
113123

114124
open Cmdliner
115125

@@ -201,44 +211,42 @@ let length =
201211
value & pos 1 int 4096 & info [] ~doc ~docv:"NUMBER"
202212

203213
let limit =
204-
let doc = "Number of clients that the server can handle. Then, it terminates." in
214+
let doc =
215+
"Number of clients that the server can handle. Then, it terminates."
216+
in
205217
let open Arg in
206218
value & opt (some int) None & info [ "limit" ] ~doc ~docv:"NUMBER"
207219

208220
let addr =
209221
let doc = "The address of the echo server." in
210-
let parser str = match Ipaddr.with_port_of_string ~default:9000 str with
222+
let parser str =
223+
match Ipaddr.with_port_of_string ~default:9000 str with
211224
| Ok (ipaddr, port) -> Ok (`Ipaddr (ipaddr, port))
212225
| Error _ -> begin
213226
match Result.bind (Domain_name.of_string str) Domain_name.host with
214227
| Ok domain_name -> Ok (`Domain domain_name)
215-
| Error _ -> error_msgf "Invalid echo server: %S" str end in
228+
| Error _ -> error_msgf "Invalid echo server: %S" str
229+
end
230+
in
216231
let pp ppf = function
217232
| `Ipaddr (ipaddr, port) -> Fmt.pf ppf "%a:%d" Ipaddr.pp ipaddr port
218-
| `Domain domain_name -> Domain_name.pp ppf domain_name in
233+
| `Domain domain_name -> Domain_name.pp ppf domain_name
234+
in
219235
let ipaddr_and_port = Arg.conv (parser, pp) in
220236
let open Arg in
221237
required & pos 0 (some ipaddr_and_port) None & info [] ~doc ~docv:"IP:PORT"
222238

223239
let term_server =
224240
let open Term in
225-
const run_server
226-
$ setup_logs
227-
$ Mnet_cli.setup
228-
$ port
229-
$ limit
241+
const run_server $ setup_logs $ Mnet_cli.setup $ port $ limit
230242

231243
let cmd_server =
232244
let info = Cmd.info "server" in
233245
Cmd.v info term_server
234246

235247
let term_client =
236248
let open Term in
237-
const run_client
238-
$ setup_logs
239-
$ Mnet_cli.setup
240-
$ addr
241-
$ length
249+
const run_client $ setup_logs $ Mnet_cli.setup $ addr $ length
242250

243251
let cmd_client =
244252
let info = Cmd.info "client" in

0 commit comments

Comments
 (0)