]> git.ipfire.org Git - people/ms/network.git/commitdiff
libnetwork: Get index for interfaces
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 4 Feb 2018 17:11:16 +0000 (17:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 4 Feb 2018 17:11:16 +0000 (17:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
configure.ac
src/libnetwork/interface.c

index 309ff79e8580f89404d4a635916812976f603d5a..faec66bfa12257a911790244d7703db64e135ecf 100644 (file)
@@ -98,6 +98,7 @@ AC_SUBST([OUR_LDFLAGS], $with_ldflags)
 AC_CHECK_HEADERS_ONCE([
        ctype.h
        errno.h
+       net/if.h
        stdarg.h
        stdio.h
        stdlib.h
index 383941c0f1e0bfb91008cf804b3c5038c2e98de7..02ccbdc2c57f933ac86f3c65d694bfb409e8722f 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <net/if.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -31,6 +32,7 @@ struct network_interface {
        struct network_ctx* ctx;
        int refcount;
 
+       unsigned int index;
        char* name;
 };
 
@@ -39,6 +41,12 @@ NETWORK_EXPORT int network_interface_new(struct network_ctx* ctx,
        if (!name)
                return -EINVAL;
 
+       unsigned int index = if_nametoindex(name);
+       if (!index) {
+               ERROR(ctx, "Could not find interface %s\n", name);
+               return -ENODEV;
+       }
+
        struct network_interface* i = calloc(1, sizeof(*i));
        if (!i)
                return -ENOMEM;
@@ -46,6 +54,7 @@ NETWORK_EXPORT int network_interface_new(struct network_ctx* ctx,
        // Initialise object
        i->ctx = network_ref(ctx);
        i->refcount = 1;
+       i->index = index;
        i->name = strdup(name);
 
        DEBUG(i->ctx, "Allocated network interface at %p\n", i);