ua_session.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_subscription.h"
  11. #include "ua_server_internal.h"
  12. #endif
  13. #define UA_SESSION_NONCELENTH 32
  14. UA_Session adminSession = {
  15. {{NULL, NULL}, /* .pointers */
  16. {0,UA_NODEIDTYPE_NUMERIC,{1}}, /* .authenticationToken */
  17. NULL,}, /* .channel */
  18. {{0, NULL},{0, NULL},
  19. {{0, NULL},{0, NULL}},
  20. UA_APPLICATIONTYPE_CLIENT,
  21. {0, NULL},{0, NULL},
  22. 0, NULL}, /* .clientDescription */
  23. {sizeof("Administrator Session")-1, (UA_Byte*)"Administrator Session"}, /* .sessionName */
  24. false, /* .activated */
  25. NULL, /* .sessionHandle */
  26. {0,UA_NODEIDTYPE_NUMERIC,{1}}, /* .sessionId */
  27. UA_UINT32_MAX, /* .maxRequestMessageSize */
  28. UA_UINT32_MAX, /* .maxResponseMessageSize */
  29. (UA_Double)UA_INT64_MAX, /* .timeout */
  30. UA_INT64_MAX, /* .validTill */
  31. {0, NULL},
  32. UA_MAXCONTINUATIONPOINTS, /* .availableContinuationPoints */
  33. {NULL}, /* .continuationPoints */
  34. #ifdef UA_ENABLE_SUBSCRIPTIONS
  35. 0, /* .lastSubscriptionId */
  36. 0, /* .lastSeenSubscriptionId */
  37. {NULL}, /* .serverSubscriptions */
  38. {NULL, NULL}, /* .responseQueue */
  39. 0, /* numSubscriptions */
  40. 0 /* numPublishReq */
  41. #endif
  42. };
  43. void UA_Session_init(UA_Session *session) {
  44. memset(session, 0, sizeof(UA_Session));
  45. session->availableContinuationPoints = UA_MAXCONTINUATIONPOINTS;
  46. #ifdef UA_ENABLE_SUBSCRIPTIONS
  47. SIMPLEQ_INIT(&session->responseQueue);
  48. #endif
  49. }
  50. void UA_Session_deleteMembersCleanup(UA_Session *session, UA_Server* server) {
  51. UA_Session_detachFromSecureChannel(session);
  52. UA_ApplicationDescription_deleteMembers(&session->clientDescription);
  53. UA_NodeId_deleteMembers(&session->header.authenticationToken);
  54. UA_NodeId_deleteMembers(&session->sessionId);
  55. UA_String_deleteMembers(&session->sessionName);
  56. UA_ByteString_deleteMembers(&session->serverNonce);
  57. struct ContinuationPointEntry *cp, *temp;
  58. LIST_FOREACH_SAFE(cp, &session->continuationPoints, pointers, temp) {
  59. LIST_REMOVE(cp, pointers);
  60. UA_ByteString_deleteMembers(&cp->identifier);
  61. UA_BrowseDescription_deleteMembers(&cp->browseDescription);
  62. UA_free(cp);
  63. }
  64. #ifdef UA_ENABLE_SUBSCRIPTIONS
  65. UA_Subscription *sub, *tempsub;
  66. LIST_FOREACH_SAFE(sub, &session->serverSubscriptions, listEntry, tempsub) {
  67. UA_Session_deleteSubscription(server, session, sub->subscriptionId);
  68. }
  69. UA_PublishResponseEntry *entry;
  70. while((entry = UA_Session_dequeuePublishReq(session))) {
  71. UA_PublishResponse_deleteMembers(&entry->response);
  72. UA_free(entry);
  73. }
  74. #endif
  75. }
  76. void UA_Session_attachToSecureChannel(UA_Session *session, UA_SecureChannel *channel) {
  77. LIST_INSERT_HEAD(&channel->sessions, &session->header, pointers);
  78. session->header.channel = channel;
  79. }
  80. void UA_Session_detachFromSecureChannel(UA_Session *session) {
  81. if(!session->header.channel)
  82. return;
  83. session->header.channel = NULL;
  84. LIST_REMOVE(&session->header, pointers);
  85. }
  86. UA_StatusCode
  87. UA_Session_generateNonce(UA_Session *session) {
  88. UA_SecureChannel *channel = session->header.channel;
  89. if(!channel || !channel->securityPolicy)
  90. return UA_STATUSCODE_BADINTERNALERROR;
  91. /* Is the length of the previous nonce correct? */
  92. if(session->serverNonce.length != UA_SESSION_NONCELENTH) {
  93. UA_ByteString_deleteMembers(&session->serverNonce);
  94. UA_StatusCode retval =
  95. UA_ByteString_allocBuffer(&session->serverNonce, UA_SESSION_NONCELENTH);
  96. if(retval != UA_STATUSCODE_GOOD)
  97. return retval;
  98. }
  99. return channel->securityPolicy->symmetricModule.
  100. generateNonce(channel->securityPolicy, &session->serverNonce);
  101. }
  102. void UA_Session_updateLifetime(UA_Session *session) {
  103. session->validTill = UA_DateTime_nowMonotonic() +
  104. (UA_DateTime)(session->timeout * UA_DATETIME_MSEC);
  105. }
  106. #ifdef UA_ENABLE_SUBSCRIPTIONS
  107. void UA_Session_addSubscription(UA_Session *session, UA_Subscription *newSubscription) {
  108. newSubscription->subscriptionId = ++session->lastSubscriptionId;
  109. LIST_INSERT_HEAD(&session->serverSubscriptions, newSubscription, listEntry);
  110. session->numSubscriptions++;
  111. }
  112. UA_StatusCode
  113. UA_Session_deleteSubscription(UA_Server *server, UA_Session *session,
  114. UA_UInt32 subscriptionId) {
  115. UA_Subscription *sub = UA_Session_getSubscriptionById(session, subscriptionId);
  116. if(!sub)
  117. return UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  118. UA_Subscription_deleteMembers(server, sub);
  119. /* Add a delayed callback to remove the subscription when the currently
  120. * scheduled jobs have completed */
  121. UA_StatusCode retval = UA_Server_delayedFree(server, sub);
  122. if(retval != UA_STATUSCODE_GOOD) {
  123. UA_LOG_WARNING_SESSION(server->config.logger, session,
  124. "Could not remove subscription with error code %s",
  125. UA_StatusCode_name(retval));
  126. return retval; /* Try again next time */
  127. }
  128. /* Remove from the session */
  129. LIST_REMOVE(sub, listEntry);
  130. UA_assert(session->numSubscriptions > 0);
  131. session->numSubscriptions--;
  132. return UA_STATUSCODE_GOOD;
  133. }
  134. UA_Subscription *
  135. UA_Session_getSubscriptionById(UA_Session *session, UA_UInt32 subscriptionId) {
  136. UA_Subscription *sub;
  137. LIST_FOREACH(sub, &session->serverSubscriptions, listEntry) {
  138. if(sub->subscriptionId == subscriptionId)
  139. break;
  140. }
  141. return sub;
  142. }
  143. UA_PublishResponseEntry*
  144. UA_Session_dequeuePublishReq(UA_Session *session) {
  145. UA_PublishResponseEntry* entry = SIMPLEQ_FIRST(&session->responseQueue);
  146. if(entry) {
  147. SIMPLEQ_REMOVE_HEAD(&session->responseQueue, listEntry);
  148. session->numPublishReq--;
  149. }
  150. return entry;
  151. }
  152. void
  153. UA_Session_queuePublishReq(UA_Session *session, UA_PublishResponseEntry* entry, UA_Boolean head) {
  154. if(!head)
  155. SIMPLEQ_INSERT_TAIL(&session->responseQueue, entry, listEntry);
  156. else
  157. SIMPLEQ_INSERT_HEAD(&session->responseQueue, entry, listEntry);
  158. session->numPublishReq++;
  159. }
  160. #endif