ua_session.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2018 (c) Thomas Stalder, Blue Time Concept SA
  7. */
  8. #include "ua_session.h"
  9. #ifdef UA_ENABLE_SUBSCRIPTIONS
  10. #include "ua_server_internal.h"
  11. #include "ua_subscription.h"
  12. #endif
  13. #define UA_SESSION_NONCELENTH 32
  14. void UA_Session_init(UA_Session *session) {
  15. memset(session, 0, sizeof(UA_Session));
  16. session->availableContinuationPoints = UA_MAXCONTINUATIONPOINTS;
  17. #ifdef UA_ENABLE_SUBSCRIPTIONS
  18. SIMPLEQ_INIT(&session->responseQueue);
  19. #endif
  20. }
  21. void UA_Session_deleteMembersCleanup(UA_Session *session, UA_Server* server) {
  22. UA_Session_detachFromSecureChannel(session);
  23. UA_ApplicationDescription_deleteMembers(&session->clientDescription);
  24. UA_NodeId_deleteMembers(&session->header.authenticationToken);
  25. UA_NodeId_deleteMembers(&session->sessionId);
  26. UA_String_deleteMembers(&session->sessionName);
  27. UA_ByteString_deleteMembers(&session->serverNonce);
  28. struct ContinuationPoint *cp, *next = session->continuationPoints;
  29. while((cp = next)) {
  30. next = ContinuationPoint_clear(cp);
  31. UA_free(cp);
  32. }
  33. session->continuationPoints = NULL;
  34. session->availableContinuationPoints = UA_MAXCONTINUATIONPOINTS;
  35. }
  36. void UA_Session_attachToSecureChannel(UA_Session *session, UA_SecureChannel *channel) {
  37. LIST_INSERT_HEAD(&channel->sessions, &session->header, pointers);
  38. session->header.channel = channel;
  39. }
  40. void UA_Session_detachFromSecureChannel(UA_Session *session) {
  41. if(!session->header.channel)
  42. return;
  43. session->header.channel = NULL;
  44. LIST_REMOVE(&session->header, pointers);
  45. }
  46. UA_StatusCode
  47. UA_Session_generateNonce(UA_Session *session) {
  48. UA_SecureChannel *channel = session->header.channel;
  49. if(!channel || !channel->securityPolicy)
  50. return UA_STATUSCODE_BADINTERNALERROR;
  51. /* Is the length of the previous nonce correct? */
  52. if(session->serverNonce.length != UA_SESSION_NONCELENTH) {
  53. UA_ByteString_deleteMembers(&session->serverNonce);
  54. UA_StatusCode retval =
  55. UA_ByteString_allocBuffer(&session->serverNonce, UA_SESSION_NONCELENTH);
  56. if(retval != UA_STATUSCODE_GOOD)
  57. return retval;
  58. }
  59. return channel->securityPolicy->symmetricModule.
  60. generateNonce(channel->securityPolicy, &session->serverNonce);
  61. }
  62. void UA_Session_updateLifetime(UA_Session *session) {
  63. session->validTill = UA_DateTime_nowMonotonic() +
  64. (UA_DateTime)(session->timeout * UA_DATETIME_MSEC);
  65. }
  66. #ifdef UA_ENABLE_SUBSCRIPTIONS
  67. void UA_Session_addSubscription(UA_Server *server, UA_Session *session, UA_Subscription *newSubscription) {
  68. newSubscription->subscriptionId = ++session->lastSubscriptionId;
  69. LIST_INSERT_HEAD(&session->serverSubscriptions, newSubscription, listEntry);
  70. session->numSubscriptions++;
  71. server->numSubscriptions++;
  72. }
  73. UA_StatusCode
  74. UA_Session_deleteSubscription(UA_Server *server, UA_Session *session,
  75. UA_UInt32 subscriptionId) {
  76. UA_Subscription *sub = UA_Session_getSubscriptionById(session, subscriptionId);
  77. if(!sub)
  78. return UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  79. UA_Subscription_deleteMembers(server, sub);
  80. /* Add a delayed callback to remove the subscription when the currently
  81. * scheduled jobs have completed. There is no actual delayed callback. Just
  82. * free the structure. */
  83. sub->delayedFreePointers.callback = NULL;
  84. UA_WorkQueue_enqueueDelayed(&server->workQueue, &sub->delayedFreePointers);
  85. /* Remove from the session */
  86. LIST_REMOVE(sub, listEntry);
  87. UA_assert(session->numSubscriptions > 0);
  88. UA_assert(server->numSubscriptions > 0);
  89. session->numSubscriptions--;
  90. server->numSubscriptions--;
  91. return UA_STATUSCODE_GOOD;
  92. }
  93. UA_Subscription *
  94. UA_Session_getSubscriptionById(UA_Session *session, UA_UInt32 subscriptionId) {
  95. UA_Subscription *sub;
  96. LIST_FOREACH(sub, &session->serverSubscriptions, listEntry) {
  97. if(sub->subscriptionId == subscriptionId)
  98. break;
  99. }
  100. return sub;
  101. }
  102. UA_PublishResponseEntry*
  103. UA_Session_dequeuePublishReq(UA_Session *session) {
  104. UA_PublishResponseEntry* entry = SIMPLEQ_FIRST(&session->responseQueue);
  105. if(entry) {
  106. SIMPLEQ_REMOVE_HEAD(&session->responseQueue, listEntry);
  107. session->numPublishReq--;
  108. }
  109. return entry;
  110. }
  111. void
  112. UA_Session_queuePublishReq(UA_Session *session, UA_PublishResponseEntry* entry, UA_Boolean head) {
  113. if(!head)
  114. SIMPLEQ_INSERT_TAIL(&session->responseQueue, entry, listEntry);
  115. else
  116. SIMPLEQ_INSERT_HEAD(&session->responseQueue, entry, listEntry);
  117. session->numPublishReq++;
  118. }
  119. #endif