ua_services_view.c 45 KB

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