ua_session.h 8.0 KB

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