ua_session.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. */
  7. #ifndef UA_SESSION_H_
  8. #define UA_SESSION_H_
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. #include "../deps/queue.h"
  13. #include "ua_securechannel.h"
  14. #include "ua_util.h"
  15. #define UA_MAXCONTINUATIONPOINTS 5
  16. typedef struct ContinuationPointEntry {
  17. LIST_ENTRY(ContinuationPointEntry) pointers;
  18. UA_ByteString identifier;
  19. UA_BrowseDescription browseDescription;
  20. UA_UInt32 maxReferences;
  21. /* The last point in the node references? */
  22. size_t referenceKindIndex;
  23. size_t targetIndex;
  24. } ContinuationPointEntry;
  25. struct UA_Subscription;
  26. typedef struct UA_Subscription UA_Subscription;
  27. #ifdef UA_ENABLE_SUBSCRIPTIONS
  28. typedef struct UA_PublishResponseEntry {
  29. SIMPLEQ_ENTRY(UA_PublishResponseEntry) listEntry;
  30. UA_UInt32 requestId;
  31. UA_PublishResponse response;
  32. } UA_PublishResponseEntry;
  33. #endif
  34. typedef struct {
  35. UA_SessionHeader header;
  36. UA_ApplicationDescription clientDescription;
  37. UA_String sessionName;
  38. UA_Boolean activated;
  39. void *sessionHandle; // pointer assigned in userland-callback
  40. UA_NodeId sessionId;
  41. UA_UInt32 maxRequestMessageSize;
  42. UA_UInt32 maxResponseMessageSize;
  43. UA_Double timeout; // [ms]
  44. UA_DateTime validTill;
  45. UA_ByteString serverNonce;
  46. UA_UInt16 availableContinuationPoints;
  47. LIST_HEAD(ContinuationPointList, ContinuationPointEntry) continuationPoints;
  48. #ifdef UA_ENABLE_SUBSCRIPTIONS
  49. UA_UInt32 lastSubscriptionId;
  50. UA_UInt32 lastSeenSubscriptionId;
  51. LIST_HEAD(UA_ListOfUASubscriptions, UA_Subscription) serverSubscriptions;
  52. SIMPLEQ_HEAD(UA_ListOfQueuedPublishResponses, UA_PublishResponseEntry) responseQueue;
  53. UA_UInt32 numSubscriptions;
  54. UA_UInt32 numPublishReq;
  55. #endif
  56. } UA_Session;
  57. /* Local access to the services (for startup and maintenance) uses this Session
  58. * with all possible access rights (Session Id: 1) */
  59. extern UA_Session adminSession;
  60. /**
  61. * Session Lifecycle
  62. * ----------------- */
  63. void UA_Session_init(UA_Session *session);
  64. void UA_Session_deleteMembersCleanup(UA_Session *session, UA_Server *server);
  65. void UA_Session_attachToSecureChannel(UA_Session *session, UA_SecureChannel *channel);
  66. void UA_Session_detachFromSecureChannel(UA_Session *session);
  67. UA_StatusCode UA_Session_generateNonce(UA_Session *session);
  68. /* If any activity on a session happens, the timeout is extended */
  69. void UA_Session_updateLifetime(UA_Session *session);
  70. /**
  71. * Subscription handling
  72. * --------------------- */
  73. #ifdef UA_ENABLE_SUBSCRIPTIONS
  74. void UA_Session_addSubscription(UA_Session *session, UA_Subscription *newSubscription);
  75. UA_Subscription * UA_Session_getSubscriptionById(UA_Session *session, UA_UInt32 subscriptionId);
  76. UA_StatusCode UA_Session_deleteSubscription(UA_Server *server, UA_Session *session, UA_UInt32 subscriptionId);
  77. void UA_Session_queuePublishReq(UA_Session *session, UA_PublishResponseEntry* entry, UA_Boolean head);
  78. UA_PublishResponseEntry* UA_Session_dequeuePublishReq(UA_Session *session);
  79. #endif
  80. /**
  81. * Log Helper
  82. * ----------
  83. * We have to jump through some hoops to enable the use of format strings
  84. * without arguments since (pedantic) C99 does not allow variadic macros with
  85. * zero arguments. So we add a dummy argument that is not printed (%.0s is
  86. * string of length zero). */
  87. #define UA_LOG_TRACE_SESSION_INTERNAL(LOGGER, SESSION, MSG, ...) \
  88. UA_LOG_TRACE(LOGGER, UA_LOGCATEGORY_SESSION, \
  89. "Connection %i | SecureChannel %i | Session " UA_PRINTF_GUID_FORMAT " | " MSG "%.0s", \
  90. ((SESSION)->header.channel ? ((SESSION)->header.channel->connection ? (SESSION)->header.channel->connection->sockfd : 0) : 0), \
  91. ((SESSION)->header.channel ? (SESSION)->header.channel->securityToken.channelId : 0), \
  92. UA_PRINTF_GUID_DATA((SESSION)->sessionId.identifier.guid), __VA_ARGS__)
  93. #define UA_LOG_TRACE_SESSION(LOGGER, SESSION, ...) \
  94. UA_MACRO_EXPAND(UA_LOG_TRACE_SESSION_INTERNAL(LOGGER, SESSION, __VA_ARGS__, ""))
  95. #define UA_LOG_DEBUG_SESSION_INTERNAL(LOGGER, SESSION, MSG, ...) \
  96. UA_LOG_DEBUG(LOGGER, UA_LOGCATEGORY_SESSION, \
  97. "Connection %i | SecureChannel %i | Session " UA_PRINTF_GUID_FORMAT " | " MSG "%.0s", \
  98. ((SESSION)->header.channel ? ((SESSION)->header.channel->connection ? (SESSION)->header.channel->connection->sockfd : 0) : 0), \
  99. ((SESSION)->header.channel ? (SESSION)->header.channel->securityToken.channelId : 0), \
  100. UA_PRINTF_GUID_DATA((SESSION)->sessionId.identifier.guid), __VA_ARGS__)
  101. #define UA_LOG_DEBUG_SESSION(LOGGER, SESSION, ...) \
  102. UA_MACRO_EXPAND(UA_LOG_DEBUG_SESSION_INTERNAL(LOGGER, SESSION, __VA_ARGS__, ""))
  103. #define UA_LOG_INFO_SESSION_INTERNAL(LOGGER, SESSION, MSG, ...) \
  104. UA_LOG_INFO(LOGGER, UA_LOGCATEGORY_SESSION, \
  105. "Connection %i | SecureChannel %i | Session " UA_PRINTF_GUID_FORMAT " | " MSG "%.0s", \
  106. ((SESSION)->header.channel ? ((SESSION)->header.channel->connection ? (SESSION)->header.channel->connection->sockfd : 0) : 0), \
  107. ((SESSION)->header.channel ? (SESSION)->header.channel->securityToken.channelId : 0), \
  108. UA_PRINTF_GUID_DATA((SESSION)->sessionId.identifier.guid), __VA_ARGS__)
  109. #define UA_LOG_INFO_SESSION(LOGGER, SESSION, ...) \
  110. UA_MACRO_EXPAND(UA_LOG_INFO_SESSION_INTERNAL(LOGGER, SESSION, __VA_ARGS__, ""))
  111. #define UA_LOG_WARNING_SESSION_INTERNAL(LOGGER, SESSION, MSG, ...) \
  112. UA_LOG_WARNING(LOGGER, UA_LOGCATEGORY_SESSION, \
  113. "Connection %i | SecureChannel %i | Session " UA_PRINTF_GUID_FORMAT " | " MSG "%.0s", \
  114. ((SESSION)->header.channel ? ((SESSION)->header.channel->connection ? (SESSION)->header.channel->connection->sockfd : 0) : 0), \
  115. ((SESSION)->header.channel ? (SESSION)->header.channel->securityToken.channelId : 0), \
  116. UA_PRINTF_GUID_DATA((SESSION)->sessionId.identifier.guid), __VA_ARGS__)
  117. #define UA_LOG_WARNING_SESSION(LOGGER, SESSION, ...) \
  118. UA_MACRO_EXPAND(UA_LOG_WARNING_SESSION_INTERNAL(LOGGER, SESSION, __VA_ARGS__, ""))
  119. #define UA_LOG_ERROR_SESSION_INTERNAL(LOGGER, SESSION, MSG, ...) \
  120. UA_LOG_ERROR(LOGGER, UA_LOGCATEGORY_SESSION, \
  121. "Connection %i | SecureChannel %i | Session " UA_PRINTF_GUID_FORMAT " | " MSG "%.0s", \
  122. ((SESSION)->header.channel ? ((SESSION)->header.channel->connection ? (SESSION)->header.channel->connection->sockfd : 0) : 0), \
  123. ((SESSION)->header.channel ? (SESSION)->header.channel->securityToken.channelId : 0), \
  124. UA_PRINTF_GUID_DATA((SESSION)->sessionId.identifier.guid), __VA_ARGS__)
  125. #define UA_LOG_ERROR_SESSION(LOGGER, SESSION, ...) \
  126. UA_MACRO_EXPAND(UA_LOG_ERROR_SESSION_INTERNAL(LOGGER, SESSION, __VA_ARGS__, ""))
  127. #define UA_LOG_FATAL_SESSION_INTERNAL(LOGGER, SESSION, MSG, ...) \
  128. UA_LOG_FATAL(LOGGER, UA_LOGCATEGORY_SESSION, \
  129. "Connection %i | SecureChannel %i | Session " UA_PRINTF_GUID_FORMAT " | " MSG "%.0s", \
  130. ((SESSION)->header.channel ? ((SESSION)->header.channel->connection ? (SESSION)->header.channel->connection->sockfd : 0) : 0), \
  131. ((SESSION)->header.channel ? (SESSION)->header.channel->securityToken.channelId : 0), \
  132. UA_PRINTF_GUID_DATA((SESSION)->sessionId.identifier.guid), __VA_ARGS__)
  133. #define UA_LOG_FATAL_SESSION(LOGGER, SESSION, ...) \
  134. UA_MACRO_EXPAND(UA_LOG_FATAL_SESSION_INTERNAL(LOGGER, SESSION, __VA_ARGS__, ""))
  135. #ifdef __cplusplus
  136. } // extern "C"
  137. #endif
  138. #endif /* UA_SESSION_H_ */