server_ctt.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  3. *
  4. * Copyright 2019 (c) Kalycito Infotech Private Limited
  5. * Copyright 2019 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. */
  7. #ifdef _MSC_VER
  8. #define _CRT_SECURE_NO_WARNINGS /* disable fopen deprication warning in msvs */
  9. #endif
  10. #include <open62541/plugin/log_stdout.h>
  11. #include <open62541/server.h>
  12. #include <open62541/server_config_default.h>
  13. #include <signal.h>
  14. #include <stdlib.h>
  15. #include "common.h"
  16. #define MAX_OPERATION_LIMIT 10000
  17. /* This server is configured to the Compliance Testing Tools (CTT) against. The
  18. * corresponding CTT configuration is available at
  19. * https://github.com/open62541/open62541-ctt */
  20. static const UA_NodeId baseDataVariableType = {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_BASEDATAVARIABLETYPE}};
  21. static const UA_NodeId accessDenied = {1, UA_NODEIDTYPE_NUMERIC, {1337}};
  22. /* Custom AccessControl policy that disallows access to one specific node */
  23. static UA_Byte
  24. getUserAccessLevel_disallowSpecific(UA_Server *server, UA_AccessControl *ac,
  25. const UA_NodeId *sessionId, void *sessionContext,
  26. const UA_NodeId *nodeId, void *nodeContext) {
  27. if(UA_NodeId_equal(nodeId, &accessDenied))
  28. return 0x00;
  29. return 0xFF;
  30. }
  31. /* Datasource Example */
  32. static UA_StatusCode
  33. readTimeData(UA_Server *server,
  34. const UA_NodeId *sessionId, void *sessionContext,
  35. const UA_NodeId *nodeId, void *nodeContext,
  36. UA_Boolean sourceTimeStamp,
  37. const UA_NumericRange *range, UA_DataValue *value) {
  38. if(range) {
  39. value->hasStatus = true;
  40. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  41. return UA_STATUSCODE_GOOD;
  42. }
  43. UA_DateTime currentTime = UA_DateTime_now();
  44. UA_Variant_setScalarCopy(&value->value, &currentTime, &UA_TYPES[UA_TYPES_DATETIME]);
  45. value->hasValue = true;
  46. if(sourceTimeStamp) {
  47. value->hasSourceTimestamp = true;
  48. value->sourceTimestamp = currentTime;
  49. }
  50. return UA_STATUSCODE_GOOD;
  51. }
  52. /* Method Node Example */
  53. #ifdef UA_ENABLE_METHODCALLS
  54. static UA_StatusCode
  55. helloWorld(UA_Server *server,
  56. const UA_NodeId *sessionId, void *sessionContext,
  57. const UA_NodeId *methodId, void *methodContext,
  58. const UA_NodeId *objectId, void *objectContext,
  59. size_t inputSize, const UA_Variant *input,
  60. size_t outputSize, UA_Variant *output) {
  61. /* input is a scalar string (checked by the server) */
  62. UA_String *name = (UA_String *)input[0].data;
  63. UA_String hello = UA_STRING("Hello ");
  64. UA_String greet;
  65. greet.length = hello.length + name->length;
  66. greet.data = (UA_Byte *)UA_malloc(greet.length);
  67. memcpy(greet.data, hello.data, hello.length);
  68. memcpy(greet.data + hello.length, name->data, name->length);
  69. UA_Variant_setScalarCopy(output, &greet, &UA_TYPES[UA_TYPES_STRING]);
  70. UA_String_clear(&greet);
  71. return UA_STATUSCODE_GOOD;
  72. }
  73. static UA_StatusCode
  74. noargMethod(UA_Server *server,
  75. const UA_NodeId *sessionId, void *sessionContext,
  76. const UA_NodeId *methodId, void *methodContext,
  77. const UA_NodeId *objectId, void *objectContext,
  78. size_t inputSize, const UA_Variant *input,
  79. size_t outputSize, UA_Variant *output) {
  80. return UA_STATUSCODE_GOOD;
  81. }
  82. static UA_StatusCode
  83. outargMethod(UA_Server *server,
  84. const UA_NodeId *sessionId, void *sessionContext,
  85. const UA_NodeId *methodId, void *methodContext,
  86. const UA_NodeId *objectId, void *objectContext,
  87. size_t inputSize, const UA_Variant *input,
  88. size_t outputSize, UA_Variant *output) {
  89. UA_Int32 out = 42;
  90. UA_Variant_setScalarCopy(output, &out, &UA_TYPES[UA_TYPES_INT32]);
  91. return UA_STATUSCODE_GOOD;
  92. }
  93. #endif
  94. static void
  95. setInformationModel(UA_Server *server) {
  96. /* add a static variable node to the server */
  97. UA_VariableAttributes myVar = UA_VariableAttributes_default;
  98. myVar.description = UA_LOCALIZEDTEXT("en-US", "the answer");
  99. myVar.displayName = UA_LOCALIZEDTEXT("en-US", "the answer");
  100. myVar.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  101. myVar.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  102. myVar.valueRank = UA_VALUERANK_SCALAR;
  103. UA_Int32 myInteger = 42;
  104. UA_Variant_setScalar(&myVar.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  105. const UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  106. const UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  107. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  108. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  109. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  110. myIntegerName, baseDataVariableType, myVar, NULL, NULL);
  111. /* add a static variable that is readable but not writable*/
  112. myVar = UA_VariableAttributes_default;
  113. myVar.description = UA_LOCALIZEDTEXT("en-US", "the answer - not readable");
  114. myVar.displayName = UA_LOCALIZEDTEXT("en-US", "the answer - not readable");
  115. myVar.accessLevel = UA_ACCESSLEVELMASK_WRITE;
  116. myVar.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  117. myVar.valueRank = UA_VALUERANK_SCALAR;
  118. UA_Variant_setScalar(&myVar.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  119. const UA_QualifiedName myInteger2Name = UA_QUALIFIEDNAME(1, "the answer - not readable");
  120. const UA_NodeId myInteger2NodeId = UA_NODEID_STRING(1, "the.answer.no.read");
  121. UA_Server_addVariableNode(server, myInteger2NodeId, parentNodeId, parentReferenceNodeId,
  122. myInteger2Name, baseDataVariableType, myVar, NULL, NULL);
  123. /* add a variable that is not readable or writable for the current user */
  124. myVar = UA_VariableAttributes_default;
  125. myVar.description = UA_LOCALIZEDTEXT("en-US", "the answer - not current user");
  126. myVar.displayName = UA_LOCALIZEDTEXT("en-US", "the answer - not current user");
  127. myVar.accessLevel = UA_ACCESSLEVELMASK_WRITE;
  128. myVar.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  129. myVar.valueRank = UA_VALUERANK_SCALAR;
  130. myVar.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  131. UA_Variant_setScalar(&myVar.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  132. const UA_QualifiedName accessDeniedName = UA_QUALIFIEDNAME(1, "the answer - not current user");
  133. UA_Server_addVariableNode(server, accessDenied, parentNodeId, parentReferenceNodeId,
  134. accessDeniedName, baseDataVariableType, myVar, NULL, NULL);
  135. /* add a variable with the datetime data source */
  136. UA_DataSource dateDataSource;
  137. dateDataSource.read = readTimeData;
  138. dateDataSource.write = NULL;
  139. UA_VariableAttributes v_attr = UA_VariableAttributes_default;
  140. v_attr.description = UA_LOCALIZEDTEXT("en-US", "current time");
  141. v_attr.displayName = UA_LOCALIZEDTEXT("en-US", "current time");
  142. v_attr.accessLevel = UA_ACCESSLEVELMASK_READ;
  143. v_attr.dataType = UA_TYPES[UA_TYPES_DATETIME].typeId;
  144. v_attr.valueRank = UA_VALUERANK_SCALAR;
  145. const UA_QualifiedName dateName = UA_QUALIFIEDNAME(1, "current time");
  146. UA_Server_addDataSourceVariableNode(server, UA_NODEID_NUMERIC(1, 2345),
  147. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  148. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), dateName,
  149. baseDataVariableType, v_attr, dateDataSource, NULL, NULL);
  150. /* add a bytestring variable with some content */
  151. myVar = UA_VariableAttributes_default;
  152. myVar.description = UA_LOCALIZEDTEXT("", "");
  153. myVar.displayName = UA_LOCALIZEDTEXT("", "example bytestring");
  154. myVar.dataType = UA_TYPES[UA_TYPES_BYTESTRING].typeId;
  155. myVar.valueRank = UA_VALUERANK_SCALAR;
  156. myVar.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  157. UA_ByteString myByteString = UA_BYTESTRING("test123\0test123");
  158. UA_Variant_setScalar(&myVar.value, &myByteString, &UA_TYPES[UA_TYPES_BYTESTRING]);
  159. const UA_QualifiedName byteStringName = UA_QUALIFIEDNAME(1, "example bytestring");
  160. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "myByteString"),
  161. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  162. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), byteStringName,
  163. baseDataVariableType, myVar, NULL, NULL);
  164. /* Add HelloWorld method to the server */
  165. #ifdef UA_ENABLE_METHODCALLS
  166. /* Method with IO Arguments */
  167. UA_Argument inputArguments;
  168. UA_Argument_init(&inputArguments);
  169. inputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  170. inputArguments.description = UA_LOCALIZEDTEXT("en-US", "Say your name");
  171. inputArguments.name = UA_STRING("Name");
  172. inputArguments.valueRank = UA_VALUERANK_SCALAR; /* scalar argument */
  173. UA_Argument outputArguments;
  174. UA_Argument_init(&outputArguments);
  175. outputArguments.arrayDimensionsSize = 0;
  176. outputArguments.arrayDimensions = NULL;
  177. outputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  178. outputArguments.description = UA_LOCALIZEDTEXT("en-US", "Receive a greeting");
  179. outputArguments.name = UA_STRING("greeting");
  180. outputArguments.valueRank = UA_VALUERANK_SCALAR;
  181. UA_MethodAttributes addmethodattributes = UA_MethodAttributes_default;
  182. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "Hello World");
  183. addmethodattributes.executable = true;
  184. addmethodattributes.userExecutable = true;
  185. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, 62541),
  186. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  187. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  188. UA_QUALIFIEDNAME(1, "hello_world"), addmethodattributes,
  189. &helloWorld, /* callback of the method node */
  190. 1, &inputArguments, 1, &outputArguments, NULL, NULL);
  191. #endif
  192. /* Add folders for demo information model */
  193. #define DEMOID 50000
  194. #define SCALARID 50001
  195. #define ARRAYID 50002
  196. #define MATRIXID 50003
  197. #define DEPTHID 50004
  198. #define SCALETESTID 40005
  199. UA_ObjectAttributes object_attr = UA_ObjectAttributes_default;
  200. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Demo");
  201. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Demo");
  202. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, DEMOID),
  203. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  204. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Demo"),
  205. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  206. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Scalar");
  207. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Scalar");
  208. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, SCALARID),
  209. UA_NODEID_NUMERIC(1, DEMOID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  210. UA_QUALIFIEDNAME(1, "Scalar"),
  211. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  212. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Array");
  213. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Array");
  214. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, ARRAYID),
  215. UA_NODEID_NUMERIC(1, DEMOID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  216. UA_QUALIFIEDNAME(1, "Array"),
  217. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  218. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Matrix");
  219. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Matrix");
  220. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, MATRIXID), UA_NODEID_NUMERIC(1, DEMOID),
  221. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Matrix"),
  222. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  223. object_attr.description = UA_LOCALIZEDTEXT("en-US", "ScaleTest");
  224. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "ScaleTest");
  225. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, SCALETESTID), UA_NODEID_NUMERIC(1, DEMOID),
  226. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "ScaleTest"),
  227. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  228. /* Fill demo nodes for each type*/
  229. UA_UInt32 matrixDims[2] = {3, 3};
  230. UA_UInt32 id = 51000; // running id in namespace 0
  231. for(UA_UInt32 type = 0; type < UA_TYPES_DIAGNOSTICINFO; type++) {
  232. if(type == UA_TYPES_VARIANT || type == UA_TYPES_DIAGNOSTICINFO)
  233. continue;
  234. UA_VariableAttributes attr = UA_VariableAttributes_default;
  235. attr.dataType = UA_TYPES[type].typeId;
  236. #ifndef UA_ENABLE_TYPENAMES
  237. char name[15];
  238. UA_snprintf(name, 15, "%02d", type);
  239. attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
  240. UA_QualifiedName qualifiedName = UA_QUALIFIEDNAME(1, name);
  241. #else
  242. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US", UA_TYPES[type].typeName);
  243. UA_QualifiedName qualifiedName = UA_QUALIFIEDNAME_ALLOC(1, UA_TYPES[type].typeName);
  244. #endif
  245. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  246. attr.writeMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  247. attr.userWriteMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  248. /* add a scalar node for every built-in type */
  249. attr.valueRank = UA_VALUERANK_SCALAR;
  250. void *value = UA_new(&UA_TYPES[type]);
  251. UA_Variant_setScalar(&attr.value, value, &UA_TYPES[type]);
  252. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id),
  253. UA_NODEID_NUMERIC(1, SCALARID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  254. qualifiedName, baseDataVariableType, attr, NULL, NULL);
  255. UA_Variant_clear(&attr.value);
  256. /* add an array node for every built-in type */
  257. UA_UInt32 arrayDims = 0;
  258. attr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  259. attr.arrayDimensions = &arrayDims;
  260. attr.arrayDimensionsSize = 1;
  261. UA_Variant_setArray(&attr.value, UA_Array_new(10, &UA_TYPES[type]), 10, &UA_TYPES[type]);
  262. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id), UA_NODEID_NUMERIC(1, ARRAYID),
  263. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), qualifiedName,
  264. baseDataVariableType, attr, NULL, NULL);
  265. UA_Variant_clear(&attr.value);
  266. /* add an matrix node for every built-in type */
  267. attr.valueRank = UA_VALUERANK_TWO_DIMENSIONS;
  268. attr.arrayDimensions = matrixDims;
  269. attr.arrayDimensionsSize = 2;
  270. void *myMultiArray = UA_Array_new(9, &UA_TYPES[type]);
  271. attr.value.arrayDimensions = (UA_UInt32 *)UA_Array_new(2, &UA_TYPES[UA_TYPES_INT32]);
  272. attr.value.arrayDimensions[0] = 3;
  273. attr.value.arrayDimensions[1] = 3;
  274. attr.value.arrayDimensionsSize = 2;
  275. attr.value.arrayLength = 9;
  276. attr.value.data = myMultiArray;
  277. attr.value.type = &UA_TYPES[type];
  278. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id), UA_NODEID_NUMERIC(1, MATRIXID),
  279. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), qualifiedName,
  280. baseDataVariableType, attr, NULL, NULL);
  281. UA_Variant_clear(&attr.value);
  282. #ifdef UA_ENABLE_TYPENAMES
  283. UA_LocalizedText_clear(&attr.displayName);
  284. UA_QualifiedName_clear(&qualifiedName);
  285. #endif
  286. }
  287. /* Add Integer and UInteger variables */
  288. UA_VariableAttributes iattr = UA_VariableAttributes_default;
  289. iattr.dataType = UA_NODEID_NUMERIC(0, UA_NS0ID_INTEGER);
  290. iattr.displayName = UA_LOCALIZEDTEXT("en-US", "Integer");
  291. iattr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  292. iattr.writeMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  293. iattr.userWriteMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  294. iattr.valueRank = UA_VALUERANK_SCALAR;
  295. UA_QualifiedName iQualifiedName = UA_QUALIFIEDNAME(1, "integer");
  296. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "integer"),
  297. UA_NODEID_NUMERIC(1, SCALARID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  298. iQualifiedName, baseDataVariableType, iattr, NULL, NULL);
  299. iattr.dataType = UA_NODEID_NUMERIC(0, UA_NS0ID_UINTEGER);
  300. iattr.displayName = UA_LOCALIZEDTEXT("en-US", "UInteger");
  301. UA_QualifiedName uQualifiedName = UA_QUALIFIEDNAME(1, "uinteger");
  302. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "uinteger"),
  303. UA_NODEID_NUMERIC(1, SCALARID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  304. uQualifiedName, baseDataVariableType, iattr, NULL, NULL);
  305. UA_Variant_clear(&iattr.value);
  306. /* Hierarchy of depth 10 for CTT testing with forward and inverse references */
  307. /* Enter node "depth 9" in CTT configuration - Project->Settings->Server
  308. Test->NodeIds->Paths->Starting Node 1 */
  309. object_attr.description = UA_LOCALIZEDTEXT("en-US", "DepthDemo");
  310. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "DepthDemo");
  311. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, DEPTHID), UA_NODEID_NUMERIC(1, DEMOID),
  312. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "DepthDemo"),
  313. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  314. id = DEPTHID; // running id in namespace 0 - Start with Matrix NODE
  315. for(UA_UInt32 i = 1; i <= 20; i++) {
  316. char name[15];
  317. UA_snprintf(name, 15, "depth%i", i);
  318. object_attr.description = UA_LOCALIZEDTEXT("en-US", name);
  319. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
  320. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, id + i),
  321. UA_NODEID_NUMERIC(1, i == 1 ? DEPTHID : id + i - 1),
  322. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  323. UA_QUALIFIEDNAME(1, name),
  324. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  325. }
  326. /* Scale Test: 100 nodes of each type */
  327. int scale_i = 0;
  328. UA_UInt32 scale_nodeid = 43000;
  329. for(UA_UInt32 type = 0; type < UA_TYPES_QUALIFIEDNAME; type++) {
  330. if(type == UA_TYPES_VARIANT || type == UA_TYPES_QUALIFIEDNAME)
  331. continue;
  332. UA_VariableAttributes attr = UA_VariableAttributes_default;
  333. attr.dataType = UA_TYPES[type].typeId;
  334. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  335. attr.writeMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  336. attr.userWriteMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  337. attr.valueRank = UA_VALUERANK_SCALAR;
  338. void *value = UA_new(&UA_TYPES[type]);
  339. UA_Variant_setScalar(&attr.value, value, &UA_TYPES[type]);
  340. for(size_t j = 0; j < 100; j++) {
  341. char name[32];
  342. #ifndef UA_ENABLE_TYPENAMES
  343. UA_snprintf(name, 20, "%02d - %i", type, scale_i);
  344. #else
  345. UA_snprintf(name, 20, "%s - %i", UA_TYPES[type].typeName, scale_i);
  346. #endif
  347. attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
  348. UA_QualifiedName qualifiedName = UA_QUALIFIEDNAME(1, name);
  349. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++scale_nodeid),
  350. UA_NODEID_NUMERIC(1, SCALETESTID),
  351. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  352. qualifiedName, baseDataVariableType, attr, NULL, NULL);
  353. scale_i++;
  354. }
  355. UA_Variant_clear(&attr.value);
  356. }
  357. /* Add the variable to some more places to get a node with three inverse references for the CTT */
  358. UA_ExpandedNodeId answer_nodeid = UA_EXPANDEDNODEID_STRING(1, "the.answer");
  359. UA_Server_addReference(server, UA_NODEID_NUMERIC(1, DEMOID),
  360. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), answer_nodeid, true);
  361. UA_Server_addReference(server, UA_NODEID_NUMERIC(1, SCALARID),
  362. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), answer_nodeid, true);
  363. /* Example for manually setting an attribute within the server */
  364. UA_LocalizedText objectsName = UA_LOCALIZEDTEXT("en-US", "Objects");
  365. UA_Server_writeDisplayName(server, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), objectsName);
  366. #define NOARGID 60000
  367. #define INARGID 60001
  368. #define OUTARGID 60002
  369. #define INOUTARGID 60003
  370. #ifdef UA_ENABLE_METHODCALLS
  371. /* adding some more method nodes to pass CTT */
  372. /* Method without arguments */
  373. addmethodattributes = UA_MethodAttributes_default;
  374. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "noarg");
  375. addmethodattributes.executable = true;
  376. addmethodattributes.userExecutable = true;
  377. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, NOARGID),
  378. UA_NODEID_NUMERIC(1, DEMOID),
  379. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  380. UA_QUALIFIEDNAME(1, "noarg"), addmethodattributes,
  381. &noargMethod, /* callback of the method node */
  382. 0, NULL, 0, NULL, NULL, NULL);
  383. /* Method with in arguments */
  384. addmethodattributes = UA_MethodAttributes_default;
  385. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "inarg");
  386. addmethodattributes.executable = true;
  387. addmethodattributes.userExecutable = true;
  388. UA_Argument_init(&inputArguments);
  389. inputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  390. inputArguments.description = UA_LOCALIZEDTEXT("en-US", "Input");
  391. inputArguments.name = UA_STRING("Input");
  392. inputArguments.valueRank = UA_VALUERANK_SCALAR; //uaexpert will crash if set to 0 ;)
  393. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, INARGID),
  394. UA_NODEID_NUMERIC(1, DEMOID),
  395. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  396. UA_QUALIFIEDNAME(1, "noarg"), addmethodattributes,
  397. &noargMethod, /* callback of the method node */
  398. 1, &inputArguments, 0, NULL, NULL, NULL);
  399. /* Method with out arguments */
  400. addmethodattributes = UA_MethodAttributes_default;
  401. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "outarg");
  402. addmethodattributes.executable = true;
  403. addmethodattributes.userExecutable = true;
  404. UA_Argument_init(&outputArguments);
  405. outputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  406. outputArguments.description = UA_LOCALIZEDTEXT("en-US", "Output");
  407. outputArguments.name = UA_STRING("Output");
  408. outputArguments.valueRank = UA_VALUERANK_SCALAR;
  409. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, OUTARGID),
  410. UA_NODEID_NUMERIC(1, DEMOID),
  411. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  412. UA_QUALIFIEDNAME(1, "outarg"), addmethodattributes,
  413. &outargMethod, /* callback of the method node */
  414. 0, NULL, 1, &outputArguments, NULL, NULL);
  415. /* Method with inout arguments */
  416. addmethodattributes = UA_MethodAttributes_default;
  417. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "inoutarg");
  418. addmethodattributes.executable = true;
  419. addmethodattributes.userExecutable = true;
  420. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, INOUTARGID),
  421. UA_NODEID_NUMERIC(1, DEMOID),
  422. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  423. UA_QUALIFIEDNAME(1, "inoutarg"), addmethodattributes,
  424. &outargMethod, /* callback of the method node */
  425. 1, &inputArguments, 1, &outputArguments, NULL, NULL);
  426. #endif
  427. }
  428. static void
  429. disableAnonymous(UA_ServerConfig *config) {
  430. for(size_t i = 0; i < config->endpointsSize; i++) {
  431. UA_EndpointDescription *ep = &config->endpoints[i];
  432. for(size_t j = 0; j < ep->userIdentityTokensSize; j++) {
  433. UA_UserTokenPolicy *utp = &ep->userIdentityTokens[j];
  434. if(utp->tokenType != UA_USERTOKENTYPE_ANONYMOUS)
  435. continue;
  436. UA_UserTokenPolicy_clear(utp);
  437. /* Move the last to this position */
  438. if(j + 1 < ep->userIdentityTokensSize) {
  439. ep->userIdentityTokens[j] = ep->userIdentityTokens[ep->userIdentityTokensSize-1];
  440. j--;
  441. }
  442. ep->userIdentityTokensSize--;
  443. }
  444. /* Delete the entire array if the last UserTokenPolicy was removed */
  445. if(ep->userIdentityTokensSize == 0) {
  446. UA_free(ep->userIdentityTokens);
  447. ep->userIdentityTokens = NULL;
  448. }
  449. }
  450. }
  451. #ifdef UA_ENABLE_ENCRYPTION
  452. static void
  453. disableUnencrypted(UA_ServerConfig *config) {
  454. for(size_t i = 0; i < config->endpointsSize; i++) {
  455. UA_EndpointDescription *ep = &config->endpoints[i];
  456. if(ep->securityMode != UA_MESSAGESECURITYMODE_NONE)
  457. continue;
  458. UA_EndpointDescription_clear(ep);
  459. /* Move the last to this position */
  460. if(i + 1 < config->endpointsSize) {
  461. config->endpoints[i] = config->endpoints[config->endpointsSize-1];
  462. i--;
  463. }
  464. config->endpointsSize--;
  465. }
  466. /* Delete the entire array if the last Endpoint was removed */
  467. if(config->endpointsSize== 0) {
  468. UA_free(config->endpoints);
  469. config->endpoints = NULL;
  470. }
  471. }
  472. static void
  473. disableOutdatedSecurityPolicy(UA_ServerConfig *config) {
  474. for(size_t i = 0; i < config->endpointsSize; i++) {
  475. UA_EndpointDescription *ep = &config->endpoints[i];
  476. UA_ByteString basic128uri = UA_BYTESTRING("http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15");
  477. UA_ByteString basic256uri = UA_BYTESTRING("http://opcfoundation.org/UA/SecurityPolicy#Basic256");
  478. if(!UA_String_equal(&ep->securityPolicyUri, &basic128uri) &&
  479. !UA_String_equal(&ep->securityPolicyUri, &basic256uri))
  480. continue;
  481. UA_EndpointDescription_clear(ep);
  482. /* Move the last to this position */
  483. if(i + 1 < config->endpointsSize) {
  484. config->endpoints[i] = config->endpoints[config->endpointsSize-1];
  485. i--;
  486. }
  487. config->endpointsSize--;
  488. }
  489. /* Delete the entire array if the last Endpoint was removed */
  490. if(config->endpointsSize== 0) {
  491. UA_free(config->endpoints);
  492. config->endpoints = NULL;
  493. }
  494. }
  495. #endif
  496. UA_Boolean running = true;
  497. static void
  498. stopHandler(int sign) {
  499. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Received Ctrl-C");
  500. running = 0;
  501. }
  502. static void
  503. usage(void) {
  504. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  505. "Usage:\n"
  506. #ifndef UA_ENABLE_ENCRYPTION
  507. "server_ctt [<server-certificate.der>]\n"
  508. #else
  509. "server_ctt <server-certificate.der> <private-key.der>\n"
  510. "\t[--trustlist <tl1.ctl> <tl2.ctl> ... ]\n"
  511. "\t[--issuerlist <il1.der> <il2.der> ... ]\n"
  512. "\t[--revocationlist <rv1.crl> <rv2.crl> ...]\n"
  513. "\t[--enableUnencrypted]\n"
  514. "\t[--enableOutdatedSecurityPolicy]\n"
  515. "\t[--enableTimestampCheck]\n"
  516. #endif
  517. "\t[--enableAnonymous]\n");
  518. }
  519. int main(int argc, char **argv) {
  520. signal(SIGINT, stopHandler); /* catches ctrl-c */
  521. signal(SIGTERM, stopHandler);
  522. for(int i = 1; i < argc; i++) {
  523. if(strcmp(argv[i], "--help") == 0 ||
  524. strcmp(argv[i], "-h") == 0) {
  525. usage();
  526. return EXIT_SUCCESS;
  527. }
  528. }
  529. UA_ServerConfig config;
  530. memset(&config, 0, sizeof(UA_ServerConfig));
  531. /* Load certificate */
  532. size_t pos = 1;
  533. UA_ByteString certificate = UA_BYTESTRING_NULL;
  534. if((size_t)argc >= pos + 1) {
  535. certificate = loadFile(argv[1]);
  536. if(certificate.length == 0) {
  537. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  538. "Unable to load file %s.", argv[pos]);
  539. return EXIT_FAILURE;
  540. }
  541. pos++;
  542. }
  543. #ifdef UA_ENABLE_ENCRYPTION
  544. /* Load the private key */
  545. UA_ByteString privateKey = UA_BYTESTRING_NULL;
  546. if((size_t)argc >= pos + 1) {
  547. privateKey = loadFile(argv[2]);
  548. if(privateKey.length == 0) {
  549. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  550. "Unable to load file %s.", argv[pos]);
  551. return EXIT_FAILURE;
  552. }
  553. pos++;
  554. }
  555. UA_ByteString trustList[100];
  556. size_t trustListSize = 0;
  557. UA_ByteString issuerList[100];
  558. size_t issuerListSize = 0;
  559. UA_ByteString revocationList[100];
  560. size_t revocationListSize = 0;
  561. char filetype = ' '; /* t==trustlist, l == issuerList, r==revocationlist */
  562. UA_Boolean enableUnencr = false;
  563. UA_Boolean enableSec = false;
  564. UA_Boolean enableTime = false;
  565. #endif
  566. UA_Boolean enableAnon = false;
  567. /* Loop over the remaining arguments */
  568. for(; pos < (size_t)argc; pos++) {
  569. if(strcmp(argv[pos], "--enableAnonymous") == 0) {
  570. enableAnon = true;
  571. continue;
  572. }
  573. #ifdef UA_ENABLE_ENCRYPTION
  574. if(strcmp(argv[pos], "--enableUnencrypted") == 0) {
  575. enableUnencr = true;
  576. continue;
  577. }
  578. if(strcmp(argv[pos], "--enableOutdatedSecurityPolicy") == 0) {
  579. enableSec = true;
  580. continue;
  581. }
  582. if(strcmp(argv[pos], "--enableTimestampCheck") == 0) {
  583. enableTime = true;
  584. continue;
  585. }
  586. if(strcmp(argv[pos], "--trustlist") == 0) {
  587. filetype = 't';
  588. continue;
  589. }
  590. if(strcmp(argv[pos], "--issuerlist") == 0) {
  591. filetype = 'l';
  592. continue;
  593. }
  594. if(strcmp(argv[pos], "--revocationlist") == 0) {
  595. filetype = 'r';
  596. continue;
  597. }
  598. if(filetype == 't') {
  599. if(trustListSize >= 100) {
  600. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  601. "Too many trust lists");
  602. return EXIT_FAILURE;
  603. }
  604. trustList[trustListSize] = loadFile(argv[pos]);
  605. if(trustList[trustListSize].data == NULL) {
  606. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  607. "Unable to load trust list %s", argv[pos]);
  608. return EXIT_FAILURE;
  609. }
  610. trustListSize++;
  611. continue;
  612. }
  613. if(filetype == 'l') {
  614. if(issuerListSize >= 100) {
  615. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  616. "Too many trust lists");
  617. return EXIT_FAILURE;
  618. }
  619. issuerList[issuerListSize] = loadFile(argv[pos]);
  620. if(issuerList[issuerListSize].data == NULL) {
  621. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  622. "Unable to load trust list %s", argv[pos]);
  623. return EXIT_FAILURE;
  624. }
  625. issuerListSize++;
  626. continue;
  627. }
  628. if(filetype == 'r') {
  629. if(revocationListSize >= 100) {
  630. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  631. "Too many revocation lists");
  632. return EXIT_FAILURE;
  633. }
  634. revocationList[revocationListSize] = loadFile(argv[pos]);
  635. if(revocationList[revocationListSize].data == NULL) {
  636. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  637. "Unable to load revocationlist %s", argv[pos]);
  638. return EXIT_FAILURE;
  639. }
  640. revocationListSize++;
  641. continue;
  642. }
  643. #endif
  644. usage();
  645. return EXIT_FAILURE;
  646. }
  647. #ifdef UA_ENABLE_ENCRYPTION
  648. UA_ServerConfig_setDefaultWithSecurityPolicies(&config, 4840,
  649. &certificate, &privateKey,
  650. trustList, trustListSize,
  651. issuerList, issuerListSize,
  652. revocationList, revocationListSize);
  653. if(!enableUnencr)
  654. disableUnencrypted(&config);
  655. if(!enableSec)
  656. disableOutdatedSecurityPolicy(&config);
  657. /* Set operation limits */
  658. config.maxNodesPerRead = MAX_OPERATION_LIMIT;
  659. config.maxNodesPerWrite = MAX_OPERATION_LIMIT;
  660. config.maxNodesPerMethodCall = MAX_OPERATION_LIMIT;
  661. config.maxNodesPerBrowse = MAX_OPERATION_LIMIT;
  662. config.maxNodesPerRegisterNodes = MAX_OPERATION_LIMIT;
  663. config.maxNodesPerTranslateBrowsePathsToNodeIds = MAX_OPERATION_LIMIT;
  664. config.maxNodesPerNodeManagement = MAX_OPERATION_LIMIT;
  665. config.maxMonitoredItemsPerCall = MAX_OPERATION_LIMIT;
  666. /* If RequestTimestamp is '0', log the warning and proceed */
  667. config.verifyRequestTimestamp = UA_RULEHANDLING_WARN;
  668. if(enableTime)
  669. config.verifyRequestTimestamp = UA_RULEHANDLING_DEFAULT;
  670. #else
  671. UA_ServerConfig_setMinimal(&config, 4840, &certificate);
  672. #endif
  673. if(!enableAnon)
  674. disableAnonymous(&config);
  675. /* Clean up temp values */
  676. UA_ByteString_clear(&certificate);
  677. #ifdef UA_ENABLE_ENCRYPTION
  678. UA_ByteString_clear(&privateKey);
  679. for(size_t i = 0; i < trustListSize; i++)
  680. UA_ByteString_clear(&trustList[i]);
  681. for(size_t i = 0; i < issuerListSize; i++)
  682. UA_ByteString_clear(&issuerList[i]);
  683. for(size_t i = 0; i < revocationListSize; i++)
  684. UA_ByteString_clear(&revocationList[i]);
  685. #endif
  686. /* Override with a custom access control policy */
  687. config.accessControl.getUserAccessLevel = getUserAccessLevel_disallowSpecific;
  688. UA_String_clear(&config.applicationDescription.applicationUri);
  689. config.applicationDescription.applicationUri =
  690. UA_String_fromChars("urn:open62541.server.application");
  691. config.shutdownDelay = 5000.0; /* 5s */
  692. UA_Server *server = UA_Server_newWithConfig(&config);
  693. if(server == NULL)
  694. return EXIT_FAILURE;
  695. setInformationModel(server);
  696. /* run server */
  697. UA_StatusCode retval = UA_Server_run(server, &running);
  698. UA_Server_delete(server);
  699. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  700. }