ua_server_internal.h 9.9 KB

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