diff --git a/projects/miragesdk/src/sdk/net.ml b/projects/miragesdk/src/sdk/net.ml new file mode 100644 index 000000000..d093888f5 --- /dev/null +++ b/projects/miragesdk/src/sdk/net.ml @@ -0,0 +1,28 @@ +(* This file is a big hack and should be replaced ASAP with proper bindings *) + +open Lwt.Infix + +let run fmt = + Fmt.kstrf (fun str -> + match Sys.command str with + | 0 -> Lwt.return () + | i -> Fmt.kstrf Lwt.fail_with "%S exited with code %d" str i + ) fmt + +let read fmt = + Fmt.kstrf (fun str -> + Lwt_process.pread ("/bin/sh", [|"/bin/sh"; "-c"; str|]) + ) fmt + +let mac ethif = + read "ifconfig -a %s | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'" + ethif >|= fun mac -> + Macaddr.of_string_exn (String.trim mac) + +let set_ip ethif ip = + (* FIXME: use language bindings to netlink instead *) + (* run "ip addr add %s/24 dev %s" ip ethif *) + run "ifconfig %s %a netmask 255.255.255.0" ethif Ipaddr.V4.pp_hum ip + +let set_gateway gw = + run "ip route add default via %a" Ipaddr.V4.pp_hum gw diff --git a/projects/miragesdk/src/sdk/net.mli b/projects/miragesdk/src/sdk/net.mli new file mode 100644 index 000000000..58ebdffe4 --- /dev/null +++ b/projects/miragesdk/src/sdk/net.mli @@ -0,0 +1,10 @@ +(** [Net] exposes low-level system functions related to network. *) + +val mac: string -> Macaddr.t Lwt.t +(** [mac e] is the MAC address of the interface [e]. *) + +val set_ip: string -> Ipaddr.V4.t -> unit Lwt.t +(** [set_ip e ip] sets [e]'s IP address to [ip]. *) + +val set_gateway: Ipaddr.V4.t -> unit Lwt.t +(** [set_gateway ip] set the default host gateway to [ip]. *)