ua_session.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include "ua_session.h"
  5. #include "ua_types_generated_handling.h"
  6. #include "ua_util.h"
  7. #ifdef UA_ENABLE_SUBSCRIPTIONS
  8. #include "server/ua_subscription.h"
  9. #endif
  10. UA_Session adminSession = {
  11. {{0, NULL},{0, NULL},
  12. {{0, NULL},{0, NULL}},
  13. UA_APPLICATIONTYPE_CLIENT,
  14. {0, NULL},{0, NULL},
  15. 0, NULL}, /* .clientDescription */
  16. {sizeof("Administrator Session")-1, (UA_Byte*)"Administrator Session"}, /* .sessionName */
  17. false, /* .activated */
  18. NULL, /* .sessionHandle */
  19. {0,UA_NODEIDTYPE_NUMERIC,{1}}, /* .authenticationToken */
  20. {0,UA_NODEIDTYPE_NUMERIC,{1}}, /* .sessionId */
  21. UA_UINT32_MAX, /* .maxRequestMessageSize */
  22. UA_UINT32_MAX, /* .maxResponseMessageSize */
  23. (UA_Double)UA_INT64_MAX, /* .timeout */
  24. UA_INT64_MAX, /* .validTill */
  25. NULL, /* .channel */
  26. UA_MAXCONTINUATIONPOINTS, /* .availableContinuationPoints */
  27. {NULL}, /* .continuationPoints */
  28. #ifdef UA_ENABLE_SUBSCRIPTIONS
  29. 0, /* .lastSubscriptionID */
  30. {NULL}, /* .serverSubscriptions */
  31. {NULL, NULL}, /* .responseQueue */
  32. #endif
  33. };
  34. void UA_Session_init(UA_Session *session) {
  35. UA_ApplicationDescription_init(&session->clientDescription);
  36. session->activated = false;
  37. UA_NodeId_init(&session->authenticationToken);
  38. UA_NodeId_init(&session->sessionId);
  39. UA_String_init(&session->sessionName);
  40. session->maxRequestMessageSize = 0;
  41. session->maxResponseMessageSize = 0;
  42. session->timeout = 0;
  43. UA_DateTime_init(&session->validTill);
  44. session->channel = NULL;
  45. session->availableContinuationPoints = UA_MAXCONTINUATIONPOINTS;
  46. LIST_INIT(&session->continuationPoints);
  47. #ifdef UA_ENABLE_SUBSCRIPTIONS
  48. LIST_INIT(&session->serverSubscriptions);
  49. session->lastSubscriptionID = 0;
  50. SIMPLEQ_INIT(&session->responseQueue);
  51. #endif
  52. }
  53. void UA_Session_deleteMembersCleanup(UA_Session *session, UA_Server* server) {
  54. UA_ApplicationDescription_deleteMembers(&session->clientDescription);
  55. UA_NodeId_deleteMembers(&session->authenticationToken);
  56. UA_NodeId_deleteMembers(&session->sessionId);
  57. UA_String_deleteMembers(&session->sessionName);
  58. struct ContinuationPointEntry *cp, *temp;
  59. LIST_FOREACH_SAFE(cp, &session->continuationPoints, pointers, temp) {
  60. LIST_REMOVE(cp, pointers);
  61. UA_ByteString_deleteMembers(&cp->identifier);
  62. UA_BrowseDescription_deleteMembers(&cp->browseDescription);
  63. UA_free(cp);
  64. }
  65. if(session->channel)
  66. UA_SecureChannel_detachSession(session->channel, session);
  67. #ifdef UA_ENABLE_SUBSCRIPTIONS
  68. UA_Subscription *currents, *temps;
  69. LIST_FOREACH_SAFE(currents, &session->serverSubscriptions, listEntry, temps) {
  70. LIST_REMOVE(currents, listEntry);
  71. UA_Subscription_deleteMembers(currents, server);
  72. UA_free(currents);
  73. }
  74. UA_PublishResponseEntry *entry;
  75. while((entry = SIMPLEQ_FIRST(&session->responseQueue))) {
  76. SIMPLEQ_REMOVE_HEAD(&session->responseQueue, listEntry);
  77. UA_PublishResponse_deleteMembers(&entry->response);
  78. UA_free(entry);
  79. }
  80. #endif
  81. }
  82. void UA_Session_updateLifetime(UA_Session *session) {
  83. session->validTill = UA_DateTime_nowMonotonic() +
  84. (UA_DateTime)(session->timeout * UA_MSEC_TO_DATETIME);
  85. }
  86. #ifdef UA_ENABLE_SUBSCRIPTIONS
  87. void UA_Session_addSubscription(UA_Session *session, UA_Subscription *newSubscription) {
  88. LIST_INSERT_HEAD(&session->serverSubscriptions, newSubscription, listEntry);
  89. }
  90. UA_StatusCode
  91. UA_Session_deleteSubscription(UA_Server *server, UA_Session *session,
  92. UA_UInt32 subscriptionID) {
  93. UA_Subscription *sub = UA_Session_getSubscriptionByID(session, subscriptionID);
  94. if(!sub)
  95. return UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  96. LIST_REMOVE(sub, listEntry);
  97. UA_Subscription_deleteMembers(sub, server);
  98. UA_free(sub);
  99. return UA_STATUSCODE_GOOD;
  100. }
  101. UA_Subscription *
  102. UA_Session_getSubscriptionByID(UA_Session *session, UA_UInt32 subscriptionID) {
  103. UA_Subscription *sub;
  104. LIST_FOREACH(sub, &session->serverSubscriptions, listEntry) {
  105. if(sub->subscriptionID == subscriptionID)
  106. break;
  107. }
  108. return sub;
  109. }
  110. UA_UInt32 UA_Session_getUniqueSubscriptionID(UA_Session *session) {
  111. return ++(session->lastSubscriptionID);
  112. }
  113. #endif