2 * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
3 * Author "Fulup Ar Foll"
4 * Author: José Bollo <jose.bollo@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
25 #include <uuid/uuid.h>
29 #include <json-c/json.h>
31 #include "afb-session.h"
35 #define COOKEYMASK (COOKEYCOUNT - 1)
37 #define NOW (time(NULL))
44 void (*freecb)(void*);
51 time_t expiration; // expiration time of the token
53 pthread_mutex_t mutex;
54 char uuid[37]; // long term authentication of remote client
55 char token[37]; // short term authentication of remote client
56 struct cookie *cookies[COOKEYCOUNT];
59 // Session UUID are store in a simple array [for 10 sessions this should be enough]
61 pthread_mutex_t mutex; // declare a mutex to protect hash table
62 struct afb_session **store; // sessions store
63 int count; // current number of sessions
70 * Get the index of the 'key' in the cookies array.
71 * @param key the key to scan
72 * @return the index of the list for key within cookies
74 static int cookeyidx(const void *key)
76 intptr_t x = (intptr_t)key;
77 unsigned r = (unsigned)((x >> 5) ^ (x >> 15));
78 return r & COOKEYMASK;
82 static void new_uuid(char uuid[37])
85 uuid_generate(newuuid);
86 uuid_unparse_lower(newuuid, uuid);
89 static inline void lock(struct afb_session *session)
91 pthread_mutex_lock(&session->mutex);
94 static inline void unlock(struct afb_session *session)
96 pthread_mutex_unlock(&session->mutex);
99 // Free context [XXXX Should be protected again memory abort XXXX]
100 static void free_data (struct afb_session *session)
103 struct cookie *cookie, *next;
106 for (idx = 0 ; idx < COOKEYCOUNT ; idx++) {
107 cookie = session->cookies[idx];
108 session->cookies[idx] = NULL;
109 while (cookie != NULL) {
111 if (cookie->freecb != NULL)
112 cookie->freecb(cookie->value);
119 // Create a new store in RAM, not that is too small it will be automatically extended
120 void afb_session_init (int max_session_count, int timeout, const char *initok)
122 // let's create as store as hashtable does not have any
123 sessions.store = calloc (1 + (unsigned)max_session_count, sizeof(struct afb_session));
124 pthread_mutex_init(&sessions.mutex, NULL);
125 sessions.max = max_session_count;
126 sessions.timeout = timeout;
128 /* without token, a secret is made to forbid creation of sessions */
129 new_uuid(sessions.initok);
130 else if (strlen(initok) < sizeof(sessions.store[0]->token))
131 strcpy(sessions.initok, initok);
133 ERROR("initial token '%s' too long (max length 36)", initok);
138 static struct afb_session *search (const char* uuid)
141 struct afb_session *session;
143 assert (uuid != NULL);
145 pthread_mutex_lock(&sessions.mutex);
147 for (idx=0; idx < sessions.max; idx++) {
148 session = sessions.store[idx];
149 if (session && (0 == strcmp (uuid, session->uuid)))
155 pthread_mutex_unlock(&sessions.mutex);
159 static int destroy (struct afb_session *session)
164 assert (session != NULL);
166 pthread_mutex_lock(&sessions.mutex);
168 for (idx=0; idx < sessions.max; idx++) {
169 if (sessions.store[idx] == session) {
170 sessions.store[idx] = NULL;
178 pthread_mutex_unlock(&sessions.mutex);
182 static int add (struct afb_session *session)
187 assert (session != NULL);
189 pthread_mutex_lock(&sessions.mutex);
191 for (idx=0; idx < sessions.max; idx++) {
192 if (NULL == sessions.store[idx]) {
193 sessions.store[idx] = session;
201 pthread_mutex_unlock(&sessions.mutex);
205 // Check if context timeout or not
206 static int is_expired (struct afb_session *ctx, time_t now)
208 assert (ctx != NULL);
209 return ctx->expiration < now;
212 // Check if context is active or not
213 static int is_active (struct afb_session *ctx, time_t now)
215 assert (ctx != NULL);
216 return ctx->uuid[0] != 0 && ctx->expiration >= now;
219 // Loop on every entry and remove old context sessions.hash
220 static void cleanup (time_t now)
222 struct afb_session *ctx;
225 // Loop on Sessions Table and remove anything that is older than timeout
226 for (idx=0; idx < sessions.max; idx++) {
227 ctx = sessions.store[idx];
228 if (ctx != NULL && is_expired(ctx, now)) {
229 afb_session_close (ctx);
234 static struct afb_session *make_session (const char *uuid, int timeout, time_t now)
236 struct afb_session *session;
238 /* allocates a new one */
239 session = calloc(1, sizeof *session);
240 if (session == NULL) {
244 pthread_mutex_init(&session->mutex, NULL);
246 /* generate the uuid */
248 new_uuid(session->uuid);
250 if (strlen(uuid) >= sizeof session->uuid) {
254 strcpy(session->uuid, uuid);
258 strcpy(session->token, sessions.initok);
259 session->timeout = timeout;
261 session->expiration = now + timeout;
263 session->expiration = (time_t)(~(time_t)0);
264 if (session->expiration < 0)
265 session->expiration = (time_t)(((unsigned long long)session->expiration) >> 1);
267 if (!add (session)) {
272 session->access = now;
273 session->refcount = 1;
282 struct afb_session *afb_session_create (const char *uuid, int timeout)
290 /* search for an existing one not too old */
291 if (uuid != NULL && search(uuid) != NULL) {
296 return make_session(uuid, timeout, now);
299 // This function will return exiting session or newly created session
300 struct afb_session *afb_session_get (const char *uuid, int *created)
302 struct afb_session *session;
309 /* search for an existing one not too old */
311 session = search(uuid);
312 if (session != NULL) {
314 session->access = now;
321 return make_session(uuid, sessions.timeout, now);
324 struct afb_session *afb_session_addref(struct afb_session *session)
327 __atomic_add_fetch(&session->refcount, 1, __ATOMIC_RELAXED);
331 void afb_session_unref(struct afb_session *session)
333 if (session != NULL) {
334 assert(session->refcount != 0);
335 if (!__atomic_sub_fetch(&session->refcount, 1, __ATOMIC_RELAXED)) {
336 if (session->uuid[0] == 0) {
338 pthread_mutex_destroy(&session->mutex);
345 // Free Client Session Context
346 void afb_session_close (struct afb_session *session)
348 assert(session != NULL);
349 if (session->uuid[0] != 0) {
350 session->uuid[0] = 0;
352 if (session->refcount == 0) {
359 // Sample Generic Ping Debug API
360 int afb_session_check_token (struct afb_session *session, const char *token)
362 assert(session != NULL);
363 assert(token != NULL);
365 // compare current token with previous one
366 if (!is_active (session, NOW))
369 if (session->token[0] && strcmp (token, session->token) != 0)
375 // generate a new token and update client context
376 void afb_session_new_token (struct afb_session *session)
378 assert(session != NULL);
380 // Old token was valid let's regenerate a new one
381 new_uuid(session->token);
383 // keep track of time for session timeout and further clean up
384 if (session->timeout != 0)
385 session->expiration = NOW + session->timeout;
388 const char *afb_session_uuid (struct afb_session *session)
390 assert(session != NULL);
391 return session->uuid;
394 const char *afb_session_token (struct afb_session *session)
396 assert(session != NULL);
397 return session->token;
400 static struct cookie *cookie_search(struct afb_session *session, const void *key, int *idx)
402 struct cookie *cookie;
404 cookie = session->cookies[*idx = cookeyidx(key)];
405 while(cookie != NULL && cookie->key != key)
406 cookie = cookie->next;
410 static struct cookie *cookie_add(struct afb_session *session, int idx, const void *key, void *value, void (*freecb)(void*))
412 struct cookie *cookie;
414 cookie = malloc(sizeof *cookie);
419 cookie->value = value;
420 cookie->freecb = freecb;
421 cookie->next = session->cookies[idx];
422 session->cookies[idx] = cookie;
427 void *afb_session_cookie(struct afb_session *session, const void *key, void *(*makecb)(void), void (*freecb)(void*))
431 struct cookie *cookie;
434 cookie = cookie_search(session, key, &idx);
436 value = cookie->value;
438 value = makecb ? makecb() : NULL;
439 if (makecb || freecb) {
440 cookie = cookie_add(session, idx, key, value, freecb);
442 if (makecb && freecb)
452 void *afb_session_get_cookie(struct afb_session *session, const void *key)
456 struct cookie *cookie;
459 cookie = cookie_search(session, key, &idx);
460 value = cookie ? cookie->value : NULL;
465 int afb_session_set_cookie(struct afb_session *session, const void *key, void *value, void (*freecb)(void*))
468 struct cookie *cookie;
471 cookie = cookie_search(session, key, &idx);
473 cookie = cookie_add(session, idx, key, value, freecb);
475 if (cookie->value != value && cookie->freecb)
476 cookie->freecb(cookie->value);
477 cookie->value = value;
478 cookie->freecb = freecb;