Files
linuxkit/projects/miragesdk/src/sdk/net.ml
Thomas Gazagnaire df9833ed68 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>
2017-04-12 11:04:16 +02:00

29 lines
825 B
OCaml

(* 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