ua_services_view.c 27 KB

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