ua_server_internal.h 9.0 KB

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