summaryrefslogtreecommitdiff
path: root/src/iface/iface.c
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2023-02-04 08:16:22 -0600
committerJohn Estabrook <jestabro@vyos.io>2023-02-05 18:38:43 -0600
commit04d43b4e9b4b469c281bc3638d6979dd1b43dd18 (patch)
treece1666aa2b4608e38e4d85036e09ce3929685465 /src/iface/iface.c
parent4d701e3a8a48465161c003633a2f7530f5b8d99f (diff)
downloadvyos-utils-04d43b4e9b4b469c281bc3638d6979dd1b43dd18.tar.gz
vyos-utils-04d43b4e9b4b469c281bc3638d6979dd1b43dd18.zip
T4952: add wrapper to getifaddrs for completion helper list_interfaces
Diffstat (limited to 'src/iface/iface.c')
-rw-r--r--src/iface/iface.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/iface/iface.c b/src/iface/iface.c
new file mode 100644
index 0000000..bf2f025
--- /dev/null
+++ b/src/iface/iface.c
@@ -0,0 +1,38 @@
+/*
+ * Simple wrapper of getifaddrs for OCaml list of interfaces
+ */
+#include <ifaddrs.h>
+#include <caml/mlvalues.h>
+#include <caml/memory.h>
+#include <caml/alloc.h>
+
+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);
+}