ua_session_manager.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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) Julius Pfrommer, Fraunhofer IOSB
  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, void *entry) {
  31. session_list_entry *sentry = (session_list_entry*)entry;
  32. UA_Session_deleteMembersCleanup(&sentry->session, server);
  33. UA_free(sentry);
  34. }
  35. static UA_StatusCode
  36. removeSession(UA_SessionManager *sm, session_list_entry *sentry) {
  37. /* Detach the Session from the SecureChannel */
  38. UA_Session_detachFromSecureChannel(&sentry->session);
  39. /* Deactivate the session */
  40. sentry->session.activated = false;
  41. /* Add a delayed callback to remove the session when the currently
  42. * scheduled jobs have completed */
  43. UA_StatusCode retval = UA_Server_delayedCallback(sm->server, removeSessionCallback, sentry);
  44. if(retval != UA_STATUSCODE_GOOD) {
  45. UA_LOG_WARNING_SESSION(sm->server->config.logger, &sentry->session,
  46. "Could not remove session with error code %s",
  47. UA_StatusCode_name(retval));
  48. return retval; /* Try again next time */
  49. }
  50. /* Detach the session from the session manager and make the capacity
  51. * available */
  52. LIST_REMOVE(sentry, pointers);
  53. UA_atomic_subUInt32(&sm->currentSessionCount, 1);
  54. return UA_STATUSCODE_GOOD;
  55. }
  56. void
  57. UA_SessionManager_cleanupTimedOut(UA_SessionManager *sm,
  58. UA_DateTime nowMonotonic) {
  59. session_list_entry *sentry, *temp;
  60. LIST_FOREACH_SAFE(sentry, &sm->sessions, pointers, temp) {
  61. /* Session has timed out? */
  62. if(sentry->session.validTill >= nowMonotonic)
  63. continue;
  64. UA_LOG_INFO_SESSION(sm->server->config.logger, &sentry->session,
  65. "Session has timed out");
  66. sm->server->config.accessControl.closeSession(&sentry->session.sessionId,
  67. sentry->session.sessionHandle);
  68. removeSession(sm, sentry);
  69. }
  70. }
  71. UA_Session *
  72. UA_SessionManager_getSessionByToken(UA_SessionManager *sm, const UA_NodeId *token) {
  73. session_list_entry *current = NULL;
  74. LIST_FOREACH(current, &sm->sessions, pointers) {
  75. /* Token does not match */
  76. if(!UA_NodeId_equal(&current->session.header.authenticationToken, token))
  77. continue;
  78. /* Session has timed out */
  79. if(UA_DateTime_nowMonotonic() > current->session.validTill) {
  80. UA_LOG_INFO_SESSION(sm->server->config.logger, &current->session,
  81. "Client tries to use a session that has timed out");
  82. return NULL;
  83. }
  84. /* Ok, return */
  85. return &current->session;
  86. }
  87. /* Session not found */
  88. UA_LOG_INFO(sm->server->config.logger, UA_LOGCATEGORY_SESSION,
  89. "Try to use Session with token " UA_PRINTF_GUID_FORMAT " but is not found",
  90. UA_PRINTF_GUID_DATA(token->identifier.guid));
  91. return NULL;
  92. }
  93. UA_Session *
  94. UA_SessionManager_getSessionById(UA_SessionManager *sm, const UA_NodeId *sessionId) {
  95. session_list_entry *current = NULL;
  96. LIST_FOREACH(current, &sm->sessions, pointers) {
  97. /* Token does not match */
  98. if(!UA_NodeId_equal(&current->session.sessionId, sessionId))
  99. continue;
  100. /* Session has timed out */
  101. if(UA_DateTime_nowMonotonic() > current->session.validTill) {
  102. UA_LOG_INFO_SESSION(sm->server->config.logger, &current->session,
  103. "Client tries to use a session that has timed out");
  104. return NULL;
  105. }
  106. /* Ok, return */
  107. return &current->session;
  108. }
  109. /* Session not found */
  110. UA_LOG_INFO(sm->server->config.logger, UA_LOGCATEGORY_SESSION,
  111. "Try to use Session with identifier " UA_PRINTF_GUID_FORMAT " but is not found",
  112. UA_PRINTF_GUID_DATA(sessionId->identifier.guid));
  113. return NULL;
  114. }
  115. /* Creates and adds a session. But it is not yet attached to a secure channel. */
  116. UA_StatusCode
  117. UA_SessionManager_createSession(UA_SessionManager *sm, UA_SecureChannel *channel,
  118. const UA_CreateSessionRequest *request, UA_Session **session) {
  119. if(sm->currentSessionCount >= sm->server->config.maxSessions)
  120. return UA_STATUSCODE_BADTOOMANYSESSIONS;
  121. session_list_entry *newentry = (session_list_entry *)UA_malloc(sizeof(session_list_entry));
  122. if(!newentry)
  123. return UA_STATUSCODE_BADOUTOFMEMORY;
  124. UA_atomic_addUInt32(&sm->currentSessionCount, 1);
  125. UA_Session_init(&newentry->session);
  126. newentry->session.sessionId = UA_NODEID_GUID(1, UA_Guid_random());
  127. newentry->session.header.authenticationToken = UA_NODEID_GUID(1, UA_Guid_random());
  128. if(request->requestedSessionTimeout <= sm->server->config.maxSessionTimeout &&
  129. request->requestedSessionTimeout > 0)
  130. newentry->session.timeout = request->requestedSessionTimeout;
  131. else
  132. newentry->session.timeout = sm->server->config.maxSessionTimeout;
  133. UA_Session_updateLifetime(&newentry->session);
  134. LIST_INSERT_HEAD(&sm->sessions, newentry, pointers);
  135. *session = &newentry->session;
  136. return UA_STATUSCODE_GOOD;
  137. }
  138. UA_StatusCode
  139. UA_SessionManager_removeSession(UA_SessionManager *sm, const UA_NodeId *token) {
  140. session_list_entry *current;
  141. LIST_FOREACH(current, &sm->sessions, pointers) {
  142. if(UA_NodeId_equal(&current->session.header.authenticationToken, token))
  143. break;
  144. }
  145. if(!current)
  146. return UA_STATUSCODE_BADSESSIONIDINVALID;
  147. return removeSession(sm, current);
  148. }