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>
29 #define NOW (time(NULL))
31 // Session UUID are store in a simple array [for 10 sessions this should be enough]
33 pthread_mutex_t mutex; // declare a mutex to protect hash table
34 struct AFB_clientCtx **store; // sessions store
35 int count; // current number of sessions
42 void *afb_context_get(struct afb_context *actx)
47 void afb_context_set(struct afb_context *actx, void *context, void (*free_context)(void*))
49 fprintf(stderr, "afb_context_set(%p,%p) was (%p,%p)\n",context, free_context, actx->context, actx->free_context);
50 if (actx->context != NULL && actx->free_context != NULL)
51 actx->free_context(actx->context);
52 actx->context = context;
53 actx->free_context = free_context;
56 // Free context [XXXX Should be protected again memory abort XXXX]
57 static void ctxUuidFreeCB (struct AFB_clientCtx *client)
61 // If application add a handle let's free it now
62 assert (client->contexts != NULL);
64 // Free client handle with a standard Free function, with app callback or ignore it
65 for (idx=0; idx < sessions.apicount; idx ++)
66 afb_context_set(&client->contexts[idx], NULL, NULL);
69 // Create a new store in RAM, not that is too small it will be automatically extended
70 void ctxStoreInit (int max_session_count, int timeout, const char *initok, int context_count)
72 // let's create as store as hashtable does not have any
73 sessions.store = calloc (1 + (unsigned)max_session_count, sizeof(struct AFB_clientCtx));
74 sessions.max = max_session_count;
75 sessions.timeout = timeout;
76 sessions.apicount = context_count;
77 if (strlen(initok) >= 37) {
78 fprintf(stderr, "Error: initial token '%s' too long (max length 36)", initok);
81 sessions.initok = initok;
84 static struct AFB_clientCtx *ctxStoreSearch (const char* uuid)
87 struct AFB_clientCtx *client;
89 assert (uuid != NULL);
91 pthread_mutex_lock(&sessions.mutex);
93 for (idx=0; idx < sessions.max; idx++) {
94 client = sessions.store[idx];
95 if (client && (0 == strcmp (uuid, client->uuid)))
101 pthread_mutex_unlock(&sessions.mutex);
105 static int ctxStoreDel (struct AFB_clientCtx *client)
110 assert (client != NULL);
112 pthread_mutex_lock(&sessions.mutex);
114 for (idx=0; idx < sessions.max; idx++) {
115 if (sessions.store[idx] == client) {
116 sessions.store[idx]=NULL;
124 pthread_mutex_unlock(&sessions.mutex);
128 static int ctxStoreAdd (struct AFB_clientCtx *client)
133 assert (client != NULL);
135 //fprintf (stderr, "ctxStoreAdd request uuid=%s count=%d\n", client->uuid, sessions.count);
137 pthread_mutex_lock(&sessions.mutex);
139 for (idx=0; idx < sessions.max; idx++) {
140 if (NULL == sessions.store[idx]) {
141 sessions.store[idx]= client;
149 pthread_mutex_unlock(&sessions.mutex);
153 // Check if context timeout or not
154 static int ctxStoreTooOld (struct AFB_clientCtx *ctx, time_t now)
156 return ctx->expiration <= now;
159 // Loop on every entry and remove old context sessions.hash
160 static void ctxStoreCleanUp (time_t now)
162 struct AFB_clientCtx *ctx;
165 // Loop on Sessions Table and remove anything that is older than timeout
166 for (idx=0; idx < sessions.max; idx++) {
167 ctx = sessions.store[idx];
168 if (ctx != NULL && ctxStoreTooOld(ctx, now)) {
169 ctxClientClose (ctx);
174 // This function will return exiting client context or newly created client context
175 struct AFB_clientCtx *ctxClientGetForUuid (const char *uuid)
178 struct AFB_clientCtx *clientCtx;
181 /* search for an existing one not too old */
183 ctxStoreCleanUp (now);
184 clientCtx = uuid != NULL ? ctxStoreSearch (uuid) : NULL;
186 clientCtx->refcount++;
190 /* mimic old behaviour */
192 TODO remove? not remove?
193 if (sessions.initok == NULL)
196 /* check the uuid if given */
197 if (uuid != NULL && strlen(uuid) >= sizeof clientCtx->uuid)
200 /* returns a new one */
201 clientCtx = calloc(1, sizeof(struct AFB_clientCtx)); // init NULL clientContext
202 if (clientCtx != NULL) {
203 clientCtx->contexts = calloc ((unsigned)sessions.apicount, sizeof(*clientCtx->contexts));
204 if (clientCtx->contexts != NULL) {
205 /* generate the uuid */
207 uuid_generate(newuuid);
208 uuid_unparse_lower(newuuid, clientCtx->uuid);
210 strcpy(clientCtx->uuid, uuid);
212 strcpy(clientCtx->token, sessions.initok);
213 clientCtx->expiration = now + sessions.timeout;
214 clientCtx->refcount = 1;
215 if (ctxStoreAdd (clientCtx))
217 free(clientCtx->contexts);
224 struct AFB_clientCtx *ctxClientGet(struct AFB_clientCtx *clientCtx)
226 if (clientCtx != NULL)
227 clientCtx->refcount++;
231 void ctxClientPut(struct AFB_clientCtx *clientCtx)
233 if (clientCtx != NULL) {
234 assert(clientCtx->refcount != 0);
235 --clientCtx->refcount;
239 // Free Client Session Context
240 void ctxClientClose (struct AFB_clientCtx *clientCtx)
242 assert(clientCtx != NULL);
243 if (clientCtx->created) {
244 clientCtx->created = 0;
245 ctxUuidFreeCB (clientCtx);
247 if (clientCtx->refcount == 0)
248 ctxStoreDel (clientCtx);
251 // Sample Generic Ping Debug API
252 int ctxTokenCheckLen (struct AFB_clientCtx *clientCtx, const char *token, size_t length)
254 assert(clientCtx != NULL);
255 assert(token != NULL);
257 // compare current token with previous one
258 if (ctxStoreTooOld (clientCtx, NOW))
261 if (clientCtx->token[0] && (length >= sizeof(clientCtx->token) || strncmp (token, clientCtx->token, length) || clientCtx->token[length]))
264 clientCtx->created = 1; /* creates by default */
268 // Sample Generic Ping Debug API
269 int ctxTokenCheck (struct AFB_clientCtx *clientCtx, const char *token)
271 assert(clientCtx != NULL);
272 assert(token != NULL);
274 return ctxTokenCheckLen(clientCtx, token, strlen(token));
277 // generate a new token and update client context
278 void ctxTokenNew (struct AFB_clientCtx *clientCtx)
282 assert(clientCtx != NULL);
284 // Old token was valid let's regenerate a new one
285 uuid_generate(newuuid); // create a new UUID
286 uuid_unparse_lower(newuuid, clientCtx->token);
288 // keep track of time for session timeout and further clean up
289 clientCtx->expiration = NOW + sessions.timeout;