#define SIZEUUID 37
#define HEADCOUNT 16
-#define COOKEYCOUNT 8
-#define COOKEYMASK (COOKEYCOUNT - 1)
+#define COOKIECOUNT 8
+#define COOKIEMASK (COOKIECOUNT - 1)
#define _MAXEXP_ ((time_t)(~(time_t)0))
#define _MAXEXP2_ ((time_t)((((unsigned long long)_MAXEXP_) >> 1)))
int timeout;
time_t expiration; // expiration time of the token
pthread_mutex_t mutex;
- struct cookie *cookies[COOKEYCOUNT];
+ struct cookie *cookies[COOKIECOUNT];
+ char autoclose;
char idx;
char uuid[SIZEUUID]; // long term authentication of remote client
char token[SIZEUUID]; // short term authentication of remote client
struct cookie *cookie;
/* free cookies */
- for (idx = 0 ; idx < COOKEYCOUNT ; idx++) {
+ for (idx = 0 ; idx < COOKIECOUNT ; idx++) {
while ((cookie = session->cookies[idx])) {
session->cookies[idx] = cookie->next;
if (cookie->freecb != NULL)
return r; // % HEADCOUNT;
}
-// Create a new store in RAM, not that is too small it will be automatically extended
-void afb_session_init (int max_session_count, int timeout, const char *initok)
+/**
+ * Initialize the session manager with a 'max_session_count',
+ * an initial common 'timeout' and an initial common token 'initok'.
+ *
+ * @param max_session_count maximum allowed session count in the same time
+ * @param timeout the initial default timeout of sessions
+ * @param initok the initial default token of sessions
+ *
+ */
+int afb_session_init (int max_session_count, int timeout, const char *initok)
{
pthread_mutex_init(&sessions.mutex, NULL);
sessions.max = max_session_count;
strcpy(sessions.initok, initok);
else {
ERROR("initial token '%s' too long (max length %d)", initok, ((int)(sizeof sessions.initok)) - 1);
- exit(1);
+ errno = EINVAL;
+ return -1;
}
+ return 0;
}
const char *afb_session_initial_token()
assert(session->refcount != 0);
if (!__atomic_sub_fetch(&session->refcount, 1, __ATOMIC_RELAXED)) {
pthread_mutex_lock(&session->mutex);
- if (session->uuid[0] == 0)
+ if (session->autoclose || session->uuid[0] == 0)
destroy (session);
else
pthread_mutex_unlock(&session->mutex);
pthread_mutex_unlock(&session->mutex);
}
+/* set the autoclose flag */
+void afb_session_set_autoclose(struct afb_session *session, int autoclose)
+{
+ assert(session != NULL);
+ session->autoclose = (char)!!autoclose;
+}
+
// is the session active?
int afb_session_is_active (struct afb_session *session)
{
{
intptr_t x = (intptr_t)key;
unsigned r = (unsigned)((x >> 5) ^ (x >> 15));
- return r & COOKEYMASK;
+ return r & COOKIEMASK;
}
/**