server_ctt.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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/server.h>
  11. #include <open62541/plugin/log_stdout.h>
  12. #include <open62541/server_config_default.h>
  13. #include <open62541/plugin/pki_default.h>
  14. #include <signal.h>
  15. #include <stdlib.h>
  16. #include "common.h"
  17. #define MAX_OPERATION_LIMIT 10000
  18. /* This server is configured to the Compliance Testing Tools (CTT) against. The
  19. * corresponding CTT configuration is available at
  20. * https://github.com/open62541/open62541-ctt */
  21. static const UA_NodeId baseDataVariableType = {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_BASEDATAVARIABLETYPE}};
  22. static const UA_NodeId accessDenied = {1, UA_NODEIDTYPE_NUMERIC, {1337}};
  23. /* Custom AccessControl policy that disallows access to one specific node */
  24. static UA_Byte
  25. getUserAccessLevel_disallowSpecific(UA_Server *server, UA_AccessControl *ac,
  26. const UA_NodeId *sessionId, void *sessionContext,
  27. const UA_NodeId *nodeId, void *nodeContext) {
  28. if(UA_NodeId_equal(nodeId, &accessDenied))
  29. return 0x00;
  30. return 0xFF;
  31. }
  32. /* Datasource Example */
  33. static UA_StatusCode
  34. readTimeData(UA_Server *server,
  35. const UA_NodeId *sessionId, void *sessionContext,
  36. const UA_NodeId *nodeId, void *nodeContext,
  37. UA_Boolean sourceTimeStamp,
  38. const UA_NumericRange *range, UA_DataValue *value) {
  39. if(range) {
  40. value->hasStatus = true;
  41. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  42. return UA_STATUSCODE_GOOD;
  43. }
  44. UA_DateTime currentTime = UA_DateTime_now();
  45. UA_Variant_setScalarCopy(&value->value, &currentTime, &UA_TYPES[UA_TYPES_DATETIME]);
  46. value->hasValue = true;
  47. if(sourceTimeStamp) {
  48. value->hasSourceTimestamp = true;
  49. value->sourceTimestamp = currentTime;
  50. }
  51. return UA_STATUSCODE_GOOD;
  52. }
  53. static UA_StatusCode
  54. readRandomBoolData(UA_Server *server,
  55. const UA_NodeId *sessionId, void *sessionContext,
  56. const UA_NodeId *nodeId, void *nodeContext,
  57. UA_Boolean sourceTimeStamp,
  58. const UA_NumericRange *range, UA_DataValue *value) {
  59. if(range) {
  60. value->hasStatus = true;
  61. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  62. return UA_STATUSCODE_GOOD;
  63. }
  64. UA_Boolean toggle = !((UA_UInt32_random() % 10 ) % 2);
  65. UA_Variant_setScalarCopy(&value->value, &toggle, &UA_TYPES[UA_TYPES_BOOLEAN]);
  66. value->hasValue = true;
  67. if(sourceTimeStamp) {
  68. value->hasSourceTimestamp = true;
  69. value->sourceTimestamp = UA_DateTime_now();
  70. }
  71. return UA_STATUSCODE_GOOD;
  72. }
  73. static UA_StatusCode
  74. readRandomInt16Data(UA_Server *server,
  75. const UA_NodeId *sessionId, void *sessionContext,
  76. const UA_NodeId *nodeId, void *nodeContext,
  77. UA_Boolean sourceTimeStamp,
  78. const UA_NumericRange *range, UA_DataValue *value) {
  79. if(range) {
  80. value->hasStatus = true;
  81. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  82. return UA_STATUSCODE_GOOD;
  83. }
  84. UA_Int16 toggle = (UA_Int16)UA_UInt32_random();
  85. UA_Variant_setScalarCopy(&value->value, &toggle, &UA_TYPES[UA_TYPES_INT16]);
  86. value->hasValue = true;
  87. if(sourceTimeStamp) {
  88. value->hasSourceTimestamp = true;
  89. value->sourceTimestamp = UA_DateTime_now();
  90. }
  91. return UA_STATUSCODE_GOOD;
  92. }
  93. static UA_StatusCode
  94. readRandomInt32Data(UA_Server *server,
  95. const UA_NodeId *sessionId, void *sessionContext,
  96. const UA_NodeId *nodeId, void *nodeContext,
  97. UA_Boolean sourceTimeStamp,
  98. const UA_NumericRange *range, UA_DataValue *value) {
  99. if(range) {
  100. value->hasStatus = true;
  101. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  102. return UA_STATUSCODE_GOOD;
  103. }
  104. UA_Int32 toggle = (UA_Int32)UA_UInt32_random();
  105. UA_Variant_setScalarCopy(&value->value, &toggle, &UA_TYPES[UA_TYPES_INT32]);
  106. value->hasValue = true;
  107. if(sourceTimeStamp) {
  108. value->hasSourceTimestamp = true;
  109. value->sourceTimestamp = UA_DateTime_now();
  110. }
  111. return UA_STATUSCODE_GOOD;
  112. }
  113. static UA_StatusCode
  114. readRandomInt64Data(UA_Server *server,
  115. const UA_NodeId *sessionId, void *sessionContext,
  116. const UA_NodeId *nodeId, void *nodeContext,
  117. UA_Boolean sourceTimeStamp,
  118. const UA_NumericRange *range, UA_DataValue *value) {
  119. if(range) {
  120. value->hasStatus = true;
  121. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  122. return UA_STATUSCODE_GOOD;
  123. }
  124. UA_Int64 toggle = (UA_Int64)UA_UInt32_random();
  125. UA_Variant_setScalarCopy(&value->value, &toggle, &UA_TYPES[UA_TYPES_INT64]);
  126. value->hasValue = true;
  127. if(sourceTimeStamp) {
  128. value->hasSourceTimestamp = true;
  129. value->sourceTimestamp = UA_DateTime_now();
  130. }
  131. return UA_STATUSCODE_GOOD;
  132. }
  133. static UA_StatusCode
  134. readRandomUInt16Data(UA_Server *server,
  135. const UA_NodeId *sessionId, void *sessionContext,
  136. const UA_NodeId *nodeId, void *nodeContext,
  137. UA_Boolean sourceTimeStamp,
  138. const UA_NumericRange *range, UA_DataValue *value) {
  139. if(range) {
  140. value->hasStatus = true;
  141. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  142. return UA_STATUSCODE_GOOD;
  143. }
  144. UA_UInt16 toggle = (UA_UInt16)UA_UInt32_random();
  145. UA_Variant_setScalarCopy(&value->value, &toggle, &UA_TYPES[UA_TYPES_UINT16]);
  146. value->hasValue = true;
  147. if(sourceTimeStamp) {
  148. value->hasSourceTimestamp = true;
  149. value->sourceTimestamp = UA_DateTime_now();
  150. }
  151. return UA_STATUSCODE_GOOD;
  152. }
  153. static UA_StatusCode
  154. readRandomUInt32Data(UA_Server *server,
  155. const UA_NodeId *sessionId, void *sessionContext,
  156. const UA_NodeId *nodeId, void *nodeContext,
  157. UA_Boolean sourceTimeStamp,
  158. const UA_NumericRange *range, UA_DataValue *value) {
  159. if(range) {
  160. value->hasStatus = true;
  161. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  162. return UA_STATUSCODE_GOOD;
  163. }
  164. UA_UInt32 toggle = UA_UInt32_random();
  165. UA_Variant_setScalarCopy(&value->value, &toggle, &UA_TYPES[UA_TYPES_UINT32]);
  166. value->hasValue = true;
  167. if(sourceTimeStamp) {
  168. value->hasSourceTimestamp = true;
  169. value->sourceTimestamp = UA_DateTime_now();
  170. }
  171. return UA_STATUSCODE_GOOD;
  172. }
  173. static UA_StatusCode
  174. readRandomUInt64Data(UA_Server *server,
  175. const UA_NodeId *sessionId, void *sessionContext,
  176. const UA_NodeId *nodeId, void *nodeContext,
  177. UA_Boolean sourceTimeStamp,
  178. const UA_NumericRange *range, UA_DataValue *value) {
  179. if(range) {
  180. value->hasStatus = true;
  181. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  182. return UA_STATUSCODE_GOOD;
  183. }
  184. UA_UInt64 toggle = UA_UInt32_random();
  185. UA_Variant_setScalarCopy(&value->value, &toggle, &UA_TYPES[UA_TYPES_UINT64]);
  186. value->hasValue = true;
  187. if(sourceTimeStamp) {
  188. value->hasSourceTimestamp = true;
  189. value->sourceTimestamp = UA_DateTime_now();
  190. }
  191. return UA_STATUSCODE_GOOD;
  192. }
  193. static UA_StatusCode
  194. readRandomStringData (UA_Server *server,
  195. const UA_NodeId *sessionId, void *sessionContext,
  196. const UA_NodeId *nodeId, void *nodeContext,
  197. UA_Boolean sourceTimeStamp,
  198. const UA_NumericRange *range, UA_DataValue *value) {
  199. if(range) {
  200. value->hasStatus = true;
  201. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  202. return UA_STATUSCODE_GOOD;
  203. }
  204. char randomName[12];
  205. UA_snprintf(randomName, 12, "Random%d", UA_UInt32_random());
  206. UA_String toggle = UA_STRING(randomName);
  207. UA_Variant_setScalarCopy(&value->value, &toggle, &UA_TYPES[UA_TYPES_STRING]);
  208. value->hasValue = true;
  209. if(sourceTimeStamp) {
  210. value->hasSourceTimestamp = true;
  211. value->sourceTimestamp = UA_DateTime_now();
  212. }
  213. return UA_STATUSCODE_GOOD;
  214. }
  215. static UA_StatusCode
  216. readRandomFloatData (UA_Server *server,
  217. const UA_NodeId *sessionId, void *sessionContext,
  218. const UA_NodeId *nodeId, void *nodeContext,
  219. UA_Boolean sourceTimeStamp,
  220. const UA_NumericRange *range, UA_DataValue *value) {
  221. if(range) {
  222. value->hasStatus = true;
  223. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  224. return UA_STATUSCODE_GOOD;
  225. }
  226. UA_Float toggle = (UA_Float)UA_UInt32_random();
  227. UA_Variant_setScalarCopy(&value->value, &toggle, &UA_TYPES[UA_TYPES_FLOAT]);
  228. value->hasValue = true;
  229. if(sourceTimeStamp) {
  230. value->hasSourceTimestamp = true;
  231. value->sourceTimestamp = UA_DateTime_now();
  232. }
  233. return UA_STATUSCODE_GOOD;
  234. }
  235. static UA_StatusCode
  236. readRandomDoubleData (UA_Server *server,
  237. const UA_NodeId *sessionId, void *sessionContext,
  238. const UA_NodeId *nodeId, void *nodeContext,
  239. UA_Boolean sourceTimeStamp,
  240. const UA_NumericRange *range, UA_DataValue *value) {
  241. if(range) {
  242. value->hasStatus = true;
  243. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  244. return UA_STATUSCODE_GOOD;
  245. }
  246. UA_Double toggle = (UA_Double)UA_UInt32_random();
  247. UA_Variant_setScalarCopy(&value->value, &toggle, &UA_TYPES[UA_TYPES_DOUBLE]);
  248. value->hasValue = true;
  249. if(sourceTimeStamp) {
  250. value->hasSourceTimestamp = true;
  251. value->sourceTimestamp = UA_DateTime_now();
  252. }
  253. return UA_STATUSCODE_GOOD;
  254. }
  255. static UA_StatusCode
  256. readByteString (UA_Server *server,
  257. const UA_NodeId *sessionId, void *sessionContext,
  258. const UA_NodeId *nodeId, void *nodeContext,
  259. UA_Boolean sourceTimeStamp,
  260. const UA_NumericRange *range, UA_DataValue *value) {
  261. if(range) {
  262. value->hasStatus = true;
  263. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  264. return UA_STATUSCODE_GOOD;
  265. }
  266. char randomName[8];
  267. UA_snprintf(randomName, 8, "%d%d", UA_UInt32_random(), UA_UInt32_random());
  268. UA_ByteString randomByte = UA_BYTESTRING(randomName);
  269. UA_Variant_setScalarCopy(&value->value, &randomByte, &UA_TYPES[UA_TYPES_BYTESTRING]);
  270. value->hasValue = true;
  271. if(sourceTimeStamp) {
  272. value->hasSourceTimestamp = true;
  273. value->sourceTimestamp = UA_DateTime_now();
  274. }
  275. return UA_STATUSCODE_GOOD;
  276. }
  277. /* Method Node Example */
  278. #ifdef UA_ENABLE_METHODCALLS
  279. static UA_StatusCode
  280. helloWorld(UA_Server *server,
  281. const UA_NodeId *sessionId, void *sessionContext,
  282. const UA_NodeId *methodId, void *methodContext,
  283. const UA_NodeId *objectId, void *objectContext,
  284. size_t inputSize, const UA_Variant *input,
  285. size_t outputSize, UA_Variant *output) {
  286. /* input is a scalar string (checked by the server) */
  287. UA_String *name = (UA_String *)input[0].data;
  288. UA_String hello = UA_STRING("Hello ");
  289. UA_String greet;
  290. greet.length = hello.length + name->length;
  291. greet.data = (UA_Byte *)UA_malloc(greet.length);
  292. memcpy(greet.data, hello.data, hello.length);
  293. memcpy(greet.data + hello.length, name->data, name->length);
  294. UA_Variant_setScalarCopy(output, &greet, &UA_TYPES[UA_TYPES_STRING]);
  295. UA_String_clear(&greet);
  296. return UA_STATUSCODE_GOOD;
  297. }
  298. static UA_StatusCode
  299. noargMethod(UA_Server *server,
  300. const UA_NodeId *sessionId, void *sessionContext,
  301. const UA_NodeId *methodId, void *methodContext,
  302. const UA_NodeId *objectId, void *objectContext,
  303. size_t inputSize, const UA_Variant *input,
  304. size_t outputSize, UA_Variant *output) {
  305. return UA_STATUSCODE_GOOD;
  306. }
  307. static UA_StatusCode
  308. outargMethod(UA_Server *server,
  309. const UA_NodeId *sessionId, void *sessionContext,
  310. const UA_NodeId *methodId, void *methodContext,
  311. const UA_NodeId *objectId, void *objectContext,
  312. size_t inputSize, const UA_Variant *input,
  313. size_t outputSize, UA_Variant *output) {
  314. UA_Int32 out = 42;
  315. UA_Variant_setScalarCopy(output, &out, &UA_TYPES[UA_TYPES_INT32]);
  316. return UA_STATUSCODE_GOOD;
  317. }
  318. #endif
  319. static void
  320. setInformationModel(UA_Server *server) {
  321. /* add a static variable node to the server */
  322. UA_VariableAttributes myVar = UA_VariableAttributes_default;
  323. myVar.description = UA_LOCALIZEDTEXT("en-US", "the answer");
  324. myVar.displayName = UA_LOCALIZEDTEXT("en-US", "the answer");
  325. myVar.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  326. myVar.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  327. myVar.valueRank = UA_VALUERANK_SCALAR;
  328. UA_Int32 myInteger = 42;
  329. UA_Variant_setScalar(&myVar.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  330. const UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  331. const UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  332. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  333. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  334. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  335. myIntegerName, baseDataVariableType, myVar, NULL, NULL);
  336. /* add a static variable that is readable but not writable*/
  337. myVar = UA_VariableAttributes_default;
  338. myVar.description = UA_LOCALIZEDTEXT("en-US", "the answer - not readable");
  339. myVar.displayName = UA_LOCALIZEDTEXT("en-US", "the answer - not readable");
  340. myVar.accessLevel = UA_ACCESSLEVELMASK_WRITE;
  341. myVar.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  342. myVar.valueRank = UA_VALUERANK_SCALAR;
  343. UA_Variant_setScalar(&myVar.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  344. const UA_QualifiedName myInteger2Name = UA_QUALIFIEDNAME(1, "the answer - not readable");
  345. const UA_NodeId myInteger2NodeId = UA_NODEID_STRING(1, "the.answer.no.read");
  346. UA_Server_addVariableNode(server, myInteger2NodeId, parentNodeId, parentReferenceNodeId,
  347. myInteger2Name, baseDataVariableType, myVar, NULL, NULL);
  348. /* add a variable that is not readable or writable for the current user */
  349. myVar = UA_VariableAttributes_default;
  350. myVar.description = UA_LOCALIZEDTEXT("en-US", "the answer - not current user");
  351. myVar.displayName = UA_LOCALIZEDTEXT("en-US", "the answer - not current user");
  352. myVar.accessLevel = UA_ACCESSLEVELMASK_WRITE;
  353. myVar.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  354. myVar.valueRank = UA_VALUERANK_SCALAR;
  355. myVar.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  356. UA_Variant_setScalar(&myVar.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  357. const UA_QualifiedName accessDeniedName = UA_QUALIFIEDNAME(1, "the answer - not current user");
  358. UA_Server_addVariableNode(server, accessDenied, parentNodeId, parentReferenceNodeId,
  359. accessDeniedName, baseDataVariableType, myVar, NULL, NULL);
  360. /* add a variable with the datetime data source */
  361. UA_DataSource dateDataSource;
  362. dateDataSource.read = readTimeData;
  363. dateDataSource.write = NULL;
  364. UA_VariableAttributes v_attr = UA_VariableAttributes_default;
  365. v_attr.description = UA_LOCALIZEDTEXT("en-US", "current time");
  366. v_attr.displayName = UA_LOCALIZEDTEXT("en-US", "current time");
  367. v_attr.accessLevel = UA_ACCESSLEVELMASK_READ;
  368. v_attr.dataType = UA_TYPES[UA_TYPES_DATETIME].typeId;
  369. v_attr.valueRank = UA_VALUERANK_SCALAR;
  370. const UA_QualifiedName dateName = UA_QUALIFIEDNAME(1, "current time");
  371. UA_Server_addDataSourceVariableNode(server, UA_NODEID_NUMERIC(1, 2345),
  372. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  373. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), dateName,
  374. baseDataVariableType, v_attr, dateDataSource, NULL, NULL);
  375. /* add a bytestring variable with some content */
  376. myVar = UA_VariableAttributes_default;
  377. myVar.description = UA_LOCALIZEDTEXT("", "");
  378. myVar.displayName = UA_LOCALIZEDTEXT("", "example bytestring");
  379. myVar.dataType = UA_TYPES[UA_TYPES_BYTESTRING].typeId;
  380. myVar.valueRank = UA_VALUERANK_SCALAR;
  381. myVar.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  382. UA_ByteString myByteString = UA_BYTESTRING("test123\0test123");
  383. UA_Variant_setScalar(&myVar.value, &myByteString, &UA_TYPES[UA_TYPES_BYTESTRING]);
  384. const UA_QualifiedName byteStringName = UA_QUALIFIEDNAME(1, "example bytestring");
  385. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "myByteString"),
  386. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  387. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), byteStringName,
  388. baseDataVariableType, myVar, NULL, NULL);
  389. /* Add HelloWorld method to the server */
  390. #ifdef UA_ENABLE_METHODCALLS
  391. /* Method with IO Arguments */
  392. UA_Argument inputArguments;
  393. UA_Argument_init(&inputArguments);
  394. inputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  395. inputArguments.description = UA_LOCALIZEDTEXT("en-US", "Say your name");
  396. inputArguments.name = UA_STRING("Name");
  397. inputArguments.valueRank = UA_VALUERANK_SCALAR; /* scalar argument */
  398. UA_Argument outputArguments;
  399. UA_Argument_init(&outputArguments);
  400. outputArguments.arrayDimensionsSize = 0;
  401. outputArguments.arrayDimensions = NULL;
  402. outputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  403. outputArguments.description = UA_LOCALIZEDTEXT("en-US", "Receive a greeting");
  404. outputArguments.name = UA_STRING("greeting");
  405. outputArguments.valueRank = UA_VALUERANK_SCALAR;
  406. UA_MethodAttributes addmethodattributes = UA_MethodAttributes_default;
  407. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "Hello World");
  408. addmethodattributes.executable = true;
  409. addmethodattributes.userExecutable = true;
  410. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, 62541),
  411. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  412. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  413. UA_QUALIFIEDNAME(1, "hello_world"), addmethodattributes,
  414. &helloWorld, /* callback of the method node */
  415. 1, &inputArguments, 1, &outputArguments, NULL, NULL);
  416. #endif
  417. /* Add folders for demo information model */
  418. #define DEMOID 50000
  419. #define SCALARID 50001
  420. #define ARRAYID 50002
  421. #define MATRIXID 50003
  422. #define DEPTHID 50004
  423. #define SCALETESTID 40005
  424. UA_ObjectAttributes object_attr = UA_ObjectAttributes_default;
  425. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Demo");
  426. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Demo");
  427. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, DEMOID),
  428. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  429. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Demo"),
  430. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  431. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Scalar");
  432. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Scalar");
  433. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, SCALARID),
  434. UA_NODEID_NUMERIC(1, DEMOID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  435. UA_QUALIFIEDNAME(1, "Scalar"),
  436. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  437. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Array");
  438. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Array");
  439. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, ARRAYID),
  440. UA_NODEID_NUMERIC(1, DEMOID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  441. UA_QUALIFIEDNAME(1, "Array"),
  442. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  443. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Matrix");
  444. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Matrix");
  445. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, MATRIXID), UA_NODEID_NUMERIC(1, DEMOID),
  446. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Matrix"),
  447. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  448. object_attr.description = UA_LOCALIZEDTEXT("en-US", "ScaleTest");
  449. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "ScaleTest");
  450. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, SCALETESTID), UA_NODEID_NUMERIC(1, DEMOID),
  451. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "ScaleTest"),
  452. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  453. /* Fill demo nodes for each type*/
  454. UA_UInt32 matrixDims[2] = {3, 3};
  455. UA_UInt32 id = 51000; // running id in namespace 0
  456. for(UA_UInt32 type = 0; type < UA_TYPES_DIAGNOSTICINFO; type++) {
  457. if(type == UA_TYPES_VARIANT || type == UA_TYPES_DIAGNOSTICINFO)
  458. continue;
  459. UA_VariableAttributes attr = UA_VariableAttributes_default;
  460. attr.dataType = UA_TYPES[type].typeId;
  461. #ifndef UA_ENABLE_TYPEDESCRIPTION
  462. char name[15];
  463. UA_snprintf(name, 15, "%02d", type);
  464. attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
  465. UA_QualifiedName qualifiedName = UA_QUALIFIEDNAME(1, name);
  466. #else
  467. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US", UA_TYPES[type].typeName);
  468. UA_QualifiedName qualifiedName = UA_QUALIFIEDNAME_ALLOC(1, UA_TYPES[type].typeName);
  469. #endif
  470. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  471. attr.writeMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  472. attr.userWriteMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  473. /* add a scalar node for every built-in type */
  474. attr.valueRank = UA_VALUERANK_SCALAR;
  475. void *value = UA_new(&UA_TYPES[type]);
  476. UA_Variant_setScalar(&attr.value, value, &UA_TYPES[type]);
  477. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id),
  478. UA_NODEID_NUMERIC(1, SCALARID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  479. qualifiedName, baseDataVariableType, attr, NULL, NULL);
  480. UA_Variant_clear(&attr.value);
  481. /* add an array node for every built-in type */
  482. UA_UInt32 arrayDims = 0;
  483. attr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  484. attr.arrayDimensions = &arrayDims;
  485. attr.arrayDimensionsSize = 1;
  486. UA_Variant_setArray(&attr.value, UA_Array_new(10, &UA_TYPES[type]), 10, &UA_TYPES[type]);
  487. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id), UA_NODEID_NUMERIC(1, ARRAYID),
  488. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), qualifiedName,
  489. baseDataVariableType, attr, NULL, NULL);
  490. UA_Variant_clear(&attr.value);
  491. /* add an matrix node for every built-in type */
  492. attr.valueRank = UA_VALUERANK_TWO_DIMENSIONS;
  493. attr.arrayDimensions = matrixDims;
  494. attr.arrayDimensionsSize = 2;
  495. void *myMultiArray = UA_Array_new(9, &UA_TYPES[type]);
  496. attr.value.arrayDimensions = (UA_UInt32 *)UA_Array_new(2, &UA_TYPES[UA_TYPES_INT32]);
  497. attr.value.arrayDimensions[0] = 3;
  498. attr.value.arrayDimensions[1] = 3;
  499. attr.value.arrayDimensionsSize = 2;
  500. attr.value.arrayLength = 9;
  501. attr.value.data = myMultiArray;
  502. attr.value.type = &UA_TYPES[type];
  503. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id), UA_NODEID_NUMERIC(1, MATRIXID),
  504. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), qualifiedName,
  505. baseDataVariableType, attr, NULL, NULL);
  506. UA_Variant_clear(&attr.value);
  507. #ifdef UA_ENABLE_TYPEDESCRIPTION
  508. UA_LocalizedText_clear(&attr.displayName);
  509. UA_QualifiedName_clear(&qualifiedName);
  510. #endif
  511. }
  512. /* Add Integer and UInteger variables */
  513. UA_VariableAttributes iattr = UA_VariableAttributes_default;
  514. iattr.dataType = UA_NODEID_NUMERIC(0, UA_NS0ID_INTEGER);
  515. iattr.displayName = UA_LOCALIZEDTEXT("en-US", "Integer");
  516. iattr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  517. iattr.writeMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  518. iattr.userWriteMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  519. iattr.valueRank = UA_VALUERANK_SCALAR;
  520. UA_QualifiedName iQualifiedName = UA_QUALIFIEDNAME(1, "integer");
  521. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "integer"),
  522. UA_NODEID_NUMERIC(1, SCALARID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  523. iQualifiedName, baseDataVariableType, iattr, NULL, NULL);
  524. iattr.dataType = UA_NODEID_NUMERIC(0, UA_NS0ID_UINTEGER);
  525. iattr.displayName = UA_LOCALIZEDTEXT("en-US", "UInteger");
  526. UA_QualifiedName uQualifiedName = UA_QUALIFIEDNAME(1, "uinteger");
  527. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "uinteger"),
  528. UA_NODEID_NUMERIC(1, SCALARID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  529. uQualifiedName, baseDataVariableType, iattr, NULL, NULL);
  530. UA_Variant_clear(&iattr.value);
  531. /* Hierarchy of depth 10 for CTT testing with forward and inverse references */
  532. /* Enter node "depth 9" in CTT configuration - Project->Settings->Server
  533. Test->NodeIds->Paths->Starting Node 1 */
  534. object_attr.description = UA_LOCALIZEDTEXT("en-US", "DepthDemo");
  535. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "DepthDemo");
  536. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, DEPTHID), UA_NODEID_NUMERIC(1, DEMOID),
  537. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "DepthDemo"),
  538. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  539. id = DEPTHID; // running id in namespace 0 - Start with Matrix NODE
  540. for(UA_UInt32 i = 1; i <= 20; i++) {
  541. char name[15];
  542. UA_snprintf(name, 15, "depth%i", i);
  543. object_attr.description = UA_LOCALIZEDTEXT("en-US", name);
  544. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
  545. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, id + i),
  546. UA_NODEID_NUMERIC(1, i == 1 ? DEPTHID : id + i - 1),
  547. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  548. UA_QUALIFIEDNAME(1, name),
  549. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  550. }
  551. /* Scale Test: 100 nodes of each type */
  552. int scale_i = 0;
  553. UA_UInt32 scale_nodeid = 43000;
  554. for(UA_UInt32 type = 0; type < 15; type++) {
  555. if(type == UA_TYPES_SBYTE || type == UA_TYPES_BYTE
  556. || type == UA_TYPES_GUID)
  557. continue;
  558. UA_DataSource scaleTestDataSource;
  559. UA_VariableAttributes attr = UA_VariableAttributes_default;
  560. attr.dataType = UA_TYPES[type].typeId;
  561. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  562. attr.writeMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  563. attr.userWriteMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  564. attr.valueRank = UA_VALUERANK_SCALAR;
  565. switch(UA_TYPES[type].typeIndex) {
  566. case UA_TYPES_BOOLEAN: {
  567. scaleTestDataSource.read = readRandomBoolData;
  568. scaleTestDataSource.write = NULL;
  569. break;
  570. }
  571. case UA_TYPES_INT16: {
  572. scaleTestDataSource.read = readRandomInt16Data;
  573. scaleTestDataSource.write = NULL;
  574. break;
  575. }
  576. case UA_TYPES_UINT16: {
  577. scaleTestDataSource.read = readRandomUInt16Data;
  578. scaleTestDataSource.write = NULL;
  579. break;
  580. }
  581. case UA_TYPES_INT32: {
  582. scaleTestDataSource.read = readRandomInt32Data;
  583. scaleTestDataSource.write = NULL;
  584. break;
  585. }
  586. case UA_TYPES_UINT32: {
  587. scaleTestDataSource.read = readRandomUInt32Data;
  588. scaleTestDataSource.write = NULL;
  589. break;
  590. }
  591. case UA_TYPES_INT64: {
  592. scaleTestDataSource.read = readRandomInt64Data;
  593. scaleTestDataSource.write = NULL;
  594. break;
  595. }
  596. case UA_TYPES_UINT64: {
  597. scaleTestDataSource.read = readRandomUInt64Data;
  598. scaleTestDataSource.write = NULL;
  599. break;
  600. }
  601. case UA_TYPES_STRING: {
  602. scaleTestDataSource.read = readRandomStringData;
  603. scaleTestDataSource.write = NULL;
  604. break;
  605. }
  606. case UA_TYPES_FLOAT: {
  607. scaleTestDataSource.read = readRandomFloatData;
  608. scaleTestDataSource.write = NULL;
  609. break;
  610. }
  611. case UA_TYPES_DOUBLE: {
  612. scaleTestDataSource.read = readRandomDoubleData;
  613. scaleTestDataSource.write = NULL;
  614. break;
  615. }
  616. case UA_TYPES_DATETIME:
  617. scaleTestDataSource.read = readTimeData;
  618. scaleTestDataSource.write = NULL;
  619. break;
  620. case UA_TYPES_BYTESTRING:
  621. scaleTestDataSource.read = readByteString;
  622. scaleTestDataSource.write = NULL;
  623. break;
  624. default:
  625. break;
  626. }
  627. for(size_t j = 0; j < 100; j++) {
  628. char name[32];
  629. #ifndef UA_ENABLE_TYPEDESCRIPTION
  630. UA_snprintf(name, 20, "%02d - %i", type, scale_i);
  631. #else
  632. UA_snprintf(name, 20, "%s - %i", UA_TYPES[type].typeName, scale_i);
  633. #endif
  634. attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
  635. UA_QualifiedName qualifiedName = UA_QUALIFIEDNAME(1, name);
  636. UA_Server_addDataSourceVariableNode(server, UA_NODEID_NUMERIC(1, ++scale_nodeid),
  637. UA_NODEID_NUMERIC(1, SCALETESTID),
  638. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),qualifiedName,
  639. baseDataVariableType, attr, scaleTestDataSource, NULL, NULL);
  640. scale_i++;
  641. }
  642. UA_Variant_clear(&attr.value);
  643. }
  644. /* Add the variable to some more places to get a node with three inverse references for the CTT */
  645. UA_ExpandedNodeId answer_nodeid = UA_EXPANDEDNODEID_STRING(1, "the.answer");
  646. UA_Server_addReference(server, UA_NODEID_NUMERIC(1, DEMOID),
  647. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), answer_nodeid, true);
  648. UA_Server_addReference(server, UA_NODEID_NUMERIC(1, SCALARID),
  649. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), answer_nodeid, true);
  650. /* Example for manually setting an attribute within the server */
  651. UA_LocalizedText objectsName = UA_LOCALIZEDTEXT("en-US", "Objects");
  652. UA_Server_writeDisplayName(server, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), objectsName);
  653. #define NOARGID 60000
  654. #define INARGID 60001
  655. #define OUTARGID 60002
  656. #define INOUTARGID 60003
  657. #ifdef UA_ENABLE_METHODCALLS
  658. /* adding some more method nodes to pass CTT */
  659. /* Method without arguments */
  660. addmethodattributes = UA_MethodAttributes_default;
  661. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "noarg");
  662. addmethodattributes.executable = true;
  663. addmethodattributes.userExecutable = true;
  664. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, NOARGID),
  665. UA_NODEID_NUMERIC(1, DEMOID),
  666. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  667. UA_QUALIFIEDNAME(1, "noarg"), addmethodattributes,
  668. &noargMethod, /* callback of the method node */
  669. 0, NULL, 0, NULL, NULL, NULL);
  670. /* Method with in arguments */
  671. addmethodattributes = UA_MethodAttributes_default;
  672. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "inarg");
  673. addmethodattributes.executable = true;
  674. addmethodattributes.userExecutable = true;
  675. UA_Argument_init(&inputArguments);
  676. inputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  677. inputArguments.description = UA_LOCALIZEDTEXT("en-US", "Input");
  678. inputArguments.name = UA_STRING("Input");
  679. inputArguments.valueRank = UA_VALUERANK_SCALAR; //uaexpert will crash if set to 0 ;)
  680. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, INARGID),
  681. UA_NODEID_NUMERIC(1, DEMOID),
  682. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  683. UA_QUALIFIEDNAME(1, "noarg"), addmethodattributes,
  684. &noargMethod, /* callback of the method node */
  685. 1, &inputArguments, 0, NULL, NULL, NULL);
  686. /* Method with out arguments */
  687. addmethodattributes = UA_MethodAttributes_default;
  688. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "outarg");
  689. addmethodattributes.executable = true;
  690. addmethodattributes.userExecutable = true;
  691. UA_Argument_init(&outputArguments);
  692. outputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  693. outputArguments.description = UA_LOCALIZEDTEXT("en-US", "Output");
  694. outputArguments.name = UA_STRING("Output");
  695. outputArguments.valueRank = UA_VALUERANK_SCALAR;
  696. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, OUTARGID),
  697. UA_NODEID_NUMERIC(1, DEMOID),
  698. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  699. UA_QUALIFIEDNAME(1, "outarg"), addmethodattributes,
  700. &outargMethod, /* callback of the method node */
  701. 0, NULL, 1, &outputArguments, NULL, NULL);
  702. /* Method with inout arguments */
  703. addmethodattributes = UA_MethodAttributes_default;
  704. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "inoutarg");
  705. addmethodattributes.executable = true;
  706. addmethodattributes.userExecutable = true;
  707. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, INOUTARGID),
  708. UA_NODEID_NUMERIC(1, DEMOID),
  709. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  710. UA_QUALIFIEDNAME(1, "inoutarg"), addmethodattributes,
  711. &outargMethod, /* callback of the method node */
  712. 1, &inputArguments, 1, &outputArguments, NULL, NULL);
  713. #endif
  714. }
  715. static void
  716. disableAnonymous(UA_ServerConfig *config) {
  717. for(size_t i = 0; i < config->endpointsSize; i++) {
  718. UA_EndpointDescription *ep = &config->endpoints[i];
  719. for(size_t j = 0; j < ep->userIdentityTokensSize; j++) {
  720. UA_UserTokenPolicy *utp = &ep->userIdentityTokens[j];
  721. if(utp->tokenType != UA_USERTOKENTYPE_ANONYMOUS)
  722. continue;
  723. UA_UserTokenPolicy_clear(utp);
  724. /* Move the last to this position */
  725. if(j + 1 < ep->userIdentityTokensSize) {
  726. ep->userIdentityTokens[j] = ep->userIdentityTokens[ep->userIdentityTokensSize-1];
  727. j--;
  728. }
  729. ep->userIdentityTokensSize--;
  730. }
  731. /* Delete the entire array if the last UserTokenPolicy was removed */
  732. if(ep->userIdentityTokensSize == 0) {
  733. UA_free(ep->userIdentityTokens);
  734. ep->userIdentityTokens = NULL;
  735. }
  736. }
  737. }
  738. #ifdef UA_ENABLE_ENCRYPTION
  739. static void
  740. disableUnencrypted(UA_ServerConfig *config) {
  741. for(size_t i = 0; i < config->endpointsSize; i++) {
  742. UA_EndpointDescription *ep = &config->endpoints[i];
  743. if(ep->securityMode != UA_MESSAGESECURITYMODE_NONE)
  744. continue;
  745. UA_EndpointDescription_clear(ep);
  746. /* Move the last to this position */
  747. if(i + 1 < config->endpointsSize) {
  748. config->endpoints[i] = config->endpoints[config->endpointsSize-1];
  749. i--;
  750. }
  751. config->endpointsSize--;
  752. }
  753. /* Delete the entire array if the last Endpoint was removed */
  754. if(config->endpointsSize== 0) {
  755. UA_free(config->endpoints);
  756. config->endpoints = NULL;
  757. }
  758. }
  759. static void
  760. disableOutdatedSecurityPolicy(UA_ServerConfig *config) {
  761. for(size_t i = 0; i < config->endpointsSize; i++) {
  762. UA_EndpointDescription *ep = &config->endpoints[i];
  763. UA_ByteString basic128uri = UA_BYTESTRING("http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15");
  764. UA_ByteString basic256uri = UA_BYTESTRING("http://opcfoundation.org/UA/SecurityPolicy#Basic256");
  765. if(!UA_String_equal(&ep->securityPolicyUri, &basic128uri) &&
  766. !UA_String_equal(&ep->securityPolicyUri, &basic256uri))
  767. continue;
  768. UA_EndpointDescription_clear(ep);
  769. /* Move the last to this position */
  770. if(i + 1 < config->endpointsSize) {
  771. config->endpoints[i] = config->endpoints[config->endpointsSize-1];
  772. i--;
  773. }
  774. config->endpointsSize--;
  775. }
  776. /* Delete the entire array if the last Endpoint was removed */
  777. if(config->endpointsSize== 0) {
  778. UA_free(config->endpoints);
  779. config->endpoints = NULL;
  780. }
  781. }
  782. static void
  783. disableBasic128SecurityPolicy(UA_ServerConfig *config) {
  784. for(size_t i = 0; i < config->endpointsSize; i++) {
  785. UA_EndpointDescription *ep = &config->endpoints[i];
  786. UA_ByteString basic128uri = UA_BYTESTRING("http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15");
  787. if(!UA_String_equal(&ep->securityPolicyUri, &basic128uri))
  788. continue;
  789. UA_EndpointDescription_clear(ep);
  790. /* Move the last to this position */
  791. if(i + 1 < config->endpointsSize) {
  792. config->endpoints[i] = config->endpoints[config->endpointsSize-1];
  793. i--;
  794. }
  795. config->endpointsSize--;
  796. }
  797. /* Delete the entire array if the last Endpoint was removed */
  798. if(config->endpointsSize== 0) {
  799. UA_free(config->endpoints);
  800. config->endpoints = NULL;
  801. }
  802. }
  803. static void
  804. disableBasic256SecurityPolicy(UA_ServerConfig *config) {
  805. for(size_t i = 0; i < config->endpointsSize; i++) {
  806. UA_EndpointDescription *ep = &config->endpoints[i];
  807. UA_ByteString basic256uri = UA_BYTESTRING("http://opcfoundation.org/UA/SecurityPolicy#Basic256");
  808. if(!UA_String_equal(&ep->securityPolicyUri, &basic256uri))
  809. continue;
  810. UA_EndpointDescription_clear(ep);
  811. /* Move the last to this position */
  812. if(i + 1 < config->endpointsSize) {
  813. config->endpoints[i] = config->endpoints[config->endpointsSize-1];
  814. i--;
  815. }
  816. config->endpointsSize--;
  817. }
  818. /* Delete the entire array if the last Endpoint was removed */
  819. if(config->endpointsSize== 0) {
  820. UA_free(config->endpoints);
  821. config->endpoints = NULL;
  822. }
  823. }
  824. static void
  825. disableBasic256Sha256SecurityPolicy(UA_ServerConfig *config) {
  826. for(size_t i = 0; i < config->endpointsSize; i++) {
  827. UA_EndpointDescription *ep = &config->endpoints[i];
  828. UA_ByteString basic256sha256uri = UA_BYTESTRING("http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256");
  829. if(!UA_String_equal(&ep->securityPolicyUri, &basic256sha256uri))
  830. continue;
  831. UA_EndpointDescription_clear(ep);
  832. /* Move the last to this position */
  833. if(i + 1 < config->endpointsSize) {
  834. config->endpoints[i] = config->endpoints[config->endpointsSize-1];
  835. i--;
  836. }
  837. config->endpointsSize--;
  838. }
  839. /* Delete the entire array if the last Endpoint was removed */
  840. if(config->endpointsSize== 0) {
  841. UA_free(config->endpoints);
  842. config->endpoints = NULL;
  843. }
  844. }
  845. #endif
  846. UA_Boolean running = true;
  847. static void
  848. stopHandler(int sign) {
  849. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Received Ctrl-C");
  850. running = 0;
  851. }
  852. static void
  853. usage(void) {
  854. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  855. "Usage:\n"
  856. #ifndef UA_ENABLE_ENCRYPTION
  857. "server_ctt [<server-certificate.der>]\n"
  858. #else
  859. "server_ctt <server-certificate.der> <private-key.der>\n"
  860. #ifndef __linux__
  861. "\t[--trustlist <tl1.ctl> <tl2.ctl> ... ]\n"
  862. "\t[--issuerlist <il1.der> <il2.der> ... ]\n"
  863. "\t[--revocationlist <rv1.crl> <rv2.crl> ...]\n"
  864. #else
  865. "\t[--trustlistFolder <folder>]\n"
  866. "\t[--issuerlistFolder <folder>]\n"
  867. "\t[--revocationlistFolder <folder>]\n"
  868. #endif
  869. "\t[--enableUnencrypted]\n"
  870. "\t[--enableOutdatedSecurityPolicy]\n"
  871. "\t[--enableTimestampCheck]\n"
  872. "\t[--disableBasic128]\n"
  873. "\t[--disableBasic256]\n"
  874. "\t[--disableBasic256Sha256]\n"
  875. #endif
  876. "\t[--enableAnonymous]\n");
  877. }
  878. int main(int argc, char **argv) {
  879. signal(SIGINT, stopHandler); /* catches ctrl-c */
  880. signal(SIGTERM, stopHandler);
  881. for(int i = 1; i < argc; i++) {
  882. if(strcmp(argv[i], "--help") == 0 ||
  883. strcmp(argv[i], "-h") == 0) {
  884. usage();
  885. return EXIT_SUCCESS;
  886. }
  887. }
  888. UA_ServerConfig config;
  889. memset(&config, 0, sizeof(UA_ServerConfig));
  890. /* Load certificate */
  891. size_t pos = 1;
  892. UA_ByteString certificate = UA_BYTESTRING_NULL;
  893. if((size_t)argc >= pos + 1) {
  894. certificate = loadFile(argv[1]);
  895. if(certificate.length == 0) {
  896. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  897. "Unable to load file %s.", argv[pos]);
  898. return EXIT_FAILURE;
  899. }
  900. pos++;
  901. }
  902. #ifdef UA_ENABLE_ENCRYPTION
  903. /* Load the private key */
  904. UA_ByteString privateKey = UA_BYTESTRING_NULL;
  905. if((size_t)argc >= pos + 1) {
  906. privateKey = loadFile(argv[2]);
  907. if(privateKey.length == 0) {
  908. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  909. "Unable to load file %s.", argv[pos]);
  910. return EXIT_FAILURE;
  911. }
  912. pos++;
  913. }
  914. char filetype = ' '; /* t==trustlist, l == issuerList, r==revocationlist */
  915. UA_Boolean enableUnencr = false;
  916. UA_Boolean enableSec = false;
  917. UA_Boolean enableTime = false;
  918. UA_Boolean disableBasic128 = false;
  919. UA_Boolean disableBasic256 = false;
  920. UA_Boolean disableBasic256Sha256 = false;
  921. #ifndef __linux__
  922. UA_ByteString trustList[100];
  923. size_t trustListSize = 0;
  924. UA_ByteString issuerList[100];
  925. size_t issuerListSize = 0;
  926. UA_ByteString revocationList[100];
  927. size_t revocationListSize = 0;
  928. #else
  929. const char *trustlistFolder = NULL;
  930. const char *issuerlistFolder = NULL;
  931. const char *revocationlistFolder = NULL;
  932. #endif /* __linux__ */
  933. #endif /* UA_ENABLE_ENCRYPTION */
  934. UA_Boolean enableAnon = false;
  935. /* Loop over the remaining arguments */
  936. for(; pos < (size_t)argc; pos++) {
  937. if(strcmp(argv[pos], "--enableAnonymous") == 0) {
  938. enableAnon = true;
  939. continue;
  940. }
  941. #ifdef UA_ENABLE_ENCRYPTION
  942. if(strcmp(argv[pos], "--enableUnencrypted") == 0) {
  943. enableUnencr = true;
  944. continue;
  945. }
  946. if(strcmp(argv[pos], "--enableOutdatedSecurityPolicy") == 0) {
  947. enableSec = true;
  948. continue;
  949. }
  950. if(strcmp(argv[pos], "--enableTimestampCheck") == 0) {
  951. enableTime = true;
  952. continue;
  953. }
  954. if(strcmp(argv[pos], "--disableBasic128") == 0) {
  955. disableBasic128 = true;
  956. continue;
  957. }
  958. if(strcmp(argv[pos], "--disableBasic256") == 0) {
  959. disableBasic256 = true;
  960. continue;
  961. }
  962. if(strcmp(argv[pos], "--disableBasic256Sha256") == 0) {
  963. disableBasic256Sha256 = true;
  964. continue;
  965. }
  966. if(strcmp(argv[pos], "--disableBasic128") == 0) {
  967. disableBasic128 = true;
  968. continue;
  969. }
  970. if(strcmp(argv[pos], "--disableBasic256") == 0) {
  971. disableBasic256 = true;
  972. continue;
  973. }
  974. if(strcmp(argv[pos], "--disableBasic256Sha256") == 0) {
  975. disableBasic256Sha256 = true;
  976. continue;
  977. }
  978. #ifndef __linux__
  979. if(strcmp(argv[pos], "--trustlist") == 0) {
  980. filetype = 't';
  981. continue;
  982. }
  983. if(strcmp(argv[pos], "--issuerlist") == 0) {
  984. filetype = 'l';
  985. continue;
  986. }
  987. if(strcmp(argv[pos], "--revocationlist") == 0) {
  988. filetype = 'r';
  989. continue;
  990. }
  991. if(filetype == 't') {
  992. if(trustListSize >= 100) {
  993. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  994. "Too many trust lists");
  995. return EXIT_FAILURE;
  996. }
  997. trustList[trustListSize] = loadFile(argv[pos]);
  998. if(trustList[trustListSize].data == NULL) {
  999. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  1000. "Unable to load trust list %s", argv[pos]);
  1001. return EXIT_FAILURE;
  1002. }
  1003. trustListSize++;
  1004. continue;
  1005. }
  1006. if(filetype == 'l') {
  1007. if(issuerListSize >= 100) {
  1008. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  1009. "Too many trust lists");
  1010. return EXIT_FAILURE;
  1011. }
  1012. issuerList[issuerListSize] = loadFile(argv[pos]);
  1013. if(issuerList[issuerListSize].data == NULL) {
  1014. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  1015. "Unable to load trust list %s", argv[pos]);
  1016. return EXIT_FAILURE;
  1017. }
  1018. issuerListSize++;
  1019. continue;
  1020. }
  1021. if(filetype == 'r') {
  1022. if(revocationListSize >= 100) {
  1023. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  1024. "Too many revocation lists");
  1025. return EXIT_FAILURE;
  1026. }
  1027. revocationList[revocationListSize] = loadFile(argv[pos]);
  1028. if(revocationList[revocationListSize].data == NULL) {
  1029. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  1030. "Unable to load revocationlist %s", argv[pos]);
  1031. return EXIT_FAILURE;
  1032. }
  1033. revocationListSize++;
  1034. continue;
  1035. }
  1036. #else /* __linux__ */
  1037. if(strcmp(argv[pos], "--trustlistFolder") == 0) {
  1038. filetype = 't';
  1039. continue;
  1040. }
  1041. if(strcmp(argv[pos], "--issuerlistFolder") == 0) {
  1042. filetype = 'l';
  1043. continue;
  1044. }
  1045. if(strcmp(argv[pos], "--revocationlistFolder") == 0) {
  1046. filetype = 'r';
  1047. continue;
  1048. }
  1049. if(filetype == 't') {
  1050. trustlistFolder = argv[pos];
  1051. continue;
  1052. }
  1053. if(filetype == 'l') {
  1054. issuerlistFolder = argv[pos];
  1055. continue;
  1056. }
  1057. if(filetype == 'r') {
  1058. revocationlistFolder = argv[pos];
  1059. continue;
  1060. }
  1061. #endif /* __linux__ */
  1062. #endif /* UA_ENABLE_ENCRYPTION */
  1063. usage();
  1064. return EXIT_FAILURE;
  1065. }
  1066. #ifdef UA_ENABLE_ENCRYPTION
  1067. #ifndef __linux__
  1068. UA_ServerConfig_setDefaultWithSecurityPolicies(&config, 4840,
  1069. &certificate, &privateKey,
  1070. trustList, trustListSize,
  1071. issuerList, issuerListSize,
  1072. revocationList, revocationListSize);
  1073. #else /* __linux__ */
  1074. UA_ServerConfig_setDefaultWithSecurityPolicies(&config, 4840,
  1075. &certificate, &privateKey,
  1076. NULL, 0, NULL, 0, NULL, 0);
  1077. config.certificateVerification.clear(&config.certificateVerification);
  1078. UA_CertificateVerification_CertFolders(&config.certificateVerification,
  1079. trustlistFolder, issuerlistFolder,
  1080. revocationlistFolder);
  1081. #endif /* __linux__ */
  1082. if(!enableUnencr)
  1083. disableUnencrypted(&config);
  1084. if(!enableSec)
  1085. disableOutdatedSecurityPolicy(&config);
  1086. if(disableBasic128)
  1087. disableBasic128SecurityPolicy(&config);
  1088. if(disableBasic256)
  1089. disableBasic256SecurityPolicy(&config);
  1090. if(disableBasic256Sha256)
  1091. disableBasic256Sha256SecurityPolicy(&config);
  1092. /* Set operation limits */
  1093. config.maxNodesPerRead = MAX_OPERATION_LIMIT;
  1094. config.maxNodesPerWrite = MAX_OPERATION_LIMIT;
  1095. config.maxNodesPerMethodCall = MAX_OPERATION_LIMIT;
  1096. config.maxNodesPerBrowse = MAX_OPERATION_LIMIT;
  1097. config.maxNodesPerRegisterNodes = MAX_OPERATION_LIMIT;
  1098. config.maxNodesPerTranslateBrowsePathsToNodeIds = MAX_OPERATION_LIMIT;
  1099. config.maxNodesPerNodeManagement = MAX_OPERATION_LIMIT;
  1100. config.maxMonitoredItemsPerCall = MAX_OPERATION_LIMIT;
  1101. /* If RequestTimestamp is '0', log the warning and proceed */
  1102. config.verifyRequestTimestamp = UA_RULEHANDLING_WARN;
  1103. if(enableTime)
  1104. config.verifyRequestTimestamp = UA_RULEHANDLING_DEFAULT;
  1105. #else /* UA_ENABLE_ENCRYPTION */
  1106. UA_ServerConfig_setMinimal(&config, 4840, &certificate);
  1107. #endif /* UA_ENABLE_ENCRYPTION */
  1108. if(!enableAnon)
  1109. disableAnonymous(&config);
  1110. /* Clean up temp values */
  1111. UA_ByteString_clear(&certificate);
  1112. #if defined(UA_ENABLE_ENCRYPTION) && !defined(__linux__)
  1113. UA_ByteString_clear(&privateKey);
  1114. for(size_t i = 0; i < trustListSize; i++)
  1115. UA_ByteString_clear(&trustList[i]);
  1116. for(size_t i = 0; i < issuerListSize; i++)
  1117. UA_ByteString_clear(&issuerList[i]);
  1118. for(size_t i = 0; i < revocationListSize; i++)
  1119. UA_ByteString_clear(&revocationList[i]);
  1120. #endif
  1121. /* Override with a custom access control policy */
  1122. config.accessControl.getUserAccessLevel = getUserAccessLevel_disallowSpecific;
  1123. UA_String_clear(&config.applicationDescription.applicationUri);
  1124. config.applicationDescription.applicationUri =
  1125. UA_String_fromChars("urn:open62541.server.application");
  1126. config.shutdownDelay = 5000.0; /* 5s */
  1127. UA_Server *server = UA_Server_newWithConfig(&config);
  1128. if(server == NULL)
  1129. return EXIT_FAILURE;
  1130. setInformationModel(server);
  1131. /* run server */
  1132. UA_StatusCode retval = UA_Server_run(server, &running);
  1133. UA_Server_delete(server);
  1134. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  1135. }