ua_services_view.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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-2019 (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. #include "ziptree.h"
  21. /***********/
  22. /* RefTree */
  23. /***********/
  24. /* References are browsed with a minimum number of copy operations. A RefTree
  25. * holds a single array for both the NodeIds encountered during (recursive)
  26. * browsing and the entries for a tree-structure to check for duplicates. Once
  27. * the (recursive) browse has finished, the tree-structure part can be simply
  28. * cut away. A single realloc operation (with some pointer repairing) can be
  29. * used to increase the capacity of the RefTree.
  30. *
  31. * If an ExpandedNodeId is encountered, it has to be processed right away.
  32. * Remote ExpandedNodeId are not put into the tree, since it is not possible to
  33. * recurse into them anyway.
  34. *
  35. * The layout of the results array is as follows:
  36. *
  37. * | Targets [NodeId] | Tree [RefEntry] | */
  38. #define UA_BROWSE_INITIAL_SIZE 16
  39. typedef struct RefEntry {
  40. ZIP_ENTRY(RefEntry) zipfields;
  41. const UA_NodeId *target;
  42. UA_UInt32 targetHash; /* Hash of the target nodeid */
  43. } RefEntry;
  44. static enum ZIP_CMP
  45. cmpTarget(const void *a, const void *b) {
  46. const RefEntry *aa = (const RefEntry*)a;
  47. const RefEntry *bb = (const RefEntry*)b;
  48. if(aa->targetHash < bb->targetHash)
  49. return ZIP_CMP_LESS;
  50. if(aa->targetHash > bb->targetHash)
  51. return ZIP_CMP_MORE;
  52. return (enum ZIP_CMP)UA_NodeId_order(aa->target, bb->target);
  53. }
  54. ZIP_HEAD(RefHead, RefEntry);
  55. typedef struct RefHead RefHead;
  56. ZIP_PROTTYPE(RefHead, RefEntry, RefEntry)
  57. ZIP_IMPL(RefHead, RefEntry, zipfields, RefEntry, zipfields, cmpTarget)
  58. typedef struct {
  59. UA_NodeId *targets;
  60. RefHead head;
  61. size_t capacity; /* available space */
  62. size_t size; /* used space */
  63. } RefTree;
  64. static UA_StatusCode UA_FUNC_ATTR_WARN_UNUSED_RESULT
  65. RefTree_init(RefTree *rt) {
  66. size_t space = (sizeof(UA_NodeId) + sizeof(RefEntry)) * UA_BROWSE_INITIAL_SIZE;
  67. rt->targets = (UA_NodeId*)UA_malloc(space);
  68. if(!rt->targets)
  69. return UA_STATUSCODE_BADOUTOFMEMORY;
  70. rt->capacity = UA_BROWSE_INITIAL_SIZE;
  71. rt->size = 0;
  72. ZIP_INIT(&rt->head);
  73. return UA_STATUSCODE_GOOD;
  74. }
  75. static
  76. void RefTree_clear(RefTree *rt) {
  77. for(size_t i = 0; i < rt->size; i++)
  78. UA_NodeId_deleteMembers(&rt->targets[i]);
  79. UA_free(rt->targets);
  80. }
  81. /* Double the capacity of the reftree */
  82. static UA_StatusCode UA_FUNC_ATTR_WARN_UNUSED_RESULT
  83. RefTree_double(RefTree *rt) {
  84. size_t capacity = rt->capacity * 2;
  85. UA_assert(capacity > 0);
  86. size_t space = (sizeof(UA_NodeId) + sizeof(RefEntry)) * capacity;
  87. UA_NodeId *newTargets = (UA_NodeId*)UA_realloc(rt->targets, space);
  88. if(!newTargets)
  89. return UA_STATUSCODE_BADOUTOFMEMORY;
  90. /* Repair the pointers for the realloced array+tree */
  91. uintptr_t arraydiff = (uintptr_t)newTargets - (uintptr_t)rt->targets;
  92. RefEntry *reArray = (RefEntry*)
  93. ((uintptr_t)newTargets + (capacity * sizeof(UA_NodeId)));
  94. uintptr_t entrydiff = (uintptr_t)reArray -
  95. ((uintptr_t)rt->targets + (rt->capacity * sizeof(UA_NodeId)));
  96. RefEntry *oldReArray = (RefEntry*)
  97. ((uintptr_t)newTargets + (rt->capacity * sizeof(UA_NodeId)));
  98. memmove(reArray, oldReArray, rt->size * sizeof(RefEntry));
  99. for(size_t i = 0; i < rt->size; i++) {
  100. if(reArray[i].zipfields.zip_left)
  101. *(uintptr_t*)&reArray[i].zipfields.zip_left += entrydiff;
  102. if(reArray[i].zipfields.zip_right)
  103. *(uintptr_t*)&reArray[i].zipfields.zip_right += entrydiff;
  104. *(uintptr_t*)&reArray[i].target += arraydiff;
  105. }
  106. rt->head.zip_root = (RefEntry*)((uintptr_t)rt->head.zip_root + entrydiff);
  107. rt->capacity = capacity;
  108. rt->targets = newTargets;
  109. return UA_STATUSCODE_GOOD;
  110. }
  111. static UA_StatusCode UA_FUNC_ATTR_WARN_UNUSED_RESULT
  112. RefTree_add(RefTree *rt, const UA_NodeId *target) {
  113. UA_StatusCode s = UA_STATUSCODE_GOOD;
  114. if(rt->capacity <= rt->size) {
  115. s = RefTree_double(rt);
  116. if(s != UA_STATUSCODE_GOOD)
  117. return s;
  118. }
  119. s = UA_NodeId_copy(target, &rt->targets[rt->size]);
  120. if(s != UA_STATUSCODE_GOOD)
  121. return s;
  122. RefEntry *re = (RefEntry*)((uintptr_t)rt->targets +
  123. (sizeof(UA_NodeId) * rt->capacity) +
  124. (sizeof(RefEntry) * rt->size));
  125. re->target = &rt->targets[rt->size];
  126. re->targetHash = UA_NodeId_hash(target);
  127. ZIP_INSERT(RefHead, &rt->head, re, ZIP_FFS32(UA_UInt32_random()));
  128. rt->size++;
  129. return UA_STATUSCODE_GOOD;
  130. }
  131. /*********************/
  132. /* ContinuationPoint */
  133. /*********************/
  134. struct ContinuationPoint {
  135. ContinuationPoint *next;
  136. UA_Guid identifier;
  137. UA_UInt32 maxReferences;
  138. UA_BrowseDescription bd;
  139. UA_Boolean recursive;
  140. RefTree rt;
  141. size_t n; /* Index of the node in the rt that is currently visited */
  142. size_t nk; /* Index of the ReferenceKind in the node that is visited */
  143. size_t nki; /* Index of the reference in the ReferenceKind that is visited */
  144. };
  145. static UA_StatusCode UA_FUNC_ATTR_WARN_UNUSED_RESULT
  146. ContinuationPoint_init(ContinuationPoint *cp, UA_UInt32 maxRefs,
  147. UA_Boolean recursive) {
  148. memset(cp, 0, sizeof(ContinuationPoint));
  149. cp->identifier = UA_Guid_random();
  150. cp->maxReferences = maxRefs;
  151. cp->recursive = recursive;
  152. return RefTree_init(&cp->rt);
  153. }
  154. ContinuationPoint *
  155. ContinuationPoint_clear(ContinuationPoint *cp) {
  156. UA_BrowseDescription_clear(&cp->bd);
  157. RefTree_clear(&cp->rt);
  158. return cp->next;
  159. }
  160. /**********/
  161. /* Browse */
  162. /**********/
  163. /* The RefTree structure is kept across calls to Browse(Next). The RefResult
  164. * structure is valid only for the current service call. Note that we can call
  165. * Browse internally without a RefResult. Then, only the RefTree is set up with
  166. * the target identifiers. */
  167. typedef struct {
  168. size_t size;
  169. size_t capacity;
  170. UA_ReferenceDescription *descr;
  171. } RefResult;
  172. static UA_StatusCode UA_FUNC_ATTR_WARN_UNUSED_RESULT
  173. RefResult_init(RefResult *rr, UA_UInt32 maxRefs) {
  174. UA_UInt32 initialRes = UA_BROWSE_INITIAL_SIZE;
  175. if(initialRes > maxRefs)
  176. initialRes = maxRefs;
  177. memset(rr, 0, sizeof(RefResult));
  178. rr->descr = (UA_ReferenceDescription*)
  179. UA_Array_new(initialRes, &UA_TYPES[UA_TYPES_REFERENCEDESCRIPTION]);
  180. if(!rr->descr)
  181. return UA_STATUSCODE_BADOUTOFMEMORY;
  182. rr->capacity = initialRes;
  183. rr->size = 0;
  184. return UA_STATUSCODE_GOOD;
  185. }
  186. static UA_StatusCode UA_FUNC_ATTR_WARN_UNUSED_RESULT
  187. RefResult_double(RefResult *rr, UA_UInt32 maxSize) {
  188. size_t newSize = rr->capacity * 2;
  189. if(newSize > maxSize)
  190. newSize = maxSize;
  191. UA_ReferenceDescription *rd = (UA_ReferenceDescription*)
  192. UA_realloc(rr->descr, newSize * sizeof(UA_ReferenceDescription));
  193. if(!rd)
  194. return UA_STATUSCODE_BADOUTOFMEMORY;
  195. memset(&rd[rr->size], 0, sizeof(UA_ReferenceDescription) * (newSize - rr->size));
  196. rr->descr = rd;
  197. rr->capacity = newSize;
  198. return UA_STATUSCODE_GOOD;
  199. }
  200. static void
  201. RefResult_clear(RefResult *rr) {
  202. UA_assert(rr->descr != NULL);
  203. for(size_t i = 0; i < rr->size; i++)
  204. UA_ReferenceDescription_clear(&rr->descr[i]);
  205. UA_free(rr->descr);
  206. }
  207. /* Target node on top of the stack */
  208. static UA_StatusCode UA_FUNC_ATTR_WARN_UNUSED_RESULT
  209. fillReferenceDescription(UA_Server *server, const UA_NodeReferenceKind *ref, UA_UInt32 mask,
  210. const UA_ExpandedNodeId *nodeId, const UA_Node *curr,
  211. UA_ReferenceDescription *descr) {
  212. /* Fields without access to the actual node */
  213. UA_StatusCode retval = UA_ExpandedNodeId_copy(nodeId, &descr->nodeId);
  214. if(mask & UA_BROWSERESULTMASK_REFERENCETYPEID)
  215. retval |= UA_NodeId_copy(&ref->referenceTypeId, &descr->referenceTypeId);
  216. if(mask & UA_BROWSERESULTMASK_ISFORWARD)
  217. descr->isForward = !ref->isInverse;
  218. /* Remote references (ExpandedNodeId) are not further looked up here */
  219. if(!curr)
  220. return retval;
  221. /* Fields that require the actual node */
  222. if(mask & UA_BROWSERESULTMASK_NODECLASS)
  223. retval |= UA_NodeClass_copy(&curr->nodeClass, &descr->nodeClass);
  224. if(mask & UA_BROWSERESULTMASK_BROWSENAME)
  225. retval |= UA_QualifiedName_copy(&curr->browseName, &descr->browseName);
  226. if(mask & UA_BROWSERESULTMASK_DISPLAYNAME)
  227. retval |= UA_LocalizedText_copy(&curr->displayName, &descr->displayName);
  228. if(mask & UA_BROWSERESULTMASK_TYPEDEFINITION) {
  229. if(curr->nodeClass == UA_NODECLASS_OBJECT ||
  230. curr->nodeClass == UA_NODECLASS_VARIABLE) {
  231. const UA_Node *type = getNodeType(server, curr);
  232. if(type) {
  233. retval |= UA_NodeId_copy(&type->nodeId, &descr->typeDefinition.nodeId);
  234. UA_Nodestore_releaseNode(server->nsCtx, type);
  235. }
  236. }
  237. }
  238. return retval;
  239. }
  240. static UA_Boolean
  241. relevantReference(UA_Server *server, UA_Boolean includeSubtypes,
  242. const UA_NodeId *rootRef, const UA_NodeId *testRef) {
  243. if(!includeSubtypes)
  244. return UA_NodeId_equal(rootRef, testRef);
  245. const UA_NodeId hasSubType = UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE);
  246. return isNodeInTree(server->nsCtx, testRef, rootRef, &hasSubType, 1);
  247. }
  248. static UA_Boolean
  249. matchClassMask(const UA_Node *node, UA_UInt32 nodeClassMask) {
  250. if(nodeClassMask != UA_NODECLASS_UNSPECIFIED &&
  251. (node->nodeClass & nodeClassMask) == 0)
  252. return false;
  253. return true;
  254. }
  255. static UA_StatusCode UA_FUNC_ATTR_WARN_UNUSED_RESULT
  256. browseNodeRefKind(UA_Server *server, UA_Session *session, ContinuationPoint *cp,
  257. RefResult *rr, UA_Boolean *maxed, const UA_NodeReferenceKind *rk,
  258. const UA_ExpandedNodeId *target) {
  259. /* Is the target already in the tree? */
  260. RefEntry re;
  261. re.target = &target->nodeId;
  262. re.targetHash = UA_NodeId_hash(&target->nodeId);
  263. if(ZIP_FIND(RefHead, &cp->rt.head, &re)) {
  264. cp->nki++;
  265. return UA_STATUSCODE_GOOD;
  266. }
  267. /* Get the target if it is not a remote node */
  268. const UA_Node *node = NULL;
  269. if(target->serverIndex == 0) {
  270. node = UA_Nodestore_getNode(server->nsCtx, &target->nodeId);
  271. if(node) {
  272. /* Test if the node class matches */
  273. UA_Boolean match = matchClassMask(node, cp->bd.nodeClassMask);
  274. if(!match) {
  275. UA_Nodestore_releaseNode(server->nsCtx, node);
  276. cp->nki++;
  277. return UA_STATUSCODE_GOOD;
  278. }
  279. }
  280. }
  281. /* A match! */
  282. /* Don't return detailed results if not required */
  283. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  284. if(!rr)
  285. goto cleanup;
  286. /* Increase the capacity of the results array/tree if required */
  287. if(rr->capacity <= rr->size) {
  288. retval = RefResult_double(rr, cp->maxReferences);
  289. /* Reached MaxReferences; Redo for this target */
  290. if(retval != UA_STATUSCODE_GOOD)
  291. goto cleanup;
  292. if(rr->size == rr->capacity) {
  293. *maxed = true;
  294. goto cleanup;
  295. }
  296. }
  297. /* Fill the detailed results */
  298. retval = fillReferenceDescription(server, rk, cp->bd.resultMask, target,
  299. node, &rr->descr[rr->size]);
  300. if(retval != UA_STATUSCODE_GOOD)
  301. goto cleanup;
  302. rr->size++;
  303. cleanup:
  304. if(!(*maxed) && retval == UA_STATUSCODE_GOOD) {
  305. /* Proceed to the next target */
  306. if(node)
  307. retval = RefTree_add(&cp->rt, &target->nodeId);
  308. cp->nki++;
  309. }
  310. if(node)
  311. UA_Nodestore_releaseNode(server->nsCtx, node);
  312. return retval;
  313. }
  314. static UA_StatusCode UA_FUNC_ATTR_WARN_UNUSED_RESULT
  315. browseNode(UA_Server *server, UA_Session *session,
  316. ContinuationPoint *cp, RefResult *rr, UA_Boolean *maxed,
  317. size_t referenceTypesSize, const UA_NodeId *referenceTypes,
  318. const UA_Node *node) {
  319. for(; cp->nk < node->referencesSize; cp->nk++, cp->nki = 0) {
  320. UA_NodeReferenceKind *rk = &node->references[cp->nk];
  321. /* Reference in the right direction? */
  322. if(rk->isInverse && cp->bd.browseDirection == UA_BROWSEDIRECTION_FORWARD)
  323. continue;
  324. if(!rk->isInverse && cp->bd.browseDirection == UA_BROWSEDIRECTION_INVERSE)
  325. continue;
  326. /* Is the reference part of the hierarchy of references we look for? */
  327. UA_Boolean relevant = (referenceTypes == NULL);
  328. for(size_t i = 0; i < referenceTypesSize && !relevant; i++)
  329. relevant = UA_NodeId_equal(&rk->referenceTypeId, &referenceTypes[i]);
  330. if(!relevant)
  331. continue;
  332. /* cp->nki is increaed in browseNodeRefKind upon success */
  333. for(; cp->nki < rk->targetIdsSize; ) {
  334. UA_StatusCode retval = browseNodeRefKind(server, session, cp, rr, maxed,
  335. rk, &rk->targetIds[cp->nki]);
  336. if(*maxed || retval != UA_STATUSCODE_GOOD)
  337. return retval;
  338. }
  339. }
  340. return UA_STATUSCODE_GOOD;
  341. }
  342. /* Results for a single browsedescription. This is the inner loop for both
  343. * Browse and BrowseNext. The ContinuationPoint contains all the data used.
  344. * Including the BrowseDescription. Returns whether there are remaining
  345. * references. */
  346. /* Results for a single browsedescription. Sets all NodeIds for the RefTree. */
  347. static UA_StatusCode UA_FUNC_ATTR_WARN_UNUSED_RESULT
  348. browseWithCp(UA_Server *server, UA_Session *session, ContinuationPoint *cp,
  349. RefResult *rr, UA_Boolean *maxed) {
  350. /* Is the browsedirection valid? */
  351. if(cp->bd.browseDirection != UA_BROWSEDIRECTION_BOTH &&
  352. cp->bd.browseDirection != UA_BROWSEDIRECTION_FORWARD &&
  353. cp->bd.browseDirection != UA_BROWSEDIRECTION_INVERSE) {
  354. return UA_STATUSCODE_BADBROWSEDIRECTIONINVALID;
  355. }
  356. /* Set the list of references to check */
  357. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  358. UA_NodeId *referenceTypes = NULL; /* NULL -> all reference types are relevant */
  359. size_t referenceTypesSize = 0;
  360. if(!UA_NodeId_isNull(&cp->bd.referenceTypeId)) {
  361. const UA_Node *reftype = UA_Nodestore_getNode(server->nsCtx, &cp->bd.referenceTypeId);
  362. if(!reftype)
  363. return UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  364. UA_Boolean isRef = (reftype->nodeClass == UA_NODECLASS_REFERENCETYPE);
  365. UA_Nodestore_releaseNode(server->nsCtx, reftype);
  366. if(!isRef)
  367. return UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  368. if(!UA_NodeId_isNull(&cp->bd.referenceTypeId)) {
  369. if(cp->bd.includeSubtypes) {
  370. retval = getLocalRecursiveHierarchy(server, &cp->bd.referenceTypeId, 1, &subtypeId, 1,
  371. true, &referenceTypes, &referenceTypesSize);
  372. if(retval != UA_STATUSCODE_GOOD)
  373. return retval;
  374. UA_assert(referenceTypesSize > 0); /* The original referenceTypeId has been mirrored back */
  375. } else {
  376. referenceTypes = &cp->bd.referenceTypeId;
  377. referenceTypesSize = 1;
  378. }
  379. }
  380. }
  381. for(; cp->n < cp->rt.size; cp->n++, cp->nk = 0, cp->nki = 0) {
  382. /* Get the node */
  383. const UA_Node *node = UA_Nodestore_getNode(server->nsCtx, &cp->rt.targets[cp->n]);
  384. if(!node)
  385. continue;
  386. /* Browse the references in the current node */
  387. retval = browseNode(server, session, cp, rr, maxed, referenceTypesSize, referenceTypes, node);
  388. UA_Nodestore_releaseNode(server->nsCtx, node);
  389. if(retval != UA_STATUSCODE_GOOD)
  390. break;
  391. if(!cp->recursive || *maxed)
  392. break;
  393. }
  394. if(referenceTypes != &cp->bd.referenceTypeId)
  395. UA_Array_delete(referenceTypes, referenceTypesSize, &UA_TYPES[UA_TYPES_NODEID]);
  396. return retval;
  397. }
  398. /* Start to browse with no previous cp */
  399. void
  400. Operation_Browse(UA_Server *server, UA_Session *session, const struct BrowseOpts *bo,
  401. const UA_BrowseDescription *descr, UA_BrowseResult *result) {
  402. /* How many references can we return at most? */
  403. UA_UInt32 maxRefs = bo->maxReferences;
  404. if(maxRefs == 0) {
  405. if(server->config.maxReferencesPerNode != 0) {
  406. maxRefs = server->config.maxReferencesPerNode;
  407. } else {
  408. maxRefs = UA_INT32_MAX;
  409. }
  410. } else {
  411. if(server->config.maxReferencesPerNode != 0 &&
  412. maxRefs > server->config.maxReferencesPerNode) {
  413. maxRefs = server->config.maxReferencesPerNode;
  414. }
  415. }
  416. /* Does the original node exist? */
  417. const UA_Node *node = UA_Nodestore_getNode(server->nsCtx, &descr->nodeId);
  418. if(!node) {
  419. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  420. return;
  421. }
  422. UA_Nodestore_releaseNode(server->nsCtx, node);
  423. /* Create the results array */
  424. RefResult rr;
  425. result->statusCode = RefResult_init(&rr, maxRefs);
  426. if(result->statusCode != UA_STATUSCODE_GOOD)
  427. return;
  428. ContinuationPoint cp;
  429. result->statusCode = ContinuationPoint_init(&cp, maxRefs, bo->recursive);
  430. if(result->statusCode != UA_STATUSCODE_GOOD) {
  431. RefResult_clear(&rr);
  432. return;
  433. }
  434. cp.bd = *descr; /* Deep-copy only when the cp is persisted in the session */
  435. /* Add the initial node to the RefTree */
  436. result->statusCode = RefTree_add(&cp.rt, &descr->nodeId);
  437. if(result->statusCode != UA_STATUSCODE_GOOD) {
  438. RefTree_clear(&cp.rt);
  439. RefResult_clear(&rr);
  440. return;
  441. }
  442. /* Recurse to get all references */
  443. UA_Boolean maxed = false;
  444. result->statusCode = browseWithCp(server, session, &cp, &rr, &maxed);
  445. if(result->statusCode != UA_STATUSCODE_GOOD) {
  446. RefTree_clear(&cp.rt);
  447. RefResult_clear(&rr);
  448. return;
  449. }
  450. /* No results */
  451. if(rr.size == 0) {
  452. result->references = (UA_ReferenceDescription*)UA_EMPTY_ARRAY_SENTINEL;
  453. RefTree_clear(&cp.rt);
  454. UA_free(rr.descr);
  455. return;
  456. }
  457. /* Move results to the output */
  458. result->references = rr.descr;
  459. result->referencesSize = rr.size;
  460. /* Nothing left for BrowseNext */
  461. if(!maxed) {
  462. RefTree_clear(&cp.rt);
  463. return;
  464. }
  465. /* Is there a ContinuationPoint available? */
  466. if(session->availableContinuationPoints == 0) {
  467. RefTree_clear(&cp.rt);
  468. result->statusCode = UA_STATUSCODE_BADNOCONTINUATIONPOINTS;
  469. return;
  470. }
  471. /* Create a new continuation point. */
  472. UA_ByteString tmp;
  473. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  474. ContinuationPoint *newCp = (ContinuationPoint*)UA_malloc(sizeof(ContinuationPoint));
  475. if(!newCp) {
  476. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  477. goto cleanup;
  478. }
  479. /* Copy, make a deep copy of the BrowseDescription */
  480. *newCp = cp;
  481. retval = UA_BrowseDescription_copy(descr, &newCp->bd);
  482. if(retval != UA_STATUSCODE_GOOD)
  483. goto cleanup;
  484. /* Return the cp identifier */
  485. tmp.length = sizeof(UA_Guid);
  486. tmp.data = (UA_Byte*)&newCp->identifier;
  487. retval = UA_ByteString_copy(&tmp, &result->continuationPoint);
  488. if(retval != UA_STATUSCODE_GOOD)
  489. goto cleanup;
  490. /* Attach the cp to the session */
  491. newCp->next = session->continuationPoints;
  492. session->continuationPoints = newCp;
  493. --session->availableContinuationPoints;
  494. return;
  495. cleanup:
  496. UA_BrowseResult_deleteMembers(result); /* Holds the content that was in rr before */
  497. if(newCp) {
  498. ContinuationPoint_clear(newCp);
  499. UA_free(newCp);
  500. }
  501. result->statusCode = retval;
  502. }
  503. void Service_Browse(UA_Server *server, UA_Session *session,
  504. const UA_BrowseRequest *request, UA_BrowseResponse *response) {
  505. UA_LOG_DEBUG_SESSION(&server->config.logger, session, "Processing BrowseRequest");
  506. /* Test the number of operations in the request */
  507. if(server->config.maxNodesPerBrowse != 0 &&
  508. request->nodesToBrowseSize > server->config.maxNodesPerBrowse) {
  509. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  510. return;
  511. }
  512. /* No views supported at the moment */
  513. if(!UA_NodeId_isNull(&request->view.viewId)) {
  514. response->responseHeader.serviceResult = UA_STATUSCODE_BADVIEWIDUNKNOWN;
  515. return;
  516. }
  517. struct BrowseOpts bo;
  518. bo.maxReferences = request->requestedMaxReferencesPerNode;
  519. bo.recursive = false;
  520. response->responseHeader.serviceResult =
  521. UA_Server_processServiceOperations(server, session,
  522. (UA_ServiceOperation)Operation_Browse, &bo,
  523. &request->nodesToBrowseSize,
  524. &UA_TYPES[UA_TYPES_BROWSEDESCRIPTION],
  525. &response->resultsSize,
  526. &UA_TYPES[UA_TYPES_BROWSERESULT]);
  527. }
  528. UA_BrowseResult
  529. UA_Server_browse(UA_Server *server, UA_UInt32 maxReferences,
  530. const UA_BrowseDescription *bd) {
  531. UA_BrowseResult result;
  532. UA_BrowseResult_init(&result);
  533. struct BrowseOpts bo;
  534. bo.maxReferences = maxReferences;
  535. bo.recursive = false;
  536. Operation_Browse(server, &server->adminSession, &bo, bd, &result);
  537. return result;
  538. }
  539. UA_BrowseResult
  540. UA_Server_browseRecursive(UA_Server *server, UA_UInt32 maxReferences,
  541. const UA_BrowseDescription *bd) {
  542. UA_BrowseResult result;
  543. UA_BrowseResult_init(&result);
  544. struct BrowseOpts bo;
  545. bo.maxReferences = maxReferences;
  546. bo.recursive = true;
  547. Operation_Browse(server, &server->adminSession, &bo, bd, &result);
  548. return result;
  549. }
  550. static void
  551. Operation_BrowseNext(UA_Server *server, UA_Session *session,
  552. const UA_Boolean *releaseContinuationPoints,
  553. const UA_ByteString *continuationPoint, UA_BrowseResult *result) {
  554. /* Find the continuation point */
  555. ContinuationPoint **prev = &session->continuationPoints, *cp;
  556. UA_ByteString identifier;
  557. while((cp = *prev)) {
  558. identifier.length = sizeof(UA_Guid);
  559. identifier.data = (UA_Byte*)&cp->identifier;
  560. if(UA_ByteString_equal(&identifier, continuationPoint))
  561. break;
  562. prev = &cp->next;
  563. }
  564. if(!cp) {
  565. result->statusCode = UA_STATUSCODE_BADCONTINUATIONPOINTINVALID;
  566. return;
  567. }
  568. /* Remove the cp */
  569. if(*releaseContinuationPoints) {
  570. *prev = ContinuationPoint_clear(cp);
  571. UA_free(cp);
  572. ++session->availableContinuationPoints;
  573. return;
  574. }
  575. /* Allocate the results array */
  576. RefResult rr;
  577. result->statusCode = RefResult_init(&rr, cp->maxReferences);
  578. if(result->statusCode != UA_STATUSCODE_GOOD)
  579. return;
  580. /* Recurse to get all references */
  581. UA_Boolean maxed = false;
  582. result->statusCode = browseWithCp(server, session, cp, &rr, &maxed);
  583. if(result->statusCode != UA_STATUSCODE_GOOD)
  584. goto removeCp;
  585. /* No results */
  586. if(rr.size == 0) {
  587. result->references = (UA_ReferenceDescription*)UA_EMPTY_ARRAY_SENTINEL;
  588. goto removeCp;
  589. }
  590. if(maxed) {
  591. /* Keep the cp */
  592. result->statusCode = UA_ByteString_copy(&identifier, &result->continuationPoint);
  593. if(result->statusCode != UA_STATUSCODE_GOOD)
  594. goto removeCp;
  595. } else {
  596. /* All done, remove the cp */
  597. *prev = ContinuationPoint_clear(cp);
  598. UA_free(cp);
  599. ++session->availableContinuationPoints;
  600. }
  601. /* Move results to the output */
  602. result->references = rr.descr;
  603. result->referencesSize = rr.size;
  604. return;
  605. removeCp:
  606. *prev = cp->next;
  607. ContinuationPoint_clear(cp);
  608. UA_free(cp);
  609. session->availableContinuationPoints++;
  610. RefResult_clear(&rr);
  611. }
  612. void
  613. Service_BrowseNext(UA_Server *server, UA_Session *session,
  614. const UA_BrowseNextRequest *request, UA_BrowseNextResponse *response) {
  615. UA_LOG_DEBUG_SESSION(&server->config.logger, session, "Processing BrowseNextRequest");
  616. response->responseHeader.serviceResult =
  617. UA_Server_processServiceOperations(server, session, (UA_ServiceOperation)Operation_BrowseNext,
  618. &request->releaseContinuationPoints,
  619. &request->continuationPointsSize,
  620. &UA_TYPES[UA_TYPES_BYTESTRING], &response->resultsSize,
  621. &UA_TYPES[UA_TYPES_BROWSERESULT]);
  622. }
  623. UA_BrowseResult
  624. UA_Server_browseNext(UA_Server *server, UA_Boolean releaseContinuationPoint,
  625. const UA_ByteString *continuationPoint) {
  626. UA_BrowseResult result;
  627. UA_BrowseResult_init(&result);
  628. Operation_BrowseNext(server, &server->adminSession, &releaseContinuationPoint,
  629. continuationPoint, &result);
  630. return result;
  631. }
  632. UA_StatusCode
  633. getLocalRecursiveHierarchy(UA_Server *server, const UA_NodeId *startNodes, size_t startNodesSize,
  634. const UA_NodeId *refTypes, size_t refTypesSize, UA_Boolean walkDownwards,
  635. UA_NodeId **results, size_t *resultsSize) {
  636. ContinuationPoint cp;
  637. UA_StatusCode retval = ContinuationPoint_init(&cp, 0, true);
  638. if(retval != UA_STATUSCODE_GOOD)
  639. return retval;
  640. for(size_t i = 0; i < startNodesSize; i++)
  641. retval |= RefTree_add(&cp.rt, &startNodes[i]);
  642. if(retval != UA_STATUSCODE_GOOD) {
  643. ContinuationPoint_clear(&cp);
  644. return retval;
  645. }
  646. if(!walkDownwards)
  647. cp.bd.browseDirection = UA_BROWSEDIRECTION_INVERSE;
  648. UA_Boolean maxed = false;
  649. for(; cp.n < cp.rt.size; cp.n++, cp.nk = 0, cp.nki = 0) {
  650. /* Get the node */
  651. const UA_Node *node = UA_Nodestore_getNode(server->nsCtx, &cp.rt.targets[cp.n]);
  652. if(!node)
  653. continue;
  654. /* Browse the references in the current node */
  655. retval = browseNode(server, &server->adminSession, &cp, NULL,
  656. &maxed, refTypesSize, refTypes, node);
  657. UA_Nodestore_releaseNode(server->nsCtx, node);
  658. if(retval != UA_STATUSCODE_GOOD)
  659. break;
  660. }
  661. if(retval == UA_STATUSCODE_GOOD) {
  662. *results = cp.rt.targets;
  663. *resultsSize = cp.rt.size;
  664. cp.rt.targets = NULL;
  665. cp.rt.size = 0;
  666. }
  667. ContinuationPoint_clear(&cp);
  668. return retval;
  669. }
  670. /***********************/
  671. /* TranslateBrowsePath */
  672. /***********************/
  673. static void
  674. walkBrowsePathElementReferenceTargets(UA_BrowsePathResult *result, size_t *targetsSize,
  675. UA_NodeId **next, size_t *nextSize, size_t *nextCount,
  676. UA_UInt32 elemDepth, const UA_NodeReferenceKind *rk) {
  677. /* Loop over the targets */
  678. for(size_t i = 0; i < rk->targetIdsSize; i++) {
  679. UA_ExpandedNodeId *targetId = &rk->targetIds[i];
  680. /* Does the reference point to an external server? Then add to the
  681. * targets with the right path depth. */
  682. if(targetId->serverIndex != 0) {
  683. UA_BrowsePathTarget *tempTargets =
  684. (UA_BrowsePathTarget*)UA_realloc(result->targets,
  685. sizeof(UA_BrowsePathTarget) * (*targetsSize) * 2);
  686. if(!tempTargets) {
  687. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  688. return;
  689. }
  690. result->targets = tempTargets;
  691. (*targetsSize) *= 2;
  692. result->statusCode = UA_ExpandedNodeId_copy(targetId,
  693. &result->targets[result->targetsSize].targetId);
  694. result->targets[result->targetsSize].remainingPathIndex = elemDepth;
  695. continue;
  696. }
  697. /* Can we store the node in the array of candidates for deep-search? */
  698. if(*nextSize <= *nextCount) {
  699. UA_NodeId *tempNext =
  700. (UA_NodeId*)UA_realloc(*next, sizeof(UA_NodeId) * (*nextSize) * 2);
  701. if(!tempNext) {
  702. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  703. return;
  704. }
  705. *next = tempNext;
  706. (*nextSize) *= 2;
  707. }
  708. /* Add the node to the next array for the following path element */
  709. result->statusCode = UA_NodeId_copy(&targetId->nodeId,
  710. &(*next)[*nextCount]);
  711. if(result->statusCode != UA_STATUSCODE_GOOD)
  712. return;
  713. ++(*nextCount);
  714. }
  715. }
  716. static void
  717. walkBrowsePathElement(UA_Server *server, UA_Session *session, UA_UInt32 nodeClassMask,
  718. UA_BrowsePathResult *result, size_t *targetsSize,
  719. const UA_RelativePathElement *elem, UA_UInt32 elemDepth,
  720. const UA_QualifiedName *targetName,
  721. const UA_NodeId *current, const size_t currentCount,
  722. UA_NodeId **next, size_t *nextSize, size_t *nextCount) {
  723. /* Return all references? */
  724. UA_Boolean all_refs = UA_NodeId_isNull(&elem->referenceTypeId);
  725. if(!all_refs) {
  726. const UA_Node *rootRef = UA_Nodestore_getNode(server->nsCtx, &elem->referenceTypeId);
  727. if(!rootRef)
  728. return;
  729. UA_Boolean match = (rootRef->nodeClass == UA_NODECLASS_REFERENCETYPE);
  730. UA_Nodestore_releaseNode(server->nsCtx, rootRef);
  731. if(!match)
  732. return;
  733. }
  734. /* Iterate over all nodes at the current depth-level */
  735. for(size_t i = 0; i < currentCount; ++i) {
  736. /* Get the node */
  737. const UA_Node *node = UA_Nodestore_getNode(server->nsCtx, &current[i]);
  738. if(!node) {
  739. /* If we cannot find the node at depth 0, the starting node does not exist */
  740. if(elemDepth == 0)
  741. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  742. continue;
  743. }
  744. /* Test whether the node fits the class mask */
  745. if(!matchClassMask(node, nodeClassMask)) {
  746. UA_Nodestore_releaseNode(server->nsCtx, node);
  747. continue;
  748. }
  749. /* Test whether the node has the target name required in the previous
  750. * path element */
  751. if(targetName && (targetName->namespaceIndex != node->browseName.namespaceIndex ||
  752. !UA_String_equal(&targetName->name, &node->browseName.name))) {
  753. UA_Nodestore_releaseNode(server->nsCtx, node);
  754. continue;
  755. }
  756. /* Loop over the nodes references */
  757. for(size_t r = 0; r < node->referencesSize &&
  758. result->statusCode == UA_STATUSCODE_GOOD; ++r) {
  759. UA_NodeReferenceKind *rk = &node->references[r];
  760. /* Does the direction of the reference match? */
  761. if(rk->isInverse != elem->isInverse)
  762. continue;
  763. /* Is the node relevant? */
  764. if(!all_refs && !relevantReference(server, elem->includeSubtypes,
  765. &elem->referenceTypeId, &rk->referenceTypeId))
  766. continue;
  767. /* Walk over the reference targets */
  768. walkBrowsePathElementReferenceTargets(result, targetsSize, next, nextSize,
  769. nextCount, elemDepth, rk);
  770. }
  771. UA_Nodestore_releaseNode(server->nsCtx, node);
  772. }
  773. }
  774. /* This assumes that result->targets has enough room for all currentCount elements */
  775. static void
  776. addBrowsePathTargets(UA_Server *server, UA_Session *session, UA_UInt32 nodeClassMask,
  777. UA_BrowsePathResult *result, const UA_QualifiedName *targetName,
  778. UA_NodeId *current, size_t currentCount) {
  779. for(size_t i = 0; i < currentCount; i++) {
  780. const UA_Node *node = UA_Nodestore_getNode(server->nsCtx, &current[i]);
  781. if(!node) {
  782. UA_NodeId_deleteMembers(&current[i]);
  783. continue;
  784. }
  785. /* Test whether the node fits the class mask */
  786. UA_Boolean skip = !matchClassMask(node, nodeClassMask);
  787. /* Test whether the node has the target name required in the
  788. * previous path element */
  789. if(targetName->namespaceIndex != node->browseName.namespaceIndex ||
  790. !UA_String_equal(&targetName->name, &node->browseName.name))
  791. skip = true;
  792. UA_Nodestore_releaseNode(server->nsCtx, node);
  793. if(skip) {
  794. UA_NodeId_deleteMembers(&current[i]);
  795. continue;
  796. }
  797. /* Move the nodeid to the target array */
  798. UA_BrowsePathTarget_init(&result->targets[result->targetsSize]);
  799. result->targets[result->targetsSize].targetId.nodeId = current[i];
  800. result->targets[result->targetsSize].remainingPathIndex = UA_UINT32_MAX;
  801. ++result->targetsSize;
  802. }
  803. }
  804. static void
  805. walkBrowsePath(UA_Server *server, UA_Session *session, const UA_BrowsePath *path,
  806. UA_UInt32 nodeClassMask, UA_BrowsePathResult *result, size_t targetsSize,
  807. UA_NodeId **current, size_t *currentSize, size_t *currentCount,
  808. UA_NodeId **next, size_t *nextSize, size_t *nextCount) {
  809. UA_assert(*currentCount == 1);
  810. UA_assert(*nextCount == 0);
  811. /* Points to the targetName of the _previous_ path element */
  812. const UA_QualifiedName *targetName = NULL;
  813. /* Iterate over path elements */
  814. UA_assert(path->relativePath.elementsSize > 0);
  815. for(UA_UInt32 i = 0; i < path->relativePath.elementsSize; ++i) {
  816. walkBrowsePathElement(server, session, nodeClassMask, result, &targetsSize,
  817. &path->relativePath.elements[i], i, targetName,
  818. *current, *currentCount, next, nextSize, nextCount);
  819. /* Clean members of current */
  820. for(size_t j = 0; j < *currentCount; j++)
  821. UA_NodeId_deleteMembers(&(*current)[j]);
  822. *currentCount = 0;
  823. /* When no targets are left or an error occurred. None of next's
  824. * elements will be copied to result->targets */
  825. if(*nextCount == 0 || result->statusCode != UA_STATUSCODE_GOOD) {
  826. UA_assert(*currentCount == 0);
  827. UA_assert(*nextCount == 0);
  828. return;
  829. }
  830. /* Exchange current and next for the next depth */
  831. size_t tSize = *currentSize; size_t tCount = *currentCount; UA_NodeId *tT = *current;
  832. *currentSize = *nextSize; *currentCount = *nextCount; *current = *next;
  833. *nextSize = tSize; *nextCount = tCount; *next = tT;
  834. /* Store the target name of the previous path element */
  835. targetName = &path->relativePath.elements[i].targetName;
  836. }
  837. UA_assert(targetName != NULL);
  838. UA_assert(*nextCount == 0);
  839. /* After the last BrowsePathElement, move members from current to the
  840. * result targets */
  841. /* Realloc if more space is needed */
  842. if(targetsSize < result->targetsSize + (*currentCount)) {
  843. UA_BrowsePathTarget *newTargets =
  844. (UA_BrowsePathTarget*)UA_realloc(result->targets, sizeof(UA_BrowsePathTarget) *
  845. (result->targetsSize + (*currentCount)));
  846. if(!newTargets) {
  847. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  848. for(size_t i = 0; i < *currentCount; ++i)
  849. UA_NodeId_deleteMembers(&(*current)[i]);
  850. *currentCount = 0;
  851. return;
  852. }
  853. result->targets = newTargets;
  854. }
  855. /* Move the elements of current to the targets */
  856. addBrowsePathTargets(server, session, nodeClassMask, result, targetName, *current, *currentCount);
  857. *currentCount = 0;
  858. }
  859. static void
  860. Operation_TranslateBrowsePathToNodeIds(UA_Server *server, UA_Session *session,
  861. const UA_UInt32 *nodeClassMask, const UA_BrowsePath *path,
  862. UA_BrowsePathResult *result) {
  863. if(path->relativePath.elementsSize <= 0) {
  864. result->statusCode = UA_STATUSCODE_BADNOTHINGTODO;
  865. return;
  866. }
  867. /* RelativePath elements must not have an empty targetName */
  868. for(size_t i = 0; i < path->relativePath.elementsSize; ++i) {
  869. if(UA_QualifiedName_isNull(&path->relativePath.elements[i].targetName)) {
  870. result->statusCode = UA_STATUSCODE_BADBROWSENAMEINVALID;
  871. return;
  872. }
  873. }
  874. /* Allocate memory for the targets */
  875. size_t targetsSize = 10; /* When to realloc; the member count is stored in
  876. * result->targetsSize */
  877. result->targets =
  878. (UA_BrowsePathTarget*)UA_malloc(sizeof(UA_BrowsePathTarget) * targetsSize);
  879. if(!result->targets) {
  880. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  881. return;
  882. }
  883. /* Allocate memory for two temporary arrays. One with the results for the
  884. * previous depth of the path. The other for the new results at the current
  885. * depth. The two arrays alternate as we descend down the tree. */
  886. size_t currentSize = 10; /* When to realloc */
  887. size_t currentCount = 0; /* Current elements */
  888. UA_NodeId *current = (UA_NodeId*)UA_malloc(sizeof(UA_NodeId) * currentSize);
  889. if(!current) {
  890. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  891. UA_free(result->targets);
  892. return;
  893. }
  894. size_t nextSize = 10; /* When to realloc */
  895. size_t nextCount = 0; /* Current elements */
  896. UA_NodeId *next = (UA_NodeId*)UA_malloc(sizeof(UA_NodeId) * nextSize);
  897. if(!next) {
  898. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  899. UA_free(result->targets);
  900. UA_free(current);
  901. return;
  902. }
  903. /* Copy the starting node into current */
  904. result->statusCode = UA_NodeId_copy(&path->startingNode, &current[0]);
  905. if(result->statusCode != UA_STATUSCODE_GOOD) {
  906. UA_free(result->targets);
  907. UA_free(current);
  908. UA_free(next);
  909. return;
  910. }
  911. currentCount = 1;
  912. /* Walk the path elements */
  913. walkBrowsePath(server, session, path, *nodeClassMask, result, targetsSize,
  914. &current, &currentSize, &currentCount,
  915. &next, &nextSize, &nextCount);
  916. UA_assert(currentCount == 0);
  917. UA_assert(nextCount == 0);
  918. /* No results => BadNoMatch status code */
  919. if(result->targetsSize == 0 && result->statusCode == UA_STATUSCODE_GOOD)
  920. result->statusCode = UA_STATUSCODE_BADNOMATCH;
  921. /* Clean up the temporary arrays and the targets */
  922. UA_free(current);
  923. UA_free(next);
  924. if(result->statusCode != UA_STATUSCODE_GOOD) {
  925. for(size_t i = 0; i < result->targetsSize; ++i)
  926. UA_BrowsePathTarget_deleteMembers(&result->targets[i]);
  927. UA_free(result->targets);
  928. result->targets = NULL;
  929. result->targetsSize = 0;
  930. }
  931. }
  932. UA_BrowsePathResult
  933. UA_Server_translateBrowsePathToNodeIds(UA_Server *server,
  934. const UA_BrowsePath *browsePath) {
  935. UA_BrowsePathResult result;
  936. UA_BrowsePathResult_init(&result);
  937. UA_UInt32 nodeClassMask = 0; /* All node classes */
  938. Operation_TranslateBrowsePathToNodeIds(server, &server->adminSession, &nodeClassMask,
  939. browsePath, &result);
  940. return result;
  941. }
  942. void
  943. Service_TranslateBrowsePathsToNodeIds(UA_Server *server, UA_Session *session,
  944. const UA_TranslateBrowsePathsToNodeIdsRequest *request,
  945. UA_TranslateBrowsePathsToNodeIdsResponse *response) {
  946. UA_LOG_DEBUG_SESSION(&server->config.logger, session,
  947. "Processing TranslateBrowsePathsToNodeIdsRequest");
  948. /* Test the number of operations in the request */
  949. if(server->config.maxNodesPerTranslateBrowsePathsToNodeIds != 0 &&
  950. request->browsePathsSize > server->config.maxNodesPerTranslateBrowsePathsToNodeIds) {
  951. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  952. return;
  953. }
  954. UA_UInt32 nodeClassMask = 0; /* All node classes */
  955. response->responseHeader.serviceResult =
  956. UA_Server_processServiceOperations(server, session,
  957. (UA_ServiceOperation)Operation_TranslateBrowsePathToNodeIds,
  958. &nodeClassMask,
  959. &request->browsePathsSize, &UA_TYPES[UA_TYPES_BROWSEPATH],
  960. &response->resultsSize, &UA_TYPES[UA_TYPES_BROWSEPATHRESULT]);
  961. }
  962. UA_BrowsePathResult
  963. UA_Server_browseSimplifiedBrowsePath(UA_Server *server, const UA_NodeId origin,
  964. size_t browsePathSize, const UA_QualifiedName *browsePath) {
  965. /* Construct the BrowsePath */
  966. UA_BrowsePath bp;
  967. UA_BrowsePath_init(&bp);
  968. bp.startingNode = origin;
  969. UA_STACKARRAY(UA_RelativePathElement, rpe, browsePathSize);
  970. memset(rpe, 0, sizeof(UA_RelativePathElement) * browsePathSize);
  971. for(size_t j = 0; j < browsePathSize; j++) {
  972. rpe[j].referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HIERARCHICALREFERENCES);
  973. rpe[j].includeSubtypes = true;
  974. rpe[j].targetName = browsePath[j];
  975. }
  976. bp.relativePath.elements = rpe;
  977. bp.relativePath.elementsSize = browsePathSize;
  978. /* Browse */
  979. UA_BrowsePathResult bpr;
  980. UA_BrowsePathResult_init(&bpr);
  981. UA_UInt32 nodeClassMask = UA_NODECLASS_OBJECT | UA_NODECLASS_VARIABLE;
  982. Operation_TranslateBrowsePathToNodeIds(server, &server->adminSession, &nodeClassMask, &bp, &bpr);
  983. return bpr;
  984. }
  985. /************/
  986. /* Register */
  987. /************/
  988. void Service_RegisterNodes(UA_Server *server, UA_Session *session,
  989. const UA_RegisterNodesRequest *request,
  990. UA_RegisterNodesResponse *response) {
  991. UA_LOG_DEBUG_SESSION(&server->config.logger, session,
  992. "Processing RegisterNodesRequest");
  993. //TODO: hang the nodeids to the session if really needed
  994. if(request->nodesToRegisterSize == 0) {
  995. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  996. return;
  997. }
  998. /* Test the number of operations in the request */
  999. if(server->config.maxNodesPerRegisterNodes != 0 &&
  1000. request->nodesToRegisterSize > server->config.maxNodesPerRegisterNodes) {
  1001. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  1002. return;
  1003. }
  1004. response->responseHeader.serviceResult =
  1005. UA_Array_copy(request->nodesToRegister, request->nodesToRegisterSize,
  1006. (void**)&response->registeredNodeIds, &UA_TYPES[UA_TYPES_NODEID]);
  1007. if(response->responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  1008. response->registeredNodeIdsSize = request->nodesToRegisterSize;
  1009. }
  1010. void Service_UnregisterNodes(UA_Server *server, UA_Session *session,
  1011. const UA_UnregisterNodesRequest *request,
  1012. UA_UnregisterNodesResponse *response) {
  1013. UA_LOG_DEBUG_SESSION(&server->config.logger, session,
  1014. "Processing UnRegisterNodesRequest");
  1015. //TODO: remove the nodeids from the session if really needed
  1016. if(request->nodesToUnregisterSize == 0)
  1017. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  1018. /* Test the number of operations in the request */
  1019. if(server->config.maxNodesPerRegisterNodes != 0 &&
  1020. request->nodesToUnregisterSize > server->config.maxNodesPerRegisterNodes) {
  1021. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  1022. return;
  1023. }
  1024. }