summaryrefslogtreecommitdiff
path: root/src/iface/iface.c
blob: bf2f0250e5034f02bc7e55ca54b9549c92f915f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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);
}