mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-08-01 06:58:56 +00:00
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:
parent
abe96b0447
commit
df9833ed68
28
projects/miragesdk/src/sdk/net.ml
Normal file
28
projects/miragesdk/src/sdk/net.ml
Normal 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
|
10
projects/miragesdk/src/sdk/net.mli
Normal file
10
projects/miragesdk/src/sdk/net.mli
Normal 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]. *)
|
Loading…
Reference in New Issue
Block a user