/src/open62541_15/src/server/ua_services_view.c
Line | Count | Source |
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 | | |
19 | | #include "ua_server_internal.h" |
20 | | #include "ua_services.h" |
21 | | #include "ziptree.h" |
22 | | |
23 | 105M | #define UA_MAX_TREE_RECURSE 50 /* How deep up/down the tree do we recurse at most? */ |
24 | | |
25 | | static UA_UInt32 |
26 | 28.6M | resultMask2AttributesMask(UA_UInt32 resultMask) { |
27 | 28.6M | UA_UInt32 result = 0; |
28 | 28.6M | if(resultMask & UA_BROWSERESULTMASK_NODECLASS) |
29 | 16.4M | result |= UA_NODEATTRIBUTESMASK_NODECLASS; |
30 | 28.6M | if(resultMask & UA_BROWSERESULTMASK_BROWSENAME) |
31 | 17.1M | result |= UA_NODEATTRIBUTESMASK_BROWSENAME; |
32 | 28.6M | if(resultMask & UA_BROWSERESULTMASK_DISPLAYNAME) |
33 | 0 | result |= UA_NODEATTRIBUTESMASK_DISPLAYNAME; |
34 | 28.6M | return result; |
35 | 28.6M | } |
36 | | |
37 | | UA_StatusCode |
38 | | referenceTypeIndices(UA_Server *server, const UA_NodeId *refType, |
39 | 27.8M | UA_ReferenceTypeSet *indices, UA_Boolean includeSubtypes) { |
40 | 27.8M | if(UA_NodeId_isNull(refType)) { |
41 | 406 | *indices = UA_REFERENCETYPESET_ALL; |
42 | 406 | return UA_STATUSCODE_GOOD; |
43 | 406 | } |
44 | | |
45 | 27.8M | UA_ReferenceTypeSet_init(indices); |
46 | | |
47 | | /* Get the node with only the NodeClass attribute. If it is a |
48 | | * ReferenceTypeNode, then the indices are always included, as this is an |
49 | | * open62541 specific field (not selectable via the attribute id). */ |
50 | 27.8M | const UA_Node *refNode = |
51 | 27.8M | UA_NODESTORE_GET_SELECTIVE(server, refType, |
52 | 27.8M | UA_NODEATTRIBUTESMASK_NODECLASS, |
53 | 27.8M | UA_REFERENCETYPESET_NONE, |
54 | 27.8M | UA_BROWSEDIRECTION_INVALID); |
55 | | |
56 | 27.8M | if(!refNode) |
57 | 0 | return UA_STATUSCODE_BADREFERENCETYPEIDINVALID; |
58 | | |
59 | 27.8M | if(refNode->head.nodeClass != UA_NODECLASS_REFERENCETYPE) { |
60 | 0 | UA_NODESTORE_RELEASE(server, refNode); |
61 | 0 | return UA_STATUSCODE_BADREFERENCETYPEIDINVALID; |
62 | 0 | } |
63 | | |
64 | 27.8M | if(!includeSubtypes) |
65 | 306k | *indices = UA_REFTYPESET(refNode->referenceTypeNode.referenceTypeIndex); |
66 | 27.5M | else |
67 | 27.5M | *indices = refNode->referenceTypeNode.subTypes; |
68 | | |
69 | 27.8M | UA_NODESTORE_RELEASE(server, refNode); |
70 | 27.8M | return UA_STATUSCODE_GOOD; |
71 | 27.8M | } |
72 | | |
73 | | static UA_Boolean |
74 | 16.2M | matchClassMask(const UA_Node *node, UA_UInt32 nodeClassMask) { |
75 | 16.2M | if(nodeClassMask != UA_NODECLASS_UNSPECIFIED && |
76 | 10.7M | (node->head.nodeClass & nodeClassMask) == 0) |
77 | 36.4k | return false; |
78 | 16.2M | return true; |
79 | 16.2M | } |
80 | | |
81 | | /****************/ |
82 | | /* IsNodeInTree */ |
83 | | /****************/ |
84 | | |
85 | | /* Internal method to check if a node is already upwards from a leaf node */ |
86 | | |
87 | | static enum ZIP_CMP |
88 | 212M | cmpRefTarget(const void *a, const void *b) { |
89 | 212M | const UA_ReferenceTarget *aa = (const UA_ReferenceTarget*)a; |
90 | 212M | const UA_ReferenceTarget *bb = (const UA_ReferenceTarget*)b; |
91 | 212M | return (enum ZIP_CMP)UA_NodePointer_order(aa->targetId, bb->targetId); |
92 | 212M | } |
93 | | |
94 | | typedef ZIP_HEAD(UA_ParentRefsTree, UA_ReferenceTargetTreeElem) UA_ParentRefsTree; |
95 | 136M | ZIP_FUNCTIONS(UA_ParentRefsTree, UA_ReferenceTargetTreeElem, idTreeEntry, ua_services_view.c:UA_ParentRefsTree_ZIP_INSERT Line | Count | Source | 95 | | ZIP_FUNCTIONS(UA_ParentRefsTree, UA_ReferenceTargetTreeElem, idTreeEntry, |
ua_services_view.c:UA_ParentRefsTree_ZIP_REMOVE Line | Count | Source | 95 | | ZIP_FUNCTIONS(UA_ParentRefsTree, UA_ReferenceTargetTreeElem, idTreeEntry, |
|
96 | | UA_NodePointer, target, cmpRefTarget) |
97 | | |
98 | | struct IsNodeInTreeContext { |
99 | | UA_Server *server; |
100 | | UA_NodePointer nodeToFind; |
101 | | UA_ParentRefsTree parents; |
102 | | UA_ReferenceTypeSet relevantRefs; |
103 | | UA_UInt16 depth; |
104 | | }; |
105 | | |
106 | | static void * |
107 | 77.6M | isNodeInTreeIterateCallback(void *context, UA_ReferenceTarget *t) { |
108 | 77.6M | struct IsNodeInTreeContext *tc = |
109 | 77.6M | (struct IsNodeInTreeContext*)context; |
110 | | |
111 | | /* Don't follow remote targets */ |
112 | 77.6M | if(!UA_NodePointer_isLocal(t->targetId)) |
113 | 0 | return NULL; |
114 | | |
115 | | /* Found the node? -> return non-NULL */ |
116 | 77.6M | if(UA_NodePointer_equal(tc->nodeToFind, t->targetId)) |
117 | 9.62M | return (void*)0x01; |
118 | | |
119 | | /* Prevent endless loop */ |
120 | 68.0M | if(ZIP_FIND(UA_ParentRefsTree, &tc->parents, &t->targetId)) |
121 | 0 | return NULL; |
122 | | |
123 | | /* Prevent pathological recursion depth */ |
124 | 68.0M | if(tc->depth >= UA_MAX_TREE_RECURSE) |
125 | 0 | return NULL; |
126 | | |
127 | | /* Get the node without attributes (if the NodeStore supports it) and only |
128 | | * the relevant references in inverse direction */ |
129 | 68.0M | const UA_Node *node = |
130 | 68.0M | UA_NODESTORE_GETFROMREF_SELECTIVE(tc->server, t->targetId, |
131 | 68.0M | UA_NODEATTRIBUTESMASK_NONE, |
132 | 68.0M | tc->relevantRefs, |
133 | 68.0M | UA_BROWSEDIRECTION_INVERSE); |
134 | 68.0M | if(!node) |
135 | 0 | return NULL; |
136 | | |
137 | | /* Add current NodeId to parents tree */ |
138 | 68.0M | UA_ReferenceTargetTreeElem stackElem; |
139 | 68.0M | stackElem.target = *t; |
140 | 68.0M | ZIP_INSERT(UA_ParentRefsTree, &tc->parents, &stackElem); |
141 | | |
142 | | /* Recurse into appropriate references starting from this tree */ |
143 | 68.0M | tc->depth++; |
144 | 68.0M | void *res = NULL; |
145 | 198M | for(size_t i = 0; i < node->head.referencesSize && !res; i++) { |
146 | 130M | UA_NodeReferenceKind *rk = &node->head.references[i]; |
147 | | /* Search upwards in the tree */ |
148 | 130M | if(!rk->isInverse) |
149 | 59.3M | continue; |
150 | | |
151 | | /* Consider only the indicated reference types */ |
152 | 71.5M | if(!UA_ReferenceTypeSet_contains(&tc->relevantRefs, rk->referenceTypeIndex)) |
153 | 7.55M | continue; |
154 | | |
155 | 64.0M | res = UA_NodeReferenceKind_iterate(rk, isNodeInTreeIterateCallback, tc); |
156 | 64.0M | } |
157 | 68.0M | tc->depth--; |
158 | | |
159 | | /* Clean up */ |
160 | 68.0M | UA_NODESTORE_RELEASE(tc->server, node); |
161 | 68.0M | ZIP_REMOVE(UA_ParentRefsTree, &tc->parents, &stackElem); |
162 | 68.0M | return res; |
163 | 68.0M | } |
164 | | |
165 | | UA_Boolean |
166 | | isNodeInTree(UA_Server *server, const UA_NodeId *leafNode, |
167 | | const UA_NodeId *nodeToFind, |
168 | 13.6M | const UA_ReferenceTypeSet *relevantRefs) { |
169 | 13.6M | struct IsNodeInTreeContext ctx; |
170 | 13.6M | memset(&ctx, 0, sizeof(struct IsNodeInTreeContext)); |
171 | 13.6M | ctx.server = server; |
172 | 13.6M | ctx.nodeToFind = UA_NodePointer_fromNodeId(nodeToFind); |
173 | 13.6M | ctx.relevantRefs = *relevantRefs; |
174 | 13.6M | UA_ReferenceTarget tmpTarget; |
175 | 13.6M | memset(&tmpTarget, 0, sizeof(UA_ReferenceTarget)); |
176 | 13.6M | tmpTarget.targetId = UA_NodePointer_fromNodeId(leafNode); |
177 | 13.6M | return (isNodeInTreeIterateCallback(&ctx, &tmpTarget) != NULL); |
178 | 13.6M | } |
179 | | |
180 | | UA_Boolean |
181 | | isNodeInTree_singleRef(UA_Server *server, const UA_NodeId *leafNode, |
182 | 5.11M | const UA_NodeId *nodeToFind, const UA_Byte relevantRefTypeIndex) { |
183 | 5.11M | UA_ReferenceTypeSet reftypes = UA_REFTYPESET(relevantRefTypeIndex); |
184 | 5.11M | return isNodeInTree(server, leafNode, nodeToFind, &reftypes); |
185 | 5.11M | } |
186 | | |
187 | | static enum ZIP_CMP |
188 | 15.9M | cmpTarget(const void *a, const void *b) { |
189 | 15.9M | const RefEntry *aa = (const RefEntry*)a; |
190 | 15.9M | const RefEntry *bb = (const RefEntry*)b; |
191 | 15.9M | if(aa->targetHash < bb->targetHash) |
192 | 12.2M | return ZIP_CMP_LESS; |
193 | 3.67M | if(aa->targetHash > bb->targetHash) |
194 | 3.27M | return ZIP_CMP_MORE; |
195 | 401k | return (enum ZIP_CMP)UA_ExpandedNodeId_order(aa->target, bb->target); |
196 | 3.67M | } |
197 | | |
198 | 12.4M | ZIP_FUNCTIONS(RefHead, RefEntry, zipfields, RefEntry, zipfields, cmpTarget) |
199 | | |
200 | | UA_StatusCode |
201 | 5.69M | RefTree_init(RefTree *rt) { |
202 | 5.69M | rt->size = 0; |
203 | 5.69M | rt->capacity = 0; |
204 | 5.69M | ZIP_INIT(&rt->head); |
205 | 5.69M | size_t space = (sizeof(UA_ExpandedNodeId) + sizeof(RefEntry)) * UA_REFTREE_INITIAL_SIZE; |
206 | 5.69M | rt->targets = (UA_ExpandedNodeId*)UA_malloc(space); |
207 | 5.69M | if(!rt->targets) |
208 | 0 | return UA_STATUSCODE_BADOUTOFMEMORY; |
209 | 5.69M | rt->capacity = UA_REFTREE_INITIAL_SIZE; |
210 | 5.69M | return UA_STATUSCODE_GOOD; |
211 | 5.69M | } |
212 | | |
213 | | void |
214 | 558k | RefTree_clear(RefTree *rt) { |
215 | 2.25M | for(size_t i = 0; i < rt->size; i++) |
216 | 1.69M | UA_ExpandedNodeId_clear(&rt->targets[i]); |
217 | 558k | if(rt->targets) |
218 | 558k | UA_free(rt->targets); |
219 | 558k | } |
220 | | |
221 | | /* Double the capacity of the reftree */ |
222 | | static UA_StatusCode UA_INTERNAL_FUNC_ATTR_WARN_UNUSED_RESULT |
223 | 0 | RefTree_double(RefTree *rt) { |
224 | 0 | size_t capacity = rt->capacity * 2; |
225 | 0 | UA_assert(capacity > 0); |
226 | 0 | size_t space = (sizeof(UA_ExpandedNodeId) + sizeof(RefEntry)) * capacity; |
227 | 0 | UA_ExpandedNodeId *newTargets = (UA_ExpandedNodeId*)UA_realloc(rt->targets, space); |
228 | 0 | if(!newTargets) |
229 | 0 | return UA_STATUSCODE_BADOUTOFMEMORY; |
230 | | |
231 | | /* Move the entries to the new location */ |
232 | 0 | RefEntry *reArray = (RefEntry*) |
233 | 0 | ((uintptr_t)newTargets + (capacity * sizeof(UA_ExpandedNodeId))); |
234 | 0 | RefEntry *oldReArray = (RefEntry*) |
235 | 0 | ((uintptr_t)newTargets + (rt->capacity * sizeof(UA_ExpandedNodeId))); |
236 | 0 | memmove(reArray, oldReArray, rt->size * sizeof(RefEntry)); |
237 | | |
238 | | /* Reinsert all entries into the tree. The new pointer location has changed |
239 | | * their ziptree rank. */ |
240 | 0 | rt->head.root = NULL; |
241 | 0 | for(size_t i = 0; i < rt->size; i++) { |
242 | 0 | reArray[i].target = &newTargets[i]; |
243 | 0 | ZIP_INSERT(RefHead, &rt->head, &reArray[i]); |
244 | 0 | } |
245 | |
|
246 | 0 | rt->capacity = capacity; |
247 | 0 | rt->targets = newTargets; |
248 | 0 | return UA_STATUSCODE_GOOD; |
249 | 0 | } |
250 | | |
251 | | static UA_StatusCode |
252 | 12.8M | RefTree_add(RefTree *rt, UA_NodePointer target, UA_Boolean *duplicate) { |
253 | 12.8M | UA_ExpandedNodeId en = UA_NodePointer_toExpandedNodeId(target); |
254 | | |
255 | | /* Is the target already in the tree? */ |
256 | 12.8M | RefEntry dummy; |
257 | 12.8M | memset(&dummy, 0, sizeof(RefEntry)); |
258 | 12.8M | dummy.target = &en; |
259 | 12.8M | dummy.targetHash = UA_ExpandedNodeId_hash(&en); |
260 | 12.8M | if(ZIP_FIND(RefHead, &rt->head, &dummy)) { |
261 | 401k | if(duplicate) |
262 | 401k | *duplicate = true; |
263 | 401k | return UA_STATUSCODE_GOOD; |
264 | 401k | } |
265 | | |
266 | 12.4M | UA_StatusCode s = UA_STATUSCODE_GOOD; |
267 | 12.4M | if(rt->capacity <= rt->size) { |
268 | 0 | s = RefTree_double(rt); |
269 | 0 | if(s != UA_STATUSCODE_GOOD) |
270 | 0 | return s; |
271 | 0 | } |
272 | 12.4M | s = UA_ExpandedNodeId_copy(&en, &rt->targets[rt->size]); |
273 | 12.4M | if(s != UA_STATUSCODE_GOOD) |
274 | 0 | return s; |
275 | 12.4M | RefEntry *re = (RefEntry*)((uintptr_t)rt->targets + |
276 | 12.4M | (sizeof(UA_ExpandedNodeId) * rt->capacity) + |
277 | 12.4M | (sizeof(RefEntry) * rt->size)); |
278 | 12.4M | re->target = &rt->targets[rt->size]; |
279 | 12.4M | re->targetHash = dummy.targetHash; |
280 | 12.4M | ZIP_INSERT(RefHead, &rt->head, re); |
281 | 12.4M | rt->size++; |
282 | 12.4M | return UA_STATUSCODE_GOOD; |
283 | 12.4M | } |
284 | | |
285 | | UA_StatusCode |
286 | | RefTree_addNodeId(RefTree *rt, const UA_NodeId *target, |
287 | 12.8M | UA_Boolean *duplicate) { |
288 | 12.8M | return RefTree_add(rt, UA_NodePointer_fromNodeId(target), duplicate); |
289 | 12.8M | } |
290 | | |
291 | | UA_Boolean |
292 | 0 | RefTree_contains(RefTree *rt, const UA_ExpandedNodeId *target) { |
293 | 0 | RefEntry dummy; |
294 | 0 | dummy.target = target; |
295 | 0 | dummy.targetHash = UA_ExpandedNodeId_hash(target); |
296 | 0 | return !!ZIP_FIND(RefHead, &rt->head, &dummy); |
297 | 0 | } |
298 | | |
299 | | UA_Boolean |
300 | 0 | RefTree_containsNodeId(RefTree *rt, const UA_NodeId *target) { |
301 | 0 | UA_ExpandedNodeId en; |
302 | 0 | en.nodeId = *target; |
303 | 0 | en.namespaceUri = UA_STRING_NULL; |
304 | 0 | en.serverIndex = 0; |
305 | 0 | return RefTree_contains(rt, &en); |
306 | 0 | } |
307 | | |
308 | | /********************/ |
309 | | /* Browse Recursive */ |
310 | | /********************/ |
311 | | |
312 | | struct BrowseRecursiveContext { |
313 | | UA_Server *server; |
314 | | RefTree *rt; |
315 | | UA_UInt16 depth; |
316 | | UA_BrowseDirection browseDirection; |
317 | | UA_ReferenceTypeSet refTypes; |
318 | | UA_UInt32 nodeClassMask; |
319 | | UA_StatusCode status; |
320 | | UA_Boolean includeStartNodes; |
321 | | }; |
322 | | |
323 | | static void * |
324 | 37.2M | browseRecursiveCallback(void *context, UA_ReferenceTarget *t) { |
325 | 37.2M | struct BrowseRecursiveContext *brc = |
326 | 37.2M | (struct BrowseRecursiveContext*)context; |
327 | | |
328 | | /* Have we reached the max recursion depth? */ |
329 | 37.2M | if(brc->depth >= UA_MAX_TREE_RECURSE) |
330 | 0 | return NULL; |
331 | | |
332 | | /* Is this a non-local reference? If yes include it in the returned set. */ |
333 | 37.2M | if(!UA_NodePointer_isLocal(t->targetId)) { |
334 | 0 | brc->status = RefTree_add(brc->rt, t->targetId, NULL); |
335 | 0 | return (brc->status == UA_STATUSCODE_GOOD) ? NULL : (void*)0x01; |
336 | 0 | } |
337 | | |
338 | | /* We only look at the NodeClass attribute and a subset of the references. |
339 | | * Get a node with only these elements if the NodeStore supports that. */ |
340 | 37.2M | const UA_Node *node = |
341 | 37.2M | UA_NODESTORE_GETFROMREF_SELECTIVE(brc->server, t->targetId, |
342 | 37.2M | UA_NODEATTRIBUTESMASK_NODECLASS, |
343 | 37.2M | brc->refTypes, brc->browseDirection); |
344 | 37.2M | if(!node) |
345 | 0 | return NULL; |
346 | | |
347 | | /* Add the current node if we don't want to skip it as a start node and it |
348 | | * matches the nodeClassMask filter. Recurse into the children in any |
349 | | * case. */ |
350 | 37.2M | void *res = NULL; |
351 | 37.2M | const UA_NodeHead *head = &node->head; |
352 | 37.2M | if((brc->includeStartNodes || brc->depth > 0) && |
353 | 7.32M | matchClassMask(node, brc->nodeClassMask)) { |
354 | 7.32M | UA_Boolean duplicate = false; |
355 | 7.32M | brc->status = RefTree_addNodeId(brc->rt, &head->nodeId, &duplicate); |
356 | 7.32M | if(duplicate || brc->status != UA_STATUSCODE_GOOD) |
357 | 401k | goto cleanup; |
358 | 7.32M | } |
359 | | |
360 | | /* Recurse */ |
361 | 36.8M | brc->depth++; |
362 | 127M | for(size_t i = 0; i < head->referencesSize && !res; i++) { |
363 | 91.0M | UA_NodeReferenceKind *rk = &head->references[i]; |
364 | | |
365 | | /* Reference in the right direction? */ |
366 | 91.0M | if(rk->isInverse && brc->browseDirection == UA_BROWSEDIRECTION_FORWARD) |
367 | 31.0M | continue; |
368 | 60.0M | if(!rk->isInverse && brc->browseDirection == UA_BROWSEDIRECTION_INVERSE) |
369 | 17.3M | continue; |
370 | | |
371 | | /* Is the reference part of the hierarchy of references we look for? */ |
372 | 42.6M | if(!UA_ReferenceTypeSet_contains(&brc->refTypes, rk->referenceTypeIndex)) |
373 | 35.3M | continue; |
374 | | |
375 | 7.32M | res = UA_NodeReferenceKind_iterate(rk, browseRecursiveCallback, brc); |
376 | 7.32M | } |
377 | 36.8M | brc->depth--; |
378 | | |
379 | 37.2M | cleanup: |
380 | 37.2M | UA_NODESTORE_RELEASE(brc->server, node); |
381 | 37.2M | return (brc->status == UA_STATUSCODE_GOOD) ? NULL : (void*)0x01; |
382 | 36.8M | } |
383 | | |
384 | | UA_StatusCode |
385 | | browseRecursive(UA_Server *server, size_t startNodesSize, const UA_NodeId *startNodes, |
386 | | UA_BrowseDirection browseDirection, const UA_ReferenceTypeSet *refTypes, |
387 | | UA_UInt32 nodeClassMask, UA_Boolean includeStartNodes, |
388 | 211k | size_t *resultsSize, UA_ExpandedNodeId **results) { |
389 | 211k | RefTree rt; |
390 | 211k | UA_StatusCode retval = RefTree_init(&rt); |
391 | 211k | if(retval != UA_STATUSCODE_GOOD) |
392 | 0 | return retval; |
393 | | |
394 | 211k | struct BrowseRecursiveContext brc; |
395 | 211k | brc.server = server; |
396 | 211k | brc.rt = &rt; |
397 | 211k | brc.depth = 0; |
398 | 211k | brc.refTypes = *refTypes; |
399 | 211k | brc.nodeClassMask = nodeClassMask; |
400 | 211k | brc.status = UA_STATUSCODE_GOOD; |
401 | 211k | brc.includeStartNodes = includeStartNodes; |
402 | | |
403 | 423k | for(size_t i = 0; i < startNodesSize && brc.status == UA_STATUSCODE_GOOD; i++) { |
404 | 211k | UA_ReferenceTarget target; |
405 | 211k | target.targetId = UA_NodePointer_fromNodeId(&startNodes[i]); |
406 | | |
407 | | /* Call the inner recursive browse separately for the search direction. |
408 | | * Otherwise we might take one step up and another step down in the |
409 | | * search tree. */ |
410 | 211k | if(browseDirection == UA_BROWSEDIRECTION_FORWARD || |
411 | 211k | browseDirection == UA_BROWSEDIRECTION_BOTH) { |
412 | 0 | brc.browseDirection = UA_BROWSEDIRECTION_FORWARD; |
413 | 0 | browseRecursiveCallback(&brc, &target); |
414 | 0 | } |
415 | | |
416 | 211k | if(browseDirection == UA_BROWSEDIRECTION_INVERSE || |
417 | 211k | browseDirection == UA_BROWSEDIRECTION_BOTH) { |
418 | 211k | brc.browseDirection = UA_BROWSEDIRECTION_INVERSE; |
419 | 211k | browseRecursiveCallback(&brc, &target); |
420 | 211k | } |
421 | 211k | } |
422 | | |
423 | 211k | if(rt.size > 0 && brc.status == UA_STATUSCODE_GOOD) { |
424 | 204k | *results = rt.targets; |
425 | 204k | *resultsSize = rt.size; |
426 | 204k | } else { |
427 | 7.29k | RefTree_clear(&rt); |
428 | 7.29k | } |
429 | 211k | return brc.status; |
430 | 211k | } |
431 | | |
432 | | UA_StatusCode |
433 | | browseRecursiveRefTree(UA_Server *server, RefTree *rt, UA_BrowseDirection browseDirection, |
434 | 16.4M | const UA_ReferenceTypeSet *refTypes, UA_UInt32 nodeClassMask) { |
435 | 16.4M | struct BrowseRecursiveContext brc; |
436 | 16.4M | brc.server = server; |
437 | 16.4M | brc.rt = rt; |
438 | 16.4M | brc.depth = 0; |
439 | 16.4M | brc.refTypes = *refTypes; |
440 | 16.4M | brc.nodeClassMask = nodeClassMask; |
441 | 16.4M | brc.status = UA_STATUSCODE_GOOD; |
442 | 16.4M | brc.includeStartNodes = false; |
443 | | |
444 | 46.1M | for(size_t i = 0; i < rt->size && brc.status == UA_STATUSCODE_GOOD; i++) { |
445 | 29.7M | UA_ReferenceTarget target; |
446 | 29.7M | UA_ExpandedNodeId current = rt->targets[i]; |
447 | 29.7M | target.targetId = UA_NodePointer_fromExpandedNodeId(¤t); |
448 | | |
449 | | /* Call the inner recursive browse separately for the search direction. |
450 | | * Otherwise we might take one step up and another step down in the |
451 | | * search tree. */ |
452 | 29.7M | if(browseDirection == UA_BROWSEDIRECTION_FORWARD || |
453 | 17.8M | browseDirection == UA_BROWSEDIRECTION_BOTH) { |
454 | 17.8M | brc.browseDirection = UA_BROWSEDIRECTION_FORWARD; |
455 | 17.8M | browseRecursiveCallback(&brc, &target); |
456 | 17.8M | } |
457 | | |
458 | 29.7M | if(browseDirection == UA_BROWSEDIRECTION_INVERSE || |
459 | 17.8M | browseDirection == UA_BROWSEDIRECTION_BOTH) { |
460 | 11.8M | brc.browseDirection = UA_BROWSEDIRECTION_INVERSE; |
461 | 11.8M | browseRecursiveCallback(&brc, &target); |
462 | 11.8M | } |
463 | 29.7M | } |
464 | | |
465 | 16.4M | return brc.status; |
466 | 16.4M | } |
467 | | |
468 | | UA_StatusCode |
469 | | UA_Server_browseRecursive(UA_Server *server, const UA_BrowseDescription *bd, |
470 | 0 | size_t *resultsSize, UA_ExpandedNodeId **results) { |
471 | 0 | lockServer(server); |
472 | | |
473 | | /* Set the list of relevant reference types */ |
474 | 0 | UA_ReferenceTypeSet refTypes; |
475 | 0 | UA_StatusCode retval = referenceTypeIndices(server, &bd->referenceTypeId, |
476 | 0 | &refTypes, bd->includeSubtypes); |
477 | 0 | if(retval != UA_STATUSCODE_GOOD) { |
478 | 0 | unlockServer(server); |
479 | 0 | return retval; |
480 | 0 | } |
481 | | |
482 | | /* Browse */ |
483 | 0 | retval = browseRecursive(server, 1, &bd->nodeId, bd->browseDirection, |
484 | 0 | &refTypes, bd->nodeClassMask, false, resultsSize, results); |
485 | |
|
486 | 0 | unlockServer(server); |
487 | 0 | return retval; |
488 | 0 | } |
489 | | |
490 | | /**********/ |
491 | | /* Browse */ |
492 | | /**********/ |
493 | | |
494 | | typedef struct { |
495 | | size_t size; |
496 | | size_t capacity; |
497 | | UA_ReferenceDescription *descr; |
498 | | } RefResult; |
499 | | |
500 | | static UA_StatusCode UA_INTERNAL_FUNC_ATTR_WARN_UNUSED_RESULT |
501 | 19.7M | RefResult_init(RefResult *rr) { |
502 | 19.7M | memset(rr, 0, sizeof(RefResult)); |
503 | 19.7M | rr->descr = (UA_ReferenceDescription*) |
504 | 19.7M | UA_Array_new(UA_REFTREE_INITIAL_SIZE, &UA_TYPES[UA_TYPES_REFERENCEDESCRIPTION]); |
505 | 19.7M | if(!rr->descr) |
506 | 0 | return UA_STATUSCODE_BADOUTOFMEMORY; |
507 | 19.7M | rr->capacity = UA_REFTREE_INITIAL_SIZE; |
508 | 19.7M | rr->size = 0; |
509 | 19.7M | return UA_STATUSCODE_GOOD; |
510 | 19.7M | } |
511 | | |
512 | | static UA_StatusCode UA_INTERNAL_FUNC_ATTR_WARN_UNUSED_RESULT |
513 | 138k | RefResult_double(RefResult *rr) { |
514 | 138k | size_t newSize = rr->capacity * 2; |
515 | 138k | UA_ReferenceDescription *rd = (UA_ReferenceDescription*) |
516 | 138k | UA_realloc(rr->descr, newSize * sizeof(UA_ReferenceDescription)); |
517 | 138k | if(!rd) |
518 | 0 | return UA_STATUSCODE_BADOUTOFMEMORY; |
519 | 138k | memset(&rd[rr->size], 0, sizeof(UA_ReferenceDescription) * (newSize - rr->size)); |
520 | 138k | rr->descr = rd; |
521 | 138k | rr->capacity = newSize; |
522 | 138k | return UA_STATUSCODE_GOOD; |
523 | 138k | } |
524 | | |
525 | | static void |
526 | 17.7M | RefResult_clear(RefResult *rr) { |
527 | 17.7M | UA_assert(rr->descr != NULL); |
528 | 17.7M | for(size_t i = 0; i < rr->size; i++) |
529 | 0 | UA_ReferenceDescription_clear(&rr->descr[i]); |
530 | 17.7M | UA_free(rr->descr); |
531 | 17.7M | } |
532 | | |
533 | | struct ContinuationPoint { |
534 | | ContinuationPoint *next; |
535 | | UA_ByteString identifier; |
536 | | |
537 | | /* Parameters of the Browse Request */ |
538 | | UA_BrowseDescription browseDescription; |
539 | | UA_UInt32 maxReferences; |
540 | | UA_ReferenceTypeSet relevantReferences; |
541 | | |
542 | | /* The last reference target that was transmitted to the client. The results |
543 | | * list may be incomplete if the last target is removed or references added |
544 | | * between the calls to Browse/BrowseNext. */ |
545 | | UA_NodePointer lastTarget; |
546 | | UA_Byte lastRefKindIndex; |
547 | | UA_Boolean lastRefInverse; |
548 | | }; |
549 | | |
550 | | ContinuationPoint * |
551 | 0 | ContinuationPoint_clear(ContinuationPoint *cp) { |
552 | 0 | UA_ByteString_clear(&cp->identifier); |
553 | 0 | UA_BrowseDescription_clear(&cp->browseDescription); |
554 | 0 | UA_NodePointer_clear(&cp->lastTarget); |
555 | 0 | return cp->next; |
556 | 0 | } |
557 | | |
558 | | struct BrowseContext { |
559 | | /* Context */ |
560 | | ContinuationPoint *cp; |
561 | | UA_Server *server; |
562 | | UA_Session *session; |
563 | | UA_NodeReferenceKind *rk; |
564 | | UA_ReferenceTypeSet resultRefs; /* With additional references for type |
565 | | * lookups */ |
566 | | UA_Boolean activeCP; /* true during "forwarding" to the position of the last |
567 | | * reference target */ |
568 | | |
569 | | /* Results */ |
570 | | RefResult rr; |
571 | | UA_StatusCode status; |
572 | | UA_Boolean done; |
573 | | }; |
574 | | |
575 | | /* Target node on top of the stack */ |
576 | | static UA_StatusCode |
577 | | addReferenceDescription(struct BrowseContext *bc, UA_NodePointer nodeP, |
578 | 8.89M | const UA_Node *curr) { |
579 | 8.89M | UA_assert(curr); |
580 | 8.89M | UA_BrowseDescription *bd = &bc->cp->browseDescription; |
581 | | |
582 | | /* Ensure capacity is left */ |
583 | 8.89M | UA_StatusCode res = UA_STATUSCODE_GOOD; |
584 | 8.89M | if(bc->rr.size >= bc->rr.capacity) { |
585 | 138k | res = RefResult_double(&bc->rr); |
586 | 138k | if(res != UA_STATUSCODE_GOOD) |
587 | 0 | return res; |
588 | 138k | } |
589 | | |
590 | 8.89M | UA_ReferenceDescription *descr = &bc->rr.descr[bc->rr.size]; |
591 | | |
592 | | /* Fields without access to the actual node */ |
593 | 8.89M | UA_ExpandedNodeId en = UA_NodePointer_toExpandedNodeId(nodeP); |
594 | 8.89M | res = UA_ExpandedNodeId_copy(&en, &descr->nodeId); |
595 | 8.89M | if(bd->resultMask & UA_BROWSERESULTMASK_REFERENCETYPEID) { |
596 | 3.56M | const UA_NodeId *refTypeId = |
597 | 3.56M | UA_NODESTORE_GETREFERENCETYPEID(bc->server, bc->rk->referenceTypeIndex); |
598 | 3.56M | if(UA_LIKELY(refTypeId != NULL)) |
599 | 3.56M | res |= UA_NodeId_copy(refTypeId, &descr->referenceTypeId); |
600 | 0 | else |
601 | 0 | res |= UA_STATUSCODE_BADINTERNALERROR; |
602 | 3.56M | } |
603 | 8.89M | if(bd->resultMask & UA_BROWSERESULTMASK_ISFORWARD) |
604 | 0 | descr->isForward = !bc->rk->isInverse; |
605 | | |
606 | | /* Create fields that require access to the actual node */ |
607 | 8.89M | if(bd->resultMask & UA_BROWSERESULTMASK_NODECLASS) |
608 | 3.56M | descr->nodeClass = curr->head.nodeClass; |
609 | | |
610 | 8.89M | if(bd->resultMask & UA_BROWSERESULTMASK_BROWSENAME) |
611 | 3.93M | res |= UA_QualifiedName_copy(&curr->head.browseName, |
612 | 3.93M | &descr->browseName); |
613 | | |
614 | 8.89M | if(bd->resultMask & UA_BROWSERESULTMASK_DISPLAYNAME) { |
615 | 0 | UA_LocalizedText displayname = |
616 | 0 | UA_Session_getNodeDisplayName(bc->session, &curr->head); |
617 | 0 | res |= UA_LocalizedText_copy(&displayname, &descr->displayName); |
618 | 0 | } |
619 | | |
620 | 8.89M | if(bd->resultMask & UA_BROWSERESULTMASK_TYPEDEFINITION) { |
621 | 3.56M | if(curr->head.nodeClass == UA_NODECLASS_OBJECT || |
622 | 3.36M | curr->head.nodeClass == UA_NODECLASS_VARIABLE) { |
623 | 3.10M | const UA_Node *type = |
624 | 3.10M | getNodeType(bc->server, &curr->head, 0, |
625 | 3.10M | UA_REFERENCETYPESET_NONE, |
626 | 3.10M | UA_BROWSEDIRECTION_INVALID); |
627 | 3.10M | if(type) { |
628 | 3.10M | res |= UA_NodeId_copy(&type->head.nodeId, |
629 | 3.10M | &descr->typeDefinition.nodeId); |
630 | 3.10M | UA_NODESTORE_RELEASE(bc->server, type); |
631 | 3.10M | } |
632 | 3.10M | } |
633 | 3.56M | } |
634 | | |
635 | | /* Clean up and return */ |
636 | 8.89M | if(res != UA_STATUSCODE_GOOD) { |
637 | 0 | UA_ReferenceDescription_clear(descr); |
638 | 0 | return res; |
639 | 0 | } |
640 | 8.89M | bc->rr.size++; |
641 | 8.89M | return UA_STATUSCODE_GOOD; |
642 | 8.89M | } |
643 | | |
644 | | static void * |
645 | 8.93M | browseReferencTargetCallback(void *context, UA_ReferenceTarget *t) { |
646 | 8.93M | struct BrowseContext *bc = (struct BrowseContext*)context; |
647 | 8.93M | const UA_BrowseDescription *bd = &bc->cp->browseDescription; |
648 | 8.93M | ContinuationPoint *cp = bc->cp; |
649 | | |
650 | | /* Remote references are ignored */ |
651 | 8.93M | if(!UA_NodePointer_isLocal(t->targetId)) |
652 | 0 | return NULL; |
653 | | |
654 | | /* Include references only for figuring out the TypeDefinition within |
655 | | * addReferenceDescription. */ |
656 | 8.93M | UA_BrowseDirection direction = UA_BROWSEDIRECTION_INVALID; |
657 | 8.93M | UA_ReferenceTypeSet refs = UA_REFERENCETYPESET_NONE; |
658 | 8.93M | if(bd->resultMask & UA_BROWSERESULTMASK_TYPEDEFINITION) { |
659 | 3.56M | direction = UA_BROWSEDIRECTION_BOTH; |
660 | 3.56M | UA_ReferenceTypeSet_add(&refs, UA_REFERENCETYPEINDEX_HASTYPEDEFINITION); |
661 | 3.56M | UA_ReferenceTypeSet_add(&refs, UA_REFERENCETYPEINDEX_HASSUBTYPE); |
662 | 3.56M | } |
663 | | |
664 | | /* Get the node */ |
665 | 8.93M | const UA_Node *target = |
666 | 8.93M | UA_NODESTORE_GETFROMREF_SELECTIVE(bc->server, t->targetId, |
667 | 8.93M | resultMask2AttributesMask(bd->resultMask), |
668 | 8.93M | refs, direction); |
669 | 8.93M | if(!target) |
670 | 0 | return NULL; |
671 | | |
672 | | /* The node class has to match */ |
673 | 8.93M | if(!matchClassMask(target, bd->nodeClassMask)) { |
674 | 36.4k | UA_NODESTORE_RELEASE(bc->server, target); |
675 | 36.4k | return NULL; |
676 | 36.4k | } |
677 | | |
678 | | /* Reached maxrefs. Return the "abort" signal. */ |
679 | 8.89M | if(bc->rr.size >= cp->maxReferences) { |
680 | 0 | UA_NODESTORE_RELEASE(bc->server, target); |
681 | 0 | return (void*)0x01; |
682 | 0 | } |
683 | | |
684 | | /* Create the reference description */ |
685 | 8.89M | bc->status = addReferenceDescription(bc, t->targetId, target); |
686 | | |
687 | | /* Release the node */ |
688 | 8.89M | UA_NODESTORE_RELEASE(bc->server, target); |
689 | | |
690 | | /* Store as last target. The itarget-id is a shallow copy for now. */ |
691 | 8.89M | cp->lastTarget = t->targetId; |
692 | 8.89M | cp->lastRefKindIndex = bc->rk->referenceTypeIndex; |
693 | 8.89M | cp->lastRefInverse = bc->rk->isInverse; |
694 | | |
695 | | /* Abort if the status is not good. Also doesn't make a deep-copy of |
696 | | * cp->lastTarget after returning from here. */ |
697 | 8.89M | if(bc->status != UA_STATUSCODE_GOOD) { |
698 | 0 | UA_NodePointer_init(&cp->lastTarget); |
699 | 0 | return (void*)0x01; |
700 | 0 | } |
701 | 8.89M | return NULL; |
702 | 8.89M | } |
703 | | |
704 | | /* Returns whether the node / continuationpoint is done */ |
705 | | static void |
706 | 19.7M | browseWithNode(struct BrowseContext *bc, const UA_NodeHead *head ) { |
707 | 19.7M | ContinuationPoint *cp = bc->cp; |
708 | 19.7M | const UA_BrowseDescription *bd = &cp->browseDescription; |
709 | | |
710 | | /* Loop over the ReferenceKinds */ |
711 | 70.9M | for(size_t i = 0; i < head->referencesSize && bc->status == UA_STATUSCODE_GOOD; ++i) { |
712 | 51.1M | UA_NodeReferenceKind *rk = &head->references[i]; |
713 | | |
714 | | /* If the continuation point was previously used, skip forward to the |
715 | | * last ReferenceType that was transmitted */ |
716 | 51.1M | if(bc->activeCP && rk->referenceTypeIndex != cp->lastRefKindIndex) |
717 | 0 | continue; |
718 | 51.1M | if(bc->activeCP && rk->isInverse != cp->lastRefInverse) |
719 | 0 | continue; |
720 | | |
721 | | /* Reference in the right direction? */ |
722 | 51.1M | if(rk->isInverse && bd->browseDirection == UA_BROWSEDIRECTION_FORWARD) |
723 | 25.6M | continue; |
724 | 25.4M | if(!rk->isInverse && bd->browseDirection == UA_BROWSEDIRECTION_INVERSE) |
725 | 36.4k | continue; |
726 | | |
727 | | /* Is the reference part of the hierarchy of references we look for? */ |
728 | 25.4M | if(!UA_ReferenceTypeSet_contains(&cp->relevantReferences, rk->referenceTypeIndex)) |
729 | 23.1M | continue; |
730 | | |
731 | | /* We have a matching ReferenceKind */ |
732 | | |
733 | | /* Skip ahead to the target where the last continuation point stopped. |
734 | | * This temporarily modifies rk. */ |
735 | 2.28M | UA_ReferenceIdTree left = {NULL}, right = {NULL}; |
736 | 2.28M | size_t nextTargetIndex = 0; |
737 | 2.28M | if(bc->activeCP) { |
738 | 0 | if(rk->hasRefTree) { |
739 | | /* Unzip the tree until the continuation point. All NodeIds |
740 | | * larger than the last target are guaranteed to sit on the |
741 | | * right-hand side. */ |
742 | 0 | UA_ExpandedNodeId lastEn = |
743 | 0 | UA_NodePointer_toExpandedNodeId(cp->lastTarget); |
744 | 0 | UA_ReferenceTargetTreeElem key; |
745 | 0 | key.target.targetId = cp->lastTarget; |
746 | 0 | key.targetIdHash = UA_ExpandedNodeId_hash(&lastEn); |
747 | 0 | ZIP_UNZIP(UA_ReferenceIdTree, |
748 | 0 | (UA_ReferenceIdTree*)&rk->targets.tree.idRoot, |
749 | 0 | &key, &left, &right); |
750 | 0 | rk->targets.tree.idRoot = right.root; |
751 | 0 | } else { |
752 | | /* Iterate over the array to find the match */ |
753 | 0 | for(; nextTargetIndex < rk->targetsSize; nextTargetIndex++) { |
754 | 0 | UA_ReferenceTarget *t = &rk->targets.array[nextTargetIndex]; |
755 | 0 | if(UA_NodePointer_equal(cp->lastTarget, t->targetId)) |
756 | 0 | break; |
757 | 0 | } |
758 | 0 | if(nextTargetIndex == rk->targetsSize) { |
759 | | /* Not found - assume that this reference kind is done */ |
760 | 0 | bc->activeCP = false; |
761 | 0 | continue; |
762 | 0 | } |
763 | 0 | nextTargetIndex++; /* From the last index to the next index */ |
764 | 0 | rk->targets.array = &rk->targets.array[nextTargetIndex]; |
765 | 0 | rk->targetsSize -= nextTargetIndex; |
766 | 0 | } |
767 | | |
768 | | /* Clear cp->lastTarget before it gets overwritten in the following |
769 | | * browse steps. */ |
770 | 0 | UA_NodePointer_clear(&cp->lastTarget); |
771 | 0 | } |
772 | | |
773 | | /* Iterate over all reference targets */ |
774 | 2.28M | bc->rk = rk; |
775 | 2.28M | void *res = UA_NodeReferenceKind_iterate(rk, browseReferencTargetCallback, bc); |
776 | | |
777 | | /* Undo the "skipping ahead" for the continuation point */ |
778 | 2.28M | if(bc->activeCP) { |
779 | 0 | if(rk->hasRefTree) { |
780 | 0 | rk->targets.tree.idRoot = |
781 | 0 | ZIP_ZIP(UA_ReferenceIdTree, left.root, right.root); |
782 | 0 | } else { |
783 | | /* rk->targets.array = rk->targets.array[-nextTargetIndex]; */ |
784 | 0 | rk->targets.array = rk->targets.array - nextTargetIndex; |
785 | 0 | rk->targetsSize += nextTargetIndex; |
786 | 0 | UA_assert(rk->targetsSize > 0); |
787 | 0 | } |
788 | 0 | bc->activeCP = false; |
789 | 0 | } |
790 | | |
791 | | /* The iteration was aborted */ |
792 | 2.28M | if(res != NULL) { |
793 | | /* Aborted with status code good -> the maximum number of browse |
794 | | * results was reached. Make a deep copy of the last target for the |
795 | | * continuation point. */ |
796 | 0 | if(bc->status == UA_STATUSCODE_GOOD) |
797 | 0 | bc->status = UA_NodePointer_copy(cp->lastTarget, &cp->lastTarget); |
798 | 0 | return; |
799 | 0 | } |
800 | 2.28M | } |
801 | | |
802 | | /* Reset last-target to prevent clearing it up */ |
803 | 19.7M | UA_NodePointer_init(&cp->lastTarget); |
804 | | |
805 | | /* Browsing the node is done */ |
806 | 19.7M | bc->done = true; |
807 | 19.7M | } |
808 | | |
809 | | /* Results for a single browsedescription. This is the inner loop for both |
810 | | * Browse and BrowseNext. The ContinuationPoint contains all the data used. |
811 | | * Including the BrowseDescription. Returns whether there are remaining |
812 | | * references. */ |
813 | | static void |
814 | 19.7M | browse(struct BrowseContext *bc) { |
815 | | /* Is the browsedirection valid? */ |
816 | 19.7M | struct ContinuationPoint *cp = bc->cp; |
817 | 19.7M | const UA_BrowseDescription *descr = &cp->browseDescription; |
818 | 19.7M | if(descr->browseDirection != UA_BROWSEDIRECTION_BOTH && |
819 | 19.7M | descr->browseDirection != UA_BROWSEDIRECTION_FORWARD && |
820 | 36.4k | descr->browseDirection != UA_BROWSEDIRECTION_INVERSE) { |
821 | 0 | bc->status = UA_STATUSCODE_BADBROWSEDIRECTIONINVALID; |
822 | 0 | return; |
823 | 0 | } |
824 | | |
825 | | /* Get node with only the selected references and attributes */ |
826 | 19.7M | const UA_Node *node = |
827 | 19.7M | UA_NODESTORE_GET_SELECTIVE(bc->server, &descr->nodeId, |
828 | 19.7M | resultMask2AttributesMask(descr->resultMask), |
829 | 19.7M | bc->resultRefs, descr->browseDirection); |
830 | 19.7M | if(!node) { |
831 | 0 | bc->status = UA_STATUSCODE_BADNODEIDUNKNOWN; |
832 | 0 | return; |
833 | 0 | } |
834 | | |
835 | | /* Check AccessControl rights */ |
836 | 19.7M | if(bc->session != &bc->server->adminSession) { |
837 | 0 | UA_LOCK_ASSERT(&bc->server->serviceMutex); |
838 | 0 | if(!bc->server->config.accessControl. |
839 | 0 | allowBrowseNode(bc->server, &bc->server->config.accessControl, |
840 | 0 | &bc->session->sessionId, bc->session->context, |
841 | 0 | &descr->nodeId, node->head.context)) { |
842 | 0 | UA_NODESTORE_RELEASE(bc->server, node); |
843 | 0 | bc->status = UA_STATUSCODE_BADUSERACCESSDENIED; |
844 | 0 | return; |
845 | 0 | } |
846 | 0 | } |
847 | | |
848 | | /* Browse the node */ |
849 | 19.7M | browseWithNode(bc, &node->head); |
850 | 19.7M | UA_NODESTORE_RELEASE(bc->server, node); |
851 | | |
852 | | /* Is the reference type valid? This is very infrequent. So we only test |
853 | | * this if browsing came up empty. If the node has references of that type, |
854 | | * we know the reftype to be good. */ |
855 | 19.7M | if(bc->rr.size == 0 && !UA_NodeId_isNull(&descr->referenceTypeId)) { |
856 | 17.7M | const UA_Node *reftype = |
857 | 17.7M | UA_NODESTORE_GET_SELECTIVE(bc->server, &descr->referenceTypeId, |
858 | 17.7M | UA_NODEATTRIBUTESMASK_NODECLASS, |
859 | 17.7M | UA_REFERENCETYPESET_NONE, |
860 | 17.7M | UA_BROWSEDIRECTION_INVALID); |
861 | 17.7M | if(!reftype) { |
862 | 0 | bc->status = UA_STATUSCODE_BADREFERENCETYPEIDINVALID; |
863 | 0 | return; |
864 | 0 | } |
865 | | |
866 | 17.7M | UA_Boolean isRef = (reftype->head.nodeClass == UA_NODECLASS_REFERENCETYPE); |
867 | 17.7M | UA_NODESTORE_RELEASE(bc->server, reftype); |
868 | | |
869 | 17.7M | if(!isRef) { |
870 | 0 | bc->status = UA_STATUSCODE_BADREFERENCETYPEIDINVALID; |
871 | 0 | return; |
872 | 0 | } |
873 | 17.7M | } |
874 | 19.7M | } |
875 | | |
876 | | /* Start to browse with no previous cp */ |
877 | | void |
878 | | Operation_Browse(UA_Server *server, UA_Session *session, const UA_UInt32 *maxrefs, |
879 | 19.7M | const UA_BrowseDescription *descr, UA_BrowseResult *result) { |
880 | | /* Stack-allocate a temporary cp */ |
881 | 19.7M | ContinuationPoint cp; |
882 | 19.7M | memset(&cp, 0, sizeof(ContinuationPoint)); |
883 | 19.7M | cp.maxReferences = *maxrefs; |
884 | 19.7M | cp.browseDescription = *descr; /* Shallow copy. Deep-copy later if we persist the cp. */ |
885 | | |
886 | | /* How many references can we return at most? */ |
887 | 19.7M | if(cp.maxReferences == 0) { |
888 | 19.7M | if(server->config.maxReferencesPerNode != 0) { |
889 | 0 | cp.maxReferences = server->config.maxReferencesPerNode; |
890 | 19.7M | } else { |
891 | 19.7M | cp.maxReferences = UA_INT32_MAX; |
892 | 19.7M | } |
893 | 19.7M | } else { |
894 | 0 | if(server->config.maxReferencesPerNode != 0 && |
895 | 0 | cp.maxReferences > server->config.maxReferencesPerNode) { |
896 | 0 | cp.maxReferences= server->config.maxReferencesPerNode; |
897 | 0 | } |
898 | 0 | } |
899 | | |
900 | | /* Get the list of relevant reference types */ |
901 | 19.7M | result->statusCode = |
902 | 19.7M | referenceTypeIndices(server, &descr->referenceTypeId, |
903 | 19.7M | &cp.relevantReferences, descr->includeSubtypes); |
904 | 19.7M | if(result->statusCode != UA_STATUSCODE_GOOD) |
905 | 0 | return; |
906 | | |
907 | | /* Prepare the context */ |
908 | 19.7M | struct BrowseContext bc; |
909 | 19.7M | bc.cp = &cp; |
910 | 19.7M | bc.server = server; |
911 | 19.7M | bc.session = session; |
912 | 19.7M | bc.status = UA_STATUSCODE_GOOD; |
913 | 19.7M | bc.done = false; |
914 | 19.7M | bc.activeCP = false; |
915 | 19.7M | bc.resultRefs = cp.relevantReferences; |
916 | 19.7M | result->statusCode = RefResult_init(&bc.rr); |
917 | 19.7M | if(result->statusCode != UA_STATUSCODE_GOOD) |
918 | 0 | return; |
919 | | |
920 | | /* Perform the browse */ |
921 | 19.7M | browse(&bc); |
922 | | |
923 | 19.7M | if(bc.status != UA_STATUSCODE_GOOD || bc.rr.size == 0) { |
924 | | /* No relevant references, return array of length zero */ |
925 | 17.7M | RefResult_clear(&bc.rr); |
926 | 17.7M | result->references = (UA_ReferenceDescription*)UA_EMPTY_ARRAY_SENTINEL; |
927 | 17.7M | result->statusCode = bc.status; |
928 | 17.7M | return; |
929 | 17.7M | } |
930 | | |
931 | | /* Move results */ |
932 | 1.96M | result->references = bc.rr.descr; |
933 | 1.96M | result->referencesSize = bc.rr.size; |
934 | | |
935 | | /* Exit early if done */ |
936 | 1.96M | if(bc.done) |
937 | 1.96M | return; |
938 | | |
939 | | /* Persist the continuation point */ |
940 | | |
941 | 0 | ContinuationPoint *cp2 = NULL; |
942 | 0 | UA_Guid *ident = NULL; |
943 | 0 | UA_StatusCode retval = UA_STATUSCODE_GOOD; |
944 | | |
945 | | /* Enough space for the continuation point? */ |
946 | 0 | if(session->availableContinuationPoints == 0) { |
947 | 0 | retval = UA_STATUSCODE_BADNOCONTINUATIONPOINTS; |
948 | 0 | goto cleanup; |
949 | 0 | } |
950 | | |
951 | | /* Allocate and fill the data structure */ |
952 | 0 | cp2 = (ContinuationPoint*)UA_calloc(1, sizeof(ContinuationPoint)); |
953 | 0 | if(!cp2) { |
954 | 0 | retval = UA_STATUSCODE_BADOUTOFMEMORY; |
955 | 0 | goto cleanup; |
956 | 0 | } |
957 | | |
958 | | /* The BrowseDescription is only a shallow copy so far */ |
959 | 0 | retval = UA_BrowseDescription_copy(descr, &cp2->browseDescription); |
960 | 0 | if(retval != UA_STATUSCODE_GOOD) |
961 | 0 | goto cleanup; |
962 | 0 | cp2->maxReferences = cp.maxReferences; |
963 | 0 | cp2->relevantReferences = cp.relevantReferences; |
964 | 0 | cp2->lastTarget = cp.lastTarget; /* Move the (deep) copy */ |
965 | 0 | UA_NodePointer_init(&cp.lastTarget); /* No longer clear below (cleanup) */ |
966 | 0 | cp2->lastRefKindIndex = cp.lastRefKindIndex; |
967 | 0 | cp2->lastRefInverse = cp.lastRefInverse; |
968 | | |
969 | | /* Create a random bytestring via a Guid */ |
970 | 0 | ident = UA_Guid_new(); |
971 | 0 | if(!ident) { |
972 | 0 | retval = UA_STATUSCODE_BADOUTOFMEMORY; |
973 | 0 | goto cleanup; |
974 | 0 | } |
975 | 0 | *ident = UA_Guid_random(); |
976 | 0 | cp2->identifier.data = (UA_Byte*)ident; |
977 | 0 | cp2->identifier.length = sizeof(UA_Guid); |
978 | | |
979 | | /* Return the cp identifier */ |
980 | 0 | retval = UA_ByteString_copy(&cp2->identifier, &result->continuationPoint); |
981 | 0 | if(retval != UA_STATUSCODE_GOOD) |
982 | 0 | goto cleanup; |
983 | | |
984 | | /* Attach the cp to the session */ |
985 | 0 | cp2->next = session->continuationPoints; |
986 | 0 | session->continuationPoints = cp2; |
987 | 0 | --session->availableContinuationPoints; |
988 | 0 | return; |
989 | | |
990 | 0 | cleanup: |
991 | 0 | if(cp2) { |
992 | 0 | ContinuationPoint_clear(cp2); |
993 | 0 | UA_free(cp2); |
994 | 0 | } |
995 | 0 | UA_NodePointer_clear(&cp.lastTarget); |
996 | 0 | UA_BrowseResult_clear(result); |
997 | 0 | result->statusCode = retval; |
998 | 0 | } |
999 | | |
1000 | | UA_Boolean |
1001 | | Service_Browse(UA_Server *server, UA_Session *session, |
1002 | 0 | const UA_BrowseRequest *request, UA_BrowseResponse *response) { |
1003 | 0 | UA_LOG_DEBUG_SESSION(server->config.logging, session, "Processing BrowseRequest"); |
1004 | 0 | UA_LOCK_ASSERT(&server->serviceMutex); |
1005 | | |
1006 | | /* Test the number of operations in the request */ |
1007 | 0 | if(server->config.maxNodesPerBrowse != 0 && |
1008 | 0 | request->nodesToBrowseSize > server->config.maxNodesPerBrowse) { |
1009 | 0 | response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS; |
1010 | 0 | return true; |
1011 | 0 | } |
1012 | | |
1013 | | /* No views supported at the moment */ |
1014 | 0 | if(!UA_NodeId_isNull(&request->view.viewId)) { |
1015 | 0 | response->responseHeader.serviceResult = UA_STATUSCODE_BADVIEWIDUNKNOWN; |
1016 | 0 | return true; |
1017 | 0 | } |
1018 | | |
1019 | 0 | response->responseHeader.serviceResult = |
1020 | 0 | allocProcessServiceOperations(server, session, |
1021 | 0 | (UA_ServiceOperation)Operation_Browse, |
1022 | 0 | &request->requestedMaxReferencesPerNode, |
1023 | 0 | &request->nodesToBrowseSize, |
1024 | 0 | &UA_TYPES[UA_TYPES_BROWSEDESCRIPTION], |
1025 | 0 | &response->resultsSize, |
1026 | 0 | &UA_TYPES[UA_TYPES_BROWSERESULT]); |
1027 | 0 | return true; |
1028 | 0 | } |
1029 | | |
1030 | | UA_BrowseResult |
1031 | | UA_Server_browse(UA_Server *server, UA_UInt32 maxReferences, |
1032 | 0 | const UA_BrowseDescription *bd) { |
1033 | 0 | UA_BrowseResult result; |
1034 | 0 | UA_BrowseResult_init(&result); |
1035 | 0 | lockServer(server); |
1036 | 0 | Operation_Browse(server, &server->adminSession, &maxReferences, bd, &result); |
1037 | 0 | unlockServer(server); |
1038 | 0 | return result; |
1039 | 0 | } |
1040 | | |
1041 | | static void |
1042 | | Operation_BrowseNext(UA_Server *server, UA_Session *session, |
1043 | | const UA_Boolean *releaseContinuationPoints, |
1044 | 0 | const UA_ByteString *continuationPoint, UA_BrowseResult *result) { |
1045 | | /* Find the continuation point */ |
1046 | 0 | ContinuationPoint **prev = &session->continuationPoints; |
1047 | 0 | ContinuationPoint *cp; |
1048 | 0 | while((cp = *prev)) { |
1049 | 0 | if(UA_ByteString_equal(&cp->identifier, continuationPoint)) |
1050 | 0 | break; |
1051 | 0 | prev = &cp->next; |
1052 | 0 | } |
1053 | 0 | if(!cp) { |
1054 | 0 | result->statusCode = UA_STATUSCODE_BADCONTINUATIONPOINTINVALID; |
1055 | 0 | return; |
1056 | 0 | } |
1057 | | |
1058 | | /* Remove the cp */ |
1059 | 0 | if(*releaseContinuationPoints) { |
1060 | 0 | *prev = ContinuationPoint_clear(cp); |
1061 | 0 | UA_free(cp); |
1062 | 0 | ++session->availableContinuationPoints; |
1063 | 0 | return; |
1064 | 0 | } |
1065 | | |
1066 | | /* Prepare the context */ |
1067 | 0 | struct BrowseContext bc; |
1068 | 0 | bc.cp = cp; |
1069 | 0 | bc.server = server; |
1070 | 0 | bc.session = session; |
1071 | 0 | bc.status = UA_STATUSCODE_GOOD; |
1072 | 0 | bc.done = false; |
1073 | 0 | bc.activeCP = true; |
1074 | 0 | bc.resultRefs = cp->relevantReferences; |
1075 | 0 | result->statusCode = RefResult_init(&bc.rr); |
1076 | 0 | if(result->statusCode != UA_STATUSCODE_GOOD) |
1077 | 0 | return; |
1078 | | |
1079 | | /* Continue browsing */ |
1080 | 0 | browse(&bc); |
1081 | |
|
1082 | 0 | if(bc.status != UA_STATUSCODE_GOOD || bc.rr.size == 0) { |
1083 | | /* No relevant references, return array of length zero */ |
1084 | 0 | RefResult_clear(&bc.rr); |
1085 | 0 | result->references = (UA_ReferenceDescription*)UA_EMPTY_ARRAY_SENTINEL; |
1086 | 0 | result->statusCode = bc.status; |
1087 | 0 | goto remove_cp; |
1088 | 0 | } |
1089 | | |
1090 | | /* Move results */ |
1091 | 0 | result->references = bc.rr.descr; |
1092 | 0 | result->referencesSize = bc.rr.size; |
1093 | |
|
1094 | 0 | if(bc.done) |
1095 | 0 | goto remove_cp; |
1096 | | |
1097 | | /* Return the cp identifier to signal that there are references left */ |
1098 | 0 | bc.status = UA_ByteString_copy(&cp->identifier, &result->continuationPoint); |
1099 | 0 | if(bc.status != UA_STATUSCODE_GOOD) { |
1100 | 0 | UA_BrowseResult_clear(result); |
1101 | 0 | result->statusCode = bc.status; |
1102 | 0 | } |
1103 | 0 | return; |
1104 | | |
1105 | 0 | remove_cp: |
1106 | | /* Remove the cp */ |
1107 | 0 | *prev = ContinuationPoint_clear(cp); |
1108 | 0 | UA_free(cp); |
1109 | 0 | ++session->availableContinuationPoints; |
1110 | 0 | } |
1111 | | |
1112 | | UA_Boolean |
1113 | | Service_BrowseNext(UA_Server *server, UA_Session *session, |
1114 | | const UA_BrowseNextRequest *request, |
1115 | 0 | UA_BrowseNextResponse *response) { |
1116 | 0 | UA_LOG_DEBUG_SESSION(server->config.logging, session, |
1117 | 0 | "Processing BrowseNextRequest"); |
1118 | 0 | UA_LOCK_ASSERT(&server->serviceMutex); |
1119 | |
|
1120 | 0 | UA_Boolean releaseContinuationPoints = |
1121 | 0 | request->releaseContinuationPoints; /* request is const */ |
1122 | 0 | response->responseHeader.serviceResult = |
1123 | 0 | allocProcessServiceOperations(server, session, |
1124 | 0 | (UA_ServiceOperation)Operation_BrowseNext, |
1125 | 0 | &releaseContinuationPoints, |
1126 | 0 | &request->continuationPointsSize, |
1127 | 0 | &UA_TYPES[UA_TYPES_BYTESTRING], |
1128 | 0 | &response->resultsSize, |
1129 | 0 | &UA_TYPES[UA_TYPES_BROWSERESULT]); |
1130 | 0 | return true; |
1131 | 0 | } |
1132 | | |
1133 | | UA_BrowseResult |
1134 | | UA_Server_browseNext(UA_Server *server, UA_Boolean releaseContinuationPoint, |
1135 | 0 | const UA_ByteString *continuationPoint) { |
1136 | 0 | UA_BrowseResult result; |
1137 | 0 | UA_BrowseResult_init(&result); |
1138 | 0 | lockServer(server); |
1139 | 0 | Operation_BrowseNext(server, &server->adminSession, &releaseContinuationPoint, |
1140 | 0 | continuationPoint, &result); |
1141 | 0 | unlockServer(server); |
1142 | 0 | return result; |
1143 | 0 | } |
1144 | | |
1145 | | /***********************/ |
1146 | | /* TranslateBrowsePath */ |
1147 | | /***********************/ |
1148 | | |
1149 | | /* Add all entries for the hash. There are possible duplicates due to hash |
1150 | | * collisions. The full browsename is checked afterwards. */ |
1151 | | static void * |
1152 | 0 | addBrowseHashTarget(void *context, UA_ReferenceTargetTreeElem *elem) { |
1153 | 0 | RefTree *next = (RefTree*)context; |
1154 | 0 | return (void*)(uintptr_t)RefTree_add(next, elem->target.targetId, NULL); |
1155 | 0 | } |
1156 | | |
1157 | | static UA_StatusCode |
1158 | | walkBrowsePathElement(UA_Server *server, UA_Session *session, |
1159 | | const UA_RelativePath *path, const size_t pathIndex, |
1160 | | UA_UInt32 nodeClassMask, const UA_QualifiedName *lastBrowseName, |
1161 | 406 | UA_BrowsePathResult *result, RefTree *current, RefTree *next) { |
1162 | | /* For the next level. Note the difference from lastBrowseName */ |
1163 | 406 | const UA_RelativePathElement *elem = &path->elements[pathIndex]; |
1164 | 406 | UA_UInt32 browseNameHash = UA_QualifiedName_hash(&elem->targetName); |
1165 | | |
1166 | | /* Get the relevant ReferenceTypes */ |
1167 | 406 | UA_ReferenceTypeSet refTypes; |
1168 | 406 | UA_StatusCode res = |
1169 | 406 | referenceTypeIndices(server, &elem->referenceTypeId, |
1170 | 406 | &refTypes, elem->includeSubtypes); |
1171 | 406 | if(res != UA_STATUSCODE_GOOD) |
1172 | 0 | return UA_STATUSCODE_BADNOMATCH; |
1173 | | |
1174 | | /* Loop over all Nodes in the current depth level */ |
1175 | 812 | for(size_t i = 0; i < current->size; i++) { |
1176 | | /* Remote Node. Immediately add to the results with the |
1177 | | * RemainingPathIndex set. */ |
1178 | 406 | if(!UA_ExpandedNodeId_isLocal(¤t->targets[i])) { |
1179 | | /* Increase the size of the results array */ |
1180 | 0 | UA_BrowsePathTarget *tmpResults = (UA_BrowsePathTarget*) |
1181 | 0 | UA_realloc(result->targets, sizeof(UA_BrowsePathTarget) * |
1182 | 0 | (result->targetsSize + 1)); |
1183 | 0 | if(!tmpResults) |
1184 | 0 | return UA_STATUSCODE_BADOUTOFMEMORY; |
1185 | 0 | result->targets = tmpResults; |
1186 | | |
1187 | | /* Copy over the result */ |
1188 | 0 | UA_BrowsePathTarget *newEntry = &result->targets[result->targetsSize]; |
1189 | 0 | res = UA_ExpandedNodeId_copy(¤t->targets[i], &newEntry->targetId); |
1190 | 0 | newEntry->remainingPathIndex = (UA_UInt32)pathIndex; |
1191 | 0 | result->targetsSize++; |
1192 | 0 | if(res != UA_STATUSCODE_GOOD) |
1193 | 0 | break; |
1194 | 0 | continue; |
1195 | 0 | } |
1196 | | |
1197 | | /* Local Node. Add to the tree of results at the next depth. Get only |
1198 | | * the NodeClass + BrowseName attribute and the selected ReferenceTypes |
1199 | | * if the nodestore supports that. */ |
1200 | 406 | const UA_Node *node = |
1201 | 406 | UA_NODESTORE_GET_SELECTIVE(server, ¤t->targets[i].nodeId, |
1202 | 406 | UA_NODEATTRIBUTESMASK_NODECLASS | |
1203 | 406 | UA_NODEATTRIBUTESMASK_BROWSENAME, |
1204 | 406 | refTypes, |
1205 | 406 | elem->isInverse ? UA_BROWSEDIRECTION_INVERSE : |
1206 | 406 | UA_BROWSEDIRECTION_FORWARD); |
1207 | 406 | if(!node) |
1208 | 0 | continue; |
1209 | | |
1210 | | /* Test whether the node fits the class mask */ |
1211 | 406 | UA_Boolean skip = !matchClassMask(node, nodeClassMask); |
1212 | | |
1213 | | /* Does the BrowseName match for the current node (not the references |
1214 | | * going out here) */ |
1215 | 406 | skip |= (lastBrowseName && |
1216 | 0 | !UA_QualifiedName_equal(lastBrowseName, &node->head.browseName)); |
1217 | | |
1218 | 406 | if(skip) { |
1219 | 0 | UA_NODESTORE_RELEASE(server, node); |
1220 | 0 | continue; |
1221 | 0 | } |
1222 | | |
1223 | | /* Loop over the ReferenceKinds */ |
1224 | 406 | UA_ReferenceTarget targetHashKey; |
1225 | 406 | targetHashKey.targetNameHash = browseNameHash; |
1226 | 812 | for(size_t j = 0; j < node->head.referencesSize; j++) { |
1227 | 406 | UA_NodeReferenceKind *rk = &node->head.references[j]; |
1228 | | |
1229 | | /* Does the direction of the reference match? */ |
1230 | 406 | if(rk->isInverse != elem->isInverse) |
1231 | 406 | continue; |
1232 | | |
1233 | | /* Does the reference type match? */ |
1234 | 0 | if(!UA_ReferenceTypeSet_contains(&refTypes, rk->referenceTypeIndex)) |
1235 | 0 | continue; |
1236 | | |
1237 | | /* Retrieve by BrowseName hash. We might have several nodes where |
1238 | | * the hash matches. The exact BrowseName will be verified in the |
1239 | | * next iteration of the outer loop. So we only have to retrieve |
1240 | | * every node just once. */ |
1241 | | |
1242 | 0 | if(rk->hasRefTree) { |
1243 | 0 | res = (UA_StatusCode)(uintptr_t) |
1244 | 0 | ZIP_ITER_KEY(UA_ReferenceNameTree, |
1245 | 0 | (UA_ReferenceNameTree*)&rk->targets.tree.nameRoot, |
1246 | 0 | &targetHashKey, addBrowseHashTarget, next); |
1247 | 0 | if(res != UA_STATUSCODE_GOOD) |
1248 | 0 | break; |
1249 | 0 | } else { |
1250 | | /* The array entries don't have a BrowseName hash. Add all of |
1251 | | * them at this level to be checked with a full string |
1252 | | * comparison. */ |
1253 | 0 | for(size_t k = 0; k < rk->targetsSize; k++) { |
1254 | 0 | if(rk->targets.array[k].targetNameHash != browseNameHash) |
1255 | 0 | continue; |
1256 | 0 | res = RefTree_add(next, rk->targets.array[k].targetId, NULL); |
1257 | 0 | if(res != UA_STATUSCODE_GOOD) |
1258 | 0 | break; |
1259 | 0 | } |
1260 | 0 | if(res != UA_STATUSCODE_GOOD) |
1261 | 0 | break; |
1262 | 0 | } |
1263 | 0 | } |
1264 | | |
1265 | 406 | UA_NODESTORE_RELEASE(server, node); |
1266 | 406 | } |
1267 | 406 | return res; |
1268 | 406 | } |
1269 | | |
1270 | | static void |
1271 | | Operation_TranslateBrowsePathToNodeIds(UA_Server *server, UA_Session *session, |
1272 | | const UA_UInt32 *nodeClassMask, |
1273 | | const UA_BrowsePath *path, |
1274 | 406 | UA_BrowsePathResult *result) { |
1275 | 406 | UA_LOCK_ASSERT(&server->serviceMutex); |
1276 | | |
1277 | 406 | if(path->relativePath.elementsSize == 0) { |
1278 | 0 | result->statusCode = UA_STATUSCODE_BADNOTHINGTODO; |
1279 | 0 | return; |
1280 | 0 | } |
1281 | | |
1282 | | /* RelativePath elements must not have an empty targetName */ |
1283 | 812 | for(size_t i = 0; i < path->relativePath.elementsSize; ++i) { |
1284 | 406 | if(UA_QualifiedName_isNull(&path->relativePath.elements[i].targetName)) { |
1285 | 0 | result->statusCode = UA_STATUSCODE_BADBROWSENAMEINVALID; |
1286 | 0 | return; |
1287 | 0 | } |
1288 | 406 | } |
1289 | | |
1290 | | /* Check if the starting node exists */ |
1291 | 406 | const UA_Node *startingNode = |
1292 | 406 | UA_NODESTORE_GET_SELECTIVE(server, &path->startingNode, |
1293 | 406 | UA_NODEATTRIBUTESMASK_NONE, |
1294 | 406 | UA_REFERENCETYPESET_NONE, |
1295 | 406 | UA_BROWSEDIRECTION_INVALID); |
1296 | 406 | if(!startingNode) { |
1297 | 0 | result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN; |
1298 | 0 | return; |
1299 | 0 | } |
1300 | 406 | UA_NODESTORE_RELEASE(server, startingNode); |
1301 | | |
1302 | | /* Create two RefTrees that are alternated between path elements */ |
1303 | 406 | RefTree rt1; |
1304 | 406 | RefTree rt2; |
1305 | 406 | RefTree *current = &rt1; |
1306 | 406 | RefTree *next = &rt2; |
1307 | 406 | RefTree *tmp; |
1308 | 406 | result->statusCode |= RefTree_init(&rt1); |
1309 | 406 | result->statusCode |= RefTree_init(&rt2); |
1310 | 406 | UA_BrowsePathTarget *tmpResults = NULL; |
1311 | 406 | UA_QualifiedName *browseNameFilter = NULL; |
1312 | 406 | if(result->statusCode != UA_STATUSCODE_GOOD) |
1313 | 0 | goto cleanup; |
1314 | | |
1315 | | /* Copy the starting node into next */ |
1316 | 406 | result->statusCode = RefTree_addNodeId(next, &path->startingNode, NULL); |
1317 | 406 | if(result->statusCode != UA_STATUSCODE_GOOD) |
1318 | 0 | goto cleanup; |
1319 | | |
1320 | | /* Walk the path elements. Retrieve the nodes only once from the NodeStore. |
1321 | | * Hence the BrowseName is checked with one element "delay". */ |
1322 | 812 | for(size_t i = 0; i < path->relativePath.elementsSize; i++) { |
1323 | | /* Switch the trees */ |
1324 | 406 | tmp = current; |
1325 | 406 | current = next; |
1326 | 406 | next = tmp; |
1327 | | |
1328 | | /* Clear up current, keep the capacity */ |
1329 | 406 | for(size_t j = 0; j < next->size; j++) |
1330 | 0 | UA_ExpandedNodeId_clear(&next->targets[j]); |
1331 | 406 | next->size = 0; |
1332 | 406 | ZIP_INIT(&next->head); |
1333 | | |
1334 | | /* Do this check after next->size has been set to zero */ |
1335 | 406 | if(current->size == 0) |
1336 | 0 | break; |
1337 | | |
1338 | | /* Walk element for all NodeIds in the "current" tree. |
1339 | | * Puts new results in the "next" tree. */ |
1340 | 406 | result->statusCode = |
1341 | 406 | walkBrowsePathElement(server, session, &path->relativePath, i, |
1342 | 406 | *nodeClassMask, browseNameFilter, result, current, next); |
1343 | 406 | if(result->statusCode != UA_STATUSCODE_GOOD) |
1344 | 0 | goto cleanup; |
1345 | | |
1346 | 406 | browseNameFilter = &path->relativePath.elements[i].targetName; |
1347 | 406 | } |
1348 | | |
1349 | | /* Allocate space for the results array */ |
1350 | 406 | tmpResults = (UA_BrowsePathTarget*) |
1351 | 406 | UA_realloc(result->targets, sizeof(UA_BrowsePathTarget) * |
1352 | 406 | (result->targetsSize + next->size)); |
1353 | 406 | if(!tmpResults && next->size > 0) { |
1354 | 0 | result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY; |
1355 | 0 | goto cleanup; |
1356 | 0 | } |
1357 | 406 | result->targets = tmpResults; |
1358 | | |
1359 | 406 | for(size_t k = 0; k < next->size; k++) { |
1360 | | /* Check the BrowseName. It has been filtered only via its hash so far. |
1361 | | * Get only the BrowseName attribute if the nodestore supports that. */ |
1362 | 0 | const UA_Node *node = |
1363 | 0 | UA_NODESTORE_GET_SELECTIVE(server, &next->targets[k].nodeId, |
1364 | 0 | UA_NODEATTRIBUTESMASK_BROWSENAME, |
1365 | 0 | UA_REFERENCETYPESET_NONE, |
1366 | 0 | UA_BROWSEDIRECTION_INVALID); |
1367 | 0 | if(!node) |
1368 | 0 | continue; |
1369 | 0 | UA_Boolean match = UA_QualifiedName_equal(browseNameFilter, &node->head.browseName); |
1370 | 0 | UA_NODESTORE_RELEASE(server, node); |
1371 | 0 | if(!match) |
1372 | 0 | continue; |
1373 | | |
1374 | | /* Move the target to the results array */ |
1375 | 0 | result->targets[result->targetsSize].targetId = next->targets[k]; |
1376 | 0 | result->targets[result->targetsSize].remainingPathIndex = UA_UINT32_MAX; |
1377 | 0 | UA_ExpandedNodeId_init(&next->targets[k]); |
1378 | 0 | result->targetsSize++; |
1379 | 0 | } |
1380 | | |
1381 | | /* No results => BadNoMatch status code */ |
1382 | 406 | if(result->targetsSize == 0 && result->statusCode == UA_STATUSCODE_GOOD) |
1383 | 406 | result->statusCode = UA_STATUSCODE_BADNOMATCH; |
1384 | | |
1385 | | /* Clean up the temporary arrays and the targets */ |
1386 | 406 | cleanup: |
1387 | 406 | RefTree_clear(&rt1); |
1388 | 406 | RefTree_clear(&rt2); |
1389 | 406 | if(result->statusCode != UA_STATUSCODE_GOOD) { |
1390 | 406 | for(size_t i = 0; i < result->targetsSize; ++i) |
1391 | 0 | UA_BrowsePathTarget_clear(&result->targets[i]); |
1392 | 406 | if(result->targets) |
1393 | 406 | UA_free(result->targets); |
1394 | 406 | result->targets = NULL; |
1395 | 406 | result->targetsSize = 0; |
1396 | 406 | } |
1397 | 406 | } |
1398 | | |
1399 | | UA_BrowsePathResult |
1400 | | translateBrowsePathToNodeIds(UA_Server *server, |
1401 | 406 | const UA_BrowsePath *browsePath) { |
1402 | 406 | UA_LOCK_ASSERT(&server->serviceMutex); |
1403 | 406 | UA_BrowsePathResult result; |
1404 | 406 | UA_BrowsePathResult_init(&result); |
1405 | 406 | UA_UInt32 nodeClassMask = 0; /* All node classes */ |
1406 | 406 | Operation_TranslateBrowsePathToNodeIds(server, &server->adminSession, &nodeClassMask, |
1407 | 406 | browsePath, &result); |
1408 | 406 | return result; |
1409 | 406 | } |
1410 | | |
1411 | | UA_BrowsePathResult |
1412 | | UA_Server_translateBrowsePathToNodeIds(UA_Server *server, |
1413 | 0 | const UA_BrowsePath *browsePath) { |
1414 | 0 | lockServer(server); |
1415 | 0 | UA_BrowsePathResult result = translateBrowsePathToNodeIds(server, browsePath); |
1416 | 0 | unlockServer(server); |
1417 | 0 | return result; |
1418 | 0 | } |
1419 | | |
1420 | | UA_Boolean |
1421 | | Service_TranslateBrowsePathsToNodeIds(UA_Server *server, UA_Session *session, |
1422 | | const UA_TranslateBrowsePathsToNodeIdsRequest *request, |
1423 | 0 | UA_TranslateBrowsePathsToNodeIdsResponse *response) { |
1424 | 0 | UA_LOG_DEBUG_SESSION(server->config.logging, session, |
1425 | 0 | "Processing TranslateBrowsePathsToNodeIdsRequest"); |
1426 | 0 | UA_LOCK_ASSERT(&server->serviceMutex); |
1427 | | |
1428 | | /* Test the number of operations in the request */ |
1429 | 0 | if(server->config.maxNodesPerTranslateBrowsePathsToNodeIds != 0 && |
1430 | 0 | request->browsePathsSize > server->config.maxNodesPerTranslateBrowsePathsToNodeIds) { |
1431 | 0 | response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS; |
1432 | 0 | return true; |
1433 | 0 | } |
1434 | | |
1435 | 0 | UA_UInt32 nodeClassMask = 0; /* All node classes */ |
1436 | 0 | response->responseHeader.serviceResult = |
1437 | 0 | allocProcessServiceOperations(server, session, |
1438 | 0 | (UA_ServiceOperation)Operation_TranslateBrowsePathToNodeIds, |
1439 | 0 | &nodeClassMask, &request->browsePathsSize, |
1440 | 0 | &UA_TYPES[UA_TYPES_BROWSEPATH], &response->resultsSize, |
1441 | 0 | &UA_TYPES[UA_TYPES_BROWSEPATHRESULT]); |
1442 | 0 | return true; |
1443 | 0 | } |
1444 | | |
1445 | | UA_BrowsePathResult |
1446 | | browseSimplifiedBrowsePath(UA_Server *server, const UA_NodeId origin, |
1447 | 0 | size_t browsePathSize, const UA_QualifiedName *browsePath) { |
1448 | 0 | UA_LOCK_ASSERT(&server->serviceMutex); |
1449 | |
|
1450 | 0 | UA_BrowsePathResult bpr; |
1451 | 0 | UA_BrowsePathResult_init(&bpr); |
1452 | 0 | if(browsePathSize > UA_MAX_TREE_RECURSE) { |
1453 | 0 | UA_LOG_WARNING(server->config.logging, UA_LOGCATEGORY_SERVER, |
1454 | 0 | "Simplified Browse Path too long"); |
1455 | 0 | bpr.statusCode = UA_STATUSCODE_BADINTERNALERROR; |
1456 | 0 | return bpr; |
1457 | 0 | } |
1458 | | |
1459 | | /* Construct the BrowsePath */ |
1460 | 0 | UA_BrowsePath bp; |
1461 | 0 | UA_BrowsePath_init(&bp); |
1462 | 0 | bp.startingNode = origin; |
1463 | |
|
1464 | 0 | UA_RelativePathElement rpe[UA_MAX_TREE_RECURSE]; |
1465 | 0 | memset(rpe, 0, sizeof(UA_RelativePathElement) * browsePathSize); |
1466 | 0 | for(size_t j = 0; j < browsePathSize; j++) { |
1467 | 0 | rpe[j].referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HIERARCHICALREFERENCES); |
1468 | 0 | rpe[j].includeSubtypes = true; |
1469 | 0 | rpe[j].targetName = browsePath[j]; |
1470 | 0 | } |
1471 | 0 | bp.relativePath.elements = rpe; |
1472 | 0 | bp.relativePath.elementsSize = browsePathSize; |
1473 | | |
1474 | | /* Browse */ |
1475 | 0 | UA_UInt32 nodeClassMask = UA_NODECLASS_OBJECT | UA_NODECLASS_VARIABLE | UA_NODECLASS_OBJECTTYPE; |
1476 | |
|
1477 | 0 | Operation_TranslateBrowsePathToNodeIds(server, &server->adminSession, &nodeClassMask, &bp, &bpr); |
1478 | 0 | return bpr; |
1479 | 0 | } |
1480 | | |
1481 | | UA_BrowsePathResult |
1482 | | UA_Server_browseSimplifiedBrowsePath(UA_Server *server, const UA_NodeId origin, |
1483 | 0 | size_t browsePathSize, const UA_QualifiedName *browsePath) { |
1484 | 0 | lockServer(server); |
1485 | 0 | UA_BrowsePathResult bpr = browseSimplifiedBrowsePath(server, origin, browsePathSize, browsePath); |
1486 | 0 | unlockServer(server); |
1487 | 0 | return bpr; |
1488 | 0 | } |
1489 | | |
1490 | | /************/ |
1491 | | /* Register */ |
1492 | | /************/ |
1493 | | |
1494 | | UA_Boolean |
1495 | | Service_RegisterNodes(UA_Server *server, UA_Session *session, |
1496 | | const UA_RegisterNodesRequest *request, |
1497 | 0 | UA_RegisterNodesResponse *response) { |
1498 | 0 | UA_LOG_DEBUG_SESSION(server->config.logging, session, |
1499 | 0 | "Processing RegisterNodesRequest"); |
1500 | 0 | UA_LOCK_ASSERT(&server->serviceMutex); |
1501 | | |
1502 | | //TODO: hang the nodeids to the session if really needed |
1503 | 0 | if(request->nodesToRegisterSize == 0) { |
1504 | 0 | response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO; |
1505 | 0 | return true; |
1506 | 0 | } |
1507 | | |
1508 | | /* Test the number of operations in the request */ |
1509 | 0 | if(server->config.maxNodesPerRegisterNodes != 0 && |
1510 | 0 | request->nodesToRegisterSize > server->config.maxNodesPerRegisterNodes) { |
1511 | 0 | response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS; |
1512 | 0 | return true; |
1513 | 0 | } |
1514 | | |
1515 | 0 | response->responseHeader.serviceResult = |
1516 | 0 | UA_Array_copy(request->nodesToRegister, request->nodesToRegisterSize, |
1517 | 0 | (void**)&response->registeredNodeIds, &UA_TYPES[UA_TYPES_NODEID]); |
1518 | 0 | if(response->responseHeader.serviceResult == UA_STATUSCODE_GOOD) |
1519 | 0 | response->registeredNodeIdsSize = request->nodesToRegisterSize; |
1520 | |
|
1521 | 0 | return true; |
1522 | 0 | } |
1523 | | |
1524 | | UA_Boolean |
1525 | | Service_UnregisterNodes(UA_Server *server, UA_Session *session, |
1526 | | const UA_UnregisterNodesRequest *request, |
1527 | 0 | UA_UnregisterNodesResponse *response) { |
1528 | 0 | UA_LOG_DEBUG_SESSION(server->config.logging, session, |
1529 | 0 | "Processing UnRegisterNodesRequest"); |
1530 | 0 | UA_LOCK_ASSERT(&server->serviceMutex); |
1531 | |
|
1532 | 0 | if(request->nodesToUnregisterSize == 0) { |
1533 | 0 | response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO; |
1534 | 0 | } else if(server->config.maxNodesPerRegisterNodes != 0 && |
1535 | 0 | request->nodesToUnregisterSize > server->config.maxNodesPerRegisterNodes) { |
1536 | 0 | response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS; |
1537 | 0 | } |
1538 | |
|
1539 | | return true; |
1540 | 0 | } |