123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #include "ua_session_manager.h"
- #include "ua_util.h"
- struct session_list_entry {
- UA_Session session;
- LIST_ENTRY(session_list_entry) pointers;
- };
- struct UA_SessionManager {
- LIST_HEAD(session_list, session_list_entry) sessions;
- UA_UInt32 maxSessionCount;
- UA_Int32 lastSessionId;
- UA_UInt32 currentSessionCount;
- UA_DateTime maxSessionLifeTime;
- UA_DateTime sessionTimeout;
- };
- UA_Int32 UA_SessionManager_new(UA_SessionManager **sessionManager, UA_UInt32 maxSessionCount,
- UA_UInt32 sessionTimeout, UA_UInt32 startSessionId) {
- UA_Int32 retval = UA_SUCCESS;
- retval |= UA_alloc((void **)sessionManager, sizeof(UA_SessionManager));
- if(retval != UA_SUCCESS)
- return UA_ERROR;
- LIST_INIT(&(*sessionManager)->sessions);
- (*sessionManager)->maxSessionCount = maxSessionCount;
- (*sessionManager)->lastSessionId = startSessionId;
- (*sessionManager)->sessionTimeout = sessionTimeout;
- return retval;
- }
- UA_Int32 UA_SessionManager_delete(UA_SessionManager *sessionManager) {
-
- return UA_SUCCESS;
- }
- UA_Int32 UA_SessionManager_getSessionById(UA_SessionManager *sessionManager, UA_NodeId *sessionId, UA_Session **session) {
- if(sessionManager == UA_NULL) {
- *session = UA_NULL;
- return UA_ERROR;
- }
- struct session_list_entry *current = UA_NULL;
- LIST_FOREACH(current, &sessionManager->sessions, pointers) {
- if(UA_NodeId_equal(¤t->session.sessionId, sessionId) == UA_EQUAL)
- break;
- }
- if(!current) {
- *session = UA_NULL;
- return UA_ERROR;
- }
-
-
- *session = ¤t->session;
- return UA_SUCCESS;
- }
- UA_Int32 UA_SessionManager_getSessionByToken(UA_SessionManager *sessionManager, UA_NodeId *token, UA_Session **session) {
- if(sessionManager == UA_NULL) {
- *session = UA_NULL;
- return UA_ERROR;
- }
- struct session_list_entry *current = UA_NULL;
- LIST_FOREACH(current, &sessionManager->sessions, pointers) {
- if(UA_NodeId_equal(¤t->session.authenticationToken, token) == UA_EQUAL)
- break;
- }
- if(!current) {
- *session = UA_NULL;
- return UA_ERROR;
- }
-
-
- *session = ¤t->session;
- return UA_SUCCESS;
- }
- UA_Int32 UA_SessionManager_createSession(UA_SessionManager *sessionManager, UA_SecureChannel *channel, UA_Session **session) {
- if(sessionManager->currentSessionCount >= sessionManager->maxSessionCount)
- return UA_ERROR;
- struct session_list_entry *newentry;
- if(UA_alloc((void **)&newentry, sizeof(struct session_list_entry)) != UA_SUCCESS)
- return UA_ERROR;
- UA_Session_init(&newentry->session);
- newentry->session.sessionId = (UA_NodeId) {.namespaceIndex = 1, .identifierType = UA_NODEIDTYPE_NUMERIC,
- .identifier.numeric = sessionManager->lastSessionId++ };
- newentry->session.authenticationToken = (UA_NodeId) {.namespaceIndex = 1, .identifierType = UA_NODEIDTYPE_NUMERIC,
- .identifier.numeric = sessionManager->lastSessionId };
- newentry->session.channel = channel;
- newentry->session.timeout = 3600 * 1000;
- UA_Session_setExpirationDate(&newentry->session);
- sessionManager->currentSessionCount++;
- LIST_INSERT_HEAD(&sessionManager->sessions, newentry, pointers);
- *session = &newentry->session;
- return UA_SUCCESS;
- }
- UA_Int32 UA_SessionManager_removeSession(UA_SessionManager *sessionManager, UA_NodeId *sessionId) {
- struct session_list_entry *current = UA_NULL;
- LIST_FOREACH(current, &sessionManager->sessions, pointers) {
- if(UA_NodeId_equal(¤t->session.sessionId, sessionId) == UA_EQUAL)
- break;
- }
- if(!current)
- return UA_ERROR;
- LIST_REMOVE(current, pointers);
- UA_free(current);
- return UA_SUCCESS;
- }
|