ua_config_default.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #include "ua_config_default.h"
  4. #include "ua_log_stdout.h"
  5. #include "ua_network_tcp.h"
  6. #include "ua_accesscontrol_default.h"
  7. #include "ua_nodestore_default.h"
  8. #include "ua_types_generated.h"
  9. #include "ua_securitypolicy_none.h"
  10. #include "ua_types.h"
  11. #include "ua_types_generated_handling.h"
  12. #include "ua_client_highlevel.h"
  13. #define ANONYMOUS_POLICY "open62541-anonymous-policy"
  14. #define USERNAME_POLICY "open62541-username-policy"
  15. /* Struct initialization works across ANSI C/C99/C++ if it is done when the
  16. * variable is first declared. Assigning values to existing structs is
  17. * heterogeneous across the three. */
  18. static UA_INLINE UA_UInt32Range
  19. UA_UINT32RANGE(UA_UInt32 min, UA_UInt32 max) {
  20. UA_UInt32Range range = {min, max};
  21. return range;
  22. }
  23. static UA_INLINE UA_DurationRange
  24. UA_DURATIONRANGE(UA_Double min, UA_Double max) {
  25. UA_DurationRange range = {min, max};
  26. return range;
  27. }
  28. /*******************************/
  29. /* Default Connection Settings */
  30. /*******************************/
  31. const UA_ConnectionConfig UA_ConnectionConfig_default = {
  32. 0, /* .protocolVersion */
  33. 65535, /* .sendBufferSize, 64k per chunk */
  34. 65535, /* .recvBufferSize, 64k per chunk */
  35. 0, /* .maxMessageSize, 0 -> unlimited */
  36. 0 /* .maxChunkCount, 0 -> unlimited */
  37. };
  38. /***************************/
  39. /* Default Server Settings */
  40. /***************************/
  41. #define MANUFACTURER_NAME "open62541"
  42. #define PRODUCT_NAME "open62541 OPC UA Server"
  43. #define PRODUCT_URI "http://open62541.org"
  44. #define APPLICATION_NAME "open62541-based OPC UA Application"
  45. #define APPLICATION_URI "urn:unconfigured:application"
  46. #define STRINGIFY(arg) #arg
  47. #define VERSION(MAJOR, MINOR, PATCH, LABEL) \
  48. STRINGIFY(MAJOR) "." STRINGIFY(MINOR) "." STRINGIFY(PATCH) LABEL
  49. static UA_StatusCode
  50. createSecurityPolicyNoneEndpoint(UA_ServerConfig *conf, UA_Endpoint *endpoint,
  51. const UA_ByteString localCertificate) {
  52. UA_EndpointDescription_init(&endpoint->endpointDescription);
  53. UA_SecurityPolicy_None(&endpoint->securityPolicy, localCertificate, conf->logger);
  54. endpoint->endpointDescription.securityMode = UA_MESSAGESECURITYMODE_NONE;
  55. endpoint->endpointDescription.securityPolicyUri =
  56. UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#None");
  57. endpoint->endpointDescription.transportProfileUri =
  58. UA_STRING_ALLOC("http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary");
  59. /* enable anonymous and username/password */
  60. size_t policies = 2;
  61. endpoint->endpointDescription.userIdentityTokens = (UA_UserTokenPolicy*)
  62. UA_Array_new(policies, &UA_TYPES[UA_TYPES_USERTOKENPOLICY]);
  63. if(!endpoint->endpointDescription.userIdentityTokens)
  64. return UA_STATUSCODE_BADOUTOFMEMORY;
  65. endpoint->endpointDescription.userIdentityTokensSize = policies;
  66. endpoint->endpointDescription.userIdentityTokens[0].tokenType =
  67. UA_USERTOKENTYPE_ANONYMOUS;
  68. endpoint->endpointDescription.userIdentityTokens[0].policyId =
  69. UA_STRING_ALLOC(ANONYMOUS_POLICY);
  70. endpoint->endpointDescription.userIdentityTokens[1].tokenType =
  71. UA_USERTOKENTYPE_USERNAME;
  72. endpoint->endpointDescription.userIdentityTokens[1].policyId =
  73. UA_STRING_ALLOC(USERNAME_POLICY);
  74. UA_String_copy(&localCertificate, &endpoint->endpointDescription.serverCertificate);
  75. UA_ApplicationDescription_copy(&conf->applicationDescription,
  76. &endpoint->endpointDescription.server);
  77. return UA_STATUSCODE_GOOD;
  78. }
  79. void
  80. UA_ServerConfig_set_customHostname(UA_ServerConfig *config, const UA_String customHostname){
  81. if(!config)
  82. return;
  83. UA_String_deleteMembers(&config->customHostname);
  84. UA_String_copy(&customHostname, &config->customHostname);
  85. }
  86. UA_ServerConfig *
  87. UA_ServerConfig_new_minimal(UA_UInt16 portNumber,
  88. const UA_ByteString *certificate) {
  89. UA_ServerConfig *conf = (UA_ServerConfig*)UA_malloc(sizeof(UA_ServerConfig));
  90. if(!conf)
  91. return NULL;
  92. /* Zero out.. All members have a valid initial value */
  93. memset(conf, 0, sizeof(UA_ServerConfig));
  94. /* --> Start setting the default static config <-- */
  95. conf->nThreads = 1;
  96. conf->logger = UA_Log_Stdout;
  97. /* Server Description */
  98. conf->buildInfo.productUri = UA_STRING_ALLOC(PRODUCT_URI);
  99. conf->buildInfo.manufacturerName = UA_STRING_ALLOC(MANUFACTURER_NAME);
  100. conf->buildInfo.productName = UA_STRING_ALLOC(PRODUCT_NAME);
  101. conf->buildInfo.softwareVersion =
  102. UA_STRING_ALLOC(VERSION(UA_OPEN62541_VER_MAJOR, UA_OPEN62541_VER_MINOR,
  103. UA_OPEN62541_VER_PATCH, UA_OPEN62541_VER_LABEL));
  104. conf->buildInfo.buildNumber = UA_STRING_ALLOC(__DATE__ " " __TIME__);
  105. conf->buildInfo.buildDate = 0;
  106. conf->applicationDescription.applicationUri = UA_STRING_ALLOC(APPLICATION_URI);
  107. conf->applicationDescription.productUri = UA_STRING_ALLOC(PRODUCT_URI);
  108. conf->applicationDescription.applicationName =
  109. UA_LOCALIZEDTEXT_ALLOC("en", APPLICATION_NAME);
  110. conf->applicationDescription.applicationType = UA_APPLICATIONTYPE_SERVER;
  111. /* conf->applicationDescription.gatewayServerUri = UA_STRING_NULL; */
  112. /* conf->applicationDescription.discoveryProfileUri = UA_STRING_NULL; */
  113. /* conf->applicationDescription.discoveryUrlsSize = 0; */
  114. /* conf->applicationDescription.discoveryUrls = NULL; */
  115. #ifdef UA_ENABLE_DISCOVERY
  116. /* conf->mdnsServerName = UA_STRING_NULL; */
  117. /* conf->serverCapabilitiesSize = 0; */
  118. /* conf->serverCapabilities = NULL; */
  119. #endif
  120. /* Custom DataTypes */
  121. /* conf->customDataTypesSize = 0; */
  122. /* conf->customDataTypes = NULL; */
  123. /* Networking */
  124. /* conf->networkLayersSize = 0; */
  125. /* conf->networkLayers = NULL; */
  126. /* conf->customHostname = UA_STRING_NULL; */
  127. /* Endpoints */
  128. /* conf->endpoints = {0, NULL}; */
  129. /* Global Node Lifecycle */
  130. conf->nodeLifecycle.constructor = NULL;
  131. conf->nodeLifecycle.destructor = NULL;
  132. /* Access Control */
  133. conf->accessControl.enableAnonymousLogin = true;
  134. conf->accessControl.enableUsernamePasswordLogin = true;
  135. conf->accessControl.activateSession = activateSession_default;
  136. conf->accessControl.closeSession = closeSession_default;
  137. conf->accessControl.getUserRightsMask = getUserRightsMask_default;
  138. conf->accessControl.getUserAccessLevel = getUserAccessLevel_default;
  139. conf->accessControl.getUserExecutable = getUserExecutable_default;
  140. conf->accessControl.getUserExecutableOnObject = getUserExecutableOnObject_default;
  141. conf->accessControl.allowAddNode = allowAddNode_default;
  142. conf->accessControl.allowAddReference = allowAddReference_default;
  143. conf->accessControl.allowDeleteNode = allowDeleteNode_default;
  144. conf->accessControl.allowDeleteReference = allowDeleteReference_default;
  145. /* Limits for SecureChannels */
  146. conf->maxSecureChannels = 40;
  147. conf->maxSecurityTokenLifetime = 10 * 60 * 1000; /* 10 minutes */
  148. /* Limits for Sessions */
  149. conf->maxSessions = 100;
  150. conf->maxSessionTimeout = 60.0 * 60.0 * 1000.0; /* 1h */
  151. /* Limits for Subscriptions */
  152. conf->publishingIntervalLimits = UA_DURATIONRANGE(100.0, 3600.0 * 1000.0);
  153. conf->lifeTimeCountLimits = UA_UINT32RANGE(3, 15000);
  154. conf->keepAliveCountLimits = UA_UINT32RANGE(1, 100);
  155. conf->maxNotificationsPerPublish = 1000;
  156. conf->maxRetransmissionQueueSize = 0; /* unlimited */
  157. /* Limits for MonitoredItems */
  158. conf->samplingIntervalLimits = UA_DURATIONRANGE(50.0, 24.0 * 3600.0 * 1000.0);
  159. conf->queueSizeLimits = UA_UINT32RANGE(1, 100);
  160. #ifdef UA_ENABLE_DISCOVERY
  161. conf->discoveryCleanupTimeout = 60 * 60;
  162. #endif
  163. /* --> Finish setting the default static config <-- */
  164. UA_StatusCode retval = UA_Nodestore_default_new(&conf->nodestore);
  165. if(retval != UA_STATUSCODE_GOOD) {
  166. UA_ServerConfig_delete(conf);
  167. return NULL;
  168. }
  169. /* Add a network layer */
  170. conf->networkLayers = (UA_ServerNetworkLayer*)
  171. UA_malloc(sizeof(UA_ServerNetworkLayer));
  172. if(!conf->networkLayers) {
  173. UA_ServerConfig_delete(conf);
  174. return NULL;
  175. }
  176. conf->networkLayers[0] =
  177. UA_ServerNetworkLayerTCP(UA_ConnectionConfig_default, portNumber);
  178. conf->networkLayersSize = 1;
  179. /* Allocate the endpoint */
  180. conf->endpointsSize = 1;
  181. conf->endpoints = (UA_Endpoint*)UA_malloc(sizeof(UA_Endpoint));
  182. if(!conf->endpoints) {
  183. UA_ServerConfig_delete(conf);
  184. return NULL;
  185. }
  186. /* Populate the endpoint */
  187. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  188. if(certificate)
  189. localCertificate = *certificate;
  190. retval =
  191. createSecurityPolicyNoneEndpoint(conf, &conf->endpoints[0], localCertificate);
  192. if(retval != UA_STATUSCODE_GOOD) {
  193. UA_ServerConfig_delete(conf);
  194. return NULL;
  195. }
  196. return conf;
  197. }
  198. void
  199. UA_ServerConfig_delete(UA_ServerConfig *config) {
  200. if(!config)
  201. return;
  202. /* Server Description */
  203. UA_BuildInfo_deleteMembers(&config->buildInfo);
  204. UA_ApplicationDescription_deleteMembers(&config->applicationDescription);
  205. #ifdef UA_ENABLE_DISCOVERY
  206. UA_String_deleteMembers(&config->mdnsServerName);
  207. UA_Array_delete(config->serverCapabilities, config->serverCapabilitiesSize,
  208. &UA_TYPES[UA_TYPES_STRING]);
  209. config->serverCapabilities = NULL;
  210. config->serverCapabilitiesSize = 0;
  211. #endif
  212. /* Nodestore */
  213. if(config->nodestore.deleteNodestore)
  214. config->nodestore.deleteNodestore(config->nodestore.context);
  215. /* Custom DataTypes */
  216. for(size_t i = 0; i < config->customDataTypesSize; ++i)
  217. UA_free(config->customDataTypes[i].members);
  218. UA_free(config->customDataTypes);
  219. config->customDataTypes = NULL;
  220. config->customDataTypesSize = 0;
  221. /* Networking */
  222. for(size_t i = 0; i < config->networkLayersSize; ++i)
  223. config->networkLayers[i].deleteMembers(&config->networkLayers[i]);
  224. UA_free(config->networkLayers);
  225. config->networkLayers = NULL;
  226. config->networkLayersSize = 0;
  227. UA_String_deleteMembers(&config->customHostname);
  228. config->customHostname = UA_STRING_NULL;
  229. for(size_t i = 0; i < config->endpointsSize; ++i) {
  230. UA_SecurityPolicy *policy = &config->endpoints[i].securityPolicy;
  231. policy->deleteMembers(policy);
  232. UA_EndpointDescription_deleteMembers(&config->endpoints[i].endpointDescription);
  233. }
  234. UA_free(config->endpoints);
  235. config->endpoints = NULL;
  236. config->endpointsSize = 0;
  237. UA_free(config);
  238. }
  239. /***************************/
  240. /* Default Client Settings */
  241. /***************************/
  242. const UA_ClientConfig UA_ClientConfig_default = {
  243. 5000, /* .timeout, 5 seconds */
  244. 10 * 60 * 1000, /* .secureChannelLifeTime, 10 minutes */
  245. UA_Log_Stdout, /* .logger */
  246. /* .localConnectionConfig */
  247. {0, /* .protocolVersion */
  248. 65535, /* .sendBufferSize, 64k per chunk */
  249. 65535, /* .recvBufferSize, 64k per chunk */
  250. 0, /* .maxMessageSize, 0 -> unlimited */
  251. 0}, /* .maxChunkCount, 0 -> unlimited */
  252. UA_ClientConnectionTCP, /* .connectionFunc */
  253. 0, /* .customDataTypesSize */
  254. NULL, /*.customDataTypes */
  255. NULL /*.stateCallback */
  256. };
  257. /****************************************/
  258. /* Default Client Subscription Settings */
  259. /****************************************/
  260. #ifdef UA_ENABLE_SUBSCRIPTIONS
  261. const UA_SubscriptionSettings UA_SubscriptionSettings_default = {
  262. 500.0, /* .requestedPublishingInterval */
  263. 10000, /* .requestedLifetimeCount */
  264. 1, /* .requestedMaxKeepAliveCount */
  265. 0, /* .maxNotificationsPerPublish */
  266. true, /* .publishingEnabled */
  267. 0 /* .priority */
  268. };
  269. #endif