From a89b0e79b00aa656cede3c6527cfda23b5ea19b1 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 22 May 2024 13:37:52 +0100 Subject: T6380: Move list_interfaces code to a dedicated dir for completion helpers --- src/completion/list_interfaces/func.ml | 1 + src/completion/list_interfaces/func.mli | 1 + src/completion/list_interfaces/iface.c | 38 ++++++ src/completion/list_interfaces/list_interfaces.ml | 135 ++++++++++++++++++++++ src/iface/func.ml | 1 - src/iface/func.mli | 1 - src/iface/iface.c | 38 ------ src/iface/list_interfaces.ml | 135 ---------------------- 8 files changed, 175 insertions(+), 175 deletions(-) create mode 100644 src/completion/list_interfaces/func.ml create mode 100644 src/completion/list_interfaces/func.mli create mode 100644 src/completion/list_interfaces/iface.c create mode 100644 src/completion/list_interfaces/list_interfaces.ml delete mode 100644 src/iface/func.ml delete mode 100644 src/iface/func.mli delete mode 100644 src/iface/iface.c delete mode 100644 src/iface/list_interfaces.ml (limited to 'src') diff --git a/src/completion/list_interfaces/func.ml b/src/completion/list_interfaces/func.ml new file mode 100644 index 0000000..13e1860 --- /dev/null +++ b/src/completion/list_interfaces/func.ml @@ -0,0 +1 @@ +external list_interfaces: unit -> string list = "interface_list" diff --git a/src/completion/list_interfaces/func.mli b/src/completion/list_interfaces/func.mli new file mode 100644 index 0000000..b16d373 --- /dev/null +++ b/src/completion/list_interfaces/func.mli @@ -0,0 +1 @@ +external list_interfaces : unit -> string list = "interface_list" diff --git a/src/completion/list_interfaces/iface.c b/src/completion/list_interfaces/iface.c new file mode 100644 index 0000000..bf2f025 --- /dev/null +++ b/src/completion/list_interfaces/iface.c @@ -0,0 +1,38 @@ +/* + * Simple wrapper of getifaddrs for OCaml list of interfaces + */ +#include +#include +#include +#include + +CAMLprim value interface_list(value unit) { + struct ifaddrs *ifaddr; + struct ifaddrs *ifa; + + CAMLparam1( unit ); + CAMLlocal2( cli, cons ); + + cli = Val_emptylist; + + if (getifaddrs(&ifaddr) == -1) { + CAMLreturn(cli); + } + for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_name == NULL) + continue; + + CAMLlocal1( ml_s ); + cons = caml_alloc(2, 0); + + ml_s = caml_copy_string(ifa->ifa_name); + Store_field( cons, 0, ml_s ); + Store_field( cons, 1, cli ); + + cli = cons; + } + + freeifaddrs(ifaddr); + + CAMLreturn(cli); +} diff --git a/src/completion/list_interfaces/list_interfaces.ml b/src/completion/list_interfaces/list_interfaces.ml new file mode 100644 index 0000000..fec5c9c --- /dev/null +++ b/src/completion/list_interfaces/list_interfaces.ml @@ -0,0 +1,135 @@ +(* + *) +let intf_type = ref "" +let broadcast = ref false +let bridgeable = ref false +let bondable = ref false +let no_vlan = ref false + +let args = [ + ("--type", Arg.String (fun s -> intf_type := s), "List interfaces of specified type"); + ("--broadcast", Arg.Unit (fun () -> broadcast := true), "List broadcast interfaces"); + ("--bridgeable", Arg.Unit (fun () -> bridgeable := true), "List bridgeable interfaces"); + ("--bondable", Arg.Unit (fun () -> bondable := true), "List bondable interfaces"); + ("--no-vlan-subinterfaces", Arg.Unit (fun () -> no_vlan := true), "List only parent interfaces"); +] +let usage = Printf.sprintf "Usage: %s [OPTIONS] " Sys.argv.(0) + +let () = Arg.parse args (fun _ -> ()) usage + +let type_to_prefix it = + match it with + | "" -> "" + | "bonding" -> "bond" + | "bridge" -> "br" + | "dummy" -> "dum" + | "ethernet" -> "eth" + | "geneve" -> "gnv" + | "input" -> "ifb" + | "l2tpeth" -> "l2tpeth" + | "loopback" -> "lo" + | "macsec" -> "macsec" + | "openvpn" -> "vtun" + | "pppoe" -> "pppoe" + | "pseudo-ethernet" -> "peth" + | "sstpc" -> "sstpc" + | "tunnel" -> "tun" + | "virtual-ethernet" -> "veth" + | "vti" -> "vti" + | "vxlan" -> "vxlan" + | "wireguard" -> "wg" + | "wireless" -> "wlan" + | "wwan" -> "wwan" + | _ -> "" + +(* filter_section to match the constraint of python.vyos.ifconfig.section + *) +let rx = Pcre.regexp {|\d(\d|v|\.)*$|} + +let filter_section s = + let r = Pcre.qreplace_first ~rex:rx ~templ:"" s in + match r with + |"bond"|"br"|"dum"|"eth"|"gnv"|"ifb"|"l2tpeth"|"lo"|"macsec" -> true + |"peth"|"pppoe"|"sstpc"|"tun"|"veth"|"vti"|"vtun"|"vxlan"|"wg"|"wlan"|"wwan" -> true + | _ -> false + +let filter_from_prefix p s = + let pattern = Printf.sprintf "^%s(.*)$" p + in + try + let _ = Pcre.exec ~pat:pattern s in + true + with Not_found -> false + +let filter_from_type it = + let pre = type_to_prefix it in + match pre with + | "" -> None + | _ -> Some (filter_from_prefix pre) + +let filter_broadcast s = + let pattern = {|^(bond|br|eth)(.*)$|} + in + try + let _ = Pcre.exec ~pat:pattern s in + true + with Not_found -> false + +let filter_bridgeable s = + let pattern = {|^(bond|eth|gnv|l2tpeth|lo|tun|veth|vtun|vxlan|wlan)(.*)$|} + in + try + let _ = Pcre.exec ~pat:pattern s in + true + with Not_found -> false + +let filter_bondable s = + let pattern = {|^(eth)(.*)$|} + in + try + let _ = Pcre.exec ~pat:pattern s in + true + with Not_found -> false + +let filter_no_vlan s = + let pattern = {|^([^.]+)(\.\d+)+$|} + in + try + let _ = Pcre.exec ~pat:pattern s in + false + with Not_found -> true + +let get_interfaces = + let intf_type = !intf_type in + let fltr = + if String.length(intf_type) > 0 then + filter_from_type intf_type + else None + in + let l = Func.list_interfaces () in + let res = List.sort_uniq compare l in + let res = + if !broadcast then List.filter filter_broadcast res + else res + in + let res = + if !bridgeable then List.filter filter_bridgeable res + else res + in + let res = + if !bondable then List.filter filter_bondable res + else res + in + let res = + if !no_vlan then List.filter filter_no_vlan res + else res + in + let res = List.filter filter_section res in + match fltr with + | Some f -> List.filter f res + | None -> res + +let () = + let res = get_interfaces in + List.iter (Printf.printf "%s ") res; + Printf.printf "\n" diff --git a/src/iface/func.ml b/src/iface/func.ml deleted file mode 100644 index 13e1860..0000000 --- a/src/iface/func.ml +++ /dev/null @@ -1 +0,0 @@ -external list_interfaces: unit -> string list = "interface_list" diff --git a/src/iface/func.mli b/src/iface/func.mli deleted file mode 100644 index b16d373..0000000 --- a/src/iface/func.mli +++ /dev/null @@ -1 +0,0 @@ -external list_interfaces : unit -> string list = "interface_list" diff --git a/src/iface/iface.c b/src/iface/iface.c deleted file mode 100644 index bf2f025..0000000 --- a/src/iface/iface.c +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Simple wrapper of getifaddrs for OCaml list of interfaces - */ -#include -#include -#include -#include - -CAMLprim value interface_list(value unit) { - struct ifaddrs *ifaddr; - struct ifaddrs *ifa; - - CAMLparam1( unit ); - CAMLlocal2( cli, cons ); - - cli = Val_emptylist; - - if (getifaddrs(&ifaddr) == -1) { - CAMLreturn(cli); - } - for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { - if (ifa->ifa_name == NULL) - continue; - - CAMLlocal1( ml_s ); - cons = caml_alloc(2, 0); - - ml_s = caml_copy_string(ifa->ifa_name); - Store_field( cons, 0, ml_s ); - Store_field( cons, 1, cli ); - - cli = cons; - } - - freeifaddrs(ifaddr); - - CAMLreturn(cli); -} diff --git a/src/iface/list_interfaces.ml b/src/iface/list_interfaces.ml deleted file mode 100644 index fec5c9c..0000000 --- a/src/iface/list_interfaces.ml +++ /dev/null @@ -1,135 +0,0 @@ -(* - *) -let intf_type = ref "" -let broadcast = ref false -let bridgeable = ref false -let bondable = ref false -let no_vlan = ref false - -let args = [ - ("--type", Arg.String (fun s -> intf_type := s), "List interfaces of specified type"); - ("--broadcast", Arg.Unit (fun () -> broadcast := true), "List broadcast interfaces"); - ("--bridgeable", Arg.Unit (fun () -> bridgeable := true), "List bridgeable interfaces"); - ("--bondable", Arg.Unit (fun () -> bondable := true), "List bondable interfaces"); - ("--no-vlan-subinterfaces", Arg.Unit (fun () -> no_vlan := true), "List only parent interfaces"); -] -let usage = Printf.sprintf "Usage: %s [OPTIONS] " Sys.argv.(0) - -let () = Arg.parse args (fun _ -> ()) usage - -let type_to_prefix it = - match it with - | "" -> "" - | "bonding" -> "bond" - | "bridge" -> "br" - | "dummy" -> "dum" - | "ethernet" -> "eth" - | "geneve" -> "gnv" - | "input" -> "ifb" - | "l2tpeth" -> "l2tpeth" - | "loopback" -> "lo" - | "macsec" -> "macsec" - | "openvpn" -> "vtun" - | "pppoe" -> "pppoe" - | "pseudo-ethernet" -> "peth" - | "sstpc" -> "sstpc" - | "tunnel" -> "tun" - | "virtual-ethernet" -> "veth" - | "vti" -> "vti" - | "vxlan" -> "vxlan" - | "wireguard" -> "wg" - | "wireless" -> "wlan" - | "wwan" -> "wwan" - | _ -> "" - -(* filter_section to match the constraint of python.vyos.ifconfig.section - *) -let rx = Pcre.regexp {|\d(\d|v|\.)*$|} - -let filter_section s = - let r = Pcre.qreplace_first ~rex:rx ~templ:"" s in - match r with - |"bond"|"br"|"dum"|"eth"|"gnv"|"ifb"|"l2tpeth"|"lo"|"macsec" -> true - |"peth"|"pppoe"|"sstpc"|"tun"|"veth"|"vti"|"vtun"|"vxlan"|"wg"|"wlan"|"wwan" -> true - | _ -> false - -let filter_from_prefix p s = - let pattern = Printf.sprintf "^%s(.*)$" p - in - try - let _ = Pcre.exec ~pat:pattern s in - true - with Not_found -> false - -let filter_from_type it = - let pre = type_to_prefix it in - match pre with - | "" -> None - | _ -> Some (filter_from_prefix pre) - -let filter_broadcast s = - let pattern = {|^(bond|br|eth)(.*)$|} - in - try - let _ = Pcre.exec ~pat:pattern s in - true - with Not_found -> false - -let filter_bridgeable s = - let pattern = {|^(bond|eth|gnv|l2tpeth|lo|tun|veth|vtun|vxlan|wlan)(.*)$|} - in - try - let _ = Pcre.exec ~pat:pattern s in - true - with Not_found -> false - -let filter_bondable s = - let pattern = {|^(eth)(.*)$|} - in - try - let _ = Pcre.exec ~pat:pattern s in - true - with Not_found -> false - -let filter_no_vlan s = - let pattern = {|^([^.]+)(\.\d+)+$|} - in - try - let _ = Pcre.exec ~pat:pattern s in - false - with Not_found -> true - -let get_interfaces = - let intf_type = !intf_type in - let fltr = - if String.length(intf_type) > 0 then - filter_from_type intf_type - else None - in - let l = Func.list_interfaces () in - let res = List.sort_uniq compare l in - let res = - if !broadcast then List.filter filter_broadcast res - else res - in - let res = - if !bridgeable then List.filter filter_bridgeable res - else res - in - let res = - if !bondable then List.filter filter_bondable res - else res - in - let res = - if !no_vlan then List.filter filter_no_vlan res - else res - in - let res = List.filter filter_section res in - match fltr with - | Some f -> List.filter f res - | None -> res - -let () = - let res = get_interfaces in - List.iter (Printf.printf "%s ") res; - Printf.printf "\n" -- cgit v1.2.3