ua_server_internal.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #ifndef UA_SERVER_INTERNAL_H_
  5. #define UA_SERVER_INTERNAL_H_
  6. #include "ua_util.h"
  7. #include "ua_server.h"
  8. #include "ua_server_external_ns.h"
  9. #include "ua_connection_internal.h"
  10. #include "ua_session_manager.h"
  11. #include "ua_securechannel_manager.h"
  12. #include "ua_nodestore.h"
  13. #define ANONYMOUS_POLICY "open62541-anonymous-policy"
  14. #define USERNAME_POLICY "open62541-username-policy"
  15. /* The general idea of RCU is to delay freeing nodes (or any callback invoked
  16. * with call_rcu) until all threads have left their critical section. Thus we
  17. * can delete nodes safely in concurrent operations. The macros UA_RCU_LOCK and
  18. * UA_RCU_UNLOCK are used to test during debugging that we do not nest read-side
  19. * critical sections (although this is generally allowed). */
  20. #ifdef UA_ENABLE_MULTITHREADING
  21. # define _LGPL_SOURCE
  22. # include <urcu.h>
  23. # include <urcu/lfstack.h>
  24. # ifdef NDEBUG
  25. # define UA_RCU_LOCK() rcu_read_lock()
  26. # define UA_RCU_UNLOCK() rcu_read_unlock()
  27. # define UA_ASSERT_RCU_LOCKED()
  28. # define UA_ASSERT_RCU_UNLOCKED()
  29. # else
  30. extern UA_THREAD_LOCAL bool rcu_locked;
  31. # define UA_ASSERT_RCU_LOCKED() assert(rcu_locked)
  32. # define UA_ASSERT_RCU_UNLOCKED() assert(!rcu_locked)
  33. # define UA_RCU_LOCK() do { \
  34. UA_ASSERT_RCU_UNLOCKED(); \
  35. rcu_locked = true; \
  36. rcu_read_lock(); } while(0)
  37. # define UA_RCU_UNLOCK() do { \
  38. UA_ASSERT_RCU_LOCKED(); \
  39. rcu_locked = false; \
  40. rcu_read_unlock(); } while(0)
  41. # endif
  42. #else
  43. # define UA_RCU_LOCK()
  44. # define UA_RCU_UNLOCK()
  45. # define UA_ASSERT_RCU_LOCKED()
  46. # define UA_ASSERT_RCU_UNLOCKED()
  47. #endif
  48. #ifdef UA_ENABLE_EXTERNAL_NAMESPACES
  49. /* Mapping of namespace-id and url to an external nodestore. For namespaces that
  50. * have no mapping defined, the internal nodestore is used by default. */
  51. typedef struct UA_ExternalNamespace {
  52. UA_UInt16 index;
  53. UA_String url;
  54. UA_ExternalNodeStore externalNodeStore;
  55. } UA_ExternalNamespace;
  56. #endif
  57. #ifdef UA_ENABLE_MULTITHREADING
  58. typedef struct {
  59. UA_Server *server;
  60. pthread_t thr;
  61. UA_UInt32 counter;
  62. volatile UA_Boolean running;
  63. char padding[64 - sizeof(void*) - sizeof(pthread_t) -
  64. sizeof(UA_UInt32) - sizeof(UA_Boolean)]; // separate cache lines
  65. } UA_Worker;
  66. #endif
  67. #if defined(UA_ENABLE_METHODCALLS) && defined(UA_ENABLE_SUBSCRIPTIONS)
  68. /* Internally used context to a session 'context' of the current mehtod call */
  69. extern UA_THREAD_LOCAL UA_Session* methodCallSession;
  70. #endif
  71. struct UA_Server {
  72. /* Meta */
  73. UA_DateTime startTime;
  74. size_t endpointDescriptionsSize;
  75. UA_EndpointDescription *endpointDescriptions;
  76. /* Security */
  77. UA_SecureChannelManager secureChannelManager;
  78. UA_SessionManager sessionManager;
  79. /* Address Space */
  80. UA_NodeStore *nodestore;
  81. size_t namespacesSize;
  82. UA_String *namespaces;
  83. #ifdef UA_ENABLE_EXTERNAL_NAMESPACES
  84. size_t externalNamespacesSize;
  85. UA_ExternalNamespace *externalNamespaces;
  86. #endif
  87. /* Jobs with a repetition interval */
  88. LIST_HEAD(RepeatedJobsList, RepeatedJob) repeatedJobs;
  89. #ifndef UA_ENABLE_MULTITHREADING
  90. SLIST_HEAD(DelayedJobsList, UA_DelayedJob) delayedCallbacks;
  91. #else
  92. /* Dispatch queue head for the worker threads (the tail should not be in the same cache line) */
  93. struct cds_wfcq_head dispatchQueue_head;
  94. UA_Worker *workers; /* there are nThread workers in a running server */
  95. struct cds_lfs_stack mainLoopJobs; /* Work that shall be executed only in the main loop and not
  96. by worker threads */
  97. struct DelayedJobs *delayedJobs;
  98. pthread_cond_t dispatchQueue_condition; /* so the workers don't spin if the queue is empty */
  99. pthread_mutex_t dispatchQueue_mutex; /* mutex for access to condition variable */
  100. struct cds_wfcq_tail dispatchQueue_tail; /* Dispatch queue tail for the worker threads */
  101. #endif
  102. /* Config is the last element so that MSVC allows the usernamePasswordLogins
  103. field with zero-sized array */
  104. UA_ServerConfig config;
  105. };
  106. /*****************/
  107. /* Node Handling */
  108. /*****************/
  109. void UA_Node_deleteMembersAnyNodeClass(UA_Node *node);
  110. UA_StatusCode UA_Node_copyAnyNodeClass(const UA_Node *src, UA_Node *dst);
  111. /* Calls callback on the node. In the multithreaded case, the node is copied before and replaced in
  112. the nodestore. */
  113. typedef UA_StatusCode (*UA_EditNodeCallback)(UA_Server*, UA_Session*, UA_Node*, const void*);
  114. UA_StatusCode UA_Server_editNode(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId,
  115. UA_EditNodeCallback callback, const void *data);
  116. /********************/
  117. /* Event Processing */
  118. /********************/
  119. void UA_Server_processBinaryMessage(UA_Server *server, UA_Connection *connection,
  120. const UA_ByteString *message);
  121. UA_StatusCode UA_Server_delayedCallback(UA_Server *server, UA_ServerCallback callback, void *data);
  122. UA_StatusCode UA_Server_delayedFree(UA_Server *server, void *data);
  123. void UA_Server_deleteAllRepeatedJobs(UA_Server *server);
  124. /* Add an existing node. The node is assumed to be "finished", i.e. no
  125. * instantiation from inheritance is necessary. Instantiationcallback and
  126. * addedNodeId may be NULL. */
  127. UA_StatusCode
  128. Service_AddNodes_existing(UA_Server *server, UA_Session *session, UA_Node *node,
  129. const UA_NodeId *parentNodeId,
  130. const UA_NodeId *referenceTypeId,
  131. const UA_NodeId *typeDefinition,
  132. UA_InstantiationCallback *instantiationCallback,
  133. UA_NodeId *addedNodeId);
  134. /*********************/
  135. /* Utility Functions */
  136. /*********************/
  137. UA_StatusCode
  138. parse_numericrange(const UA_String *str, UA_NumericRange *range);
  139. UA_UInt16 addNamespace(UA_Server *server, const UA_String name);
  140. UA_Boolean
  141. UA_Node_hasSubTypeOrInstances(const UA_Node *node);
  142. const UA_VariableTypeNode *
  143. getVariableNodeType(UA_Server *server, const UA_VariableNode *node);
  144. const UA_ObjectTypeNode *
  145. getObjectNodeType(UA_Server *server, const UA_ObjectNode *node);
  146. /* Returns an array with all subtype nodeids (including the root). Subtypes need
  147. * to have the same nodeClass as root and are (recursively) related with a
  148. * hasSubType reference. Since multi-inheritance is possible, we test for
  149. * duplicates and return evey nodeid at most once. */
  150. UA_StatusCode
  151. getTypeHierarchy(UA_NodeStore *ns, const UA_Node *rootRef, UA_Boolean inverse,
  152. UA_NodeId **typeHierarchy, size_t *typeHierarchySize);
  153. /* Recursively searches "upwards" in the tree following specific reference types */
  154. UA_Boolean
  155. isNodeInTree(UA_NodeStore *ns, const UA_NodeId *leafNode,
  156. const UA_NodeId *nodeToFind, const UA_NodeId *referenceTypeIds,
  157. size_t referenceTypeIdsSize);
  158. const UA_Node *
  159. getNodeType(UA_Server *server, const UA_Node *node);
  160. /***************************************/
  161. /* Check Information Model Consistency */
  162. /***************************************/
  163. UA_StatusCode
  164. readValueAttribute(UA_Server *server, const UA_VariableNode *vn, UA_DataValue *v);
  165. UA_StatusCode
  166. typeCheckValue(UA_Server *server, const UA_NodeId *targetDataTypeId,
  167. UA_Int32 targetValueRank, size_t targetArrayDimensionsSize,
  168. const UA_UInt32 *targetArrayDimensions, const UA_Variant *value,
  169. const UA_NumericRange *range, UA_Variant *editableValue);
  170. UA_StatusCode
  171. writeDataTypeAttribute(UA_Server *server, UA_VariableNode *node,
  172. const UA_NodeId *dataType, const UA_NodeId *constraintDataType);
  173. UA_StatusCode
  174. compatibleArrayDimensions(size_t constraintArrayDimensionsSize,
  175. const UA_UInt32 *constraintArrayDimensions,
  176. size_t testArrayDimensionsSize,
  177. const UA_UInt32 *testArrayDimensions);
  178. UA_StatusCode
  179. writeValueRankAttribute(UA_Server *server, UA_VariableNode *node, UA_Int32 valueRank,
  180. UA_Int32 constraintValueRank);
  181. UA_StatusCode
  182. writeValueAttribute(UA_Server *server, UA_VariableNode *node,
  183. const UA_DataValue *value, const UA_String *indexRange);
  184. /*******************/
  185. /* Single-Services */
  186. /*******************/
  187. /* Some services take an array of "independent" requests. The single-services
  188. are stored here to keep ua_services.h clean for documentation purposes. */
  189. void Service_Browse_single(UA_Server *server, UA_Session *session,
  190. struct ContinuationPointEntry *cp,
  191. const UA_BrowseDescription *descr,
  192. UA_UInt32 maxrefs, UA_BrowseResult *result);
  193. void Service_Read_single(UA_Server *server, UA_Session *session,
  194. UA_TimestampsToReturn timestamps,
  195. const UA_ReadValueId *id, UA_DataValue *v);
  196. void Service_Call_single(UA_Server *server, UA_Session *session,
  197. const UA_CallMethodRequest *request,
  198. UA_CallMethodResult *result);
  199. #endif /* UA_SERVER_INTERNAL_H_ */