ua_server_internal.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #ifndef UA_SERVER_INTERNAL_H_
  2. #define UA_SERVER_INTERNAL_H_
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  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. #ifdef UA_ENABLE_DISCOVERY
  72. typedef struct registeredServer_list_entry {
  73. LIST_ENTRY(registeredServer_list_entry) pointers;
  74. UA_RegisteredServer registeredServer;
  75. UA_DateTime lastSeen;
  76. } registeredServer_list_entry;
  77. #endif
  78. struct UA_Server {
  79. /* Meta */
  80. UA_DateTime startTime;
  81. size_t endpointDescriptionsSize;
  82. UA_EndpointDescription *endpointDescriptions;
  83. /* Security */
  84. UA_SecureChannelManager secureChannelManager;
  85. UA_SessionManager sessionManager;
  86. /* Address Space */
  87. UA_NodeStore *nodestore;
  88. #ifdef UA_ENABLE_DISCOVERY
  89. /* Discovery */
  90. LIST_HEAD(registeredServer_list, registeredServer_list_entry) registeredServers; // doubly-linked list of registered servers
  91. size_t registeredServersSize;
  92. #endif
  93. size_t namespacesSize;
  94. UA_String *namespaces;
  95. #ifdef UA_ENABLE_EXTERNAL_NAMESPACES
  96. size_t externalNamespacesSize;
  97. UA_ExternalNamespace *externalNamespaces;
  98. #endif
  99. /* Jobs with a repetition interval */
  100. LIST_HEAD(RepeatedJobsList, RepeatedJob) repeatedJobs;
  101. #ifndef UA_ENABLE_MULTITHREADING
  102. SLIST_HEAD(DelayedJobsList, UA_DelayedJob) delayedCallbacks;
  103. #else
  104. /* Dispatch queue head for the worker threads (the tail should not be in the same cache line) */
  105. struct cds_wfcq_head dispatchQueue_head;
  106. UA_Worker *workers; /* there are nThread workers in a running server */
  107. struct cds_lfs_stack mainLoopJobs; /* Work that shall be executed only in the main loop and not
  108. by worker threads */
  109. struct DelayedJobs *delayedJobs;
  110. pthread_cond_t dispatchQueue_condition; /* so the workers don't spin if the queue is empty */
  111. pthread_mutex_t dispatchQueue_mutex; /* mutex for access to condition variable */
  112. struct cds_wfcq_tail dispatchQueue_tail; /* Dispatch queue tail for the worker threads */
  113. #endif
  114. /* Config is the last element so that MSVC allows the usernamePasswordLogins
  115. field with zero-sized array */
  116. UA_ServerConfig config;
  117. };
  118. /*****************/
  119. /* Node Handling */
  120. /*****************/
  121. void UA_Node_deleteMembersAnyNodeClass(UA_Node *node);
  122. UA_StatusCode UA_Node_copyAnyNodeClass(const UA_Node *src, UA_Node *dst);
  123. typedef UA_StatusCode (*UA_EditNodeCallback)(UA_Server*, UA_Session*, UA_Node*, const void*);
  124. /* Calls callback on the node. In the multithreaded case, the node is copied before and replaced in
  125. the nodestore. */
  126. UA_StatusCode UA_Server_editNode(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId,
  127. UA_EditNodeCallback callback, const void *data);
  128. void UA_Server_processBinaryMessage(UA_Server *server, UA_Connection *connection,
  129. const UA_ByteString *message);
  130. UA_StatusCode UA_Server_delayedCallback(UA_Server *server, UA_ServerCallback callback, void *data);
  131. UA_StatusCode UA_Server_delayedFree(UA_Server *server, void *data);
  132. void UA_Server_deleteAllRepeatedJobs(UA_Server *server);
  133. /* Add an existing node. The node is assumed to be "finished", i.e. no
  134. * instantiation from inheritance is necessary. Instantiationcallback and
  135. * addedNodeId may be NULL. */
  136. UA_StatusCode
  137. Service_AddNodes_existing(UA_Server *server, UA_Session *session, UA_Node *node,
  138. const UA_NodeId *parentNodeId,
  139. const UA_NodeId *referenceTypeId,
  140. const UA_NodeId *typeDefinition,
  141. UA_InstantiationCallback *instantiationCallback,
  142. UA_NodeId *addedNodeId);
  143. /*********************/
  144. /* Utility Functions */
  145. /*********************/
  146. UA_StatusCode
  147. parse_numericrange(const UA_String *str, UA_NumericRange *range);
  148. UA_UInt16 addNamespace(UA_Server *server, const UA_String name);
  149. UA_Boolean
  150. UA_Node_hasSubTypeOrInstances(const UA_Node *node);
  151. const UA_VariableTypeNode *
  152. getVariableNodeType(UA_Server *server, const UA_VariableNode *node);
  153. const UA_ObjectTypeNode *
  154. getObjectNodeType(UA_Server *server, const UA_ObjectNode *node);
  155. /* Returns an array with all subtype nodeids (including the root). Subtypes need
  156. * to have the same nodeClass as root and are (recursively) related with a
  157. * hasSubType reference. Since multi-inheritance is possible, we test for
  158. * duplicates and return evey nodeid at most once. */
  159. UA_StatusCode
  160. getTypeHierarchy(UA_NodeStore *ns, const UA_Node *rootRef, UA_Boolean inverse,
  161. UA_NodeId **typeHierarchy, size_t *typeHierarchySize);
  162. /* Recursively searches "upwards" in the tree following specific reference types */
  163. UA_Boolean
  164. isNodeInTree(UA_NodeStore *ns, const UA_NodeId *leafNode,
  165. const UA_NodeId *nodeToFind, const UA_NodeId *referenceTypeIds,
  166. size_t referenceTypeIdsSize);
  167. const UA_Node *
  168. getNodeType(UA_Server *server, const UA_Node *node);
  169. /***************************************/
  170. /* Check Information Model Consistency */
  171. /***************************************/
  172. UA_StatusCode
  173. readValueAttribute(UA_Server *server, const UA_VariableNode *vn, UA_DataValue *v);
  174. UA_StatusCode
  175. typeCheckValue(UA_Server *server, const UA_NodeId *targetDataTypeId,
  176. UA_Int32 targetValueRank, size_t targetArrayDimensionsSize,
  177. const UA_UInt32 *targetArrayDimensions, const UA_Variant *value,
  178. const UA_NumericRange *range, UA_Variant *editableValue);
  179. UA_StatusCode
  180. writeDataTypeAttribute(UA_Server *server, UA_VariableNode *node,
  181. const UA_NodeId *dataType, const UA_NodeId *constraintDataType);
  182. UA_StatusCode
  183. compatibleArrayDimensions(size_t constraintArrayDimensionsSize,
  184. const UA_UInt32 *constraintArrayDimensions,
  185. size_t testArrayDimensionsSize,
  186. const UA_UInt32 *testArrayDimensions);
  187. UA_StatusCode
  188. writeValueRankAttribute(UA_Server *server, UA_VariableNode *node, UA_Int32 valueRank,
  189. UA_Int32 constraintValueRank);
  190. UA_StatusCode
  191. writeValueAttribute(UA_Server *server, UA_VariableNode *node,
  192. const UA_DataValue *value, const UA_String *indexRange);
  193. /*******************/
  194. /* Single-Services */
  195. /*******************/
  196. /* Some services take an array of "independent" requests. The single-services
  197. are stored here to keep ua_services.h clean for documentation purposes. */
  198. UA_StatusCode
  199. Service_AddReferences_single(UA_Server *server, UA_Session *session,
  200. const UA_AddReferencesItem *item);
  201. UA_StatusCode
  202. Service_DeleteNodes_single(UA_Server *server, UA_Session *session,
  203. const UA_NodeId *nodeId, UA_Boolean deleteReferences);
  204. UA_StatusCode
  205. Service_DeleteReferences_single(UA_Server *server, UA_Session *session,
  206. const UA_DeleteReferencesItem *item);
  207. void Service_Browse_single(UA_Server *server, UA_Session *session,
  208. struct ContinuationPointEntry *cp,
  209. const UA_BrowseDescription *descr,
  210. UA_UInt32 maxrefs, UA_BrowseResult *result);
  211. void
  212. Service_TranslateBrowsePathsToNodeIds_single(UA_Server *server, UA_Session *session,
  213. const UA_BrowsePath *path,
  214. UA_BrowsePathResult *result);
  215. void Service_Read_single(UA_Server *server, UA_Session *session,
  216. UA_TimestampsToReturn timestamps,
  217. const UA_ReadValueId *id, UA_DataValue *v);
  218. void Service_Call_single(UA_Server *server, UA_Session *session,
  219. const UA_CallMethodRequest *request,
  220. UA_CallMethodResult *result);
  221. /* Periodic task to clean up the discovery registry */
  222. void UA_Discovery_cleanupTimedOut(UA_Server *server, UA_DateTime nowMonotonic);
  223. #ifdef __cplusplus
  224. } // extern "C"
  225. #endif
  226. #endif /* UA_SERVER_INTERNAL_H_ */