ua_server_internal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include "ua_util.h"
  10. #include "ua_server.h"
  11. #include "ua_server_config.h"
  12. #include "ua_timer.h"
  13. #include "ua_connection_internal.h"
  14. #include "ua_session_manager.h"
  15. #include "ua_securechannel_manager.h"
  16. #ifdef UA_ENABLE_MULTITHREADING
  17. /* TODO: Don't depend on liburcu */
  18. #include <urcu.h>
  19. #include <urcu/lfstack.h>
  20. struct UA_Worker;
  21. typedef struct UA_Worker UA_Worker;
  22. #endif /* UA_ENABLE_MULTITHREADING */
  23. #ifdef UA_ENABLE_DISCOVERY
  24. typedef struct registeredServer_list_entry {
  25. LIST_ENTRY(registeredServer_list_entry) pointers;
  26. UA_RegisteredServer registeredServer;
  27. UA_DateTime lastSeen;
  28. } registeredServer_list_entry;
  29. typedef struct periodicServerRegisterCallback_entry {
  30. LIST_ENTRY(periodicServerRegisterCallback_entry) pointers;
  31. struct PeriodicServerRegisterCallback *callback;
  32. } periodicServerRegisterCallback_entry;
  33. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  34. #include "mdnsd/libmdnsd/mdnsd.h"
  35. typedef struct serverOnNetwork_list_entry {
  36. LIST_ENTRY(serverOnNetwork_list_entry) pointers;
  37. UA_ServerOnNetwork serverOnNetwork;
  38. UA_DateTime created;
  39. UA_DateTime lastSeen;
  40. UA_Boolean txtSet;
  41. UA_Boolean srvSet;
  42. char* pathTmp;
  43. } serverOnNetwork_list_entry;
  44. #define SERVER_ON_NETWORK_HASH_PRIME 1009
  45. typedef struct serverOnNetwork_hash_entry {
  46. serverOnNetwork_list_entry* entry;
  47. struct serverOnNetwork_hash_entry* next;
  48. } serverOnNetwork_hash_entry;
  49. #endif /* UA_ENABLE_DISCOVERY_MULTICAST */
  50. #endif /* UA_ENABLE_DISCOVERY */
  51. struct UA_Server {
  52. /* Meta */
  53. UA_DateTime startTime;
  54. /* Security */
  55. UA_SecureChannelManager secureChannelManager;
  56. UA_SessionManager sessionManager;
  57. #ifdef UA_ENABLE_DISCOVERY
  58. /* Discovery */
  59. LIST_HEAD(registeredServer_list, registeredServer_list_entry) registeredServers; // doubly-linked list of registered servers
  60. size_t registeredServersSize;
  61. LIST_HEAD(periodicServerRegisterCallback_list, periodicServerRegisterCallback_entry) periodicServerRegisterCallbacks; // doubly-linked list of current register callbacks
  62. UA_Server_registerServerCallback registerServerCallback;
  63. void* registerServerCallbackData;
  64. # ifdef UA_ENABLE_DISCOVERY_MULTICAST
  65. mdns_daemon_t *mdnsDaemon;
  66. int mdnsSocket;
  67. UA_Boolean mdnsMainSrvAdded;
  68. # ifdef UA_ENABLE_MULTITHREADING
  69. pthread_t mdnsThread;
  70. UA_Boolean mdnsRunning;
  71. # endif
  72. LIST_HEAD(serverOnNetwork_list, serverOnNetwork_list_entry) serverOnNetwork; // doubly-linked list of servers on the network (from mDNS)
  73. size_t serverOnNetworkSize;
  74. UA_UInt32 serverOnNetworkRecordIdCounter;
  75. UA_DateTime serverOnNetworkRecordIdLastReset;
  76. // hash mapping domain name to serverOnNetwork list entry
  77. struct serverOnNetwork_hash_entry* serverOnNetworkHash[SERVER_ON_NETWORK_HASH_PRIME];
  78. UA_Server_serverOnNetworkCallback serverOnNetworkCallback;
  79. void* serverOnNetworkCallbackData;
  80. # endif
  81. #endif
  82. /* Namespaces */
  83. size_t namespacesSize;
  84. UA_String *namespaces;
  85. /* Callbacks with a repetition interval */
  86. UA_Timer timer;
  87. /* Delayed callbacks */
  88. SLIST_HEAD(DelayedCallbacksList, UA_DelayedCallback) delayedCallbacks;
  89. /* Worker threads */
  90. #ifdef UA_ENABLE_MULTITHREADING
  91. /* Dispatch queue head for the worker threads (the tail should not be in the same cache line) */
  92. struct cds_wfcq_head dispatchQueue_head;
  93. UA_Worker *workers; /* there are nThread workers in a running server */
  94. pthread_cond_t dispatchQueue_condition; /* so the workers don't spin if the queue is empty */
  95. pthread_mutex_t dispatchQueue_mutex; /* mutex for access to condition variable */
  96. struct cds_wfcq_tail dispatchQueue_tail; /* Dispatch queue tail for the worker threads */
  97. #endif
  98. /* For bootstrapping, omit some consistency checks, creating a reference to
  99. * the parent and member instantiation */
  100. UA_Boolean bootstrapNS0;
  101. /* Config */
  102. UA_ServerConfig config;
  103. };
  104. /*****************/
  105. /* Node Handling */
  106. /*****************/
  107. #define UA_Nodestore_get(SERVER, NODEID) \
  108. (SERVER)->config.nodestore.getNode((SERVER)->config.nodestore.context, NODEID)
  109. #define UA_Nodestore_release(SERVER, NODEID) \
  110. (SERVER)->config.nodestore.releaseNode((SERVER)->config.nodestore.context, NODEID)
  111. #define UA_Nodestore_new(SERVER, NODECLASS) \
  112. (SERVER)->config.nodestore.newNode((SERVER)->config.nodestore.context, NODECLASS)
  113. #define UA_Nodestore_getCopy(SERVER, NODEID, OUTNODE) \
  114. (SERVER)->config.nodestore.getNodeCopy((SERVER)->config.nodestore.context, NODEID, OUTNODE)
  115. #define UA_Nodestore_insert(SERVER, NODE, OUTNODEID) \
  116. (SERVER)->config.nodestore.insertNode((SERVER)->config.nodestore.context, NODE, OUTNODEID)
  117. #define UA_Nodestore_delete(SERVER, NODE) \
  118. (SERVER)->config.nodestore.deleteNode((SERVER)->config.nodestore.context, NODE)
  119. #define UA_Nodestore_remove(SERVER, NODEID) \
  120. (SERVER)->config.nodestore.removeNode((SERVER)->config.nodestore.context, NODEID)
  121. /* Calls the callback with the node retrieved from the nodestore on top of the
  122. * stack. Either a copy or the original node for in-situ editing. Depends on
  123. * multithreading and the nodestore.*/
  124. typedef UA_StatusCode (*UA_EditNodeCallback)(UA_Server*, UA_Session*,
  125. UA_Node *node, const void*);
  126. UA_StatusCode UA_Server_editNode(UA_Server *server, UA_Session *session,
  127. const UA_NodeId *nodeId,
  128. UA_EditNodeCallback callback,
  129. const void *data);
  130. /*************/
  131. /* Callbacks */
  132. /*************/
  133. /* Delayed callbacks are executed when all previously dispatched callbacks are
  134. * finished */
  135. UA_StatusCode
  136. UA_Server_delayedCallback(UA_Server *server, UA_ServerCallback callback, void *data);
  137. /* Callback is executed in the same thread or, if possible, dispatched to one of
  138. * the worker threads. */
  139. void
  140. UA_Server_workerCallback(UA_Server *server, UA_ServerCallback callback, void *data);
  141. /*********************/
  142. /* Utility Functions */
  143. /*********************/
  144. /* A few global NodeId definitions */
  145. extern const UA_NodeId subtypeId;
  146. UA_StatusCode
  147. UA_NumericRange_parseFromString(UA_NumericRange *range, const UA_String *str);
  148. UA_UInt16 addNamespace(UA_Server *server, const UA_String name);
  149. UA_Boolean
  150. UA_Node_hasSubTypeOrInstances(const UA_Node *node);
  151. /* Recursively searches "upwards" in the tree following specific reference types */
  152. UA_Boolean
  153. isNodeInTree(UA_Nodestore *ns, const UA_NodeId *leafNode,
  154. const UA_NodeId *nodeToFind, const UA_NodeId *referenceTypeIds,
  155. size_t referenceTypeIdsSize);
  156. /* Returns an array with the hierarchy of type nodes. The returned array starts
  157. * at the leaf and continues "upwards" in the hierarchy based on the
  158. * ``hasSubType`` references. Since multiple-inheritance is possible in general,
  159. * duplicate entries are removed. */
  160. UA_StatusCode
  161. getTypeHierarchy(UA_Nodestore *ns, const UA_NodeId *leafType,
  162. UA_NodeId **typeHierarchy, size_t *typeHierarchySize);
  163. /* Returns the type node from the node on the stack top. The type node is pushed
  164. * on the stack and returned. */
  165. const UA_Node * getNodeType(UA_Server *server, const UA_Node *node);
  166. /* Many services come as an array of operations. This function generalizes the
  167. * processing of the operations. */
  168. typedef void (*UA_ServiceOperation)(UA_Server *server, UA_Session *session,
  169. const void *requestOperation,
  170. void *responseOperation);
  171. UA_StatusCode
  172. UA_Server_processServiceOperations(UA_Server *server, UA_Session *session,
  173. UA_ServiceOperation operationCallback,
  174. const size_t *requestOperations,
  175. const UA_DataType *requestOperationsType,
  176. size_t *responseOperations,
  177. const UA_DataType *responseOperationsType);
  178. /***************************************/
  179. /* Check Information Model Consistency */
  180. /***************************************/
  181. UA_StatusCode
  182. readValueAttribute(UA_Server *server, UA_Session *session,
  183. const UA_VariableNode *vn, UA_DataValue *v);
  184. /* Test whether the value matches a variable definition given by
  185. * - datatype
  186. * - valueranke
  187. * - array dimensions.
  188. * Sometimes it can be necessary to transform the content of the value, e.g.
  189. * byte array to bytestring or uint32 to some enum. If editableValue is non-NULL,
  190. * we try to create a matching variant that points to the original data. */
  191. UA_Boolean
  192. compatibleValue(UA_Server *server, const UA_NodeId *targetDataTypeId,
  193. UA_Int32 targetValueRank, size_t targetArrayDimensionsSize,
  194. const UA_UInt32 *targetArrayDimensions, const UA_Variant *value,
  195. const UA_NumericRange *range);
  196. UA_Boolean
  197. compatibleArrayDimensions(size_t constraintArrayDimensionsSize,
  198. const UA_UInt32 *constraintArrayDimensions,
  199. size_t testArrayDimensionsSize,
  200. const UA_UInt32 *testArrayDimensions);
  201. UA_Boolean
  202. compatibleValueArrayDimensions(const UA_Variant *value, size_t targetArrayDimensionsSize,
  203. const UA_UInt32 *targetArrayDimensions);
  204. UA_Boolean
  205. compatibleValueRankArrayDimensions(UA_Int32 valueRank, size_t arrayDimensionsSize);
  206. UA_Boolean
  207. compatibleDataType(UA_Server *server, const UA_NodeId *dataType,
  208. const UA_NodeId *constraintDataType);
  209. UA_Boolean
  210. compatibleValueRanks(UA_Int32 valueRank, UA_Int32 constraintValueRank);
  211. /*******************/
  212. /* Single-Services */
  213. /*******************/
  214. /* Some services take an array of "independent" requests. The single-services
  215. * are stored here to keep ua_services.h clean for documentation purposes. */
  216. void Service_Browse_single(UA_Server *server, UA_Session *session,
  217. struct ContinuationPointEntry *cp,
  218. const UA_BrowseDescription *descr,
  219. UA_UInt32 maxrefs, UA_BrowseResult *result);
  220. UA_DataValue
  221. UA_Server_readWithSession(UA_Server *server, UA_Session *session,
  222. const UA_ReadValueId *item,
  223. UA_TimestampsToReturn timestamps);
  224. /* Checks if a registration timed out and removes that registration.
  225. * Should be called periodically in main loop */
  226. void UA_Discovery_cleanupTimedOut(UA_Server *server, UA_DateTime nowMonotonic);
  227. # ifdef UA_ENABLE_DISCOVERY_MULTICAST
  228. UA_StatusCode
  229. initMulticastDiscoveryServer(UA_Server* server);
  230. void startMulticastDiscoveryServer(UA_Server *server);
  231. void stopMulticastDiscoveryServer(UA_Server *server);
  232. UA_StatusCode
  233. iterateMulticastDiscoveryServer(UA_Server* server, UA_DateTime *nextRepeat,
  234. UA_Boolean processIn);
  235. void destroyMulticastDiscoveryServer(UA_Server* server);
  236. typedef enum {
  237. UA_DISCOVERY_TCP, /* OPC UA TCP mapping */
  238. UA_DISCOVERY_TLS /* OPC UA HTTPS mapping */
  239. } UA_DiscoveryProtocol;
  240. /* Send a multicast probe to find any other OPC UA server on the network through mDNS. */
  241. UA_StatusCode
  242. UA_Discovery_multicastQuery(UA_Server* server);
  243. UA_StatusCode
  244. UA_Discovery_addRecord(UA_Server *server, const UA_String *servername,
  245. const UA_String *hostname, UA_UInt16 port,
  246. const UA_String *path, const UA_DiscoveryProtocol protocol,
  247. UA_Boolean createTxt, const UA_String* capabilites,
  248. size_t *capabilitiesSize);
  249. UA_StatusCode
  250. UA_Discovery_removeRecord(UA_Server *server, const UA_String *servername,
  251. const UA_String *hostname, UA_UInt16 port,
  252. UA_Boolean removeTxt);
  253. # endif
  254. /*****************************/
  255. /* AddNodes Begin and Finish */
  256. /*****************************/
  257. /* Creates a new node in the nodestore. */
  258. UA_StatusCode
  259. Operation_addNode_begin(UA_Server *server, UA_Session *session,
  260. const UA_AddNodesItem *item, void *nodeContext,
  261. UA_NodeId *outNewNodeId, UA_Boolean overrideChecks);
  262. /* Children, references, type-checking, constructors. */
  263. UA_StatusCode
  264. Operation_addNode_finish(UA_Server *server, UA_Session *session,
  265. const UA_NodeId *nodeId, const UA_NodeId *parentNodeId,
  266. const UA_NodeId *referenceTypeId, const UA_NodeId *typeDefinitionId,
  267. UA_Boolean overrideChecks);
  268. UA_StatusCode
  269. UA_Server_addMethodNode_finish(UA_Server *server, const UA_NodeId nodeId,
  270. const UA_NodeId parentNodeId, const UA_NodeId referenceTypeId,
  271. UA_MethodCallback method,
  272. size_t inputArgumentsSize, const UA_Argument* inputArguments,
  273. size_t outputArgumentsSize, const UA_Argument* outputArguments);
  274. /**********************/
  275. /* Create Namespace 0 */
  276. /**********************/
  277. #ifndef UA_ENABLE_GENERATE_NAMESPACE0
  278. void UA_Server_createNS0(UA_Server *server);
  279. #endif
  280. #ifdef __cplusplus
  281. } // extern "C"
  282. #endif
  283. #endif /* UA_SERVER_INTERNAL_H_ */