package hvsock

  1. Overview
  2. Docs
Bindings for Hyper-V AF_VSOCK

Install

dune-project
 Dependency

Authors

Maintainers

Sources

hvsock-3.1.0.tbz
sha256=c3dc439a50b2b83dc584b20726a1311ca52187792613e9cf441c39da51ef7407
sha512=cd631f190c63dabd8151e51715d048c828aa9a8207661a6b06c5b7754de030ebec81f06d6c545afc3b7fc2637466ebe4ac5f205e4cbad672591f79f9c5366242

doc/src/hvsock/socket.ml.html

Source file socket.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
(*
 * Copyright (C) 2018 Docker Inc
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *)

type t = Unix.file_descr

type platform =
  | Windows
  | Linux
  | Mac
  | Unsupported of string

let finally f g =
  try
    let result = f () in
    g ();
    result
  with e ->
    g ();
    raise e

let startswith prefix line =
  let prefix_length = String.length prefix
  and line_length = String.length line in
  prefix_length <= line_length && (String.sub line 0 prefix_length = prefix)

let platform = match Sys.os_type with
  | "Win32" -> Windows
  | "Unix" ->
    let read_line f =
      finally
        (fun () -> input_line f)
        (fun () -> close_in f) in
    begin
      try
        let line = read_line (open_in "/proc/version") in
        if startswith "Linux" line
        then Linux
        else Unsupported line
      with _ ->
        (* Maybe it's a Mac *)
        begin
          let line = read_line (Unix.open_process_in "uname") in
          if startswith "Darwin" line
          then Mac
          else Unsupported line
        end
    end
  | x -> Unsupported x    

exception Unsupported_platform of string

let create () = match platform with
  | Windows -> Af_hyperv.create ()
  | Linux -> Af_vsock.create ()
  | Mac -> Hyperkit.create ()
  | Unsupported x -> raise (Unsupported_platform x)

type port =
  | Port of Af_vsock.port
  | Serviceid of Af_hyperv.serviceid

let serviceid_of_port = function
  | Port x -> Printf.sprintf "%08lx-FACB-11E6-BD58-64006A7986D3" x
  | Serviceid x -> x

let port_of_port = function
  | Port x -> x
  | Serviceid x ->
    try
        Scanf.sscanf x "%08lx-FACB-11E6-BD58-64006A7986D3" (fun x -> x)
    with _ ->
        raise (Unsupported_platform "Generic service IDs are only supported on Windows")

type peer =
  | Any
  | CID of Af_vsock.cid
  | VMID of Af_hyperv.vmid
  | Hyperkit of string

type sockaddr = peer * port

let string_of_sockaddr (peer, port) =
  let string_of_peer = function
    | Any    -> "Any"
    | CID x  -> Printf.sprintf "CID %s" (Af_vsock.string_of_cid x)
    | VMID x -> Printf.sprintf "VMID %s" (Af_hyperv.string_of_vmid x)
    | Hyperkit x -> Printf.sprintf "Hyperkit %s" x in
  let string_of_port = function
    | Port x      -> Int32.to_string x
    | Serviceid x -> x in
  Printf.sprintf "Socket { peer = %s; port = %s }" (string_of_peer peer) (string_of_port port)

let sockaddr_of_uri uri =
  let strip_slash x =
    if x = ""
    then ""
    else
      if x.[0] = '/'
      then String.sub x 1 (String.length x - 1)
      else x in
match Uri.scheme uri, Uri.host uri, Uri.port uri, Uri.path uri with
  | Some "vsock", Some "", Some port, _ -> Any, Port (Int32.of_int port)
  | Some "vsock", Some cid, Some port, _ -> CID (Af_vsock.Id (Int32.of_string cid)), Port (Int32.of_int port)
  | Some "hvsock", Some "", _, serviceid -> Any, Serviceid (strip_slash serviceid)
  | Some "hvsock", Some vmid, _, serviceid ->
    let vmid = match Uuidm.of_string vmid with
    | None ->
      (* Attempt to look up the UUID from the name *)
      Af_hyperv.vmid_of_name vmid
    | Some vmid -> vmid in
    VMID (Af_hyperv.Id vmid), Serviceid (strip_slash serviceid)
  | Some "hyperkit", _, Some port, hyperkit_path ->
    (* Support relative paths which start with "/." *)
    let hyperkit_path =
      if String.length hyperkit_path >= 2 && String.sub hyperkit_path 0 2 = "/."
      then String.sub hyperkit_path 1 (String.length hyperkit_path - 1)
      else hyperkit_path in
    Hyperkit hyperkit_path, Port (Int32.of_int port)
  | _, _, _, _ -> invalid_arg "sockaddr_of_uri"

let vmid_of_peer = function
  | Any -> Af_hyperv.Wildcard
  | VMID x -> x
  | Hyperkit _ -> raise (Unsupported_platform "Hyperkit is only supported on Mac")
  | CID _ -> raise (Unsupported_platform "CIDs are only supported on Linux")

let cid_of_peer = function
  | Any -> Af_vsock.Any
  | CID x -> x
  | Hyperkit _ -> raise (Unsupported_platform "Hyperkit is only supported on Mac")
  | VMID _ -> raise (Unsupported_platform "VMIDs are only supported on Windows")

let path_of_peer = function
  | Hyperkit x -> x
  | _ -> raise (Unsupported_platform "Mac only supports Hyperkit")

let bind fd (peer, port) = match platform with
  | Windows -> Af_hyperv.bind fd { Af_hyperv.vmid = vmid_of_peer peer; serviceid = serviceid_of_port port }
  | Linux   -> Af_vsock.bind fd { Af_vsock.cid = cid_of_peer peer; port = port_of_port port }
  | Mac -> Hyperkit.bind fd { Hyperkit.hyperkit_path = path_of_peer peer; port = port_of_port port }
  | Unsupported x -> raise (Unsupported_platform x)

let accept fd = match platform with
  | Windows ->
    let fd', { Af_hyperv.vmid; serviceid } = Af_hyperv.accept fd in
    fd', (VMID vmid, Serviceid serviceid)
  | Linux ->
    let fd', { Af_vsock.cid; port } = Af_vsock.accept fd in
    fd', (CID cid, Port port)
  | Mac ->
    let fd', { Hyperkit.hyperkit_path; port } = Hyperkit.accept fd in
    fd', (Hyperkit hyperkit_path, Port port)
  | Unsupported x -> raise (Unsupported_platform x)

let to_hyperv (peer, port) =
  try
    let vmid = vmid_of_peer peer in
    let serviceid = serviceid_of_port port in
    Some { Af_hyperv.vmid; serviceid }
  with _ -> None

let to_vsock (peer, port) =
  try
    let cid = cid_of_peer peer in
    let port = port_of_port port in
    Some { Af_vsock.cid; port }
  with _ -> None

let to_hyperkit (peer, port) =
  try
    let hyperkit_path = path_of_peer peer in
    let port = port_of_port port in
    Some Hyperkit.({hyperkit_path; port })
  with _ -> None

let connect ?timeout_ms fd sockaddr = match platform, sockaddr with
  | Windows, (peer, port) ->
    let vmid = vmid_of_peer peer in
    let serviceid = serviceid_of_port port in
    Af_hyperv.connect ?timeout_ms fd { Af_hyperv.vmid; serviceid }
  | Linux, (peer, port) ->
    let cid = cid_of_peer peer in
    let port = port_of_port port in
    Af_vsock.connect ?timeout_ms fd { Af_vsock.cid; port }
  | Mac, (peer, port) ->
    let path = path_of_peer peer in
    let port = port_of_port port in
    Hyperkit.connect ?timeout_ms fd Hyperkit.({hyperkit_path = path; port = port})
  | Unsupported x, _ -> raise (Unsupported_platform x)

let read_into = Af_common.read_into
let writev = Af_common.writev
let shutdown_read fd = Unix.shutdown fd Unix.SHUTDOWN_RECEIVE
let shutdown_write fd = Unix.shutdown fd Unix.SHUTDOWN_SEND
let close = Unix.close
let listen = Unix.listen
OCaml

Innovation. Community. Security.