2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Fulup Ar Foll"
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
24 #include <uuid/uuid.h>
28 #include <json-c/json.h>
33 #define NOW (time(NULL))
38 void (*free_value)(void*);
46 time_t expiration; // expiration time of the token
48 char uuid[37]; // long term authentication of remote client
49 char token[37]; // short term authentication of remote client
50 struct client_value *values;
53 // Session UUID are store in a simple array [for 10 sessions this should be enough]
55 pthread_mutex_t mutex; // declare a mutex to protect hash table
56 struct AFB_clientCtx **store; // sessions store
57 int count; // current number of sessions
65 static void new_uuid(char uuid[37])
68 uuid_generate(newuuid);
69 uuid_unparse_lower(newuuid, uuid);
72 // Free context [XXXX Should be protected again memory abort XXXX]
73 static void ctxUuidFreeCB (struct AFB_clientCtx *client)
77 // If application add a handle let's free it now
78 assert (client->values != NULL);
80 // Free client handle with a standard Free function, with app callback or ignore it
81 for (idx=0; idx < sessions.apicount; idx ++)
82 ctxClientValueSet(client, idx, NULL, NULL);
85 // Create a new store in RAM, not that is too small it will be automatically extended
86 void ctxStoreInit (int max_session_count, int timeout, const char *initok, int context_count)
88 // let's create as store as hashtable does not have any
89 sessions.store = calloc (1 + (unsigned)max_session_count, sizeof(struct AFB_clientCtx));
90 sessions.max = max_session_count;
91 sessions.timeout = timeout;
92 sessions.apicount = context_count;
94 /* without token, a secret is made to forbid creation of sessions */
95 new_uuid(sessions.initok);
96 else if (strlen(initok) < sizeof(sessions.store[0]->token))
97 strcpy(sessions.initok, initok);
99 ERROR("initial token '%s' too long (max length 36)", initok);
104 static struct AFB_clientCtx *ctxStoreSearch (const char* uuid)
107 struct AFB_clientCtx *client;
109 assert (uuid != NULL);
111 pthread_mutex_lock(&sessions.mutex);
113 for (idx=0; idx < sessions.max; idx++) {
114 client = sessions.store[idx];
115 if (client && (0 == strcmp (uuid, client->uuid)))
121 pthread_mutex_unlock(&sessions.mutex);
125 static int ctxStoreDel (struct AFB_clientCtx *client)
130 assert (client != NULL);
132 pthread_mutex_lock(&sessions.mutex);
134 for (idx=0; idx < sessions.max; idx++) {
135 if (sessions.store[idx] == client) {
136 sessions.store[idx] = NULL;
144 pthread_mutex_unlock(&sessions.mutex);
148 static int ctxStoreAdd (struct AFB_clientCtx *client)
153 assert (client != NULL);
155 pthread_mutex_lock(&sessions.mutex);
157 for (idx=0; idx < sessions.max; idx++) {
158 if (NULL == sessions.store[idx]) {
159 sessions.store[idx] = client;
167 pthread_mutex_unlock(&sessions.mutex);
171 // Check if context timeout or not
172 static int ctxStoreTooOld (struct AFB_clientCtx *ctx, time_t now)
174 assert (ctx != NULL);
175 return ctx->expiration < now;
178 // Check if context is active or not
179 static int ctxIsActive (struct AFB_clientCtx *ctx, time_t now)
181 assert (ctx != NULL);
182 return ctx->uuid[0] != 0 && ctx->expiration >= now;
185 // Loop on every entry and remove old context sessions.hash
186 static void ctxStoreCleanUp (time_t now)
188 struct AFB_clientCtx *ctx;
191 // Loop on Sessions Table and remove anything that is older than timeout
192 for (idx=0; idx < sessions.max; idx++) {
193 ctx = sessions.store[idx];
194 if (ctx != NULL && ctxStoreTooOld(ctx, now)) {
195 ctxClientClose (ctx);
200 static struct AFB_clientCtx *new_context (const char *uuid, int timeout, time_t now)
202 struct AFB_clientCtx *clientCtx;
204 /* allocates a new one */
205 clientCtx = calloc(1, sizeof(struct AFB_clientCtx) + ((unsigned)sessions.apicount * sizeof(*clientCtx->values)));
206 if (clientCtx == NULL) {
210 clientCtx->values = (void*)(clientCtx + 1);
212 /* generate the uuid */
214 new_uuid(clientCtx->uuid);
216 if (strlen(uuid) >= sizeof clientCtx->uuid) {
220 strcpy(clientCtx->uuid, uuid);
224 strcpy(clientCtx->token, sessions.initok);
225 clientCtx->timeout = timeout;
227 clientCtx->expiration = now + timeout;
229 clientCtx->expiration = (time_t)(~(time_t)0);
230 if (clientCtx->expiration < 0)
231 clientCtx->expiration = (time_t)(((unsigned long long)clientCtx->expiration) >> 1);
233 if (!ctxStoreAdd (clientCtx)) {
238 clientCtx->access = now;
239 clientCtx->refcount = 1;
248 struct AFB_clientCtx *ctxClientCreate (const char *uuid, int timeout)
254 ctxStoreCleanUp (now);
256 /* search for an existing one not too old */
257 if (uuid != NULL && ctxStoreSearch(uuid) != NULL) {
262 return new_context(uuid, timeout, now);
265 // This function will return exiting client context or newly created client context
266 struct AFB_clientCtx *ctxClientGetSession (const char *uuid, int *created)
268 struct AFB_clientCtx *clientCtx;
273 ctxStoreCleanUp (now);
275 /* search for an existing one not too old */
277 clientCtx = ctxStoreSearch(uuid);
278 if (clientCtx != NULL) {
280 clientCtx->access = now;
281 clientCtx->refcount++;
287 return new_context(uuid, sessions.timeout, now);
290 struct AFB_clientCtx *ctxClientAddRef(struct AFB_clientCtx *clientCtx)
292 if (clientCtx != NULL)
293 clientCtx->refcount++;
297 void ctxClientUnref(struct AFB_clientCtx *clientCtx)
299 if (clientCtx != NULL) {
300 assert(clientCtx->refcount != 0);
301 --clientCtx->refcount;
302 if (clientCtx->refcount == 0 && clientCtx->uuid[0] == 0) {
303 ctxStoreDel (clientCtx);
309 // Free Client Session Context
310 void ctxClientClose (struct AFB_clientCtx *clientCtx)
312 assert(clientCtx != NULL);
313 if (clientCtx->uuid[0] != 0) {
314 clientCtx->uuid[0] = 0;
315 ctxUuidFreeCB (clientCtx);
316 if (clientCtx->refcount == 0) {
317 ctxStoreDel (clientCtx);
323 // Sample Generic Ping Debug API
324 int ctxTokenCheck (struct AFB_clientCtx *clientCtx, const char *token)
326 assert(clientCtx != NULL);
327 assert(token != NULL);
329 // compare current token with previous one
330 if (!ctxIsActive (clientCtx, NOW))
333 if (clientCtx->token[0] && strcmp (token, clientCtx->token) != 0)
339 // generate a new token and update client context
340 void ctxTokenNew (struct AFB_clientCtx *clientCtx)
342 assert(clientCtx != NULL);
344 // Old token was valid let's regenerate a new one
345 new_uuid(clientCtx->token);
347 // keep track of time for session timeout and further clean up
348 if (clientCtx->timeout != 0)
349 clientCtx->expiration = NOW + clientCtx->timeout;
352 const char *ctxClientGetUuid (struct AFB_clientCtx *clientCtx)
354 assert(clientCtx != NULL);
355 return clientCtx->uuid;
358 const char *ctxClientGetToken (struct AFB_clientCtx *clientCtx)
360 assert(clientCtx != NULL);
361 return clientCtx->token;
364 unsigned ctxClientGetLOA (struct AFB_clientCtx *clientCtx)
366 assert(clientCtx != NULL);
367 return clientCtx->loa;
370 void ctxClientSetLOA (struct AFB_clientCtx *clientCtx, unsigned loa)
372 assert(clientCtx != NULL);
373 clientCtx->loa = loa;
376 void *ctxClientValueGet(struct AFB_clientCtx *clientCtx, int index)
378 assert(clientCtx != NULL);
380 assert(index < sessions.apicount);
381 return clientCtx->values[index].value;
384 void ctxClientValueSet(struct AFB_clientCtx *clientCtx, int index, void *value, void (*free_value)(void*))
386 struct client_value prev;
387 assert(clientCtx != NULL);
389 assert(index < sessions.apicount);
390 prev = clientCtx->values[index];
391 clientCtx->values[index] = (struct client_value){.value = value, .free_value = free_value};
392 if (prev.value != NULL && prev.value != value && prev.free_value != NULL)
393 prev.free_value(prev.value);