Change-Id: I4035a59d125e0768b0366c3dc5e7509e416ab9c6
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
#include "afb-session.h"
#include "verbose.h"
#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)
struct afb_session *next; /* link to the next */
unsigned refcount;
int timeout;
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;
pthread_mutex_t mutex;
char idx;
- char uuid[37]; // long term authentication of remote client
- char token[37]; // short term authentication of remote client
+ char uuid[SIZEUUID]; // long term authentication of remote client
+ char token[SIZEUUID]; // short term authentication of remote client
struct cookie *cookies[COOKEYCOUNT];
};
// Session UUID are store in a simple array [for 10 sessions this should be enough]
static struct {
struct cookie *cookies[COOKEYCOUNT];
};
// 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
struct afb_session *heads[HEADCOUNT]; // sessions
- int count; // current number of sessions
+ int count; // current number of sessions
} sessions;
/* generate a uuid */
} sessions;
/* generate a uuid */
-static void new_uuid(char uuid[37])
+static void new_uuid(char uuid[SIZEUUID])
{
uuid_t newuuid;
uuid_generate(newuuid);
{
uuid_t newuuid;
uuid_generate(newuuid);
static struct afb_session *add_session (const char *uuid, int timeout, time_t now, int idx)
{
struct afb_session *session;
static struct afb_session *add_session (const char *uuid, int timeout, time_t now, int idx)
{
struct afb_session *session;
if (!AFB_SESSION_TIMEOUT_IS_VALID(timeout)
|| (uuid && strlen(uuid) >= sizeof session->uuid)) {
errno = EINVAL;
return NULL;
}
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;
}
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) {
/* allocates a new one */
session = calloc(1, sizeof *session);
if (session == NULL) {
pthread_mutex_init(&session->mutex, NULL);
session->refcount = 1;
strcpy(session->uuid, uuid);
strcpy(session->token, sessions.initok);
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->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;
session->idx = (char)idx;
session->next = sessions.heads[idx];
sessions.heads[idx] = session;
session->idx = (char)idx;
session->next = sessions.heads[idx];
sessions.heads[idx] = session;
+/* create a new session for the given timeout */
static struct afb_session *new_session (int timeout, time_t now)
{
int idx;
static struct afb_session *new_session (int timeout, time_t now)
{
int idx;
*
* The behaviour of this function depends on its parameters:
*
*
* 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
*
*
* @return the value of the cookie
*