ua_server_internal.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. *
  5. * Copyright 2014-2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2014, 2017 (c) Florian Palm
  7. * Copyright 2015-2016 (c) Sten Grüner
  8. * Copyright 2015 (c) Chris Iatrou
  9. * Copyright 2015-2016 (c) Oleksiy Vasylyev
  10. * Copyright 2016-2017 (c) Stefan Profanter, fortiss GmbH
  11. * Copyright 2017 (c) Julian Grothoff
  12. */
  13. #ifndef UA_SERVER_INTERNAL_H_
  14. #define UA_SERVER_INTERNAL_H_
  15. #include "ua_util_internal.h"
  16. #include "ua_server.h"
  17. #include "ua_server_config.h"
  18. #include "ua_timer.h"
  19. #include "ua_connection_internal.h"
  20. #include "ua_session_manager.h"
  21. #include "ua_securechannel_manager.h"
  22. _UA_BEGIN_DECLS
  23. #ifdef UA_ENABLE_PUBSUB
  24. #include "ua_pubsub_manager.h"
  25. #endif
  26. #ifdef UA_ENABLE_SUBSCRIPTIONS
  27. #include "ua_subscription.h"
  28. typedef struct {
  29. UA_MonitoredItem monitoredItem;
  30. void *context;
  31. union {
  32. UA_Server_DataChangeNotificationCallback dataChangeCallback;
  33. /* UA_Server_EventNotificationCallback eventCallback; */
  34. } callback;
  35. } UA_LocalMonitoredItem;
  36. #endif
  37. #ifdef UA_ENABLE_MULTITHREADING
  38. #include <pthread.h>
  39. struct UA_Worker;
  40. typedef struct UA_Worker UA_Worker;
  41. struct UA_WorkerCallback;
  42. typedef struct UA_WorkerCallback UA_WorkerCallback;
  43. SIMPLEQ_HEAD(UA_DispatchQueue, UA_WorkerCallback);
  44. typedef struct UA_DispatchQueue UA_DispatchQueue;
  45. #endif /* UA_ENABLE_MULTITHREADING */
  46. #ifdef UA_ENABLE_DISCOVERY
  47. typedef struct registeredServer_list_entry {
  48. LIST_ENTRY(registeredServer_list_entry) pointers;
  49. UA_RegisteredServer registeredServer;
  50. UA_DateTime lastSeen;
  51. } registeredServer_list_entry;
  52. typedef struct periodicServerRegisterCallback_entry {
  53. LIST_ENTRY(periodicServerRegisterCallback_entry) pointers;
  54. struct PeriodicServerRegisterCallback *callback;
  55. } periodicServerRegisterCallback_entry;
  56. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  57. #include "mdnsd/libmdnsd/mdnsd.h"
  58. typedef struct serverOnNetwork_list_entry {
  59. LIST_ENTRY(serverOnNetwork_list_entry) pointers;
  60. UA_ServerOnNetwork serverOnNetwork;
  61. UA_DateTime created;
  62. UA_DateTime lastSeen;
  63. UA_Boolean txtSet;
  64. UA_Boolean srvSet;
  65. char* pathTmp;
  66. } serverOnNetwork_list_entry;
  67. #define SERVER_ON_NETWORK_HASH_PRIME 1009
  68. typedef struct serverOnNetwork_hash_entry {
  69. serverOnNetwork_list_entry* entry;
  70. struct serverOnNetwork_hash_entry* next;
  71. } serverOnNetwork_hash_entry;
  72. typedef struct mdnsHostnameToIp_list_entry {
  73. LIST_ENTRY(mdnsHostnameToIp_list_entry) pointers;
  74. UA_String mdnsHostname;
  75. struct in_addr addr;
  76. } mdnsHostnameToIp_list_entry;
  77. #define MDNS_HOSTNAME_TO_IP_HASH_PRIME 1009
  78. typedef struct mdnsHostnameToIp_hash_entry {
  79. mdnsHostnameToIp_list_entry* entry;
  80. struct mdnsHostnameToIp_hash_entry* next;
  81. } mdnsHostnameToIp_hash_entry;
  82. #endif /* UA_ENABLE_DISCOVERY_MULTICAST */
  83. #endif /* UA_ENABLE_DISCOVERY */
  84. struct UA_Server {
  85. /* Meta */
  86. UA_DateTime startTime;
  87. /* Security */
  88. UA_SecureChannelManager secureChannelManager;
  89. UA_SessionManager sessionManager;
  90. UA_Session adminSession; /* Local access to the services (for startup and
  91. * maintenance) uses this Session with all possible
  92. * access rights (Session Id: 1) */
  93. #ifdef UA_ENABLE_DISCOVERY
  94. /* Discovery */
  95. LIST_HEAD(registeredServer_list, registeredServer_list_entry) registeredServers; // doubly-linked list of registered servers
  96. size_t registeredServersSize;
  97. LIST_HEAD(periodicServerRegisterCallback_list, periodicServerRegisterCallback_entry) periodicServerRegisterCallbacks; // doubly-linked list of current register callbacks
  98. UA_Server_registerServerCallback registerServerCallback;
  99. void* registerServerCallbackData;
  100. # ifdef UA_ENABLE_DISCOVERY_MULTICAST
  101. mdns_daemon_t *mdnsDaemon;
  102. UA_SOCKET mdnsSocket;
  103. UA_Boolean mdnsMainSrvAdded;
  104. # ifdef UA_ENABLE_MULTITHREADING
  105. pthread_t mdnsThread;
  106. UA_Boolean mdnsRunning;
  107. # endif
  108. LIST_HEAD(serverOnNetwork_list, serverOnNetwork_list_entry) serverOnNetwork; // doubly-linked list of servers on the network (from mDNS)
  109. size_t serverOnNetworkSize;
  110. UA_UInt32 serverOnNetworkRecordIdCounter;
  111. UA_DateTime serverOnNetworkRecordIdLastReset;
  112. // hash mapping domain name to serverOnNetwork list entry
  113. struct serverOnNetwork_hash_entry* serverOnNetworkHash[SERVER_ON_NETWORK_HASH_PRIME];
  114. UA_Server_serverOnNetworkCallback serverOnNetworkCallback;
  115. void* serverOnNetworkCallbackData;
  116. LIST_HEAD(mdnsHostnameToIp_list, mdnsHostnameToIp_list_entry) mdnsHostnameToIp; // doubly-linked list of hostname to IP mapping (from mDNS)
  117. // hash mapping hostname to ip
  118. struct mdnsHostnameToIp_hash_entry* mdnsHostnameToIpHash[MDNS_HOSTNAME_TO_IP_HASH_PRIME];
  119. # endif
  120. #endif
  121. /* Namespaces */
  122. size_t namespacesSize;
  123. UA_String *namespaces;
  124. /* Callbacks with a repetition interval */
  125. UA_Timer timer;
  126. /* Delayed callbacks */
  127. SLIST_HEAD(DelayedCallbacksList, UA_DelayedCallback) delayedCallbacks;
  128. /* Worker threads */
  129. #ifdef UA_ENABLE_MULTITHREADING
  130. UA_Worker *workers; /* there are nThread workers in a running server */
  131. UA_DispatchQueue dispatchQueue; /* Dispatch queue for the worker threads */
  132. pthread_mutex_t dispatchQueue_accessMutex; /* mutex for access to queue */
  133. pthread_cond_t dispatchQueue_condition; /* so the workers don't spin if the queue is empty */
  134. pthread_mutex_t dispatchQueue_conditionMutex; /* mutex for access to condition variable */
  135. #endif
  136. /* For bootstrapping, omit some consistency checks, creating a reference to
  137. * the parent and member instantiation */
  138. UA_Boolean bootstrapNS0;
  139. #ifdef UA_ENABLE_SUBSCRIPTIONS
  140. /* To be cast to UA_LocalMonitoredItem to get the callback and context */
  141. LIST_HEAD(LocalMonitoredItems, UA_MonitoredItem) localMonitoredItems;
  142. UA_UInt32 lastLocalMonitoredItemId;
  143. #endif
  144. #ifdef UA_ENABLE_PUBSUB
  145. /* Publish/Subscribe toplevel container */
  146. UA_PubSubManager pubSubManager;
  147. #endif
  148. /* Config */
  149. UA_ServerConfig config;
  150. };
  151. /*****************/
  152. /* Node Handling */
  153. /*****************/
  154. #define UA_Nodestore_get(SERVER, NODEID) \
  155. (SERVER)->config.nodestore.getNode((SERVER)->config.nodestore.context, NODEID)
  156. #define UA_Nodestore_release(SERVER, NODEID) \
  157. (SERVER)->config.nodestore.releaseNode((SERVER)->config.nodestore.context, NODEID)
  158. #define UA_Nodestore_new(SERVER, NODECLASS) \
  159. (SERVER)->config.nodestore.newNode((SERVER)->config.nodestore.context, NODECLASS)
  160. #define UA_Nodestore_getCopy(SERVER, NODEID, OUTNODE) \
  161. (SERVER)->config.nodestore.getNodeCopy((SERVER)->config.nodestore.context, NODEID, OUTNODE)
  162. #define UA_Nodestore_insert(SERVER, NODE, OUTNODEID) \
  163. (SERVER)->config.nodestore.insertNode((SERVER)->config.nodestore.context, NODE, OUTNODEID)
  164. #define UA_Nodestore_delete(SERVER, NODE) \
  165. (SERVER)->config.nodestore.deleteNode((SERVER)->config.nodestore.context, NODE)
  166. #define UA_Nodestore_remove(SERVER, NODEID) \
  167. (SERVER)->config.nodestore.removeNode((SERVER)->config.nodestore.context, NODEID)
  168. /* Deletes references from the node which are not matching any type in the given
  169. * array. Could be used to e.g. delete all the references, except
  170. * 'HASMODELINGRULE' */
  171. void UA_Node_deleteReferencesSubset(UA_Node *node, size_t referencesSkipSize,
  172. UA_NodeId* referencesSkip);
  173. /* Calls the callback with the node retrieved from the nodestore on top of the
  174. * stack. Either a copy or the original node for in-situ editing. Depends on
  175. * multithreading and the nodestore.*/
  176. typedef UA_StatusCode (*UA_EditNodeCallback)(UA_Server*, UA_Session*,
  177. UA_Node *node, void*);
  178. UA_StatusCode UA_Server_editNode(UA_Server *server, UA_Session *session,
  179. const UA_NodeId *nodeId,
  180. UA_EditNodeCallback callback,
  181. void *data);
  182. /*************/
  183. /* Callbacks */
  184. /*************/
  185. /* Delayed callbacks are executed when all previously dispatched callbacks are
  186. * finished */
  187. UA_StatusCode
  188. UA_Server_delayedCallback(UA_Server *server, UA_ServerCallback callback, void *data);
  189. UA_StatusCode
  190. UA_Server_delayedFree(UA_Server *server, void *data);
  191. #ifndef UA_ENABLE_MULTITHREADING
  192. /* Execute all delayed callbacks regardless of whether the worker threads have
  193. * finished previous work */
  194. void UA_Server_cleanupDelayedCallbacks(UA_Server *server);
  195. #else
  196. void UA_Server_cleanupDispatchQueue(UA_Server *server);
  197. #endif
  198. /* Callback is executed in the same thread or, if possible, dispatched to one of
  199. * the worker threads. */
  200. void
  201. UA_Server_workerCallback(UA_Server *server, UA_ServerCallback callback, void *data);
  202. /*********************/
  203. /* Utility Functions */
  204. /*********************/
  205. /* A few global NodeId definitions */
  206. extern const UA_NodeId subtypeId;
  207. extern const UA_NodeId hierarchicalReferences;
  208. UA_UInt16 addNamespace(UA_Server *server, const UA_String name);
  209. UA_Boolean
  210. UA_Node_hasSubTypeOrInstances(const UA_Node *node);
  211. /* Recursively searches "upwards" in the tree following specific reference types */
  212. UA_Boolean
  213. isNodeInTree(UA_Nodestore *ns, const UA_NodeId *leafNode,
  214. const UA_NodeId *nodeToFind, const UA_NodeId *referenceTypeIds,
  215. size_t referenceTypeIdsSize);
  216. /* Returns an array with the hierarchy of type nodes. The returned array starts
  217. * at the leaf and continues "upwards" or "downwards" in the hierarchy based on the
  218. * ``hasSubType`` references. Since multiple-inheritance is possible in general,
  219. * duplicate entries are removed.
  220. * The parameter `walkDownwards` indicates the direction of search.
  221. * If set to TRUE it will get all the subtypes of the given
  222. * leafType (including leafType).
  223. * If set to FALSE it will get all the parent types of the given
  224. * leafType (including leafType)*/
  225. UA_StatusCode
  226. getTypeHierarchy(UA_Nodestore *ns, const UA_NodeId *leafType,
  227. UA_NodeId **typeHierarchy, size_t *typeHierarchySize,
  228. UA_Boolean walkDownwards);
  229. /* Same as getTypeHierarchy but takes multiple leafTypes as parameter and returns
  230. * an combined list of all the found types for all the leaf types */
  231. UA_StatusCode
  232. getTypesHierarchy(UA_Nodestore *ns, const UA_NodeId *leafType, size_t leafTypeSize,
  233. UA_NodeId **typeHierarchy, size_t *typeHierarchySize,
  234. UA_Boolean walkDownwards);
  235. /* Returns the type node from the node on the stack top. The type node is pushed
  236. * on the stack and returned. */
  237. const UA_Node * getNodeType(UA_Server *server, const UA_Node *node);
  238. /* Write a node attribute with a defined session */
  239. UA_StatusCode
  240. UA_Server_writeWithSession(UA_Server *server, UA_Session *session,
  241. const UA_WriteValue *value);
  242. /* Many services come as an array of operations. This function generalizes the
  243. * processing of the operations. */
  244. typedef void (*UA_ServiceOperation)(UA_Server *server, UA_Session *session,
  245. void *context,
  246. const void *requestOperation,
  247. void *responseOperation);
  248. UA_StatusCode
  249. UA_Server_processServiceOperations(UA_Server *server, UA_Session *session,
  250. UA_ServiceOperation operationCallback,
  251. void *context,
  252. const size_t *requestOperations,
  253. const UA_DataType *requestOperationsType,
  254. size_t *responseOperations,
  255. const UA_DataType *responseOperationsType)
  256. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  257. /***************************************/
  258. /* Check Information Model Consistency */
  259. /***************************************/
  260. /* Read a node attribute in the context of a "checked-out" node. So the
  261. * attribute will not be copied when possible. The variant then points into the
  262. * node and has UA_VARIANT_DATA_NODELETE set. */
  263. void
  264. ReadWithNode(const UA_Node *node, UA_Server *server, UA_Session *session,
  265. UA_TimestampsToReturn timestampsToReturn,
  266. const UA_ReadValueId *id, UA_DataValue *v);
  267. UA_StatusCode
  268. readValueAttribute(UA_Server *server, UA_Session *session,
  269. const UA_VariableNode *vn, UA_DataValue *v);
  270. /* Test whether the value matches a variable definition given by
  271. * - datatype
  272. * - valueranke
  273. * - array dimensions.
  274. * Sometimes it can be necessary to transform the content of the value, e.g.
  275. * byte array to bytestring or uint32 to some enum. If editableValue is non-NULL,
  276. * we try to create a matching variant that points to the original data. */
  277. UA_Boolean
  278. compatibleValue(UA_Server *server, UA_Session *session, const UA_NodeId *targetDataTypeId,
  279. UA_Int32 targetValueRank, size_t targetArrayDimensionsSize,
  280. const UA_UInt32 *targetArrayDimensions, const UA_Variant *value,
  281. const UA_NumericRange *range);
  282. UA_Boolean
  283. compatibleArrayDimensions(size_t constraintArrayDimensionsSize,
  284. const UA_UInt32 *constraintArrayDimensions,
  285. size_t testArrayDimensionsSize,
  286. const UA_UInt32 *testArrayDimensions);
  287. UA_Boolean
  288. compatibleValueArrayDimensions(const UA_Variant *value, size_t targetArrayDimensionsSize,
  289. const UA_UInt32 *targetArrayDimensions);
  290. UA_Boolean
  291. compatibleValueRankArrayDimensions(UA_Server *server, UA_Session *session,
  292. UA_Int32 valueRank, size_t arrayDimensionsSize);
  293. UA_Boolean
  294. compatibleDataType(UA_Server *server, const UA_NodeId *dataType,
  295. const UA_NodeId *constraintDataType, UA_Boolean isValue);
  296. UA_Boolean
  297. compatibleValueRanks(UA_Int32 valueRank, UA_Int32 constraintValueRank);
  298. void
  299. Operation_Browse(UA_Server *server, UA_Session *session, UA_UInt32 *maxrefs,
  300. const UA_BrowseDescription *descr, UA_BrowseResult *result);
  301. UA_DataValue
  302. UA_Server_readWithSession(UA_Server *server, UA_Session *session,
  303. const UA_ReadValueId *item,
  304. UA_TimestampsToReturn timestampsToReturn);
  305. /* Checks if a registration timed out and removes that registration.
  306. * Should be called periodically in main loop */
  307. void UA_Discovery_cleanupTimedOut(UA_Server *server, UA_DateTime nowMonotonic);
  308. # ifdef UA_ENABLE_DISCOVERY_MULTICAST
  309. UA_StatusCode
  310. initMulticastDiscoveryServer(UA_Server* server);
  311. void startMulticastDiscoveryServer(UA_Server *server);
  312. void stopMulticastDiscoveryServer(UA_Server *server);
  313. UA_StatusCode
  314. iterateMulticastDiscoveryServer(UA_Server* server, UA_DateTime *nextRepeat,
  315. UA_Boolean processIn);
  316. void destroyMulticastDiscoveryServer(UA_Server* server);
  317. typedef enum {
  318. UA_DISCOVERY_TCP, /* OPC UA TCP mapping */
  319. UA_DISCOVERY_TLS /* OPC UA HTTPS mapping */
  320. } UA_DiscoveryProtocol;
  321. /* Send a multicast probe to find any other OPC UA server on the network through mDNS. */
  322. UA_StatusCode
  323. UA_Discovery_multicastQuery(UA_Server* server);
  324. UA_StatusCode
  325. UA_Discovery_addRecord(UA_Server *server, const UA_String *servername,
  326. const UA_String *hostname, UA_UInt16 port,
  327. const UA_String *path, const UA_DiscoveryProtocol protocol,
  328. UA_Boolean createTxt, const UA_String* capabilites,
  329. size_t *capabilitiesSize);
  330. UA_StatusCode
  331. UA_Discovery_removeRecord(UA_Server *server, const UA_String *servername,
  332. const UA_String *hostname, UA_UInt16 port,
  333. UA_Boolean removeTxt);
  334. # endif
  335. /*****************************/
  336. /* AddNodes Begin and Finish */
  337. /*****************************/
  338. /* Creates a new node in the nodestore. */
  339. UA_StatusCode
  340. AddNode_raw(UA_Server *server, UA_Session *session, void *nodeContext,
  341. const UA_AddNodesItem *item, UA_NodeId *outNewNodeId);
  342. /* Check the reference to the parent node; Add references. */
  343. UA_StatusCode
  344. AddNode_addRefs(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId,
  345. const UA_NodeId *parentNodeId, const UA_NodeId *referenceTypeId,
  346. const UA_NodeId *typeDefinitionId);
  347. /* Type-check type-definition; Run the constructors */
  348. UA_StatusCode
  349. AddNode_finish(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId);
  350. /**********************/
  351. /* Create Namespace 0 */
  352. /**********************/
  353. UA_StatusCode UA_Server_initNS0(UA_Server *server);
  354. _UA_END_DECLS
  355. #endif /* UA_SERVER_INTERNAL_H_ */