ua_services_view.c 32 KB

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