ua_session_manager.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. typedef struct session_list_entry {
  10. UA_Session session;
  11. LIST_ENTRY(session_list_entry) pointers;
  12. } session_list_entry;
  13. UA_StatusCode UA_SessionManager_init(UA_SessionManager *sessionManager, UA_UInt32 maxSessionCount,
  14. UA_UInt32 maxSessionLifeTime, UA_UInt32 startSessionId) {
  15. LIST_INIT(&sessionManager->sessions);
  16. sessionManager->maxSessionCount = maxSessionCount;
  17. sessionManager->lastSessionId = startSessionId;
  18. sessionManager->maxSessionLifeTime = maxSessionLifeTime;
  19. sessionManager->currentSessionCount = 0;
  20. return UA_STATUSCODE_GOOD;
  21. }
  22. void UA_SessionManager_deleteMembers(UA_SessionManager *sessionManager) {
  23. session_list_entry *current, *next = LIST_FIRST(&sessionManager->sessions);
  24. while(next) {
  25. current = next;
  26. next = LIST_NEXT(current, pointers);
  27. LIST_REMOVE(current, pointers);
  28. if(current->session.channel)
  29. current->session.channel->session = UA_NULL; // the channel is no longer attached to a session
  30. UA_Session_deleteMembers(&current->session);
  31. UA_free(current);
  32. }
  33. }
  34. UA_StatusCode
  35. UA_SessionManager_getSessionById(UA_SessionManager *sessionManager, const UA_NodeId *sessionId,
  36. UA_Session **session)
  37. {
  38. if(sessionManager == UA_NULL) {
  39. *session = UA_NULL;
  40. return UA_STATUSCODE_BADINTERNALERROR;
  41. }
  42. session_list_entry *current = UA_NULL;
  43. LIST_FOREACH(current, &sessionManager->sessions, pointers) {
  44. if(UA_NodeId_equal(&current->session.sessionId, sessionId))
  45. break;
  46. }
  47. if(!current) {
  48. *session = UA_NULL;
  49. return UA_STATUSCODE_BADINTERNALERROR;
  50. }
  51. // Lifetime handling is not done here, but in a regular cleanup by the
  52. // server. If the session still exists, then it is valid.
  53. *session = &current->session;
  54. return UA_STATUSCODE_GOOD;
  55. }
  56. UA_StatusCode
  57. UA_SessionManager_getSessionByToken(UA_SessionManager *sessionManager, const UA_NodeId *token,
  58. UA_Session **session)
  59. {
  60. if(sessionManager == UA_NULL) {
  61. *session = UA_NULL;
  62. return UA_STATUSCODE_BADINTERNALERROR;
  63. }
  64. session_list_entry *current = UA_NULL;
  65. LIST_FOREACH(current, &sessionManager->sessions, pointers) {
  66. if(UA_NodeId_equal(&current->session.authenticationToken, token))
  67. break;
  68. }
  69. if(!current) {
  70. *session = UA_NULL;
  71. return UA_STATUSCODE_BADINTERNALERROR;
  72. }
  73. // Lifetime handling is not done here, but in a regular cleanup by the
  74. // server. If the session still exists, then it is valid.
  75. *session = &current->session;
  76. return UA_STATUSCODE_GOOD;
  77. }
  78. /** Creates and adds a session. */
  79. UA_StatusCode
  80. UA_SessionManager_createSession(UA_SessionManager *sessionManager, UA_SecureChannel *channel,
  81. const UA_CreateSessionRequest *request, UA_Session **session)
  82. {
  83. if(sessionManager->currentSessionCount >= sessionManager->maxSessionCount)
  84. return UA_STATUSCODE_BADTOOMANYSESSIONS;
  85. session_list_entry *newentry = UA_malloc(sizeof(session_list_entry));
  86. if(!newentry)
  87. return UA_STATUSCODE_BADOUTOFMEMORY;
  88. UA_Session_init(&newentry->session);
  89. newentry->session.sessionId = UA_NODEID_NUMERIC(1, sessionManager->lastSessionId++);
  90. newentry->session.authenticationToken = UA_NODEID_NUMERIC(1, sessionManager->lastSessionId);
  91. newentry->session.channel = channel;
  92. newentry->session.timeout =
  93. (request->requestedSessionTimeout <= sessionManager->maxSessionLifeTime &&
  94. request->requestedSessionTimeout>0) ?
  95. request->requestedSessionTimeout : sessionManager->maxSessionLifeTime;
  96. UA_Session_setExpirationDate(&newentry->session);
  97. sessionManager->currentSessionCount++;
  98. LIST_INSERT_HEAD(&sessionManager->sessions, newentry, pointers);
  99. *session = &newentry->session;
  100. return UA_STATUSCODE_GOOD;
  101. }
  102. UA_StatusCode
  103. UA_SessionManager_removeSession(UA_SessionManager *sessionManager, const UA_NodeId *sessionId)
  104. {
  105. session_list_entry *current = UA_NULL;
  106. LIST_FOREACH(current, &sessionManager->sessions, pointers) {
  107. if(UA_NodeId_equal(&current->session.sessionId, sessionId))
  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. }