Skip to content

Commit

Permalink
Use Ipaddr.V4.Map instead of our own IpMap (the first is available si…
Browse files Browse the repository at this point in the history
…nce ipaddr.5.2.0)
  • Loading branch information
dinosaure committed May 22, 2024
1 parent 635f47e commit 9af4234
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
16 changes: 8 additions & 8 deletions client_eth.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let src = Logs.Src.create "client_eth" ~doc:"Ethernet networks for NetVM clients
module Log = (val Logs.src_log src : Logs.LOG)

type t = {
mutable iface_of_ip : client_link IpMap.t;
mutable iface_of_ip : client_link Ipaddr.V4.Map.t;
changed : unit Lwt_condition.t; (* Fires when [iface_of_ip] changes. *)
my_ip : Ipaddr.V4.t; (* The IP that clients are given as their default gateway. *)
}
Expand All @@ -21,33 +21,33 @@ type host =
let create config =
let changed = Lwt_condition.create () in
let my_ip = config.Dao.our_ip in
Lwt.return { iface_of_ip = IpMap.empty; my_ip; changed }
Lwt.return { iface_of_ip = Ipaddr.V4.Map.empty; my_ip; changed }

let client_gw t = t.my_ip

let add_client t iface =
let ip = iface#other_ip in
let rec aux () =
match IpMap.find ip t.iface_of_ip with
match Ipaddr.V4.Map.find_opt ip t.iface_of_ip with
| Some old ->
(* Wait for old client to disappear before adding one with the same IP address.
Otherwise, its [remove_client] call will remove the new client instead. *)
Log.info (fun f -> f ~header:iface#log_header "Waiting for old client %s to go away before accepting new one" old#log_header);
Lwt_condition.wait t.changed >>= aux
| None ->
t.iface_of_ip <- t.iface_of_ip |> IpMap.add ip iface;
t.iface_of_ip <- t.iface_of_ip |> Ipaddr.V4.Map.add ip iface;
Lwt_condition.broadcast t.changed ();
Lwt.return_unit
in
aux ()

let remove_client t iface =
let ip = iface#other_ip in
assert (IpMap.mem ip t.iface_of_ip);
t.iface_of_ip <- t.iface_of_ip |> IpMap.remove ip;
assert (Ipaddr.V4.Map.mem ip t.iface_of_ip);
t.iface_of_ip <- t.iface_of_ip |> Ipaddr.V4.Map.remove ip;
Lwt_condition.broadcast t.changed ()

let lookup t ip = IpMap.find ip t.iface_of_ip
let lookup t ip = Ipaddr.V4.Map.find_opt ip t.iface_of_ip

let classify t ip =
match ip with
Expand Down Expand Up @@ -79,7 +79,7 @@ module ARP = struct
(* We're now treating client networks as point-to-point links,
so we no longer respond on behalf of other clients. *)
(*
else match IpMap.find ip t.net.iface_of_ip with
else match Ipaddr.V4.Map.find_opt ip t.net.iface_of_ip with
| Some client_iface -> Some client_iface#other_mac
| None -> None
*)
Expand Down
8 changes: 0 additions & 8 deletions fw_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@

(** General utility functions. *)

module IpMap = struct
include Map.Make(Ipaddr.V4)
let find x map =
try Some (find x map)
with Not_found -> None
| _ -> Logs.err( fun f -> f "uncaught exception in find...%!"); None
end

(** An Ethernet interface. *)
class type interface = object
method my_mac : Macaddr.t
Expand Down

0 comments on commit 9af4234

Please sign in to comment.