2 * Copyright (C) 2015-2019 "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.
27 #include <uuid/uuid.h>
31 #include "afb-session.h"
39 #define COOKIEMASK (COOKIECOUNT - 1)
41 #define _MAXEXP_ ((time_t)(~(time_t)0))
42 #define _MAXEXP2_ ((time_t)((((unsigned long long)_MAXEXP_) >> 1)))
43 #define MAX_EXPIRATION (_MAXEXP_ >= 0 ? _MAXEXP_ : _MAXEXP2_)
44 #define NOW (time_now())
47 * structure for a cookie added to sessions
51 struct cookie *next; /**< link to next cookie */
52 const void *key; /**< pointer key */
53 void *value; /**< value */
54 void (*freecb)(void*); /**< function to call when session is closed */
58 * structure for session
62 struct afb_session *next; /**< link to the next */
63 unsigned refcount; /**< count of reference to the session */
64 int timeout; /**< timeout of the session */
65 time_t expiration; /**< expiration time of the token */
66 pthread_mutex_t mutex; /**< mutex of the session */
67 struct cookie *cookies[COOKIECOUNT]; /**< cookies of the session */
68 char *lang; /**< current language setting for the session */
69 uint8_t closed: 1; /**< is the session closed ? */
70 uint8_t autoclose: 1; /**< close the session when unreferenced */
71 uint8_t notinset: 1; /**< session removed from the set of sessions */
72 char uuid[SIZEUUID]; /**< long term authentication of remote client */
73 char token[SIZEUUID]; /**< short term authentication of remote client */
77 * structure for managing sessions
80 int count; /**< current number of sessions */
81 int max; /**< maximum count of sessions */
82 int timeout; /**< common initial timeout */
83 struct afb_session *heads[HEADCOUNT]; /**< sessions */
84 char initok[SIZEUUID]; /**< common initial token */
85 pthread_mutex_t mutex; /**< declare a mutex to protect hash table */
92 .mutex = PTHREAD_MUTEX_INITIALIZER
96 * Get the actual raw time
98 static inline time_t time_now()
101 clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
106 * generate a new fresh 'uuid'
108 static void new_uuid(char uuid[SIZEUUID])
112 #if defined(USE_UUID_GENERATE)
113 uuid_generate(newuuid);
117 static uint16_t counter;
118 static char state[32];
119 static struct random_data rdata;
122 clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
124 pid = (uint16_t)getpid();
125 counter = (uint16_t)(ts.tv_nsec >> 8);
127 initstate_r((((unsigned)pid) << 16) + ((unsigned)counter),
128 state, sizeof state, &rdata);
130 ts.tv_nsec ^= (long)ts.tv_sec;
134 newuuid[0] = (char)(ts.tv_nsec >> 24);
135 newuuid[1] = (char)(ts.tv_nsec >> 16);
136 newuuid[2] = (char)(ts.tv_nsec >> 8);
137 newuuid[3] = (char)(ts.tv_nsec);
139 newuuid[4] = (char)(pid >> 8);
140 newuuid[5] = (char)(pid);
142 random_r(&rdata, &x);
143 newuuid[6] = (char)(((x >> 16) & 0x0f) | 0x40); /* pseudo-random version */
144 newuuid[7] = (char)(x >> 8);
146 random_r(&rdata, &x);
147 newuuid[8] = (char)(((x >> 16) & 0x3f) | 0x80); /* variant RFC4122 */
148 newuuid[9] = (char)(x >> 8);
150 random_r(&rdata, &x);
151 newuuid[10] = (char)(x >> 16);
152 newuuid[11] = (char)(x >> 8);
154 random_r(&rdata, &x);
155 newuuid[12] = (char)(x >> 16);
156 newuuid[13] = (char)(x >> 8);
158 newuuid[14] = (char)(counter >> 8);
159 newuuid[15] = (char)(counter);
161 uuid_unparse_lower(newuuid, uuid);
164 /* lock the set of sessions for exclusive access */
165 static inline void sessionset_lock()
167 pthread_mutex_lock(&sessions.mutex);
170 /* unlock the set of sessions of exclusive access */
171 static inline void sessionset_unlock()
173 pthread_mutex_unlock(&sessions.mutex);
177 * search within the set of sessions the session of 'uuid'.
178 * 'hashidx' is the precomputed hash for 'uuid'
179 * return the session or NULL
181 static struct afb_session *sessionset_search(const char *uuid, uint8_t hashidx)
183 struct afb_session *session;
185 session = sessions.heads[hashidx];
186 while (session && strcmp(uuid, session->uuid))
187 session = session->next;
192 /* add 'session' to the set of sessions */
193 static int sessionset_add(struct afb_session *session, uint8_t hashidx)
195 /* check availability */
196 if (sessions.max && sessions.count >= sessions.max) {
201 /* add the session */
202 session->next = sessions.heads[hashidx];
203 sessions.heads[hashidx] = session;
208 /* make a new uuid not used in the set of sessions */
209 static uint8_t sessionset_make_uuid (char uuid[SIZEUUID])
215 hashidx = pearson4(uuid);
216 } while(sessionset_search(uuid, hashidx));
220 /* lock the 'session' for exclusive access */
221 static inline void session_lock(struct afb_session *session)
223 pthread_mutex_lock(&session->mutex);
226 /* unlock the 'session' of exclusive access */
227 static inline void session_unlock(struct afb_session *session)
229 pthread_mutex_unlock(&session->mutex);
232 /* close the 'session' */
233 static void session_close(struct afb_session *session)
236 struct cookie *cookie;
238 /* close only one time */
239 if (!session->closed) {
245 afb_hook_session_close(session);
248 /* release cookies */
249 for (idx = 0 ; idx < COOKIECOUNT ; idx++) {
250 while ((cookie = session->cookies[idx])) {
251 session->cookies[idx] = cookie->next;
252 if (cookie->freecb != NULL)
253 cookie->freecb(cookie->value);
260 /* destroy the 'session' */
261 static void session_destroy (struct afb_session *session)
264 afb_hook_session_destroy(session);
266 pthread_mutex_destroy(&session->mutex);
271 /* update expiration of 'session' according to 'now' */
272 static void session_update_expiration(struct afb_session *session, time_t now)
276 /* compute expiration */
277 expiration = now + afb_session_timeout(session);
279 expiration = MAX_EXPIRATION;
281 /* record the expiration */
282 session->expiration = expiration;
286 * Add a new session with the 'uuid' (of 'hashidx')
287 * and the 'timeout' starting from 'now'.
288 * Add it to the set of sessions
289 * Return the created session
291 static struct afb_session *session_add(const char *uuid, int timeout, time_t now, uint8_t hashidx)
293 struct afb_session *session;
295 /* check arguments */
296 if (!AFB_SESSION_TIMEOUT_IS_VALID(timeout)
297 || (uuid && strlen(uuid) >= sizeof session->uuid)) {
302 /* allocates a new one */
303 session = calloc(1, sizeof *session);
304 if (session == NULL) {
310 pthread_mutex_init(&session->mutex, NULL);
311 session->refcount = 1;
312 strcpy(session->uuid, uuid);
313 strcpy(session->token, sessions.initok);
314 session->timeout = timeout;
315 session_update_expiration(session, now);
318 if (sessionset_add(session, hashidx)) {
324 afb_hook_session_create(session);
330 /* Remove expired sessions and return current time (now) */
331 static time_t sessionset_cleanup (int force)
333 struct afb_session *session, **prv;
337 /* Loop on Sessions Table and remove anything that is older than timeout */
339 for (idx = 0 ; idx < HEADCOUNT; idx++) {
340 prv = &sessions.heads[idx];
341 while ((session = *prv)) {
342 session_lock(session);
343 if (force || session->expiration < now)
344 session_close(session);
345 if (!session->closed)
346 prv = &session->next;
348 *prv = session->next;
350 session->notinset = 1;
351 if ( !session->refcount) {
352 session_destroy(session);
356 session_unlock(session);
363 * Initialize the session manager with a 'max_session_count',
364 * an initial common 'timeout' and an initial common token 'initok'.
366 * @param max_session_count maximum allowed session count in the same time
367 * @param timeout the initial default timeout of sessions
368 * @param initok the initial default token of sessions
371 int afb_session_init (int max_session_count, int timeout, const char *initok)
373 /* check parameters */
374 if (initok && strlen(initok) >= sizeof sessions.initok) {
375 ERROR("initial token '%s' too long (max length %d)",
376 initok, ((int)(sizeof sessions.initok)) - 1);
381 /* init the sessionset (after cleanup) */
383 sessionset_cleanup(1);
384 sessions.max = max_session_count;
385 sessions.timeout = timeout;
387 new_uuid(sessions.initok);
389 strcpy(sessions.initok, initok);
395 * Iterate the sessions and call 'callback' with
396 * the 'closure' for each session.
398 void afb_session_foreach(void (*callback)(void *closure, struct afb_session *session), void *closure)
400 struct afb_session *session;
403 /* Loop on Sessions Table and remove anything that is older than timeout */
405 for (idx = 0 ; idx < HEADCOUNT; idx++) {
406 session = sessions.heads[idx];
408 if (!session->closed)
409 callback(closure, session);
410 session = session->next;
417 * Cleanup the sessionset of its closed or expired sessions
419 void afb_session_purge()
422 sessionset_cleanup(0);
427 * @return the initial token set at initialization
429 const char *afb_session_initial_token()
431 return sessions.initok;
434 /* Searchs the session of 'uuid' */
435 struct afb_session *afb_session_search (const char *uuid)
437 struct afb_session *session;
440 sessionset_cleanup(0);
441 session = sessionset_search(uuid, pearson4(uuid));
442 session = afb_session_addref(session);
449 * Creates a new session with 'timeout'
451 struct afb_session *afb_session_create (int timeout)
453 return afb_session_get(NULL, timeout, NULL);
457 * Returns the timeout of 'session' in seconds
459 int afb_session_timeout(struct afb_session *session)
463 /* compute timeout */
464 timeout = session->timeout;
465 if (timeout == AFB_SESSION_TIMEOUT_DEFAULT)
466 timeout = sessions.timeout;
473 * Returns the second remaining before expiration of 'session'
475 int afb_session_what_remains(struct afb_session *session)
477 return (int)(session->expiration - NOW);
480 /* This function will return exiting session or newly created session */
481 struct afb_session *afb_session_get (const char *uuid, int timeout, int *created)
483 char _uuid_[SIZEUUID];
485 struct afb_session *session;
491 now = sessionset_cleanup(0);
493 /* search for an existing one not too old */
495 hashidx = sessionset_make_uuid(_uuid_);
498 hashidx = pearson4(uuid);
499 session = sessionset_search(uuid, hashidx);
502 afb_session_addref(session);
507 /* create the session */
508 session = session_add(uuid, timeout, now, hashidx);
518 /* increase the use count on 'session' (can be NULL) */
519 struct afb_session *afb_session_addref(struct afb_session *session)
521 if (session != NULL) {
523 afb_hook_session_addref(session);
525 session_lock(session);
527 session_unlock(session);
532 /* decrease the use count of 'session' (can be NULL) */
533 void afb_session_unref(struct afb_session *session)
539 afb_hook_session_unref(session);
541 session_lock(session);
542 if (!--session->refcount) {
543 if (session->autoclose)
544 session_close(session);
545 if (session->notinset) {
546 session_destroy(session);
550 session_unlock(session);
553 /* close 'session' */
554 void afb_session_close (struct afb_session *session)
556 session_lock(session);
557 session_close(session);
558 session_unlock(session);
562 * Set the 'autoclose' flag of the 'session'
564 * A session whose autoclose flag is true will close as
565 * soon as it is no more referenced.
567 * @param session the session to set
568 * @param autoclose the value to set
570 void afb_session_set_autoclose(struct afb_session *session, int autoclose)
572 session->autoclose = !!autoclose;
575 /* is 'session' closed? */
576 int afb_session_is_closed (struct afb_session *session)
578 return session->closed;
582 * check whether the token of 'session' is 'token'
583 * return 1 if true or 0 otherwise
585 int afb_session_check_token (struct afb_session *session, const char *token)
589 session_lock(session);
591 && session->expiration >= NOW
592 && !(session->token[0] && strcmp (token, session->token));
593 session_unlock(session);
597 /* generate a new token and update client context */
598 void afb_session_new_token (struct afb_session *session)
600 session_lock(session);
601 new_uuid(session->token);
602 session_update_expiration(session, NOW);
604 afb_hook_session_renew(session);
606 session_unlock(session);
609 /* Returns the uuid of 'session' */
610 const char *afb_session_uuid (struct afb_session *session)
612 return session->uuid;
615 /* Returns the token of 'session' */
616 const char *afb_session_token (struct afb_session *session)
618 return session->token;
622 * Get the index of the 'key' in the cookies array.
623 * @param key the key to scan
624 * @return the index of the list for key within cookies
626 static int cookeyidx(const void *key)
628 intptr_t x = (intptr_t)key;
629 unsigned r = (unsigned)((x >> 5) ^ (x >> 15));
630 return r & COOKIEMASK;
634 * Set, get, replace, remove a cookie of 'key' for the 'session'
636 * The behaviour of this function depends on its parameters:
638 * @param session the session
639 * @param key the key of the cookie
640 * @param makecb the creation function or NULL
641 * @param freecb the release function or NULL
642 * @param closure an argument for makecb or the value if makecb==NULL
643 * @param replace a boolean enforcing replecement of the previous value
645 * @return the value of the cookie
647 * The 'key' is a pointer and compared as pointers.
649 * For getting the current value of the cookie:
651 * afb_session_cookie(session, key, NULL, NULL, NULL, 0)
653 * For storing the value of the cookie
655 * afb_session_cookie(session, key, NULL, NULL, value, 1)
657 void *afb_session_cookie(struct afb_session *session, const void *key, void *(*makecb)(void *closure), void (*freecb)(void *item), void *closure, int replace)
661 struct cookie *cookie, **prv;
663 /* get key hashed index */
664 idx = cookeyidx(key);
666 /* lock session and search for the cookie of 'key' */
667 session_lock(session);
668 prv = &session->cookies[idx];
672 /* 'key' not found, create value using 'closure' and 'makecb' */
673 value = makecb ? makecb(closure) : closure;
674 /* store the the only if it has some meaning */
675 if (replace || makecb || freecb) {
676 cookie = malloc(sizeof *cookie);
679 /* calling freecb if there is no makecb may have issue */
680 if (makecb && freecb)
685 cookie->value = value;
686 cookie->freecb = freecb;
692 } else if (cookie->key == key) {
693 /* cookie of key found */
695 /* not replacing, get the value */
696 value = cookie->value;
698 /* create value using 'closure' and 'makecb' */
699 value = makecb ? makecb(closure) : closure;
701 /* free previous value is needed */
702 if (cookie->value != value && cookie->freecb)
703 cookie->freecb(cookie->value);
705 /* if both value and freecb are NULL drop the cookie */
706 if (!value && !freecb) {
710 /* store the value and its releaser */
711 cookie->value = value;
712 cookie->freecb = freecb;
717 prv = &(cookie->next);
721 /* unlock the session and return the value */
722 session_unlock(session);
727 * Get the cookie of 'key' in the 'session'.
729 * @param session the session to search in
730 * @param key the key of the data to retrieve
732 * @return the data staored for the key or NULL if the key isn't found
734 void *afb_session_get_cookie(struct afb_session *session, const void *key)
736 return afb_session_cookie(session, key, NULL, NULL, NULL, 0);
740 * Set the cookie of 'key' in the 'session' to the 'value' that can be
741 * cleaned using 'freecb' (if not null).
743 * @param session the session to set
744 * @param key the key of the data to store
745 * @param value the value to store at key
746 * @param freecb a function to use when the cookie value is to remove (or null)
748 * @return 0 in case of success or -1 in case of error
750 int afb_session_set_cookie(struct afb_session *session, const void *key, void *value, void (*freecb)(void*))
752 return -(value != afb_session_cookie(session, key, NULL, freecb, value, 1));
756 * Set the language attached to the session
758 * @param session the session to set
759 * @param lang the language specifiction to set to session
761 * @return 0 in case of success or -1 in case of error
763 int afb_session_set_language(struct afb_session *session, const char *lang)
771 oldl = session->lang;
772 session->lang = newl;
778 * Get the language attached to the session
780 * @param session the session to query
781 * @param lang a default language specifiction
783 * @return the langauage specification to use for session
785 const char *afb_session_get_language(struct afb_session *session, const char *lang)
787 return session->lang ?: lang;