2 * Copyright (C) 2015 "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*);
41 struct afb_event_listener_list
43 struct afb_event_listener_list *next;
44 struct afb_event_listener listener;
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 client_value *values;
56 struct afb_event_listener_list *listeners;
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_clientCtx **store; // sessions store
63 int count; // current number of sessions
70 // Free context [XXXX Should be protected again memory abort XXXX]
71 static void ctxUuidFreeCB (struct AFB_clientCtx *client)
75 // If application add a handle let's free it now
76 assert (client->values != NULL);
78 // Free client handle with a standard Free function, with app callback or ignore it
79 for (idx=0; idx < sessions.apicount; idx ++)
80 ctxClientValueSet(client, idx, NULL, NULL);
83 // Create a new store in RAM, not that is too small it will be automatically extended
84 void ctxStoreInit (int max_session_count, int timeout, const char *initok, int context_count)
87 // let's create as store as hashtable does not have any
88 sessions.store = calloc (1 + (unsigned)max_session_count, sizeof(struct AFB_clientCtx));
89 sessions.max = max_session_count;
90 sessions.timeout = timeout;
91 sessions.apicount = context_count;
93 ERROR("\"--token=\" parameter is mandatory");
96 if (strlen(initok) >= sizeof(sessions.store[0]->token)) {
97 ERROR("initial token '%s' too long (max length 36)", initok);
100 sessions.initok = initok;
103 static struct AFB_clientCtx *ctxStoreSearch (const char* uuid)
106 struct AFB_clientCtx *client;
108 assert (uuid != NULL);
110 pthread_mutex_lock(&sessions.mutex);
112 for (idx=0; idx < sessions.max; idx++) {
113 client = sessions.store[idx];
114 if (client && (0 == strcmp (uuid, client->uuid)))
120 pthread_mutex_unlock(&sessions.mutex);
124 static int ctxStoreDel (struct AFB_clientCtx *client)
129 assert (client != NULL);
131 pthread_mutex_lock(&sessions.mutex);
133 for (idx=0; idx < sessions.max; idx++) {
134 if (sessions.store[idx] == client) {
135 sessions.store[idx] = NULL;
143 pthread_mutex_unlock(&sessions.mutex);
147 static int ctxStoreAdd (struct AFB_clientCtx *client)
152 assert (client != NULL);
154 pthread_mutex_lock(&sessions.mutex);
156 for (idx=0; idx < sessions.max; idx++) {
157 if (NULL == sessions.store[idx]) {
158 sessions.store[idx] = client;
166 pthread_mutex_unlock(&sessions.mutex);
170 // Check if context timeout or not
171 static int ctxStoreTooOld (struct AFB_clientCtx *ctx, time_t now)
173 return ctx->expiration <= now;
176 // Loop on every entry and remove old context sessions.hash
177 static void ctxStoreCleanUp (time_t now)
179 struct AFB_clientCtx *ctx;
182 // Loop on Sessions Table and remove anything that is older than timeout
183 for (idx=0; idx < sessions.max; idx++) {
184 ctx = sessions.store[idx];
185 if (ctx != NULL && ctxStoreTooOld(ctx, now)) {
186 ctxClientClose (ctx);
191 // This function will return exiting client context or newly created client context
192 struct AFB_clientCtx *ctxClientGetSession (const char *uuid, int *created)
195 struct AFB_clientCtx *clientCtx;
200 ctxStoreCleanUp (now);
202 /* search for an existing one not too old */
204 if (strlen(uuid) >= sizeof clientCtx->uuid) {
208 clientCtx = ctxStoreSearch(uuid);
209 if (clientCtx != NULL) {
215 /* returns a new one */
216 clientCtx = calloc(1, sizeof(struct AFB_clientCtx) + ((unsigned)sessions.apicount * sizeof(*clientCtx->values)));
217 if (clientCtx == NULL) {
221 clientCtx->values = (void*)(clientCtx + 1);
223 /* generate the uuid */
225 uuid_generate(newuuid);
226 uuid_unparse_lower(newuuid, clientCtx->uuid);
228 strcpy(clientCtx->uuid, uuid);
232 strcpy(clientCtx->token, sessions.initok);
233 clientCtx->expiration = now + sessions.timeout;
234 if (!ctxStoreAdd (clientCtx)) {
241 clientCtx->access = now;
242 clientCtx->refcount++;
251 struct AFB_clientCtx *ctxClientAddRef(struct AFB_clientCtx *clientCtx)
253 if (clientCtx != NULL)
254 clientCtx->refcount++;
258 void ctxClientUnref(struct AFB_clientCtx *clientCtx)
260 if (clientCtx != NULL) {
261 assert(clientCtx->refcount != 0);
262 --clientCtx->refcount;
263 if (clientCtx->refcount == 0 && clientCtx->uuid[0] == 0) {
264 ctxStoreDel (clientCtx);
269 // Free Client Session Context
270 void ctxClientClose (struct AFB_clientCtx *clientCtx)
272 assert(clientCtx != NULL);
273 ctxUuidFreeCB (clientCtx);
274 clientCtx->uuid[0] = 0;
277 // Sample Generic Ping Debug API
278 int ctxTokenCheck (struct AFB_clientCtx *clientCtx, const char *token)
280 assert(clientCtx != NULL);
281 assert(token != NULL);
283 // compare current token with previous one
284 if (ctxStoreTooOld (clientCtx, NOW))
287 if (clientCtx->token[0] && strcmp (token, clientCtx->token) != 0)
293 // generate a new token and update client context
294 void ctxTokenNew (struct AFB_clientCtx *clientCtx)
298 assert(clientCtx != NULL);
300 // Old token was valid let's regenerate a new one
301 uuid_generate(newuuid); // create a new UUID
302 uuid_unparse_lower(newuuid, clientCtx->token);
304 // keep track of time for session timeout and further clean up
305 clientCtx->expiration = NOW + sessions.timeout;
308 int ctxClientEventListenerAdd(struct AFB_clientCtx *clientCtx, struct afb_event_listener listener)
310 struct afb_event_listener_list *iter, **prv;
312 prv = &clientCtx->listeners;
316 iter = calloc(1, sizeof *iter);
321 iter->listener = listener;
326 if (iter->listener.itf == listener.itf && iter->listener.closure == listener.closure) {
334 void ctxClientEventListenerRemove(struct AFB_clientCtx *clientCtx, struct afb_event_listener listener)
336 struct afb_event_listener_list *iter, **prv;
338 prv = &clientCtx->listeners;
343 if (iter->listener.itf == listener.itf && iter->listener.closure == listener.closure) {
344 if (!--iter->refcount) {
354 static int send(struct AFB_clientCtx *clientCtx, const char *event, struct json_object *object)
356 struct afb_event_listener_list *iter;
360 iter = clientCtx->listeners;
361 while (iter != NULL) {
362 iter->listener.itf->send(iter->listener.closure, event, json_object_get(object));
370 int ctxClientEventSend(struct AFB_clientCtx *clientCtx, const char *event, struct json_object *object)
376 if (clientCtx != NULL)
377 result = send(clientCtx, event, object);
381 for (idx=0; idx < sessions.max; idx++) {
382 clientCtx = sessions.store[idx];
383 if (clientCtx != NULL && !ctxStoreTooOld(clientCtx, now)) {
384 clientCtx = ctxClientAddRef(clientCtx);
385 result += send(clientCtx, event, object);
386 ctxClientUnref(clientCtx);
393 const char *ctxClientGetUuid (struct AFB_clientCtx *clientCtx)
395 assert(clientCtx != NULL);
396 return clientCtx->uuid;
399 const char *ctxClientGetToken (struct AFB_clientCtx *clientCtx)
401 assert(clientCtx != NULL);
402 return clientCtx->token;
405 void *ctxClientValueGet(struct AFB_clientCtx *clientCtx, int index)
407 assert(clientCtx != NULL);
409 assert(index < sessions.apicount);
410 return clientCtx->values[index].value;
413 void ctxClientValueSet(struct AFB_clientCtx *clientCtx, int index, void *value, void (*free_value)(void*))
415 struct client_value prev;
416 assert(clientCtx != NULL);
418 assert(index < sessions.apicount);
419 prev = clientCtx->values[index];
420 clientCtx->values[index] = (struct client_value){.value = value, .free_value = free_value};
421 if (prev.value != NULL && prev.free_value != NULL)
422 prev.free_value(prev.value);