afb-session: improve naming
[src/app-framework-binder.git] / src / afb-session.c
index 8f80761..43e08d4 100644 (file)
 #include "afb-session.h"
 #include "verbose.h"
 
-#define HEADCOUNT    16
-#define COOKEYCOUNT  8
-#define COOKEYMASK   (COOKEYCOUNT - 1)
+#define SIZEUUID       37
+#define HEADCOUNT      16
+#define COOKEYCOUNT    8
+#define COOKEYMASK     (COOKEYCOUNT - 1)
 
 #define NOW (time(NULL))
 
@@ -50,26 +51,26 @@ struct afb_session
        struct afb_session *next; /* link to the next */
        unsigned refcount;
        int timeout;
-       time_t expiration;    // expiration time of the token
+       time_t expiration;      // expiration time of the token
        pthread_mutex_t mutex;
-       char idx;
-       char uuid[37];        // long term authentication of remote client
-       char token[37];       // short term authentication of remote client
        struct cookie *cookies[COOKEYCOUNT];
+       char idx;
+       char uuid[SIZEUUID];    // long term authentication of remote client
+       char token[SIZEUUID];   // short term authentication of remote client
 };
 
 // Session UUID are store in a simple array [for 10 sessions this should be enough]
 static struct {
-       pthread_mutex_t mutex;          // declare a mutex to protect hash table
+       pthread_mutex_t mutex;  // declare a mutex to protect hash table
        struct afb_session *heads[HEADCOUNT]; // sessions
-       int count;                      // current number of sessions
+       int count;      // current number of sessions
        int max;
        int timeout;
-       char initok[37];
+       char initok[SIZEUUID];
 } sessions;
 
 /* generate a uuid */
-static void new_uuid(char uuid[37])
+static void new_uuid(char uuid[SIZEUUID])
 {
        uuid_t newuuid;
        uuid_generate(newuuid);
@@ -87,21 +88,18 @@ static inline void unlock(struct afb_session *session)
 }
 
 // Free context [XXXX Should be protected again memory abort XXXX]
-static void remove_all_cookies(struct afb_session *session)
+static void close_session(struct afb_session *session)
 {
        int idx;
-       struct cookie *cookie, *next;
+       struct cookie *cookie;
 
-       // free cookies
+       /* free cookies */
        for (idx = 0 ; idx < COOKEYCOUNT ; idx++) {
-               cookie = session->cookies[idx];
-               session->cookies[idx] = NULL;
-               while (cookie != NULL) {
-                       next = cookie->next;
+               while ((cookie = session->cookies[idx])) {
+                       session->cookies[idx] = cookie->next;
                        if (cookie->freecb != NULL)
                                cookie->freecb(cookie->value);
                        free(cookie);
-                       cookie = next;
                }
        }
 }
@@ -161,7 +159,7 @@ static void destroy (struct afb_session *session)
 
        assert (session != NULL);
 
-       remove_all_cookies(session);
+       close_session(session);
        pthread_mutex_lock(&sessions.mutex);
        prv = &sessions.heads[(int)session->idx];
        while (*prv)
@@ -215,18 +213,31 @@ static time_t cleanup ()
 static struct afb_session *add_session (const char *uuid, int timeout, time_t now, int idx)
 {
        struct afb_session *session;
+       time_t expiration;
 
+       /* check arguments */
        if (!AFB_SESSION_TIMEOUT_IS_VALID(timeout)
         || (uuid && strlen(uuid) >= sizeof session->uuid)) {
                errno = EINVAL;
                return NULL;
        }
 
+       /* check session count */
        if (sessions.count >= sessions.max) {
                errno = EBUSY;
                return NULL;
        }
 
+       /* compute expiration */
+       if (timeout == AFB_SESSION_TIMEOUT_DEFAULT)
+               timeout = sessions.timeout;
+       expiration = now + timeout;
+       if (timeout == AFB_SESSION_TIMEOUT_INFINITE || expiration < 0) {
+               expiration = (time_t)(~(time_t)0);
+               if (expiration < 0)
+                       expiration = (time_t)(((unsigned long long)expiration) >> 1);
+       }
+
        /* allocates a new one */
        session = calloc(1, sizeof *session);
        if (session == NULL) {
@@ -234,22 +245,15 @@ static struct afb_session *add_session (const char *uuid, int timeout, time_t no
                return NULL;
        }
 
+       /* initialize */
        pthread_mutex_init(&session->mutex, NULL);
        session->refcount = 1;
        strcpy(session->uuid, uuid);
        strcpy(session->token, sessions.initok);
-
-       /* init timeout */
-       if (timeout == AFB_SESSION_TIMEOUT_DEFAULT)
-               timeout = sessions.timeout;
        session->timeout = timeout;
-       session->expiration = now + timeout;
-       if (timeout == AFB_SESSION_TIMEOUT_INFINITE || session->expiration < 0) {
-               session->expiration = (time_t)(~(time_t)0);
-               if (session->expiration < 0)
-                       session->expiration = (time_t)(((unsigned long long)session->expiration) >> 1);
-       }
+       session->expiration = expiration;
 
+       /* link */
        session->idx = (char)idx;
        session->next = sessions.heads[idx];
        sessions.heads[idx] = session;
@@ -258,10 +262,11 @@ static struct afb_session *add_session (const char *uuid, int timeout, time_t no
        return session;
 }
 
+/* create a new session for the given timeout */
 static struct afb_session *new_session (int timeout, time_t now)
 {
        int idx;
-       char uuid[37];
+       char uuid[SIZEUUID];
 
        do {
                new_uuid(uuid);
@@ -365,8 +370,9 @@ void afb_session_close (struct afb_session *session)
        pthread_mutex_lock(&session->mutex);
        if (session->uuid[0] != 0) {
                session->uuid[0] = 0;
-               remove_all_cookies(session);
-               if (session->refcount == 0) {
+               if (session->refcount)
+                       close_session(session);
+               else {
                        destroy (session);
                        return;
                }
@@ -434,12 +440,12 @@ static int cookeyidx(const void *key)
  *
  * The behaviour of this function depends on its parameters:
  *
- * @param session the session
- * @param key     the key of the cookie
- * @param makecb  the creation function
- * @param freecb  the release function
- * @param closure an argument
- * @param replace a boolean enforcing replecement of the previous value
+ * @param session      the session
+ * @param key          the key of the cookie
+ * @param makecb       the creation function or NULL
+ * @param freecb       the release function or NULL
+ * @param closure      an argument for makecb or the value if makecb==NULL
+ * @param replace      a boolean enforcing replecement of the previous value
  *
  * @return the value of the cookie
  *