From 248629ae7541f4c0ff1e724fed3eadd658153b16 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 2 Feb 2018 13:55:20 +0000 Subject: [PATCH] libnetwork: Add interface objects Signed-off-by: Michael Tremer --- Makefile.am | 2 + src/libnetwork/interface.c | 88 ++++++++++++++++++++++++++++++ src/libnetwork/libnetwork.sym | 4 ++ src/libnetwork/network/interface.h | 36 ++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 src/libnetwork/interface.c create mode 100644 src/libnetwork/network/interface.h diff --git a/Makefile.am b/Makefile.am index f05805c..5e455a6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 0000000..383941c --- /dev/null +++ b/src/libnetwork/interface.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include +#include + +#include +#include +#include +#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; +} diff --git a/src/libnetwork/libnetwork.sym b/src/libnetwork/libnetwork.sym index 8e884c9..5742649 100644 --- a/src/libnetwork/libnetwork.sym +++ b/src/libnetwork/libnetwork.sym @@ -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 index 0000000..6d823e8 --- /dev/null +++ b/src/libnetwork/network/interface.h @@ -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 . # +# # +#############################################################################*/ + +#ifndef NETWORK_INTERFACE_H +#define NETWORK_INTERFACE_H + +#include + +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 */ -- 2.39.2