ua_session_manager.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include "ua_subscription.h"
  14. UA_StatusCode
  15. UA_SessionManager_init(UA_SessionManager *sm, UA_Server *server) {
  16. LIST_INIT(&sm->sessions);
  17. sm->currentSessionCount = 0;
  18. sm->server = server;
  19. return UA_STATUSCODE_GOOD;
  20. }
  21. /* Delayed callback to free the session memory */
  22. static void
  23. removeSessionCallback(UA_Server *server, session_list_entry *entry) {
  24. UA_Session_deleteMembersCleanup(&entry->session, server);
  25. }
  26. static void
  27. removeSession(UA_SessionManager *sm, session_list_entry *sentry) {
  28. /* Remove the Subscriptions */
  29. #ifdef UA_ENABLE_SUBSCRIPTIONS
  30. UA_Subscription *sub, *tempsub;
  31. LIST_FOREACH_SAFE(sub, &sentry->session.serverSubscriptions, listEntry, tempsub) {
  32. UA_Session_deleteSubscription(sm->server, &sentry->session, sub->subscriptionId);
  33. }
  34. UA_PublishResponseEntry *entry;
  35. while((entry = UA_Session_dequeuePublishReq(&sentry->session))) {
  36. UA_PublishResponse_deleteMembers(&entry->response);
  37. UA_free(entry);
  38. }
  39. #endif
  40. /* Detach the Session from the SecureChannel */
  41. UA_Session_detachFromSecureChannel(&sentry->session);
  42. /* Deactivate the session */
  43. sentry->session.activated = false;
  44. /* Detach the session from the session manager and make the capacity
  45. * available */
  46. LIST_REMOVE(sentry, pointers);
  47. UA_atomic_subUInt32(&sm->currentSessionCount, 1);
  48. /* Add a delayed callback to remove the session when the currently
  49. * scheduled jobs have completed */
  50. sentry->cleanupCallback.callback = (UA_ApplicationCallback)removeSessionCallback;
  51. sentry->cleanupCallback.application = sm->server;
  52. sentry->cleanupCallback.data = sentry;
  53. UA_WorkQueue_enqueueDelayed(&sm->server->workQueue, &sentry->cleanupCallback);
  54. }
  55. void UA_SessionManager_deleteMembers(UA_SessionManager *sm) {
  56. session_list_entry *current, *temp;
  57. LIST_FOREACH_SAFE(current, &sm->sessions, pointers, temp) {
  58. removeSession(sm, current);
  59. }
  60. }
  61. void
  62. UA_SessionManager_cleanupTimedOut(UA_SessionManager *sm,
  63. UA_DateTime nowMonotonic) {
  64. session_list_entry *sentry, *temp;
  65. LIST_FOREACH_SAFE(sentry, &sm->sessions, pointers, temp) {
  66. /* Session has timed out? */
  67. if(sentry->session.validTill >= nowMonotonic)
  68. continue;
  69. UA_LOG_INFO_SESSION(&sm->server->config.logger, &sentry->session,
  70. "Session has timed out");
  71. sm->server->config.accessControl.closeSession(sm->server,
  72. &sm->server->config.accessControl,
  73. &sentry->session.sessionId,
  74. sentry->session.sessionHandle);
  75. removeSession(sm, sentry);
  76. }
  77. }
  78. UA_Session *
  79. UA_SessionManager_getSessionByToken(UA_SessionManager *sm, const UA_NodeId *token) {
  80. session_list_entry *current = NULL;
  81. LIST_FOREACH(current, &sm->sessions, pointers) {
  82. /* Token does not match */
  83. if(!UA_NodeId_equal(&current->session.header.authenticationToken, token))
  84. continue;
  85. /* Session has timed out */
  86. if(UA_DateTime_nowMonotonic() > current->session.validTill) {
  87. UA_LOG_INFO_SESSION(&sm->server->config.logger, &current->session,
  88. "Client tries to use a session that has timed out");
  89. return NULL;
  90. }
  91. /* Ok, return */
  92. return &current->session;
  93. }
  94. /* Session not found */
  95. UA_String nodeIdStr = UA_STRING_NULL;
  96. UA_NodeId_toString(token, &nodeIdStr);
  97. UA_LOG_INFO(&sm->server->config.logger, UA_LOGCATEGORY_SESSION,
  98. "Try to use Session with token %.*s but is not found",
  99. (int)nodeIdStr.length, nodeIdStr.data);
  100. UA_String_deleteMembers(&nodeIdStr);
  101. return NULL;
  102. }
  103. UA_Session *
  104. UA_SessionManager_getSessionById(UA_SessionManager *sm, const UA_NodeId *sessionId) {
  105. session_list_entry *current = NULL;
  106. LIST_FOREACH(current, &sm->sessions, pointers) {
  107. /* Token does not match */
  108. if(!UA_NodeId_equal(&current->session.sessionId, sessionId))
  109. continue;
  110. /* Session has timed out */
  111. if(UA_DateTime_nowMonotonic() > current->session.validTill) {
  112. UA_LOG_INFO_SESSION(&sm->server->config.logger, &current->session,
  113. "Client tries to use a session that has timed out");
  114. return NULL;
  115. }
  116. /* Ok, return */
  117. return &current->session;
  118. }
  119. /* Session not found */
  120. UA_String sessionIdStr = UA_STRING_NULL;
  121. UA_NodeId_toString(sessionId, &sessionIdStr);
  122. UA_LOG_INFO(&sm->server->config.logger, UA_LOGCATEGORY_SESSION,
  123. "Try to use Session with identifier %.*s but is not found",
  124. (int)sessionIdStr.length, sessionIdStr.data);
  125. UA_String_deleteMembers(&sessionIdStr);
  126. return NULL;
  127. }
  128. /* Creates and adds a session. But it is not yet attached to a secure channel. */
  129. UA_StatusCode
  130. UA_SessionManager_createSession(UA_SessionManager *sm, UA_SecureChannel *channel,
  131. const UA_CreateSessionRequest *request, UA_Session **session) {
  132. if(sm->currentSessionCount >= sm->server->config.maxSessions)
  133. return UA_STATUSCODE_BADTOOMANYSESSIONS;
  134. session_list_entry *newentry = (session_list_entry *)UA_malloc(sizeof(session_list_entry));
  135. if(!newentry)
  136. return UA_STATUSCODE_BADOUTOFMEMORY;
  137. UA_atomic_addUInt32(&sm->currentSessionCount, 1);
  138. UA_Session_init(&newentry->session);
  139. newentry->session.sessionId = UA_NODEID_GUID(1, UA_Guid_random());
  140. newentry->session.header.authenticationToken = UA_NODEID_GUID(1, UA_Guid_random());
  141. if(request->requestedSessionTimeout <= sm->server->config.maxSessionTimeout &&
  142. request->requestedSessionTimeout > 0)
  143. newentry->session.timeout = request->requestedSessionTimeout;
  144. else
  145. newentry->session.timeout = sm->server->config.maxSessionTimeout;
  146. UA_Session_updateLifetime(&newentry->session);
  147. LIST_INSERT_HEAD(&sm->sessions, newentry, pointers);
  148. *session = &newentry->session;
  149. return UA_STATUSCODE_GOOD;
  150. }
  151. UA_StatusCode
  152. UA_SessionManager_removeSession(UA_SessionManager *sm, const UA_NodeId *token) {
  153. session_list_entry *current;
  154. LIST_FOREACH(current, &sm->sessions, pointers) {
  155. if(UA_NodeId_equal(&current->session.header.authenticationToken, token))
  156. break;
  157. }
  158. if(!current)
  159. return UA_STATUSCODE_BADSESSIONIDINVALID;
  160. removeSession(sm, current);
  161. return UA_STATUSCODE_GOOD;
  162. }