ua_services_view.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. #include "ua_server_internal.h"
  5. #include "ua_services.h"
  6. static UA_StatusCode
  7. fillReferenceDescription(UA_Server *server, const UA_Node *curr,
  8. const UA_NodeReferenceKind *ref, UA_UInt32 mask,
  9. UA_ReferenceDescription *descr) {
  10. UA_ReferenceDescription_init(descr);
  11. UA_StatusCode retval = UA_NodeId_copy(&curr->nodeId, &descr->nodeId.nodeId);
  12. if(mask & UA_BROWSERESULTMASK_REFERENCETYPEID)
  13. retval |= UA_NodeId_copy(&ref->referenceTypeId, &descr->referenceTypeId);
  14. if(mask & UA_BROWSERESULTMASK_ISFORWARD)
  15. descr->isForward = !ref->isInverse;
  16. if(mask & UA_BROWSERESULTMASK_NODECLASS)
  17. retval |= UA_NodeClass_copy(&curr->nodeClass, &descr->nodeClass);
  18. if(mask & UA_BROWSERESULTMASK_BROWSENAME)
  19. retval |= UA_QualifiedName_copy(&curr->browseName, &descr->browseName);
  20. if(mask & UA_BROWSERESULTMASK_DISPLAYNAME)
  21. retval |= UA_LocalizedText_copy(&curr->displayName, &descr->displayName);
  22. if(mask & UA_BROWSERESULTMASK_TYPEDEFINITION) {
  23. if(curr->nodeClass == UA_NODECLASS_OBJECT || curr->nodeClass == UA_NODECLASS_VARIABLE) {
  24. UA_NodeId type;
  25. getNodeType(server, curr , &type);
  26. retval |= UA_NodeId_copy(&type, &descr->typeDefinition.nodeId);
  27. }
  28. }
  29. return retval;
  30. }
  31. #ifdef UA_ENABLE_EXTERNAL_NAMESPACES
  32. static const UA_Node *
  33. returnRelevantNodeExternal(UA_ExternalNodeStore *ens, const UA_BrowseDescription *descr,
  34. const UA_ReferenceNode *reference) {
  35. /* prepare a read request in the external nodestore */
  36. UA_ReadValueId *readValueIds = UA_Array_new(5,&UA_TYPES[UA_TYPES_READVALUEID]);
  37. UA_UInt32 *indices = UA_Array_new(5,&UA_TYPES[UA_TYPES_UINT32]);
  38. UA_UInt32 indicesSize = 5;
  39. UA_DataValue *readNodesResults = UA_Array_new(5,&UA_TYPES[UA_TYPES_DATAVALUE]);
  40. UA_DiagnosticInfo *diagnosticInfos = UA_Array_new(5,&UA_TYPES[UA_TYPES_DIAGNOSTICINFO]);
  41. for(UA_UInt32 i = 0; i < 5; ++i) {
  42. readValueIds[i].nodeId = reference->targetId.nodeId;
  43. indices[i] = i;
  44. }
  45. readValueIds[0].attributeId = UA_ATTRIBUTEID_NODECLASS;
  46. readValueIds[1].attributeId = UA_ATTRIBUTEID_BROWSENAME;
  47. readValueIds[2].attributeId = UA_ATTRIBUTEID_DISPLAYNAME;
  48. readValueIds[3].attributeId = UA_ATTRIBUTEID_DESCRIPTION;
  49. readValueIds[4].attributeId = UA_ATTRIBUTEID_WRITEMASK;
  50. ens->readNodes(ens->ensHandle, NULL, readValueIds, indices,
  51. indicesSize, readNodesResults, false, diagnosticInfos);
  52. /* create and fill a dummy nodeStructure */
  53. UA_Node *node = (UA_Node*) UA_NodeStore_newObjectNode();
  54. UA_NodeId_copy(&(reference->targetId.nodeId), &(node->nodeId));
  55. if(readNodesResults[0].status == UA_STATUSCODE_GOOD)
  56. UA_NodeClass_copy((UA_NodeClass*)readNodesResults[0].value.data, &(node->nodeClass));
  57. if(readNodesResults[1].status == UA_STATUSCODE_GOOD)
  58. UA_QualifiedName_copy((UA_QualifiedName*)readNodesResults[1].value.data, &(node->browseName));
  59. if(readNodesResults[2].status == UA_STATUSCODE_GOOD)
  60. UA_LocalizedText_copy((UA_LocalizedText*)readNodesResults[2].value.data, &(node->displayName));
  61. if(readNodesResults[3].status == UA_STATUSCODE_GOOD)
  62. UA_LocalizedText_copy((UA_LocalizedText*)readNodesResults[3].value.data, &(node->description));
  63. if(readNodesResults[4].status == UA_STATUSCODE_GOOD)
  64. UA_UInt32_copy((UA_UInt32*)readNodesResults[4].value.data, &(node->writeMask));
  65. UA_ReferenceNode **references = &node->references;
  66. UA_UInt32 *referencesSize = (UA_UInt32*)&node->referencesSize;
  67. ens->getOneWayReferences (ens->ensHandle, &node->nodeId, referencesSize, references);
  68. UA_Array_delete(readValueIds,5, &UA_TYPES[UA_TYPES_READVALUEID]);
  69. UA_Array_delete(indices,5, &UA_TYPES[UA_TYPES_UINT32]);
  70. UA_Array_delete(readNodesResults,5, &UA_TYPES[UA_TYPES_DATAVALUE]);
  71. UA_Array_delete(diagnosticInfos,5, &UA_TYPES[UA_TYPES_DIAGNOSTICINFO]);
  72. if(node && descr->nodeClassMask != 0 && (node->nodeClass & descr->nodeClassMask) == 0) {
  73. UA_NodeStore_deleteNode(node);
  74. return NULL;
  75. }
  76. return node;
  77. }
  78. #endif
  79. static void removeCp(struct ContinuationPointEntry *cp, UA_Session* session) {
  80. LIST_REMOVE(cp, pointers);
  81. UA_ByteString_deleteMembers(&cp->identifier);
  82. UA_BrowseDescription_deleteMembers(&cp->browseDescription);
  83. UA_free(cp);
  84. ++session->availableContinuationPoints;
  85. }
  86. /* Returns whether the node / continuationpoint is done */
  87. static UA_Boolean
  88. browseRelevantReferences(UA_Server *server, UA_BrowseResult *result, const UA_NodeId *relevant_refs,
  89. size_t relevant_refs_size, const UA_BrowseDescription *descr,
  90. struct ContinuationPointEntry *cp) {
  91. UA_assert(cp != NULL);
  92. /* Get the node */
  93. const UA_Node *node = UA_NodeStore_get(server->nodestore, &descr->nodeId);
  94. if(!node) {
  95. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  96. return true;;
  97. }
  98. /* If the node has no references, just return */
  99. if(node->referencesSize == 0) {
  100. result->referencesSize = 0;
  101. return true;;
  102. }
  103. /* How many references can we return at most? */
  104. size_t maxrefs = cp->maxReferences;
  105. if(maxrefs == 0)
  106. maxrefs = UA_INT32_MAX;
  107. else if(maxrefs > node->referencesSize)
  108. maxrefs = node->referencesSize;
  109. /* Allocate the results array */
  110. size_t refs_size = 2; /* True size of the array */
  111. result->references =
  112. (UA_ReferenceDescription*)UA_Array_new(refs_size, &UA_TYPES[UA_TYPES_REFERENCEDESCRIPTION]);
  113. if(!result->references) {
  114. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  115. return false;
  116. }
  117. size_t referenceKindIndex = 0;
  118. size_t targetIndex = 0;
  119. if(cp) {
  120. referenceKindIndex = cp->referenceKindIndex;
  121. targetIndex = cp->targetIndex;
  122. }
  123. /* Loop over the node's references */
  124. for(; referenceKindIndex < node->referencesSize; ++referenceKindIndex) {
  125. UA_NodeReferenceKind *rk = &node->references[referenceKindIndex];
  126. /* Reference in the right direction? */
  127. if(rk->isInverse && descr->browseDirection == UA_BROWSEDIRECTION_FORWARD)
  128. continue;
  129. if(!rk->isInverse && descr->browseDirection == UA_BROWSEDIRECTION_INVERSE)
  130. continue;
  131. /* Is the reference part of the hierarchy of references we look for? */
  132. if(relevant_refs) {
  133. UA_Boolean is_relevant = false;
  134. for(size_t i = 0; i < relevant_refs_size; ++i) {
  135. if(UA_NodeId_equal(&rk->referenceTypeId, &relevant_refs[i])) {
  136. is_relevant = true;
  137. break;
  138. }
  139. }
  140. if(!is_relevant)
  141. continue;
  142. }
  143. /* Loop over the targets */
  144. for(; targetIndex < rk->targetIdsSize; ++targetIndex) {
  145. /* Get the node */
  146. const UA_Node *target = UA_NodeStore_get(server->nodestore, &rk->targetIds[targetIndex].nodeId);
  147. /* Test if the node class matches */
  148. if(!target || (descr->nodeClassMask != 0 && (target->nodeClass & descr->nodeClassMask) == 0))
  149. continue;
  150. /* A match! Can we return it? */
  151. if(result->referencesSize >= maxrefs) {
  152. /* There are references we could not return */
  153. cp->referenceKindIndex = referenceKindIndex;
  154. cp->targetIndex = targetIndex;
  155. return false;
  156. }
  157. /* Make enough space in the array */
  158. if(result->referencesSize >= refs_size) {
  159. refs_size *= 2;
  160. UA_ReferenceDescription *rd = UA_realloc(result->references,
  161. sizeof(UA_ReferenceDescription) * refs_size);
  162. if(!rd) {
  163. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  164. goto error_recovery;
  165. }
  166. result->references = rd;
  167. }
  168. /* Copy the node description */
  169. result->statusCode = fillReferenceDescription(server, target, rk, descr->resultMask,
  170. &result->references[result->referencesSize]);
  171. /* fillReferenceDescription(UA_Server *server, const UA_Node *curr, */
  172. /* const UA_NodeReferenceKind *ref, */
  173. /* const UA_ExpandedNodeId *target, UA_UInt32 mask, */
  174. /* UA_ReferenceDescription *descr) { */
  175. if(result->statusCode != UA_STATUSCODE_GOOD)
  176. goto error_recovery;
  177. /* Increase the counter */
  178. result->referencesSize++;
  179. }
  180. targetIndex = 0; /* Start at index 0 for the next reference kind */
  181. }
  182. /* No relevant references, return array of length zero */
  183. if(result->referencesSize == 0) {
  184. UA_free(result->references);
  185. result->references = (UA_ReferenceDescription *)UA_EMPTY_ARRAY_SENTINEL;
  186. }
  187. /* The node is done */
  188. return true;
  189. error_recovery:
  190. if(result->referencesSize == 0)
  191. UA_free(result->references);
  192. else
  193. UA_Array_delete(result->references, result->referencesSize,
  194. &UA_TYPES[UA_TYPES_REFERENCEDESCRIPTION]);
  195. result->references = NULL;
  196. result->referencesSize = 0;
  197. return false;
  198. }
  199. /* Results for a single browsedescription. This is the inner loop for both
  200. * Browse and BrowseNext
  201. *
  202. * @param session Session to save continuationpoints
  203. * @param ns The nodstore where the to-be-browsed node can be found
  204. * @param cp If cp is not null, we continue from here If cp is null, we can add
  205. * a new continuation point if possible and necessary.
  206. * @param descr If no cp is set, we take the browsedescription from there
  207. * @param maxrefs The maximum number of references the client has requested. If 0,
  208. * all matching references are returned at once.
  209. * @param result The entry in the request */
  210. void
  211. Service_Browse_single(UA_Server *server, UA_Session *session,
  212. struct ContinuationPointEntry *cp, const UA_BrowseDescription *descr,
  213. UA_UInt32 maxrefs, UA_BrowseResult *result) {
  214. struct ContinuationPointEntry *internal_cp = cp;
  215. if(!internal_cp) {
  216. /* If there is no continuation point, stack-allocate one. It gets copied
  217. * on the heap when this is required at a later point. */
  218. internal_cp = (struct ContinuationPointEntry *)UA_alloca(sizeof(struct ContinuationPointEntry));
  219. memset(internal_cp, 0, sizeof(struct ContinuationPointEntry));
  220. internal_cp->maxReferences = maxrefs;
  221. } else {
  222. /* Set the browsedescription if a cp is given */
  223. descr = &cp->browseDescription;
  224. }
  225. /* Is the browsedirection valid? */
  226. if(descr->browseDirection != UA_BROWSEDIRECTION_BOTH &&
  227. descr->browseDirection != UA_BROWSEDIRECTION_FORWARD &&
  228. descr->browseDirection != UA_BROWSEDIRECTION_INVERSE) {
  229. result->statusCode = UA_STATUSCODE_BADBROWSEDIRECTIONINVALID;
  230. return;
  231. }
  232. /* Get the references that match the browsedescription. reftypes == NULL
  233. * indicates that all references shall be returned. */
  234. size_t reftypesSize = 0;
  235. UA_NodeId *reftypes = NULL;
  236. UA_Boolean all_refs = UA_NodeId_isNull(&descr->referenceTypeId);
  237. if(!all_refs) {
  238. const UA_Node *rootRef = UA_NodeStore_get(server->nodestore, &descr->referenceTypeId);
  239. if(!rootRef || rootRef->nodeClass != UA_NODECLASS_REFERENCETYPE) {
  240. result->statusCode = UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  241. return;
  242. }
  243. if(descr->includeSubtypes) {
  244. result->statusCode = getTypeHierarchy(server->nodestore, rootRef, false,
  245. &reftypes, &reftypesSize);
  246. if(result->statusCode != UA_STATUSCODE_GOOD)
  247. return;
  248. } else {
  249. reftypes = (UA_NodeId*)(uintptr_t)&descr->referenceTypeId;
  250. reftypesSize = 1;
  251. }
  252. }
  253. /* Browse with the relevant references */
  254. UA_Boolean done = browseRelevantReferences(server, result, reftypes, reftypesSize, descr, internal_cp);
  255. /* Clean up the array of relevant references */
  256. if(!all_refs && descr->includeSubtypes)
  257. UA_Array_delete(reftypes, reftypesSize, &UA_TYPES[UA_TYPES_NODEID]);
  258. /* Exit early if an error occured */
  259. if(result->statusCode != UA_STATUSCODE_GOOD)
  260. return;
  261. /* A continuation point exists already */
  262. if(cp) {
  263. if(done)
  264. removeCp(cp, session); /* All done, remove a finished continuationPoint */
  265. else
  266. UA_ByteString_copy(&cp->identifier, &result->continuationPoint); /* Return the cp identifier */
  267. return;
  268. }
  269. /* Create a new continuation point */
  270. if(!done) {
  271. if(session->availableContinuationPoints <= 0 ||
  272. !(cp = (struct ContinuationPointEntry *)UA_malloc(sizeof(struct ContinuationPointEntry)))) {
  273. result->statusCode = UA_STATUSCODE_BADNOCONTINUATIONPOINTS;
  274. return;
  275. }
  276. UA_BrowseDescription_copy(descr, &cp->browseDescription);
  277. cp->referenceKindIndex = internal_cp->referenceKindIndex;
  278. cp->targetIndex = internal_cp->targetIndex;
  279. cp->maxReferences = internal_cp->maxReferences;
  280. /* Create a random bytestring via a Guid */
  281. UA_Guid *ident = UA_Guid_new();
  282. *ident = UA_Guid_random();
  283. cp->identifier.data = (UA_Byte*)ident;
  284. cp->identifier.length = sizeof(UA_Guid);
  285. /* Return the cp identifier */
  286. UA_ByteString_copy(&cp->identifier, &result->continuationPoint);
  287. /* Attach the cp to the session */
  288. LIST_INSERT_HEAD(&session->continuationPoints, cp, pointers);
  289. --session->availableContinuationPoints;
  290. }
  291. }
  292. void Service_Browse(UA_Server *server, UA_Session *session, const UA_BrowseRequest *request,
  293. UA_BrowseResponse *response) {
  294. UA_LOG_DEBUG_SESSION(server->config.logger, session, "Processing BrowseRequest");
  295. if(!UA_NodeId_isNull(&request->view.viewId)) {
  296. response->responseHeader.serviceResult = UA_STATUSCODE_BADVIEWIDUNKNOWN;
  297. return;
  298. }
  299. if(request->nodesToBrowseSize <= 0) {
  300. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  301. return;
  302. }
  303. size_t size = request->nodesToBrowseSize;
  304. response->results = (UA_BrowseResult *)UA_Array_new(size, &UA_TYPES[UA_TYPES_BROWSERESULT]);
  305. if(!response->results) {
  306. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  307. return;
  308. }
  309. response->resultsSize = size;
  310. #ifdef UA_ENABLE_EXTERNAL_NAMESPACES
  311. #ifdef NO_ALLOCA
  312. UA_Boolean isExternal[size];
  313. UA_UInt32 indices[size];
  314. #else
  315. UA_Boolean *isExternal = UA_alloca(sizeof(UA_Boolean) * size);
  316. UA_UInt32 *indices = UA_alloca(sizeof(UA_UInt32) * size);
  317. #endif /*NO_ALLOCA */
  318. memset(isExternal, false, sizeof(UA_Boolean) * size);
  319. for(size_t j = 0; j < server->externalNamespacesSize; ++j) {
  320. size_t indexSize = 0;
  321. for(size_t i = 0; i < size; ++i) {
  322. if(request->nodesToBrowse[i].nodeId.namespaceIndex != server->externalNamespaces[j].index)
  323. continue;
  324. isExternal[i] = true;
  325. indices[indexSize] = (UA_UInt32)i;
  326. ++indexSize;
  327. }
  328. if(indexSize == 0)
  329. continue;
  330. UA_ExternalNodeStore *ens = &server->externalNamespaces[j].externalNodeStore;
  331. ens->browseNodes(ens->ensHandle, &request->requestHeader, request->nodesToBrowse, indices,
  332. (UA_UInt32)indexSize, request->requestedMaxReferencesPerNode,
  333. response->results, response->diagnosticInfos);
  334. }
  335. #endif
  336. for(size_t i = 0; i < size; ++i) {
  337. #ifdef UA_ENABLE_EXTERNAL_NAMESPACES
  338. if(!isExternal[i])
  339. #endif
  340. Service_Browse_single(server, session, NULL, &request->nodesToBrowse[i],
  341. request->requestedMaxReferencesPerNode, &response->results[i]);
  342. }
  343. }
  344. UA_BrowseResult
  345. UA_Server_browse(UA_Server *server, UA_UInt32 maxrefs, const UA_BrowseDescription *descr) {
  346. UA_BrowseResult result;
  347. UA_BrowseResult_init(&result);
  348. UA_RCU_LOCK();
  349. Service_Browse_single(server, &adminSession, NULL, descr, maxrefs, &result);
  350. UA_RCU_UNLOCK();
  351. return result;
  352. }
  353. static void
  354. UA_Server_browseNext_single(UA_Server *server, UA_Session *session, UA_Boolean releaseContinuationPoint,
  355. const UA_ByteString *continuationPoint, UA_BrowseResult *result) {
  356. result->statusCode = UA_STATUSCODE_BADCONTINUATIONPOINTINVALID;
  357. struct ContinuationPointEntry *cp, *temp;
  358. LIST_FOREACH_SAFE(cp, &session->continuationPoints, pointers, temp) {
  359. if(UA_ByteString_equal(&cp->identifier, continuationPoint)) {
  360. result->statusCode = UA_STATUSCODE_GOOD;
  361. if(!releaseContinuationPoint)
  362. Service_Browse_single(server, session, cp, NULL, 0, result);
  363. else
  364. removeCp(cp, session);
  365. break;
  366. }
  367. }
  368. }
  369. void Service_BrowseNext(UA_Server *server, UA_Session *session, const UA_BrowseNextRequest *request,
  370. UA_BrowseNextResponse *response) {
  371. UA_LOG_DEBUG_SESSION(server->config.logger, session, "Processing BrowseNextRequest");
  372. if(request->continuationPointsSize <= 0) {
  373. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  374. return;
  375. }
  376. size_t size = request->continuationPointsSize;
  377. response->results = (UA_BrowseResult *)UA_Array_new(size, &UA_TYPES[UA_TYPES_BROWSERESULT]);
  378. if(!response->results) {
  379. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  380. return;
  381. }
  382. response->resultsSize = size;
  383. for(size_t i = 0; i < size; ++i)
  384. UA_Server_browseNext_single(server, session, request->releaseContinuationPoints,
  385. &request->continuationPoints[i], &response->results[i]);
  386. }
  387. UA_BrowseResult
  388. UA_Server_browseNext(UA_Server *server, UA_Boolean releaseContinuationPoint,
  389. const UA_ByteString *continuationPoint) {
  390. UA_BrowseResult result;
  391. UA_BrowseResult_init(&result);
  392. UA_RCU_LOCK();
  393. UA_Server_browseNext_single(server, &adminSession, releaseContinuationPoint,
  394. continuationPoint, &result);
  395. UA_RCU_UNLOCK();
  396. return result;
  397. }
  398. /***********************/
  399. /* TranslateBrowsePath */
  400. /***********************/
  401. static void
  402. walkBrowsePathElementNodeReference(UA_BrowsePathResult *result, size_t *targetsSize,
  403. UA_NodeId **next, size_t *nextSize, size_t *nextCount,
  404. UA_UInt32 elemDepth, UA_Boolean inverse, UA_Boolean all_refs,
  405. const UA_NodeId *reftypes, size_t reftypes_count,
  406. const UA_NodeReferenceKind *rk) {
  407. /* Does the direction of the reference match? */
  408. if(rk->isInverse != inverse)
  409. return;
  410. /* Is the node relevant? */
  411. if(!all_refs) {
  412. UA_Boolean match = false;
  413. for(size_t j = 0; j < reftypes_count; ++j) {
  414. if(UA_NodeId_equal(&rk->referenceTypeId, &reftypes[j])) {
  415. match = true;
  416. break;
  417. }
  418. }
  419. if(!match)
  420. return;
  421. }
  422. /* Loop over the targets */
  423. for(size_t i = 0; i < rk->targetIdsSize; i++) {
  424. UA_ExpandedNodeId *targetId = &rk->targetIds[i];
  425. /* Does the reference point to an external server? Then add to the
  426. * targets with the right path "depth" */
  427. if(targetId->serverIndex != 0) {
  428. UA_BrowsePathTarget *tempTargets =
  429. (UA_BrowsePathTarget *)UA_realloc(result->targets, sizeof(UA_BrowsePathTarget) * (*targetsSize) * 2);
  430. if(!tempTargets) {
  431. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  432. return;
  433. }
  434. result->targets = tempTargets;
  435. (*targetsSize) *= 2;
  436. result->statusCode = UA_ExpandedNodeId_copy(targetId,
  437. &result->targets[result->targetsSize].targetId);
  438. result->targets[result->targetsSize].remainingPathIndex = elemDepth;
  439. continue;
  440. }
  441. /* Can we store the node in the array of candidates for deep-search? */
  442. if(*nextSize <= *nextCount) {
  443. UA_NodeId *tempNext = (UA_NodeId *)UA_realloc(*next, sizeof(UA_NodeId) * (*nextSize) * 2);
  444. if(!tempNext) {
  445. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  446. return;
  447. }
  448. *next = tempNext;
  449. (*nextSize) *= 2;
  450. }
  451. /* Add the node to the next array for the following path element */
  452. result->statusCode = UA_NodeId_copy(&targetId->nodeId,
  453. &(*next)[*nextCount]);
  454. if(result->statusCode != UA_STATUSCODE_GOOD)
  455. return;
  456. ++(*nextCount);
  457. }
  458. }
  459. static void
  460. walkBrowsePathElement(UA_Server *server, UA_Session *session,
  461. UA_BrowsePathResult *result, size_t *targetsSize,
  462. const UA_RelativePathElement *elem, UA_UInt32 elemDepth,
  463. const UA_QualifiedName *targetName,
  464. const UA_NodeId *current, const size_t currentCount,
  465. UA_NodeId **next, size_t *nextSize, size_t *nextCount) {
  466. /* Get the full list of relevant referencetypes for this path element */
  467. UA_NodeId *reftypes = NULL;
  468. size_t reftypes_count = 1; // all_refs or no subtypes => 1
  469. UA_Boolean all_refs = false;
  470. if(UA_NodeId_isNull(&elem->referenceTypeId)) {
  471. all_refs = true;
  472. } else if(!elem->includeSubtypes) {
  473. reftypes = (UA_NodeId*)(uintptr_t)&elem->referenceTypeId; // ptr magic due to const cast
  474. } else {
  475. const UA_Node *rootRef = UA_NodeStore_get(server->nodestore, &elem->referenceTypeId);
  476. if(!rootRef || rootRef->nodeClass != UA_NODECLASS_REFERENCETYPE)
  477. return;
  478. UA_StatusCode retval =
  479. getTypeHierarchy(server->nodestore, rootRef, false, &reftypes, &reftypes_count);
  480. if(retval != UA_STATUSCODE_GOOD)
  481. return;
  482. }
  483. /* Iterate over all nodes at the current depth-level */
  484. for(size_t i = 0; i < currentCount; ++i) {
  485. /* Get the node */
  486. const UA_Node *node = UA_NodeStore_get(server->nodestore, &current[i]);
  487. if(!node) {
  488. /* If we cannot find the node at depth 0, the starting node does not exist */
  489. if(elemDepth == 0)
  490. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  491. continue;
  492. }
  493. /* Test whether the current node has the target name required in the
  494. * previous path element */
  495. if(targetName && (targetName->namespaceIndex != node->browseName.namespaceIndex ||
  496. !UA_String_equal(&targetName->name, &node->browseName.name)))
  497. continue;
  498. /* Walk over the references in the node */
  499. /* Loop over the nodes references */
  500. for(size_t r = 0; r < node->referencesSize &&
  501. result->statusCode == UA_STATUSCODE_GOOD; ++r) {
  502. UA_NodeReferenceKind *rk = &node->references[r];
  503. walkBrowsePathElementNodeReference(result, targetsSize, next, nextSize, nextCount,
  504. elemDepth, elem->isInverse, all_refs,
  505. reftypes, reftypes_count, rk);
  506. }
  507. }
  508. if(!all_refs && elem->includeSubtypes)
  509. UA_Array_delete(reftypes, reftypes_count, &UA_TYPES[UA_TYPES_NODEID]);
  510. }
  511. /* This assumes that result->targets has enough room for all currentCount elements */
  512. static void
  513. addBrowsePathTargets(UA_Server *server, UA_Session *session, UA_BrowsePathResult *result,
  514. const UA_QualifiedName *targetName, UA_NodeId *current, size_t currentCount) {
  515. for(size_t i = 0; i < currentCount; i++) {
  516. /* Get the node */
  517. const UA_Node *node = UA_NodeStore_get(server->nodestore, &current[i]);
  518. if(!node) {
  519. UA_NodeId_deleteMembers(&current[i]);
  520. continue;
  521. }
  522. /* Test whether the current node has the target name required in the
  523. * previous path element */
  524. if(targetName->namespaceIndex != node->browseName.namespaceIndex ||
  525. !UA_String_equal(&targetName->name, &node->browseName.name)) {
  526. UA_NodeId_deleteMembers(&current[i]);
  527. continue;
  528. }
  529. /* Move the nodeid to the target array */
  530. UA_BrowsePathTarget_init(&result->targets[result->targetsSize]);
  531. result->targets[result->targetsSize].targetId.nodeId = current[i];
  532. result->targets[result->targetsSize].remainingPathIndex = UA_UINT32_MAX;
  533. ++result->targetsSize;
  534. }
  535. }
  536. static void
  537. walkBrowsePath(UA_Server *server, UA_Session *session, const UA_BrowsePath *path,
  538. UA_BrowsePathResult *result, size_t targetsSize,
  539. UA_NodeId **current, size_t *currentSize, size_t *currentCount,
  540. UA_NodeId **next, size_t *nextSize, size_t *nextCount) {
  541. UA_assert(*currentCount == 1);
  542. UA_assert(*nextCount == 0);
  543. /* Points to the targetName of the _previous_ path element */
  544. const UA_QualifiedName *targetName = NULL;
  545. /* Iterate over path elements */
  546. UA_assert(path->relativePath.elementsSize > 0);
  547. for(UA_UInt32 i = 0; i < path->relativePath.elementsSize; ++i) {
  548. walkBrowsePathElement(server, session, result, &targetsSize,
  549. &path->relativePath.elements[i], i, targetName,
  550. *current, *currentCount, next, nextSize, nextCount);
  551. /* Clean members of current */
  552. for(size_t j = 0; j < *currentCount; j++)
  553. UA_NodeId_deleteMembers(&(*current)[j]);
  554. *currentCount = 0;
  555. /* When no targets are left or an error occurred. None of next's
  556. * elements will be copied to result->targets */
  557. if(*nextCount == 0 || result->statusCode != UA_STATUSCODE_GOOD) {
  558. UA_assert(*currentCount == 0);
  559. UA_assert(*nextCount == 0);
  560. return;
  561. }
  562. /* Exchange current and next for the next depth */
  563. size_t tSize = *currentSize; size_t tCount = *currentCount; UA_NodeId *tT = *current;
  564. *currentSize = *nextSize; *currentCount = *nextCount; *current = *next;
  565. *nextSize = tSize; *nextCount = tCount; *next = tT;
  566. /* Store the target name of the previous path element */
  567. targetName = &path->relativePath.elements[i].targetName;
  568. }
  569. UA_assert(targetName != NULL);
  570. UA_assert(*nextCount == 0);
  571. /* After the last BrowsePathElement, move members from current to the
  572. * result targets */
  573. /* Realloc if more space is needed */
  574. if(targetsSize < result->targetsSize + (*currentCount)) {
  575. UA_BrowsePathTarget *newTargets =
  576. (UA_BrowsePathTarget*)UA_realloc(result->targets, sizeof(UA_BrowsePathTarget) *
  577. (result->targetsSize + (*currentCount)));
  578. if(!newTargets) {
  579. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  580. for(size_t i = 0; i < *currentCount; ++i)
  581. UA_NodeId_deleteMembers(&(*current)[i]);
  582. *currentCount = 0;
  583. return;
  584. }
  585. result->targets = newTargets;
  586. }
  587. /* Move the elements of current to the targets */
  588. addBrowsePathTargets(server, session, result, targetName, *current, *currentCount);
  589. *currentCount = 0;
  590. }
  591. static void
  592. translateBrowsePathToNodeIds(UA_Server *server, UA_Session *session,
  593. const UA_BrowsePath *path, UA_BrowsePathResult *result) {
  594. if(path->relativePath.elementsSize <= 0) {
  595. result->statusCode = UA_STATUSCODE_BADNOTHINGTODO;
  596. return;
  597. }
  598. /* RelativePath elements must not have an empty targetName */
  599. for(size_t i = 0; i < path->relativePath.elementsSize; ++i) {
  600. if(UA_QualifiedName_isNull(&path->relativePath.elements[i].targetName)) {
  601. result->statusCode = UA_STATUSCODE_BADBROWSENAMEINVALID;
  602. return;
  603. }
  604. }
  605. /* Allocate memory for the targets */
  606. size_t targetsSize = 10; /* When to realloc; the member count is stored in
  607. * result->targetsSize */
  608. result->targets = (UA_BrowsePathTarget*)UA_malloc(sizeof(UA_BrowsePathTarget) * targetsSize);
  609. if(!result->targets) {
  610. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  611. return;
  612. }
  613. /* Allocate memory for two temporary arrays. One with the results for the
  614. * previous depth of the path. The other for the new results at the current
  615. * depth. The two arrays alternate as we descend down the tree. */
  616. size_t currentSize = 10; /* When to realloc */
  617. size_t currentCount = 0; /* Current elements */
  618. UA_NodeId *current = (UA_NodeId*)UA_malloc(sizeof(UA_NodeId) * currentSize);
  619. if(!current) {
  620. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  621. UA_free(result->targets);
  622. return;
  623. }
  624. size_t nextSize = 10; /* When to realloc */
  625. size_t nextCount = 0; /* Current elements */
  626. UA_NodeId *next = (UA_NodeId*)UA_malloc(sizeof(UA_NodeId) * nextSize);
  627. if(!next) {
  628. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  629. UA_free(result->targets);
  630. UA_free(current);
  631. return;
  632. }
  633. /* Copy the starting node into current */
  634. result->statusCode = UA_NodeId_copy(&path->startingNode, &current[0]);
  635. if(result->statusCode != UA_STATUSCODE_GOOD) {
  636. UA_free(result->targets);
  637. UA_free(current);
  638. UA_free(next);
  639. return;
  640. }
  641. currentCount = 1;
  642. /* Walk the path elements */
  643. walkBrowsePath(server, session, path, result, targetsSize,
  644. &current, &currentSize, &currentCount,
  645. &next, &nextSize, &nextCount);
  646. UA_assert(currentCount == 0);
  647. UA_assert(nextCount == 0);
  648. /* No results => BadNoMatch status code */
  649. if(result->targetsSize == 0 && result->statusCode == UA_STATUSCODE_GOOD)
  650. result->statusCode = UA_STATUSCODE_BADNOMATCH;
  651. /* Clean up the temporary arrays and the targets */
  652. UA_free(current);
  653. UA_free(next);
  654. if(result->statusCode != UA_STATUSCODE_GOOD) {
  655. for(size_t i = 0; i < result->targetsSize; ++i)
  656. UA_BrowsePathTarget_deleteMembers(&result->targets[i]);
  657. UA_free(result->targets);
  658. result->targets = NULL;
  659. result->targetsSize = 0;
  660. }
  661. }
  662. UA_BrowsePathResult
  663. UA_Server_translateBrowsePathToNodeIds(UA_Server *server,
  664. const UA_BrowsePath *browsePath) {
  665. UA_BrowsePathResult result;
  666. UA_BrowsePathResult_init(&result);
  667. UA_RCU_LOCK();
  668. translateBrowsePathToNodeIds(server, &adminSession, browsePath, &result);
  669. UA_RCU_UNLOCK();
  670. return result;
  671. }
  672. void Service_TranslateBrowsePathsToNodeIds(UA_Server *server, UA_Session *session,
  673. const UA_TranslateBrowsePathsToNodeIdsRequest *request,
  674. UA_TranslateBrowsePathsToNodeIdsResponse *response) {
  675. UA_LOG_DEBUG_SESSION(server->config.logger, session, "Processing TranslateBrowsePathsToNodeIdsRequest");
  676. if(request->browsePathsSize <= 0) {
  677. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  678. return;
  679. }
  680. size_t size = request->browsePathsSize;
  681. response->results = (UA_BrowsePathResult *)UA_Array_new(size, &UA_TYPES[UA_TYPES_BROWSEPATHRESULT]);
  682. if(!response->results) {
  683. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  684. return;
  685. }
  686. #ifdef UA_ENABLE_EXTERNAL_NAMESPACES
  687. #ifdef NO_ALLOCA
  688. UA_Boolean isExternal[size];
  689. UA_UInt32 indices[size];
  690. #else
  691. UA_Boolean *isExternal = UA_alloca(sizeof(UA_Boolean) * size);
  692. UA_UInt32 *indices = UA_alloca(sizeof(UA_UInt32) * size);
  693. #endif /* NO_ALLOCA */
  694. memset(isExternal, false, sizeof(UA_Boolean) * size);
  695. for(size_t j = 0; j < server->externalNamespacesSize; ++j) {
  696. size_t indexSize = 0;
  697. for(size_t i = 0;i < size;++i) {
  698. if(request->browsePaths[i].startingNode.namespaceIndex != server->externalNamespaces[j].index)
  699. continue;
  700. isExternal[i] = true;
  701. indices[indexSize] = (UA_UInt32)i;
  702. ++indexSize;
  703. }
  704. if(indexSize == 0)
  705. continue;
  706. UA_ExternalNodeStore *ens = &server->externalNamespaces[j].externalNodeStore;
  707. ens->translateBrowsePathsToNodeIds(ens->ensHandle, &request->requestHeader, request->browsePaths,
  708. indices, (UA_UInt32)indexSize, response->results,
  709. response->diagnosticInfos);
  710. }
  711. response->resultsSize = size;
  712. for(size_t i = 0; i < size; ++i) {
  713. if(!isExternal[i])
  714. translateBrowsePathToNodeIds(server, session, &request->browsePaths[i],
  715. &response->results[i]);
  716. }
  717. #else
  718. response->resultsSize = size;
  719. for(size_t i = 0; i < size; ++i)
  720. translateBrowsePathToNodeIds(server, session, &request->browsePaths[i],
  721. &response->results[i]);
  722. #endif
  723. }
  724. void Service_RegisterNodes(UA_Server *server, UA_Session *session, const UA_RegisterNodesRequest *request,
  725. UA_RegisterNodesResponse *response) {
  726. UA_LOG_DEBUG_SESSION(server->config.logger, session, "Processing RegisterNodesRequest");
  727. //TODO: hang the nodeids to the session if really needed
  728. response->responseHeader.timestamp = UA_DateTime_now();
  729. if(request->nodesToRegisterSize <= 0)
  730. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  731. else {
  732. response->responseHeader.serviceResult =
  733. UA_Array_copy(request->nodesToRegister, request->nodesToRegisterSize,
  734. (void**)&response->registeredNodeIds, &UA_TYPES[UA_TYPES_NODEID]);
  735. if(response->responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  736. response->registeredNodeIdsSize = request->nodesToRegisterSize;
  737. }
  738. }
  739. void Service_UnregisterNodes(UA_Server *server, UA_Session *session, const UA_UnregisterNodesRequest *request,
  740. UA_UnregisterNodesResponse *response) {
  741. UA_LOG_DEBUG_SESSION(server->config.logger, session, "Processing UnRegisterNodesRequest");
  742. //TODO: remove the nodeids from the session if really needed
  743. response->responseHeader.timestamp = UA_DateTime_now();
  744. if(request->nodesToUnregisterSize==0)
  745. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  746. }