ua_services_view.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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, const 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. if(server->config.maxNodesPerBrowse != 0 &&
  282. request->nodesToBrowseSize > server->config.maxNodesPerBrowse) {
  283. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  284. return;
  285. }
  286. /* No views supported at the moment */
  287. if(!UA_NodeId_isNull(&request->view.viewId)) {
  288. response->responseHeader.serviceResult = UA_STATUSCODE_BADVIEWIDUNKNOWN;
  289. return;
  290. }
  291. UA_UInt32 requestedMaxReferencesPerNode = request->requestedMaxReferencesPerNode;
  292. response->responseHeader.serviceResult =
  293. UA_Server_processServiceOperations(server, session, (UA_ServiceOperation)Operation_Browse,
  294. &requestedMaxReferencesPerNode,
  295. &request->nodesToBrowseSize, &UA_TYPES[UA_TYPES_BROWSEDESCRIPTION],
  296. &response->resultsSize, &UA_TYPES[UA_TYPES_BROWSERESULT]);
  297. }
  298. UA_BrowseResult
  299. UA_Server_browse(UA_Server *server, UA_UInt32 maxrefs, const UA_BrowseDescription *descr) {
  300. UA_BrowseResult result;
  301. UA_BrowseResult_init(&result);
  302. Operation_Browse(server, &server->adminSession, &maxrefs, descr, &result);
  303. return result;
  304. }
  305. static void
  306. Operation_BrowseNext(UA_Server *server, UA_Session *session, const UA_Boolean *releaseContinuationPoints,
  307. const UA_ByteString *continuationPoint, UA_BrowseResult *result) {
  308. /* Find the continuation point */
  309. ContinuationPointEntry *cp;
  310. LIST_FOREACH(cp, &session->continuationPoints, pointers) {
  311. if(UA_ByteString_equal(&cp->identifier, continuationPoint))
  312. break;
  313. }
  314. if(!cp) {
  315. result->statusCode = UA_STATUSCODE_BADCONTINUATIONPOINTINVALID;
  316. return;
  317. }
  318. /* Remove the cp */
  319. if(*releaseContinuationPoints) {
  320. removeCp(cp, session);
  321. return;
  322. }
  323. /* Continue browsing */
  324. UA_Boolean done = browseWithContinuation(server, session, cp, result);
  325. if(done) {
  326. /* Remove the cp if there are no references left */
  327. removeCp(cp, session);
  328. } else {
  329. /* Return the cp identifier */
  330. UA_StatusCode retval = UA_ByteString_copy(&cp->identifier, &result->continuationPoint);
  331. if(retval != UA_STATUSCODE_GOOD) {
  332. UA_BrowseResult_deleteMembers(result);
  333. result->statusCode = retval;
  334. }
  335. }
  336. }
  337. void
  338. Service_BrowseNext(UA_Server *server, UA_Session *session,
  339. const UA_BrowseNextRequest *request,
  340. UA_BrowseNextResponse *response) {
  341. UA_LOG_DEBUG_SESSION(&server->config.logger, session,
  342. "Processing BrowseNextRequest");
  343. UA_Boolean releaseContinuationPoints = request->releaseContinuationPoints; /* request is const */
  344. response->responseHeader.serviceResult =
  345. UA_Server_processServiceOperations(server, session, (UA_ServiceOperation)Operation_BrowseNext,
  346. &releaseContinuationPoints,
  347. &request->continuationPointsSize, &UA_TYPES[UA_TYPES_BYTESTRING],
  348. &response->resultsSize, &UA_TYPES[UA_TYPES_BROWSERESULT]);
  349. }
  350. UA_BrowseResult
  351. UA_Server_browseNext(UA_Server *server, UA_Boolean releaseContinuationPoint,
  352. const UA_ByteString *continuationPoint) {
  353. UA_BrowseResult result;
  354. UA_BrowseResult_init(&result);
  355. Operation_BrowseNext(server, &server->adminSession, &releaseContinuationPoint,
  356. continuationPoint, &result);
  357. return result;
  358. }
  359. /***********************/
  360. /* TranslateBrowsePath */
  361. /***********************/
  362. static void
  363. walkBrowsePathElementReferenceTargets(UA_BrowsePathResult *result, size_t *targetsSize,
  364. UA_NodeId **next, size_t *nextSize, size_t *nextCount,
  365. UA_UInt32 elemDepth, const UA_NodeReferenceKind *rk) {
  366. /* Loop over the targets */
  367. for(size_t i = 0; i < rk->targetIdsSize; i++) {
  368. UA_ExpandedNodeId *targetId = &rk->targetIds[i];
  369. /* Does the reference point to an external server? Then add to the
  370. * targets with the right path depth. */
  371. if(targetId->serverIndex != 0) {
  372. UA_BrowsePathTarget *tempTargets =
  373. (UA_BrowsePathTarget*)UA_realloc(result->targets,
  374. sizeof(UA_BrowsePathTarget) * (*targetsSize) * 2);
  375. if(!tempTargets) {
  376. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  377. return;
  378. }
  379. result->targets = tempTargets;
  380. (*targetsSize) *= 2;
  381. result->statusCode = UA_ExpandedNodeId_copy(targetId,
  382. &result->targets[result->targetsSize].targetId);
  383. result->targets[result->targetsSize].remainingPathIndex = elemDepth;
  384. continue;
  385. }
  386. /* Can we store the node in the array of candidates for deep-search? */
  387. if(*nextSize <= *nextCount) {
  388. UA_NodeId *tempNext =
  389. (UA_NodeId*)UA_realloc(*next, sizeof(UA_NodeId) * (*nextSize) * 2);
  390. if(!tempNext) {
  391. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  392. return;
  393. }
  394. *next = tempNext;
  395. (*nextSize) *= 2;
  396. }
  397. /* Add the node to the next array for the following path element */
  398. result->statusCode = UA_NodeId_copy(&targetId->nodeId,
  399. &(*next)[*nextCount]);
  400. if(result->statusCode != UA_STATUSCODE_GOOD)
  401. return;
  402. ++(*nextCount);
  403. }
  404. }
  405. static void
  406. walkBrowsePathElement(UA_Server *server, UA_Session *session, UA_UInt32 nodeClassMask,
  407. UA_BrowsePathResult *result, size_t *targetsSize,
  408. const UA_RelativePathElement *elem, UA_UInt32 elemDepth,
  409. const UA_QualifiedName *targetName,
  410. const UA_NodeId *current, const size_t currentCount,
  411. UA_NodeId **next, size_t *nextSize, size_t *nextCount) {
  412. /* Return all references? */
  413. UA_Boolean all_refs = UA_NodeId_isNull(&elem->referenceTypeId);
  414. if(!all_refs) {
  415. const UA_Node *rootRef = UA_Nodestore_get(server, &elem->referenceTypeId);
  416. if(!rootRef)
  417. return;
  418. UA_Boolean match = (rootRef->nodeClass == UA_NODECLASS_REFERENCETYPE);
  419. UA_Nodestore_release(server, rootRef);
  420. if(!match)
  421. return;
  422. }
  423. /* Iterate over all nodes at the current depth-level */
  424. for(size_t i = 0; i < currentCount; ++i) {
  425. /* Get the node */
  426. const UA_Node *node = UA_Nodestore_get(server, &current[i]);
  427. if(!node) {
  428. /* If we cannot find the node at depth 0, the starting node does not exist */
  429. if(elemDepth == 0)
  430. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  431. continue;
  432. }
  433. /* Test whether the node fits the class mask */
  434. if(!matchClassMask(node, nodeClassMask)) {
  435. UA_Nodestore_release(server, node);
  436. continue;
  437. }
  438. /* Test whether the node has the target name required in the previous
  439. * path element */
  440. if(targetName && (targetName->namespaceIndex != node->browseName.namespaceIndex ||
  441. !UA_String_equal(&targetName->name, &node->browseName.name))) {
  442. UA_Nodestore_release(server, node);
  443. continue;
  444. }
  445. /* Loop over the nodes references */
  446. for(size_t r = 0; r < node->referencesSize &&
  447. result->statusCode == UA_STATUSCODE_GOOD; ++r) {
  448. UA_NodeReferenceKind *rk = &node->references[r];
  449. /* Does the direction of the reference match? */
  450. if(rk->isInverse != elem->isInverse)
  451. continue;
  452. /* Is the node relevant? */
  453. if(!all_refs && !relevantReference(server, elem->includeSubtypes,
  454. &elem->referenceTypeId, &rk->referenceTypeId))
  455. continue;
  456. /* Walk over the reference targets */
  457. walkBrowsePathElementReferenceTargets(result, targetsSize, next, nextSize,
  458. nextCount, elemDepth, rk);
  459. }
  460. UA_Nodestore_release(server, node);
  461. }
  462. }
  463. /* This assumes that result->targets has enough room for all currentCount elements */
  464. static void
  465. addBrowsePathTargets(UA_Server *server, UA_Session *session, UA_UInt32 nodeClassMask,
  466. UA_BrowsePathResult *result, const UA_QualifiedName *targetName,
  467. UA_NodeId *current, size_t currentCount) {
  468. for(size_t i = 0; i < currentCount; i++) {
  469. const UA_Node *node = UA_Nodestore_get(server, &current[i]);
  470. if(!node) {
  471. UA_NodeId_deleteMembers(&current[i]);
  472. continue;
  473. }
  474. /* Test whether the node fits the class mask */
  475. UA_Boolean skip = !matchClassMask(node, nodeClassMask);
  476. /* Test whether the node has the target name required in the
  477. * previous path element */
  478. if(targetName->namespaceIndex != node->browseName.namespaceIndex ||
  479. !UA_String_equal(&targetName->name, &node->browseName.name))
  480. skip = true;
  481. UA_Nodestore_release(server, node);
  482. if(skip) {
  483. UA_NodeId_deleteMembers(&current[i]);
  484. continue;
  485. }
  486. /* Move the nodeid to the target array */
  487. UA_BrowsePathTarget_init(&result->targets[result->targetsSize]);
  488. result->targets[result->targetsSize].targetId.nodeId = current[i];
  489. result->targets[result->targetsSize].remainingPathIndex = UA_UINT32_MAX;
  490. ++result->targetsSize;
  491. }
  492. }
  493. static void
  494. walkBrowsePath(UA_Server *server, UA_Session *session, const UA_BrowsePath *path,
  495. UA_UInt32 nodeClassMask, UA_BrowsePathResult *result, size_t targetsSize,
  496. UA_NodeId **current, size_t *currentSize, size_t *currentCount,
  497. UA_NodeId **next, size_t *nextSize, size_t *nextCount) {
  498. UA_assert(*currentCount == 1);
  499. UA_assert(*nextCount == 0);
  500. /* Points to the targetName of the _previous_ path element */
  501. const UA_QualifiedName *targetName = NULL;
  502. /* Iterate over path elements */
  503. UA_assert(path->relativePath.elementsSize > 0);
  504. for(UA_UInt32 i = 0; i < path->relativePath.elementsSize; ++i) {
  505. walkBrowsePathElement(server, session, nodeClassMask, result, &targetsSize,
  506. &path->relativePath.elements[i], i, targetName,
  507. *current, *currentCount, next, nextSize, nextCount);
  508. /* Clean members of current */
  509. for(size_t j = 0; j < *currentCount; j++)
  510. UA_NodeId_deleteMembers(&(*current)[j]);
  511. *currentCount = 0;
  512. /* When no targets are left or an error occurred. None of next's
  513. * elements will be copied to result->targets */
  514. if(*nextCount == 0 || result->statusCode != UA_STATUSCODE_GOOD) {
  515. UA_assert(*currentCount == 0);
  516. UA_assert(*nextCount == 0);
  517. return;
  518. }
  519. /* Exchange current and next for the next depth */
  520. size_t tSize = *currentSize; size_t tCount = *currentCount; UA_NodeId *tT = *current;
  521. *currentSize = *nextSize; *currentCount = *nextCount; *current = *next;
  522. *nextSize = tSize; *nextCount = tCount; *next = tT;
  523. /* Store the target name of the previous path element */
  524. targetName = &path->relativePath.elements[i].targetName;
  525. }
  526. UA_assert(targetName != NULL);
  527. UA_assert(*nextCount == 0);
  528. /* After the last BrowsePathElement, move members from current to the
  529. * result targets */
  530. /* Realloc if more space is needed */
  531. if(targetsSize < result->targetsSize + (*currentCount)) {
  532. UA_BrowsePathTarget *newTargets =
  533. (UA_BrowsePathTarget*)UA_realloc(result->targets, sizeof(UA_BrowsePathTarget) *
  534. (result->targetsSize + (*currentCount)));
  535. if(!newTargets) {
  536. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  537. for(size_t i = 0; i < *currentCount; ++i)
  538. UA_NodeId_deleteMembers(&(*current)[i]);
  539. *currentCount = 0;
  540. return;
  541. }
  542. result->targets = newTargets;
  543. }
  544. /* Move the elements of current to the targets */
  545. addBrowsePathTargets(server, session, nodeClassMask, result, targetName, *current, *currentCount);
  546. *currentCount = 0;
  547. }
  548. static void
  549. Operation_TranslateBrowsePathToNodeIds(UA_Server *server, UA_Session *session,
  550. const UA_UInt32 *nodeClassMask, const UA_BrowsePath *path,
  551. UA_BrowsePathResult *result) {
  552. if(path->relativePath.elementsSize <= 0) {
  553. result->statusCode = UA_STATUSCODE_BADNOTHINGTODO;
  554. return;
  555. }
  556. /* RelativePath elements must not have an empty targetName */
  557. for(size_t i = 0; i < path->relativePath.elementsSize; ++i) {
  558. if(UA_QualifiedName_isNull(&path->relativePath.elements[i].targetName)) {
  559. result->statusCode = UA_STATUSCODE_BADBROWSENAMEINVALID;
  560. return;
  561. }
  562. }
  563. /* Allocate memory for the targets */
  564. size_t targetsSize = 10; /* When to realloc; the member count is stored in
  565. * result->targetsSize */
  566. result->targets =
  567. (UA_BrowsePathTarget*)UA_malloc(sizeof(UA_BrowsePathTarget) * targetsSize);
  568. if(!result->targets) {
  569. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  570. return;
  571. }
  572. /* Allocate memory for two temporary arrays. One with the results for the
  573. * previous depth of the path. The other for the new results at the current
  574. * depth. The two arrays alternate as we descend down the tree. */
  575. size_t currentSize = 10; /* When to realloc */
  576. size_t currentCount = 0; /* Current elements */
  577. UA_NodeId *current = (UA_NodeId*)UA_malloc(sizeof(UA_NodeId) * currentSize);
  578. if(!current) {
  579. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  580. UA_free(result->targets);
  581. return;
  582. }
  583. size_t nextSize = 10; /* When to realloc */
  584. size_t nextCount = 0; /* Current elements */
  585. UA_NodeId *next = (UA_NodeId*)UA_malloc(sizeof(UA_NodeId) * nextSize);
  586. if(!next) {
  587. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  588. UA_free(result->targets);
  589. UA_free(current);
  590. return;
  591. }
  592. /* Copy the starting node into current */
  593. result->statusCode = UA_NodeId_copy(&path->startingNode, &current[0]);
  594. if(result->statusCode != UA_STATUSCODE_GOOD) {
  595. UA_free(result->targets);
  596. UA_free(current);
  597. UA_free(next);
  598. return;
  599. }
  600. currentCount = 1;
  601. /* Walk the path elements */
  602. walkBrowsePath(server, session, path, *nodeClassMask, result, targetsSize,
  603. &current, &currentSize, &currentCount,
  604. &next, &nextSize, &nextCount);
  605. UA_assert(currentCount == 0);
  606. UA_assert(nextCount == 0);
  607. /* No results => BadNoMatch status code */
  608. if(result->targetsSize == 0 && result->statusCode == UA_STATUSCODE_GOOD)
  609. result->statusCode = UA_STATUSCODE_BADNOMATCH;
  610. /* Clean up the temporary arrays and the targets */
  611. UA_free(current);
  612. UA_free(next);
  613. if(result->statusCode != UA_STATUSCODE_GOOD) {
  614. for(size_t i = 0; i < result->targetsSize; ++i)
  615. UA_BrowsePathTarget_deleteMembers(&result->targets[i]);
  616. UA_free(result->targets);
  617. result->targets = NULL;
  618. result->targetsSize = 0;
  619. }
  620. }
  621. UA_BrowsePathResult
  622. UA_Server_translateBrowsePathToNodeIds(UA_Server *server,
  623. const UA_BrowsePath *browsePath) {
  624. UA_BrowsePathResult result;
  625. UA_BrowsePathResult_init(&result);
  626. UA_UInt32 nodeClassMask = 0; /* All node classes */
  627. Operation_TranslateBrowsePathToNodeIds(server, &server->adminSession, &nodeClassMask,
  628. browsePath, &result);
  629. return result;
  630. }
  631. void
  632. Service_TranslateBrowsePathsToNodeIds(UA_Server *server, UA_Session *session,
  633. const UA_TranslateBrowsePathsToNodeIdsRequest *request,
  634. UA_TranslateBrowsePathsToNodeIdsResponse *response) {
  635. UA_LOG_DEBUG_SESSION(&server->config.logger, session,
  636. "Processing TranslateBrowsePathsToNodeIdsRequest");
  637. if(server->config.maxNodesPerTranslateBrowsePathsToNodeIds != 0 &&
  638. request->browsePathsSize > server->config.maxNodesPerTranslateBrowsePathsToNodeIds) {
  639. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  640. return;
  641. }
  642. UA_UInt32 nodeClassMask = 0; /* All node classes */
  643. response->responseHeader.serviceResult =
  644. UA_Server_processServiceOperations(server, session,
  645. (UA_ServiceOperation)Operation_TranslateBrowsePathToNodeIds,
  646. &nodeClassMask,
  647. &request->browsePathsSize, &UA_TYPES[UA_TYPES_BROWSEPATH],
  648. &response->resultsSize, &UA_TYPES[UA_TYPES_BROWSEPATHRESULT]);
  649. }
  650. UA_BrowsePathResult
  651. UA_Server_browseSimplifiedBrowsePath(UA_Server *server, const UA_NodeId origin,
  652. size_t browsePathSize, const UA_QualifiedName *browsePath) {
  653. /* Construct the BrowsePath */
  654. UA_BrowsePath bp;
  655. UA_BrowsePath_init(&bp);
  656. bp.startingNode = origin;
  657. UA_STACKARRAY(UA_RelativePathElement, rpe, browsePathSize);
  658. memset(rpe, 0, sizeof(UA_RelativePathElement) * browsePathSize);
  659. for(size_t j = 0; j < browsePathSize; j++) {
  660. rpe[j].referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HIERARCHICALREFERENCES);
  661. rpe[j].includeSubtypes = true;
  662. rpe[j].targetName = browsePath[j];
  663. }
  664. bp.relativePath.elements = rpe;
  665. bp.relativePath.elementsSize = browsePathSize;
  666. /* Browse */
  667. UA_BrowsePathResult bpr;
  668. UA_BrowsePathResult_init(&bpr);
  669. UA_UInt32 nodeClassMask = UA_NODECLASS_OBJECT | UA_NODECLASS_VARIABLE;
  670. Operation_TranslateBrowsePathToNodeIds(server, &server->adminSession, &nodeClassMask, &bp, &bpr);
  671. return bpr;
  672. }
  673. /************/
  674. /* Register */
  675. /************/
  676. void Service_RegisterNodes(UA_Server *server, UA_Session *session,
  677. const UA_RegisterNodesRequest *request,
  678. UA_RegisterNodesResponse *response) {
  679. UA_LOG_DEBUG_SESSION(&server->config.logger, session,
  680. "Processing RegisterNodesRequest");
  681. //TODO: hang the nodeids to the session if really needed
  682. if(request->nodesToRegisterSize == 0) {
  683. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  684. return;
  685. }
  686. if(server->config.maxNodesPerRegisterNodes != 0 &&
  687. request->nodesToRegisterSize > server->config.maxNodesPerRegisterNodes) {
  688. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  689. return;
  690. }
  691. response->responseHeader.serviceResult =
  692. UA_Array_copy(request->nodesToRegister, request->nodesToRegisterSize,
  693. (void**)&response->registeredNodeIds, &UA_TYPES[UA_TYPES_NODEID]);
  694. if(response->responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  695. response->registeredNodeIdsSize = request->nodesToRegisterSize;
  696. }
  697. void Service_UnregisterNodes(UA_Server *server, UA_Session *session,
  698. const UA_UnregisterNodesRequest *request,
  699. UA_UnregisterNodesResponse *response) {
  700. UA_LOG_DEBUG_SESSION(&server->config.logger, session,
  701. "Processing UnRegisterNodesRequest");
  702. //TODO: remove the nodeids from the session if really needed
  703. if(request->nodesToUnregisterSize == 0)
  704. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  705. if(server->config.maxNodesPerRegisterNodes != 0 &&
  706. request->nodesToUnregisterSize > server->config.maxNodesPerRegisterNodes) {
  707. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  708. return;
  709. }
  710. }