ua_session_manager.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // todo
  34. return UA_SUCCESS;
  35. }
  36. UA_Int32 UA_SessionManager_getSessionById(UA_SessionManager *sessionManager, UA_NodeId *sessionId, UA_Session **session) {
  37. if(sessionManager == UA_NULL) {
  38. *session = UA_NULL;
  39. return UA_ERROR;
  40. }
  41. struct session_list_entry *current = UA_NULL;
  42. LIST_FOREACH(current, &sessionManager->sessions, pointers) {
  43. if(UA_NodeId_equal(&current->session.sessionId, sessionId) == UA_EQUAL)
  44. break;
  45. }
  46. if(!current) {
  47. *session = UA_NULL;
  48. return UA_ERROR;
  49. }
  50. // Lifetime handling is not done here, but in a regular cleanup by the
  51. // server. If the session still exists, then it is valid.
  52. *session = &current->session;
  53. return UA_SUCCESS;
  54. }
  55. UA_Int32 UA_SessionManager_getSessionByToken(UA_SessionManager *sessionManager, UA_NodeId *token, UA_Session **session) {
  56. if(sessionManager == UA_NULL) {
  57. *session = UA_NULL;
  58. return UA_ERROR;
  59. }
  60. struct session_list_entry *current = UA_NULL;
  61. LIST_FOREACH(current, &sessionManager->sessions, pointers) {
  62. if(UA_NodeId_equal(&current->session.authenticationToken, token) == UA_EQUAL)
  63. break;
  64. }
  65. if(!current) {
  66. *session = UA_NULL;
  67. return UA_ERROR;
  68. }
  69. // Lifetime handling is not done here, but in a regular cleanup by the
  70. // server. If the session still exists, then it is valid.
  71. *session = &current->session;
  72. return UA_SUCCESS;
  73. }
  74. /** Creates and adds a session. */
  75. UA_Int32 UA_SessionManager_createSession(UA_SessionManager *sessionManager, UA_SecureChannel *channel, UA_Session **session) {
  76. if(sessionManager->currentSessionCount >= sessionManager->maxSessionCount)
  77. return UA_ERROR;
  78. struct session_list_entry *newentry;
  79. if(UA_alloc((void **)&newentry, sizeof(struct session_list_entry)) != UA_SUCCESS)
  80. return UA_ERROR;
  81. UA_Session_init(&newentry->session);
  82. newentry->session.sessionId = (UA_NodeId) {.namespaceIndex = 1, .identifierType = UA_NODEIDTYPE_NUMERIC,
  83. .identifier.numeric = sessionManager->lastSessionId++ };
  84. newentry->session.authenticationToken = (UA_NodeId) {.namespaceIndex = 1, .identifierType = UA_NODEIDTYPE_NUMERIC,
  85. .identifier.numeric = sessionManager->lastSessionId };
  86. newentry->session.channel = channel;
  87. newentry->session.timeout = 3600 * 1000; // 1h
  88. UA_Session_setExpirationDate(&newentry->session);
  89. sessionManager->currentSessionCount++;
  90. LIST_INSERT_HEAD(&sessionManager->sessions, newentry, pointers);
  91. *session = &newentry->session;
  92. return UA_SUCCESS;
  93. }
  94. UA_Int32 UA_SessionManager_removeSession(UA_SessionManager *sessionManager, UA_NodeId *sessionId) {
  95. struct session_list_entry *current = UA_NULL;
  96. LIST_FOREACH(current, &sessionManager->sessions, pointers) {
  97. if(UA_NodeId_equal(&current->session.sessionId, sessionId) == UA_EQUAL)
  98. break;
  99. }
  100. if(!current)
  101. return UA_ERROR;
  102. LIST_REMOVE(current, pointers);
  103. UA_free(current);
  104. return UA_SUCCESS;
  105. }