ua_session_manager.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2014-2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2014, 2017 (c) Florian Palm
  7. * Copyright 2015 (c) Sten Grüner
  8. * Copyright 2015 (c) Oleksiy Vasylyev
  9. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  10. */
  11. #include "ua_session_manager.h"
  12. #include "ua_server_internal.h"
  13. UA_StatusCode
  14. UA_SessionManager_init(UA_SessionManager *sm, UA_Server *server) {
  15. LIST_INIT(&sm->sessions);
  16. sm->currentSessionCount = 0;
  17. sm->server = server;
  18. return UA_STATUSCODE_GOOD;
  19. }
  20. void UA_SessionManager_deleteMembers(UA_SessionManager *sm) {
  21. session_list_entry *current, *temp;
  22. LIST_FOREACH_SAFE(current, &sm->sessions, pointers, temp) {
  23. LIST_REMOVE(current, pointers);
  24. UA_Session_deleteMembersCleanup(&current->session, sm->server);
  25. UA_free(current);
  26. }
  27. }
  28. /* Delayed callback to free the session memory */
  29. static void
  30. removeSessionCallback(UA_Server *server, session_list_entry *entry) {
  31. UA_Session_deleteMembersCleanup(&entry->session, server);
  32. }
  33. static void
  34. removeSession(UA_SessionManager *sm, session_list_entry *sentry) {
  35. /* Detach the Session from the SecureChannel */
  36. UA_Session_detachFromSecureChannel(&sentry->session);
  37. /* Deactivate the session */
  38. sentry->session.activated = false;
  39. /* Detach the session from the session manager and make the capacity
  40. * available */
  41. LIST_REMOVE(sentry, pointers);
  42. UA_atomic_subUInt32(&sm->currentSessionCount, 1);
  43. /* Add a delayed callback to remove the session when the currently
  44. * scheduled jobs have completed */
  45. sentry->cleanupCallback.callback = (UA_ApplicationCallback)removeSessionCallback;
  46. sentry->cleanupCallback.application = sm->server;
  47. sentry->cleanupCallback.data = sentry;
  48. UA_WorkQueue_enqueueDelayed(&sm->server->workQueue, &sentry->cleanupCallback);
  49. }
  50. void
  51. UA_SessionManager_cleanupTimedOut(UA_SessionManager *sm,
  52. UA_DateTime nowMonotonic) {
  53. session_list_entry *sentry, *temp;
  54. LIST_FOREACH_SAFE(sentry, &sm->sessions, pointers, temp) {
  55. /* Session has timed out? */
  56. if(sentry->session.validTill >= nowMonotonic)
  57. continue;
  58. UA_LOG_INFO_SESSION(sm->server->config.logger, &sentry->session,
  59. "Session has timed out");
  60. sm->server->config.accessControl.closeSession(sm->server,
  61. &sm->server->config.accessControl,
  62. &sentry->session.sessionId,
  63. sentry->session.sessionHandle);
  64. removeSession(sm, sentry);
  65. }
  66. }
  67. UA_Session *
  68. UA_SessionManager_getSessionByToken(UA_SessionManager *sm, const UA_NodeId *token) {
  69. session_list_entry *current = NULL;
  70. LIST_FOREACH(current, &sm->sessions, pointers) {
  71. /* Token does not match */
  72. if(!UA_NodeId_equal(&current->session.header.authenticationToken, token))
  73. continue;
  74. /* Session has timed out */
  75. if(UA_DateTime_nowMonotonic() > current->session.validTill) {
  76. UA_LOG_INFO_SESSION(sm->server->config.logger, &current->session,
  77. "Client tries to use a session that has timed out");
  78. return NULL;
  79. }
  80. /* Ok, return */
  81. return &current->session;
  82. }
  83. /* Session not found */
  84. UA_String nodeIdStr = UA_STRING_NULL;
  85. UA_NodeId_toString(token, &nodeIdStr);
  86. UA_LOG_INFO(sm->server->config.logger, UA_LOGCATEGORY_SESSION,
  87. "Try to use Session with token %.*s but is not found",
  88. (int)nodeIdStr.length, nodeIdStr.data);
  89. UA_String_deleteMembers(&nodeIdStr);
  90. return NULL;
  91. }
  92. UA_Session *
  93. UA_SessionManager_getSessionById(UA_SessionManager *sm, const UA_NodeId *sessionId) {
  94. session_list_entry *current = NULL;
  95. LIST_FOREACH(current, &sm->sessions, pointers) {
  96. /* Token does not match */
  97. if(!UA_NodeId_equal(&current->session.sessionId, sessionId))
  98. continue;
  99. /* Session has timed out */
  100. if(UA_DateTime_nowMonotonic() > current->session.validTill) {
  101. UA_LOG_INFO_SESSION(sm->server->config.logger, &current->session,
  102. "Client tries to use a session that has timed out");
  103. return NULL;
  104. }
  105. /* Ok, return */
  106. return &current->session;
  107. }
  108. /* Session not found */
  109. UA_String sessionIdStr = UA_STRING_NULL;
  110. UA_NodeId_toString(sessionId, &sessionIdStr);
  111. UA_LOG_INFO(sm->server->config.logger, UA_LOGCATEGORY_SESSION,
  112. "Try to use Session with identifier %.*s but is not found",
  113. (int)sessionIdStr.length, sessionIdStr.data);
  114. UA_String_deleteMembers(&sessionIdStr);
  115. return NULL;
  116. }
  117. /* Creates and adds a session. But it is not yet attached to a secure channel. */
  118. UA_StatusCode
  119. UA_SessionManager_createSession(UA_SessionManager *sm, UA_SecureChannel *channel,
  120. const UA_CreateSessionRequest *request, UA_Session **session) {
  121. if(sm->currentSessionCount >= sm->server->config.maxSessions)
  122. return UA_STATUSCODE_BADTOOMANYSESSIONS;
  123. session_list_entry *newentry = (session_list_entry *)UA_malloc(sizeof(session_list_entry));
  124. if(!newentry)
  125. return UA_STATUSCODE_BADOUTOFMEMORY;
  126. UA_atomic_addUInt32(&sm->currentSessionCount, 1);
  127. UA_Session_init(&newentry->session);
  128. newentry->session.sessionId = UA_NODEID_GUID(1, UA_Guid_random());
  129. newentry->session.header.authenticationToken = UA_NODEID_GUID(1, UA_Guid_random());
  130. if(request->requestedSessionTimeout <= sm->server->config.maxSessionTimeout &&
  131. request->requestedSessionTimeout > 0)
  132. newentry->session.timeout = request->requestedSessionTimeout;
  133. else
  134. newentry->session.timeout = sm->server->config.maxSessionTimeout;
  135. UA_Session_updateLifetime(&newentry->session);
  136. LIST_INSERT_HEAD(&sm->sessions, newentry, pointers);
  137. *session = &newentry->session;
  138. return UA_STATUSCODE_GOOD;
  139. }
  140. UA_StatusCode
  141. UA_SessionManager_removeSession(UA_SessionManager *sm, const UA_NodeId *token) {
  142. session_list_entry *current;
  143. LIST_FOREACH(current, &sm->sessions, pointers) {
  144. if(UA_NodeId_equal(&current->session.header.authenticationToken, token))
  145. break;
  146. }
  147. if(!current)
  148. return UA_STATUSCODE_BADSESSIONIDINVALID;
  149. removeSession(sm, current);
  150. return UA_STATUSCODE_GOOD;
  151. }