ua_services_view.c 32 KB

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