Module Vmnet

macOS userspace network bridging.

type t

t contains the interface state for one vmnet interface.

val sexp_of_t : t -> Ppx_sexp_conv_lib.Sexp.t
type mode =
| Host_mode
| Shared_mode

mode controls the level of sharing exposed to the vmnet interface.

  • Host_mode lets the guest network interface communicate with other guest network interfaces in the host mode and to the native host.
  • Shared_mode lets the guest network interface reach the Internet using a network address translator.

Note that in MacOS X Yosemite, Host_mode also provides a NAT to the guest, but with the subnet and DNS options not set (so it has no way to communicate externally but can still retrieve host-local network configuration via DHCP).

val sexp_of_mode : mode -> Ppx_sexp_conv_lib.Sexp.t
val mode_of_sexp : Ppx_sexp_conv_lib.Sexp.t -> mode
type error =
| Failure
| Mem_failure
| Invalid_argument
| Setup_incomplete
| Invalid_access
| Packet_too_big
| Buffer_exhausted
| Too_many_packets
| Unknown of int

error represents hard failures from the underlying vmnet functions.

val sexp_of_error : error -> Ppx_sexp_conv_lib.Sexp.t
val error_of_sexp : Ppx_sexp_conv_lib.Sexp.t -> error
exception Error of error
exception Permission_denied
exception No_packets_waiting
val init : ?⁠mode:mode -> unit -> t

init ?mode will initialise a fresh vmnet interface, defaulting to Shared_mode for the output. Raises Error if something goes wrong.

val mac : t -> Macaddr.t

mac t will return the MAC address bound to the guest network interface.

val mtu : t -> int

mtu t will return the Maximum Transmission Unit (MTU) bound to the guest network interface.

val max_packet_size : t -> int

max_packet_size t will return the maximum allowed packet buffer that can be passed to write. Exceeding this will raise Packet_too_big from write. This is also the minimum buffer size that must be passed to read.

val set_event_handler : t -> unit

set_event_handler t will initalise the internal thread state in the library that listen for event notifications from the library. The wait_for_event function should not be called until this set_event_handler been called once.

val wait_for_event : t -> unit

wait_for_event t will block the current OCaml thread until an event notification has been received on the t vmnet interface.

val read : t -> Cstruct.t -> Cstruct.t

read t buf will read a network packet into the buf Cstruct.t and return a fresh subview that represents the packet with the correct length and offset. It will raise No_packets_waiting if there is nothing to read.

val write : t -> Cstruct.t -> unit

write t buf will transmit a network packet contained in buf. This will normally not block, but the vmnet interface isnt clear on whether this might happen.