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 char uuid[37]; // long term authentication of remote client
54 char token[37]; // short term authentication of remote client
55 struct cookie *cookies[COOKEYCOUNT];
58 // Session UUID are store in a simple array [for 10 sessions this should be enough]
60 pthread_mutex_t mutex; // declare a mutex to protect hash table
61 struct afb_session **store; // sessions store
62 int count; // current number of sessions
69 * Get the index of the 'key' in the cookies array.
70 * @param key the key to scan
71 * @return the index of the list for key within cookies
73 static int cookeyidx(const void *key)
75 intptr_t x = (intptr_t)key;
76 unsigned r = (unsigned)((x >> 5) ^ (x >> 15));
77 return r & COOKEYMASK;
81 static void new_uuid(char uuid[37])
84 uuid_generate(newuuid);
85 uuid_unparse_lower(newuuid, uuid);
88 // Free context [XXXX Should be protected again memory abort XXXX]
89 static void free_data (struct afb_session *session)
92 struct cookie *cookie, *next;
95 for (idx = 0 ; idx < COOKEYCOUNT ; idx++) {
96 cookie = session->cookies[idx];
97 session->cookies[idx] = NULL;
98 while (cookie != NULL) {
100 if (cookie->freecb != NULL)
101 cookie->freecb(cookie->value);
108 // Create a new store in RAM, not that is too small it will be automatically extended
109 void afb_session_init (int max_session_count, int timeout, const char *initok)
111 // let's create as store as hashtable does not have any
112 sessions.store = calloc (1 + (unsigned)max_session_count, sizeof(struct afb_session));
113 sessions.max = max_session_count;
114 sessions.timeout = timeout;
116 /* without token, a secret is made to forbid creation of sessions */
117 new_uuid(sessions.initok);
118 else if (strlen(initok) < sizeof(sessions.store[0]->token))
119 strcpy(sessions.initok, initok);
121 ERROR("initial token '%s' too long (max length 36)", initok);
126 static struct afb_session *search (const char* uuid)
129 struct afb_session *session;
131 assert (uuid != NULL);
133 pthread_mutex_lock(&sessions.mutex);
135 for (idx=0; idx < sessions.max; idx++) {
136 session = sessions.store[idx];
137 if (session && (0 == strcmp (uuid, session->uuid)))
143 pthread_mutex_unlock(&sessions.mutex);
147 static int destroy (struct afb_session *session)
152 assert (session != NULL);
154 pthread_mutex_lock(&sessions.mutex);
156 for (idx=0; idx < sessions.max; idx++) {
157 if (sessions.store[idx] == session) {
158 sessions.store[idx] = NULL;
166 pthread_mutex_unlock(&sessions.mutex);
170 static int add (struct afb_session *session)
175 assert (session != NULL);
177 pthread_mutex_lock(&sessions.mutex);
179 for (idx=0; idx < sessions.max; idx++) {
180 if (NULL == sessions.store[idx]) {
181 sessions.store[idx] = session;
189 pthread_mutex_unlock(&sessions.mutex);
193 // Check if context timeout or not
194 static int is_expired (struct afb_session *ctx, time_t now)
196 assert (ctx != NULL);
197 return ctx->expiration < now;
200 // Check if context is active or not
201 static int is_active (struct afb_session *ctx, time_t now)
203 assert (ctx != NULL);
204 return ctx->uuid[0] != 0 && ctx->expiration >= now;
207 // Loop on every entry and remove old context sessions.hash
208 static void cleanup (time_t now)
210 struct afb_session *ctx;
213 // Loop on Sessions Table and remove anything that is older than timeout
214 for (idx=0; idx < sessions.max; idx++) {
215 ctx = sessions.store[idx];
216 if (ctx != NULL && is_expired(ctx, now)) {
217 afb_session_close (ctx);
222 static struct afb_session *make_session (const char *uuid, int timeout, time_t now)
224 struct afb_session *session;
226 /* allocates a new one */
227 session = calloc(1, sizeof(struct afb_session));
228 if (session == NULL) {
233 /* generate the uuid */
235 new_uuid(session->uuid);
237 if (strlen(uuid) >= sizeof session->uuid) {
241 strcpy(session->uuid, uuid);
245 strcpy(session->token, sessions.initok);
246 session->timeout = timeout;
248 session->expiration = now + timeout;
250 session->expiration = (time_t)(~(time_t)0);
251 if (session->expiration < 0)
252 session->expiration = (time_t)(((unsigned long long)session->expiration) >> 1);
254 if (!add (session)) {
259 session->access = now;
260 session->refcount = 1;
269 struct afb_session *afb_session_create (const char *uuid, int timeout)
277 /* search for an existing one not too old */
278 if (uuid != NULL && search(uuid) != NULL) {
283 return make_session(uuid, timeout, now);
286 // This function will return exiting session or newly created session
287 struct afb_session *afb_session_get (const char *uuid, int *created)
289 struct afb_session *session;
296 /* search for an existing one not too old */
298 session = search(uuid);
299 if (session != NULL) {
301 session->access = now;
308 return make_session(uuid, sessions.timeout, now);
311 struct afb_session *afb_session_addref(struct afb_session *session)
318 void afb_session_unref(struct afb_session *session)
320 if (session != NULL) {
321 assert(session->refcount != 0);
323 if (session->refcount == 0 && session->uuid[0] == 0) {
330 // Free Client Session Context
331 void afb_session_close (struct afb_session *session)
333 assert(session != NULL);
334 if (session->uuid[0] != 0) {
335 session->uuid[0] = 0;
337 if (session->refcount == 0) {
344 // Sample Generic Ping Debug API
345 int afb_session_check_token (struct afb_session *session, const char *token)
347 assert(session != NULL);
348 assert(token != NULL);
350 // compare current token with previous one
351 if (!is_active (session, NOW))
354 if (session->token[0] && strcmp (token, session->token) != 0)
360 // generate a new token and update client context
361 void afb_session_new_token (struct afb_session *session)
363 assert(session != NULL);
365 // Old token was valid let's regenerate a new one
366 new_uuid(session->token);
368 // keep track of time for session timeout and further clean up
369 if (session->timeout != 0)
370 session->expiration = NOW + session->timeout;
373 const char *afb_session_uuid (struct afb_session *session)
375 assert(session != NULL);
376 return session->uuid;
379 const char *afb_session_token (struct afb_session *session)
381 assert(session != NULL);
382 return session->token;
385 void *afb_session_get_cookie(struct afb_session *session, const void *key)
387 struct cookie *cookie;
390 idx = cookeyidx(key);
391 cookie = session->cookies[idx];
392 while(cookie != NULL) {
393 if (cookie->key == key)
394 return cookie->value;
395 cookie = cookie->next;
400 int afb_session_set_cookie(struct afb_session *session, const void *key, void *value, void (*freecb)(void*))
402 struct cookie *cookie;
405 /* search for a replacement */
406 idx = cookeyidx(key);
407 cookie = session->cookies[idx];
408 while(cookie != NULL) {
409 if (cookie->key == key) {
410 if (cookie->value != value && cookie->freecb)
411 cookie->freecb(cookie->value);
412 cookie->value = value;
413 cookie->freecb = freecb;
416 cookie = cookie->next;
420 cookie = malloc(sizeof *cookie);
421 if (cookie == NULL) {
427 cookie->value = value;
428 cookie->freecb = freecb;
429 cookie->next = session->cookies[idx];
430 session->cookies[idx] = cookie;