ua_session_manager.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "ua_session_manager.h"
  2. #include "ua_statuscodes.h"
  3. #include "ua_util.h"
  4. /**
  5. The functions in this file are not thread-safe. For multi-threaded access, a
  6. second implementation should be provided. See for example, how a nodestore
  7. implementation is choosen based on whether multithreading is enabled or not.
  8. */
  9. struct session_list_entry {
  10. UA_Session session;
  11. LIST_ENTRY(session_list_entry) pointers;
  12. };
  13. struct UA_SessionManager {
  14. LIST_HEAD(session_list, session_list_entry) sessions;
  15. UA_UInt32 maxSessionCount;
  16. UA_Int32 lastSessionId;
  17. UA_UInt32 currentSessionCount;
  18. UA_DateTime maxSessionLifeTime;
  19. UA_DateTime sessionTimeout;
  20. };
  21. UA_StatusCode UA_SessionManager_new(UA_SessionManager **sessionManager, UA_UInt32 maxSessionCount,
  22. UA_UInt32 sessionTimeout, UA_UInt32 startSessionId) {
  23. if(!(*sessionManager = UA_alloc(sizeof(UA_SessionManager))))
  24. return UA_STATUSCODE_BADOUTOFMEMORY;
  25. LIST_INIT(&(*sessionManager)->sessions);
  26. (*sessionManager)->maxSessionCount = maxSessionCount;
  27. (*sessionManager)->lastSessionId = startSessionId;
  28. (*sessionManager)->sessionTimeout = sessionTimeout;
  29. (*sessionManager)->currentSessionCount = 0;
  30. return UA_STATUSCODE_GOOD;
  31. }
  32. void UA_SessionManager_delete(UA_SessionManager *sessionManager) {
  33. struct session_list_entry *current = LIST_FIRST(&sessionManager->sessions);
  34. while(current) {
  35. LIST_REMOVE(current, pointers);
  36. if(current->session.channel)
  37. current->session.channel->session = UA_NULL; // the channel is no longer attached to a session
  38. UA_Session_deleteMembers(&current->session);
  39. UA_free(current);
  40. current = LIST_FIRST(&sessionManager->sessions);
  41. }
  42. UA_free(sessionManager);
  43. }
  44. UA_StatusCode UA_SessionManager_getSessionById(UA_SessionManager *sessionManager, UA_NodeId *sessionId, UA_Session **session) {
  45. if(sessionManager == UA_NULL) {
  46. *session = UA_NULL;
  47. return UA_STATUSCODE_BADINTERNALERROR;
  48. }
  49. struct session_list_entry *current = UA_NULL;
  50. LIST_FOREACH(current, &sessionManager->sessions, pointers) {
  51. if(UA_NodeId_equal(&current->session.sessionId, sessionId) == UA_EQUAL)
  52. break;
  53. }
  54. if(!current) {
  55. *session = UA_NULL;
  56. return UA_STATUSCODE_BADINTERNALERROR;
  57. }
  58. // Lifetime handling is not done here, but in a regular cleanup by the
  59. // server. If the session still exists, then it is valid.
  60. *session = &current->session;
  61. return UA_STATUSCODE_GOOD;
  62. }
  63. UA_StatusCode UA_SessionManager_getSessionByToken(UA_SessionManager *sessionManager, UA_NodeId *token, UA_Session **session) {
  64. if(sessionManager == UA_NULL) {
  65. *session = UA_NULL;
  66. return UA_STATUSCODE_BADINTERNALERROR;
  67. }
  68. struct session_list_entry *current = UA_NULL;
  69. LIST_FOREACH(current, &sessionManager->sessions, pointers) {
  70. if(UA_NodeId_equal(&current->session.authenticationToken, token) == UA_EQUAL)
  71. break;
  72. }
  73. if(!current) {
  74. *session = UA_NULL;
  75. return UA_STATUSCODE_BADINTERNALERROR;
  76. }
  77. // Lifetime handling is not done here, but in a regular cleanup by the
  78. // server. If the session still exists, then it is valid.
  79. *session = &current->session;
  80. return UA_STATUSCODE_GOOD;
  81. }
  82. /** Creates and adds a session. */
  83. UA_StatusCode UA_SessionManager_createSession(UA_SessionManager *sessionManager, UA_SecureChannel *channel,
  84. UA_Session **session) {
  85. if(sessionManager->currentSessionCount >= sessionManager->maxSessionCount)
  86. return UA_STATUSCODE_BADTOOMANYSESSIONS;
  87. struct session_list_entry *newentry = UA_alloc(sizeof(struct session_list_entry));
  88. if(!newentry)
  89. return UA_STATUSCODE_BADOUTOFMEMORY;
  90. UA_Session_init(&newentry->session);
  91. newentry->session.sessionId = (UA_NodeId) {.namespaceIndex = 1, .identifierType = UA_NODEIDTYPE_NUMERIC,
  92. .identifier.numeric = sessionManager->lastSessionId++ };
  93. newentry->session.authenticationToken = (UA_NodeId) {.namespaceIndex = 1,
  94. .identifierType = UA_NODEIDTYPE_NUMERIC,
  95. .identifier.numeric = sessionManager->lastSessionId };
  96. newentry->session.channel = channel;
  97. newentry->session.timeout = 3600 * 1000; // 1h
  98. UA_Session_setExpirationDate(&newentry->session);
  99. sessionManager->currentSessionCount++;
  100. LIST_INSERT_HEAD(&sessionManager->sessions, newentry, pointers);
  101. *session = &newentry->session;
  102. return UA_STATUSCODE_GOOD;
  103. }
  104. UA_StatusCode UA_SessionManager_removeSession(UA_SessionManager *sessionManager, UA_NodeId *sessionId) {
  105. struct session_list_entry *current = UA_NULL;
  106. LIST_FOREACH(current, &sessionManager->sessions, pointers) {
  107. if(UA_NodeId_equal(&current->session.sessionId, sessionId) == UA_EQUAL)
  108. break;
  109. }
  110. if(!current)
  111. return UA_STATUSCODE_BADINTERNALERROR;
  112. LIST_REMOVE(current, pointers);
  113. if(current->session.channel)
  114. current->session.channel->session = UA_NULL; // the channel is no longer attached to a session
  115. UA_Session_deleteMembers(&current->session);
  116. UA_free(current);
  117. return UA_STATUSCODE_GOOD;
  118. }