miragesdk: add missing Sdk.Net files

Probably my inconscient which refused to show the world these hacks.

Signed-off-by: Thomas Gazagnaire <thomas@gazagnaire.org>
This commit is contained in:
Thomas Gazagnaire 2017-04-07 18:26:03 +02:00
parent abe96b0447
commit df9833ed68
2 changed files with 38 additions and 0 deletions

View File

@ -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

View File

@ -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]. *)