From: Michael Tremer Date: Fri, 2 Feb 2018 12:18:37 +0000 (+0000) Subject: libnetwork: Add central context object X-Git-Tag: 010~177 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0e8800488758e6ed9226654658060182057d6247;p=network.git libnetwork: Add central context object Signed-off-by: Michael Tremer --- diff --git a/configure.ac b/configure.ac index b2c06d0f..e9384872 100644 --- a/configure.ac +++ b/configure.ac @@ -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]), diff --git a/src/libnetwork/libnetwork.c b/src/libnetwork/libnetwork.c index 629a6b19..6793fd52 100644 --- a/src/libnetwork/libnetwork.c +++ b/src/libnetwork/libnetwork.c @@ -18,9 +18,51 @@ # # #############################################################################*/ +#include +#include + #include #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; } diff --git a/src/libnetwork/libnetwork.sym b/src/libnetwork/libnetwork.sym index 5a5a464a..8e884c96 100644 --- a/src/libnetwork/libnetwork.sym +++ b/src/libnetwork/libnetwork.sym @@ -1,5 +1,8 @@ LIBNETWORK_0 { global: + network_new; + network_ref; + network_unref; network_version; local: *; diff --git a/src/libnetwork/network/libnetwork.h b/src/libnetwork/network/libnetwork.h index 615a6289..48f7e68c 100644 --- a/src/libnetwork/network/libnetwork.h +++ b/src/libnetwork/network/libnetwork.h @@ -21,6 +21,13 @@ #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