server_ctt.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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. 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. /* Set operation limits */
  1102. config.maxNodesPerRead = MAX_OPERATION_LIMIT;
  1103. config.maxNodesPerWrite = MAX_OPERATION_LIMIT;
  1104. config.maxNodesPerMethodCall = MAX_OPERATION_LIMIT;
  1105. config.maxNodesPerBrowse = MAX_OPERATION_LIMIT;
  1106. config.maxNodesPerRegisterNodes = MAX_OPERATION_LIMIT;
  1107. config.maxNodesPerTranslateBrowsePathsToNodeIds = MAX_OPERATION_LIMIT;
  1108. config.maxNodesPerNodeManagement = MAX_OPERATION_LIMIT;
  1109. config.maxMonitoredItemsPerCall = MAX_OPERATION_LIMIT;
  1110. /* If RequestTimestamp is '0', log the warning and proceed */
  1111. config.verifyRequestTimestamp = UA_RULEHANDLING_WARN;
  1112. if(enableTime)
  1113. config.verifyRequestTimestamp = UA_RULEHANDLING_DEFAULT;
  1114. #else /* UA_ENABLE_ENCRYPTION */
  1115. UA_StatusCode res =
  1116. UA_ServerConfig_setMinimal(&config, 4840, &certificate);
  1117. #endif /* UA_ENABLE_ENCRYPTION */
  1118. if(!enableAnon)
  1119. disableAnonymous(&config);
  1120. /* Override with a custom access control policy */
  1121. config.accessControl.getUserAccessLevel = getUserAccessLevel_disallowSpecific;
  1122. UA_String_clear(&config.applicationDescription.applicationUri);
  1123. config.applicationDescription.applicationUri =
  1124. UA_String_fromChars("urn:open62541.server.application");
  1125. config.shutdownDelay = 5000.0; /* 5s */
  1126. server = UA_Server_newWithConfig(&config);
  1127. if(!server) {
  1128. res = UA_STATUSCODE_BADINTERNALERROR;
  1129. goto cleanup;
  1130. }
  1131. setInformationModel(server);
  1132. /* run server */
  1133. res = UA_Server_run(server, &running);
  1134. cleanup:
  1135. if(server)
  1136. UA_Server_delete(server);
  1137. else
  1138. UA_ServerConfig_clean(&config);
  1139. UA_ByteString_clear(&certificate);
  1140. #if defined(UA_ENABLE_ENCRYPTION)
  1141. UA_ByteString_clear(&privateKey);
  1142. #ifndef __linux__
  1143. for(size_t i = 0; i < trustListSize; i++)
  1144. UA_ByteString_clear(&trustList[i]);
  1145. for(size_t i = 0; i < issuerListSize; i++)
  1146. UA_ByteString_clear(&issuerList[i]);
  1147. for(size_t i = 0; i < revocationListSize; i++)
  1148. UA_ByteString_clear(&revocationList[i]);
  1149. #endif
  1150. #endif
  1151. return res == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  1152. }