mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-24 19:28:09 +00:00
miragesdk: remove dead-code and code repetition
Signed-off-by: Thomas Gazagnaire <thomas@gazagnaire.org>
This commit is contained in:
parent
95d362ab7e
commit
0d7e584e32
@ -5,15 +5,6 @@ open Astring
|
|||||||
let src = Logs.Src.create "dhcp-client" ~doc:"DHCP client"
|
let src = Logs.Src.create "dhcp-client" ~doc:"DHCP client"
|
||||||
module Log = (val Logs.src_log src : Logs.LOG)
|
module Log = (val Logs.src_log src : Logs.LOG)
|
||||||
|
|
||||||
let failf fmt = Fmt.kstrf Lwt.fail_with fmt
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
module Handlers = struct
|
module Handlers = struct
|
||||||
|
|
||||||
(* System handlers *)
|
(* System handlers *)
|
||||||
@ -23,27 +14,27 @@ module Handlers = struct
|
|||||||
| `Updated (_, (_, `Contents (v, _))) -> Some v
|
| `Updated (_, (_, `Contents (v, _))) -> Some v
|
||||||
| _ -> None
|
| _ -> None
|
||||||
|
|
||||||
|
let with_ip str f =
|
||||||
|
match Ipaddr.V4.of_string (String.trim str) with
|
||||||
|
| Some ip ->
|
||||||
|
Log.info (fun l -> l "SET IP to %a" Ipaddr.V4.pp_hum ip);
|
||||||
|
f ip
|
||||||
|
| None ->
|
||||||
|
Log.err (fun l -> l "%s is not a valid IP" str);
|
||||||
|
Lwt.return_unit
|
||||||
|
|
||||||
let ip ~ethif t =
|
let ip ~ethif t =
|
||||||
Ctl.KV.watch_key t ["ip"] (fun diff ->
|
Ctl.KV.watch_key t ["ip"] (fun diff ->
|
||||||
match contents_of_diff diff with
|
match contents_of_diff diff with
|
||||||
| None -> Lwt.return_unit
|
| None -> Lwt.return_unit
|
||||||
| Some ip ->
|
| Some ip -> with_ip ip (fun ip -> Net.set_ip ethif ip)
|
||||||
let ip = String.trim ip in
|
|
||||||
Log.info (fun l -> l "SET IP to %s" ip);
|
|
||||||
(* FIXME: use language bindings to netlink instead *)
|
|
||||||
run "ifconfig %s %s netmask 255.255.255.0" ethif ip
|
|
||||||
(* run "ip addr add %s/24 dev %s" ip ethif *)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
let gateway t =
|
let gateway t =
|
||||||
Ctl.KV.watch_key t ["gateway"] (fun diff ->
|
Ctl.KV.watch_key t ["gateway"] (fun diff ->
|
||||||
match contents_of_diff diff with
|
match contents_of_diff diff with
|
||||||
| None -> Lwt.return_unit
|
| None -> Lwt.return_unit
|
||||||
| Some gw ->
|
| Some gw -> with_ip gw (fun gw -> Net.set_gateway gw)
|
||||||
let gw = String.trim gw in
|
|
||||||
Log.info (fun l -> l "SET GATEWAY to %s" gw);
|
|
||||||
(* FIXME: use language bindings to netlink instead *)
|
|
||||||
run "ip route add default via %s" gw
|
|
||||||
)
|
)
|
||||||
|
|
||||||
let handlers ~ethif = [
|
let handlers ~ethif = [
|
||||||
@ -52,7 +43,7 @@ module Handlers = struct
|
|||||||
]
|
]
|
||||||
|
|
||||||
let watch ~ethif db =
|
let watch ~ethif db =
|
||||||
Lwt_list.map_p (fun f -> f db) (handlers ethif) >>= fun _ ->
|
Lwt_list.map_p (fun f -> f db) (handlers ~ethif) >>= fun _ ->
|
||||||
let t, _ = Lwt.task () in
|
let t, _ = Lwt.task () in
|
||||||
t
|
t
|
||||||
|
|
||||||
|
@ -45,8 +45,8 @@ let forward ~src ~dst =
|
|||||||
Log.err (fun l -> l "forward[%a => %a] %a" pp src pp dst pp_error e);
|
Log.err (fun l -> l "forward[%a => %a] %a" pp src pp dst pp_error e);
|
||||||
Lwt.return_unit
|
Lwt.return_unit
|
||||||
| Ok (`Data buf) ->
|
| Ok (`Data buf) ->
|
||||||
Log.debug (fun l -> l "forward[%a => %a] %a"
|
Log.debug (fun l -> l "forward[%a => %a] @[%S@]"
|
||||||
pp src pp dst Cstruct.hexdump_pp buf);
|
pp src pp dst @@ Cstruct.to_string buf);
|
||||||
write dst buf >>= function
|
write dst buf >>= function
|
||||||
| Ok () -> loop ()
|
| Ok () -> loop ()
|
||||||
| Error e ->
|
| Error e ->
|
||||||
|
Loading…
Reference in New Issue
Block a user