ua_services_view.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. #include "ua_server_internal.h"
  2. #include "ua_services.h"
  3. #include "ua_statuscodes.h"
  4. #include "ua_nodestore.h"
  5. #include "ua_util.h"
  6. static UA_StatusCode fillrefdescr(UA_NodeStore *ns, const UA_Node *curr, UA_ReferenceNode *ref,
  7. UA_UInt32 mask, UA_ReferenceDescription *descr)
  8. {
  9. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  10. UA_ReferenceDescription_init(descr);
  11. retval |= UA_NodeId_copy(&curr->nodeId, &descr->nodeId.nodeId);
  12. //TODO: ExpandedNodeId is mocked up
  13. descr->nodeId.serverIndex = 0;
  14. descr->nodeId.namespaceUri.length = -1;
  15. if(mask & UA_BROWSERESULTMASK_REFERENCETYPEID)
  16. retval |= UA_NodeId_copy(&ref->referenceTypeId, &descr->referenceTypeId);
  17. if(mask & UA_BROWSERESULTMASK_ISFORWARD)
  18. descr->isForward = !ref->isInverse;
  19. if(mask & UA_BROWSERESULTMASK_NODECLASS)
  20. retval |= UA_NodeClass_copy(&curr->nodeClass, &descr->nodeClass);
  21. if(mask & UA_BROWSERESULTMASK_BROWSENAME)
  22. retval |= UA_QualifiedName_copy(&curr->browseName, &descr->browseName);
  23. if(mask & UA_BROWSERESULTMASK_DISPLAYNAME)
  24. retval |= UA_LocalizedText_copy(&curr->displayName, &descr->displayName);
  25. if(mask & UA_BROWSERESULTMASK_TYPEDEFINITION){
  26. if(curr->nodeClass == UA_NODECLASS_OBJECT || curr->nodeClass == UA_NODECLASS_VARIABLE) {
  27. for(UA_Int32 i = 0; i < curr->referencesSize; i++) {
  28. UA_ReferenceNode *refnode = &curr->references[i];
  29. if(refnode->referenceTypeId.identifier.numeric != UA_NS0ID_HASTYPEDEFINITION)
  30. continue;
  31. retval |= UA_ExpandedNodeId_copy(&refnode->targetId, &descr->typeDefinition);
  32. break;
  33. }
  34. }
  35. }
  36. if(retval)
  37. UA_ReferenceDescription_deleteMembers(descr);
  38. return retval;
  39. }
  40. /* Tests if the node is relevant to the browse request and shall be returned. If
  41. so, it is retrieved from the Nodestore. If not, null is returned. */
  42. static const UA_Node *relevant_node(UA_Server *server, UA_NodeStore *ns, const UA_BrowseDescription *descr,
  43. UA_Boolean return_all, UA_ReferenceNode *reference,
  44. UA_NodeId *relevant, size_t relevant_count)
  45. {
  46. if(reference->isInverse == UA_TRUE && descr->browseDirection == UA_BROWSEDIRECTION_FORWARD)
  47. return UA_NULL;
  48. else if(reference->isInverse == UA_FALSE && descr->browseDirection == UA_BROWSEDIRECTION_INVERSE)
  49. return UA_NULL;
  50. if(!return_all) {
  51. for(size_t i = 0; i < relevant_count; i++) {
  52. if(UA_NodeId_equal(&reference->referenceTypeId, &relevant[i]))
  53. goto is_relevant;
  54. }
  55. return UA_NULL;
  56. }
  57. is_relevant: ;
  58. #ifdef UA_EXTERNAL_NAMESPACES
  59. const UA_Node *node = NULL;
  60. UA_Boolean isExternal = UA_FALSE;
  61. size_t nsIndex;
  62. for(nsIndex = 0; nsIndex < server->externalNamespacesSize; nsIndex++) {
  63. if(reference->targetId.nodeId.namespaceIndex != server->externalNamespaces[nsIndex].index)
  64. continue;
  65. else{
  66. isExternal = UA_TRUE;
  67. break;
  68. }
  69. }
  70. if(isExternal == UA_FALSE){
  71. node = UA_NodeStore_get(ns, &reference->targetId.nodeId);
  72. } else {
  73. /* prepare a read request in the external nodestore */
  74. UA_ExternalNodeStore *ens = &server->externalNamespaces[nsIndex].externalNodeStore;
  75. UA_ReadValueId *readValueIds = UA_Array_new(&UA_TYPES[UA_TYPES_READVALUEID], 6);
  76. UA_UInt32 *indices = UA_Array_new(&UA_TYPES[UA_TYPES_UINT32], 6);
  77. UA_UInt32 indicesSize = 6;
  78. UA_DataValue *readNodesResults = UA_Array_new(&UA_TYPES[UA_TYPES_DATAVALUE], 6);
  79. UA_DiagnosticInfo *diagnosticInfos = UA_Array_new(&UA_TYPES[UA_TYPES_DIAGNOSTICINFO], 6);
  80. for(UA_UInt32 i = 0; i<6; i++){
  81. readValueIds[i].nodeId = reference->targetId.nodeId;
  82. UA_String_init(&(readValueIds[i].indexRange));
  83. UA_QualifiedName_init(&(readValueIds[i].dataEncoding));
  84. indices[i] = i;
  85. UA_DataValue_init(&(readNodesResults[i]));
  86. UA_DiagnosticInfo_init(&(diagnosticInfos[i]));
  87. }
  88. readValueIds[0].attributeId = UA_ATTRIBUTEID_NODECLASS;
  89. readValueIds[1].attributeId = UA_ATTRIBUTEID_BROWSENAME;
  90. readValueIds[2].attributeId = UA_ATTRIBUTEID_DISPLAYNAME;
  91. readValueIds[3].attributeId = UA_ATTRIBUTEID_DESCRIPTION;
  92. readValueIds[4].attributeId = UA_ATTRIBUTEID_WRITEMASK;
  93. readValueIds[5].attributeId = UA_ATTRIBUTEID_USERWRITEMASK;
  94. ens->readNodes(ens->ensHandle, NULL, readValueIds,
  95. indices, indicesSize, readNodesResults, UA_FALSE, diagnosticInfos);
  96. /* create and fill a dummy nodeStructure */
  97. UA_Node *tempNode = (UA_Node*) UA_ObjectNode_new();
  98. UA_NodeId_copy(&(reference->targetId.nodeId), &(tempNode->nodeId));
  99. if(readNodesResults[0].status == UA_STATUSCODE_GOOD){
  100. UA_NodeClass_copy((UA_NodeClass*)readNodesResults[0].value.data, &(tempNode->nodeClass));
  101. }
  102. if(readNodesResults[1].status == UA_STATUSCODE_GOOD){
  103. UA_QualifiedName_copy((UA_QualifiedName*)readNodesResults[1].value.data, &(tempNode->browseName));
  104. }
  105. if(readNodesResults[2].status == UA_STATUSCODE_GOOD){
  106. UA_LocalizedText_copy((UA_LocalizedText*)readNodesResults[2].value.data, &(tempNode->displayName));
  107. }
  108. if(readNodesResults[3].status == UA_STATUSCODE_GOOD){
  109. UA_LocalizedText_copy((UA_LocalizedText*)readNodesResults[3].value.data, &(tempNode->description));
  110. }
  111. if(readNodesResults[4].status == UA_STATUSCODE_GOOD){
  112. UA_UInt32_copy((UA_UInt32*)readNodesResults[4].value.data, &(tempNode->writeMask));
  113. }
  114. if(readNodesResults[5].status == UA_STATUSCODE_GOOD){
  115. UA_UInt32_copy((UA_UInt32*)readNodesResults[5].value.data, &(tempNode->userWriteMask));
  116. }
  117. UA_Array_delete(readValueIds, &UA_TYPES[UA_TYPES_READVALUEID], 6);
  118. UA_Array_delete(indices, &UA_TYPES[UA_TYPES_UINT32], 6);
  119. UA_Array_delete(readNodesResults, &UA_TYPES[UA_TYPES_DATAVALUE], 6);
  120. UA_Array_delete(diagnosticInfos, &UA_TYPES[UA_TYPES_DIAGNOSTICINFO], 6);
  121. node = tempNode;
  122. }
  123. #else
  124. const UA_Node *node = UA_NodeStore_get(ns, &reference->targetId.nodeId);
  125. #endif
  126. if(node && descr->nodeClassMask != 0 && (node->nodeClass & descr->nodeClassMask) == 0) {
  127. #ifdef UA_EXTERNAL_NAMESPACES
  128. if(isExternal == UA_TRUE){
  129. ;
  130. } else
  131. #endif
  132. UA_NodeStore_release(node);
  133. return UA_NULL;
  134. }
  135. return node;
  136. }
  137. /**
  138. * We find all subtypes by a single iteration over the array. We start with an array with a single
  139. * root nodeid at the beginning. When we find relevant references, we add the nodeids to the back of
  140. * the array and increase the size. Since the hierarchy is not cyclic, we can safely progress in the
  141. * array to process the newly found referencetype nodeids (emulated recursion).
  142. */
  143. static UA_StatusCode findsubtypes(UA_NodeStore *ns, const UA_NodeId *root, UA_NodeId **reftypes,
  144. size_t *reftypes_count) {
  145. const UA_Node *node = UA_NodeStore_get(ns, root);
  146. if(!node)
  147. return UA_STATUSCODE_BADNOMATCH;
  148. if(node->nodeClass != UA_NODECLASS_REFERENCETYPE) {
  149. UA_NodeStore_release(node);
  150. return UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  151. }
  152. UA_NodeStore_release(node);
  153. size_t results_size = 20; // probably too big, but saves mallocs
  154. UA_NodeId *results = UA_malloc(sizeof(UA_NodeId) * results_size);
  155. if(!results)
  156. return UA_STATUSCODE_BADOUTOFMEMORY;
  157. UA_StatusCode retval = UA_NodeId_copy(root, &results[0]);
  158. if(retval != UA_STATUSCODE_GOOD) {
  159. UA_free(results);
  160. return retval;
  161. }
  162. size_t index = 0; // where are we currently in the array?
  163. size_t last = 0; // where is the last element in the array?
  164. do {
  165. node = UA_NodeStore_get(ns, &results[index]);
  166. if(!node || node->nodeClass != UA_NODECLASS_REFERENCETYPE)
  167. continue;
  168. for(UA_Int32 i = 0; i < node->referencesSize; i++) {
  169. if(node->references[i].referenceTypeId.identifier.numeric != UA_NS0ID_HASSUBTYPE ||
  170. node->references[i].isInverse == UA_TRUE)
  171. continue;
  172. if(++last >= results_size) { // is the array big enough?
  173. UA_NodeId *new_results = UA_realloc(results, sizeof(UA_NodeId) * results_size * 2);
  174. if(!new_results) {
  175. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  176. break;
  177. }
  178. results = new_results;
  179. results_size *= 2;
  180. }
  181. retval = UA_NodeId_copy(&node->references[i].targetId.nodeId, &results[last]);
  182. if(retval != UA_STATUSCODE_GOOD) {
  183. last--; // for array_delete
  184. break;
  185. }
  186. }
  187. UA_NodeStore_release(node);
  188. } while(++index <= last && retval == UA_STATUSCODE_GOOD);
  189. if(retval) {
  190. UA_Array_delete(results, &UA_TYPES[UA_TYPES_NODEID], last);
  191. return retval;
  192. }
  193. *reftypes = results;
  194. *reftypes_count = last + 1;
  195. return UA_STATUSCODE_GOOD;
  196. }
  197. static void removeCp(struct ContinuationPointEntry *cp, UA_Session* session){
  198. session->availableContinuationPoints++;
  199. UA_ByteString_deleteMembers(&cp->identifier);
  200. UA_BrowseDescription_deleteMembers(&cp->browseDescription);
  201. LIST_REMOVE(cp, pointers);
  202. UA_free(cp);
  203. }
  204. /**
  205. * Results for a single browsedescription. This is the inner loop for both Browse and BrowseNext
  206. * @param session Session to save continuationpoints
  207. * @param ns The nodstore where the to-be-browsed node can be found
  208. * @param cp If cp points to a continuationpoint, we continue from there.
  209. * If cp is null, we can add a new continuation point if possible and necessary.
  210. * @param descr If no cp is set, we take the browsedescription from there
  211. * @param maxrefs The maximum number of references the client has requested
  212. * @param result The entry in the request
  213. */
  214. static void browse(UA_Server *server, UA_Session *session, UA_NodeStore *ns, struct ContinuationPointEntry *cp,
  215. const UA_BrowseDescription *descr, UA_UInt32 maxrefs, UA_BrowseResult *result) {
  216. UA_UInt32 continuationIndex = 0;
  217. size_t referencesCount = 0;
  218. UA_Int32 referencesIndex = 0;
  219. /* set the browsedescription if a cp is given */
  220. if(cp) {
  221. descr = &cp->browseDescription;
  222. maxrefs = cp->maxReferences;
  223. continuationIndex = cp->continuationIndex;
  224. }
  225. /* is the browsedirection valid? */
  226. if(descr->browseDirection != UA_BROWSEDIRECTION_BOTH &&
  227. descr->browseDirection != UA_BROWSEDIRECTION_FORWARD &&
  228. descr->browseDirection != UA_BROWSEDIRECTION_INVERSE) {
  229. result->statusCode = UA_STATUSCODE_BADBROWSEDIRECTIONINVALID;
  230. return;
  231. }
  232. /* get the references that match the browsedescription */
  233. size_t relevant_refs_size = 0;
  234. UA_NodeId *relevant_refs = UA_NULL;
  235. UA_Boolean all_refs = UA_NodeId_isNull(&descr->referenceTypeId);
  236. if(!all_refs) {
  237. if(descr->includeSubtypes) {
  238. result->statusCode = findsubtypes(ns, &descr->referenceTypeId, &relevant_refs, &relevant_refs_size);
  239. if(result->statusCode != UA_STATUSCODE_GOOD)
  240. return;
  241. } else {
  242. const UA_Node *rootRef = UA_NodeStore_get(ns, &descr->referenceTypeId);
  243. if(!rootRef) {
  244. result->statusCode = UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  245. return;
  246. } else if(rootRef->nodeClass != UA_NODECLASS_REFERENCETYPE) {
  247. UA_NodeStore_release(rootRef);
  248. result->statusCode = UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  249. return;
  250. }
  251. UA_NodeStore_release(rootRef);
  252. relevant_refs = (UA_NodeId*)(uintptr_t)&descr->referenceTypeId;
  253. relevant_refs_size = 1;
  254. }
  255. }
  256. /* get the node */
  257. const UA_Node *node = UA_NodeStore_get(ns, &descr->nodeId);
  258. if(!node) {
  259. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  260. if(!all_refs && descr->includeSubtypes)
  261. UA_Array_delete(relevant_refs, &UA_TYPES[UA_TYPES_NODEID], relevant_refs_size);
  262. return;
  263. }
  264. /* if the node has no references, just return */
  265. if(node->referencesSize <= 0) {
  266. result->referencesSize = 0;
  267. UA_NodeStore_release(node);
  268. if(!all_refs && descr->includeSubtypes)
  269. UA_Array_delete(relevant_refs, &UA_TYPES[UA_TYPES_NODEID], relevant_refs_size);
  270. return;
  271. }
  272. /* how many references can we return at most? */
  273. UA_UInt32 real_maxrefs = maxrefs;
  274. if(real_maxrefs == 0)
  275. real_maxrefs = node->referencesSize;
  276. result->references = UA_malloc(sizeof(UA_ReferenceDescription) * real_maxrefs);
  277. if(!result->references) {
  278. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  279. goto cleanup;
  280. }
  281. /* loop over the node's references */
  282. size_t skipped = 0;
  283. for(; referencesIndex < node->referencesSize && referencesCount < real_maxrefs; referencesIndex++) {
  284. const UA_Node *current = relevant_node(server, ns, descr, all_refs, &node->references[referencesIndex],
  285. relevant_refs, relevant_refs_size);
  286. if(!current)
  287. continue;
  288. if(skipped < continuationIndex) {
  289. UA_NodeStore_release(current);
  290. skipped++;
  291. continue;
  292. }
  293. UA_StatusCode retval = fillrefdescr(ns, current, &node->references[referencesIndex], descr->resultMask,
  294. &result->references[referencesCount]);
  295. UA_NodeStore_release(current);
  296. if(retval != UA_STATUSCODE_GOOD) {
  297. UA_Array_delete(result->references, &UA_TYPES[UA_TYPES_REFERENCEDESCRIPTION], referencesCount);
  298. result->references = UA_NULL;
  299. result->referencesSize = 0;
  300. result->statusCode = UA_STATUSCODE_UNCERTAINNOTALLNODESAVAILABLE;
  301. goto cleanup;
  302. }
  303. referencesCount++;
  304. }
  305. result->referencesSize = referencesCount;
  306. if(referencesCount == 0) {
  307. UA_free(result->references);
  308. result->references = UA_NULL;
  309. }
  310. cleanup:
  311. UA_NodeStore_release(node);
  312. if(!all_refs && descr->includeSubtypes)
  313. UA_Array_delete(relevant_refs, &UA_TYPES[UA_TYPES_NODEID], relevant_refs_size);
  314. if(result->statusCode != UA_STATUSCODE_GOOD)
  315. return;
  316. /* create, update, delete continuation points */
  317. if(cp) {
  318. if(referencesIndex == node->referencesSize) {
  319. /* all done, remove a finished continuationPoint */
  320. removeCp(cp, session);
  321. } else {
  322. /* update the cp and return the cp identifier */
  323. cp->continuationIndex += referencesCount;
  324. UA_ByteString_copy(&cp->identifier, &result->continuationPoint);
  325. }
  326. } else if(maxrefs != 0 && referencesCount >= maxrefs) {
  327. /* create a cp */
  328. if(session->availableContinuationPoints <= 0 || !(cp = UA_malloc(sizeof(struct ContinuationPointEntry)))) {
  329. result->statusCode = UA_STATUSCODE_BADNOCONTINUATIONPOINTS;
  330. return;
  331. }
  332. UA_BrowseDescription_copy(descr, &cp->browseDescription);
  333. cp->maxReferences = maxrefs;
  334. cp->continuationIndex = referencesCount;
  335. UA_Guid *ident = UA_Guid_new();
  336. UA_UInt32 seed = (uintptr_t)cp;
  337. *ident = UA_Guid_random(&seed);
  338. cp->identifier.data = (UA_Byte*)ident;
  339. cp->identifier.length = sizeof(UA_Guid);
  340. UA_ByteString_copy(&cp->identifier, &result->continuationPoint);
  341. /* store the cp */
  342. LIST_INSERT_HEAD(&session->continuationPoints, cp, pointers);
  343. session->availableContinuationPoints--;
  344. }
  345. }
  346. void Service_Browse(UA_Server *server, UA_Session *session, const UA_BrowseRequest *request,
  347. UA_BrowseResponse *response) {
  348. if(!UA_NodeId_isNull(&request->view.viewId)) {
  349. response->responseHeader.serviceResult = UA_STATUSCODE_BADVIEWIDUNKNOWN;
  350. return;
  351. }
  352. if(request->nodesToBrowseSize <= 0) {
  353. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  354. return;
  355. }
  356. size_t size = request->nodesToBrowseSize;
  357. response->results = UA_Array_new(&UA_TYPES[UA_TYPES_BROWSERESULT], size);
  358. if(!response->results) {
  359. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  360. return;
  361. }
  362. response->resultsSize = size;
  363. #ifdef UA_EXTERNAL_NAMESPACES
  364. #ifdef NO_ALLOCA
  365. UA_Boolean isExternal[size];
  366. UA_UInt32 indices[size];
  367. #else
  368. UA_Boolean *isExternal = UA_alloca(sizeof(UA_Boolean) * size);
  369. UA_UInt32 *indices = UA_alloca(sizeof(UA_UInt32) * size);
  370. #endif /*NO_ALLOCA */
  371. UA_memset(isExternal, UA_FALSE, sizeof(UA_Boolean) * size);
  372. for(size_t j = 0; j < server->externalNamespacesSize; j++) {
  373. size_t indexSize = 0;
  374. for(size_t i = 0; i < size; i++) {
  375. if(request->nodesToBrowse[i].nodeId.namespaceIndex != server->externalNamespaces[j].index)
  376. continue;
  377. isExternal[i] = UA_TRUE;
  378. indices[indexSize] = i;
  379. indexSize++;
  380. }
  381. if(indexSize == 0)
  382. continue;
  383. UA_ExternalNodeStore *ens = &server->externalNamespaces[j].externalNodeStore;
  384. ens->browseNodes(ens->ensHandle, &request->requestHeader, request->nodesToBrowse, indices, indexSize,
  385. request->requestedMaxReferencesPerNode, response->results, response->diagnosticInfos);
  386. }
  387. #endif
  388. for(size_t i = 0; i < size; i++) {
  389. #ifdef UA_EXTERNAL_NAMESPACES
  390. if(!isExternal[i])
  391. #endif
  392. browse(server, session, server->nodestore, UA_NULL, &request->nodesToBrowse[i],
  393. request->requestedMaxReferencesPerNode, &response->results[i]);
  394. }
  395. }
  396. void Service_BrowseNext(UA_Server *server, UA_Session *session, const UA_BrowseNextRequest *request,
  397. UA_BrowseNextResponse *response) {
  398. if(request->continuationPointsSize <= 0) {
  399. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  400. return;
  401. }
  402. size_t size = request->continuationPointsSize;
  403. if(!request->releaseContinuationPoints) {
  404. /* continue with the cp */
  405. response->results = UA_Array_new(&UA_TYPES[UA_TYPES_BROWSERESULT], size);
  406. if(!response->results) {
  407. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  408. return;
  409. }
  410. response->resultsSize = size;
  411. for(size_t i = 0; i < size; i++) {
  412. struct ContinuationPointEntry *cp;
  413. LIST_FOREACH(cp, &session->continuationPoints, pointers) {
  414. if(UA_ByteString_equal(&cp->identifier, &request->continuationPoints[i])) {
  415. browse(server, session, server->nodestore, cp, UA_NULL, 0, &response->results[i]);
  416. break;
  417. }
  418. }
  419. if(!cp)
  420. response->results[i].statusCode = UA_STATUSCODE_BADCONTINUATIONPOINTINVALID;
  421. }
  422. } else {
  423. /* remove the cp */
  424. response->results = UA_Array_new(&UA_TYPES[UA_TYPES_BROWSERESULT], size);
  425. if(!response->results) {
  426. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  427. return;
  428. }
  429. response->resultsSize = size;
  430. for(size_t i = 0; i < size; i++) {
  431. struct ContinuationPointEntry *cp = UA_NULL;
  432. LIST_FOREACH(cp, &session->continuationPoints, pointers) {
  433. if(UA_ByteString_equal(&cp->identifier, &request->continuationPoints[i])) {
  434. removeCp(cp, session);
  435. break;
  436. }
  437. }
  438. if(!cp)
  439. response->results[i].statusCode = UA_STATUSCODE_BADCONTINUATIONPOINTINVALID;
  440. }
  441. }
  442. }
  443. /***********************/
  444. /* TranslateBrowsePath */
  445. /***********************/
  446. static UA_StatusCode
  447. walkBrowsePath(UA_Server *server, UA_Session *session, const UA_Node *node, const UA_RelativePath *path,
  448. UA_Int32 pathindex, UA_BrowsePathTarget **targets, UA_Int32 *targets_size,
  449. UA_Int32 *target_count)
  450. {
  451. const UA_RelativePathElement *elem = &path->elements[pathindex];
  452. if(elem->targetName.name.length == -1)
  453. return UA_STATUSCODE_BADBROWSENAMEINVALID;
  454. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  455. UA_NodeId *reftypes;
  456. size_t reftypes_count = 1; // all_refs or no subtypes => 1
  457. UA_Boolean all_refs = UA_FALSE;
  458. if(UA_NodeId_isNull(&elem->referenceTypeId))
  459. all_refs = UA_TRUE;
  460. else if(!elem->includeSubtypes)
  461. reftypes = (UA_NodeId*)(uintptr_t)&elem->referenceTypeId; // ptr magic due to const cast
  462. else {
  463. retval = findsubtypes(server->nodestore, &elem->referenceTypeId, &reftypes, &reftypes_count);
  464. if(retval != UA_STATUSCODE_GOOD)
  465. return retval;
  466. }
  467. for(UA_Int32 i = 0; i < node->referencesSize && retval == UA_STATUSCODE_GOOD; i++) {
  468. UA_Boolean match = all_refs;
  469. for(size_t j = 0; j < reftypes_count && !match; j++) {
  470. if(node->references[i].isInverse == elem->isInverse &&
  471. UA_NodeId_equal(&node->references[i].referenceTypeId, &reftypes[j]))
  472. match = UA_TRUE;
  473. }
  474. if(!match)
  475. continue;
  476. // get the node, todo: expandednodeid
  477. const UA_Node *next = UA_NodeStore_get(server->nodestore, &node->references[i].targetId.nodeId);
  478. if(!next)
  479. continue;
  480. // test the browsename
  481. if(elem->targetName.namespaceIndex != next->browseName.namespaceIndex ||
  482. !UA_String_equal(&elem->targetName.name, &next->browseName.name)) {
  483. UA_NodeStore_release(next);
  484. continue;
  485. }
  486. if(pathindex + 1 < path->elementsSize) {
  487. // recursion if the path is longer
  488. retval = walkBrowsePath(server, session, next, path, pathindex + 1,
  489. targets, targets_size, target_count);
  490. UA_NodeStore_release(next);
  491. } else {
  492. // add the browsetarget
  493. if(*target_count >= *targets_size) {
  494. UA_BrowsePathTarget *newtargets;
  495. newtargets = UA_realloc(targets, sizeof(UA_BrowsePathTarget) * (*targets_size) * 2);
  496. if(!newtargets) {
  497. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  498. UA_NodeStore_release(next);
  499. break;
  500. }
  501. *targets = newtargets;
  502. *targets_size *= 2;
  503. }
  504. UA_BrowsePathTarget *res = *targets;
  505. UA_ExpandedNodeId_init(&res[*target_count].targetId);
  506. retval = UA_NodeId_copy(&next->nodeId, &res[*target_count].targetId.nodeId);
  507. UA_NodeStore_release(next);
  508. if(retval != UA_STATUSCODE_GOOD)
  509. break;
  510. res[*target_count].remainingPathIndex = UA_UINT32_MAX;
  511. *target_count += 1;
  512. }
  513. }
  514. if(!all_refs && elem->includeSubtypes)
  515. UA_Array_delete(reftypes, &UA_TYPES[UA_TYPES_NODEID], (UA_Int32)reftypes_count);
  516. return retval;
  517. }
  518. static void translateBrowsePath(UA_Server *server, UA_Session *session, const UA_BrowsePath *path,
  519. UA_BrowsePathResult *result) {
  520. if(path->relativePath.elementsSize <= 0) {
  521. result->statusCode = UA_STATUSCODE_BADNOTHINGTODO;
  522. return;
  523. }
  524. UA_Int32 arraySize = 10;
  525. result->targets = UA_malloc(sizeof(UA_BrowsePathTarget) * arraySize);
  526. if(!result->targets) {
  527. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  528. return;
  529. }
  530. result->targetsSize = 0;
  531. const UA_Node *firstNode = UA_NodeStore_get(server->nodestore, &path->startingNode);
  532. if(!firstNode) {
  533. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  534. UA_free(result->targets);
  535. result->targets = UA_NULL;
  536. return;
  537. }
  538. result->statusCode = walkBrowsePath(server, session, firstNode, &path->relativePath, 0,
  539. &result->targets, &arraySize, &result->targetsSize);
  540. UA_NodeStore_release(firstNode);
  541. if(result->targetsSize == 0 && result->statusCode == UA_STATUSCODE_GOOD)
  542. result->statusCode = UA_STATUSCODE_BADNOMATCH;
  543. if(result->statusCode != UA_STATUSCODE_GOOD) {
  544. UA_Array_delete(result->targets, &UA_TYPES[UA_TYPES_BROWSEPATHTARGET], result->targetsSize);
  545. result->targets = UA_NULL;
  546. result->targetsSize = -1;
  547. }
  548. }
  549. void Service_TranslateBrowsePathsToNodeIds(UA_Server *server, UA_Session *session,
  550. const UA_TranslateBrowsePathsToNodeIdsRequest *request,
  551. UA_TranslateBrowsePathsToNodeIdsResponse *response) {
  552. if(request->browsePathsSize <= 0) {
  553. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  554. return;
  555. }
  556. size_t size = request->browsePathsSize;
  557. response->results = UA_Array_new(&UA_TYPES[UA_TYPES_BROWSEPATHRESULT], size);
  558. if(!response->results) {
  559. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  560. return;
  561. }
  562. #ifdef UA_EXTERNAL_NAMESPACES
  563. #ifdef NO_ALLOCA
  564. UA_Boolean isExternal[size];
  565. UA_UInt32 indices[size];
  566. #else
  567. UA_Boolean *isExternal = UA_alloca(sizeof(UA_Boolean) * size);
  568. UA_UInt32 *indices = UA_alloca(sizeof(UA_UInt32) * size);
  569. #endif /*NO_ALLOCA */
  570. UA_memset(isExternal, UA_FALSE, sizeof(UA_Boolean) * size);
  571. for(size_t j = 0; j < server->externalNamespacesSize; j++) {
  572. size_t indexSize = 0;
  573. for(size_t i = 0;i < size;i++) {
  574. if(request->browsePaths[i].startingNode.namespaceIndex != server->externalNamespaces[j].index)
  575. continue;
  576. isExternal[i] = UA_TRUE;
  577. indices[indexSize] = i;
  578. indexSize++;
  579. }
  580. if(indexSize == 0)
  581. continue;
  582. UA_ExternalNodeStore *ens = &server->externalNamespaces[j].externalNodeStore;
  583. ens->translateBrowsePathsToNodeIds(ens->ensHandle, &request->requestHeader, request->browsePaths,
  584. indices, indexSize, response->results, response->diagnosticInfos);
  585. }
  586. #endif
  587. response->resultsSize = size;
  588. for(size_t i = 0; i < size; i++) {
  589. #ifdef UA_EXTERNAL_NAMESPACES
  590. if(!isExternal[i])
  591. #endif
  592. translateBrowsePath(server, session, &request->browsePaths[i], &response->results[i]);
  593. }
  594. }
  595. void Service_RegisterNodes(UA_Server *server, UA_Session *session, const UA_RegisterNodesRequest *request,
  596. UA_RegisterNodesResponse *response) {
  597. //TODO: hang the nodeids to the session if really needed
  598. response->responseHeader.timestamp = UA_DateTime_now();
  599. if(request->nodesToRegisterSize <= 0)
  600. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  601. else {
  602. response->responseHeader.serviceResult =
  603. UA_Array_copy(request->nodesToRegister, (void**)&response->registeredNodeIds, &UA_TYPES[UA_TYPES_NODEID],
  604. request->nodesToRegisterSize);
  605. if(response->responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  606. response->registeredNodeIdsSize = request->nodesToRegisterSize;
  607. }
  608. }
  609. void Service_UnregisterNodes(UA_Server *server, UA_Session *session, const UA_UnregisterNodesRequest *request,
  610. UA_UnregisterNodesResponse *response) {
  611. //TODO: remove the nodeids from the session if really needed
  612. response->responseHeader.timestamp = UA_DateTime_now();
  613. if(request->nodesToUnregisterSize==0)
  614. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  615. }