ua_session_manager.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "ua_session_manager.h"
  2. #include "ua_server_internal.h"
  3. UA_StatusCode
  4. UA_SessionManager_init(UA_SessionManager *sm, UA_Server *server) {
  5. LIST_INIT(&sm->sessions);
  6. sm->currentSessionCount = 0;
  7. sm->server = server;
  8. return UA_STATUSCODE_GOOD;
  9. }
  10. void UA_SessionManager_deleteMembers(UA_SessionManager *sm) {
  11. session_list_entry *current, *temp;
  12. LIST_FOREACH_SAFE(current, &sm->sessions, pointers, temp) {
  13. LIST_REMOVE(current, pointers);
  14. UA_Session_deleteMembersCleanup(&current->session, sm->server);
  15. UA_free(current);
  16. }
  17. }
  18. static void
  19. removeSessionEntry(UA_SessionManager *sm, session_list_entry *sentry) {
  20. LIST_REMOVE(sentry, pointers);
  21. UA_atomic_add(&sm->currentSessionCount, (UA_UInt32)-1);
  22. UA_Session_deleteMembersCleanup(&sentry->session, sm->server);
  23. #ifndef UA_ENABLE_MULTITHREADING
  24. UA_free(sentry);
  25. #else
  26. UA_Server_delayedFree(sm->server, sentry);
  27. #endif
  28. }
  29. void UA_SessionManager_cleanupTimedOut(UA_SessionManager *sm, UA_DateTime nowMonotonic) {
  30. session_list_entry *sentry, *temp;
  31. LIST_FOREACH_SAFE(sentry, &sm->sessions, pointers, temp) {
  32. if(sentry->session.validTill < nowMonotonic) {
  33. UA_LOG_DEBUG(sm->server->config.logger, UA_LOGCATEGORY_SESSION,
  34. "Session with token %i has timed out and is removed",
  35. sentry->session.sessionId.identifier.numeric);
  36. removeSessionEntry(sm, sentry);
  37. }
  38. }
  39. }
  40. UA_Session *
  41. UA_SessionManager_getSession(UA_SessionManager *sm, const UA_NodeId *token) {
  42. session_list_entry *current = NULL;
  43. LIST_FOREACH(current, &sm->sessions, pointers) {
  44. if(UA_NodeId_equal(&current->session.authenticationToken, token)) {
  45. if(UA_DateTime_nowMonotonic() > current->session.validTill) {
  46. UA_LOG_DEBUG(sm->server->config.logger, UA_LOGCATEGORY_SESSION,
  47. "Try to use Session with token " UA_PRINTF_GUID_FORMAT ", but has timed out",
  48. UA_PRINTF_GUID_DATA(token->identifier.guid));
  49. return NULL;
  50. }
  51. return &current->session;
  52. }
  53. }
  54. UA_LOG_DEBUG(sm->server->config.logger, UA_LOGCATEGORY_SESSION,
  55. "Try to use Session with token " UA_PRINTF_GUID_FORMAT " but is not found",
  56. UA_PRINTF_GUID_DATA(token->identifier.guid));
  57. return NULL;
  58. }
  59. /* Creates and adds a session. But it is not yet attached to a secure channel. */
  60. UA_StatusCode
  61. UA_SessionManager_createSession(UA_SessionManager *sm, UA_SecureChannel *channel,
  62. const UA_CreateSessionRequest *request, UA_Session **session) {
  63. if(sm->currentSessionCount >= sm->server->config.maxSessions)
  64. return UA_STATUSCODE_BADTOOMANYSESSIONS;
  65. session_list_entry *newentry = (session_list_entry *)UA_malloc(sizeof(session_list_entry));
  66. if(!newentry)
  67. return UA_STATUSCODE_BADOUTOFMEMORY;
  68. UA_atomic_add(&sm->currentSessionCount, 1);
  69. UA_Session_init(&newentry->session);
  70. newentry->session.sessionId = UA_NODEID_GUID(1, UA_Guid_random());
  71. newentry->session.authenticationToken = UA_NODEID_GUID(1, UA_Guid_random());
  72. if(request->requestedSessionTimeout <= sm->server->config.maxSessionTimeout &&
  73. request->requestedSessionTimeout > 0)
  74. newentry->session.timeout = request->requestedSessionTimeout;
  75. else
  76. newentry->session.timeout = sm->server->config.maxSessionTimeout;
  77. UA_Session_updateLifetime(&newentry->session);
  78. LIST_INSERT_HEAD(&sm->sessions, newentry, pointers);
  79. *session = &newentry->session;
  80. return UA_STATUSCODE_GOOD;
  81. }
  82. UA_StatusCode
  83. UA_SessionManager_removeSession(UA_SessionManager *sm, const UA_NodeId *token) {
  84. session_list_entry *current;
  85. LIST_FOREACH(current, &sm->sessions, pointers) {
  86. if(UA_NodeId_equal(&current->session.authenticationToken, token))
  87. break;
  88. }
  89. if(!current)
  90. return UA_STATUSCODE_BADSESSIONIDINVALID;
  91. removeSessionEntry(sm, current);
  92. return UA_STATUSCODE_GOOD;
  93. }