ua_session_manager.c 5.0 KB

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