]> git.ipfire.org Git - network.git/commitdiff
libnetwork: Add central context object
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 2 Feb 2018 12:18:37 +0000 (12:18 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 2 Feb 2018 12:18:37 +0000 (12:18 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
configure.ac
src/libnetwork/libnetwork.c
src/libnetwork/libnetwork.sym
src/libnetwork/network/libnetwork.h

index b2c06d0f0acfc549c1529170a36f8bfbf16b1a4d..e9384872e1366d4b0129e8bd5418b13dd00f6f1c 100644 (file)
@@ -93,6 +93,13 @@ CC_CHECK_FLAGS_APPEND([with_ldflags], [LDFLAGS], [\
        -Wl,-z,now])
 AC_SUBST([OUR_LDFLAGS], $with_ldflags)
 
+# ------------------------------------------------------------------------------
+
+AC_CHECK_HEADERS_ONCE([
+       errno.h
+       stdlib.h
+])
+
 # ------------------------------------------------------------------------------
 AC_ARG_WITH([systemdsystemunitdir],
        AS_HELP_STRING([--with-systemdsystemunitdir=DIR], [Directory for systemd service files]),
index 629a6b196751cc3a48071562a1ab3ae53168dade..6793fd5251aa646ad25a45035ed1c996cfe0c7b3 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <errno.h>
+#include <stdlib.h>
+
 #include <network/libnetwork.h>
 #include "libnetwork-private.h"
 
+struct network_ctx {
+       int refcount;
+};
+
+NETWORK_EXPORT int network_new(struct network_ctx** ctx) {
+       struct network_ctx* c = calloc(1, sizeof(*c));
+       if (!c)
+               return -ENOMEM;
+
+       // Initialise basic variables
+       c->refcount = 1;
+
+       *ctx = c;
+       return 0;
+}
+
+NETWORK_EXPORT struct network_ctx* network_ref(struct network_ctx* ctx) {
+       if (!ctx)
+               return NULL;
+
+       ctx->refcount++;
+       return ctx;
+}
+
+static void network_free(struct network_ctx* ctx) {
+       // Nothing to do, yet
+}
+
+NETWORK_EXPORT struct network_ctx* network_unref(struct network_ctx* ctx) {
+       if (!ctx)
+               return NULL;
+
+       if (--ctx->refcount > 0)
+               return ctx;
+
+       network_free(ctx);
+       return NULL;
+}
+
 NETWORK_EXPORT const char* network_version() {
        return "network " VERSION;
 }
index 5a5a464ab8d8422d8202ddae35bf0e6469ec247c..8e884c9643078413be8d1d49a8e4ac0df455438c 100644 (file)
@@ -1,5 +1,8 @@
 LIBNETWORK_0 {
 global:
+        network_new;
+        network_ref;
+        network_unref;
         network_version;
 local:
         *;
index 615a628905d2edf17fa0e9b527163a9931ce3167..48f7e68ccd19a070ad7f16faa0c287c32d2a5eb1 100644 (file)
 #ifndef LIBNETWORK_H
 #define LIBNETWORK_H
 
+// Central context for all network operations
+struct network_ctx;
+
+int network_new(struct network_ctx** ctx);
+struct network_ctx* network_ref(struct network_ctx* ctx);
+struct network_ctx* network_unref(struct network_ctx* ctx);
+
 const char* network_version();
 
 #endif