server_ctt.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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[--disableBasic128]\n"
  872. "\t[--disableBasic256]\n"
  873. "\t[--disableBasic256Sha256]\n"
  874. #endif
  875. "\t[--enableTimestampCheck]\n"
  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 disableBasic128 = false;
  918. UA_Boolean disableBasic256 = false;
  919. UA_Boolean disableBasic256Sha256 = false;
  920. #ifndef __linux__
  921. UA_ByteString trustList[100];
  922. size_t trustListSize = 0;
  923. UA_ByteString issuerList[100];
  924. size_t issuerListSize = 0;
  925. UA_ByteString revocationList[100];
  926. size_t revocationListSize = 0;
  927. #else
  928. const char *trustlistFolder = NULL;
  929. const char *issuerlistFolder = NULL;
  930. const char *revocationlistFolder = NULL;
  931. #endif /* __linux__ */
  932. #endif /* UA_ENABLE_ENCRYPTION */
  933. UA_Boolean enableAnon = false;
  934. UA_Boolean enableTime = 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. if(strcmp(argv[pos], "--enableTimestampCheck") == 0) {
  942. enableTime = true;
  943. continue;
  944. }
  945. #ifdef UA_ENABLE_ENCRYPTION
  946. if(strcmp(argv[pos], "--enableUnencrypted") == 0) {
  947. enableUnencr = true;
  948. continue;
  949. }
  950. if(strcmp(argv[pos], "--enableOutdatedSecurityPolicy") == 0) {
  951. enableSec = 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. UA_Server *server = NULL;
  1067. #ifdef UA_ENABLE_ENCRYPTION
  1068. #ifndef __linux__
  1069. UA_StatusCode res =
  1070. UA_ServerConfig_setDefaultWithSecurityPolicies(&config, 4840,
  1071. &certificate, &privateKey,
  1072. trustList, trustListSize,
  1073. issuerList, issuerListSize,
  1074. revocationList, revocationListSize);
  1075. if(res != UA_STATUSCODE_GOOD)
  1076. goto cleanup;
  1077. #else /* On Linux we can monitor the certs folder and reload when changes are made */
  1078. UA_StatusCode res =
  1079. UA_ServerConfig_setDefaultWithSecurityPolicies(&config, 4840,
  1080. &certificate, &privateKey,
  1081. NULL, 0, NULL, 0, NULL, 0);
  1082. if(res != UA_STATUSCODE_GOOD)
  1083. goto cleanup;
  1084. config.certificateVerification.clear(&config.certificateVerification);
  1085. res = UA_CertificateVerification_CertFolders(&config.certificateVerification,
  1086. trustlistFolder, issuerlistFolder,
  1087. revocationlistFolder);
  1088. if(res != UA_STATUSCODE_GOOD)
  1089. goto cleanup;
  1090. #endif /* __linux__ */
  1091. if(!enableUnencr)
  1092. disableUnencrypted(&config);
  1093. if(!enableSec)
  1094. disableOutdatedSecurityPolicy(&config);
  1095. if(disableBasic128)
  1096. disableBasic128SecurityPolicy(&config);
  1097. if(disableBasic256)
  1098. disableBasic256SecurityPolicy(&config);
  1099. if(disableBasic256Sha256)
  1100. disableBasic256Sha256SecurityPolicy(&config);
  1101. #else /* UA_ENABLE_ENCRYPTION */
  1102. UA_StatusCode res =
  1103. UA_ServerConfig_setMinimal(&config, 4840, &certificate);
  1104. if(res != UA_STATUSCODE_GOOD)
  1105. goto cleanup;
  1106. #endif /* UA_ENABLE_ENCRYPTION */
  1107. if(!enableAnon)
  1108. disableAnonymous(&config);
  1109. /* Set operation limits */
  1110. config.maxNodesPerRead = MAX_OPERATION_LIMIT;
  1111. config.maxNodesPerWrite = MAX_OPERATION_LIMIT;
  1112. config.maxNodesPerMethodCall = MAX_OPERATION_LIMIT;
  1113. config.maxNodesPerBrowse = MAX_OPERATION_LIMIT;
  1114. config.maxNodesPerRegisterNodes = MAX_OPERATION_LIMIT;
  1115. config.maxNodesPerTranslateBrowsePathsToNodeIds = MAX_OPERATION_LIMIT;
  1116. config.maxNodesPerNodeManagement = MAX_OPERATION_LIMIT;
  1117. config.maxMonitoredItemsPerCall = MAX_OPERATION_LIMIT;
  1118. /* If RequestTimestamp is '0', log the warning and proceed */
  1119. config.verifyRequestTimestamp = UA_RULEHANDLING_WARN;
  1120. if(enableTime)
  1121. config.verifyRequestTimestamp = UA_RULEHANDLING_DEFAULT;
  1122. /* Override with a custom access control policy */
  1123. config.accessControl.getUserAccessLevel = getUserAccessLevel_disallowSpecific;
  1124. UA_String_clear(&config.applicationDescription.applicationUri);
  1125. config.applicationDescription.applicationUri =
  1126. UA_String_fromChars("urn:open62541.server.application");
  1127. config.shutdownDelay = 5000.0; /* 5s */
  1128. server = UA_Server_newWithConfig(&config);
  1129. if(!server) {
  1130. res = UA_STATUSCODE_BADINTERNALERROR;
  1131. goto cleanup;
  1132. }
  1133. setInformationModel(server);
  1134. /* run server */
  1135. res = UA_Server_run(server, &running);
  1136. cleanup:
  1137. if(server)
  1138. UA_Server_delete(server);
  1139. else
  1140. UA_ServerConfig_clean(&config);
  1141. UA_ByteString_clear(&certificate);
  1142. #if defined(UA_ENABLE_ENCRYPTION)
  1143. UA_ByteString_clear(&privateKey);
  1144. #ifndef __linux__
  1145. for(size_t i = 0; i < trustListSize; i++)
  1146. UA_ByteString_clear(&trustList[i]);
  1147. for(size_t i = 0; i < issuerListSize; i++)
  1148. UA_ByteString_clear(&issuerList[i]);
  1149. for(size_t i = 0; i < revocationListSize; i++)
  1150. UA_ByteString_clear(&revocationList[i]);
  1151. #endif
  1152. #endif
  1153. return res == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  1154. }