ua_session.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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) Julius Pfrommer, Fraunhofer IOSB
  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. /* If any activity on a session happens, the timeout is extended */
  67. void UA_Session_updateLifetime(UA_Session *session);
  68. /**
  69. * Subscription handling
  70. * --------------------- */
  71. #ifdef UA_ENABLE_SUBSCRIPTIONS
  72. void UA_Session_addSubscription(UA_Session *session, UA_Subscription *newSubscription);
  73. UA_UInt32
  74. UA_Session_getNumSubscriptions(UA_Session *session );
  75. UA_Subscription *
  76. UA_Session_getSubscriptionById(UA_Session *session, UA_UInt32 subscriptionId);
  77. UA_StatusCode
  78. UA_Session_deleteSubscription(UA_Server *server, UA_Session *session,
  79. UA_UInt32 subscriptionId);
  80. UA_UInt32
  81. UA_Session_getUniqueSubscriptionId(UA_Session *session);
  82. UA_UInt32
  83. UA_Session_getNumPublishReq(UA_Session *session);
  84. UA_PublishResponseEntry*
  85. UA_Session_getPublishReq(UA_Session *session);
  86. void
  87. UA_Session_removePublishReq(UA_Session *session, UA_PublishResponseEntry* entry);
  88. void
  89. UA_Session_addPublishReq(UA_Session *session, UA_PublishResponseEntry* entry);
  90. #endif
  91. /**
  92. * Log Helper
  93. * ----------
  94. * We have to jump through some hoops to enable the use of format strings
  95. * without arguments since (pedantic) C99 does not allow variadic macros with
  96. * zero arguments. So we add a dummy argument that is not printed (%.0s is
  97. * string of length zero). */
  98. #define UA_LOG_TRACE_SESSION_INTERNAL(LOGGER, SESSION, MSG, ...) \
  99. UA_LOG_TRACE(LOGGER, UA_LOGCATEGORY_SESSION, \
  100. "Connection %i | SecureChannel %i | Session " UA_PRINTF_GUID_FORMAT " | " MSG "%.0s", \
  101. ((SESSION)->header.channel ? ((SESSION)->header.channel->connection ? (SESSION)->header.channel->connection->sockfd : 0) : 0), \
  102. ((SESSION)->header.channel ? (SESSION)->header.channel->securityToken.channelId : 0), \
  103. UA_PRINTF_GUID_DATA((SESSION)->sessionId.identifier.guid), __VA_ARGS__)
  104. #define UA_LOG_TRACE_SESSION(LOGGER, SESSION, ...) \
  105. UA_MACRO_EXPAND(UA_LOG_TRACE_SESSION_INTERNAL(LOGGER, SESSION, __VA_ARGS__, ""))
  106. #define UA_LOG_DEBUG_SESSION_INTERNAL(LOGGER, SESSION, MSG, ...) \
  107. UA_LOG_DEBUG(LOGGER, UA_LOGCATEGORY_SESSION, \
  108. "Connection %i | SecureChannel %i | Session " UA_PRINTF_GUID_FORMAT " | " MSG "%.0s", \
  109. ((SESSION)->header.channel ? ((SESSION)->header.channel->connection ? (SESSION)->header.channel->connection->sockfd : 0) : 0), \
  110. ((SESSION)->header.channel ? (SESSION)->header.channel->securityToken.channelId : 0), \
  111. UA_PRINTF_GUID_DATA((SESSION)->sessionId.identifier.guid), __VA_ARGS__)
  112. #define UA_LOG_DEBUG_SESSION(LOGGER, SESSION, ...) \
  113. UA_MACRO_EXPAND(UA_LOG_DEBUG_SESSION_INTERNAL(LOGGER, SESSION, __VA_ARGS__, ""))
  114. #define UA_LOG_INFO_SESSION_INTERNAL(LOGGER, SESSION, MSG, ...) \
  115. UA_LOG_INFO(LOGGER, UA_LOGCATEGORY_SESSION, \
  116. "Connection %i | SecureChannel %i | Session " UA_PRINTF_GUID_FORMAT " | " MSG "%.0s", \
  117. ((SESSION)->header.channel ? ((SESSION)->header.channel->connection ? (SESSION)->header.channel->connection->sockfd : 0) : 0), \
  118. ((SESSION)->header.channel ? (SESSION)->header.channel->securityToken.channelId : 0), \
  119. UA_PRINTF_GUID_DATA((SESSION)->sessionId.identifier.guid), __VA_ARGS__)
  120. #define UA_LOG_INFO_SESSION(LOGGER, SESSION, ...) \
  121. UA_MACRO_EXPAND(UA_LOG_INFO_SESSION_INTERNAL(LOGGER, SESSION, __VA_ARGS__, ""))
  122. #define UA_LOG_WARNING_SESSION_INTERNAL(LOGGER, SESSION, MSG, ...) \
  123. UA_LOG_WARNING(LOGGER, UA_LOGCATEGORY_SESSION, \
  124. "Connection %i | SecureChannel %i | Session " UA_PRINTF_GUID_FORMAT " | " MSG "%.0s", \
  125. ((SESSION)->header.channel ? ((SESSION)->header.channel->connection ? (SESSION)->header.channel->connection->sockfd : 0) : 0), \
  126. ((SESSION)->header.channel ? (SESSION)->header.channel->securityToken.channelId : 0), \
  127. UA_PRINTF_GUID_DATA((SESSION)->sessionId.identifier.guid), __VA_ARGS__)
  128. #define UA_LOG_WARNING_SESSION(LOGGER, SESSION, ...) \
  129. UA_MACRO_EXPAND(UA_LOG_WARNING_SESSION_INTERNAL(LOGGER, SESSION, __VA_ARGS__, ""))
  130. #define UA_LOG_ERROR_SESSION_INTERNAL(LOGGER, SESSION, MSG, ...) \
  131. UA_LOG_ERROR(LOGGER, UA_LOGCATEGORY_SESSION, \
  132. "Connection %i | SecureChannel %i | Session " UA_PRINTF_GUID_FORMAT " | " MSG "%.0s", \
  133. ((SESSION)->header.channel ? ((SESSION)->header.channel->connection ? (SESSION)->header.channel->connection->sockfd : 0) : 0), \
  134. ((SESSION)->header.channel ? (SESSION)->header.channel->securityToken.channelId : 0), \
  135. UA_PRINTF_GUID_DATA((SESSION)->sessionId.identifier.guid), __VA_ARGS__)
  136. #define UA_LOG_ERROR_SESSION(LOGGER, SESSION, ...) \
  137. UA_MACRO_EXPAND(UA_LOG_ERROR_SESSION_INTERNAL(LOGGER, SESSION, __VA_ARGS__, ""))
  138. #define UA_LOG_FATAL_SESSION_INTERNAL(LOGGER, SESSION, MSG, ...) \
  139. UA_LOG_FATAL(LOGGER, UA_LOGCATEGORY_SESSION, \
  140. "Connection %i | SecureChannel %i | Session " UA_PRINTF_GUID_FORMAT " | " MSG "%.0s", \
  141. ((SESSION)->header.channel ? ((SESSION)->header.channel->connection ? (SESSION)->header.channel->connection->sockfd : 0) : 0), \
  142. ((SESSION)->header.channel ? (SESSION)->header.channel->securityToken.channelId : 0), \
  143. UA_PRINTF_GUID_DATA((SESSION)->sessionId.identifier.guid), __VA_ARGS__)
  144. #define UA_LOG_FATAL_SESSION(LOGGER, SESSION, ...) \
  145. UA_MACRO_EXPAND(UA_LOG_FATAL_SESSION_INTERNAL(LOGGER, SESSION, __VA_ARGS__, ""))
  146. #ifdef __cplusplus
  147. } // extern "C"
  148. #endif
  149. #endif /* UA_SESSION_H_ */