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