]> git.ipfire.org Git - people/ms/network.git/commitdiff
libnetwork: Add interface objects
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 2 Feb 2018 13:55:20 +0000 (13:55 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 2 Feb 2018 13:55:20 +0000 (13:55 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/libnetwork/interface.c [new file with mode: 0644]
src/libnetwork/libnetwork.sym
src/libnetwork/network/interface.h [new file with mode: 0644]

index f05805c1269d108bec893663d7a1c591555ea4b1..5e455a633a41f66f9f578d487e07776be6840691 100644 (file)
@@ -245,6 +245,7 @@ LIBNETWORK_REVISION=0
 LIBNETWORK_AGE=0
 
 pkginclude_HEADERS = \
+       src/libnetwork/network/interface.h \
        src/libnetwork/network/libnetwork.h \
        src/libnetwork/network/logging.h
 
@@ -252,6 +253,7 @@ lib_LTLIBRARIES = \
        src/libnetwork.la
 
 src_libnetwork_la_SOURCES = \
+       src/libnetwork/interface.c \
        src/libnetwork/libnetwork-private.h \
        src/libnetwork/libnetwork.c
 
diff --git a/src/libnetwork/interface.c b/src/libnetwork/interface.c
new file mode 100644 (file)
index 0000000..383941c
--- /dev/null
@@ -0,0 +1,88 @@
+/*#############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2018 IPFire Network Development Team                          #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <network/interface.h>
+#include <network/libnetwork.h>
+#include <network/logging.h>
+#include "libnetwork-private.h"
+
+struct network_interface {
+       struct network_ctx* ctx;
+       int refcount;
+
+       char* name;
+};
+
+NETWORK_EXPORT int network_interface_new(struct network_ctx* ctx,
+               struct network_interface** intf, const char* name) {
+       if (!name)
+               return -EINVAL;
+
+       struct network_interface* i = calloc(1, sizeof(*i));
+       if (!i)
+               return -ENOMEM;
+
+       // Initialise object
+       i->ctx = network_ref(ctx);
+       i->refcount = 1;
+       i->name = strdup(name);
+
+       DEBUG(i->ctx, "Allocated network interface at %p\n", i);
+
+       *intf = i;
+       return 0;
+}
+
+NETWORK_EXPORT struct network_interface* network_interface_ref(struct network_interface* intf) {
+       if (!intf)
+               return NULL;
+
+       intf->refcount++;
+       return intf;
+}
+
+static void network_interface_free(struct network_interface* intf) {
+       DEBUG(intf->ctx, "Released network interface at %p\n", intf);
+
+       if (intf->name)
+               free(intf->name);
+
+       network_unref(intf->ctx);
+       free(intf);
+}
+
+NETWORK_EXPORT struct network_interface* network_interface_unref(struct network_interface* intf) {
+       if (!intf)
+               return NULL;
+
+       if (--intf->refcount > 0)
+               return intf;
+
+       network_interface_free(intf);
+       return NULL;
+}
+
+NETWORK_EXPORT const char* network_interface_get_name(struct network_interface* intf) {
+       return intf->name;
+}
index 8e884c9643078413be8d1d49a8e4ac0df455438c..57426492effe4f93421e4e37d390fdd27e879930 100644 (file)
@@ -1,5 +1,9 @@
 LIBNETWORK_0 {
 global:
+        network_interface_get_name;
+        network_interface_new;
+        network_interface_ref;
+        network_interface_unref;
         network_new;
         network_ref;
         network_unref;
diff --git a/src/libnetwork/network/interface.h b/src/libnetwork/network/interface.h
new file mode 100644 (file)
index 0000000..6d823e8
--- /dev/null
@@ -0,0 +1,36 @@
+/*#############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2018 IPFire Network Development Team                          #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef NETWORK_INTERFACE_H
+#define NETWORK_INTERFACE_H
+
+#include <network/libnetwork.h>
+
+struct network_interface;
+
+int network_interface_new(struct network_ctx* ctx,
+       struct network_interface** intf, const char* name);
+
+struct network_interface* network_interface_ref(struct network_interface* intf);
+struct network_interface* network_interface_unref(struct network_interface* intf);
+
+const char* network_interface_get_name(struct network_interface* intf);
+
+#endif /* NETWORK_INTERFACE_H */