ua_services_nodemanagement.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257
  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-2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2014-2017 (c) Florian Palm
  7. * Copyright 2015-2016 (c) Sten Grüner
  8. * Copyright 2015-2016 (c) Chris Iatrou
  9. * Copyright 2015-2016 (c) Oleksiy Vasylyev
  10. * Copyright 2017 (c) Julian Grothoff
  11. * Copyright 2016 (c) LEvertz
  12. * Copyright 2016 (c) Lorenz Haas
  13. * Copyright 2017 (c) frax2222
  14. * Copyright 2017-2018 (c) Stefan Profanter, fortiss GmbH
  15. * Copyright 2017 (c) Christian von Arnim
  16. * Copyright 2017 (c) Henrik Norrman
  17. */
  18. #include "ua_server_internal.h"
  19. #include "ua_services.h"
  20. /*********************/
  21. /* Edit Node Context */
  22. /*********************/
  23. UA_StatusCode
  24. UA_Server_getNodeContext(UA_Server *server, UA_NodeId nodeId,
  25. void **nodeContext) {
  26. UA_LOCK(server->serviceMutex);
  27. UA_StatusCode retval = getNodeContext(server, nodeId, nodeContext);
  28. UA_UNLOCK(server->serviceMutex);
  29. return retval;
  30. }
  31. UA_StatusCode
  32. getNodeContext(UA_Server *server, UA_NodeId nodeId,
  33. void **nodeContext) {
  34. const UA_Node *node = UA_NODESTORE_GET(server, &nodeId);
  35. if(!node)
  36. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  37. *nodeContext = node->context;
  38. UA_NODESTORE_RELEASE(server, node);
  39. return UA_STATUSCODE_GOOD;
  40. }
  41. static UA_StatusCode
  42. setDeconstructedNode(UA_Server *server, UA_Session *session,
  43. UA_Node *node, void *context) {
  44. node->constructed = false;
  45. return UA_STATUSCODE_GOOD;
  46. }
  47. static UA_StatusCode
  48. setConstructedNodeContext(UA_Server *server, UA_Session *session,
  49. UA_Node *node, void *context) {
  50. node->context = context;
  51. node->constructed = true;
  52. return UA_STATUSCODE_GOOD;
  53. }
  54. static UA_StatusCode
  55. editNodeContext(UA_Server *server, UA_Session* session,
  56. UA_Node* node, void *context) {
  57. node->context = context;
  58. return UA_STATUSCODE_GOOD;
  59. }
  60. UA_StatusCode
  61. UA_Server_setNodeContext(UA_Server *server, UA_NodeId nodeId,
  62. void *nodeContext) {
  63. UA_LOCK(server->serviceMutex);
  64. UA_StatusCode retval = UA_Server_editNode(server, &server->adminSession, &nodeId,
  65. (UA_EditNodeCallback)editNodeContext, nodeContext);
  66. UA_UNLOCK(server->serviceMutex);
  67. return retval;
  68. }
  69. /**********************/
  70. /* Consistency Checks */
  71. /**********************/
  72. #define UA_PARENT_REFERENCES_COUNT 2
  73. const UA_NodeId parentReferences[UA_PARENT_REFERENCES_COUNT] = {
  74. {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_HASSUBTYPE}},
  75. {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_HASCOMPONENT}}
  76. };
  77. /* Check if the requested parent node exists, has the right node class and is
  78. * referenced with an allowed (hierarchical) reference type. For "type" nodes,
  79. * only hasSubType references are allowed. */
  80. static UA_StatusCode
  81. checkParentReference(UA_Server *server, UA_Session *session, UA_NodeClass nodeClass,
  82. const UA_NodeId *parentNodeId, const UA_NodeId *referenceTypeId) {
  83. /* Objects do not need a parent (e.g. mandatory/optional modellingrules) */
  84. /* Also, there are some variables which do not have parents, e.g. EnumStrings, EnumValues */
  85. if((nodeClass == UA_NODECLASS_OBJECT || nodeClass == UA_NODECLASS_VARIABLE) &&
  86. UA_NodeId_isNull(parentNodeId) && UA_NodeId_isNull(referenceTypeId))
  87. return UA_STATUSCODE_GOOD;
  88. /* See if the parent exists */
  89. const UA_Node *parent = UA_NODESTORE_GET(server, parentNodeId);
  90. if(!parent) {
  91. UA_LOG_NODEID_WRAP(parentNodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  92. "AddNodes: Parent node %.*s not found",
  93. (int)nodeIdStr.length, nodeIdStr.data));
  94. return UA_STATUSCODE_BADPARENTNODEIDINVALID;
  95. }
  96. UA_NodeClass parentNodeClass = parent->nodeClass;
  97. UA_NODESTORE_RELEASE(server, parent);
  98. /* Check the referencetype exists */
  99. const UA_ReferenceTypeNode *referenceType = (const UA_ReferenceTypeNode*)
  100. UA_NODESTORE_GET(server, referenceTypeId);
  101. if(!referenceType) {
  102. UA_LOG_NODEID_WRAP(referenceTypeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  103. "AddNodes: Reference type %.*s to the parent not found",
  104. (int)nodeIdStr.length, nodeIdStr.data));
  105. return UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  106. }
  107. /* Check if the referencetype is a reference type node */
  108. if(referenceType->nodeClass != UA_NODECLASS_REFERENCETYPE) {
  109. UA_LOG_NODEID_WRAP(referenceTypeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  110. "AddNodes: Reference type %.*s to the parent is not a ReferenceTypeNode",
  111. (int)nodeIdStr.length, nodeIdStr.data));
  112. UA_NODESTORE_RELEASE(server, (const UA_Node*)referenceType);
  113. return UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  114. }
  115. UA_Boolean referenceTypeIsAbstract = referenceType->isAbstract;
  116. UA_NODESTORE_RELEASE(server, (const UA_Node*)referenceType);
  117. /* Check that the reference type is not abstract */
  118. if(referenceTypeIsAbstract == true) {
  119. UA_LOG_NODEID_WRAP(referenceTypeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  120. "AddNodes: Abstract reference type %.*s to the parent not allowed",
  121. (int)nodeIdStr.length, nodeIdStr.data));
  122. return UA_STATUSCODE_BADREFERENCENOTALLOWED;
  123. }
  124. /* Check hassubtype relation for type nodes */
  125. if(nodeClass == UA_NODECLASS_DATATYPE ||
  126. nodeClass == UA_NODECLASS_VARIABLETYPE ||
  127. nodeClass == UA_NODECLASS_OBJECTTYPE ||
  128. nodeClass == UA_NODECLASS_REFERENCETYPE) {
  129. /* type needs hassubtype reference to the supertype */
  130. if(!UA_NodeId_equal(referenceTypeId, &subtypeId)) {
  131. UA_LOG_INFO_SESSION(&server->config.logger, session,
  132. "AddNodes: Type nodes need to have a HasSubType "
  133. "reference to the parent");
  134. return UA_STATUSCODE_BADREFERENCENOTALLOWED;
  135. }
  136. /* supertype needs to be of the same node type */
  137. if(parentNodeClass != nodeClass) {
  138. UA_LOG_INFO_SESSION(&server->config.logger, session,
  139. "AddNodes: Type nodes needs to be of the same node "
  140. "type as their parent");
  141. return UA_STATUSCODE_BADPARENTNODEIDINVALID;
  142. }
  143. return UA_STATUSCODE_GOOD;
  144. }
  145. /* Test if the referencetype is hierarchical */
  146. if(!isNodeInTree(server, referenceTypeId, &hierarchicalReferences, &subtypeId, 1)) {
  147. UA_LOG_INFO_SESSION(&server->config.logger, session,
  148. "AddNodes: Reference type to the parent is not hierarchical");
  149. return UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  150. }
  151. return UA_STATUSCODE_GOOD;
  152. }
  153. static UA_StatusCode
  154. typeCheckVariableNode(UA_Server *server, UA_Session *session,
  155. const UA_VariableNode *node,
  156. const UA_VariableTypeNode *vt) {
  157. /* The value might come from a datasource, so we perform a
  158. * regular read. */
  159. UA_DataValue value;
  160. UA_DataValue_init(&value);
  161. UA_StatusCode retval = readValueAttribute(server, session, node, &value);
  162. if(retval != UA_STATUSCODE_GOOD)
  163. return retval;
  164. UA_NodeId baseDataType = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATATYPE);
  165. /* Check the datatype against the vt */
  166. /* If the node does not have any value and the dataType is BaseDataType,
  167. * then it's also fine. This is the default for empty nodes. */
  168. if(!compatibleDataType(server, &node->dataType, &vt->dataType, false) &&
  169. (value.hasValue || !UA_NodeId_equal(&node->dataType, &baseDataType))) {
  170. UA_LOG_NODEID_WRAP(&node->nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  171. "AddNodes: The value of %.*s is incompatible with "
  172. "the datatype of the VariableType",
  173. (int)nodeIdStr.length, nodeIdStr.data));
  174. UA_DataValue_clear(&value);
  175. return UA_STATUSCODE_BADTYPEMISMATCH;
  176. }
  177. /* Check valueRank against array dimensions */
  178. if(!compatibleValueRankArrayDimensions(server, session, node->valueRank,
  179. node->arrayDimensionsSize)) {
  180. UA_LOG_NODEID_WRAP(&node->nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  181. "AddNodes: The value rank of %.*s is incomatible "
  182. "with its array dimensions", (int)nodeIdStr.length, nodeIdStr.data));
  183. UA_DataValue_clear(&value);
  184. return UA_STATUSCODE_BADTYPEMISMATCH;
  185. }
  186. /* Check valueRank against the vt */
  187. if(!compatibleValueRanks(node->valueRank, vt->valueRank)) {
  188. UA_LOG_NODEID_WRAP(&node->nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  189. "AddNodes: The value rank of %.*s is incomatible "
  190. "with the value rank of the VariableType",
  191. (int)nodeIdStr.length, nodeIdStr.data));
  192. UA_DataValue_clear(&value);
  193. return UA_STATUSCODE_BADTYPEMISMATCH;
  194. }
  195. /* Check array dimensions against the vt */
  196. if(!compatibleArrayDimensions(vt->arrayDimensionsSize, vt->arrayDimensions,
  197. node->arrayDimensionsSize, node->arrayDimensions)) {
  198. UA_LOG_NODEID_WRAP(&node->nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  199. "AddNodes: The array dimensions of %.*s are "
  200. "incomatible with the array dimensions of the VariableType",
  201. (int)nodeIdStr.length, nodeIdStr.data));
  202. UA_DataValue_clear(&value);
  203. return UA_STATUSCODE_BADTYPEMISMATCH;
  204. }
  205. /* Typecheck the value */
  206. if(value.hasValue && value.value.data) {
  207. /* If the type-check failed write the same value again. The
  208. * write-service tries to convert to the correct type... */
  209. if(!compatibleValue(server, session, &node->dataType, node->valueRank,
  210. node->arrayDimensionsSize, node->arrayDimensions,
  211. &value.value, NULL)) {
  212. retval = writeWithWriteValue(server, &node->nodeId, UA_ATTRIBUTEID_VALUE, &UA_TYPES[UA_TYPES_VARIANT], &value.value);
  213. }
  214. UA_DataValue_clear(&value);
  215. if(retval != UA_STATUSCODE_GOOD) {
  216. UA_LOG_NODEID_WRAP(&node->nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  217. "AddNodes: The value of of %.*s is incomatible with the "
  218. "variable definition", (int)nodeIdStr.length, nodeIdStr.data));
  219. }
  220. }
  221. return retval;
  222. }
  223. /********************/
  224. /* Instantiate Node */
  225. /********************/
  226. static const UA_NodeId baseDataVariableType =
  227. {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_BASEDATAVARIABLETYPE}};
  228. static const UA_NodeId baseObjectType =
  229. {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_BASEOBJECTTYPE}};
  230. static const UA_NodeId hasTypeDefinition =
  231. {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_HASTYPEDEFINITION}};
  232. /* Use attributes from the variable type wherever required. Reload the node if
  233. * changes were made. */
  234. static UA_StatusCode
  235. useVariableTypeAttributes(UA_Server *server, UA_Session *session,
  236. const UA_VariableNode **node_ptr,
  237. const UA_VariableTypeNode *vt) {
  238. const UA_VariableNode *node = *node_ptr;
  239. UA_Boolean modified = false;
  240. /* If no value is set, see if the vt provides one and copy it. This needs to
  241. * be done before copying the datatype from the vt, as setting the datatype
  242. * triggers a typecheck. */
  243. UA_DataValue orig;
  244. UA_DataValue_init(&orig);
  245. UA_StatusCode retval = readValueAttribute(server, session, node, &orig);
  246. if(retval != UA_STATUSCODE_GOOD)
  247. return retval;
  248. if(orig.value.type) {
  249. /* A value is present */
  250. UA_DataValue_clear(&orig);
  251. } else {
  252. UA_WriteValue v;
  253. UA_WriteValue_init(&v);
  254. retval = readValueAttribute(server, session, (const UA_VariableNode*)vt, &v.value);
  255. if(retval == UA_STATUSCODE_GOOD && v.value.hasValue) {
  256. v.nodeId = node->nodeId;
  257. v.attributeId = UA_ATTRIBUTEID_VALUE;
  258. retval = writeWithSession(server, session, &v);
  259. modified = true;
  260. }
  261. UA_DataValue_clear(&v.value);
  262. if(retval != UA_STATUSCODE_GOOD)
  263. return retval;
  264. }
  265. /* If no datatype is given, use the datatype of the vt */
  266. if(UA_NodeId_isNull(&node->dataType)) {
  267. UA_LOG_INFO_SESSION(&server->config.logger, session, "AddNodes: "
  268. "No datatype given; Copy the datatype attribute "
  269. "from the TypeDefinition");
  270. UA_WriteValue v;
  271. UA_WriteValue_init(&v);
  272. v.nodeId = node->nodeId;
  273. v.attributeId = UA_ATTRIBUTEID_DATATYPE;
  274. v.value.hasValue = true;
  275. UA_Variant_setScalar(&v.value.value, (void*)(uintptr_t)&vt->dataType,
  276. &UA_TYPES[UA_TYPES_NODEID]);
  277. retval = writeWithSession(server, session, &v);
  278. modified = true;
  279. if(retval != UA_STATUSCODE_GOOD)
  280. return retval;
  281. }
  282. /* Use the ArrayDimensions of the vt */
  283. if(node->arrayDimensionsSize == 0 && vt->arrayDimensionsSize > 0) {
  284. UA_WriteValue v;
  285. UA_WriteValue_init(&v);
  286. v.nodeId = node->nodeId;
  287. v.attributeId = UA_ATTRIBUTEID_ARRAYDIMENSIONS;
  288. v.value.hasValue = true;
  289. UA_Variant_setArray(&v.value.value, vt->arrayDimensions,
  290. vt->arrayDimensionsSize, &UA_TYPES[UA_TYPES_UINT32]);
  291. retval = writeWithSession(server, session, &v);
  292. modified = true;
  293. if(retval != UA_STATUSCODE_GOOD)
  294. return retval;
  295. }
  296. /* If the node was modified, update the pointer to the new version */
  297. if(modified) {
  298. const UA_VariableNode *updated = (const UA_VariableNode*)
  299. UA_NODESTORE_GET(server, &node->nodeId);
  300. if(!updated)
  301. return UA_STATUSCODE_BADINTERNALERROR;
  302. UA_NODESTORE_RELEASE(server, (const UA_Node*)node);
  303. *node_ptr = updated;
  304. }
  305. return UA_STATUSCODE_GOOD;
  306. }
  307. /* Search for an instance of "browseName" in node searchInstance. Used during
  308. * copyChildNodes to find overwritable/mergable nodes. Does not touch
  309. * outInstanceNodeId if no child is found. */
  310. static UA_StatusCode
  311. findChildByBrowsename(UA_Server *server, UA_Session *session,
  312. const UA_NodeId *searchInstance,
  313. const UA_QualifiedName *browseName,
  314. UA_NodeId *outInstanceNodeId) {
  315. UA_BrowseDescription bd;
  316. UA_BrowseDescription_init(&bd);
  317. bd.nodeId = *searchInstance;
  318. bd.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_AGGREGATES);
  319. bd.includeSubtypes = true;
  320. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  321. bd.nodeClassMask = UA_NODECLASS_OBJECT | UA_NODECLASS_VARIABLE | UA_NODECLASS_METHOD;
  322. bd.resultMask = UA_BROWSERESULTMASK_BROWSENAME;
  323. UA_BrowseResult br;
  324. UA_BrowseResult_init(&br);
  325. UA_UInt32 maxrefs = 0;
  326. Operation_Browse(server, session, &maxrefs, &bd, &br);
  327. if(br.statusCode != UA_STATUSCODE_GOOD)
  328. return br.statusCode;
  329. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  330. for(size_t i = 0; i < br.referencesSize; ++i) {
  331. UA_ReferenceDescription *rd = &br.references[i];
  332. if(rd->browseName.namespaceIndex == browseName->namespaceIndex &&
  333. UA_String_equal(&rd->browseName.name, &browseName->name)) {
  334. retval = UA_NodeId_copy(&rd->nodeId.nodeId, outInstanceNodeId);
  335. break;
  336. }
  337. }
  338. UA_BrowseResult_clear(&br);
  339. return retval;
  340. }
  341. static const UA_NodeId mandatoryId =
  342. {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_MODELLINGRULE_MANDATORY}};
  343. static const UA_NodeId hasModellingRuleId =
  344. {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_HASMODELLINGRULE}};
  345. static UA_Boolean
  346. isMandatoryChild(UA_Server *server, UA_Session *session,
  347. const UA_NodeId *childNodeId) {
  348. /* Get the child */
  349. const UA_Node *child = UA_NODESTORE_GET(server, childNodeId);
  350. if(!child)
  351. return false;
  352. /* Look for the reference making the child mandatory */
  353. for(size_t i = 0; i < child->referencesSize; ++i) {
  354. UA_NodeReferenceKind *refs = &child->references[i];
  355. if(!UA_NodeId_equal(&hasModellingRuleId, &refs->referenceTypeId))
  356. continue;
  357. if(refs->isInverse)
  358. continue;
  359. for(size_t j = 0; j < refs->refTargetsSize; ++j) {
  360. if(UA_NodeId_equal(&mandatoryId, &refs->refTargets[j].target.nodeId)) {
  361. UA_NODESTORE_RELEASE(server, child);
  362. return true;
  363. }
  364. }
  365. }
  366. UA_NODESTORE_RELEASE(server, child);
  367. return false;
  368. }
  369. static UA_StatusCode
  370. copyAllChildren(UA_Server *server, UA_Session *session,
  371. const UA_NodeId *source, const UA_NodeId *destination);
  372. static UA_StatusCode
  373. recursiveTypeCheckAddChildren(UA_Server *server, UA_Session *session,
  374. const UA_Node **node, const UA_Node *type);
  375. static void
  376. Operation_addReference(UA_Server *server, UA_Session *session, void *context,
  377. const UA_AddReferencesItem *item, UA_StatusCode *retval);
  378. static UA_StatusCode
  379. copyChild(UA_Server *server, UA_Session *session, const UA_NodeId *destinationNodeId,
  380. const UA_ReferenceDescription *rd) {
  381. /* Is there an existing child with the browsename? */
  382. UA_NodeId existingChild = UA_NODEID_NULL;
  383. UA_StatusCode retval = findChildByBrowsename(server, session, destinationNodeId,
  384. &rd->browseName, &existingChild);
  385. if(retval != UA_STATUSCODE_GOOD)
  386. return retval;
  387. /* Have a child with that browseName. Deep-copy missing members. */
  388. if(!UA_NodeId_isNull(&existingChild)) {
  389. if(rd->nodeClass == UA_NODECLASS_VARIABLE ||
  390. rd->nodeClass == UA_NODECLASS_OBJECT)
  391. retval = copyAllChildren(server, session, &rd->nodeId.nodeId, &existingChild);
  392. UA_NodeId_clear(&existingChild);
  393. return retval;
  394. }
  395. /* Is the child mandatory? If not, ask callback whether child should be instantiated.
  396. * If not, skip. */
  397. if(!isMandatoryChild(server, session, &rd->nodeId.nodeId)) {
  398. if(!server->config.nodeLifecycle.createOptionalChild)
  399. return UA_STATUSCODE_GOOD;
  400. UA_UNLOCK(server->serviceMutex);
  401. retval = server->config.nodeLifecycle.createOptionalChild(server,
  402. &session->sessionId,
  403. session->sessionHandle,
  404. &rd->nodeId.nodeId,
  405. destinationNodeId,
  406. &rd->referenceTypeId);
  407. UA_LOCK(server->serviceMutex);
  408. if(retval == UA_FALSE) {
  409. return UA_STATUSCODE_GOOD;
  410. }
  411. }
  412. /* Child is a method -> create a reference */
  413. if(rd->nodeClass == UA_NODECLASS_METHOD) {
  414. UA_AddReferencesItem newItem;
  415. UA_AddReferencesItem_init(&newItem);
  416. newItem.sourceNodeId = *destinationNodeId;
  417. newItem.referenceTypeId = rd->referenceTypeId;
  418. newItem.isForward = true;
  419. newItem.targetNodeId = rd->nodeId;
  420. newItem.targetNodeClass = UA_NODECLASS_METHOD;
  421. Operation_addReference(server, session, NULL, &newItem, &retval);
  422. return retval;
  423. }
  424. /* Child is a variable or object */
  425. if(rd->nodeClass == UA_NODECLASS_VARIABLE ||
  426. rd->nodeClass == UA_NODECLASS_OBJECT) {
  427. /* Make a copy of the node */
  428. UA_Node *node;
  429. retval = UA_NODESTORE_GETCOPY(server, &rd->nodeId.nodeId, &node);
  430. if(retval != UA_STATUSCODE_GOOD)
  431. return retval;
  432. /* Remove the context of the copied node */
  433. node->context = NULL;
  434. node->constructed = false;
  435. /* Reset the NodeId (random numeric id will be assigned in the nodestore) */
  436. UA_NodeId_clear(&node->nodeId);
  437. node->nodeId.namespaceIndex = destinationNodeId->namespaceIndex;
  438. if (server->config.nodeLifecycle.generateChildNodeId) {
  439. UA_UNLOCK(server->serviceMutex);
  440. retval = server->config.nodeLifecycle.generateChildNodeId(server,
  441. &session->sessionId, session->sessionHandle,
  442. &rd->nodeId.nodeId,
  443. destinationNodeId,
  444. &rd->referenceTypeId,
  445. &node->nodeId);
  446. UA_LOCK(server->serviceMutex);
  447. if(retval != UA_STATUSCODE_GOOD) {
  448. UA_NODESTORE_DELETE(server, node);
  449. return retval;
  450. }
  451. }
  452. /* Remove references, they are re-created from scratch in addnode_finish */
  453. /* TODO: Be more clever in removing references that are re-added during
  454. * addnode_finish. That way, we can call addnode_finish also on children that were
  455. * manually added by the user during addnode_begin and addnode_finish. */
  456. /* For now we keep all the modelling rule references and delete all others */
  457. UA_NodeId modellingRuleReferenceId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE);
  458. UA_Node_deleteReferencesSubset(node, 1, &modellingRuleReferenceId);
  459. /* Add the node to the nodestore */
  460. UA_NodeId newNodeId;
  461. retval = UA_NODESTORE_INSERT(server, node, &newNodeId);
  462. if(retval != UA_STATUSCODE_GOOD)
  463. return retval;
  464. /* Add the node references */
  465. retval = AddNode_addRefs(server, session, &newNodeId, destinationNodeId,
  466. &rd->referenceTypeId, &rd->typeDefinition.nodeId);
  467. if(retval != UA_STATUSCODE_GOOD) {
  468. UA_NODESTORE_REMOVE(server, &newNodeId);
  469. return retval;
  470. }
  471. /* For the new child, recursively copy the members of the original. No
  472. * typechecking is performed here. Assuming that the original is
  473. * consistent. */
  474. retval = copyAllChildren(server, session, &rd->nodeId.nodeId, &newNodeId);
  475. }
  476. return retval;
  477. }
  478. /* Copy any children of Node sourceNodeId to another node destinationNodeId. */
  479. static UA_StatusCode
  480. copyAllChildren(UA_Server *server, UA_Session *session,
  481. const UA_NodeId *source, const UA_NodeId *destination) {
  482. /* Browse to get all children of the source */
  483. UA_BrowseDescription bd;
  484. UA_BrowseDescription_init(&bd);
  485. bd.nodeId = *source;
  486. bd.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_AGGREGATES);
  487. bd.includeSubtypes = true;
  488. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  489. bd.nodeClassMask = UA_NODECLASS_OBJECT | UA_NODECLASS_VARIABLE | UA_NODECLASS_METHOD;
  490. bd.resultMask = UA_BROWSERESULTMASK_REFERENCETYPEID | UA_BROWSERESULTMASK_NODECLASS |
  491. UA_BROWSERESULTMASK_BROWSENAME | UA_BROWSERESULTMASK_TYPEDEFINITION;
  492. UA_BrowseResult br;
  493. UA_BrowseResult_init(&br);
  494. UA_UInt32 maxrefs = 0;
  495. Operation_Browse(server, session, &maxrefs, &bd, &br);
  496. if(br.statusCode != UA_STATUSCODE_GOOD)
  497. return br.statusCode;
  498. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  499. for(size_t i = 0; i < br.referencesSize; ++i) {
  500. UA_ReferenceDescription *rd = &br.references[i];
  501. retval = copyChild(server, session, destination, rd);
  502. if(retval != UA_STATUSCODE_GOOD)
  503. return retval;
  504. }
  505. UA_BrowseResult_clear(&br);
  506. return retval;
  507. }
  508. static UA_StatusCode
  509. addTypeChildren(UA_Server *server, UA_Session *session,
  510. const UA_Node *node, const UA_Node *type) {
  511. /* Get the hierarchy of the type and all its supertypes */
  512. UA_NodeId *hierarchy = NULL;
  513. size_t hierarchySize = 0;
  514. UA_StatusCode retval = getParentTypeAndInterfaceHierarchy(server, &type->nodeId,
  515. &hierarchy, &hierarchySize);
  516. if(retval != UA_STATUSCODE_GOOD)
  517. return retval;
  518. UA_assert(hierarchySize < 1000);
  519. /* Copy members of the type and supertypes (and instantiate them) */
  520. for(size_t i = 0; i < hierarchySize; ++i) {
  521. retval = copyAllChildren(server, session, &hierarchy[i], &node->nodeId);
  522. if(retval != UA_STATUSCODE_GOOD)
  523. break;
  524. }
  525. UA_Array_delete(hierarchy, hierarchySize, &UA_TYPES[UA_TYPES_NODEID]);
  526. return retval;
  527. }
  528. static UA_StatusCode
  529. addRef(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId,
  530. const UA_NodeId *referenceTypeId, const UA_NodeId *parentNodeId,
  531. UA_Boolean forward) {
  532. UA_AddReferencesItem ref_item;
  533. UA_AddReferencesItem_init(&ref_item);
  534. ref_item.sourceNodeId = *nodeId;
  535. ref_item.referenceTypeId = *referenceTypeId;
  536. ref_item.isForward = forward;
  537. ref_item.targetNodeId.nodeId = *parentNodeId;
  538. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  539. Operation_addReference(server, session, NULL, &ref_item, &retval);
  540. return retval;
  541. }
  542. /************/
  543. /* Add Node */
  544. /************/
  545. static const UA_NodeId hasSubtype = {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_HASSUBTYPE}};
  546. UA_StatusCode
  547. AddNode_addRefs(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId,
  548. const UA_NodeId *parentNodeId, const UA_NodeId *referenceTypeId,
  549. const UA_NodeId *typeDefinitionId) {
  550. /* Get the node */
  551. const UA_Node *type = NULL;
  552. const UA_Node *node = UA_NODESTORE_GET(server, nodeId);
  553. if(!node)
  554. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  555. /* Use the typeDefinition as parent for type-nodes */
  556. if(node->nodeClass == UA_NODECLASS_VARIABLETYPE ||
  557. node->nodeClass == UA_NODECLASS_OBJECTTYPE ||
  558. node->nodeClass == UA_NODECLASS_REFERENCETYPE ||
  559. node->nodeClass == UA_NODECLASS_DATATYPE) {
  560. if(UA_NodeId_equal(referenceTypeId, &UA_NODEID_NULL))
  561. referenceTypeId = &hasSubtype;
  562. const UA_Node *parentNode = UA_NODESTORE_GET(server, parentNodeId);
  563. if(parentNode) {
  564. if(parentNode->nodeClass == node->nodeClass)
  565. typeDefinitionId = parentNodeId;
  566. UA_NODESTORE_RELEASE(server, parentNode);
  567. }
  568. }
  569. UA_StatusCode retval;
  570. /* Make sure newly created node does not have itself as parent */
  571. if (UA_NodeId_equal(nodeId, parentNodeId)) {
  572. UA_LOG_NODEID_WRAP(nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  573. "AddNodes: The node %.*s can not have "
  574. "itself as parent",
  575. (int)nodeIdStr.length, nodeIdStr.data));
  576. retval = UA_STATUSCODE_BADINVALIDARGUMENT;
  577. goto cleanup;
  578. }
  579. /* Check parent reference. Objects may have no parent. */
  580. retval = checkParentReference(server, session, node->nodeClass,
  581. parentNodeId, referenceTypeId);
  582. if(retval != UA_STATUSCODE_GOOD) {
  583. UA_LOG_NODEID_WRAP(nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  584. "AddNodes: The parent reference for %.*s is invalid "
  585. "with status code %s",
  586. (int)nodeIdStr.length, nodeIdStr.data,
  587. UA_StatusCode_name(retval)));
  588. goto cleanup;
  589. }
  590. /* Replace empty typeDefinition with the most permissive default */
  591. if((node->nodeClass == UA_NODECLASS_VARIABLE ||
  592. node->nodeClass == UA_NODECLASS_OBJECT) &&
  593. UA_NodeId_isNull(typeDefinitionId)) {
  594. UA_LOG_NODEID_WRAP(nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  595. "AddNodes: No TypeDefinition for %.*s; Use the default "
  596. "TypeDefinition for the Variable/Object",
  597. (int)nodeIdStr.length, nodeIdStr.data));
  598. if(node->nodeClass == UA_NODECLASS_VARIABLE)
  599. typeDefinitionId = &baseDataVariableType;
  600. else
  601. typeDefinitionId = &baseObjectType;
  602. }
  603. /* Get the node type. There must be a typedefinition for variables, objects
  604. * and type-nodes. See the above checks. */
  605. if(!UA_NodeId_isNull(typeDefinitionId)) {
  606. /* Get the type node */
  607. type = UA_NODESTORE_GET(server, typeDefinitionId);
  608. if(!type) {
  609. UA_LOG_NODEID_WRAP(typeDefinitionId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  610. "AddNodes: Node type %.*s not found",
  611. (int)nodeIdStr.length, nodeIdStr.data));
  612. retval = UA_STATUSCODE_BADTYPEDEFINITIONINVALID;
  613. goto cleanup;
  614. }
  615. UA_Boolean typeOk = false;
  616. switch(node->nodeClass) {
  617. case UA_NODECLASS_DATATYPE:
  618. typeOk = type->nodeClass == UA_NODECLASS_DATATYPE;
  619. break;
  620. case UA_NODECLASS_METHOD:
  621. typeOk = type->nodeClass == UA_NODECLASS_METHOD;
  622. break;
  623. case UA_NODECLASS_OBJECT:
  624. typeOk = type->nodeClass == UA_NODECLASS_OBJECTTYPE;
  625. break;
  626. case UA_NODECLASS_OBJECTTYPE:
  627. typeOk = type->nodeClass == UA_NODECLASS_OBJECTTYPE;
  628. break;
  629. case UA_NODECLASS_REFERENCETYPE:
  630. typeOk = type->nodeClass == UA_NODECLASS_REFERENCETYPE;
  631. break;
  632. case UA_NODECLASS_VARIABLE:
  633. typeOk = type->nodeClass == UA_NODECLASS_VARIABLETYPE;
  634. break;
  635. case UA_NODECLASS_VARIABLETYPE:
  636. typeOk = type->nodeClass == UA_NODECLASS_VARIABLETYPE;
  637. break;
  638. case UA_NODECLASS_VIEW:
  639. typeOk = type->nodeClass == UA_NODECLASS_VIEW;
  640. break;
  641. default:
  642. typeOk = false;
  643. }
  644. if(!typeOk) {
  645. UA_LOG_NODEID_WRAP(nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  646. "AddNodes: Type for %.*s does not match node class",
  647. (int)nodeIdStr.length, nodeIdStr.data));
  648. retval = UA_STATUSCODE_BADTYPEDEFINITIONINVALID;
  649. goto cleanup;
  650. }
  651. /* See if the type has the correct node class. For type-nodes, we know
  652. * that type has the same nodeClass from checkParentReference. */
  653. if(node->nodeClass == UA_NODECLASS_VARIABLE) {
  654. if(((const UA_VariableTypeNode*)type)->isAbstract) {
  655. /* Get subtypes of the parent reference types */
  656. UA_NodeId *parentTypeHierarchy = NULL;
  657. size_t parentTypeHierarchySize = 0;
  658. retval |= referenceSubtypes(server, &parentReferences[0],
  659. &parentTypeHierarchySize, &parentTypeHierarchy);
  660. retval |= referenceSubtypes(server, &parentReferences[1],
  661. &parentTypeHierarchySize, &parentTypeHierarchy);
  662. if(retval != UA_STATUSCODE_GOOD) {
  663. UA_Array_delete(parentTypeHierarchy, parentTypeHierarchySize,
  664. &UA_TYPES[UA_TYPES_NODEID]);
  665. goto cleanup;
  666. }
  667. /* Abstract variable is allowed if parent is a children of a
  668. * base data variable. An abstract variable may be part of an
  669. * object type which again is below BaseObjectType */
  670. const UA_NodeId variableTypes = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE);
  671. const UA_NodeId objectTypes = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE);
  672. if(!isNodeInTree(server, parentNodeId, &variableTypes,
  673. parentTypeHierarchy, parentTypeHierarchySize) &&
  674. !isNodeInTree(server, parentNodeId, &objectTypes,
  675. parentTypeHierarchy, parentTypeHierarchySize)) {
  676. UA_LOG_NODEID_WRAP(nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  677. "AddNodes: Type of variable node %.*s must "
  678. "be VariableType and not cannot be abstract",
  679. (int)nodeIdStr.length, nodeIdStr.data));
  680. retval = UA_STATUSCODE_BADTYPEDEFINITIONINVALID;
  681. }
  682. UA_Array_delete(parentTypeHierarchy, parentTypeHierarchySize,
  683. &UA_TYPES[UA_TYPES_NODEID]);
  684. if(retval != UA_STATUSCODE_GOOD)
  685. goto cleanup;
  686. }
  687. }
  688. if(node->nodeClass == UA_NODECLASS_OBJECT) {
  689. if(((const UA_ObjectTypeNode*)type)->isAbstract) {
  690. /* Get subtypes of the parent reference types */
  691. UA_NodeId *parentTypeHierarchy = NULL;
  692. size_t parentTypeHierarchySize = 0;
  693. retval |= referenceSubtypes(server, &parentReferences[0],
  694. &parentTypeHierarchySize, &parentTypeHierarchy);
  695. retval |= referenceSubtypes(server, &parentReferences[1],
  696. &parentTypeHierarchySize, &parentTypeHierarchy);
  697. if(retval != UA_STATUSCODE_GOOD) {
  698. UA_Array_delete(parentTypeHierarchy, parentTypeHierarchySize,
  699. &UA_TYPES[UA_TYPES_NODEID]);
  700. goto cleanup;
  701. }
  702. /* Object node created of an abstract ObjectType. Only allowed
  703. * if within BaseObjectType folder or if it's an event (subType of BaseEventType) */
  704. const UA_NodeId objectTypes = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE);
  705. UA_Boolean isInBaseObjectType =
  706. isNodeInTree(server, parentNodeId, &objectTypes,
  707. parentTypeHierarchy, parentTypeHierarchySize);
  708. const UA_NodeId eventTypes = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE);
  709. UA_Boolean isInBaseEventType =
  710. isNodeInTree(server, &type->nodeId, &eventTypes, &hasSubtype, 1);
  711. if(!isInBaseObjectType && !(isInBaseEventType && UA_NodeId_isNull(parentNodeId))) {
  712. UA_LOG_NODEID_WRAP(nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  713. "AddNodes: Type of object node %.*s must "
  714. "be ObjectType and not be abstract",
  715. (int)nodeIdStr.length, nodeIdStr.data));
  716. retval = UA_STATUSCODE_BADTYPEDEFINITIONINVALID;
  717. }
  718. UA_Array_delete(parentTypeHierarchy, parentTypeHierarchySize,
  719. &UA_TYPES[UA_TYPES_NODEID]);
  720. if(retval != UA_STATUSCODE_GOOD)
  721. goto cleanup;
  722. }
  723. }
  724. }
  725. /* Add reference to the parent */
  726. if(!UA_NodeId_isNull(parentNodeId)) {
  727. if(UA_NodeId_isNull(referenceTypeId)) {
  728. UA_LOG_NODEID_WRAP(nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  729. "AddNodes: Reference to parent of %.*s cannot be null",
  730. (int)nodeIdStr.length, nodeIdStr.data));
  731. retval = UA_STATUSCODE_BADTYPEDEFINITIONINVALID;
  732. goto cleanup;
  733. }
  734. retval = addRef(server, session, &node->nodeId, referenceTypeId, parentNodeId, false);
  735. if(retval != UA_STATUSCODE_GOOD) {
  736. UA_LOG_NODEID_WRAP(nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  737. "AddNodes: Adding reference to parent of %.*s failed",
  738. (int)nodeIdStr.length, nodeIdStr.data));
  739. goto cleanup;
  740. }
  741. }
  742. /* Add a hasTypeDefinition reference */
  743. if(node->nodeClass == UA_NODECLASS_VARIABLE ||
  744. node->nodeClass == UA_NODECLASS_OBJECT) {
  745. UA_assert(type != NULL); /* see above */
  746. retval = addRef(server, session, &node->nodeId, &hasTypeDefinition, &type->nodeId, true);
  747. if(retval != UA_STATUSCODE_GOOD) {
  748. UA_LOG_NODEID_WRAP(nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  749. "AddNodes: Adding a reference to the type "
  750. "definition of %.*s failed with error code %s",
  751. (int)nodeIdStr.length, nodeIdStr.data,
  752. UA_StatusCode_name(retval)));
  753. }
  754. }
  755. cleanup:
  756. UA_NODESTORE_RELEASE(server, node);
  757. if(type)
  758. UA_NODESTORE_RELEASE(server, type);
  759. return retval;
  760. }
  761. /* Create the node and add it to the nodestore. But don't typecheck and add
  762. * references so far */
  763. UA_StatusCode
  764. AddNode_raw(UA_Server *server, UA_Session *session, void *nodeContext,
  765. const UA_AddNodesItem *item, UA_NodeId *outNewNodeId) {
  766. /* Do not check access for server */
  767. if(session != &server->adminSession && server->config.accessControl.allowAddNode) {
  768. UA_UNLOCK(server->serviceMutex)
  769. if (!server->config.accessControl.allowAddNode(server, &server->config.accessControl,
  770. &session->sessionId, session->sessionHandle, item)) {
  771. UA_LOCK(server->serviceMutex);
  772. return UA_STATUSCODE_BADUSERACCESSDENIED;
  773. }
  774. UA_LOCK(server->serviceMutex);
  775. }
  776. /* Check the namespaceindex */
  777. if(item->requestedNewNodeId.nodeId.namespaceIndex >= server->namespacesSize) {
  778. UA_LOG_INFO_SESSION(&server->config.logger, session,
  779. "AddNodes: Namespace invalid");
  780. return UA_STATUSCODE_BADNODEIDINVALID;
  781. }
  782. if(item->nodeAttributes.encoding != UA_EXTENSIONOBJECT_DECODED &&
  783. item->nodeAttributes.encoding != UA_EXTENSIONOBJECT_DECODED_NODELETE) {
  784. UA_LOG_INFO_SESSION(&server->config.logger, session,
  785. "AddNodes: Node attributes invalid");
  786. return UA_STATUSCODE_BADINTERNALERROR;
  787. }
  788. /* Create a node */
  789. UA_Node *node = UA_NODESTORE_NEW(server, item->nodeClass);
  790. if(!node) {
  791. UA_LOG_INFO_SESSION(&server->config.logger, session,
  792. "AddNodes: Node could not create a node "
  793. "in the nodestore");
  794. return UA_STATUSCODE_BADOUTOFMEMORY;
  795. }
  796. /* Fill the node attributes */
  797. node->context = nodeContext;
  798. UA_StatusCode retval = UA_NodeId_copy(&item->requestedNewNodeId.nodeId, &node->nodeId);
  799. if(retval != UA_STATUSCODE_GOOD)
  800. goto create_error;
  801. retval = UA_QualifiedName_copy(&item->browseName, &node->browseName);
  802. if(retval != UA_STATUSCODE_GOOD)
  803. goto create_error;
  804. retval = UA_Node_setAttributes(node, item->nodeAttributes.content.decoded.data,
  805. item->nodeAttributes.content.decoded.type);
  806. if(retval != UA_STATUSCODE_GOOD)
  807. goto create_error;
  808. /* Add the node to the nodestore */
  809. retval = UA_NODESTORE_INSERT(server, node, outNewNodeId);
  810. if(retval != UA_STATUSCODE_GOOD)
  811. UA_LOG_INFO_SESSION(&server->config.logger, session,
  812. "AddNodes: Node could not add the new node "
  813. "to the nodestore with error code %s",
  814. UA_StatusCode_name(retval));
  815. return retval;
  816. create_error:
  817. UA_LOG_INFO_SESSION(&server->config.logger, session,
  818. "AddNodes: Node could not create a node "
  819. "with error code %s", UA_StatusCode_name(retval));
  820. UA_NODESTORE_DELETE(server, node);
  821. return retval;
  822. }
  823. /* Prepare the node, then add it to the nodestore */
  824. static UA_StatusCode
  825. Operation_addNode_begin(UA_Server *server, UA_Session *session, void *nodeContext,
  826. const UA_AddNodesItem *item, const UA_NodeId *parentNodeId,
  827. const UA_NodeId *referenceTypeId, UA_NodeId *outNewNodeId) {
  828. /* Create a temporary NodeId if none is returned */
  829. UA_NodeId newId;
  830. if(!outNewNodeId) {
  831. UA_NodeId_init(&newId);
  832. outNewNodeId = &newId;
  833. }
  834. /* Create the node and add it to the nodestore */
  835. UA_StatusCode retval = AddNode_raw(server, session, nodeContext, item, outNewNodeId);
  836. if(retval != UA_STATUSCODE_GOOD)
  837. return retval;
  838. /* Typecheck and add references to parent and type definition */
  839. retval = AddNode_addRefs(server, session, outNewNodeId, parentNodeId,
  840. referenceTypeId, &item->typeDefinition.nodeId);
  841. if(retval != UA_STATUSCODE_GOOD)
  842. deleteNode(server, *outNewNodeId, true);
  843. if(outNewNodeId == &newId)
  844. UA_NodeId_clear(&newId);
  845. return retval;
  846. }
  847. static UA_StatusCode
  848. recursiveTypeCheckAddChildren(UA_Server *server, UA_Session *session,
  849. const UA_Node **nodeptr, const UA_Node *type) {
  850. UA_assert(type != NULL);
  851. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  852. const UA_Node *node = *nodeptr;
  853. /* Use attributes from the type. The value and value constraints are the
  854. * same for the variable and variabletype attribute structs. */
  855. if(node->nodeClass == UA_NODECLASS_VARIABLE ||
  856. node->nodeClass == UA_NODECLASS_VARIABLETYPE) {
  857. retval = useVariableTypeAttributes(server, session, (const UA_VariableNode**)nodeptr,
  858. (const UA_VariableTypeNode*)type);
  859. node = *nodeptr; /* If the node was replaced */
  860. if(retval != UA_STATUSCODE_GOOD) {
  861. UA_LOG_NODEID_WRAP(&node->nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  862. "AddNodes: Using attributes for %.*s from the variable type "
  863. "failed with error code %s", (int)nodeIdStr.length,
  864. nodeIdStr.data, UA_StatusCode_name(retval)));
  865. return retval;
  866. }
  867. /* Check NodeClass for 'hasSubtype'. UA_NODECLASS_VARIABLE not allowed to have subtype */
  868. if((node->nodeClass == UA_NODECLASS_VARIABLE) && (UA_NodeId_equal(
  869. &node->references->referenceTypeId, &hasSubtype))) {
  870. UA_LOG_INFO_SESSION(&server->config.logger, session,
  871. "AddNodes: VariableType not allowed to have HasSubType");
  872. return UA_STATUSCODE_BADREFERENCENOTALLOWED;
  873. }
  874. /* Check if all attributes hold the constraints of the type now. The initial
  875. * attributes must type-check. The constructor might change the attributes
  876. * again. Then, the changes are type-checked by the normal write service. */
  877. retval = typeCheckVariableNode(server, session, (const UA_VariableNode*)node,
  878. (const UA_VariableTypeNode*)type);
  879. if(retval != UA_STATUSCODE_GOOD) {
  880. UA_LOG_NODEID_WRAP(&node->nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  881. "AddNodes: Type-checking the variable node %.*s "
  882. "failed with error code %s", (int)nodeIdStr.length,
  883. nodeIdStr.data, UA_StatusCode_name(retval)));
  884. return retval;
  885. }
  886. }
  887. /* Add (mandatory) child nodes from the type definition */
  888. if(node->nodeClass == UA_NODECLASS_VARIABLE ||
  889. node->nodeClass == UA_NODECLASS_OBJECT) {
  890. retval = addTypeChildren(server, session, node, type);
  891. if(retval != UA_STATUSCODE_GOOD) {
  892. UA_LOG_NODEID_WRAP(&node->nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  893. "AddNodes: Adding child nodes of %.*s failed with error code %s",
  894. (int)nodeIdStr.length, nodeIdStr.data, UA_StatusCode_name(retval)));
  895. }
  896. }
  897. return UA_STATUSCODE_GOOD;
  898. }
  899. static UA_StatusCode
  900. findDefaultInstanceBrowseNameNode(UA_Server *server,
  901. UA_NodeId startingNode, UA_NodeId *foundId){
  902. UA_NodeId_init(foundId);
  903. UA_RelativePathElement rpe;
  904. UA_RelativePathElement_init(&rpe);
  905. rpe.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY);
  906. rpe.isInverse = false;
  907. rpe.includeSubtypes = false;
  908. rpe.targetName = UA_QUALIFIEDNAME(0, "DefaultInstanceBrowseName");
  909. UA_BrowsePath bp;
  910. UA_BrowsePath_init(&bp);
  911. bp.startingNode = startingNode;
  912. bp.relativePath.elementsSize = 1;
  913. bp.relativePath.elements = &rpe;
  914. UA_BrowsePathResult bpr =
  915. translateBrowsePathToNodeIds(server, &bp);
  916. UA_StatusCode retval = bpr.statusCode;
  917. if (retval == UA_STATUSCODE_GOOD &&
  918. bpr.targetsSize > 0) {
  919. retval = UA_NodeId_copy(&bpr.targets[0].targetId.nodeId, foundId);
  920. }
  921. UA_BrowsePathResult_clear(&bpr);
  922. return retval;
  923. }
  924. /* Check if we got a valid browse name for the new node.
  925. * For object nodes the BrowseName may only be null if the parent type has a
  926. * 'DefaultInstanceBrowseName' property.
  927. * */
  928. static UA_StatusCode
  929. checkValidBrowseName(UA_Server *server, UA_Session *session,
  930. const UA_Node *node, const UA_Node *type) {
  931. UA_assert(type != NULL);
  932. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  933. if(node->nodeClass != UA_NODECLASS_OBJECT) {
  934. /* nodes other than Objects must have a browseName */
  935. if (UA_QualifiedName_isNull(&node->browseName))
  936. return UA_STATUSCODE_BADBROWSENAMEINVALID;
  937. return UA_STATUSCODE_GOOD;
  938. }
  939. /* If the object node already has a browse name we are done here. */
  940. if(!UA_QualifiedName_isNull(&node->browseName))
  941. return UA_STATUSCODE_GOOD;
  942. /* at this point we have an object with an empty browse name.
  943. * Check the type node if it has a DefaultInstanceBrowseName property
  944. */
  945. UA_NodeId defaultBrowseNameNode;
  946. retval = findDefaultInstanceBrowseNameNode(server, type->nodeId, &defaultBrowseNameNode);
  947. if (retval != UA_STATUSCODE_GOOD) {
  948. if (retval == UA_STATUSCODE_BADNOMATCH)
  949. /* the DefaultBrowseName property is not found, return the corresponding status code */
  950. return UA_STATUSCODE_BADBROWSENAMEINVALID;
  951. return retval;
  952. }
  953. UA_Variant defaultBrowseName;
  954. retval = readWithReadValue(server, &defaultBrowseNameNode, UA_ATTRIBUTEID_VALUE, &defaultBrowseName);
  955. if (retval != UA_STATUSCODE_GOOD)
  956. return retval;
  957. UA_QualifiedName *defaultValue = (UA_QualifiedName *) defaultBrowseName.data;
  958. retval = writeWithWriteValue(server, &node->nodeId, UA_ATTRIBUTEID_BROWSENAME, &UA_TYPES[UA_TYPES_QUALIFIEDNAME], defaultValue);
  959. UA_Variant_clear(&defaultBrowseName);
  960. return retval;
  961. }
  962. /* Construct children first */
  963. static UA_StatusCode
  964. recursiveCallConstructors(UA_Server *server, UA_Session *session,
  965. const UA_Node *node, const UA_Node *type) {
  966. if(node->constructed)
  967. return UA_STATUSCODE_GOOD;
  968. /* Construct the children */
  969. UA_BrowseDescription bd;
  970. UA_BrowseDescription_init(&bd);
  971. bd.nodeId = node->nodeId;
  972. bd.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_AGGREGATES);
  973. bd.includeSubtypes = true;
  974. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  975. UA_BrowseResult br;
  976. UA_BrowseResult_init(&br);
  977. UA_UInt32 maxrefs = 0;
  978. Operation_Browse(server, session, &maxrefs, &bd, &br);
  979. if(br.statusCode != UA_STATUSCODE_GOOD)
  980. return br.statusCode;
  981. /* Call the constructor for every unconstructed node */
  982. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  983. for(size_t i = 0; i < br.referencesSize; ++i) {
  984. UA_ReferenceDescription *rd = &br.references[i];
  985. const UA_Node *target = UA_NODESTORE_GET(server, &rd->nodeId.nodeId);
  986. if(!target)
  987. continue;
  988. if(target->constructed) {
  989. UA_NODESTORE_RELEASE(server, target);
  990. continue;
  991. }
  992. const UA_Node *targetType = NULL;
  993. if(node->nodeClass == UA_NODECLASS_VARIABLE ||
  994. node->nodeClass == UA_NODECLASS_OBJECT) {
  995. targetType = getNodeType(server, target);
  996. if(!targetType) {
  997. UA_NODESTORE_RELEASE(server, target);
  998. retval = UA_STATUSCODE_BADTYPEDEFINITIONINVALID;
  999. break;
  1000. }
  1001. }
  1002. retval = recursiveCallConstructors(server, session, target, targetType);
  1003. UA_NODESTORE_RELEASE(server, target);
  1004. if(targetType)
  1005. UA_NODESTORE_RELEASE(server, targetType);
  1006. if(retval != UA_STATUSCODE_GOOD)
  1007. break;
  1008. }
  1009. UA_BrowseResult_clear(&br);
  1010. /* If a child could not be constructed or the node is already constructed */
  1011. if(retval != UA_STATUSCODE_GOOD)
  1012. return retval;
  1013. /* Get the node type constructor */
  1014. const UA_NodeTypeLifecycle *lifecycle = NULL;
  1015. if(type && node->nodeClass == UA_NODECLASS_OBJECT) {
  1016. const UA_ObjectTypeNode *ot = (const UA_ObjectTypeNode*)type;
  1017. lifecycle = &ot->lifecycle;
  1018. } else if(type && node->nodeClass == UA_NODECLASS_VARIABLE) {
  1019. const UA_VariableTypeNode *vt = (const UA_VariableTypeNode*)type;
  1020. lifecycle = &vt->lifecycle;
  1021. }
  1022. /* Call the global constructor */
  1023. void *context = node->context;
  1024. if(server->config.nodeLifecycle.constructor) {
  1025. UA_UNLOCK(server->serviceMutex);
  1026. retval = server->config.nodeLifecycle.constructor(server, &session->sessionId,
  1027. session->sessionHandle,
  1028. &node->nodeId, &context);
  1029. UA_LOCK(server->serviceMutex);
  1030. }
  1031. /* Call the type constructor */
  1032. if(retval == UA_STATUSCODE_GOOD && lifecycle && lifecycle->constructor) {
  1033. UA_UNLOCK(server->serviceMutex)
  1034. retval = lifecycle->constructor(server, &session->sessionId,
  1035. session->sessionHandle, &type->nodeId,
  1036. type->context, &node->nodeId, &context);
  1037. UA_LOCK(server->serviceMutex);
  1038. }
  1039. if(retval != UA_STATUSCODE_GOOD)
  1040. goto fail1;
  1041. /* Set the context *and* mark the node as constructed */
  1042. if(retval == UA_STATUSCODE_GOOD)
  1043. retval = UA_Server_editNode(server, &server->adminSession, &node->nodeId,
  1044. (UA_EditNodeCallback)setConstructedNodeContext,
  1045. context);
  1046. /* All good, return */
  1047. if(retval == UA_STATUSCODE_GOOD)
  1048. return retval;
  1049. /* Fail. Call the destructors. */
  1050. if(lifecycle && lifecycle->destructor) {
  1051. UA_UNLOCK(server->serviceMutex);
  1052. lifecycle->destructor(server, &session->sessionId,
  1053. session->sessionHandle, &type->nodeId,
  1054. type->context, &node->nodeId, &context);
  1055. UA_LOCK(server->serviceMutex)
  1056. }
  1057. fail1:
  1058. if(server->config.nodeLifecycle.destructor) {
  1059. UA_UNLOCK(server->serviceMutex);
  1060. server->config.nodeLifecycle.destructor(server, &session->sessionId,
  1061. session->sessionHandle,
  1062. &node->nodeId, context);
  1063. UA_LOCK(server->serviceMutex);
  1064. }
  1065. return retval;
  1066. }
  1067. static void
  1068. recursiveDeconstructNode(UA_Server *server, UA_Session *session,
  1069. size_t hierarchicalReferencesSize,
  1070. UA_ExpandedNodeId *hierarchicalReferences,
  1071. const UA_Node *node);
  1072. static void
  1073. recursiveDeleteNode(UA_Server *server, UA_Session *session,
  1074. size_t hierarchicalReferencesSize,
  1075. UA_ExpandedNodeId *hierarchicalReferences,
  1076. const UA_Node *node, UA_Boolean removeTargetRefs);
  1077. /* Children, references, type-checking, constructors. */
  1078. UA_StatusCode
  1079. AddNode_finish(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId) {
  1080. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1081. /* Get the node */
  1082. const UA_Node *node = UA_NODESTORE_GET(server, nodeId);
  1083. if(!node)
  1084. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  1085. const UA_Node *type = NULL;
  1086. /* Instantiate variables and objects */
  1087. if(node->nodeClass == UA_NODECLASS_VARIABLE ||
  1088. node->nodeClass == UA_NODECLASS_VARIABLETYPE ||
  1089. node->nodeClass == UA_NODECLASS_OBJECT) {
  1090. /* Get the type node */
  1091. type = getNodeType(server, node);
  1092. if(!type) {
  1093. if(server->bootstrapNS0)
  1094. goto constructor;
  1095. UA_LOG_NODEID_WRAP(&node->nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  1096. "AddNodes: Node type for %.*s not found",
  1097. (int)nodeIdStr.length, nodeIdStr.data));
  1098. retval = UA_STATUSCODE_BADTYPEDEFINITIONINVALID;
  1099. goto cleanup;
  1100. }
  1101. retval = checkValidBrowseName(server, session, node, type);
  1102. if(retval != UA_STATUSCODE_GOOD)
  1103. goto cleanup;
  1104. retval = recursiveTypeCheckAddChildren(server, session, &node, type);
  1105. if(retval != UA_STATUSCODE_GOOD)
  1106. goto cleanup;
  1107. }
  1108. /* Call the constructor(s) */
  1109. constructor:
  1110. retval = recursiveCallConstructors(server, session, node, type);
  1111. if(retval != UA_STATUSCODE_GOOD) {
  1112. UA_LOG_NODEID_WRAP(&node->nodeId, UA_LOG_INFO_SESSION(&server->config.logger, session,
  1113. "AddNodes: Calling the node constructor(s) of %.*s failed "
  1114. "with status code %s", (int)nodeIdStr.length,
  1115. nodeIdStr.data, UA_StatusCode_name(retval)));
  1116. }
  1117. cleanup:
  1118. if(type)
  1119. UA_NODESTORE_RELEASE(server, type);
  1120. if(retval != UA_STATUSCODE_GOOD) {
  1121. recursiveDeconstructNode(server, session, 0, NULL, node);
  1122. recursiveDeleteNode(server, session, 0, NULL, node, true);
  1123. }
  1124. UA_NODESTORE_RELEASE(server, node);
  1125. return retval;
  1126. }
  1127. static void
  1128. Operation_addNode(UA_Server *server, UA_Session *session, void *nodeContext,
  1129. const UA_AddNodesItem *item, UA_AddNodesResult *result) {
  1130. result->statusCode =
  1131. Operation_addNode_begin(server, session, nodeContext, item, &item->parentNodeId.nodeId,
  1132. &item->referenceTypeId, &result->addedNodeId);
  1133. if(result->statusCode != UA_STATUSCODE_GOOD)
  1134. return;
  1135. /* AddNodes_finish */
  1136. result->statusCode = AddNode_finish(server, session, &result->addedNodeId);
  1137. /* If finishing failed, the node was deleted */
  1138. if(result->statusCode != UA_STATUSCODE_GOOD)
  1139. UA_NodeId_clear(&result->addedNodeId);
  1140. }
  1141. void
  1142. Service_AddNodes(UA_Server *server, UA_Session *session,
  1143. const UA_AddNodesRequest *request,
  1144. UA_AddNodesResponse *response) {
  1145. UA_LOG_DEBUG_SESSION(&server->config.logger, session, "Processing AddNodesRequest");
  1146. UA_LOCK_ASSERT(server->serviceMutex, 1);
  1147. if(server->config.maxNodesPerNodeManagement != 0 &&
  1148. request->nodesToAddSize > server->config.maxNodesPerNodeManagement) {
  1149. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  1150. return;
  1151. }
  1152. response->responseHeader.serviceResult =
  1153. UA_Server_processServiceOperations(server, session,
  1154. (UA_ServiceOperation)Operation_addNode, NULL,
  1155. &request->nodesToAddSize, &UA_TYPES[UA_TYPES_ADDNODESITEM],
  1156. &response->resultsSize, &UA_TYPES[UA_TYPES_ADDNODESRESULT]);
  1157. }
  1158. UA_StatusCode
  1159. addNode(UA_Server *server, const UA_NodeClass nodeClass,
  1160. const UA_NodeId *requestedNewNodeId,
  1161. const UA_NodeId *parentNodeId,
  1162. const UA_NodeId *referenceTypeId,
  1163. const UA_QualifiedName browseName,
  1164. const UA_NodeId *typeDefinition,
  1165. const UA_NodeAttributes *attr,
  1166. const UA_DataType *attributeType,
  1167. void *nodeContext, UA_NodeId *outNewNodeId) {
  1168. UA_LOCK_ASSERT(server->serviceMutex, 1);
  1169. /* Create the AddNodesItem */
  1170. UA_AddNodesItem item;
  1171. UA_AddNodesItem_init(&item);
  1172. item.nodeClass = nodeClass;
  1173. item.requestedNewNodeId.nodeId = *requestedNewNodeId;
  1174. item.browseName = browseName;
  1175. item.parentNodeId.nodeId = *parentNodeId;
  1176. item.referenceTypeId = *referenceTypeId;
  1177. item.typeDefinition.nodeId = *typeDefinition;
  1178. item.nodeAttributes.encoding = UA_EXTENSIONOBJECT_DECODED_NODELETE;
  1179. item.nodeAttributes.content.decoded.type = attributeType;
  1180. item.nodeAttributes.content.decoded.data = (void*)(uintptr_t)attr;
  1181. /* Call the normal addnodes service */
  1182. UA_AddNodesResult result;
  1183. UA_AddNodesResult_init(&result);
  1184. Operation_addNode(server, &server->adminSession, nodeContext, &item, &result);
  1185. if(outNewNodeId)
  1186. *outNewNodeId = result.addedNodeId;
  1187. else
  1188. UA_NodeId_clear(&result.addedNodeId);
  1189. return result.statusCode;
  1190. }
  1191. UA_StatusCode
  1192. __UA_Server_addNode(UA_Server *server, const UA_NodeClass nodeClass,
  1193. const UA_NodeId *requestedNewNodeId,
  1194. const UA_NodeId *parentNodeId,
  1195. const UA_NodeId *referenceTypeId,
  1196. const UA_QualifiedName browseName,
  1197. const UA_NodeId *typeDefinition,
  1198. const UA_NodeAttributes *attr,
  1199. const UA_DataType *attributeType,
  1200. void *nodeContext, UA_NodeId *outNewNodeId) {
  1201. UA_LOCK(server->serviceMutex)
  1202. UA_StatusCode reval = addNode(server, nodeClass, requestedNewNodeId, parentNodeId,
  1203. referenceTypeId, browseName, typeDefinition, attr, attributeType, nodeContext, outNewNodeId);
  1204. UA_UNLOCK(server->serviceMutex);
  1205. return reval;
  1206. }
  1207. UA_StatusCode
  1208. UA_Server_addNode_begin(UA_Server *server, const UA_NodeClass nodeClass,
  1209. const UA_NodeId requestedNewNodeId,
  1210. const UA_NodeId parentNodeId,
  1211. const UA_NodeId referenceTypeId,
  1212. const UA_QualifiedName browseName,
  1213. const UA_NodeId typeDefinition,
  1214. const void *attr, const UA_DataType *attributeType,
  1215. void *nodeContext, UA_NodeId *outNewNodeId) {
  1216. UA_AddNodesItem item;
  1217. UA_AddNodesItem_init(&item);
  1218. item.nodeClass = nodeClass;
  1219. item.requestedNewNodeId.nodeId = requestedNewNodeId;
  1220. item.browseName = browseName;
  1221. item.typeDefinition.nodeId = typeDefinition;
  1222. item.nodeAttributes.encoding = UA_EXTENSIONOBJECT_DECODED_NODELETE;
  1223. item.nodeAttributes.content.decoded.type = attributeType;
  1224. item.nodeAttributes.content.decoded.data = (void*)(uintptr_t)attr;
  1225. UA_LOCK(server->serviceMutex);
  1226. UA_StatusCode retval = Operation_addNode_begin(server, &server->adminSession, nodeContext, &item,
  1227. &parentNodeId, &referenceTypeId, outNewNodeId);
  1228. UA_UNLOCK(server->serviceMutex);
  1229. return retval;
  1230. }
  1231. UA_StatusCode
  1232. UA_Server_addNode_finish(UA_Server *server, const UA_NodeId nodeId) {
  1233. UA_LOCK(server->serviceMutex);
  1234. UA_StatusCode retval = AddNode_finish(server, &server->adminSession, &nodeId);
  1235. UA_UNLOCK(server->serviceMutex);
  1236. return retval;
  1237. }
  1238. /****************/
  1239. /* Delete Nodes */
  1240. /****************/
  1241. static void
  1242. Operation_deleteReference(UA_Server *server, UA_Session *session, void *context,
  1243. const UA_DeleteReferencesItem *item, UA_StatusCode *retval);
  1244. /* Remove references to this node (in the other nodes) */
  1245. static void
  1246. removeIncomingReferences(UA_Server *server, UA_Session *session,
  1247. const UA_Node *node) {
  1248. UA_DeleteReferencesItem item;
  1249. UA_DeleteReferencesItem_init(&item);
  1250. item.targetNodeId.nodeId = node->nodeId;
  1251. item.deleteBidirectional = false;
  1252. UA_StatusCode dummy;
  1253. for(size_t i = 0; i < node->referencesSize; ++i) {
  1254. UA_NodeReferenceKind *refs = &node->references[i];
  1255. item.isForward = refs->isInverse;
  1256. item.referenceTypeId = refs->referenceTypeId;
  1257. for(size_t j = 0; j < refs->refTargetsSize; ++j) {
  1258. item.sourceNodeId = refs->refTargets[j].target.nodeId;
  1259. Operation_deleteReference(server, session, NULL, &item, &dummy);
  1260. }
  1261. }
  1262. }
  1263. /* A node can only be deleted if it has at most one incoming hierarchical
  1264. * reference. If hierarchicalReferences is NULL, always remove. */
  1265. static UA_Boolean
  1266. multipleHierarchies(size_t hierarchicalRefsSize, UA_ExpandedNodeId *hierarchicalRefs,
  1267. const UA_Node *node) {
  1268. if(!hierarchicalRefs)
  1269. return false;
  1270. size_t incomingRefs = 0;
  1271. for(size_t i = 0; i < node->referencesSize; i++) {
  1272. const UA_NodeReferenceKind *k = &node->references[i];
  1273. if(!k->isInverse)
  1274. continue;
  1275. UA_Boolean hierarchical = false;
  1276. for(size_t j = 0; j < hierarchicalRefsSize; j++) {
  1277. if(UA_NodeId_equal(&hierarchicalRefs[j].nodeId,
  1278. &k->referenceTypeId)) {
  1279. hierarchical = true;
  1280. break;
  1281. }
  1282. }
  1283. if(!hierarchical)
  1284. continue;
  1285. incomingRefs += k->refTargetsSize;
  1286. if(incomingRefs > 1)
  1287. return true;
  1288. }
  1289. return false;
  1290. }
  1291. /* Recursively call the destructors of this node and all child nodes.
  1292. * Deconstructs the parent before its children. */
  1293. static void
  1294. recursiveDeconstructNode(UA_Server *server, UA_Session *session,
  1295. size_t hierarchicalRefsSize,
  1296. UA_ExpandedNodeId *hierarchicalRefs,
  1297. const UA_Node *node) {
  1298. /* Was the constructor called for the node? */
  1299. if(!node->constructed)
  1300. return;
  1301. /* Call the type-level destructor */
  1302. void *context = node->context; /* No longer needed after this function */
  1303. if(node->nodeClass == UA_NODECLASS_OBJECT ||
  1304. node->nodeClass == UA_NODECLASS_VARIABLE) {
  1305. const UA_Node *type = getNodeType(server, node);
  1306. if(type) {
  1307. const UA_NodeTypeLifecycle *lifecycle;
  1308. if(node->nodeClass == UA_NODECLASS_OBJECT)
  1309. lifecycle = &((const UA_ObjectTypeNode*)type)->lifecycle;
  1310. else
  1311. lifecycle = &((const UA_VariableTypeNode*)type)->lifecycle;
  1312. if(lifecycle->destructor) {
  1313. UA_UNLOCK(server->serviceMutex);
  1314. lifecycle->destructor(server,
  1315. &session->sessionId, session->sessionHandle,
  1316. &type->nodeId, type->context,
  1317. &node->nodeId, &context);
  1318. UA_LOCK(server->serviceMutex);
  1319. }
  1320. UA_NODESTORE_RELEASE(server, type);
  1321. }
  1322. }
  1323. /* Call the global destructor */
  1324. if(server->config.nodeLifecycle.destructor) {
  1325. UA_UNLOCK(server->serviceMutex);
  1326. server->config.nodeLifecycle.destructor(server, &session->sessionId,
  1327. session->sessionHandle,
  1328. &node->nodeId, context);
  1329. UA_LOCK(server->serviceMutex);
  1330. }
  1331. /* Set the constructed flag to false */
  1332. UA_Server_editNode(server, &server->adminSession, &node->nodeId,
  1333. (UA_EditNodeCallback)setDeconstructedNode, context);
  1334. /* Browse to get all children of the node */
  1335. UA_BrowseDescription bd;
  1336. UA_BrowseDescription_init(&bd);
  1337. bd.nodeId = node->nodeId;
  1338. bd.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_AGGREGATES);
  1339. bd.includeSubtypes = true;
  1340. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  1341. UA_BrowseResult br;
  1342. UA_BrowseResult_init(&br);
  1343. UA_UInt32 maxrefs = 0;
  1344. Operation_Browse(server, session, &maxrefs, &bd, &br);
  1345. if(br.statusCode != UA_STATUSCODE_GOOD)
  1346. return;
  1347. /* Deconstruct every child node */
  1348. for(size_t i = 0; i < br.referencesSize; ++i) {
  1349. UA_ReferenceDescription *rd = &br.references[i];
  1350. const UA_Node *child = UA_NODESTORE_GET(server, &rd->nodeId.nodeId);
  1351. if(!child)
  1352. continue;
  1353. /* Only delete child nodes that have no other parent */
  1354. if(!multipleHierarchies(hierarchicalRefsSize, hierarchicalRefs, child))
  1355. recursiveDeconstructNode(server, session, hierarchicalRefsSize,
  1356. hierarchicalRefs, child);
  1357. UA_NODESTORE_RELEASE(server, child);
  1358. }
  1359. UA_BrowseResult_clear(&br);
  1360. }
  1361. static void
  1362. recursiveDeleteNode(UA_Server *server, UA_Session *session,
  1363. size_t hierarchicalRefsSize,
  1364. UA_ExpandedNodeId *hierarchicalRefs,
  1365. const UA_Node *node, UA_Boolean removeTargetRefs) {
  1366. /* Browse to get all children of the node */
  1367. UA_BrowseDescription bd;
  1368. UA_BrowseDescription_init(&bd);
  1369. bd.nodeId = node->nodeId;
  1370. bd.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_AGGREGATES);
  1371. bd.includeSubtypes = true;
  1372. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  1373. UA_BrowseResult br;
  1374. UA_BrowseResult_init(&br);
  1375. UA_UInt32 maxrefs = 0;
  1376. Operation_Browse(server, session, &maxrefs, &bd, &br);
  1377. if(br.statusCode != UA_STATUSCODE_GOOD)
  1378. return;
  1379. /* Remove every child */
  1380. for(size_t i = 0; i < br.referencesSize; ++i) {
  1381. UA_ReferenceDescription *rd = &br.references[i];
  1382. /* Check for self-reference to avoid endless loop */
  1383. if(UA_NodeId_equal(&node->nodeId, &rd->nodeId.nodeId))
  1384. continue;
  1385. const UA_Node *child = UA_NODESTORE_GET(server, &rd->nodeId.nodeId);
  1386. if(!child)
  1387. continue;
  1388. /* Only delete child nodes that have no other parent */
  1389. if(!multipleHierarchies(hierarchicalRefsSize, hierarchicalRefs, child))
  1390. recursiveDeleteNode(server, session, hierarchicalRefsSize,
  1391. hierarchicalRefs, child, true);
  1392. UA_NODESTORE_RELEASE(server, child);
  1393. }
  1394. UA_BrowseResult_clear(&br);
  1395. if(removeTargetRefs)
  1396. removeIncomingReferences(server, session, node);
  1397. UA_NODESTORE_REMOVE(server, &node->nodeId);
  1398. }
  1399. static void
  1400. deleteNodeOperation(UA_Server *server, UA_Session *session, void *context,
  1401. const UA_DeleteNodesItem *item, UA_StatusCode *result) {
  1402. /* Do not check access for server */
  1403. if(session != &server->adminSession && server->config.accessControl.allowDeleteNode) {
  1404. UA_UNLOCK(server->serviceMutex);
  1405. if ( !server->config.accessControl.allowDeleteNode(server, &server->config.accessControl,
  1406. &session->sessionId, session->sessionHandle, item)) {
  1407. UA_LOCK(server->serviceMutex);
  1408. *result = UA_STATUSCODE_BADUSERACCESSDENIED;
  1409. return;
  1410. }
  1411. UA_LOCK(server->serviceMutex);
  1412. }
  1413. const UA_Node *node = UA_NODESTORE_GET(server, &item->nodeId);
  1414. if(!node) {
  1415. *result = UA_STATUSCODE_BADNODEIDUNKNOWN;
  1416. return;
  1417. }
  1418. if(UA_Node_hasSubTypeOrInstances(node)) {
  1419. UA_LOG_INFO_SESSION(&server->config.logger, session,
  1420. "Delete Nodes: Cannot delete a type node "
  1421. "with active instances or subtypes");
  1422. UA_NODESTORE_RELEASE(server, node);
  1423. *result = UA_STATUSCODE_BADINTERNALERROR;
  1424. return;
  1425. }
  1426. /* TODO: Check if the information model consistency is violated */
  1427. /* TODO: Check if the node is a mandatory child of a parent */
  1428. /* A node can be referenced with hierarchical references from several
  1429. * parents in the information model. (But not in a circular way.) The
  1430. * hierarchical references are checked to see if a node can be deleted.
  1431. * Getting the type hierarchy can fail in case of low RAM. In that case the
  1432. * nodes are always deleted. */
  1433. UA_ExpandedNodeId *hierarchicalRefs = NULL;
  1434. size_t hierarchicalRefsSize = 0;
  1435. UA_NodeId hr = UA_NODEID_NUMERIC(0, UA_NS0ID_HIERARCHICALREFERENCES);
  1436. browseRecursive(server, 1, &hr, 1, &subtypeId, UA_BROWSEDIRECTION_FORWARD, true,
  1437. &hierarchicalRefsSize, &hierarchicalRefs);
  1438. if(!hierarchicalRefs) {
  1439. UA_LOG_WARNING_SESSION(&server->config.logger, session,
  1440. "Delete Nodes: Cannot test for hierarchical "
  1441. "references. Deleting the node and all child nodes.");
  1442. }
  1443. recursiveDeconstructNode(server, session, hierarchicalRefsSize, hierarchicalRefs, node);
  1444. recursiveDeleteNode(server, session, hierarchicalRefsSize, hierarchicalRefs, node,
  1445. item->deleteTargetReferences);
  1446. UA_Array_delete(hierarchicalRefs, hierarchicalRefsSize, &UA_TYPES[UA_TYPES_EXPANDEDNODEID]);
  1447. UA_NODESTORE_RELEASE(server, node);
  1448. }
  1449. void Service_DeleteNodes(UA_Server *server, UA_Session *session,
  1450. const UA_DeleteNodesRequest *request,
  1451. UA_DeleteNodesResponse *response) {
  1452. UA_LOG_DEBUG_SESSION(&server->config.logger, session,
  1453. "Processing DeleteNodesRequest");
  1454. UA_LOCK_ASSERT(server->serviceMutex, 1);
  1455. if(server->config.maxNodesPerNodeManagement != 0 &&
  1456. request->nodesToDeleteSize > server->config.maxNodesPerNodeManagement) {
  1457. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  1458. return;
  1459. }
  1460. response->responseHeader.serviceResult =
  1461. UA_Server_processServiceOperations(server, session,
  1462. (UA_ServiceOperation)deleteNodeOperation,
  1463. NULL, &request->nodesToDeleteSize,
  1464. &UA_TYPES[UA_TYPES_DELETENODESITEM],
  1465. &response->resultsSize, &UA_TYPES[UA_TYPES_STATUSCODE]);
  1466. }
  1467. UA_StatusCode
  1468. UA_Server_deleteNode(UA_Server *server, const UA_NodeId nodeId,
  1469. UA_Boolean deleteReferences) {
  1470. UA_LOCK(server->serviceMutex)
  1471. UA_StatusCode retval = deleteNode(server, nodeId, deleteReferences);
  1472. UA_UNLOCK(server->serviceMutex);
  1473. return retval;
  1474. }
  1475. UA_StatusCode
  1476. deleteNode(UA_Server *server, const UA_NodeId nodeId,
  1477. UA_Boolean deleteReferences) {
  1478. UA_LOCK_ASSERT(server->serviceMutex, 1);
  1479. UA_DeleteNodesItem item;
  1480. item.deleteTargetReferences = deleteReferences;
  1481. item.nodeId = nodeId;
  1482. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1483. deleteNodeOperation(server, &server->adminSession, NULL, &item, &retval);
  1484. return retval;
  1485. }
  1486. /******************/
  1487. /* Add References */
  1488. /******************/
  1489. static UA_StatusCode
  1490. addOneWayReference(UA_Server *server, UA_Session *session,
  1491. UA_Node *node, const UA_AddReferencesItem *item) {
  1492. return UA_Node_addReference(node, item);
  1493. }
  1494. static UA_StatusCode
  1495. deleteOneWayReference(UA_Server *server, UA_Session *session, UA_Node *node,
  1496. const UA_DeleteReferencesItem *item) {
  1497. return UA_Node_deleteReference(node, item);
  1498. }
  1499. static void
  1500. Operation_addReference(UA_Server *server, UA_Session *session, void *context,
  1501. const UA_AddReferencesItem *item, UA_StatusCode *retval) {
  1502. /* Do not check access for server */
  1503. if(session != &server->adminSession && server->config.accessControl.allowAddReference) {
  1504. UA_UNLOCK(server->serviceMutex);
  1505. if (!server->config.accessControl.
  1506. allowAddReference(server, &server->config.accessControl,
  1507. &session->sessionId, session->sessionHandle, item)) {
  1508. UA_LOCK(server->serviceMutex);
  1509. *retval = UA_STATUSCODE_BADUSERACCESSDENIED;
  1510. return;
  1511. }
  1512. UA_LOCK(server->serviceMutex);
  1513. }
  1514. /* Currently no expandednodeids are allowed */
  1515. if(item->targetServerUri.length > 0) {
  1516. *retval = UA_STATUSCODE_BADNOTIMPLEMENTED;
  1517. return;
  1518. }
  1519. /* Add the first direction */
  1520. *retval = UA_Server_editNode(server, session, &item->sourceNodeId,
  1521. (UA_EditNodeCallback)addOneWayReference,
  1522. /* cast away const because callback uses const anyway */
  1523. (UA_AddReferencesItem *)(uintptr_t)item);
  1524. UA_Boolean firstExisted = false;
  1525. if(*retval == UA_STATUSCODE_BADDUPLICATEREFERENCENOTALLOWED) {
  1526. *retval = UA_STATUSCODE_GOOD;
  1527. firstExisted = true;
  1528. } else if(*retval != UA_STATUSCODE_GOOD)
  1529. return;
  1530. /* Add the second direction */
  1531. UA_AddReferencesItem secondItem;
  1532. UA_AddReferencesItem_init(&secondItem);
  1533. secondItem.sourceNodeId = item->targetNodeId.nodeId;
  1534. secondItem.referenceTypeId = item->referenceTypeId;
  1535. secondItem.isForward = !item->isForward;
  1536. secondItem.targetNodeId.nodeId = item->sourceNodeId;
  1537. /* keep default secondItem.targetNodeClass = UA_NODECLASS_UNSPECIFIED */
  1538. *retval = UA_Server_editNode(server, session, &secondItem.sourceNodeId,
  1539. (UA_EditNodeCallback)addOneWayReference, &secondItem);
  1540. /* remove reference if the second direction failed */
  1541. UA_Boolean secondExisted = false;
  1542. if(*retval == UA_STATUSCODE_BADDUPLICATEREFERENCENOTALLOWED) {
  1543. *retval = UA_STATUSCODE_GOOD;
  1544. secondExisted = true;
  1545. } else if(*retval != UA_STATUSCODE_GOOD && !firstExisted) {
  1546. UA_DeleteReferencesItem deleteItem;
  1547. deleteItem.sourceNodeId = item->sourceNodeId;
  1548. deleteItem.referenceTypeId = item->referenceTypeId;
  1549. deleteItem.isForward = item->isForward;
  1550. deleteItem.targetNodeId = item->targetNodeId;
  1551. deleteItem.deleteBidirectional = false;
  1552. /* ignore returned status code */
  1553. UA_Server_editNode(server, session, &item->sourceNodeId,
  1554. (UA_EditNodeCallback)deleteOneWayReference, &deleteItem);
  1555. }
  1556. /* Calculate common duplicate reference not allowed result and set bad result
  1557. * if BOTH directions already existed */
  1558. if(firstExisted && secondExisted)
  1559. *retval = UA_STATUSCODE_BADDUPLICATEREFERENCENOTALLOWED;
  1560. }
  1561. void Service_AddReferences(UA_Server *server, UA_Session *session,
  1562. const UA_AddReferencesRequest *request,
  1563. UA_AddReferencesResponse *response) {
  1564. UA_LOG_DEBUG_SESSION(&server->config.logger, session,
  1565. "Processing AddReferencesRequest");
  1566. UA_LOCK_ASSERT(server->serviceMutex, 1);
  1567. if(server->config.maxNodesPerNodeManagement != 0 &&
  1568. request->referencesToAddSize > server->config.maxNodesPerNodeManagement) {
  1569. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  1570. return;
  1571. }
  1572. response->responseHeader.serviceResult =
  1573. UA_Server_processServiceOperations(server, session,
  1574. (UA_ServiceOperation)Operation_addReference,
  1575. NULL, &request->referencesToAddSize,
  1576. &UA_TYPES[UA_TYPES_ADDREFERENCESITEM],
  1577. &response->resultsSize, &UA_TYPES[UA_TYPES_STATUSCODE]);
  1578. }
  1579. UA_StatusCode
  1580. UA_Server_addReference(UA_Server *server, const UA_NodeId sourceId,
  1581. const UA_NodeId refTypeId,
  1582. const UA_ExpandedNodeId targetId,
  1583. UA_Boolean isForward) {
  1584. UA_AddReferencesItem item;
  1585. UA_AddReferencesItem_init(&item);
  1586. item.sourceNodeId = sourceId;
  1587. item.referenceTypeId = refTypeId;
  1588. item.isForward = isForward;
  1589. item.targetNodeId = targetId;
  1590. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1591. UA_LOCK(server->serviceMutex);
  1592. Operation_addReference(server, &server->adminSession, NULL, &item, &retval);
  1593. UA_UNLOCK(server->serviceMutex)
  1594. return retval;
  1595. }
  1596. /*********************/
  1597. /* Delete References */
  1598. /*********************/
  1599. static void
  1600. Operation_deleteReference(UA_Server *server, UA_Session *session, void *context,
  1601. const UA_DeleteReferencesItem *item, UA_StatusCode *retval) {
  1602. /* Do not check access for server */
  1603. if(session != &server->adminSession && server->config.accessControl.allowDeleteReference) {
  1604. UA_UNLOCK(server->serviceMutex);
  1605. if (!server->config.accessControl.
  1606. allowDeleteReference(server, &server->config.accessControl,
  1607. &session->sessionId, session->sessionHandle, item)){
  1608. UA_LOCK(server->serviceMutex);
  1609. *retval = UA_STATUSCODE_BADUSERACCESSDENIED;
  1610. return;
  1611. }
  1612. UA_LOCK(server->serviceMutex)
  1613. }
  1614. // TODO: Check consistency constraints, remove the references.
  1615. *retval = UA_Server_editNode(server, session, &item->sourceNodeId,
  1616. (UA_EditNodeCallback)deleteOneWayReference,
  1617. /* cast away const qualifier because callback uses it anyway */
  1618. (UA_DeleteReferencesItem *)(uintptr_t)item);
  1619. if(*retval != UA_STATUSCODE_GOOD)
  1620. return;
  1621. if(!item->deleteBidirectional || item->targetNodeId.serverIndex != 0)
  1622. return;
  1623. UA_DeleteReferencesItem secondItem;
  1624. UA_DeleteReferencesItem_init(&secondItem);
  1625. secondItem.isForward = !item->isForward;
  1626. secondItem.sourceNodeId = item->targetNodeId.nodeId;
  1627. secondItem.targetNodeId.nodeId = item->sourceNodeId;
  1628. secondItem.referenceTypeId = item->referenceTypeId;
  1629. *retval = UA_Server_editNode(server, session, &secondItem.sourceNodeId,
  1630. (UA_EditNodeCallback)deleteOneWayReference,
  1631. &secondItem);
  1632. }
  1633. void
  1634. Service_DeleteReferences(UA_Server *server, UA_Session *session,
  1635. const UA_DeleteReferencesRequest *request,
  1636. UA_DeleteReferencesResponse *response) {
  1637. UA_LOG_DEBUG_SESSION(&server->config.logger, session,
  1638. "Processing DeleteReferencesRequest");
  1639. UA_LOCK_ASSERT(server->serviceMutex, 1);
  1640. if(server->config.maxNodesPerNodeManagement != 0 &&
  1641. request->referencesToDeleteSize > server->config.maxNodesPerNodeManagement) {
  1642. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  1643. return;
  1644. }
  1645. response->responseHeader.serviceResult =
  1646. UA_Server_processServiceOperations(server, session,
  1647. (UA_ServiceOperation)Operation_deleteReference,
  1648. NULL, &request->referencesToDeleteSize,
  1649. &UA_TYPES[UA_TYPES_DELETEREFERENCESITEM],
  1650. &response->resultsSize, &UA_TYPES[UA_TYPES_STATUSCODE]);
  1651. }
  1652. UA_StatusCode
  1653. UA_Server_deleteReference(UA_Server *server, const UA_NodeId sourceNodeId,
  1654. const UA_NodeId referenceTypeId, UA_Boolean isForward,
  1655. const UA_ExpandedNodeId targetNodeId,
  1656. UA_Boolean deleteBidirectional) {
  1657. UA_DeleteReferencesItem item;
  1658. item.sourceNodeId = sourceNodeId;
  1659. item.referenceTypeId = referenceTypeId;
  1660. item.isForward = isForward;
  1661. item.targetNodeId = targetNodeId;
  1662. item.deleteBidirectional = deleteBidirectional;
  1663. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1664. UA_LOCK(server->serviceMutex);
  1665. Operation_deleteReference(server, &server->adminSession, NULL, &item, &retval);
  1666. UA_UNLOCK(server->serviceMutex);
  1667. return retval;
  1668. }
  1669. /**********************/
  1670. /* Set Value Callback */
  1671. /**********************/
  1672. static UA_StatusCode
  1673. setValueCallback(UA_Server *server, UA_Session *session,
  1674. UA_VariableNode *node, const UA_ValueCallback *callback) {
  1675. if(node->nodeClass != UA_NODECLASS_VARIABLE)
  1676. return UA_STATUSCODE_BADNODECLASSINVALID;
  1677. node->value.data.callback = *callback;
  1678. return UA_STATUSCODE_GOOD;
  1679. }
  1680. UA_StatusCode
  1681. UA_Server_setVariableNode_valueCallback(UA_Server *server,
  1682. const UA_NodeId nodeId,
  1683. const UA_ValueCallback callback) {
  1684. UA_LOCK(server->serviceMutex);
  1685. UA_StatusCode retval = UA_Server_editNode(server, &server->adminSession, &nodeId,
  1686. (UA_EditNodeCallback)setValueCallback,
  1687. /* cast away const because callback uses const anyway */
  1688. (UA_ValueCallback *)(uintptr_t) &callback);
  1689. UA_UNLOCK(server->serviceMutex);
  1690. return retval;
  1691. }
  1692. /***************************************************/
  1693. /* Special Handling of Variables with Data Sources */
  1694. /***************************************************/
  1695. UA_StatusCode
  1696. UA_Server_addDataSourceVariableNode(UA_Server *server, const UA_NodeId requestedNewNodeId,
  1697. const UA_NodeId parentNodeId, const UA_NodeId referenceTypeId,
  1698. const UA_QualifiedName browseName, const UA_NodeId typeDefinition,
  1699. const UA_VariableAttributes attr, const UA_DataSource dataSource,
  1700. void *nodeContext, UA_NodeId *outNewNodeId) {
  1701. UA_AddNodesItem item;
  1702. UA_AddNodesItem_init(&item);
  1703. item.nodeClass = UA_NODECLASS_VARIABLE;
  1704. item.requestedNewNodeId.nodeId = requestedNewNodeId;
  1705. item.browseName = browseName;
  1706. UA_ExpandedNodeId typeDefinitionId;
  1707. UA_ExpandedNodeId_init(&typeDefinitionId);
  1708. typeDefinitionId.nodeId = typeDefinition;
  1709. item.typeDefinition = typeDefinitionId;
  1710. item.nodeAttributes.encoding = UA_EXTENSIONOBJECT_DECODED_NODELETE;
  1711. item.nodeAttributes.content.decoded.data = (void*)(uintptr_t)&attr;
  1712. item.nodeAttributes.content.decoded.type = &UA_TYPES[UA_TYPES_VARIABLEATTRIBUTES];
  1713. UA_NodeId newNodeId;
  1714. if(!outNewNodeId) {
  1715. newNodeId = UA_NODEID_NULL;
  1716. outNewNodeId = &newNodeId;
  1717. }
  1718. UA_LOCK(server->serviceMutex);
  1719. /* Create the node and add it to the nodestore */
  1720. UA_StatusCode retval = AddNode_raw(server, &server->adminSession, nodeContext,
  1721. &item, outNewNodeId);
  1722. if(retval != UA_STATUSCODE_GOOD)
  1723. goto cleanup;
  1724. /* Set the data source */
  1725. retval = setVariableNode_dataSource(server, *outNewNodeId, dataSource);
  1726. if(retval != UA_STATUSCODE_GOOD)
  1727. goto cleanup;
  1728. /* Typecheck and add references to parent and type definition */
  1729. retval = AddNode_addRefs(server, &server->adminSession, outNewNodeId, &parentNodeId,
  1730. &referenceTypeId, &typeDefinition);
  1731. if(retval != UA_STATUSCODE_GOOD)
  1732. goto cleanup;
  1733. /* Call the constructors */
  1734. retval = AddNode_finish(server, &server->adminSession, outNewNodeId);
  1735. cleanup:
  1736. UA_UNLOCK(server->serviceMutex);
  1737. if(outNewNodeId == &newNodeId)
  1738. UA_NodeId_clear(&newNodeId);
  1739. return retval;
  1740. }
  1741. static UA_StatusCode
  1742. setDataSource(UA_Server *server, UA_Session *session,
  1743. UA_VariableNode* node, const UA_DataSource *dataSource) {
  1744. if(node->nodeClass != UA_NODECLASS_VARIABLE)
  1745. return UA_STATUSCODE_BADNODECLASSINVALID;
  1746. if(node->valueSource == UA_VALUESOURCE_DATA)
  1747. UA_DataValue_clear(&node->value.data.value);
  1748. node->value.dataSource = *dataSource;
  1749. node->valueSource = UA_VALUESOURCE_DATASOURCE;
  1750. return UA_STATUSCODE_GOOD;
  1751. }
  1752. UA_StatusCode
  1753. setVariableNode_dataSource(UA_Server *server, const UA_NodeId nodeId,
  1754. const UA_DataSource dataSource) {
  1755. UA_LOCK_ASSERT(server->serviceMutex, 1);
  1756. return UA_Server_editNode(server, &server->adminSession, &nodeId,
  1757. (UA_EditNodeCallback)setDataSource,
  1758. /* casting away const because callback casts it back anyway */
  1759. (UA_DataSource *) (uintptr_t)&dataSource);
  1760. }
  1761. UA_StatusCode
  1762. UA_Server_setVariableNode_dataSource(UA_Server *server, const UA_NodeId nodeId,
  1763. const UA_DataSource dataSource) {
  1764. UA_LOCK(server->serviceMutex);
  1765. UA_StatusCode retval = setVariableNode_dataSource(server, nodeId, dataSource);
  1766. UA_UNLOCK(server->serviceMutex);
  1767. return retval;
  1768. }
  1769. /************************************/
  1770. /* Special Handling of Method Nodes */
  1771. /************************************/
  1772. #ifdef UA_ENABLE_METHODCALLS
  1773. static const UA_NodeId hasproperty = {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_HASPROPERTY}};
  1774. static const UA_NodeId propertytype = {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_PROPERTYTYPE}};
  1775. static UA_StatusCode
  1776. UA_Server_addMethodNodeEx_finish(UA_Server *server, const UA_NodeId nodeId, UA_MethodCallback method,
  1777. const size_t inputArgumentsSize, const UA_Argument *inputArguments,
  1778. const UA_NodeId inputArgumentsRequestedNewNodeId,
  1779. UA_NodeId *inputArgumentsOutNewNodeId,
  1780. const size_t outputArgumentsSize, const UA_Argument *outputArguments,
  1781. const UA_NodeId outputArgumentsRequestedNewNodeId,
  1782. UA_NodeId *outputArgumentsOutNewNodeId) {
  1783. /* Browse to see which argument nodes exist */
  1784. UA_BrowseDescription bd;
  1785. UA_BrowseDescription_init(&bd);
  1786. bd.nodeId = nodeId;
  1787. bd.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY);
  1788. bd.includeSubtypes = false;
  1789. bd.browseDirection = UA_BROWSEDIRECTION_FORWARD;
  1790. bd.nodeClassMask = UA_NODECLASS_VARIABLE;
  1791. bd.resultMask = UA_BROWSERESULTMASK_BROWSENAME;
  1792. UA_BrowseResult br;
  1793. UA_BrowseResult_init(&br);
  1794. UA_UInt32 maxrefs = 0;
  1795. Operation_Browse(server, &server->adminSession, &maxrefs, &bd, &br);
  1796. UA_StatusCode retval = br.statusCode;
  1797. if(retval != UA_STATUSCODE_GOOD) {
  1798. deleteNode(server, nodeId, true);
  1799. UA_BrowseResult_clear(&br);
  1800. return retval;
  1801. }
  1802. /* Filter out the argument nodes */
  1803. UA_NodeId inputArgsId = UA_NODEID_NULL;
  1804. UA_NodeId outputArgsId = UA_NODEID_NULL;
  1805. const UA_QualifiedName inputArgsName = UA_QUALIFIEDNAME(0, "InputArguments");
  1806. const UA_QualifiedName outputArgsName = UA_QUALIFIEDNAME(0, "OutputArguments");
  1807. for(size_t i = 0; i < br.referencesSize; i++) {
  1808. UA_ReferenceDescription *rd = &br.references[i];
  1809. if(rd->browseName.namespaceIndex == 0 &&
  1810. UA_String_equal(&rd->browseName.name, &inputArgsName.name))
  1811. inputArgsId = rd->nodeId.nodeId;
  1812. else if(rd->browseName.namespaceIndex == 0 &&
  1813. UA_String_equal(&rd->browseName.name, &outputArgsName.name))
  1814. outputArgsId = rd->nodeId.nodeId;
  1815. }
  1816. /* Add the Input Arguments VariableNode */
  1817. if(inputArgumentsSize > 0 && UA_NodeId_isNull(&inputArgsId)) {
  1818. UA_VariableAttributes attr = UA_VariableAttributes_default;
  1819. char *name = "InputArguments";
  1820. attr.displayName = UA_LOCALIZEDTEXT("", name);
  1821. attr.dataType = UA_TYPES[UA_TYPES_ARGUMENT].typeId;
  1822. attr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  1823. UA_UInt32 inputArgsSize32 = (UA_UInt32)inputArgumentsSize;
  1824. attr.arrayDimensions = &inputArgsSize32;
  1825. attr.arrayDimensionsSize = 1;
  1826. UA_Variant_setArray(&attr.value, (void *)(uintptr_t)inputArguments,
  1827. inputArgumentsSize, &UA_TYPES[UA_TYPES_ARGUMENT]);
  1828. retval = addNode(server, UA_NODECLASS_VARIABLE, &inputArgumentsRequestedNewNodeId,
  1829. &nodeId, &hasproperty, UA_QUALIFIEDNAME(0, name),
  1830. &propertytype, (const UA_NodeAttributes*)&attr,
  1831. &UA_TYPES[UA_TYPES_VARIABLEATTRIBUTES],
  1832. NULL, &inputArgsId);
  1833. if(retval != UA_STATUSCODE_GOOD)
  1834. goto error;
  1835. }
  1836. /* Add the Output Arguments VariableNode */
  1837. if(outputArgumentsSize > 0 && UA_NodeId_isNull(&outputArgsId)) {
  1838. UA_VariableAttributes attr = UA_VariableAttributes_default;
  1839. char *name = "OutputArguments";
  1840. attr.displayName = UA_LOCALIZEDTEXT("", name);
  1841. attr.dataType = UA_TYPES[UA_TYPES_ARGUMENT].typeId;
  1842. attr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  1843. UA_UInt32 outputArgsSize32 = (UA_UInt32)outputArgumentsSize;
  1844. attr.arrayDimensions = &outputArgsSize32;
  1845. attr.arrayDimensionsSize = 1;
  1846. UA_Variant_setArray(&attr.value, (void *)(uintptr_t)outputArguments,
  1847. outputArgumentsSize, &UA_TYPES[UA_TYPES_ARGUMENT]);
  1848. retval = addNode(server, UA_NODECLASS_VARIABLE, &outputArgumentsRequestedNewNodeId,
  1849. &nodeId, &hasproperty, UA_QUALIFIEDNAME(0, name),
  1850. &propertytype, (const UA_NodeAttributes*)&attr,
  1851. &UA_TYPES[UA_TYPES_VARIABLEATTRIBUTES],
  1852. NULL, &outputArgsId);
  1853. if(retval != UA_STATUSCODE_GOOD)
  1854. goto error;
  1855. }
  1856. retval = setMethodNode_callback(server, nodeId, method);
  1857. if(retval != UA_STATUSCODE_GOOD)
  1858. goto error;
  1859. /* Call finish to add the parent reference */
  1860. retval = AddNode_finish(server, &server->adminSession, &nodeId);
  1861. if(retval != UA_STATUSCODE_GOOD)
  1862. goto error;
  1863. if(inputArgumentsOutNewNodeId != NULL) {
  1864. UA_NodeId_copy(&inputArgsId, inputArgumentsOutNewNodeId);
  1865. }
  1866. if(outputArgumentsOutNewNodeId != NULL) {
  1867. UA_NodeId_copy(&outputArgsId, outputArgumentsOutNewNodeId);
  1868. }
  1869. UA_BrowseResult_clear(&br);
  1870. return retval;
  1871. error:
  1872. deleteNode(server, nodeId, true);
  1873. deleteNode(server, inputArgsId, true);
  1874. deleteNode(server, outputArgsId, true);
  1875. UA_BrowseResult_clear(&br);
  1876. return retval;
  1877. }
  1878. UA_StatusCode
  1879. UA_Server_addMethodNode_finish(UA_Server *server, const UA_NodeId nodeId,
  1880. UA_MethodCallback method,
  1881. size_t inputArgumentsSize, const UA_Argument* inputArguments,
  1882. size_t outputArgumentsSize, const UA_Argument* outputArguments) {
  1883. UA_LOCK(server->serviceMutex)
  1884. UA_StatusCode retval = UA_Server_addMethodNodeEx_finish(server, nodeId, method,
  1885. inputArgumentsSize, inputArguments, UA_NODEID_NULL, NULL,
  1886. outputArgumentsSize, outputArguments, UA_NODEID_NULL, NULL);
  1887. UA_UNLOCK(server->serviceMutex)
  1888. return retval;
  1889. }
  1890. UA_StatusCode
  1891. UA_Server_addMethodNodeEx(UA_Server *server, const UA_NodeId requestedNewNodeId,
  1892. const UA_NodeId parentNodeId,
  1893. const UA_NodeId referenceTypeId,
  1894. const UA_QualifiedName browseName,
  1895. const UA_MethodAttributes attr, UA_MethodCallback method,
  1896. size_t inputArgumentsSize, const UA_Argument *inputArguments,
  1897. const UA_NodeId inputArgumentsRequestedNewNodeId,
  1898. UA_NodeId *inputArgumentsOutNewNodeId,
  1899. size_t outputArgumentsSize, const UA_Argument *outputArguments,
  1900. const UA_NodeId outputArgumentsRequestedNewNodeId,
  1901. UA_NodeId *outputArgumentsOutNewNodeId,
  1902. void *nodeContext, UA_NodeId *outNewNodeId) {
  1903. UA_AddNodesItem item;
  1904. UA_AddNodesItem_init(&item);
  1905. item.nodeClass = UA_NODECLASS_METHOD;
  1906. item.requestedNewNodeId.nodeId = requestedNewNodeId;
  1907. item.browseName = browseName;
  1908. item.nodeAttributes.encoding = UA_EXTENSIONOBJECT_DECODED_NODELETE;
  1909. item.nodeAttributes.content.decoded.data = (void*)(uintptr_t)&attr;
  1910. item.nodeAttributes.content.decoded.type = &UA_TYPES[UA_TYPES_METHODATTRIBUTES];
  1911. UA_NodeId newId;
  1912. if(!outNewNodeId) {
  1913. UA_NodeId_init(&newId);
  1914. outNewNodeId = &newId;
  1915. }
  1916. UA_LOCK(server->serviceMutex);
  1917. UA_StatusCode retval = Operation_addNode_begin(server, &server->adminSession,
  1918. nodeContext, &item, &parentNodeId,
  1919. &referenceTypeId, outNewNodeId);
  1920. if(retval != UA_STATUSCODE_GOOD) {
  1921. UA_UNLOCK(server->serviceMutex);
  1922. return retval;
  1923. }
  1924. retval = UA_Server_addMethodNodeEx_finish(server, *outNewNodeId, method,
  1925. inputArgumentsSize, inputArguments,
  1926. inputArgumentsRequestedNewNodeId,
  1927. inputArgumentsOutNewNodeId,
  1928. outputArgumentsSize, outputArguments,
  1929. outputArgumentsRequestedNewNodeId,
  1930. outputArgumentsOutNewNodeId);
  1931. UA_UNLOCK(server->serviceMutex);
  1932. if(outNewNodeId == &newId)
  1933. UA_NodeId_clear(&newId);
  1934. return retval;
  1935. }
  1936. static UA_StatusCode
  1937. editMethodCallback(UA_Server *server, UA_Session* session,
  1938. UA_Node* node, void* handle) {
  1939. if(node->nodeClass != UA_NODECLASS_METHOD)
  1940. return UA_STATUSCODE_BADNODECLASSINVALID;
  1941. UA_MethodNode *mnode = (UA_MethodNode*) node;
  1942. mnode->method = (UA_MethodCallback)(uintptr_t)handle;
  1943. return UA_STATUSCODE_GOOD;
  1944. }
  1945. UA_StatusCode
  1946. setMethodNode_callback(UA_Server *server,
  1947. const UA_NodeId methodNodeId,
  1948. UA_MethodCallback methodCallback) {
  1949. UA_LOCK_ASSERT(server->serviceMutex, 1);
  1950. return UA_Server_editNode(server, &server->adminSession, &methodNodeId,
  1951. (UA_EditNodeCallback)editMethodCallback,
  1952. (void*)(uintptr_t)methodCallback);
  1953. }
  1954. UA_StatusCode
  1955. UA_Server_setMethodNode_callback(UA_Server *server,
  1956. const UA_NodeId methodNodeId,
  1957. UA_MethodCallback methodCallback) {
  1958. UA_LOCK(server->serviceMutex);
  1959. UA_StatusCode retVal = setMethodNode_callback(server, methodNodeId, methodCallback);
  1960. UA_UNLOCK(server->serviceMutex);
  1961. return retVal;
  1962. }
  1963. #endif
  1964. /************************/
  1965. /* Lifecycle Management */
  1966. /************************/
  1967. static UA_StatusCode
  1968. setNodeTypeLifecycle(UA_Server *server, UA_Session *session,
  1969. UA_Node* node, UA_NodeTypeLifecycle *lifecycle) {
  1970. if(node->nodeClass == UA_NODECLASS_OBJECTTYPE) {
  1971. UA_ObjectTypeNode *ot = (UA_ObjectTypeNode*)node;
  1972. ot->lifecycle = *lifecycle;
  1973. return UA_STATUSCODE_GOOD;
  1974. }
  1975. if(node->nodeClass == UA_NODECLASS_VARIABLETYPE) {
  1976. UA_VariableTypeNode *vt = (UA_VariableTypeNode*)node;
  1977. vt->lifecycle = *lifecycle;
  1978. return UA_STATUSCODE_GOOD;
  1979. }
  1980. return UA_STATUSCODE_BADNODECLASSINVALID;
  1981. }
  1982. UA_StatusCode
  1983. UA_Server_setNodeTypeLifecycle(UA_Server *server, UA_NodeId nodeId,
  1984. UA_NodeTypeLifecycle lifecycle) {
  1985. UA_LOCK(server->serviceMutex);
  1986. UA_StatusCode retval = UA_Server_editNode(server, &server->adminSession, &nodeId,
  1987. (UA_EditNodeCallback)setNodeTypeLifecycle,
  1988. &lifecycle);
  1989. UA_UNLOCK(server->serviceMutex);
  1990. return retval;
  1991. }