From 3bf52bb36ed428d0a7b947519fbccc7c376fd4a9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Mon, 27 Mar 2017 14:24:46 +0200 Subject: [PATCH] Replace session's value with sessions's cookies MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This is a simplifaction with the benefits of only allocating needed memory and avoiding to create indexes on apis (path to dynanic?). Conversely it replaces a direct access with a linear search. Change-Id: Ibb130528ad8f23dfd6b420c228f51e181efb2664 Signed-off-by: José Bollo --- src/afb-apis.c | 10 +--------- src/afb-apis.h | 1 - src/afb-context.c | 8 +++++--- src/afb-context.h | 2 +- src/afb-session.c | 42 ++---------------------------------------- src/afb-session.h | 6 +----- src/main.c | 2 +- 7 files changed, 11 insertions(+), 60 deletions(-) diff --git a/src/afb-apis.c b/src/afb-apis.c index 5a53d375..9ffffdb3 100644 --- a/src/afb-apis.c +++ b/src/afb-apis.c @@ -38,14 +38,6 @@ struct api_desc { static struct api_desc *apis_array = NULL; static int apis_count = 0; -/** - * Returns the current count of APIs - */ -int afb_apis_count() -{ - return apis_count; -} - /** * Checks wether 'name' is a valid API name. * @return 1 if valid, 0 otherwise @@ -178,7 +170,7 @@ void afb_apis_call(struct afb_req req, struct afb_context *context, const char * c = strcasecmp(a->name, api); if (c == 0) { /* api found */ - context->api_index = i; + context->api_key = a->api.closure; a->api.call(a->api.closure, req, context, verb); break; } diff --git a/src/afb-apis.h b/src/afb-apis.h index 3e90f661..9b060ce8 100644 --- a/src/afb-apis.h +++ b/src/afb-apis.h @@ -28,7 +28,6 @@ struct afb_api }; -extern int afb_apis_count(); extern int afb_apis_is_valid_api_name(const char *name); extern int afb_apis_add(const char *name, struct afb_api api); diff --git a/src/afb-context.c b/src/afb-context.c index eec4ebb0..6eb29f0d 100644 --- a/src/afb-context.c +++ b/src/afb-context.c @@ -31,7 +31,7 @@ static void init_context(struct afb_context *context, struct afb_session *sessio /* reset the context for the session */ context->session = session; context->flags = 0; - context->api_index = -1; + context->api_key = NULL; context->loa_in = afb_session_get_LOA(session) & 7; /* check the token */ @@ -109,13 +109,15 @@ const char *afb_context_sent_uuid(struct afb_context *context) void *afb_context_get(struct afb_context *context) { assert(context->session != NULL); - return afb_session_get_value(context->session, context->api_index); + return afb_session_get_cookie(context->session, context->api_key); } void afb_context_set(struct afb_context *context, void *value, void (*free_value)(void*)) { + int rc; assert(context->session != NULL); - return afb_session_set_value(context->session, context->api_index, value, free_value); + rc = afb_session_set_cookie(context->session, context->api_key, value, free_value); + (void)rc; /* TODO */ } void afb_context_close(struct afb_context *context) diff --git a/src/afb-context.h b/src/afb-context.h index d65e6ffa..3eb49742 100644 --- a/src/afb-context.h +++ b/src/afb-context.h @@ -38,7 +38,7 @@ struct afb_context unsigned loa_changed: 1; }; }; - int api_index; + void *api_key; }; extern void afb_context_init(struct afb_context *context, struct afb_session *session, const char *token); diff --git a/src/afb-session.c b/src/afb-session.c index db81457c..13b9505e 100644 --- a/src/afb-session.c +++ b/src/afb-session.c @@ -33,12 +33,6 @@ #define NOW (time(NULL)) -struct value -{ - void *value; - void (*freecb)(void*); -}; - struct cookie { struct cookie *next; @@ -56,7 +50,6 @@ struct afb_session time_t access; char uuid[37]; // long term authentication of remote client char token[37]; // short term authentication of remote client - struct value *values; struct cookie *cookies; }; @@ -67,7 +60,6 @@ static struct { int count; // current number of sessions int max; int timeout; - int apicount; char initok[37]; } sessions; @@ -82,16 +74,8 @@ static void new_uuid(char uuid[37]) // Free context [XXXX Should be protected again memory abort XXXX] static void free_data (struct afb_session *session) { - int idx; struct cookie *cookie; - // If application add a handle let's free it now - assert (session->values != NULL); - - // Free session handle with a standard Free function, with app callback or ignore it - for (idx=0; idx < sessions.apicount; idx ++) - afb_session_set_value(session, idx, NULL, NULL); - // free cookies cookie = session->cookies; while (cookie != NULL) { @@ -104,13 +88,12 @@ static void free_data (struct afb_session *session) } // Create a new store in RAM, not that is too small it will be automatically extended -void afb_session_init (int max_session_count, int timeout, const char *initok, int context_count) +void afb_session_init (int max_session_count, int timeout, const char *initok) { // let's create as store as hashtable does not have any sessions.store = calloc (1 + (unsigned)max_session_count, sizeof(struct afb_session)); sessions.max = max_session_count; sessions.timeout = timeout; - sessions.apicount = context_count; if (initok == NULL) /* without token, a secret is made to forbid creation of sessions */ new_uuid(sessions.initok); @@ -223,12 +206,11 @@ static struct afb_session *make_session (const char *uuid, int timeout, time_t n struct afb_session *session; /* allocates a new one */ - session = calloc(1, sizeof(struct afb_session) + ((unsigned)sessions.apicount * sizeof(*session->values))); + session = calloc(1, sizeof(struct afb_session)); if (session == NULL) { errno = ENOMEM; goto error; } - session->values = (void*)(session + 1); /* generate the uuid */ if (uuid == NULL) { @@ -394,26 +376,6 @@ void afb_session_set_LOA (struct afb_session *session, unsigned loa) session->loa = loa; } -void *afb_session_get_value(struct afb_session *session, int index) -{ - assert(session != NULL); - assert(index >= 0); - assert(index < sessions.apicount); - return session->values[index].value; -} - -void afb_session_set_value(struct afb_session *session, int index, void *value, void (*freecb)(void*)) -{ - struct value prev; - assert(session != NULL); - assert(index >= 0); - assert(index < sessions.apicount); - prev = session->values[index]; - session->values[index] = (struct value){.value = value, .freecb = freecb}; - if (prev.value != NULL && prev.value != value && prev.freecb != NULL) - prev.freecb(prev.value); -} - void *afb_session_get_cookie(struct afb_session *session, const void *key) { struct cookie *cookie; diff --git a/src/afb-session.h b/src/afb-session.h index 7b3a7101..c45a7e23 100644 --- a/src/afb-session.h +++ b/src/afb-session.h @@ -17,10 +17,9 @@ #pragma once -struct json_object; struct afb_session; -extern void afb_session_init(int max_session_count, int timeout, const char *initok, int context_count); +extern void afb_session_init(int max_session_count, int timeout, const char *initok); extern struct afb_session *afb_session_create (const char *uuid, int timeout); extern struct afb_session *afb_session_get (const char *uuid, int *created); @@ -38,9 +37,6 @@ extern const char *afb_session_token(struct afb_session *session); extern unsigned afb_session_get_LOA(struct afb_session *session); extern void afb_session_set_LOA (struct afb_session *session, unsigned loa); -extern void *afb_session_get_value(struct afb_session *session, int index); -extern void afb_session_set_value(struct afb_session *session, int index, void *value, void (*freecb)(void*)); - extern void *afb_session_get_cookie(struct afb_session *session, const void *key); extern int afb_session_set_cookie(struct afb_session *session, const void *key, void *value, void (*freecb)(void*)); diff --git a/src/main.c b/src/main.c index 40ad19c8..39296742 100644 --- a/src/main.c +++ b/src/main.c @@ -429,7 +429,7 @@ int main(int argc, char *argv[]) start_list(config->ldpaths, afb_api_so_add_pathset, "the binding path set"); start_list(config->so_bindings, afb_api_so_add_binding, "the binding"); - afb_session_init(config->nbSessionMax, config->cntxTimeout, config->token, afb_apis_count()); + afb_session_init(config->nbSessionMax, config->cntxTimeout, config->token); start_list(config->dbus_servers, afb_api_dbus_add_server, "the afb-dbus service"); start_list(config->ws_servers, afb_api_ws_add_server, "the afb-websocket service"); -- 2.16.6