ua_session.h 6.1 KB

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