Skip to content

Commit

Permalink
autoformat
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesm committed Sep 24, 2024
1 parent a0637e1 commit 33a08b1
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 157 deletions.
21 changes: 15 additions & 6 deletions app/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ let write_to_fd fd data =
let rec w fd data off =
if len = off then Lwt_result.return ()
else
let* len = Lwt_unix.write fd (Bytes.unsafe_of_string data) off (len - off) in
let* len =
Lwt_unix.write fd (Bytes.unsafe_of_string data) off (len - off)
in
w fd data (len + off)
in
Lwt.catch
Expand All @@ -26,16 +28,20 @@ let read_from_fd fd =
let buf = Bytes.create bufsize in
Lwt_unix.read fd buf 0 bufsize >>= fun count ->
if count = 0 then failwith "end of file from server"
else
Logs.debug (fun m -> m "read %d bytes" count);
Lwt.return (Bytes.sub_string buf 0 count))
else Logs.debug (fun m -> m "read %d bytes" count);
Lwt.return (Bytes.sub_string buf 0 count))
|> Lwt_result.map_error (fun e -> `Msg (Printexc.to_string e))

let transmit proto fd data =
match proto with
| `Tcp -> write_to_fd fd data
| `Udp -> (
let* r = Lwt_result.catch (fun () -> Lwt_unix.write fd (Bytes.unsafe_of_string data) 0 (String.length data)) in
let* r =
Lwt_result.catch (fun () ->
Lwt_unix.write fd
(Bytes.unsafe_of_string data)
0 (String.length data))
in
match r with
| Ok len when String.length data <> len ->
Lwt_result.fail (`Msg "wrote short UDP packet")
Expand All @@ -46,7 +52,10 @@ let transmit proto fd data =

let receive proto fd =
let buf = Bytes.create 2048 in
let* r = Lwt_result.catch (fun () -> Lwt_unix.recvfrom fd buf 0 (Bytes.length buf) []) in
let* r =
Lwt_result.catch (fun () ->
Lwt_unix.recvfrom fd buf 0 (Bytes.length buf) [])
in
match (r, proto) with
| Ok (0, _), `Tcp ->
Logs.debug (fun m -> m "received end of file");
Expand Down
8 changes: 6 additions & 2 deletions app/miragevpn_client_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,19 @@ let send_recv conn config ip_config _mtu routes =
Bytes.unsafe_to_string pre ^ pkt
| Linux -> pkt
in
Lwt_unix.write tun_fd (Bytes.unsafe_of_string pkt) 0 (String.length pkt) >|= ignore)
Lwt_unix.write tun_fd
(Bytes.unsafe_of_string pkt)
0 (String.length pkt)
>|= ignore)
pkts
>>= fun () -> process_incoming ()
in
let rec process_outgoing tun_fd =
let open Lwt_result.Infix in
let buf = Bytes.create 1500 in
(* on FreeBSD, the tun read is prepended with a 4 byte protocol (AF_INET) *)
( Lwt_unix.read tun_fd buf 0 (Bytes.length buf) |> Lwt_result.ok >|= fun len ->
( Lwt_unix.read tun_fd buf 0 (Bytes.length buf) |> Lwt_result.ok
>|= fun len ->
let start, len =
match Lazy.force platform with
| Linux -> (0, len)
Expand Down
8 changes: 3 additions & 5 deletions app/miragevpn_client_notun.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ let resolve (name, ip_version) =
| Ok ip -> `Resolved (Ipaddr.V6 ip))

type action =
[ Miragevpn.action
| `Suspend
| `Transmit of string
| `Payload of string ]
[ Miragevpn.action | `Suspend | `Transmit of string | `Payload of string ]

let pp_action ppf = function
| #Miragevpn.action as action -> Miragevpn.pp_action ppf action
Expand Down Expand Up @@ -95,7 +92,8 @@ let send_ping ({ ip_config; seq_no; mtu = _; ping = _ } as ifconfig) =
~payload_len:(Cstruct.lenv [ icmpv4_hdr; payload ])
ipv4_hdr
in
(ifconfig, Cstruct.to_string (Cstruct.concat [ ipv4_hdr; icmpv4_hdr; payload ]))
( ifconfig,
Cstruct.to_string (Cstruct.concat [ ipv4_hdr; icmpv4_hdr; payload ]) )

let pong { ip_config; _ } buf =
let buf = Cstruct.of_string buf in
Expand Down
4 changes: 3 additions & 1 deletion app/miragevpn_server_notun.ml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ let handle_payload t dst source_ip data =
subheader = Unused;
}
and ip' = { ip with src = fst t.ip; dst = ip.src } in
let payload = Cstruct.of_string data ~len:(min 28 (String.length data)) in
let payload =
Cstruct.of_string data ~len:(min 28 (String.length data))
in
let data =
Cstruct.append
(Icmpv4_packet.Marshal.make_cstruct ~payload reply)
Expand Down
8 changes: 6 additions & 2 deletions mirage/miragevpn_mirage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ struct
subheader = Unused;
}
and ip' = { ip with src = fst t.ip; dst = ip.src } in
let payload = Cstruct.of_string data ~len:(min 28 (String.length data)) in
let payload =
Cstruct.of_string data ~len:(min 28 (String.length data))
in
let data =
Cstruct.append
(Icmpv4_packet.Marshal.make_cstruct ~payload reply)
Expand Down Expand Up @@ -210,7 +212,9 @@ struct
rm ip;
TCP.close flow
| Ok (`Data cs) -> (
match Miragevpn.handle !client_state (`Data (Cstruct.to_string cs)) with
match
Miragevpn.handle !client_state (`Data (Cstruct.to_string cs))
with
| Error msg ->
Log.err (fun m ->
m "%a internal miragevpn error %a" pp_dst dst
Expand Down
18 changes: 4 additions & 14 deletions src/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -257,19 +257,11 @@ module Conf_map = struct
| Rport : int k
| Script_security : int k
| Secret
: ([ `Incoming | `Outgoing ] option
* string
* string
* string
* string)
: ([ `Incoming | `Outgoing ] option * string * string * string * string)
k
| Server : Ipaddr.V4.Prefix.t k
| Tls_auth
: ([ `Incoming | `Outgoing ] option
* string
* string
* string
* string)
: ([ `Incoming | `Outgoing ] option * string * string * string * string)
k
| Tls_cert : X509.Certificate.t k
| Tls_mode : [ `Client | `Server ] k
Expand Down Expand Up @@ -559,8 +551,7 @@ module Conf_map = struct
p () "ca [inline]\n<ca>\n%a</ca>" Fmt.(list ~sep:(any "\n") pp_x509) certs
in
let[@coverage off] pp_x509_private_key key =
p () "key [inline]\n<key>\n%s</key>"
(X509.Private_key.encode_pem key)
p () "key [inline]\n<key>\n%s</key>" (X509.Private_key.encode_pem key)
in
let[@coverage off] pp_tls_version ppf v =
Fmt.string ppf
Expand Down Expand Up @@ -1163,8 +1154,7 @@ let inline_payload element =
| _ -> false)
<* (end_of_line <|> abort "Invalid hex character")
>>= fun hex ->
try return (Ohex.decode hex)
with Invalid_argument msg -> abort msg )
try return (Ohex.decode hex) with Invalid_argument msg -> abort msg )
(string "-----END OpenVPN Static key V1-----" *> a_newline
<|> abort "Missing END mark")
<* commit
Expand Down
8 changes: 4 additions & 4 deletions src/config_ext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ let tls_auth config =
| Some (direction, _, hmac1, _, hmac2) ->
let hmac_algorithm = Config.get Auth config in
let hmac_len =
let module H = (val (Digestif.module_of_hash' hmac_algorithm)) in
let module H = (val Digestif.module_of_hash' hmac_algorithm) in
H.digest_size
in
let a, b =
Expand All @@ -67,11 +67,11 @@ let secret config =
| None -> Error (`Msg "no pre-shared secret found")
| Some (dir, key1, hmac1, key2, hmac2) -> (
let hmac_len =
let module H = (val (Digestif.module_of_hash' (Config.get Auth config))) in
let module H = (val Digestif.module_of_hash' (Config.get Auth config))
in
H.digest_size
in
let hm cs = String.sub cs 0 hmac_len
and cipher cs = String.sub cs 0 32 in
let hm cs = String.sub cs 0 hmac_len and cipher cs = String.sub cs 0 32 in
match dir with
| None -> Ok (cipher key1, hm hmac1, cipher key1, hm hmac1)
| Some `Incoming -> Ok (cipher key2, hm hmac2, cipher key1, hm hmac1)
Expand Down
Loading

0 comments on commit 33a08b1

Please sign in to comment.